blob: 0b0bbf7688727781eef1bae7358894e78b9f8128 [file] [log] [blame]
Mathias Agopian875d8e12013-06-07 15:35:48 -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
Mathias Agopian875d8e12013-06-07 15:35:48 -070017#ifndef SF_RENDERENGINE_H_
18#define SF_RENDERENGINE_H_
19
Chia-I Wub2c76242017-11-09 17:17:07 -080020#include <memory>
21
Mathias Agopian875d8e12013-06-07 15:35:48 -070022#include <stdint.h>
23#include <sys/types.h>
24
25#include <EGL/egl.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070026#include <EGL/eglext.h>
Riley Andrewsc3ebe662014-09-04 16:20:31 -070027#include <Transform.h>
Chia-I Wu767fcf72017-11-30 22:07:38 -080028#include <android-base/unique_fd.h>
chaviw13fdc492017-06-27 12:40:18 -070029#include <gui/SurfaceControl.h>
Chia-I Wub027f802017-11-29 14:00:52 -080030#include <math/mat4.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070031
Jesse Hall19e87292013-12-23 21:02:15 -080032#define EGL_NO_CONFIG ((EGLConfig)0)
33
Chia-I Wueadbaa62017-11-09 11:26:15 -080034struct ANativeWindowBuffer;
35
Mathias Agopian875d8e12013-06-07 15:35:48 -070036// ---------------------------------------------------------------------------
37namespace android {
38// ---------------------------------------------------------------------------
39
40class String8;
Mathias Agopian3f844832013-08-07 21:24:32 -070041class Rect;
42class Region;
43class Mesh;
Mathias Agopian49457ac2013-08-14 18:20:17 -070044class Texture;
Mathias Agopian875d8e12013-06-07 15:35:48 -070045
Chia-I Wuf846a352017-11-10 09:22:52 -080046namespace RE {
Lloyd Pique144e1162017-12-20 16:44:52 -080047
Chia-I Wu401ef832017-12-01 10:52:22 -080048class Image;
Chia-I Wuf846a352017-11-10 09:22:52 -080049class Surface;
Lloyd Pique144e1162017-12-20 16:44:52 -080050class BindNativeBufferAsFramebuffer;
51
52namespace impl {
53class RenderEngine;
54}
Chia-I Wuf846a352017-11-10 09:22:52 -080055
Mathias Agopian875d8e12013-06-07 15:35:48 -070056class RenderEngine {
Mathias Agopian875d8e12013-06-07 15:35:48 -070057public:
Kalle Raitabbdcf1f2017-05-22 15:47:46 -070058 enum FeatureFlag {
59 WIDE_COLOR_SUPPORT = 1 << 0 // Platform has a wide color display
60 };
Jesse Hall05f8c702013-12-23 20:44:38 -080061
Lloyd Pique144e1162017-12-20 16:44:52 -080062 virtual ~RenderEngine() = 0;
Mathias Agopian875d8e12013-06-07 15:35:48 -070063
Lloyd Pique144e1162017-12-20 16:44:52 -080064 virtual std::unique_ptr<RE::Surface> createSurface() = 0;
65 virtual std::unique_ptr<RE::Image> createImage() = 0;
66
67 virtual void primeCache() const = 0;
Dan Stoza4e637772016-07-28 13:31:51 -070068
Mathias Agopian458197d2013-08-15 14:56:51 -070069 // dump the extension strings. always call the base class.
Lloyd Pique144e1162017-12-20 16:44:52 -080070 virtual void dump(String8& result) = 0;
Mathias Agopian458197d2013-08-15 14:56:51 -070071
Lloyd Pique144e1162017-12-20 16:44:52 -080072 virtual bool isCurrent() const = 0;
73 virtual bool setCurrentSurface(const RE::Surface& surface) = 0;
74 virtual void resetCurrentSurface() = 0;
Chia-I Wu9f2db772017-11-30 21:06:50 -080075
Lloyd Pique144e1162017-12-20 16:44:52 -080076 // helpers
Chia-I Wu767fcf72017-11-30 22:07:38 -080077 // flush submits RenderEngine command stream for execution and returns a
78 // native fence fd that is signaled when the execution has completed. It
79 // returns -1 on errors.
Lloyd Pique144e1162017-12-20 16:44:52 -080080 virtual base::unique_fd flush() = 0;
Chia-I Wu767fcf72017-11-30 22:07:38 -080081 // finish waits until RenderEngine command stream has been executed. It
82 // returns false on errors.
Lloyd Pique144e1162017-12-20 16:44:52 -080083 virtual bool finish() = 0;
Chia-I Wu767fcf72017-11-30 22:07:38 -080084 // waitFence inserts a wait on an external fence fd to RenderEngine
85 // command stream. It returns false on errors.
Lloyd Pique144e1162017-12-20 16:44:52 -080086 virtual bool waitFence(base::unique_fd fenceFd) = 0;
Chia-I Wu767fcf72017-11-30 22:07:38 -080087
Lloyd Pique144e1162017-12-20 16:44:52 -080088 virtual void clearWithColor(float red, float green, float blue, float alpha) = 0;
89 virtual void fillRegionWithColor(const Region& region, uint32_t height, float red, float green,
90 float blue, float alpha) = 0;
Mathias Agopian875d8e12013-06-07 15:35:48 -070091
Mathias Agopian3f844832013-08-07 21:24:32 -070092 // common to all GL versions
Lloyd Pique144e1162017-12-20 16:44:52 -080093 virtual void setScissor(uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) = 0;
94 virtual void disableScissor() = 0;
95 virtual void genTextures(size_t count, uint32_t* names) = 0;
96 virtual void deleteTextures(size_t count, uint32_t const* names) = 0;
97 virtual void bindExternalTextureImage(uint32_t texName, const RE::Image& image) = 0;
98 virtual void readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) = 0;
99 virtual void bindNativeBufferAsFrameBuffer(ANativeWindowBuffer* buffer,
100 RE::BindNativeBufferAsFramebuffer* bindHelper) = 0;
101 virtual void unbindNativeBufferAsFrameBuffer(RE::BindNativeBufferAsFramebuffer* bindHelper) = 0;
Mathias Agopian3f844832013-08-07 21:24:32 -0700102
103 // set-up
104 virtual void checkErrors() const;
Chia-I Wub027f802017-11-29 14:00:52 -0800105 virtual void setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop, size_t hwh,
106 bool yswap, Transform::orientation_flags rotation) = 0;
107 virtual void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
108 const half4& color) = 0;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700109 virtual void setupLayerTexturing(const Texture& texture) = 0;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700110 virtual void setupLayerBlackedOut() = 0;
Mathias Agopian19733a32013-08-28 18:13:56 -0700111 virtual void setupFillWithColor(float r, float g, float b, float a) = 0;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700112
Chia-I Wu8e50e692018-05-04 10:12:37 -0700113 virtual void setupColorTransform(const mat4& /* colorTransform */) = 0;
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700114 virtual void setSaturationMatrix(const mat4& /* saturationMatrix */) = 0;
Dan Stozaf0087992014-10-20 15:46:09 -0700115
Mathias Agopian875d8e12013-06-07 15:35:48 -0700116 virtual void disableTexturing() = 0;
117 virtual void disableBlending() = 0;
118
Peiyong Linfb069302018-04-25 14:34:31 -0700119 // HDR and wide color gamut support
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800120 virtual void setSourceY410BT2020(bool enable) = 0;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700121 virtual void setSourceDataSpace(ui::Dataspace source) = 0;
122 virtual void setOutputDataSpace(ui::Dataspace dataspace) = 0;
Peiyong Linfb069302018-04-25 14:34:31 -0700123 virtual void setDisplayMaxLuminance(const float maxLuminance) = 0;
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800124
Mathias Agopian3f844832013-08-07 21:24:32 -0700125 // drawing
Mathias Agopian3f844832013-08-07 21:24:32 -0700126 virtual void drawMesh(const Mesh& mesh) = 0;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700127
Mathias Agopian3f844832013-08-07 21:24:32 -0700128 // queries
Mathias Agopian875d8e12013-06-07 15:35:48 -0700129 virtual size_t getMaxTextureSize() const = 0;
130 virtual size_t getMaxViewportDims() const = 0;
Lloyd Pique144e1162017-12-20 16:44:52 -0800131};
132
133class BindNativeBufferAsFramebuffer {
134public:
135 BindNativeBufferAsFramebuffer(RenderEngine& engine, ANativeWindowBuffer* buffer)
136 : mEngine(engine) {
137 mEngine.bindNativeBufferAsFrameBuffer(buffer, this);
138 }
139 ~BindNativeBufferAsFramebuffer() { mEngine.unbindNativeBufferAsFrameBuffer(this); }
140 status_t getStatus() const { return mStatus; }
141
142protected:
143 friend impl::RenderEngine;
144
145 RenderEngine& mEngine;
146 EGLImageKHR mImage;
147 uint32_t mTexName, mFbName;
148 status_t mStatus;
149};
150
151namespace impl {
152
153class Image;
154class Surface;
155
156class RenderEngine : public RE::RenderEngine {
157 enum GlesVersion {
158 GLES_VERSION_1_0 = 0x10000,
159 GLES_VERSION_1_1 = 0x10001,
160 GLES_VERSION_2_0 = 0x20000,
161 GLES_VERSION_3_0 = 0x30000,
162 };
163 static GlesVersion parseGlesVersion(const char* str);
164
165 EGLDisplay mEGLDisplay;
166 EGLConfig mEGLConfig;
167 EGLContext mEGLContext;
168 void setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt);
169
170 static bool overrideUseContextPriorityFromConfig(bool useContextPriority);
171
172protected:
Chia-I Wu93e14df2018-06-04 10:10:17 -0700173 RenderEngine(uint32_t featureFlags);
174
175 const uint32_t mFeatureFlags;
Lloyd Pique144e1162017-12-20 16:44:52 -0800176
177public:
178 virtual ~RenderEngine() = 0;
179
180 static std::unique_ptr<RenderEngine> create(int hwcFormat, uint32_t featureFlags);
181
182 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
183
184 // RenderEngine interface implementation
185
186 std::unique_ptr<RE::Surface> createSurface() override;
187 std::unique_ptr<RE::Image> createImage() override;
188
189 void primeCache() const override;
190
191 // dump the extension strings. always call the base class.
192 void dump(String8& result) override;
193
Lloyd Pique144e1162017-12-20 16:44:52 -0800194 bool isCurrent() const;
195 bool setCurrentSurface(const RE::Surface& surface) override;
196 void resetCurrentSurface() override;
197
198 // synchronization
199
200 // flush submits RenderEngine command stream for execution and returns a
201 // native fence fd that is signaled when the execution has completed. It
202 // returns -1 on errors.
203 base::unique_fd flush() override;
204 // finish waits until RenderEngine command stream has been executed. It
205 // returns false on errors.
206 bool finish() override;
207 // waitFence inserts a wait on an external fence fd to RenderEngine
208 // command stream. It returns false on errors.
209 bool waitFence(base::unique_fd fenceFd) override;
210
211 // helpers
212 void clearWithColor(float red, float green, float blue, float alpha) override;
213 void fillRegionWithColor(const Region& region, uint32_t height, float red, float green,
214 float blue, float alpha) override;
215
216 // common to all GL versions
217 void setScissor(uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) override;
218 void disableScissor() override;
219 void genTextures(size_t count, uint32_t* names) override;
220 void deleteTextures(size_t count, uint32_t const* names) override;
221 void bindExternalTextureImage(uint32_t texName, const RE::Image& image) override;
222 void readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) override;
223
224 void checkErrors() const override;
225
Chia-I Wu8e50e692018-05-04 10:12:37 -0700226 void setupColorTransform(const mat4& /* colorTransform */) override {}
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700227 void setSaturationMatrix(const mat4& /* saturationMatrix */) override {}
Mathias Agopian875d8e12013-06-07 15:35:48 -0700228
Chia-I Wuf846a352017-11-10 09:22:52 -0800229 // internal to RenderEngine
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800230 EGLDisplay getEGLDisplay() const;
Jesse Hall05f8c702013-12-23 20:44:38 -0800231 EGLConfig getEGLConfig() const;
Lloyd Pique144e1162017-12-20 16:44:52 -0800232
233 // Common implementation
234 bool setCurrentSurface(const RE::impl::Surface& surface);
235 void bindExternalTextureImage(uint32_t texName, const RE::impl::Image& image);
236
237 void bindNativeBufferAsFrameBuffer(ANativeWindowBuffer* buffer,
238 RE::BindNativeBufferAsFramebuffer* bindHelper) override;
239 void unbindNativeBufferAsFrameBuffer(RE::BindNativeBufferAsFramebuffer* bindHelper) override;
240
241 // Overriden by each specialization
242 virtual void bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName, uint32_t* fbName,
243 uint32_t* status) = 0;
244 virtual void unbindFramebuffer(uint32_t texName, uint32_t fbName) = 0;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700245};
246
Lloyd Pique144e1162017-12-20 16:44:52 -0800247} // namespace impl
248} // namespace RE
249} // namespace android
Mathias Agopian875d8e12013-06-07 15:35:48 -0700250
251#endif /* SF_RENDERENGINE_H_ */