blob: 7b72666f025df3e924404f375715f6232ecb3315 [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>
24#include <mutex>
25#include <queue>
26#include <thread>
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>
Peiyong Lincbc184f2018-08-22 13:24:10 -070031#include <renderengine/RenderEngine.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070032#include <renderengine/private/Description.h>
Alec Mouri539319f2018-12-19 17:56:23 -080033#include <unordered_map>
Mathias Agopian3f844832013-08-07 21:24:32 -070034
Peiyong Linf11f39b2018-09-05 14:37:41 -070035#define EGL_NO_CONFIG ((EGLConfig)0)
36
Mathias Agopian3f844832013-08-07 21:24:32 -070037namespace android {
Mathias Agopian3f844832013-08-07 21:24:32 -070038
Peiyong Lin833074a2018-08-28 11:53:54 -070039namespace renderengine {
40
Mathias Agopian3f844832013-08-07 21:24:32 -070041class Mesh;
Mathias Agopian49457ac2013-08-14 18:20:17 -070042class Texture;
Mathias Agopian3f844832013-08-07 21:24:32 -070043
Peiyong Lin833074a2018-08-28 11:53:54 -070044namespace gl {
Lloyd Pique144e1162017-12-20 16:44:52 -080045
Peiyong Linf1bada92018-08-29 09:39:31 -070046class GLImage;
Peiyong Linf1bada92018-08-29 09:39:31 -070047
Peiyong Lin7e219eb2018-12-03 05:40:42 -080048class GLESRenderEngine : public impl::RenderEngine {
Mathias Agopian3f844832013-08-07 21:24:32 -070049public:
Peiyong Lin7e219eb2018-12-03 05:40:42 -080050 static std::unique_ptr<GLESRenderEngine> create(int hwcFormat, uint32_t featureFlags);
Peiyong Linf11f39b2018-09-05 14:37:41 -070051 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
52
Peiyong Lin7e219eb2018-12-03 05:40:42 -080053 GLESRenderEngine(uint32_t featureFlags, // See RenderEngine::FeatureFlag
Peiyong Linfb530cf2018-12-15 05:07:38 +000054 EGLDisplay display, EGLConfig config, EGLContext ctxt, EGLSurface dummy,
55 EGLContext protectedContext, EGLSurface protectedDummy);
Peiyong Lin7e219eb2018-12-03 05:40:42 -080056 ~GLESRenderEngine() override;
Mathias Agopian3f844832013-08-07 21:24:32 -070057
Peiyong Line5a9a7f2018-08-30 15:32:13 -070058 std::unique_ptr<Framebuffer> createFramebuffer() override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070059 std::unique_ptr<Image> createImage() override;
Peiyong Linf1bada92018-08-29 09:39:31 -070060
61 void primeCache() const override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070062 bool isCurrent() const override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070063 base::unique_fd flush() override;
64 bool finish() override;
65 bool waitFence(base::unique_fd fenceFd) override;
66 void clearWithColor(float red, float green, float blue, float alpha) override;
Chia-I Wu28e3a252018-09-07 12:05:02 -070067 void fillRegionWithColor(const Region& region, float red, float green, float blue,
68 float alpha) override;
Alec Mouri05483a02018-09-10 21:03:42 +000069 void setScissor(const Rect& region) override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070070 void disableScissor() override;
71 void genTextures(size_t count, uint32_t* names) override;
72 void deleteTextures(size_t count, uint32_t const* names) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070073 void bindExternalTextureImage(uint32_t texName, const Image& image) override;
Alec Mourie7d1d4a2019-02-05 01:13:46 +000074 status_t bindExternalTextureBuffer(uint32_t texName, sp<GraphicBuffer> buffer, sp<Fence> fence,
75 bool readCache);
Peiyong Line5a9a7f2018-08-30 15:32:13 -070076 status_t bindFrameBuffer(Framebuffer* framebuffer) override;
77 void unbindFrameBuffer(Framebuffer* framebuffer) override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070078 void checkErrors() const override;
Peiyong Linf1bada92018-08-29 09:39:31 -070079
Peiyong Linfb530cf2018-12-15 05:07:38 +000080 bool isProtected() const override { return mInProtectedContext; }
81 bool supportsProtectedContent() const override;
82 bool useProtectedContext(bool useProtectedContext) override;
Alec Mouri1089aed2018-10-25 21:33:57 -070083 status_t drawLayers(const DisplaySettings& display, const std::vector<LayerSettings>& layers,
84 ANativeWindowBuffer* buffer, base::unique_fd* drawFence) override;
Alec Mouri6e57f682018-09-29 20:45:08 -070085
Peiyong Linf11f39b2018-09-05 14:37:41 -070086 // internal to RenderEngine
87 EGLDisplay getEGLDisplay() const { return mEGLDisplay; }
88 EGLConfig getEGLConfig() const { return mEGLConfig; }
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 void setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
94 ui::Transform::orientation_flags rotation) override;
95 void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
Lucas Dupin1b6531c2018-07-05 17:18:21 -070096 const half4& color, float cornerRadius) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070097 void setupLayerTexturing(const Texture& texture) override;
98 void setupLayerBlackedOut() override;
99 void setupFillWithColor(float r, float g, float b, float a) override;
Peiyong Lind3788632018-09-18 16:01:31 -0700100 void setColorTransform(const mat4& colorTransform) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700101 void disableTexturing() override;
102 void disableBlending() override;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700103 void setupCornerRadiusCropSize(float width, float height) override;
Mathias Agopian3f844832013-08-07 21:24:32 -0700104
Peiyong Linf11f39b2018-09-05 14:37:41 -0700105 // HDR and color management related functions and state
106 void setSourceY410BT2020(bool enable) override;
107 void setSourceDataSpace(ui::Dataspace source) override;
108 void setOutputDataSpace(ui::Dataspace dataspace) override;
109 void setDisplayMaxLuminance(const float maxLuminance) override;
110
111 // drawing
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700112 void drawMesh(const Mesh& mesh) override;
Mathias Agopian3f844832013-08-07 21:24:32 -0700113
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700114 size_t getMaxTextureSize() const override;
115 size_t getMaxViewportDims() const override;
116
117private:
Peiyong Linf11f39b2018-09-05 14:37:41 -0700118 enum GlesVersion {
119 GLES_VERSION_1_0 = 0x10000,
120 GLES_VERSION_1_1 = 0x10001,
121 GLES_VERSION_2_0 = 0x20000,
122 GLES_VERSION_3_0 = 0x30000,
123 };
124
125 static GlesVersion parseGlesVersion(const char* str);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800126 static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000127 EGLContext shareContext, bool useContextPriority,
128 Protection protection);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800129 static EGLSurface createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000130 int hwcFormat, Protection protection);
Alec Mouri554d06e2018-12-20 00:15:33 -0800131 bool waitSync(EGLSyncKHR sync, EGLint flags);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700132
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700133 // A data space is considered HDR data space if it has BT2020 color space
134 // with PQ or HLG transfer function.
135 bool isHdrDataSpace(const ui::Dataspace dataSpace) const;
136 bool needsXYZTransformMatrix() const;
Alec Mouri1089aed2018-10-25 21:33:57 -0700137 // Defines the viewport, and sets the projection matrix to the projection
138 // defined by the clip.
139 void setViewportAndProjection(Rect viewport, Rect clip);
Alec Mouri539319f2018-12-19 17:56:23 -0800140 status_t bindExternalTextureBuffer(uint32_t texName, sp<GraphicBuffer> buffer, sp<Fence> fence,
141 bool readCache, bool persistCache);
142 // Evicts stale images from the buffer cache.
143 void evictImages(const std::vector<LayerSettings>& layers);
Alec Mouri7c94edb2018-12-03 21:23:26 -0800144 // Computes the cropping window for the layer and sets up cropping
145 // coordinates for the mesh.
146 FloatRect setupLayerCropping(const LayerSettings& layer, Mesh& mesh);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700147
Peiyong Linf11f39b2018-09-05 14:37:41 -0700148 EGLDisplay mEGLDisplay;
149 EGLConfig mEGLConfig;
150 EGLContext mEGLContext;
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800151 EGLSurface mDummySurface;
Peiyong Linfb530cf2018-12-15 05:07:38 +0000152 EGLContext mProtectedEGLContext;
153 EGLSurface mProtectedDummySurface;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700154 GLuint mProtectedTexName;
155 GLint mMaxViewportDims[2];
156 GLint mMaxTextureSize;
157 GLuint mVpWidth;
158 GLuint mVpHeight;
159 Description mState;
160
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700161 mat4 mSrgbToXyz;
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700162 mat4 mDisplayP3ToXyz;
Valerie Haueb8e0762018-11-06 10:10:42 -0800163 mat4 mBt2020ToXyz;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700164 mat4 mXyzToSrgb;
165 mat4 mXyzToDisplayP3;
166 mat4 mXyzToBt2020;
Valerie Haueb8e0762018-11-06 10:10:42 -0800167 mat4 mSrgbToDisplayP3;
168 mat4 mSrgbToBt2020;
169 mat4 mDisplayP3ToSrgb;
170 mat4 mDisplayP3ToBt2020;
171 mat4 mBt2020ToSrgb;
172 mat4 mBt2020ToDisplayP3;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700173
Peiyong Linfb530cf2018-12-15 05:07:38 +0000174 bool mInProtectedContext = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800175 // If set to true, then enables tracing flush() and finish() to systrace.
176 bool mTraceGpuCompletion = false;
Alec Mouri05483a02018-09-10 21:03:42 +0000177 int32_t mFboHeight = 0;
Peiyong Linfb069302018-04-25 14:34:31 -0700178
179 // Current dataspace of layer being rendered
180 ui::Dataspace mDataSpace = ui::Dataspace::UNKNOWN;
181
182 // Current output dataspace of the render engine
183 ui::Dataspace mOutputDataSpace = ui::Dataspace::UNKNOWN;
184
Peiyong Lin13effd12018-07-24 17:01:47 -0700185 // Whether device supports color management, currently color management
186 // supports sRGB, DisplayP3 color spaces.
187 const bool mUseColorManagement = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800188
Alec Mouri539319f2018-12-19 17:56:23 -0800189 // Cache of GL images that we'll store per GraphicBuffer ID
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000190 // TODO: Layer should call back on destruction instead to clean this up,
191 // as it has better system utilization at the potential expense of a
192 // more complicated interface.
Alec Mouri539319f2018-12-19 17:56:23 -0800193 std::unordered_map<uint64_t, std::unique_ptr<Image>> mImageCache;
194
Alec Mouri820c7402019-01-23 13:02:39 -0800195 std::unique_ptr<Framebuffer> mDrawingBuffer;
196
Alec Mouri554d06e2018-12-20 00:15:33 -0800197 class FlushTracer {
198 public:
199 FlushTracer(GLESRenderEngine* engine);
200 ~FlushTracer();
201 void queueSync(EGLSyncKHR sync) EXCLUDES(mMutex);
202
203 struct QueueEntry {
204 EGLSyncKHR mSync = nullptr;
205 uint64_t mFrameNum = 0;
206 };
207
208 private:
209 void loop();
210 GLESRenderEngine* const mEngine;
211 std::thread mThread;
212 std::condition_variable_any mCondition;
213 std::mutex mMutex;
214 std::queue<QueueEntry> mQueue GUARDED_BY(mMutex);
215 uint64_t mFramesQueued GUARDED_BY(mMutex) = 0;
216 bool mRunning = true;
217 };
218 friend class FlushTracer;
219 std::unique_ptr<FlushTracer> mFlushTracer;
Mathias Agopian3f844832013-08-07 21:24:32 -0700220};
221
Peiyong Lin46080ef2018-10-26 18:43:14 -0700222} // namespace gl
223} // namespace renderengine
224} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700225
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800226#endif /* SF_GLESRENDERENGINE_H_ */