blob: e7ed9c01faff2d96ae1566e59e8c1f502f10a3ee [file] [log] [blame]
Mathias Agopian3f844832013-08-07 21:24:32 -07001/*
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
Peiyong Lin7e219eb2018-12-03 05:40:42 -080017#ifndef SF_GLESRENDERENGINE_H_
18#define SF_GLESRENDERENGINE_H_
Mathias Agopian3f844832013-08-07 21:24:32 -070019
Alec Mouri554d06e2018-12-20 00:15:33 -080020#include <condition_variable>
Alec Mourida4cf3b2019-02-12 15:33:01 -080021#include <deque>
Alec Mouri554d06e2018-12-20 00:15:33 -080022#include <mutex>
23#include <queue>
24#include <thread>
Alec Mourida4cf3b2019-02-12 15:33:01 -080025#include <unordered_map>
Mathias Agopian3f844832013-08-07 21:24:32 -070026
Peiyong Linf11f39b2018-09-05 14:37:41 -070027#include <EGL/egl.h>
28#include <EGL/eglext.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070029#include <GLES2/gl2.h>
Alec Mouri16a99402019-07-29 16:37:30 -070030#include <android-base/thread_annotations.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070031#include <renderengine/RenderEngine.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070032#include <renderengine/private/Description.h>
Alec Mouri16a99402019-07-29 16:37:30 -070033#include <sys/types.h>
Vishnu Nairf19544f2020-02-03 11:23:26 -080034#include "GLShadowTexture.h"
Alec Mouri16a99402019-07-29 16:37:30 -070035#include "ImageManager.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070036
Peiyong Linf11f39b2018-09-05 14:37:41 -070037#define EGL_NO_CONFIG ((EGLConfig)0)
38
Mathias Agopian3f844832013-08-07 21:24:32 -070039namespace android {
Mathias Agopian3f844832013-08-07 21:24:32 -070040
Peiyong Lin833074a2018-08-28 11:53:54 -070041namespace renderengine {
42
Mathias Agopian3f844832013-08-07 21:24:32 -070043class Mesh;
Mathias Agopian49457ac2013-08-14 18:20:17 -070044class Texture;
Mathias Agopian3f844832013-08-07 21:24:32 -070045
Peiyong Lin833074a2018-08-28 11:53:54 -070046namespace gl {
Lloyd Pique144e1162017-12-20 16:44:52 -080047
Peiyong Linf1bada92018-08-29 09:39:31 -070048class GLImage;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080049class BlurFilter;
Peiyong Linf1bada92018-08-29 09:39:31 -070050
Alec Mouri081be4c2020-09-16 10:24:47 -070051class GLESRenderEngine : public RenderEngine {
Mathias Agopian3f844832013-08-07 21:24:32 -070052public:
Peiyong Lin4137a1d2019-10-09 10:39:09 -070053 static std::unique_ptr<GLESRenderEngine> create(const RenderEngineCreationArgs& args);
Peiyong Linf11f39b2018-09-05 14:37:41 -070054
Peiyong Lin4137a1d2019-10-09 10:39:09 -070055 GLESRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLConfig config,
Peiyong Lind8460c82020-07-28 16:04:22 -070056 EGLContext ctxt, EGLSurface stub, EGLContext protectedContext,
57 EGLSurface protectedStub);
Alec Mourid43ccab2019-03-13 12:23:45 -070058 ~GLESRenderEngine() override EXCLUDES(mRenderingMutex);
Mathias Agopian3f844832013-08-07 21:24:32 -070059
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050060 void primeCache() override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070061 void genTextures(size_t count, uint32_t* names) override;
62 void deleteTextures(size_t count, uint32_t const* names) override;
Peiyong Linfb530cf2018-12-15 05:07:38 +000063 bool isProtected() const override { return mInProtectedContext; }
64 bool supportsProtectedContent() const override;
65 bool useProtectedContext(bool useProtectedContext) override;
Vishnu Nair9b079a22020-01-21 14:36:08 -080066 status_t drawLayers(const DisplaySettings& display,
67 const std::vector<const LayerSettings*>& layers,
Alec Mouria90a5702021-04-16 16:36:21 +000068 const std::shared_ptr<ExternalTexture>& buffer,
69 const bool useFramebufferCache, base::unique_fd&& bufferFence,
70 base::unique_fd* drawFence) override;
Alec Mouri368e1582020-08-13 10:14:29 -070071 bool cleanupPostRender(CleanupMode mode) override;
Alec Mourid6f09462020-12-07 11:18:17 -080072 int getContextPriority() override;
Derek Sollenbergerb3998372021-02-16 15:16:56 -050073 bool supportsBackgroundBlur() override { return mBlurFilter != nullptr; }
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -040074 void onPrimaryDisplaySizeChanged(ui::Size size) override {}
Alec Mouri6e57f682018-09-29 20:45:08 -070075
Peiyong Linf11f39b2018-09-05 14:37:41 -070076 EGLDisplay getEGLDisplay() const { return mEGLDisplay; }
Alec Mourida4cf3b2019-02-12 15:33:01 -080077 // Creates an output image for rendering to
Alec Mourife0d72b2019-03-21 14:05:56 -070078 EGLImageKHR createFramebufferImageIfNeeded(ANativeWindowBuffer* nativeBuffer, bool isProtected,
Alec Mourief44c2d2019-07-15 15:27:22 -070079 bool useFramebufferCache)
80 EXCLUDES(mFramebufferImageCacheMutex);
Peiyong Linf11f39b2018-09-05 14:37:41 -070081
Alec Mourid43ccab2019-03-13 12:23:45 -070082 // Test-only methods
83 // Returns true iff mImageCache contains an image keyed by bufferId
84 bool isImageCachedForTesting(uint64_t bufferId) EXCLUDES(mRenderingMutex);
Alec Mouri368e1582020-08-13 10:14:29 -070085 // Returns true iff texName was previously generated by RenderEngine and was
86 // not destroyed.
87 bool isTextureNameKnownForTesting(uint32_t texName);
88 // Returns the buffer ID of the content bound to texName, or nullopt if no
89 // such mapping exists.
90 std::optional<uint64_t> getBufferIdForTextureNameForTesting(uint32_t texName);
Alec Mourid43ccab2019-03-13 12:23:45 -070091 // Returns true iff mFramebufferImageCache contains an image keyed by bufferId
Alec Mourief44c2d2019-07-15 15:27:22 -070092 bool isFramebufferImageCachedForTesting(uint64_t bufferId)
93 EXCLUDES(mFramebufferImageCacheMutex);
Alec Mouri16a99402019-07-29 16:37:30 -070094 // These are wrappers around public methods above, but exposing Barrier
95 // objects so that tests can block.
96 std::shared_ptr<ImageManager::Barrier> cacheExternalTextureBufferForTesting(
97 const sp<GraphicBuffer>& buffer);
98 std::shared_ptr<ImageManager::Barrier> unbindExternalTextureBufferForTesting(uint64_t bufferId);
Alec Mourid43ccab2019-03-13 12:23:45 -070099
Mathias Agopian3f844832013-08-07 21:24:32 -0700100protected:
John Reck992a2ca2020-09-03 17:20:56 -0700101 Framebuffer* getFramebufferForDrawing();
Alec Mourief44c2d2019-07-15 15:27:22 -0700102 void dump(std::string& result) override EXCLUDES(mRenderingMutex)
103 EXCLUDES(mFramebufferImageCacheMutex);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700104 size_t getMaxTextureSize() const override;
105 size_t getMaxViewportDims() const override;
Alec Mouria90a5702021-04-16 16:36:21 +0000106 void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable)
107 EXCLUDES(mRenderingMutex);
108 void unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) EXCLUDES(mRenderingMutex);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700109
110private:
John Reck992a2ca2020-09-03 17:20:56 -0700111 friend class BindNativeBufferAsFramebuffer;
112
Peiyong Linf11f39b2018-09-05 14:37:41 -0700113 enum GlesVersion {
114 GLES_VERSION_1_0 = 0x10000,
115 GLES_VERSION_1_1 = 0x10001,
116 GLES_VERSION_2_0 = 0x20000,
117 GLES_VERSION_3_0 = 0x30000,
118 };
119
Alec Mouri8002fca2019-06-28 15:24:13 -0700120 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700121 static GlesVersion parseGlesVersion(const char* str);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800122 static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
Alec Mourid6f09462020-12-07 11:18:17 -0800123 EGLContext shareContext,
124 std::optional<ContextPriority> contextPriority,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000125 Protection protection);
Alec Mourid6f09462020-12-07 11:18:17 -0800126 static std::optional<RenderEngine::ContextPriority> createContextPriority(
127 const RenderEngineCreationArgs& args);
Peiyong Lind8460c82020-07-28 16:04:22 -0700128 static EGLSurface createStubEglPbufferSurface(EGLDisplay display, EGLConfig config,
129 int hwcFormat, Protection protection);
Alec Mouri8002fca2019-06-28 15:24:13 -0700130 std::unique_ptr<Framebuffer> createFramebuffer();
131 std::unique_ptr<Image> createImage();
132 void checkErrors() const;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800133 void checkErrors(const char* tag) const;
Peiyong Lin1c097612019-03-22 16:21:46 -0700134 void setScissor(const Rect& region);
135 void disableScissor();
Alec Mouri554d06e2018-12-20 00:15:33 -0800136 bool waitSync(EGLSyncKHR sync, EGLint flags);
Alec Mouri16a99402019-07-29 16:37:30 -0700137 status_t cacheExternalTextureBufferInternal(const sp<GraphicBuffer>& buffer)
138 EXCLUDES(mRenderingMutex);
139 void unbindExternalTextureBufferInternal(uint64_t bufferId) EXCLUDES(mRenderingMutex);
John Reck992a2ca2020-09-03 17:20:56 -0700140 status_t bindFrameBuffer(Framebuffer* framebuffer);
141 void unbindFrameBuffer(Framebuffer* framebuffer);
142 void bindExternalTextureImage(uint32_t texName, const Image& image);
Alec Mouri04eae7d2020-09-14 15:34:16 -0700143 void bindExternalTextureBuffer(uint32_t texName, const sp<GraphicBuffer>& buffer,
144 const sp<Fence>& fence) EXCLUDES(mRenderingMutex);
Marin Shalamanov23c31af2020-09-09 15:01:47 +0200145 void cleanFramebufferCache() EXCLUDES(mFramebufferImageCacheMutex) override;
Peiyong Linf11f39b2018-09-05 14:37:41 -0700146
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700147 // A data space is considered HDR data space if it has BT2020 color space
148 // with PQ or HLG transfer function.
149 bool isHdrDataSpace(const ui::Dataspace dataSpace) const;
150 bool needsXYZTransformMatrix() const;
Alec Mouri1089aed2018-10-25 21:33:57 -0700151 // Defines the viewport, and sets the projection matrix to the projection
152 // defined by the clip.
153 void setViewportAndProjection(Rect viewport, Rect clip);
Alec Mouri539319f2018-12-19 17:56:23 -0800154 // Evicts stale images from the buffer cache.
155 void evictImages(const std::vector<LayerSettings>& layers);
Alec Mouri7c94edb2018-12-03 21:23:26 -0800156 // Computes the cropping window for the layer and sets up cropping
157 // coordinates for the mesh.
158 FloatRect setupLayerCropping(const LayerSettings& layer, Mesh& mesh);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700159
Peiyong Lin1c097612019-03-22 16:21:46 -0700160 // We do a special handling for rounded corners when it's possible to turn off blending
161 // for the majority of the layer. The rounded corners needs to turn on blending such that
162 // we can set the alpha value correctly, however, only the corners need this, and since
163 // blending is an expensive operation, we want to turn off blending when it's not necessary.
164 void handleRoundedCorners(const DisplaySettings& display, const LayerSettings& layer,
165 const Mesh& mesh);
Alec Mouri8002fca2019-06-28 15:24:13 -0700166 base::unique_fd flush();
167 bool finish();
168 bool waitFence(base::unique_fd fenceFd);
169 void clearWithColor(float red, float green, float blue, float alpha);
170 void fillRegionWithColor(const Region& region, float red, float green, float blue, float alpha);
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800171 void handleShadow(const FloatRect& casterRect, float casterCornerRadius,
172 const ShadowSettings& shadowSettings);
Alec Mouri8002fca2019-06-28 15:24:13 -0700173 void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
174 const half4& color, float cornerRadius);
175 void setupLayerTexturing(const Texture& texture);
176 void setupFillWithColor(float r, float g, float b, float a);
177 void setColorTransform(const mat4& colorTransform);
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800178 void setDisplayColorTransform(const mat4& colorTransform);
Alec Mouri8002fca2019-06-28 15:24:13 -0700179 void disableTexturing();
180 void disableBlending();
181 void setupCornerRadiusCropSize(float width, float height);
182
183 // HDR and color management related functions and state
184 void setSourceY410BT2020(bool enable);
185 void setSourceDataSpace(ui::Dataspace source);
186 void setOutputDataSpace(ui::Dataspace dataspace);
187 void setDisplayMaxLuminance(const float maxLuminance);
188
189 // drawing
190 void drawMesh(const Mesh& mesh);
Peiyong Lin1c097612019-03-22 16:21:46 -0700191
Peiyong Linf11f39b2018-09-05 14:37:41 -0700192 EGLDisplay mEGLDisplay;
193 EGLConfig mEGLConfig;
194 EGLContext mEGLContext;
Peiyong Lind8460c82020-07-28 16:04:22 -0700195 EGLSurface mStubSurface;
Peiyong Linfb530cf2018-12-15 05:07:38 +0000196 EGLContext mProtectedEGLContext;
Peiyong Lind8460c82020-07-28 16:04:22 -0700197 EGLSurface mProtectedStubSurface;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700198 GLint mMaxViewportDims[2];
199 GLint mMaxTextureSize;
200 GLuint mVpWidth;
201 GLuint mVpHeight;
202 Description mState;
Alec Mouri2725c092020-11-25 09:53:56 -0800203 std::unique_ptr<GLShadowTexture> mShadowTexture = nullptr;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700204
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700205 mat4 mSrgbToXyz;
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700206 mat4 mDisplayP3ToXyz;
Valerie Haueb8e0762018-11-06 10:10:42 -0800207 mat4 mBt2020ToXyz;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700208 mat4 mXyzToSrgb;
209 mat4 mXyzToDisplayP3;
210 mat4 mXyzToBt2020;
Valerie Haueb8e0762018-11-06 10:10:42 -0800211 mat4 mSrgbToDisplayP3;
212 mat4 mSrgbToBt2020;
213 mat4 mDisplayP3ToSrgb;
214 mat4 mDisplayP3ToBt2020;
215 mat4 mBt2020ToSrgb;
216 mat4 mBt2020ToDisplayP3;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700217
Peiyong Linfb530cf2018-12-15 05:07:38 +0000218 bool mInProtectedContext = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800219 // If set to true, then enables tracing flush() and finish() to systrace.
220 bool mTraceGpuCompletion = false;
Alec Mourida4cf3b2019-02-12 15:33:01 -0800221 // Maximum size of mFramebufferImageCache. If more images would be cached, then (approximately)
222 // the last recently used buffer should be kicked out.
223 uint32_t mFramebufferImageCacheSize = 0;
224
225 // Cache of output images, keyed by corresponding GraphicBuffer ID.
Alec Mourief44c2d2019-07-15 15:27:22 -0700226 std::deque<std::pair<uint64_t, EGLImageKHR>> mFramebufferImageCache
227 GUARDED_BY(mFramebufferImageCacheMutex);
228 // The only reason why we have this mutex is so that we don't segfault when
229 // dumping info.
230 std::mutex mFramebufferImageCacheMutex;
Peiyong Linfb069302018-04-25 14:34:31 -0700231
232 // Current dataspace of layer being rendered
233 ui::Dataspace mDataSpace = ui::Dataspace::UNKNOWN;
234
235 // Current output dataspace of the render engine
236 ui::Dataspace mOutputDataSpace = ui::Dataspace::UNKNOWN;
237
Peiyong Lin13effd12018-07-24 17:01:47 -0700238 // Whether device supports color management, currently color management
239 // supports sRGB, DisplayP3 color spaces.
240 const bool mUseColorManagement = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800241
Alec Mouri081be4c2020-09-16 10:24:47 -0700242 // Whether only shaders performing tone mapping from HDR to SDR will be generated on
243 // primeCache().
244 const bool mPrecacheToneMapperShaderOnly = false;
245
Alec Mouri539319f2018-12-19 17:56:23 -0800246 // Cache of GL images that we'll store per GraphicBuffer ID
Alec Mourif0497272019-03-11 15:53:11 -0700247 std::unordered_map<uint64_t, std::unique_ptr<Image>> mImageCache GUARDED_BY(mRenderingMutex);
Alec Mouri368e1582020-08-13 10:14:29 -0700248 std::unordered_map<uint32_t, std::optional<uint64_t>> mTextureView;
249
Alec Mourif0497272019-03-11 15:53:11 -0700250 // Mutex guarding rendering operations, so that:
251 // 1. GL operations aren't interleaved, and
252 // 2. Internal state related to rendering that is potentially modified by
253 // multiple threads is guaranteed thread-safe.
254 std::mutex mRenderingMutex;
255
Alec Mouri820c7402019-01-23 13:02:39 -0800256 std::unique_ptr<Framebuffer> mDrawingBuffer;
Lingfeng Yang2e4fef62020-04-24 14:48:43 +0000257 // this is a 1x1 RGB buffer, but over-allocate in case a driver wants more
258 // memory or if it needs to satisfy alignment requirements. In this case:
259 // assume that each channel requires 4 bytes, and add 3 additional bytes to
260 // ensure that we align on a word. Allocating 16 bytes will provide a
261 // guarantee that we don't clobber memory.
262 uint32_t mPlaceholderDrawBuffer[4];
Alec Mouri368e1582020-08-13 10:14:29 -0700263 // Placeholder buffer and image, similar to mPlaceholderDrawBuffer, but
264 // instead these are intended for cleaning up texture memory with the
265 // GL_TEXTURE_EXTERNAL_OES target.
266 ANativeWindowBuffer* mPlaceholderBuffer = nullptr;
267 EGLImage mPlaceholderImage = EGL_NO_IMAGE_KHR;
Lingfeng Yang2e4fef62020-04-24 14:48:43 +0000268 sp<Fence> mLastDrawFence;
269 // Store a separate boolean checking if prior resources were cleaned up, as
270 // devices that don't support native sync fences can't rely on a last draw
271 // fence that doesn't exist.
272 bool mPriorResourcesCleaned = true;
Alec Mouri820c7402019-01-23 13:02:39 -0800273
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800274 // Blur effect processor, only instantiated when a layer requests it.
275 BlurFilter* mBlurFilter = nullptr;
276
Alec Mouri554d06e2018-12-20 00:15:33 -0800277 class FlushTracer {
278 public:
279 FlushTracer(GLESRenderEngine* engine);
280 ~FlushTracer();
281 void queueSync(EGLSyncKHR sync) EXCLUDES(mMutex);
282
283 struct QueueEntry {
284 EGLSyncKHR mSync = nullptr;
285 uint64_t mFrameNum = 0;
286 };
287
288 private:
289 void loop();
290 GLESRenderEngine* const mEngine;
291 std::thread mThread;
292 std::condition_variable_any mCondition;
293 std::mutex mMutex;
294 std::queue<QueueEntry> mQueue GUARDED_BY(mMutex);
295 uint64_t mFramesQueued GUARDED_BY(mMutex) = 0;
296 bool mRunning = true;
297 };
298 friend class FlushTracer;
Alec Mouri16a99402019-07-29 16:37:30 -0700299 friend class ImageManager;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800300 friend class GLFramebuffer;
301 friend class BlurFilter;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800302 friend class GenericProgram;
Alec Mouri554d06e2018-12-20 00:15:33 -0800303 std::unique_ptr<FlushTracer> mFlushTracer;
Alec Mouri2725c092020-11-25 09:53:56 -0800304 std::unique_ptr<ImageManager> mImageManager;
Mathias Agopian3f844832013-08-07 21:24:32 -0700305};
306
Peiyong Lin46080ef2018-10-26 18:43:14 -0700307} // namespace gl
308} // namespace renderengine
309} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700310
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800311#endif /* SF_GLESRENDERENGINE_H_ */