• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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 
17 #ifndef SF_GLESRENDERENGINE_H_
18 #define SF_GLESRENDERENGINE_H_
19 
20 #include <android-base/thread_annotations.h>
21 #include <stdint.h>
22 #include <sys/types.h>
23 #include <condition_variable>
24 #include <deque>
25 #include <mutex>
26 #include <queue>
27 #include <thread>
28 #include <unordered_map>
29 
30 #include <EGL/egl.h>
31 #include <EGL/eglext.h>
32 #include <GLES2/gl2.h>
33 #include <renderengine/RenderEngine.h>
34 #include <renderengine/private/Description.h>
35 
36 #define EGL_NO_CONFIG ((EGLConfig)0)
37 
38 namespace android {
39 
40 namespace renderengine {
41 
42 class Mesh;
43 class Texture;
44 
45 namespace gl {
46 
47 class GLImage;
48 
49 class GLESRenderEngine : public impl::RenderEngine {
50 public:
51     static std::unique_ptr<GLESRenderEngine> create(int hwcFormat, uint32_t featureFlags,
52                                                     uint32_t imageCacheSize);
53     static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
54 
55     GLESRenderEngine(uint32_t featureFlags, // See RenderEngine::FeatureFlag
56                      EGLDisplay display, EGLConfig config, EGLContext ctxt, EGLSurface dummy,
57                      EGLContext protectedContext, EGLSurface protectedDummy,
58                      uint32_t imageCacheSize);
59     ~GLESRenderEngine() override EXCLUDES(mRenderingMutex);
60 
61     std::unique_ptr<Framebuffer> createFramebuffer() override;
62     std::unique_ptr<Image> createImage() override;
63 
64     void primeCache() const override;
65     bool isCurrent() const override;
66     base::unique_fd flush() override;
67     bool finish() override;
68     bool waitFence(base::unique_fd fenceFd) override;
69     void clearWithColor(float red, float green, float blue, float alpha) override;
70     void fillRegionWithColor(const Region& region, float red, float green, float blue,
71                              float alpha) override;
72     void genTextures(size_t count, uint32_t* names) override;
73     void deleteTextures(size_t count, uint32_t const* names) override;
74     void bindExternalTextureImage(uint32_t texName, const Image& image) override;
75     status_t bindExternalTextureBuffer(uint32_t texName, const sp<GraphicBuffer>& buffer,
76                                        const sp<Fence>& fence) EXCLUDES(mRenderingMutex);
77     status_t cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) EXCLUDES(mRenderingMutex);
78     void unbindExternalTextureBuffer(uint64_t bufferId) EXCLUDES(mRenderingMutex);
79     status_t bindFrameBuffer(Framebuffer* framebuffer) override;
80     void unbindFrameBuffer(Framebuffer* framebuffer) override;
81     void checkErrors() const override;
82 
isProtected()83     bool isProtected() const override { return mInProtectedContext; }
84     bool supportsProtectedContent() const override;
85     bool useProtectedContext(bool useProtectedContext) override;
86     status_t drawLayers(const DisplaySettings& display, const std::vector<LayerSettings>& layers,
87                         ANativeWindowBuffer* buffer, const bool useFramebufferCache,
88                         base::unique_fd&& bufferFence, base::unique_fd* drawFence)
89             EXCLUDES(mRenderingMutex) override;
90 
91     // internal to RenderEngine
getEGLDisplay()92     EGLDisplay getEGLDisplay() const { return mEGLDisplay; }
getEGLConfig()93     EGLConfig getEGLConfig() const { return mEGLConfig; }
94     // Creates an output image for rendering to
95     EGLImageKHR createFramebufferImageIfNeeded(ANativeWindowBuffer* nativeBuffer, bool isProtected,
96                                                bool useFramebufferCache);
97 
98     // Test-only methods
99     // Returns true iff mImageCache contains an image keyed by bufferId
100     bool isImageCachedForTesting(uint64_t bufferId) EXCLUDES(mRenderingMutex);
101     // Returns true iff mFramebufferImageCache contains an image keyed by bufferId
102     bool isFramebufferImageCachedForTesting(uint64_t bufferId) EXCLUDES(mRenderingMutex);
103 
104 protected:
105     Framebuffer* getFramebufferForDrawing() override;
106     void dump(std::string& result) override;
107     void setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
108                                   ui::Transform::orientation_flags rotation) override;
109     void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
110                             const half4& color, float cornerRadius) override;
111     void setupLayerTexturing(const Texture& texture) override;
112     void setupLayerBlackedOut() override;
113     void setupFillWithColor(float r, float g, float b, float a) override;
114     void setColorTransform(const mat4& colorTransform) override;
115     void disableTexturing() override;
116     void disableBlending() override;
117     void setupCornerRadiusCropSize(float width, float height) override;
118 
119     // HDR and color management related functions and state
120     void setSourceY410BT2020(bool enable) override;
121     void setSourceDataSpace(ui::Dataspace source) override;
122     void setOutputDataSpace(ui::Dataspace dataspace) override;
123     void setDisplayMaxLuminance(const float maxLuminance) override;
124 
125     // drawing
126     void drawMesh(const Mesh& mesh) override;
127 
128     size_t getMaxTextureSize() const override;
129     size_t getMaxViewportDims() const override;
130 
131 private:
132     enum GlesVersion {
133         GLES_VERSION_1_0 = 0x10000,
134         GLES_VERSION_1_1 = 0x10001,
135         GLES_VERSION_2_0 = 0x20000,
136         GLES_VERSION_3_0 = 0x30000,
137     };
138 
139     static GlesVersion parseGlesVersion(const char* str);
140     static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
141                                        EGLContext shareContext, bool useContextPriority,
142                                        Protection protection);
143     static EGLSurface createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config,
144                                                    int hwcFormat, Protection protection);
145     void setScissor(const Rect& region);
146     void disableScissor();
147     bool waitSync(EGLSyncKHR sync, EGLint flags);
148 
149     // A data space is considered HDR data space if it has BT2020 color space
150     // with PQ or HLG transfer function.
151     bool isHdrDataSpace(const ui::Dataspace dataSpace) const;
152     bool needsXYZTransformMatrix() const;
153     // Defines the viewport, and sets the projection matrix to the projection
154     // defined by the clip.
155     void setViewportAndProjection(Rect viewport, Rect clip);
156     // Evicts stale images from the buffer cache.
157     void evictImages(const std::vector<LayerSettings>& layers);
158     // Computes the cropping window for the layer and sets up cropping
159     // coordinates for the mesh.
160     FloatRect setupLayerCropping(const LayerSettings& layer, Mesh& mesh);
161 
162     // We do a special handling for rounded corners when it's possible to turn off blending
163     // for the majority of the layer. The rounded corners needs to turn on blending such that
164     // we can set the alpha value correctly, however, only the corners need this, and since
165     // blending is an expensive operation, we want to turn off blending when it's not necessary.
166     void handleRoundedCorners(const DisplaySettings& display, const LayerSettings& layer,
167                               const Mesh& mesh);
168 
169     EGLDisplay mEGLDisplay;
170     EGLConfig mEGLConfig;
171     EGLContext mEGLContext;
172     EGLSurface mDummySurface;
173     EGLContext mProtectedEGLContext;
174     EGLSurface mProtectedDummySurface;
175     GLuint mProtectedTexName;
176     GLint mMaxViewportDims[2];
177     GLint mMaxTextureSize;
178     GLuint mVpWidth;
179     GLuint mVpHeight;
180     Description mState;
181 
182     mat4 mSrgbToXyz;
183     mat4 mDisplayP3ToXyz;
184     mat4 mBt2020ToXyz;
185     mat4 mXyzToSrgb;
186     mat4 mXyzToDisplayP3;
187     mat4 mXyzToBt2020;
188     mat4 mSrgbToDisplayP3;
189     mat4 mSrgbToBt2020;
190     mat4 mDisplayP3ToSrgb;
191     mat4 mDisplayP3ToBt2020;
192     mat4 mBt2020ToSrgb;
193     mat4 mBt2020ToDisplayP3;
194 
195     bool mInProtectedContext = false;
196     // If set to true, then enables tracing flush() and finish() to systrace.
197     bool mTraceGpuCompletion = false;
198     // Maximum size of mFramebufferImageCache. If more images would be cached, then (approximately)
199     // the last recently used buffer should be kicked out.
200     uint32_t mFramebufferImageCacheSize = 0;
201 
202     // Cache of output images, keyed by corresponding GraphicBuffer ID.
203     std::deque<std::pair<uint64_t, EGLImageKHR>> mFramebufferImageCache;
204 
205     // Current dataspace of layer being rendered
206     ui::Dataspace mDataSpace = ui::Dataspace::UNKNOWN;
207 
208     // Current output dataspace of the render engine
209     ui::Dataspace mOutputDataSpace = ui::Dataspace::UNKNOWN;
210 
211     // Whether device supports color management, currently color management
212     // supports sRGB, DisplayP3 color spaces.
213     const bool mUseColorManagement = false;
214 
215     // Cache of GL images that we'll store per GraphicBuffer ID
216     std::unordered_map<uint64_t, std::unique_ptr<Image>> mImageCache GUARDED_BY(mRenderingMutex);
217     // Mutex guarding rendering operations, so that:
218     // 1. GL operations aren't interleaved, and
219     // 2. Internal state related to rendering that is potentially modified by
220     // multiple threads is guaranteed thread-safe.
221     std::mutex mRenderingMutex;
222 
223     // See bindExternalTextureBuffer above, but requiring that mRenderingMutex
224     // is held.
225     status_t bindExternalTextureBufferLocked(uint32_t texName, const sp<GraphicBuffer>& buffer,
226                                              const sp<Fence>& fence) REQUIRES(mRenderingMutex);
227     // See cacheExternalTextureBuffer above, but requiring that mRenderingMutex
228     // is held.
229     status_t cacheExternalTextureBufferLocked(const sp<GraphicBuffer>& buffer)
230             REQUIRES(mRenderingMutex);
231 
232     std::unique_ptr<Framebuffer> mDrawingBuffer;
233 
234     class FlushTracer {
235     public:
236         FlushTracer(GLESRenderEngine* engine);
237         ~FlushTracer();
238         void queueSync(EGLSyncKHR sync) EXCLUDES(mMutex);
239 
240         struct QueueEntry {
241             EGLSyncKHR mSync = nullptr;
242             uint64_t mFrameNum = 0;
243         };
244 
245     private:
246         void loop();
247         GLESRenderEngine* const mEngine;
248         std::thread mThread;
249         std::condition_variable_any mCondition;
250         std::mutex mMutex;
251         std::queue<QueueEntry> mQueue GUARDED_BY(mMutex);
252         uint64_t mFramesQueued GUARDED_BY(mMutex) = 0;
253         bool mRunning = true;
254     };
255     friend class FlushTracer;
256     std::unique_ptr<FlushTracer> mFlushTracer;
257 };
258 
259 } // namespace gl
260 } // namespace renderengine
261 } // namespace android
262 
263 #endif /* SF_GLESRENDERENGINE_H_ */
264