blob: cd19931218b853ed21a1d2c4c0e39bb572b0417b [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>
Mathias Agopian3f844832013-08-07 21:24:32 -070033
Peiyong Linf11f39b2018-09-05 14:37:41 -070034#define EGL_NO_CONFIG ((EGLConfig)0)
35
Mathias Agopian3f844832013-08-07 21:24:32 -070036namespace android {
Mathias Agopian3f844832013-08-07 21:24:32 -070037
Peiyong Lin833074a2018-08-28 11:53:54 -070038namespace renderengine {
39
Mathias Agopian3f844832013-08-07 21:24:32 -070040class Mesh;
Mathias Agopian49457ac2013-08-14 18:20:17 -070041class Texture;
Mathias Agopian3f844832013-08-07 21:24:32 -070042
Peiyong Lin833074a2018-08-28 11:53:54 -070043namespace gl {
Lloyd Pique144e1162017-12-20 16:44:52 -080044
Peiyong Linf1bada92018-08-29 09:39:31 -070045class GLImage;
Peiyong Linf1bada92018-08-29 09:39:31 -070046
Peiyong Lin7e219eb2018-12-03 05:40:42 -080047class GLESRenderEngine : public impl::RenderEngine {
Mathias Agopian3f844832013-08-07 21:24:32 -070048public:
Peiyong Lin7e219eb2018-12-03 05:40:42 -080049 static std::unique_ptr<GLESRenderEngine> create(int hwcFormat, uint32_t featureFlags);
Peiyong Linf11f39b2018-09-05 14:37:41 -070050 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
51
Peiyong Lin7e219eb2018-12-03 05:40:42 -080052 GLESRenderEngine(uint32_t featureFlags, // See RenderEngine::FeatureFlag
Peiyong Linfb530cf2018-12-15 05:07:38 +000053 EGLDisplay display, EGLConfig config, EGLContext ctxt, EGLSurface dummy,
54 EGLContext protectedContext, EGLSurface protectedDummy);
Peiyong Lin7e219eb2018-12-03 05:40:42 -080055 ~GLESRenderEngine() override;
Mathias Agopian3f844832013-08-07 21:24:32 -070056
Peiyong Line5a9a7f2018-08-30 15:32:13 -070057 std::unique_ptr<Framebuffer> createFramebuffer() override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070058 std::unique_ptr<Image> createImage() override;
Peiyong Linf1bada92018-08-29 09:39:31 -070059
60 void primeCache() const override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070061 bool isCurrent() const override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070062 base::unique_fd flush() override;
63 bool finish() override;
64 bool waitFence(base::unique_fd fenceFd) override;
65 void clearWithColor(float red, float green, float blue, float alpha) override;
Chia-I Wu28e3a252018-09-07 12:05:02 -070066 void fillRegionWithColor(const Region& region, float red, float green, float blue,
67 float alpha) override;
Alec Mouri05483a02018-09-10 21:03:42 +000068 void setScissor(const Rect& region) override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070069 void disableScissor() override;
70 void genTextures(size_t count, uint32_t* names) override;
71 void deleteTextures(size_t count, uint32_t const* names) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070072 void bindExternalTextureImage(uint32_t texName, const Image& image) override;
73 status_t bindFrameBuffer(Framebuffer* framebuffer) override;
74 void unbindFrameBuffer(Framebuffer* framebuffer) override;
Peiyong Lin60bedb52018-09-05 10:47:31 -070075 void checkErrors() const override;
Peiyong Linf1bada92018-08-29 09:39:31 -070076
Peiyong Linfb530cf2018-12-15 05:07:38 +000077 bool isProtected() const override { return mInProtectedContext; }
78 bool supportsProtectedContent() const override;
79 bool useProtectedContext(bool useProtectedContext) override;
Alec Mouri1089aed2018-10-25 21:33:57 -070080 status_t drawLayers(const DisplaySettings& display, const std::vector<LayerSettings>& layers,
81 ANativeWindowBuffer* buffer, base::unique_fd* drawFence) override;
Alec Mouri6e57f682018-09-29 20:45:08 -070082
Peiyong Linf11f39b2018-09-05 14:37:41 -070083 // internal to RenderEngine
84 EGLDisplay getEGLDisplay() const { return mEGLDisplay; }
85 EGLConfig getEGLConfig() const { return mEGLConfig; }
86
Mathias Agopian3f844832013-08-07 21:24:32 -070087protected:
Yiwei Zhang5434a782018-12-05 18:06:32 -080088 void dump(std::string& result) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070089 void setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
90 ui::Transform::orientation_flags rotation) override;
91 void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
Lucas Dupin1b6531c2018-07-05 17:18:21 -070092 const half4& color, float cornerRadius) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070093 void setupLayerTexturing(const Texture& texture) override;
94 void setupLayerBlackedOut() override;
95 void setupFillWithColor(float r, float g, float b, float a) override;
Peiyong Lind3788632018-09-18 16:01:31 -070096 void setColorTransform(const mat4& colorTransform) override;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070097 void disableTexturing() override;
98 void disableBlending() override;
Lucas Dupin1b6531c2018-07-05 17:18:21 -070099 void setupCornerRadiusCropSize(float width, float height) override;
Mathias Agopian3f844832013-08-07 21:24:32 -0700100
Peiyong Linf11f39b2018-09-05 14:37:41 -0700101 // HDR and color management related functions and state
102 void setSourceY410BT2020(bool enable) override;
103 void setSourceDataSpace(ui::Dataspace source) override;
104 void setOutputDataSpace(ui::Dataspace dataspace) override;
105 void setDisplayMaxLuminance(const float maxLuminance) override;
106
107 // drawing
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700108 void drawMesh(const Mesh& mesh) override;
Mathias Agopian3f844832013-08-07 21:24:32 -0700109
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700110 size_t getMaxTextureSize() const override;
111 size_t getMaxViewportDims() const override;
112
113private:
Peiyong Linf11f39b2018-09-05 14:37:41 -0700114 enum GlesVersion {
115 GLES_VERSION_1_0 = 0x10000,
116 GLES_VERSION_1_1 = 0x10001,
117 GLES_VERSION_2_0 = 0x20000,
118 GLES_VERSION_3_0 = 0x30000,
119 };
120
121 static GlesVersion parseGlesVersion(const char* str);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800122 static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000123 EGLContext shareContext, bool useContextPriority,
124 Protection protection);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800125 static EGLSurface createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config,
Peiyong Linfb530cf2018-12-15 05:07:38 +0000126 int hwcFormat, Protection protection);
Alec Mouri554d06e2018-12-20 00:15:33 -0800127 bool waitSync(EGLSyncKHR sync, EGLint flags);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700128
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700129 // A data space is considered HDR data space if it has BT2020 color space
130 // with PQ or HLG transfer function.
131 bool isHdrDataSpace(const ui::Dataspace dataSpace) const;
132 bool needsXYZTransformMatrix() const;
Alec Mouri1089aed2018-10-25 21:33:57 -0700133 // Defines the viewport, and sets the projection matrix to the projection
134 // defined by the clip.
135 void setViewportAndProjection(Rect viewport, Rect clip);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800136 status_t bindExternalTextureBuffer(uint32_t texName, sp<GraphicBuffer> buffer, sp<Fence> fence);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700137
Peiyong Linf11f39b2018-09-05 14:37:41 -0700138 EGLDisplay mEGLDisplay;
139 EGLConfig mEGLConfig;
140 EGLContext mEGLContext;
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800141 EGLSurface mDummySurface;
Peiyong Linfb530cf2018-12-15 05:07:38 +0000142 EGLContext mProtectedEGLContext;
143 EGLSurface mProtectedDummySurface;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700144 GLuint mProtectedTexName;
145 GLint mMaxViewportDims[2];
146 GLint mMaxTextureSize;
147 GLuint mVpWidth;
148 GLuint mVpHeight;
149 Description mState;
150
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700151 mat4 mSrgbToXyz;
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700152 mat4 mDisplayP3ToXyz;
Valerie Haueb8e0762018-11-06 10:10:42 -0800153 mat4 mBt2020ToXyz;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700154 mat4 mXyzToSrgb;
155 mat4 mXyzToDisplayP3;
156 mat4 mXyzToBt2020;
Valerie Haueb8e0762018-11-06 10:10:42 -0800157 mat4 mSrgbToDisplayP3;
158 mat4 mSrgbToBt2020;
159 mat4 mDisplayP3ToSrgb;
160 mat4 mDisplayP3ToBt2020;
161 mat4 mBt2020ToSrgb;
162 mat4 mBt2020ToDisplayP3;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700163
Peiyong Linfb530cf2018-12-15 05:07:38 +0000164 bool mInProtectedContext = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800165 // If set to true, then enables tracing flush() and finish() to systrace.
166 bool mTraceGpuCompletion = false;
Alec Mouri05483a02018-09-10 21:03:42 +0000167 int32_t mFboHeight = 0;
Peiyong Linfb069302018-04-25 14:34:31 -0700168
169 // Current dataspace of layer being rendered
170 ui::Dataspace mDataSpace = ui::Dataspace::UNKNOWN;
171
172 // Current output dataspace of the render engine
173 ui::Dataspace mOutputDataSpace = ui::Dataspace::UNKNOWN;
174
Peiyong Lin13effd12018-07-24 17:01:47 -0700175 // Whether device supports color management, currently color management
176 // supports sRGB, DisplayP3 color spaces.
177 const bool mUseColorManagement = false;
Alec Mouri554d06e2018-12-20 00:15:33 -0800178
179 class FlushTracer {
180 public:
181 FlushTracer(GLESRenderEngine* engine);
182 ~FlushTracer();
183 void queueSync(EGLSyncKHR sync) EXCLUDES(mMutex);
184
185 struct QueueEntry {
186 EGLSyncKHR mSync = nullptr;
187 uint64_t mFrameNum = 0;
188 };
189
190 private:
191 void loop();
192 GLESRenderEngine* const mEngine;
193 std::thread mThread;
194 std::condition_variable_any mCondition;
195 std::mutex mMutex;
196 std::queue<QueueEntry> mQueue GUARDED_BY(mMutex);
197 uint64_t mFramesQueued GUARDED_BY(mMutex) = 0;
198 bool mRunning = true;
199 };
200 friend class FlushTracer;
201 std::unique_ptr<FlushTracer> mFlushTracer;
Mathias Agopian3f844832013-08-07 21:24:32 -0700202};
203
Peiyong Lin46080ef2018-10-26 18:43:14 -0700204} // namespace gl
205} // namespace renderengine
206} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700207
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800208#endif /* SF_GLESRENDERENGINE_H_ */