blob: 547235a90da37d89fa1e162e93ed547e5f92535a [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
20#include <stdint.h>
Alec Mouri554d06e2018-12-20 00:15:33 -080021#include <condition_variable>
Alec Mourida4cf3b2019-02-12 15:33:01 -080022#include <deque>
Alec Mouri554d06e2018-12-20 00:15:33 -080023#include <mutex>
24#include <queue>
25#include <thread>
Alec Mourida4cf3b2019-02-12 15:33:01 -080026#include <unordered_map>
Mathias Agopian3f844832013-08-07 21:24:32 -070027
Peiyong Linf11f39b2018-09-05 14:37:41 -070028#include <EGL/egl.h>
29#include <EGL/eglext.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070030#include <GLES2/gl2.h>
Alec Mouri16a99402019-07-29 16:37:30 -070031#include <android-base/thread_annotations.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070032#include <renderengine/RenderEngine.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070033#include <renderengine/private/Description.h>
Alec Mouri16a99402019-07-29 16:37:30 -070034#include <sys/types.h>
35#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
Peiyong Lin7e219eb2018-12-03 05:40:42 -080051class GLESRenderEngine : public impl::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,
56 EGLContext ctxt, EGLSurface dummy, EGLContext protectedContext,
57 EGLSurface protectedDummy);
Alec Mourid43ccab2019-03-13 12:23:45 -070058 ~GLESRenderEngine() override EXCLUDES(mRenderingMutex);
Mathias Agopian3f844832013-08-07 21:24:32 -070059
Peiyong Linf1bada92018-08-29 09:39:31 -070060 void primeCache() const 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 Line5a9a7f2018-08-30 15:32:13 -070063 void bindExternalTextureImage(uint32_t texName, const Image& image) override;
Alec Mourid7b3a8b2019-03-21 11:44:18 -070064 status_t bindExternalTextureBuffer(uint32_t texName, const sp<GraphicBuffer>& buffer,
65 const sp<Fence>& fence) EXCLUDES(mRenderingMutex);
Alec Mouri16a99402019-07-29 16:37:30 -070066 void cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) EXCLUDES(mRenderingMutex);
Alec Mourif0497272019-03-11 15:53:11 -070067 void unbindExternalTextureBuffer(uint64_t bufferId) EXCLUDES(mRenderingMutex);
Peiyong Line5a9a7f2018-08-30 15:32:13 -070068 status_t bindFrameBuffer(Framebuffer* framebuffer) override;
69 void unbindFrameBuffer(Framebuffer* framebuffer) override;
Peiyong Linf1bada92018-08-29 09:39:31 -070070
Peiyong Linfb530cf2018-12-15 05:07:38 +000071 bool isProtected() const override { return mInProtectedContext; }
72 bool supportsProtectedContent() const override;
73 bool useProtectedContext(bool useProtectedContext) override;
Alec Mouri1089aed2018-10-25 21:33:57 -070074 status_t drawLayers(const DisplaySettings& display, const std::vector<LayerSettings>& layers,
Alec Mourife0d72b2019-03-21 14:05:56 -070075 ANativeWindowBuffer* buffer, const bool useFramebufferCache,
Alec Mouri3d7c5612019-07-09 13:51:37 -070076 base::unique_fd&& bufferFence, base::unique_fd* drawFence) override;
Alec Mouri6e57f682018-09-29 20:45:08 -070077
Peiyong Linf11f39b2018-09-05 14:37:41 -070078 EGLDisplay getEGLDisplay() const { return mEGLDisplay; }
Alec Mourida4cf3b2019-02-12 15:33:01 -080079 // Creates an output image for rendering to
Alec Mourife0d72b2019-03-21 14:05:56 -070080 EGLImageKHR createFramebufferImageIfNeeded(ANativeWindowBuffer* nativeBuffer, bool isProtected,
Alec Mourief44c2d2019-07-15 15:27:22 -070081 bool useFramebufferCache)
82 EXCLUDES(mFramebufferImageCacheMutex);
Peiyong Linf11f39b2018-09-05 14:37:41 -070083
Alec Mourid43ccab2019-03-13 12:23:45 -070084 // Test-only methods
85 // Returns true iff mImageCache contains an image keyed by bufferId
86 bool isImageCachedForTesting(uint64_t bufferId) EXCLUDES(mRenderingMutex);
87 // Returns true iff mFramebufferImageCache contains an image keyed by bufferId
Alec Mourief44c2d2019-07-15 15:27:22 -070088 bool isFramebufferImageCachedForTesting(uint64_t bufferId)
89 EXCLUDES(mFramebufferImageCacheMutex);
Alec Mouri16a99402019-07-29 16:37:30 -070090 // These are wrappers around public methods above, but exposing Barrier
91 // objects so that tests can block.
92 std::shared_ptr<ImageManager::Barrier> cacheExternalTextureBufferForTesting(
93 const sp<GraphicBuffer>& buffer);
94 std::shared_ptr<ImageManager::Barrier> unbindExternalTextureBufferForTesting(uint64_t bufferId);
Alec Mourid43ccab2019-03-13 12:23:45 -070095
Mathias Agopian3f844832013-08-07 21:24:32 -070096protected:
Alec Mouri820c7402019-01-23 13:02:39 -080097 Framebuffer* getFramebufferForDrawing() override;
Alec Mourief44c2d2019-07-15 15:27:22 -070098 void dump(std::string& result) override EXCLUDES(mRenderingMutex)
99 EXCLUDES(mFramebufferImageCacheMutex);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700100 size_t getMaxTextureSize() const override;
101 size_t getMaxViewportDims() const override;
102
103private:
Peiyong Linf11f39b2018-09-05 14:37:41 -0700104 enum GlesVersion {
105 GLES_VERSION_1_0 = 0x10000,
106 GLES_VERSION_1_1 = 0x10001,
107 GLES_VERSION_2_0 = 0x20000,
108 GLES_VERSION_3_0 = 0x30000,
109 };
110
Alec Mouri8002fca2019-06-28 15:24:13 -0700111 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700112 static GlesVersion parseGlesVersion(const char* str);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800113 static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000114 EGLContext shareContext, bool useContextPriority,
115 Protection protection);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800116 static EGLSurface createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000117 int hwcFormat, Protection protection);
Alec Mouri8002fca2019-06-28 15:24:13 -0700118 std::unique_ptr<Framebuffer> createFramebuffer();
119 std::unique_ptr<Image> createImage();
120 void checkErrors() const;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800121 void checkErrors(const char* tag) const;
Peiyong Lin1c097612019-03-22 16:21:46 -0700122 void setScissor(const Rect& region);
123 void disableScissor();
Alec Mouri554d06e2018-12-20 00:15:33 -0800124 bool waitSync(EGLSyncKHR sync, EGLint flags);
Alec Mouri16a99402019-07-29 16:37:30 -0700125 status_t cacheExternalTextureBufferInternal(const sp<GraphicBuffer>& buffer)
126 EXCLUDES(mRenderingMutex);
127 void unbindExternalTextureBufferInternal(uint64_t bufferId) EXCLUDES(mRenderingMutex);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700128
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700129 // A data space is considered HDR data space if it has BT2020 color space
130 // with PQ or HLG transfer function.
131 bool isHdrDataSpace(const ui::Dataspace dataSpace) const;
132 bool needsXYZTransformMatrix() const;
Alec Mouri1089aed2018-10-25 21:33:57 -0700133 // Defines the viewport, and sets the projection matrix to the projection
134 // defined by the clip.
135 void setViewportAndProjection(Rect viewport, Rect clip);
Alec Mouri539319f2018-12-19 17:56:23 -0800136 // Evicts stale images from the buffer cache.
137 void evictImages(const std::vector<LayerSettings>& layers);
Alec Mouri7c94edb2018-12-03 21:23:26 -0800138 // Computes the cropping window for the layer and sets up cropping
139 // coordinates for the mesh.
140 FloatRect setupLayerCropping(const LayerSettings& layer, Mesh& mesh);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700141
Peiyong Lin1c097612019-03-22 16:21:46 -0700142 // We do a special handling for rounded corners when it's possible to turn off blending
143 // for the majority of the layer. The rounded corners needs to turn on blending such that
144 // we can set the alpha value correctly, however, only the corners need this, and since
145 // blending is an expensive operation, we want to turn off blending when it's not necessary.
146 void handleRoundedCorners(const DisplaySettings& display, const LayerSettings& layer,
147 const Mesh& mesh);
Alec Mouri8002fca2019-06-28 15:24:13 -0700148 base::unique_fd flush();
149 bool finish();
150 bool waitFence(base::unique_fd fenceFd);
151 void clearWithColor(float red, float green, float blue, float alpha);
152 void fillRegionWithColor(const Region& region, float red, float green, float blue, float alpha);
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800153 void handleShadow(const FloatRect& casterRect, float casterCornerRadius,
154 const ShadowSettings& shadowSettings);
Alec Mouri8002fca2019-06-28 15:24:13 -0700155 void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
156 const half4& color, float cornerRadius);
157 void setupLayerTexturing(const Texture& texture);
158 void setupFillWithColor(float r, float g, float b, float a);
159 void setColorTransform(const mat4& colorTransform);
160 void disableTexturing();
161 void disableBlending();
162 void setupCornerRadiusCropSize(float width, float height);
163
164 // HDR and color management related functions and state
165 void setSourceY410BT2020(bool enable);
166 void setSourceDataSpace(ui::Dataspace source);
167 void setOutputDataSpace(ui::Dataspace dataspace);
168 void setDisplayMaxLuminance(const float maxLuminance);
169
170 // drawing
171 void drawMesh(const Mesh& mesh);
Peiyong Lin1c097612019-03-22 16:21:46 -0700172
Peiyong Linf11f39b2018-09-05 14:37:41 -0700173 EGLDisplay mEGLDisplay;
174 EGLConfig mEGLConfig;
175 EGLContext mEGLContext;
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800176 EGLSurface mDummySurface;
Peiyong Linfb530cf2018-12-15 05:07:38 +0000177 EGLContext mProtectedEGLContext;
178 EGLSurface mProtectedDummySurface;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700179 GLuint mProtectedTexName;
180 GLint mMaxViewportDims[2];
181 GLint mMaxTextureSize;
182 GLuint mVpWidth;
183 GLuint mVpHeight;
184 Description mState;
185
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700186 mat4 mSrgbToXyz;
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700187 mat4 mDisplayP3ToXyz;
Valerie Haueb8e0762018-11-06 10:10:42 -0800188 mat4 mBt2020ToXyz;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700189 mat4 mXyzToSrgb;
190 mat4 mXyzToDisplayP3;
191 mat4 mXyzToBt2020;
Valerie Haueb8e0762018-11-06 10:10:42 -0800192 mat4 mSrgbToDisplayP3;
193 mat4 mSrgbToBt2020;
194 mat4 mDisplayP3ToSrgb;
195 mat4 mDisplayP3ToBt2020;
196 mat4 mBt2020ToSrgb;
197 mat4 mBt2020ToDisplayP3;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700198
Peiyong Linfb530cf2018-12-15 05:07:38 +0000199 bool mInProtectedContext = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800200 // If set to true, then enables tracing flush() and finish() to systrace.
201 bool mTraceGpuCompletion = false;
Alec Mourida4cf3b2019-02-12 15:33:01 -0800202 // Maximum size of mFramebufferImageCache. If more images would be cached, then (approximately)
203 // the last recently used buffer should be kicked out.
204 uint32_t mFramebufferImageCacheSize = 0;
205
206 // Cache of output images, keyed by corresponding GraphicBuffer ID.
Alec Mourief44c2d2019-07-15 15:27:22 -0700207 std::deque<std::pair<uint64_t, EGLImageKHR>> mFramebufferImageCache
208 GUARDED_BY(mFramebufferImageCacheMutex);
209 // The only reason why we have this mutex is so that we don't segfault when
210 // dumping info.
211 std::mutex mFramebufferImageCacheMutex;
Peiyong Linfb069302018-04-25 14:34:31 -0700212
213 // Current dataspace of layer being rendered
214 ui::Dataspace mDataSpace = ui::Dataspace::UNKNOWN;
215
216 // Current output dataspace of the render engine
217 ui::Dataspace mOutputDataSpace = ui::Dataspace::UNKNOWN;
218
Peiyong Lin13effd12018-07-24 17:01:47 -0700219 // Whether device supports color management, currently color management
220 // supports sRGB, DisplayP3 color spaces.
221 const bool mUseColorManagement = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800222
Alec Mouri539319f2018-12-19 17:56:23 -0800223 // Cache of GL images that we'll store per GraphicBuffer ID
Alec Mourif0497272019-03-11 15:53:11 -0700224 std::unordered_map<uint64_t, std::unique_ptr<Image>> mImageCache GUARDED_BY(mRenderingMutex);
225 // Mutex guarding rendering operations, so that:
226 // 1. GL operations aren't interleaved, and
227 // 2. Internal state related to rendering that is potentially modified by
228 // multiple threads is guaranteed thread-safe.
229 std::mutex mRenderingMutex;
230
Alec Mouri820c7402019-01-23 13:02:39 -0800231 std::unique_ptr<Framebuffer> mDrawingBuffer;
232
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800233 // Blur effect processor, only instantiated when a layer requests it.
234 BlurFilter* mBlurFilter = nullptr;
235
Alec Mouri554d06e2018-12-20 00:15:33 -0800236 class FlushTracer {
237 public:
238 FlushTracer(GLESRenderEngine* engine);
239 ~FlushTracer();
240 void queueSync(EGLSyncKHR sync) EXCLUDES(mMutex);
241
242 struct QueueEntry {
243 EGLSyncKHR mSync = nullptr;
244 uint64_t mFrameNum = 0;
245 };
246
247 private:
248 void loop();
249 GLESRenderEngine* const mEngine;
250 std::thread mThread;
251 std::condition_variable_any mCondition;
252 std::mutex mMutex;
253 std::queue<QueueEntry> mQueue GUARDED_BY(mMutex);
254 uint64_t mFramesQueued GUARDED_BY(mMutex) = 0;
255 bool mRunning = true;
256 };
257 friend class FlushTracer;
Alec Mouri16a99402019-07-29 16:37:30 -0700258 friend class ImageManager;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800259 friend class GLFramebuffer;
260 friend class BlurFilter;
261 friend class GaussianBlurFilter;
262 friend class LensBlurFilter;
263 friend class GenericProgram;
Alec Mouri554d06e2018-12-20 00:15:33 -0800264 std::unique_ptr<FlushTracer> mFlushTracer;
Alec Mouri16a99402019-07-29 16:37:30 -0700265 std::unique_ptr<ImageManager> mImageManager = std::make_unique<ImageManager>(this);
Mathias Agopian3f844832013-08-07 21:24:32 -0700266};
267
Peiyong Lin46080ef2018-10-26 18:43:14 -0700268} // namespace gl
269} // namespace renderengine
270} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700271
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800272#endif /* SF_GLESRENDERENGINE_H_ */