blob: e37c91d8f8bd48fd5848bf26ff245368e99406fd [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 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
54
Peiyong Lin7e219eb2018-12-03 05:40:42 -080055 GLESRenderEngine(uint32_t featureFlags, // See RenderEngine::FeatureFlag
Peiyong Linfb530cf2018-12-15 05:07:38 +000056 EGLDisplay display, EGLConfig config, EGLContext ctxt, EGLSurface dummy,
Alec Mourida4cf3b2019-02-12 15:33:01 -080057 EGLContext protectedContext, EGLSurface protectedDummy,
58 uint32_t imageCacheSize);
Peiyong Lin7e219eb2018-12-03 05:40:42 -080059 ~GLESRenderEngine() override;
Mathias Agopian3f844832013-08-07 21:24:32 -070060
Peiyong Line5a9a7f2018-08-30 15:32:13 -070061 std::unique_ptr<Framebuffer> createFramebuffer() override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070062 std::unique_ptr<Image> createImage() override;
Peiyong Linf1bada92018-08-29 09:39:31 -070063
64 void primeCache() const override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070065 bool isCurrent() const override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070066 base::unique_fd flush() override;
67 bool finish() override;
68 bool waitFence(base::unique_fd fenceFd) override;
69 void clearWithColor(float red, float green, float blue, float alpha) override;
Chia-I Wu28e3a252018-09-07 12:05:02 -070070 void fillRegionWithColor(const Region& region, float red, float green, float blue,
71 float alpha) override;
Alec Mouri05483a02018-09-10 21:03:42 +000072 void setScissor(const Rect& region) override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070073 void disableScissor() override;
74 void genTextures(size_t count, uint32_t* names) override;
75 void deleteTextures(size_t count, uint32_t const* names) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070076 void bindExternalTextureImage(uint32_t texName, const Image& image) override;
Alec Mourie7d1d4a2019-02-05 01:13:46 +000077 status_t bindExternalTextureBuffer(uint32_t texName, sp<GraphicBuffer> buffer, sp<Fence> fence,
78 bool readCache);
Peiyong Line5a9a7f2018-08-30 15:32:13 -070079 status_t bindFrameBuffer(Framebuffer* framebuffer) override;
80 void unbindFrameBuffer(Framebuffer* framebuffer) override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070081 void checkErrors() const override;
Peiyong Linf1bada92018-08-29 09:39:31 -070082
Peiyong Linfb530cf2018-12-15 05:07:38 +000083 bool isProtected() const override { return mInProtectedContext; }
84 bool supportsProtectedContent() const override;
85 bool useProtectedContext(bool useProtectedContext) override;
Alec Mouri1089aed2018-10-25 21:33:57 -070086 status_t drawLayers(const DisplaySettings& display, const std::vector<LayerSettings>& layers,
Alec Mouri6338c9d2019-02-07 16:57:51 -080087 ANativeWindowBuffer* buffer, base::unique_fd&& bufferFence,
88 base::unique_fd* drawFence) override;
Alec Mouri6e57f682018-09-29 20:45:08 -070089
Peiyong Linf11f39b2018-09-05 14:37:41 -070090 // internal to RenderEngine
91 EGLDisplay getEGLDisplay() const { return mEGLDisplay; }
92 EGLConfig getEGLConfig() const { return mEGLConfig; }
Alec Mourida4cf3b2019-02-12 15:33:01 -080093 // Creates an output image for rendering to
94 EGLImageKHR createFramebufferImageIfNeeded(ANativeWindowBuffer* nativeBuffer, bool isProtected);
Peiyong Linf11f39b2018-09-05 14:37:41 -070095
Mathias Agopian3f844832013-08-07 21:24:32 -070096protected:
Alec Mouri820c7402019-01-23 13:02:39 -080097 Framebuffer* getFramebufferForDrawing() override;
Yiwei Zhang5434a782018-12-05 18:06:32 -080098 void dump(std::string& result) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070099 void setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
100 ui::Transform::orientation_flags rotation) override;
101 void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700102 const half4& color, float cornerRadius) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700103 void setupLayerTexturing(const Texture& texture) override;
104 void setupLayerBlackedOut() override;
105 void setupFillWithColor(float r, float g, float b, float a) override;
Peiyong Lind3788632018-09-18 16:01:31 -0700106 void setColorTransform(const mat4& colorTransform) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700107 void disableTexturing() override;
108 void disableBlending() override;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700109 void setupCornerRadiusCropSize(float width, float height) override;
Mathias Agopian3f844832013-08-07 21:24:32 -0700110
Peiyong Linf11f39b2018-09-05 14:37:41 -0700111 // HDR and color management related functions and state
112 void setSourceY410BT2020(bool enable) override;
113 void setSourceDataSpace(ui::Dataspace source) override;
114 void setOutputDataSpace(ui::Dataspace dataspace) override;
115 void setDisplayMaxLuminance(const float maxLuminance) override;
116
117 // drawing
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700118 void drawMesh(const Mesh& mesh) override;
Mathias Agopian3f844832013-08-07 21:24:32 -0700119
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700120 size_t getMaxTextureSize() const override;
121 size_t getMaxViewportDims() const override;
122
123private:
Peiyong Linf11f39b2018-09-05 14:37:41 -0700124 enum GlesVersion {
125 GLES_VERSION_1_0 = 0x10000,
126 GLES_VERSION_1_1 = 0x10001,
127 GLES_VERSION_2_0 = 0x20000,
128 GLES_VERSION_3_0 = 0x30000,
129 };
130
131 static GlesVersion parseGlesVersion(const char* str);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800132 static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000133 EGLContext shareContext, bool useContextPriority,
134 Protection protection);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800135 static EGLSurface createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000136 int hwcFormat, Protection protection);
Alec Mouri554d06e2018-12-20 00:15:33 -0800137 bool waitSync(EGLSyncKHR sync, EGLint flags);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700138
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700139 // A data space is considered HDR data space if it has BT2020 color space
140 // with PQ or HLG transfer function.
141 bool isHdrDataSpace(const ui::Dataspace dataSpace) const;
142 bool needsXYZTransformMatrix() const;
Alec Mouri1089aed2018-10-25 21:33:57 -0700143 // Defines the viewport, and sets the projection matrix to the projection
144 // defined by the clip.
145 void setViewportAndProjection(Rect viewport, Rect clip);
Alec Mouri539319f2018-12-19 17:56:23 -0800146 status_t bindExternalTextureBuffer(uint32_t texName, sp<GraphicBuffer> buffer, sp<Fence> fence,
147 bool readCache, bool persistCache);
148 // Evicts stale images from the buffer cache.
149 void evictImages(const std::vector<LayerSettings>& layers);
Alec Mouri7c94edb2018-12-03 21:23:26 -0800150 // Computes the cropping window for the layer and sets up cropping
151 // coordinates for the mesh.
152 FloatRect setupLayerCropping(const LayerSettings& layer, Mesh& mesh);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700153
Peiyong Linf11f39b2018-09-05 14:37:41 -0700154 EGLDisplay mEGLDisplay;
155 EGLConfig mEGLConfig;
156 EGLContext mEGLContext;
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800157 EGLSurface mDummySurface;
Peiyong Linfb530cf2018-12-15 05:07:38 +0000158 EGLContext mProtectedEGLContext;
159 EGLSurface mProtectedDummySurface;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700160 GLuint mProtectedTexName;
161 GLint mMaxViewportDims[2];
162 GLint mMaxTextureSize;
163 GLuint mVpWidth;
164 GLuint mVpHeight;
165 Description mState;
166
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700167 mat4 mSrgbToXyz;
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700168 mat4 mDisplayP3ToXyz;
Valerie Haueb8e0762018-11-06 10:10:42 -0800169 mat4 mBt2020ToXyz;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700170 mat4 mXyzToSrgb;
171 mat4 mXyzToDisplayP3;
172 mat4 mXyzToBt2020;
Valerie Haueb8e0762018-11-06 10:10:42 -0800173 mat4 mSrgbToDisplayP3;
174 mat4 mSrgbToBt2020;
175 mat4 mDisplayP3ToSrgb;
176 mat4 mDisplayP3ToBt2020;
177 mat4 mBt2020ToSrgb;
178 mat4 mBt2020ToDisplayP3;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700179
Peiyong Linfb530cf2018-12-15 05:07:38 +0000180 bool mInProtectedContext = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800181 // If set to true, then enables tracing flush() and finish() to systrace.
182 bool mTraceGpuCompletion = false;
Alec Mouri05483a02018-09-10 21:03:42 +0000183 int32_t mFboHeight = 0;
Alec Mourida4cf3b2019-02-12 15:33:01 -0800184 // Maximum size of mFramebufferImageCache. If more images would be cached, then (approximately)
185 // the last recently used buffer should be kicked out.
186 uint32_t mFramebufferImageCacheSize = 0;
187
188 // Cache of output images, keyed by corresponding GraphicBuffer ID.
189 std::deque<std::pair<uint64_t, EGLImageKHR>> mFramebufferImageCache;
Peiyong Linfb069302018-04-25 14:34:31 -0700190
191 // Current dataspace of layer being rendered
192 ui::Dataspace mDataSpace = ui::Dataspace::UNKNOWN;
193
194 // Current output dataspace of the render engine
195 ui::Dataspace mOutputDataSpace = ui::Dataspace::UNKNOWN;
196
Peiyong Lin13effd12018-07-24 17:01:47 -0700197 // Whether device supports color management, currently color management
198 // supports sRGB, DisplayP3 color spaces.
199 const bool mUseColorManagement = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800200
Alec Mouri539319f2018-12-19 17:56:23 -0800201 // Cache of GL images that we'll store per GraphicBuffer ID
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000202 // TODO: Layer should call back on destruction instead to clean this up,
203 // as it has better system utilization at the potential expense of a
204 // more complicated interface.
Alec Mouri539319f2018-12-19 17:56:23 -0800205 std::unordered_map<uint64_t, std::unique_ptr<Image>> mImageCache;
206
Alec Mouri820c7402019-01-23 13:02:39 -0800207 std::unique_ptr<Framebuffer> mDrawingBuffer;
208
Alec Mouri554d06e2018-12-20 00:15:33 -0800209 class FlushTracer {
210 public:
211 FlushTracer(GLESRenderEngine* engine);
212 ~FlushTracer();
213 void queueSync(EGLSyncKHR sync) EXCLUDES(mMutex);
214
215 struct QueueEntry {
216 EGLSyncKHR mSync = nullptr;
217 uint64_t mFrameNum = 0;
218 };
219
220 private:
221 void loop();
222 GLESRenderEngine* const mEngine;
223 std::thread mThread;
224 std::condition_variable_any mCondition;
225 std::mutex mMutex;
226 std::queue<QueueEntry> mQueue GUARDED_BY(mMutex);
227 uint64_t mFramesQueued GUARDED_BY(mMutex) = 0;
228 bool mRunning = true;
229 };
230 friend class FlushTracer;
231 std::unique_ptr<FlushTracer> mFlushTracer;
Mathias Agopian3f844832013-08-07 21:24:32 -0700232};
233
Peiyong Lin46080ef2018-10-26 18:43:14 -0700234} // namespace gl
235} // namespace renderengine
236} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700237
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800238#endif /* SF_GLESRENDERENGINE_H_ */