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 <condition_variable>
21 #include <deque>
22 #include <mutex>
23 #include <queue>
24 #include <thread>
25 #include <unordered_map>
26 
27 #include <EGL/egl.h>
28 #include <EGL/eglext.h>
29 #include <GLES2/gl2.h>
30 #include <android-base/thread_annotations.h>
31 #include <renderengine/RenderEngine.h>
32 #include <renderengine/private/Description.h>
33 #include <sys/types.h>
34 #include "GLShadowTexture.h"
35 #include "ImageManager.h"
36 
37 #define EGL_NO_CONFIG ((EGLConfig)0)
38 
39 namespace android {
40 
41 namespace renderengine {
42 
43 class Mesh;
44 class Texture;
45 
46 namespace gl {
47 
48 class GLImage;
49 class BlurFilter;
50 
51 class GLESRenderEngine : public impl::RenderEngine {
52 public:
53     static std::unique_ptr<GLESRenderEngine> create(const RenderEngineCreationArgs& args);
54 
55     GLESRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLConfig config,
56                      EGLContext ctxt, EGLSurface stub, EGLContext protectedContext,
57                      EGLSurface protectedStub);
58     ~GLESRenderEngine() override EXCLUDES(mRenderingMutex);
59 
60     void primeCache() const override;
61     void genTextures(size_t count, uint32_t* names) override;
62     void deleteTextures(size_t count, uint32_t const* names) override;
63     void bindExternalTextureImage(uint32_t texName, const Image& image) override;
64     status_t bindExternalTextureBuffer(uint32_t texName, const sp<GraphicBuffer>& buffer,
65                                        const sp<Fence>& fence) EXCLUDES(mRenderingMutex);
66     void cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) EXCLUDES(mRenderingMutex);
67     void unbindExternalTextureBuffer(uint64_t bufferId) EXCLUDES(mRenderingMutex);
68     status_t bindFrameBuffer(Framebuffer* framebuffer) override;
69     void unbindFrameBuffer(Framebuffer* framebuffer) override;
70 
isProtected()71     bool isProtected() const override { return mInProtectedContext; }
72     bool supportsProtectedContent() const override;
73     bool useProtectedContext(bool useProtectedContext) override;
74     status_t drawLayers(const DisplaySettings& display,
75                         const std::vector<const LayerSettings*>& layers,
76                         const sp<GraphicBuffer>& buffer, const bool useFramebufferCache,
77                         base::unique_fd&& bufferFence, base::unique_fd* drawFence) override;
78     bool cleanupPostRender(CleanupMode mode) override;
79 
getEGLDisplay()80     EGLDisplay getEGLDisplay() const { return mEGLDisplay; }
81     // Creates an output image for rendering to
82     EGLImageKHR createFramebufferImageIfNeeded(ANativeWindowBuffer* nativeBuffer, bool isProtected,
83                                                bool useFramebufferCache)
84             EXCLUDES(mFramebufferImageCacheMutex);
85 
86     // Test-only methods
87     // Returns true iff mImageCache contains an image keyed by bufferId
88     bool isImageCachedForTesting(uint64_t bufferId) EXCLUDES(mRenderingMutex);
89     // Returns true iff texName was previously generated by RenderEngine and was
90     // not destroyed.
91     bool isTextureNameKnownForTesting(uint32_t texName);
92     // Returns the buffer ID of the content bound to texName, or nullopt if no
93     // such mapping exists.
94     std::optional<uint64_t> getBufferIdForTextureNameForTesting(uint32_t texName);
95     // Returns true iff mFramebufferImageCache contains an image keyed by bufferId
96     bool isFramebufferImageCachedForTesting(uint64_t bufferId)
97             EXCLUDES(mFramebufferImageCacheMutex);
98     // These are wrappers around public methods above, but exposing Barrier
99     // objects so that tests can block.
100     std::shared_ptr<ImageManager::Barrier> cacheExternalTextureBufferForTesting(
101             const sp<GraphicBuffer>& buffer);
102     std::shared_ptr<ImageManager::Barrier> unbindExternalTextureBufferForTesting(uint64_t bufferId);
103 
104 protected:
105     Framebuffer* getFramebufferForDrawing() override;
106     void dump(std::string& result) override EXCLUDES(mRenderingMutex)
107             EXCLUDES(mFramebufferImageCacheMutex);
108     size_t getMaxTextureSize() const override;
109     size_t getMaxViewportDims() const override;
110 
111 private:
112     enum GlesVersion {
113         GLES_VERSION_1_0 = 0x10000,
114         GLES_VERSION_1_1 = 0x10001,
115         GLES_VERSION_2_0 = 0x20000,
116         GLES_VERSION_3_0 = 0x30000,
117     };
118 
119     static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
120     static GlesVersion parseGlesVersion(const char* str);
121     static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
122                                        EGLContext shareContext, bool useContextPriority,
123                                        Protection protection);
124     static EGLSurface createStubEglPbufferSurface(EGLDisplay display, EGLConfig config,
125                                                   int hwcFormat, Protection protection);
126     std::unique_ptr<Framebuffer> createFramebuffer();
127     std::unique_ptr<Image> createImage();
128     void checkErrors() const;
129     void checkErrors(const char* tag) const;
130     void setScissor(const Rect& region);
131     void disableScissor();
132     bool waitSync(EGLSyncKHR sync, EGLint flags);
133     status_t cacheExternalTextureBufferInternal(const sp<GraphicBuffer>& buffer)
134             EXCLUDES(mRenderingMutex);
135     void unbindExternalTextureBufferInternal(uint64_t bufferId) EXCLUDES(mRenderingMutex);
136 
137     // A data space is considered HDR data space if it has BT2020 color space
138     // with PQ or HLG transfer function.
139     bool isHdrDataSpace(const ui::Dataspace dataSpace) const;
140     bool needsXYZTransformMatrix() const;
141     // Defines the viewport, and sets the projection matrix to the projection
142     // defined by the clip.
143     void setViewportAndProjection(Rect viewport, Rect clip);
144     // Evicts stale images from the buffer cache.
145     void evictImages(const std::vector<LayerSettings>& layers);
146     // Computes the cropping window for the layer and sets up cropping
147     // coordinates for the mesh.
148     FloatRect setupLayerCropping(const LayerSettings& layer, Mesh& mesh);
149 
150     // We do a special handling for rounded corners when it's possible to turn off blending
151     // for the majority of the layer. The rounded corners needs to turn on blending such that
152     // we can set the alpha value correctly, however, only the corners need this, and since
153     // blending is an expensive operation, we want to turn off blending when it's not necessary.
154     void handleRoundedCorners(const DisplaySettings& display, const LayerSettings& layer,
155                               const Mesh& mesh);
156     base::unique_fd flush();
157     bool finish();
158     bool waitFence(base::unique_fd fenceFd);
159     void clearWithColor(float red, float green, float blue, float alpha);
160     void fillRegionWithColor(const Region& region, float red, float green, float blue, float alpha);
161     void handleShadow(const FloatRect& casterRect, float casterCornerRadius,
162                       const ShadowSettings& shadowSettings);
163     void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
164                             const half4& color, float cornerRadius);
165     void setupLayerTexturing(const Texture& texture);
166     void setupFillWithColor(float r, float g, float b, float a);
167     void setColorTransform(const mat4& colorTransform);
168     void disableTexturing();
169     void disableBlending();
170     void setupCornerRadiusCropSize(float width, float height);
171 
172     // HDR and color management related functions and state
173     void setSourceY410BT2020(bool enable);
174     void setSourceDataSpace(ui::Dataspace source);
175     void setOutputDataSpace(ui::Dataspace dataspace);
176     void setDisplayMaxLuminance(const float maxLuminance);
177 
178     // drawing
179     void drawMesh(const Mesh& mesh);
180 
181     EGLDisplay mEGLDisplay;
182     EGLConfig mEGLConfig;
183     EGLContext mEGLContext;
184     EGLSurface mStubSurface;
185     EGLContext mProtectedEGLContext;
186     EGLSurface mProtectedStubSurface;
187     GLint mMaxViewportDims[2];
188     GLint mMaxTextureSize;
189     GLuint mVpWidth;
190     GLuint mVpHeight;
191     Description mState;
192     GLShadowTexture mShadowTexture;
193 
194     mat4 mSrgbToXyz;
195     mat4 mDisplayP3ToXyz;
196     mat4 mBt2020ToXyz;
197     mat4 mXyzToSrgb;
198     mat4 mXyzToDisplayP3;
199     mat4 mXyzToBt2020;
200     mat4 mSrgbToDisplayP3;
201     mat4 mSrgbToBt2020;
202     mat4 mDisplayP3ToSrgb;
203     mat4 mDisplayP3ToBt2020;
204     mat4 mBt2020ToSrgb;
205     mat4 mBt2020ToDisplayP3;
206 
207     bool mInProtectedContext = false;
208     // If set to true, then enables tracing flush() and finish() to systrace.
209     bool mTraceGpuCompletion = false;
210     // Maximum size of mFramebufferImageCache. If more images would be cached, then (approximately)
211     // the last recently used buffer should be kicked out.
212     uint32_t mFramebufferImageCacheSize = 0;
213 
214     // Cache of output images, keyed by corresponding GraphicBuffer ID.
215     std::deque<std::pair<uint64_t, EGLImageKHR>> mFramebufferImageCache
216             GUARDED_BY(mFramebufferImageCacheMutex);
217     // The only reason why we have this mutex is so that we don't segfault when
218     // dumping info.
219     std::mutex mFramebufferImageCacheMutex;
220 
221     // Current dataspace of layer being rendered
222     ui::Dataspace mDataSpace = ui::Dataspace::UNKNOWN;
223 
224     // Current output dataspace of the render engine
225     ui::Dataspace mOutputDataSpace = ui::Dataspace::UNKNOWN;
226 
227     // Whether device supports color management, currently color management
228     // supports sRGB, DisplayP3 color spaces.
229     const bool mUseColorManagement = false;
230 
231     // Cache of GL images that we'll store per GraphicBuffer ID
232     std::unordered_map<uint64_t, std::unique_ptr<Image>> mImageCache GUARDED_BY(mRenderingMutex);
233     std::unordered_map<uint32_t, std::optional<uint64_t>> mTextureView;
234 
235     // Mutex guarding rendering operations, so that:
236     // 1. GL operations aren't interleaved, and
237     // 2. Internal state related to rendering that is potentially modified by
238     // multiple threads is guaranteed thread-safe.
239     std::mutex mRenderingMutex;
240 
241     std::unique_ptr<Framebuffer> mDrawingBuffer;
242     // this is a 1x1 RGB buffer, but over-allocate in case a driver wants more
243     // memory or if it needs to satisfy alignment requirements. In this case:
244     // assume that each channel requires 4 bytes, and add 3 additional bytes to
245     // ensure that we align on a word. Allocating 16 bytes will provide a
246     // guarantee that we don't clobber memory.
247     uint32_t mPlaceholderDrawBuffer[4];
248     // Placeholder buffer and image, similar to mPlaceholderDrawBuffer, but
249     // instead these are intended for cleaning up texture memory with the
250     // GL_TEXTURE_EXTERNAL_OES target.
251     ANativeWindowBuffer* mPlaceholderBuffer = nullptr;
252     EGLImage mPlaceholderImage = EGL_NO_IMAGE_KHR;
253     sp<Fence> mLastDrawFence;
254     // Store a separate boolean checking if prior resources were cleaned up, as
255     // devices that don't support native sync fences can't rely on a last draw
256     // fence that doesn't exist.
257     bool mPriorResourcesCleaned = true;
258 
259     // Blur effect processor, only instantiated when a layer requests it.
260     BlurFilter* mBlurFilter = nullptr;
261 
262     class FlushTracer {
263     public:
264         FlushTracer(GLESRenderEngine* engine);
265         ~FlushTracer();
266         void queueSync(EGLSyncKHR sync) EXCLUDES(mMutex);
267 
268         struct QueueEntry {
269             EGLSyncKHR mSync = nullptr;
270             uint64_t mFrameNum = 0;
271         };
272 
273     private:
274         void loop();
275         GLESRenderEngine* const mEngine;
276         std::thread mThread;
277         std::condition_variable_any mCondition;
278         std::mutex mMutex;
279         std::queue<QueueEntry> mQueue GUARDED_BY(mMutex);
280         uint64_t mFramesQueued GUARDED_BY(mMutex) = 0;
281         bool mRunning = true;
282     };
283     friend class FlushTracer;
284     friend class ImageManager;
285     friend class GLFramebuffer;
286     friend class BlurFilter;
287     friend class GenericProgram;
288     std::unique_ptr<FlushTracer> mFlushTracer;
289     std::unique_ptr<ImageManager> mImageManager = std::make_unique<ImageManager>(this);
290 };
291 
292 } // namespace gl
293 } // namespace renderengine
294 } // namespace android
295 
296 #endif /* SF_GLESRENDERENGINE_H_ */
297