blob: 70b704abce94aaefd11a7d1308b9174aaa58b590 [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 <android-base/thread_annotations.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070021#include <stdint.h>
22#include <sys/types.h>
Alec Mouri554d06e2018-12-20 00:15:33 -080023#include <condition_variable>
Alec Mourida4cf3b2019-02-12 15:33:01 -080024#include <deque>
Alec Mouri554d06e2018-12-20 00:15:33 -080025#include <mutex>
26#include <queue>
27#include <thread>
Alec Mourida4cf3b2019-02-12 15:33:01 -080028#include <unordered_map>
Mathias Agopian3f844832013-08-07 21:24:32 -070029
Peiyong Linf11f39b2018-09-05 14:37:41 -070030#include <EGL/egl.h>
31#include <EGL/eglext.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070032#include <GLES2/gl2.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070033#include <renderengine/RenderEngine.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070034#include <renderengine/private/Description.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070035
Peiyong Linf11f39b2018-09-05 14:37:41 -070036#define EGL_NO_CONFIG ((EGLConfig)0)
37
Mathias Agopian3f844832013-08-07 21:24:32 -070038namespace android {
Mathias Agopian3f844832013-08-07 21:24:32 -070039
Peiyong Lin833074a2018-08-28 11:53:54 -070040namespace renderengine {
41
Mathias Agopian3f844832013-08-07 21:24:32 -070042class Mesh;
Mathias Agopian49457ac2013-08-14 18:20:17 -070043class Texture;
Mathias Agopian3f844832013-08-07 21:24:32 -070044
Peiyong Lin833074a2018-08-28 11:53:54 -070045namespace gl {
Lloyd Pique144e1162017-12-20 16:44:52 -080046
Peiyong Linf1bada92018-08-29 09:39:31 -070047class GLImage;
Peiyong Linf1bada92018-08-29 09:39:31 -070048
Peiyong Lin7e219eb2018-12-03 05:40:42 -080049class GLESRenderEngine : public impl::RenderEngine {
Mathias Agopian3f844832013-08-07 21:24:32 -070050public:
Alec Mourida4cf3b2019-02-12 15:33:01 -080051 static std::unique_ptr<GLESRenderEngine> create(int hwcFormat, uint32_t featureFlags,
52 uint32_t imageCacheSize);
Peiyong Linf11f39b2018-09-05 14:37:41 -070053
Peiyong Lin7e219eb2018-12-03 05:40:42 -080054 GLESRenderEngine(uint32_t featureFlags, // See RenderEngine::FeatureFlag
Peiyong Linfb530cf2018-12-15 05:07:38 +000055 EGLDisplay display, EGLConfig config, EGLContext ctxt, EGLSurface dummy,
Alec Mourida4cf3b2019-02-12 15:33:01 -080056 EGLContext protectedContext, EGLSurface protectedDummy,
57 uint32_t imageCacheSize);
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);
66 status_t 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,
76 base::unique_fd&& bufferFence, base::unique_fd* drawFence)
77 EXCLUDES(mRenderingMutex) override;
Alec Mouri6e57f682018-09-29 20:45:08 -070078
Peiyong Linf11f39b2018-09-05 14:37:41 -070079 EGLDisplay getEGLDisplay() const { return mEGLDisplay; }
Alec Mourida4cf3b2019-02-12 15:33:01 -080080 // Creates an output image for rendering to
Alec Mourife0d72b2019-03-21 14:05:56 -070081 EGLImageKHR createFramebufferImageIfNeeded(ANativeWindowBuffer* nativeBuffer, bool isProtected,
82 bool useFramebufferCache);
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
88 bool isFramebufferImageCachedForTesting(uint64_t bufferId) EXCLUDES(mRenderingMutex);
89
Mathias Agopian3f844832013-08-07 21:24:32 -070090protected:
Alec Mouri820c7402019-01-23 13:02:39 -080091 Framebuffer* getFramebufferForDrawing() override;
Yiwei Zhang5434a782018-12-05 18:06:32 -080092 void dump(std::string& result) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070093 size_t getMaxTextureSize() const override;
94 size_t getMaxViewportDims() const override;
95
96private:
Peiyong Linf11f39b2018-09-05 14:37:41 -070097 enum GlesVersion {
98 GLES_VERSION_1_0 = 0x10000,
99 GLES_VERSION_1_1 = 0x10001,
100 GLES_VERSION_2_0 = 0x20000,
101 GLES_VERSION_3_0 = 0x30000,
102 };
103
Alec Mouri8002fca2019-06-28 15:24:13 -0700104 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700105 static GlesVersion parseGlesVersion(const char* str);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800106 static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000107 EGLContext shareContext, bool useContextPriority,
108 Protection protection);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800109 static EGLSurface createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000110 int hwcFormat, Protection protection);
Alec Mouri8002fca2019-06-28 15:24:13 -0700111 std::unique_ptr<Framebuffer> createFramebuffer();
112 std::unique_ptr<Image> createImage();
113 void checkErrors() const;
Peiyong Lin1c097612019-03-22 16:21:46 -0700114 void setScissor(const Rect& region);
115 void disableScissor();
Alec Mouri554d06e2018-12-20 00:15:33 -0800116 bool waitSync(EGLSyncKHR sync, EGLint flags);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700117
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700118 // A data space is considered HDR data space if it has BT2020 color space
119 // with PQ or HLG transfer function.
120 bool isHdrDataSpace(const ui::Dataspace dataSpace) const;
121 bool needsXYZTransformMatrix() const;
Alec Mouri1089aed2018-10-25 21:33:57 -0700122 // Defines the viewport, and sets the projection matrix to the projection
123 // defined by the clip.
124 void setViewportAndProjection(Rect viewport, Rect clip);
Alec Mouri539319f2018-12-19 17:56:23 -0800125 // Evicts stale images from the buffer cache.
126 void evictImages(const std::vector<LayerSettings>& layers);
Alec Mouri7c94edb2018-12-03 21:23:26 -0800127 // Computes the cropping window for the layer and sets up cropping
128 // coordinates for the mesh.
129 FloatRect setupLayerCropping(const LayerSettings& layer, Mesh& mesh);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700130
Peiyong Lin1c097612019-03-22 16:21:46 -0700131 // We do a special handling for rounded corners when it's possible to turn off blending
132 // for the majority of the layer. The rounded corners needs to turn on blending such that
133 // we can set the alpha value correctly, however, only the corners need this, and since
134 // blending is an expensive operation, we want to turn off blending when it's not necessary.
135 void handleRoundedCorners(const DisplaySettings& display, const LayerSettings& layer,
136 const Mesh& mesh);
Alec Mouri8002fca2019-06-28 15:24:13 -0700137 base::unique_fd flush();
138 bool finish();
139 bool waitFence(base::unique_fd fenceFd);
140 void clearWithColor(float red, float green, float blue, float alpha);
141 void fillRegionWithColor(const Region& region, float red, float green, float blue, float alpha);
142 void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
143 const half4& color, float cornerRadius);
144 void setupLayerTexturing(const Texture& texture);
145 void setupFillWithColor(float r, float g, float b, float a);
146 void setColorTransform(const mat4& colorTransform);
147 void disableTexturing();
148 void disableBlending();
149 void setupCornerRadiusCropSize(float width, float height);
150
151 // HDR and color management related functions and state
152 void setSourceY410BT2020(bool enable);
153 void setSourceDataSpace(ui::Dataspace source);
154 void setOutputDataSpace(ui::Dataspace dataspace);
155 void setDisplayMaxLuminance(const float maxLuminance);
156
157 // drawing
158 void drawMesh(const Mesh& mesh);
Peiyong Lin1c097612019-03-22 16:21:46 -0700159
Peiyong Linf11f39b2018-09-05 14:37:41 -0700160 EGLDisplay mEGLDisplay;
161 EGLConfig mEGLConfig;
162 EGLContext mEGLContext;
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800163 EGLSurface mDummySurface;
Peiyong Linfb530cf2018-12-15 05:07:38 +0000164 EGLContext mProtectedEGLContext;
165 EGLSurface mProtectedDummySurface;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700166 GLuint mProtectedTexName;
167 GLint mMaxViewportDims[2];
168 GLint mMaxTextureSize;
169 GLuint mVpWidth;
170 GLuint mVpHeight;
171 Description mState;
172
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700173 mat4 mSrgbToXyz;
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700174 mat4 mDisplayP3ToXyz;
Valerie Haueb8e0762018-11-06 10:10:42 -0800175 mat4 mBt2020ToXyz;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700176 mat4 mXyzToSrgb;
177 mat4 mXyzToDisplayP3;
178 mat4 mXyzToBt2020;
Valerie Haueb8e0762018-11-06 10:10:42 -0800179 mat4 mSrgbToDisplayP3;
180 mat4 mSrgbToBt2020;
181 mat4 mDisplayP3ToSrgb;
182 mat4 mDisplayP3ToBt2020;
183 mat4 mBt2020ToSrgb;
184 mat4 mBt2020ToDisplayP3;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700185
Peiyong Linfb530cf2018-12-15 05:07:38 +0000186 bool mInProtectedContext = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800187 // If set to true, then enables tracing flush() and finish() to systrace.
188 bool mTraceGpuCompletion = false;
Alec Mourida4cf3b2019-02-12 15:33:01 -0800189 // Maximum size of mFramebufferImageCache. If more images would be cached, then (approximately)
190 // the last recently used buffer should be kicked out.
191 uint32_t mFramebufferImageCacheSize = 0;
192
193 // Cache of output images, keyed by corresponding GraphicBuffer ID.
194 std::deque<std::pair<uint64_t, EGLImageKHR>> mFramebufferImageCache;
Peiyong Linfb069302018-04-25 14:34:31 -0700195
196 // Current dataspace of layer being rendered
197 ui::Dataspace mDataSpace = ui::Dataspace::UNKNOWN;
198
199 // Current output dataspace of the render engine
200 ui::Dataspace mOutputDataSpace = ui::Dataspace::UNKNOWN;
201
Peiyong Lin13effd12018-07-24 17:01:47 -0700202 // Whether device supports color management, currently color management
203 // supports sRGB, DisplayP3 color spaces.
204 const bool mUseColorManagement = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800205
Alec Mouri539319f2018-12-19 17:56:23 -0800206 // Cache of GL images that we'll store per GraphicBuffer ID
Alec Mourif0497272019-03-11 15:53:11 -0700207 std::unordered_map<uint64_t, std::unique_ptr<Image>> mImageCache GUARDED_BY(mRenderingMutex);
208 // Mutex guarding rendering operations, so that:
209 // 1. GL operations aren't interleaved, and
210 // 2. Internal state related to rendering that is potentially modified by
211 // multiple threads is guaranteed thread-safe.
212 std::mutex mRenderingMutex;
213
214 // See bindExternalTextureBuffer above, but requiring that mRenderingMutex
215 // is held.
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700216 status_t bindExternalTextureBufferLocked(uint32_t texName, const sp<GraphicBuffer>& buffer,
217 const sp<Fence>& fence) REQUIRES(mRenderingMutex);
218 // See cacheExternalTextureBuffer above, but requiring that mRenderingMutex
219 // is held.
220 status_t cacheExternalTextureBufferLocked(const sp<GraphicBuffer>& buffer)
221 REQUIRES(mRenderingMutex);
Alec Mouri539319f2018-12-19 17:56:23 -0800222
Alec Mouri820c7402019-01-23 13:02:39 -0800223 std::unique_ptr<Framebuffer> mDrawingBuffer;
224
Alec Mouri554d06e2018-12-20 00:15:33 -0800225 class FlushTracer {
226 public:
227 FlushTracer(GLESRenderEngine* engine);
228 ~FlushTracer();
229 void queueSync(EGLSyncKHR sync) EXCLUDES(mMutex);
230
231 struct QueueEntry {
232 EGLSyncKHR mSync = nullptr;
233 uint64_t mFrameNum = 0;
234 };
235
236 private:
237 void loop();
238 GLESRenderEngine* const mEngine;
239 std::thread mThread;
240 std::condition_variable_any mCondition;
241 std::mutex mMutex;
242 std::queue<QueueEntry> mQueue GUARDED_BY(mMutex);
243 uint64_t mFramesQueued GUARDED_BY(mMutex) = 0;
244 bool mRunning = true;
245 };
246 friend class FlushTracer;
247 std::unique_ptr<FlushTracer> mFlushTracer;
Mathias Agopian3f844832013-08-07 21:24:32 -0700248};
249
Peiyong Lin46080ef2018-10-26 18:43:14 -0700250} // namespace gl
251} // namespace renderengine
252} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700253
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800254#endif /* SF_GLESRENDERENGINE_H_ */