summaryrefslogtreecommitdiff
path: root/stream-servers/ReadbackWorker.h
blob: b463cac1cd9d86a2abb4140f41eb3ffd2528a762 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <EGL/egl.h>                            // for EGLContext, EGLSurface
#include <GLES3/gl3.h>                          // for GLuint
#include <map>                                  // for map
#include <stdint.h>                             // for uint32_t
#include <vector>                               // for vector

#include "base/Compiler.h"              // for DISALLOW_COPY_AND_ASSIGN
#include "base/Lock.h"  // for Lock

class ColorBuffer;
class FrameBuffer;
struct RenderThreadInfo;

// This class implements async readback of emugl ColorBuffers.
// It is meant to run on both the emugl framebuffer posting thread
// and a separate GL thread, with two main points of interaction:
class ReadbackWorker {
public:
    ReadbackWorker() = default;
    ~ReadbackWorker();

    // GL initialization (must be on the thread that
    // will run getPixels)
    void initGL();

    // doNextReadback(): Call this from the emugl FrameBuffer::post thread
    // or similar rendering thread.
    // This will trigger an async glReadPixels of the current framebuffer.
    // The post callback of Framebuffer will also be triggered, but
    // in async mode it should do minimal work that involves |fbImage|.
    // |repaint|: flag to prime async readback with multiple iterations
    // so that the consumer of readback doesn't lag behind.
    // |readbackBgra|: Whether to force the readback format as GL_BGRA_EXT,
    // so that we get (depending on driver quality, heh) a gpu conversion of the
    // readback image that is suitable for webrtc, which expects formats like that.
    void doNextReadback(uint32_t displayId, ColorBuffer* cb, void* fbImage, bool repaint, bool readbackBgra);

    // getPixels(): Run this on a separate GL thread. This retrieves the
    // latest framebuffer that has been posted and read with doNextReadback.
    // This is meant for apps like video encoding to use as input; they will
    // need to do synchronized communication with the thread ReadbackWorker
    // is running on.
    void getPixels(uint32_t displayId, void* out, uint32_t bytes);

    // Duplicates the last frame and generates a post events if
    // there are no read events active.
    // This is usually called when there was no doNextReadback activity
    // for a few ms, to guarantee that end users see the final frame.
    void flushPipeline(uint32_t displayId);

    void setRecordDisplay(uint32_t displayId, uint32_t w, uint32_t h, bool add);

    class recordDisplay {
    public:
        recordDisplay() = default;
        recordDisplay(uint32_t displayId, uint32_t w, uint32_t h);
    public:
        uint32_t mReadPixelsIndexEven = 0;
        uint32_t mReadPixelsIndexOdd = 1;
        uint32_t mPrevReadPixelsIndex = 1;
        uint32_t mMapCopyIndex = 0;
        bool mIsCopying = false;
        uint32_t mBufferSize = 0;
        std::vector<GLuint> mBuffers = {};
        uint32_t m_readbackCount = 0;
        uint32_t mDisplayId = 0;
    };

private:
    EGLContext mContext;
    EGLContext mFlushContext;
    EGLSurface mSurf;
    EGLSurface mFlushSurf;

    FrameBuffer* mFb;
    android::base::Lock mLock;

    std::map<uint32_t, recordDisplay> mRecordDisplays;

    DISALLOW_COPY_AND_ASSIGN(ReadbackWorker);
};