• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #pragma once
17 
18 #include <map>
19 #include <stdint.h>
20 #include <vector>
21 
22 #include <EGL/egl.h>
23 #include <GLES3/gl3.h>
24 
25 #include "aemu/base/Compiler.h"
26 #include "aemu/base/synchronization/Lock.h"
27 #include "DisplaySurfaceGl.h"
28 #include "ReadbackWorker.h"
29 
30 namespace gfxstream {
31 class ColorBuffer;
32 }  // namespace gfxstream
33 
34 namespace gfxstream {
35 namespace gl {
36 
37 // This class implements async readback of emugl ColorBuffers.
38 // It is meant to run on both the emugl framebuffer posting thread
39 // and a separate GL thread, with two main points of interaction:
40 class ReadbackWorkerGl : public ReadbackWorker {
41   public:
42     ReadbackWorkerGl(std::unique_ptr<DisplaySurfaceGl> surface,
43                      std::unique_ptr<DisplaySurfaceGl> flushSurface);
44 
45     ~ReadbackWorkerGl();
46 
47     // GL initialization (must be on the thread that
48     // will run getPixels)
49     void init() override;
50 
51     // doNextReadback(): Call this from the emugl FrameBuffer::post thread
52     // or similar rendering thread.
53     // This will trigger an async glReadPixels of the current framebuffer.
54     // The post callback of Framebuffer will also be triggered, but
55     // in async mode it should do minimal work that involves |fbImage|.
56     // |repaint|: flag to prime async readback with multiple iterations
57     // so that the consumer of readback doesn't lag behind.
58     // |readbackBgra|: Whether to force the readback format as GL_BGRA_EXT,
59     // so that we get (depending on driver quality, heh) a gpu conversion of the
60     // readback image that is suitable for webrtc, which expects formats like that.
61     DoNextReadbackResult doNextReadback(uint32_t displayId,
62                                         ColorBuffer* cb,
63                                         void* fbImage,
64                                         bool repaint,
65                                         bool readbackBgra) override;
66 
67     // getPixels(): Run this on a separate GL thread. This retrieves the
68     // latest framebuffer that has been posted and read with doNextReadback.
69     // This is meant for apps like video encoding to use as input; they will
70     // need to do synchronized communication with the thread ReadbackWorker
71     // is running on.
72     void getPixels(uint32_t displayId, void* out, uint32_t bytes) override;
73 
74     // Duplicates the last frame and generates a post events if
75     // there are no read events active.
76     // This is usually called when there was no doNextReadback activity
77     // for a few ms, to guarantee that end users see the final frame.
78     FlushResult flushPipeline(uint32_t displayId) override;
79 
80     void initReadbackForDisplay(uint32_t displayId, uint32_t w, uint32_t h) override;
81 
82     void deinitReadbackForDisplay(uint32_t displayId) override;
83 
84     class TrackedDisplay {
85       public:
86         TrackedDisplay() = default;
87         TrackedDisplay(uint32_t displayId, uint32_t w, uint32_t h);
88 
89         uint32_t mReadPixelsIndexEven = 0;
90         uint32_t mReadPixelsIndexOdd = 1;
91         uint32_t mPrevReadPixelsIndex = 1;
92         uint32_t mMapCopyIndex = 0;
93         bool mIsCopying = false;
94         uint32_t mBufferSize = 0;
95         std::vector<GLuint> mBuffers = {};
96         uint32_t m_readbackCount = 0;
97         uint32_t mDisplayId = 0;
98     };
99 
100   private:
101     android::base::Lock mLock;
102 
103     std::unique_ptr<DisplaySurfaceGl> mSurface;
104     std::unique_ptr<DisplaySurfaceGl> mFlushSurface;
105 
106     std::map<uint32_t, TrackedDisplay> mTrackedDisplays;
107 
108     DISALLOW_COPY_AND_ASSIGN(ReadbackWorkerGl);
109 };
110 
111 }  // namespace gl
112 }  // namespace gfxstream
113