blob: 397bc1619b95dc9a3669508fb34580a102ac3393 [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;
Peiyong Linf1bada92018-08-29 09:39:31 -070049
Peiyong Lin7e219eb2018-12-03 05:40:42 -080050class GLESRenderEngine : public impl::RenderEngine {
Mathias Agopian3f844832013-08-07 21:24:32 -070051public:
Peiyong Lin4137a1d2019-10-09 10:39:09 -070052 static std::unique_ptr<GLESRenderEngine> create(const RenderEngineCreationArgs& args);
Peiyong Linf11f39b2018-09-05 14:37:41 -070053
Peiyong Lin4137a1d2019-10-09 10:39:09 -070054 GLESRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLConfig config,
55 EGLContext ctxt, EGLSurface dummy, EGLContext protectedContext,
56 EGLSurface protectedDummy);
Alec Mourid43ccab2019-03-13 12:23:45 -070057 ~GLESRenderEngine() override EXCLUDES(mRenderingMutex);
Mathias Agopian3f844832013-08-07 21:24:32 -070058
Peiyong Linf1bada92018-08-29 09:39:31 -070059 void primeCache() const override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070060 void genTextures(size_t count, uint32_t* names) override;
61 void deleteTextures(size_t count, uint32_t const* names) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070062 void bindExternalTextureImage(uint32_t texName, const Image& image) override;
Alec Mourid7b3a8b2019-03-21 11:44:18 -070063 status_t bindExternalTextureBuffer(uint32_t texName, const sp<GraphicBuffer>& buffer,
64 const sp<Fence>& fence) EXCLUDES(mRenderingMutex);
Alec Mouri16a99402019-07-29 16:37:30 -070065 void cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) EXCLUDES(mRenderingMutex);
Alec Mourif0497272019-03-11 15:53:11 -070066 void unbindExternalTextureBuffer(uint64_t bufferId) EXCLUDES(mRenderingMutex);
Peiyong Line5a9a7f2018-08-30 15:32:13 -070067 status_t bindFrameBuffer(Framebuffer* framebuffer) override;
68 void unbindFrameBuffer(Framebuffer* framebuffer) override;
Peiyong Linf1bada92018-08-29 09:39:31 -070069
Peiyong Linfb530cf2018-12-15 05:07:38 +000070 bool isProtected() const override { return mInProtectedContext; }
71 bool supportsProtectedContent() const override;
72 bool useProtectedContext(bool useProtectedContext) override;
Alec Mouri1089aed2018-10-25 21:33:57 -070073 status_t drawLayers(const DisplaySettings& display, const std::vector<LayerSettings>& layers,
Alec Mourife0d72b2019-03-21 14:05:56 -070074 ANativeWindowBuffer* buffer, const bool useFramebufferCache,
Alec Mouri3d7c5612019-07-09 13:51:37 -070075 base::unique_fd&& bufferFence, base::unique_fd* drawFence) override;
Alec Mouri6e57f682018-09-29 20:45:08 -070076
Peiyong Linf11f39b2018-09-05 14:37:41 -070077 EGLDisplay getEGLDisplay() const { return mEGLDisplay; }
Alec Mourida4cf3b2019-02-12 15:33:01 -080078 // Creates an output image for rendering to
Alec Mourife0d72b2019-03-21 14:05:56 -070079 EGLImageKHR createFramebufferImageIfNeeded(ANativeWindowBuffer* nativeBuffer, bool isProtected,
Alec Mourief44c2d2019-07-15 15:27:22 -070080 bool useFramebufferCache)
81 EXCLUDES(mFramebufferImageCacheMutex);
Peiyong Linf11f39b2018-09-05 14:37:41 -070082
Alec Mourid43ccab2019-03-13 12:23:45 -070083 // Test-only methods
84 // Returns true iff mImageCache contains an image keyed by bufferId
85 bool isImageCachedForTesting(uint64_t bufferId) EXCLUDES(mRenderingMutex);
86 // Returns true iff mFramebufferImageCache contains an image keyed by bufferId
Alec Mourief44c2d2019-07-15 15:27:22 -070087 bool isFramebufferImageCachedForTesting(uint64_t bufferId)
88 EXCLUDES(mFramebufferImageCacheMutex);
Alec Mouri16a99402019-07-29 16:37:30 -070089 // These are wrappers around public methods above, but exposing Barrier
90 // objects so that tests can block.
91 std::shared_ptr<ImageManager::Barrier> cacheExternalTextureBufferForTesting(
92 const sp<GraphicBuffer>& buffer);
93 std::shared_ptr<ImageManager::Barrier> unbindExternalTextureBufferForTesting(uint64_t bufferId);
Alec Mourid43ccab2019-03-13 12:23:45 -070094
Mathias Agopian3f844832013-08-07 21:24:32 -070095protected:
Alec Mouri820c7402019-01-23 13:02:39 -080096 Framebuffer* getFramebufferForDrawing() override;
Alec Mourief44c2d2019-07-15 15:27:22 -070097 void dump(std::string& result) override EXCLUDES(mRenderingMutex)
98 EXCLUDES(mFramebufferImageCacheMutex);
Peiyong Line5a9a7f2018-08-30 15:32:13 -070099 size_t getMaxTextureSize() const override;
100 size_t getMaxViewportDims() const override;
101
102private:
Peiyong Linf11f39b2018-09-05 14:37:41 -0700103 enum GlesVersion {
104 GLES_VERSION_1_0 = 0x10000,
105 GLES_VERSION_1_1 = 0x10001,
106 GLES_VERSION_2_0 = 0x20000,
107 GLES_VERSION_3_0 = 0x30000,
108 };
109
Alec Mouri8002fca2019-06-28 15:24:13 -0700110 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700111 static GlesVersion parseGlesVersion(const char* str);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800112 static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000113 EGLContext shareContext, bool useContextPriority,
114 Protection protection);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800115 static EGLSurface createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000116 int hwcFormat, Protection protection);
Alec Mouri8002fca2019-06-28 15:24:13 -0700117 std::unique_ptr<Framebuffer> createFramebuffer();
118 std::unique_ptr<Image> createImage();
119 void checkErrors() const;
Peiyong Lin1c097612019-03-22 16:21:46 -0700120 void setScissor(const Rect& region);
121 void disableScissor();
Alec Mouri554d06e2018-12-20 00:15:33 -0800122 bool waitSync(EGLSyncKHR sync, EGLint flags);
Alec Mouri16a99402019-07-29 16:37:30 -0700123 status_t cacheExternalTextureBufferInternal(const sp<GraphicBuffer>& buffer)
124 EXCLUDES(mRenderingMutex);
125 void unbindExternalTextureBufferInternal(uint64_t bufferId) EXCLUDES(mRenderingMutex);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700126
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700127 // A data space is considered HDR data space if it has BT2020 color space
128 // with PQ or HLG transfer function.
129 bool isHdrDataSpace(const ui::Dataspace dataSpace) const;
130 bool needsXYZTransformMatrix() const;
Alec Mouri1089aed2018-10-25 21:33:57 -0700131 // Defines the viewport, and sets the projection matrix to the projection
132 // defined by the clip.
133 void setViewportAndProjection(Rect viewport, Rect clip);
Alec Mouri539319f2018-12-19 17:56:23 -0800134 // Evicts stale images from the buffer cache.
135 void evictImages(const std::vector<LayerSettings>& layers);
Alec Mouri7c94edb2018-12-03 21:23:26 -0800136 // Computes the cropping window for the layer and sets up cropping
137 // coordinates for the mesh.
138 FloatRect setupLayerCropping(const LayerSettings& layer, Mesh& mesh);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700139
Peiyong Lin1c097612019-03-22 16:21:46 -0700140 // We do a special handling for rounded corners when it's possible to turn off blending
141 // for the majority of the layer. The rounded corners needs to turn on blending such that
142 // we can set the alpha value correctly, however, only the corners need this, and since
143 // blending is an expensive operation, we want to turn off blending when it's not necessary.
144 void handleRoundedCorners(const DisplaySettings& display, const LayerSettings& layer,
145 const Mesh& mesh);
Alec Mouri8002fca2019-06-28 15:24:13 -0700146 base::unique_fd flush();
147 bool finish();
148 bool waitFence(base::unique_fd fenceFd);
149 void clearWithColor(float red, float green, float blue, float alpha);
150 void fillRegionWithColor(const Region& region, float red, float green, float blue, float alpha);
151 void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
152 const half4& color, float cornerRadius);
153 void setupLayerTexturing(const Texture& texture);
154 void setupFillWithColor(float r, float g, float b, float a);
155 void setColorTransform(const mat4& colorTransform);
156 void disableTexturing();
157 void disableBlending();
158 void setupCornerRadiusCropSize(float width, float height);
159
160 // HDR and color management related functions and state
161 void setSourceY410BT2020(bool enable);
162 void setSourceDataSpace(ui::Dataspace source);
163 void setOutputDataSpace(ui::Dataspace dataspace);
164 void setDisplayMaxLuminance(const float maxLuminance);
165
166 // drawing
167 void drawMesh(const Mesh& mesh);
Peiyong Lin1c097612019-03-22 16:21:46 -0700168
Peiyong Linf11f39b2018-09-05 14:37:41 -0700169 EGLDisplay mEGLDisplay;
170 EGLConfig mEGLConfig;
171 EGLContext mEGLContext;
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800172 EGLSurface mDummySurface;
Peiyong Linfb530cf2018-12-15 05:07:38 +0000173 EGLContext mProtectedEGLContext;
174 EGLSurface mProtectedDummySurface;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700175 GLuint mProtectedTexName;
176 GLint mMaxViewportDims[2];
177 GLint mMaxTextureSize;
178 GLuint mVpWidth;
179 GLuint mVpHeight;
180 Description mState;
181
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700182 mat4 mSrgbToXyz;
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700183 mat4 mDisplayP3ToXyz;
Valerie Haueb8e0762018-11-06 10:10:42 -0800184 mat4 mBt2020ToXyz;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700185 mat4 mXyzToSrgb;
186 mat4 mXyzToDisplayP3;
187 mat4 mXyzToBt2020;
Valerie Haueb8e0762018-11-06 10:10:42 -0800188 mat4 mSrgbToDisplayP3;
189 mat4 mSrgbToBt2020;
190 mat4 mDisplayP3ToSrgb;
191 mat4 mDisplayP3ToBt2020;
192 mat4 mBt2020ToSrgb;
193 mat4 mBt2020ToDisplayP3;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700194
Peiyong Linfb530cf2018-12-15 05:07:38 +0000195 bool mInProtectedContext = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800196 // If set to true, then enables tracing flush() and finish() to systrace.
197 bool mTraceGpuCompletion = false;
Alec Mourida4cf3b2019-02-12 15:33:01 -0800198 // 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.
Alec Mourief44c2d2019-07-15 15:27:22 -0700203 std::deque<std::pair<uint64_t, EGLImageKHR>> mFramebufferImageCache
204 GUARDED_BY(mFramebufferImageCacheMutex);
205 // The only reason why we have this mutex is so that we don't segfault when
206 // dumping info.
207 std::mutex mFramebufferImageCacheMutex;
Peiyong Linfb069302018-04-25 14:34:31 -0700208
209 // Current dataspace of layer being rendered
210 ui::Dataspace mDataSpace = ui::Dataspace::UNKNOWN;
211
212 // Current output dataspace of the render engine
213 ui::Dataspace mOutputDataSpace = ui::Dataspace::UNKNOWN;
214
Peiyong Lin13effd12018-07-24 17:01:47 -0700215 // Whether device supports color management, currently color management
216 // supports sRGB, DisplayP3 color spaces.
217 const bool mUseColorManagement = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800218
Alec Mouri539319f2018-12-19 17:56:23 -0800219 // Cache of GL images that we'll store per GraphicBuffer ID
Alec Mourif0497272019-03-11 15:53:11 -0700220 std::unordered_map<uint64_t, std::unique_ptr<Image>> mImageCache GUARDED_BY(mRenderingMutex);
221 // Mutex guarding rendering operations, so that:
222 // 1. GL operations aren't interleaved, and
223 // 2. Internal state related to rendering that is potentially modified by
224 // multiple threads is guaranteed thread-safe.
225 std::mutex mRenderingMutex;
226
Alec Mouri820c7402019-01-23 13:02:39 -0800227 std::unique_ptr<Framebuffer> mDrawingBuffer;
228
Alec Mouri554d06e2018-12-20 00:15:33 -0800229 class FlushTracer {
230 public:
231 FlushTracer(GLESRenderEngine* engine);
232 ~FlushTracer();
233 void queueSync(EGLSyncKHR sync) EXCLUDES(mMutex);
234
235 struct QueueEntry {
236 EGLSyncKHR mSync = nullptr;
237 uint64_t mFrameNum = 0;
238 };
239
240 private:
241 void loop();
242 GLESRenderEngine* const mEngine;
243 std::thread mThread;
244 std::condition_variable_any mCondition;
245 std::mutex mMutex;
246 std::queue<QueueEntry> mQueue GUARDED_BY(mMutex);
247 uint64_t mFramesQueued GUARDED_BY(mMutex) = 0;
248 bool mRunning = true;
249 };
250 friend class FlushTracer;
Alec Mouri16a99402019-07-29 16:37:30 -0700251 friend class ImageManager;
Alec Mouri554d06e2018-12-20 00:15:33 -0800252 std::unique_ptr<FlushTracer> mFlushTracer;
Alec Mouri16a99402019-07-29 16:37:30 -0700253 std::unique_ptr<ImageManager> mImageManager = std::make_unique<ImageManager>(this);
Mathias Agopian3f844832013-08-07 21:24:32 -0700254};
255
Peiyong Lin46080ef2018-10-26 18:43:14 -0700256} // namespace gl
257} // namespace renderengine
258} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700259
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800260#endif /* SF_GLESRENDERENGINE_H_ */