blob: dd60e50c67442ce0e9b5051c68552fa35e1bc9ed [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:
Alec Mourida4cf3b2019-02-12 15:33:01 -080052 static std::unique_ptr<GLESRenderEngine> create(int hwcFormat, uint32_t featureFlags,
53 uint32_t imageCacheSize);
Peiyong Linf11f39b2018-09-05 14:37:41 -070054 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
55
Peiyong Lin7e219eb2018-12-03 05:40:42 -080056 GLESRenderEngine(uint32_t featureFlags, // See RenderEngine::FeatureFlag
Peiyong Linfb530cf2018-12-15 05:07:38 +000057 EGLDisplay display, EGLConfig config, EGLContext ctxt, EGLSurface dummy,
Alec Mourida4cf3b2019-02-12 15:33:01 -080058 EGLContext protectedContext, EGLSurface protectedDummy,
59 uint32_t imageCacheSize);
Alec Mourid43ccab2019-03-13 12:23:45 -070060 ~GLESRenderEngine() override EXCLUDES(mRenderingMutex);
Mathias Agopian3f844832013-08-07 21:24:32 -070061
Peiyong Line5a9a7f2018-08-30 15:32:13 -070062 std::unique_ptr<Framebuffer> createFramebuffer() override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070063 std::unique_ptr<Image> createImage() override;
Peiyong Linf1bada92018-08-29 09:39:31 -070064
65 void primeCache() const override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070066 bool isCurrent() const override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070067 base::unique_fd flush() override;
68 bool finish() override;
69 bool waitFence(base::unique_fd fenceFd) override;
70 void clearWithColor(float red, float green, float blue, float alpha) override;
Chia-I Wu28e3a252018-09-07 12:05:02 -070071 void fillRegionWithColor(const Region& region, float red, float green, float blue,
72 float alpha) override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070073 void genTextures(size_t count, uint32_t* names) override;
74 void deleteTextures(size_t count, uint32_t const* names) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070075 void bindExternalTextureImage(uint32_t texName, const Image& image) override;
Alec Mourid7b3a8b2019-03-21 11:44:18 -070076 status_t bindExternalTextureBuffer(uint32_t texName, const sp<GraphicBuffer>& buffer,
77 const sp<Fence>& fence) EXCLUDES(mRenderingMutex);
Alec Mouri16a99402019-07-29 16:37:30 -070078 void cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) EXCLUDES(mRenderingMutex);
Alec Mourif0497272019-03-11 15:53:11 -070079 void unbindExternalTextureBuffer(uint64_t bufferId) EXCLUDES(mRenderingMutex);
Peiyong Line5a9a7f2018-08-30 15:32:13 -070080 status_t bindFrameBuffer(Framebuffer* framebuffer) override;
81 void unbindFrameBuffer(Framebuffer* framebuffer) override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070082 void checkErrors() const override;
Peiyong Linf1bada92018-08-29 09:39:31 -070083
Peiyong Linfb530cf2018-12-15 05:07:38 +000084 bool isProtected() const override { return mInProtectedContext; }
85 bool supportsProtectedContent() const override;
86 bool useProtectedContext(bool useProtectedContext) override;
Alec Mouri1089aed2018-10-25 21:33:57 -070087 status_t drawLayers(const DisplaySettings& display, const std::vector<LayerSettings>& layers,
Alec Mourife0d72b2019-03-21 14:05:56 -070088 ANativeWindowBuffer* buffer, const bool useFramebufferCache,
Alec Mouri3d7c5612019-07-09 13:51:37 -070089 base::unique_fd&& bufferFence, base::unique_fd* drawFence) override;
Alec Mouri6e57f682018-09-29 20:45:08 -070090
Peiyong Linf11f39b2018-09-05 14:37:41 -070091 // internal to RenderEngine
92 EGLDisplay getEGLDisplay() const { return mEGLDisplay; }
93 EGLConfig getEGLConfig() const { return mEGLConfig; }
Alec Mourida4cf3b2019-02-12 15:33:01 -080094 // Creates an output image for rendering to
Alec Mourife0d72b2019-03-21 14:05:56 -070095 EGLImageKHR createFramebufferImageIfNeeded(ANativeWindowBuffer* nativeBuffer, bool isProtected,
Alec Mouri192a3752019-07-15 15:27:22 -070096 bool useFramebufferCache)
97 EXCLUDES(mFramebufferImageCacheMutex);
Peiyong Linf11f39b2018-09-05 14:37:41 -070098
Alec Mourid43ccab2019-03-13 12:23:45 -070099 // Test-only methods
100 // Returns true iff mImageCache contains an image keyed by bufferId
101 bool isImageCachedForTesting(uint64_t bufferId) EXCLUDES(mRenderingMutex);
102 // Returns true iff mFramebufferImageCache contains an image keyed by bufferId
Alec Mouri192a3752019-07-15 15:27:22 -0700103 bool isFramebufferImageCachedForTesting(uint64_t bufferId)
104 EXCLUDES(mFramebufferImageCacheMutex);
Alec Mouri16a99402019-07-29 16:37:30 -0700105 // These are wrappers around public methods above, but exposing Barrier
106 // objects so that tests can block.
107 std::shared_ptr<ImageManager::Barrier> cacheExternalTextureBufferForTesting(
108 const sp<GraphicBuffer>& buffer);
109 std::shared_ptr<ImageManager::Barrier> unbindExternalTextureBufferForTesting(uint64_t bufferId);
Alec Mourid43ccab2019-03-13 12:23:45 -0700110
Mathias Agopian3f844832013-08-07 21:24:32 -0700111protected:
Alec Mouri820c7402019-01-23 13:02:39 -0800112 Framebuffer* getFramebufferForDrawing() override;
Alec Mouri192a3752019-07-15 15:27:22 -0700113 void dump(std::string& result) override EXCLUDES(mRenderingMutex)
114 EXCLUDES(mFramebufferImageCacheMutex);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700115 void setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
116 ui::Transform::orientation_flags rotation) override;
117 void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700118 const half4& color, float cornerRadius) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700119 void setupLayerTexturing(const Texture& texture) override;
120 void setupLayerBlackedOut() override;
121 void setupFillWithColor(float r, float g, float b, float a) override;
Peiyong Lind3788632018-09-18 16:01:31 -0700122 void setColorTransform(const mat4& colorTransform) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700123 void disableTexturing() override;
124 void disableBlending() override;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700125 void setupCornerRadiusCropSize(float width, float height) override;
Mathias Agopian3f844832013-08-07 21:24:32 -0700126
Peiyong Linf11f39b2018-09-05 14:37:41 -0700127 // HDR and color management related functions and state
128 void setSourceY410BT2020(bool enable) override;
129 void setSourceDataSpace(ui::Dataspace source) override;
130 void setOutputDataSpace(ui::Dataspace dataspace) override;
131 void setDisplayMaxLuminance(const float maxLuminance) override;
132
133 // drawing
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700134 void drawMesh(const Mesh& mesh) override;
Mathias Agopian3f844832013-08-07 21:24:32 -0700135
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700136 size_t getMaxTextureSize() const override;
137 size_t getMaxViewportDims() const override;
138
139private:
Peiyong Linf11f39b2018-09-05 14:37:41 -0700140 enum GlesVersion {
141 GLES_VERSION_1_0 = 0x10000,
142 GLES_VERSION_1_1 = 0x10001,
143 GLES_VERSION_2_0 = 0x20000,
144 GLES_VERSION_3_0 = 0x30000,
145 };
146
147 static GlesVersion parseGlesVersion(const char* str);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800148 static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000149 EGLContext shareContext, bool useContextPriority,
150 Protection protection);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800151 static EGLSurface createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000152 int hwcFormat, Protection protection);
Peiyong Lin1c097612019-03-22 16:21:46 -0700153 void setScissor(const Rect& region);
154 void disableScissor();
Alec Mouri554d06e2018-12-20 00:15:33 -0800155 bool waitSync(EGLSyncKHR sync, EGLint flags);
Alec Mouri16a99402019-07-29 16:37:30 -0700156 status_t cacheExternalTextureBufferInternal(const sp<GraphicBuffer>& buffer)
157 EXCLUDES(mRenderingMutex);
158 void unbindExternalTextureBufferInternal(uint64_t bufferId) EXCLUDES(mRenderingMutex);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700159
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700160 // A data space is considered HDR data space if it has BT2020 color space
161 // with PQ or HLG transfer function.
162 bool isHdrDataSpace(const ui::Dataspace dataSpace) const;
163 bool needsXYZTransformMatrix() const;
Alec Mouri1089aed2018-10-25 21:33:57 -0700164 // Defines the viewport, and sets the projection matrix to the projection
165 // defined by the clip.
166 void setViewportAndProjection(Rect viewport, Rect clip);
Alec Mouri539319f2018-12-19 17:56:23 -0800167 // Evicts stale images from the buffer cache.
168 void evictImages(const std::vector<LayerSettings>& layers);
Alec Mouri7c94edb2018-12-03 21:23:26 -0800169 // Computes the cropping window for the layer and sets up cropping
170 // coordinates for the mesh.
171 FloatRect setupLayerCropping(const LayerSettings& layer, Mesh& mesh);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700172
Peiyong Lin1c097612019-03-22 16:21:46 -0700173 // We do a special handling for rounded corners when it's possible to turn off blending
174 // for the majority of the layer. The rounded corners needs to turn on blending such that
175 // we can set the alpha value correctly, however, only the corners need this, and since
176 // blending is an expensive operation, we want to turn off blending when it's not necessary.
177 void handleRoundedCorners(const DisplaySettings& display, const LayerSettings& layer,
178 const Mesh& mesh);
179
Peiyong Linf11f39b2018-09-05 14:37:41 -0700180 EGLDisplay mEGLDisplay;
181 EGLConfig mEGLConfig;
182 EGLContext mEGLContext;
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800183 EGLSurface mDummySurface;
Peiyong Linfb530cf2018-12-15 05:07:38 +0000184 EGLContext mProtectedEGLContext;
185 EGLSurface mProtectedDummySurface;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700186 GLuint mProtectedTexName;
187 GLint mMaxViewportDims[2];
188 GLint mMaxTextureSize;
189 GLuint mVpWidth;
190 GLuint mVpHeight;
191 Description mState;
192
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700193 mat4 mSrgbToXyz;
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700194 mat4 mDisplayP3ToXyz;
Valerie Haueb8e0762018-11-06 10:10:42 -0800195 mat4 mBt2020ToXyz;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700196 mat4 mXyzToSrgb;
197 mat4 mXyzToDisplayP3;
198 mat4 mXyzToBt2020;
Valerie Haueb8e0762018-11-06 10:10:42 -0800199 mat4 mSrgbToDisplayP3;
200 mat4 mSrgbToBt2020;
201 mat4 mDisplayP3ToSrgb;
202 mat4 mDisplayP3ToBt2020;
203 mat4 mBt2020ToSrgb;
204 mat4 mBt2020ToDisplayP3;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700205
Peiyong Linfb530cf2018-12-15 05:07:38 +0000206 bool mInProtectedContext = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800207 // If set to true, then enables tracing flush() and finish() to systrace.
208 bool mTraceGpuCompletion = false;
Alec Mourida4cf3b2019-02-12 15:33:01 -0800209 // Maximum size of mFramebufferImageCache. If more images would be cached, then (approximately)
210 // the last recently used buffer should be kicked out.
211 uint32_t mFramebufferImageCacheSize = 0;
212
213 // Cache of output images, keyed by corresponding GraphicBuffer ID.
Alec Mouri192a3752019-07-15 15:27:22 -0700214 std::deque<std::pair<uint64_t, EGLImageKHR>> mFramebufferImageCache
215 GUARDED_BY(mFramebufferImageCacheMutex);
216 // The only reason why we have this mutex is so that we don't segfault when
217 // dumping info.
218 std::mutex mFramebufferImageCacheMutex;
Peiyong Linfb069302018-04-25 14:34:31 -0700219
220 // Current dataspace of layer being rendered
221 ui::Dataspace mDataSpace = ui::Dataspace::UNKNOWN;
222
223 // Current output dataspace of the render engine
224 ui::Dataspace mOutputDataSpace = ui::Dataspace::UNKNOWN;
225
Peiyong Lin13effd12018-07-24 17:01:47 -0700226 // Whether device supports color management, currently color management
227 // supports sRGB, DisplayP3 color spaces.
228 const bool mUseColorManagement = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800229
Alec Mouri539319f2018-12-19 17:56:23 -0800230 // Cache of GL images that we'll store per GraphicBuffer ID
Alec Mourif0497272019-03-11 15:53:11 -0700231 std::unordered_map<uint64_t, std::unique_ptr<Image>> mImageCache GUARDED_BY(mRenderingMutex);
232 // Mutex guarding rendering operations, so that:
233 // 1. GL operations aren't interleaved, and
234 // 2. Internal state related to rendering that is potentially modified by
235 // multiple threads is guaranteed thread-safe.
236 std::mutex mRenderingMutex;
237
Alec Mouri820c7402019-01-23 13:02:39 -0800238 std::unique_ptr<Framebuffer> mDrawingBuffer;
239
Alec Mouri554d06e2018-12-20 00:15:33 -0800240 class FlushTracer {
241 public:
242 FlushTracer(GLESRenderEngine* engine);
243 ~FlushTracer();
244 void queueSync(EGLSyncKHR sync) EXCLUDES(mMutex);
245
246 struct QueueEntry {
247 EGLSyncKHR mSync = nullptr;
248 uint64_t mFrameNum = 0;
249 };
250
251 private:
252 void loop();
253 GLESRenderEngine* const mEngine;
254 std::thread mThread;
255 std::condition_variable_any mCondition;
256 std::mutex mMutex;
257 std::queue<QueueEntry> mQueue GUARDED_BY(mMutex);
258 uint64_t mFramesQueued GUARDED_BY(mMutex) = 0;
259 bool mRunning = true;
260 };
261 friend class FlushTracer;
Alec Mouri16a99402019-07-29 16:37:30 -0700262 friend class ImageManager;
Alec Mouri554d06e2018-12-20 00:15:33 -0800263 std::unique_ptr<FlushTracer> mFlushTracer;
Alec Mouri16a99402019-07-29 16:37:30 -0700264 std::unique_ptr<ImageManager> mImageManager = std::make_unique<ImageManager>(this);
Mathias Agopian3f844832013-08-07 21:24:32 -0700265};
266
Peiyong Lin46080ef2018-10-26 18:43:14 -0700267} // namespace gl
268} // namespace renderengine
269} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700270
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800271#endif /* SF_GLESRENDERENGINE_H_ */