| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 17 | #ifndef SF_RENDERENGINE_H_ | 
|  | 18 | #define SF_RENDERENGINE_H_ | 
|  | 19 |  | 
| Chia-I Wu | b2c7624 | 2017-11-09 17:17:07 -0800 | [diff] [blame] | 20 | #include <memory> | 
|  | 21 |  | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 22 | #include <stdint.h> | 
|  | 23 | #include <sys/types.h> | 
|  | 24 |  | 
|  | 25 | #include <EGL/egl.h> | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 26 | #include <EGL/eglext.h> | 
| Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 27 | #include <Transform.h> | 
| Chia-I Wu | 767fcf7 | 2017-11-30 22:07:38 -0800 | [diff] [blame] | 28 | #include <android-base/unique_fd.h> | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 29 | #include <gui/SurfaceControl.h> | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 30 | #include <math/mat4.h> | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 31 |  | 
| Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 32 | #define EGL_NO_CONFIG ((EGLConfig)0) | 
|  | 33 |  | 
| Chia-I Wu | eadbaa6 | 2017-11-09 11:26:15 -0800 | [diff] [blame] | 34 | struct ANativeWindowBuffer; | 
|  | 35 |  | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 36 | // --------------------------------------------------------------------------- | 
|  | 37 | namespace android { | 
|  | 38 | // --------------------------------------------------------------------------- | 
|  | 39 |  | 
|  | 40 | class String8; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 41 | class Rect; | 
|  | 42 | class Region; | 
|  | 43 | class Mesh; | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 44 | class Texture; | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 45 |  | 
| Chia-I Wu | f846a35 | 2017-11-10 09:22:52 -0800 | [diff] [blame] | 46 | namespace RE { | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 47 |  | 
| Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 48 | class Image; | 
| Chia-I Wu | f846a35 | 2017-11-10 09:22:52 -0800 | [diff] [blame] | 49 | class Surface; | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 50 | class BindNativeBufferAsFramebuffer; | 
|  | 51 |  | 
|  | 52 | namespace impl { | 
|  | 53 | class RenderEngine; | 
|  | 54 | } | 
| Chia-I Wu | f846a35 | 2017-11-10 09:22:52 -0800 | [diff] [blame] | 55 |  | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 56 | class RenderEngine { | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 57 | public: | 
| Kalle Raita | bbdcf1f | 2017-05-22 15:47:46 -0700 | [diff] [blame] | 58 | enum FeatureFlag { | 
|  | 59 | WIDE_COLOR_SUPPORT = 1 << 0 // Platform has a wide color display | 
|  | 60 | }; | 
| Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 61 |  | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 62 | virtual ~RenderEngine() = 0; | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 63 |  | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 64 | 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 Stoza | 4e63777 | 2016-07-28 13:31:51 -0700 | [diff] [blame] | 68 |  | 
| Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 69 | // dump the extension strings. always call the base class. | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 70 | virtual void dump(String8& result) = 0; | 
| Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 71 |  | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 72 | virtual bool supportsImageCrop() const = 0; | 
| Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 73 |  | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 74 | virtual bool isCurrent() const = 0; | 
|  | 75 | virtual bool setCurrentSurface(const RE::Surface& surface) = 0; | 
|  | 76 | virtual void resetCurrentSurface() = 0; | 
| Chia-I Wu | 9f2db77 | 2017-11-30 21:06:50 -0800 | [diff] [blame] | 77 |  | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 78 | // helpers | 
| Chia-I Wu | 767fcf7 | 2017-11-30 22:07:38 -0800 | [diff] [blame] | 79 | // flush submits RenderEngine command stream for execution and returns a | 
|  | 80 | // native fence fd that is signaled when the execution has completed.  It | 
|  | 81 | // returns -1 on errors. | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 82 | virtual base::unique_fd flush() = 0; | 
| Chia-I Wu | 767fcf7 | 2017-11-30 22:07:38 -0800 | [diff] [blame] | 83 | // finish waits until RenderEngine command stream has been executed.  It | 
|  | 84 | // returns false on errors. | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 85 | virtual bool finish() = 0; | 
| Chia-I Wu | 767fcf7 | 2017-11-30 22:07:38 -0800 | [diff] [blame] | 86 | // waitFence inserts a wait on an external fence fd to RenderEngine | 
|  | 87 | // command stream.  It returns false on errors. | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 88 | virtual bool waitFence(base::unique_fd fenceFd) = 0; | 
| Chia-I Wu | 767fcf7 | 2017-11-30 22:07:38 -0800 | [diff] [blame] | 89 |  | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 90 | virtual void clearWithColor(float red, float green, float blue, float alpha) = 0; | 
|  | 91 | virtual void fillRegionWithColor(const Region& region, uint32_t height, float red, float green, | 
|  | 92 | float blue, float alpha) = 0; | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 93 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 94 | // common to all GL versions | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 95 | virtual void setScissor(uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) = 0; | 
|  | 96 | virtual void disableScissor() = 0; | 
|  | 97 | virtual void genTextures(size_t count, uint32_t* names) = 0; | 
|  | 98 | virtual void deleteTextures(size_t count, uint32_t const* names) = 0; | 
|  | 99 | virtual void bindExternalTextureImage(uint32_t texName, const RE::Image& image) = 0; | 
|  | 100 | virtual void readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) = 0; | 
|  | 101 | virtual void bindNativeBufferAsFrameBuffer(ANativeWindowBuffer* buffer, | 
|  | 102 | RE::BindNativeBufferAsFramebuffer* bindHelper) = 0; | 
|  | 103 | virtual void unbindNativeBufferAsFrameBuffer(RE::BindNativeBufferAsFramebuffer* bindHelper) = 0; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 104 |  | 
|  | 105 | // set-up | 
|  | 106 | virtual void checkErrors() const; | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 107 | virtual void setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop, size_t hwh, | 
|  | 108 | bool yswap, Transform::orientation_flags rotation) = 0; | 
|  | 109 | virtual void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture, | 
|  | 110 | const half4& color) = 0; | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 111 | virtual void setupLayerTexturing(const Texture& texture) = 0; | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 112 | virtual void setupLayerBlackedOut() = 0; | 
| Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 113 | virtual void setupFillWithColor(float r, float g, float b, float a) = 0; | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 114 |  | 
| Chia-I Wu | 8e50e69 | 2018-05-04 10:12:37 -0700 | [diff] [blame] | 115 | virtual void setupColorTransform(const mat4& /* colorTransform */) = 0; | 
| Peiyong Lin | 76dd77a | 2018-05-09 15:35:33 -0700 | [diff] [blame] | 116 | virtual void setSaturationMatrix(const mat4& /* saturationMatrix */) = 0; | 
| Dan Stoza | f008799 | 2014-10-20 15:46:09 -0700 | [diff] [blame] | 117 |  | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 118 | virtual void disableTexturing() = 0; | 
|  | 119 | virtual void disableBlending() = 0; | 
|  | 120 |  | 
| Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 121 | // HDR and wide color gamut support | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 122 | virtual void setSourceY410BT2020(bool enable) = 0; | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 123 | virtual void setSourceDataSpace(ui::Dataspace source) = 0; | 
|  | 124 | virtual void setOutputDataSpace(ui::Dataspace dataspace) = 0; | 
| Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 125 | virtual void setDisplayMaxLuminance(const float maxLuminance) = 0; | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 126 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 127 | // drawing | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 128 | virtual void drawMesh(const Mesh& mesh) = 0; | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 129 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 130 | // queries | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 131 | virtual size_t getMaxTextureSize() const = 0; | 
|  | 132 | virtual size_t getMaxViewportDims() const = 0; | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 133 | }; | 
|  | 134 |  | 
|  | 135 | class BindNativeBufferAsFramebuffer { | 
|  | 136 | public: | 
|  | 137 | BindNativeBufferAsFramebuffer(RenderEngine& engine, ANativeWindowBuffer* buffer) | 
|  | 138 | : mEngine(engine) { | 
|  | 139 | mEngine.bindNativeBufferAsFrameBuffer(buffer, this); | 
|  | 140 | } | 
|  | 141 | ~BindNativeBufferAsFramebuffer() { mEngine.unbindNativeBufferAsFrameBuffer(this); } | 
|  | 142 | status_t getStatus() const { return mStatus; } | 
|  | 143 |  | 
|  | 144 | protected: | 
|  | 145 | friend impl::RenderEngine; | 
|  | 146 |  | 
|  | 147 | RenderEngine& mEngine; | 
|  | 148 | EGLImageKHR mImage; | 
|  | 149 | uint32_t mTexName, mFbName; | 
|  | 150 | status_t mStatus; | 
|  | 151 | }; | 
|  | 152 |  | 
|  | 153 | namespace impl { | 
|  | 154 |  | 
|  | 155 | class Image; | 
|  | 156 | class Surface; | 
|  | 157 |  | 
|  | 158 | class RenderEngine : public RE::RenderEngine { | 
|  | 159 | enum GlesVersion { | 
|  | 160 | GLES_VERSION_1_0 = 0x10000, | 
|  | 161 | GLES_VERSION_1_1 = 0x10001, | 
|  | 162 | GLES_VERSION_2_0 = 0x20000, | 
|  | 163 | GLES_VERSION_3_0 = 0x30000, | 
|  | 164 | }; | 
|  | 165 | static GlesVersion parseGlesVersion(const char* str); | 
|  | 166 |  | 
|  | 167 | EGLDisplay mEGLDisplay; | 
|  | 168 | EGLConfig mEGLConfig; | 
|  | 169 | EGLContext mEGLContext; | 
|  | 170 | void setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt); | 
|  | 171 |  | 
|  | 172 | static bool overrideUseContextPriorityFromConfig(bool useContextPriority); | 
|  | 173 |  | 
|  | 174 | protected: | 
| Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 175 | RenderEngine(uint32_t featureFlags); | 
|  | 176 |  | 
|  | 177 | const uint32_t mFeatureFlags; | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 178 |  | 
|  | 179 | public: | 
|  | 180 | virtual ~RenderEngine() = 0; | 
|  | 181 |  | 
|  | 182 | static std::unique_ptr<RenderEngine> create(int hwcFormat, uint32_t featureFlags); | 
|  | 183 |  | 
|  | 184 | static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig); | 
|  | 185 |  | 
|  | 186 | // RenderEngine interface implementation | 
|  | 187 |  | 
|  | 188 | std::unique_ptr<RE::Surface> createSurface() override; | 
|  | 189 | std::unique_ptr<RE::Image> createImage() override; | 
|  | 190 |  | 
|  | 191 | void primeCache() const override; | 
|  | 192 |  | 
|  | 193 | // dump the extension strings. always call the base class. | 
|  | 194 | void dump(String8& result) override; | 
|  | 195 |  | 
|  | 196 | bool supportsImageCrop() const override; | 
|  | 197 |  | 
|  | 198 | bool isCurrent() const; | 
|  | 199 | bool setCurrentSurface(const RE::Surface& surface) override; | 
|  | 200 | void resetCurrentSurface() override; | 
|  | 201 |  | 
|  | 202 | // synchronization | 
|  | 203 |  | 
|  | 204 | // flush submits RenderEngine command stream for execution and returns a | 
|  | 205 | // native fence fd that is signaled when the execution has completed.  It | 
|  | 206 | // returns -1 on errors. | 
|  | 207 | base::unique_fd flush() override; | 
|  | 208 | // finish waits until RenderEngine command stream has been executed.  It | 
|  | 209 | // returns false on errors. | 
|  | 210 | bool finish() override; | 
|  | 211 | // waitFence inserts a wait on an external fence fd to RenderEngine | 
|  | 212 | // command stream.  It returns false on errors. | 
|  | 213 | bool waitFence(base::unique_fd fenceFd) override; | 
|  | 214 |  | 
|  | 215 | // helpers | 
|  | 216 | void clearWithColor(float red, float green, float blue, float alpha) override; | 
|  | 217 | void fillRegionWithColor(const Region& region, uint32_t height, float red, float green, | 
|  | 218 | float blue, float alpha) override; | 
|  | 219 |  | 
|  | 220 | // common to all GL versions | 
|  | 221 | void setScissor(uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) override; | 
|  | 222 | void disableScissor() override; | 
|  | 223 | void genTextures(size_t count, uint32_t* names) override; | 
|  | 224 | void deleteTextures(size_t count, uint32_t const* names) override; | 
|  | 225 | void bindExternalTextureImage(uint32_t texName, const RE::Image& image) override; | 
|  | 226 | void readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) override; | 
|  | 227 |  | 
|  | 228 | void checkErrors() const override; | 
|  | 229 |  | 
| Chia-I Wu | 8e50e69 | 2018-05-04 10:12:37 -0700 | [diff] [blame] | 230 | void setupColorTransform(const mat4& /* colorTransform */) override {} | 
| Peiyong Lin | 76dd77a | 2018-05-09 15:35:33 -0700 | [diff] [blame] | 231 | void setSaturationMatrix(const mat4& /* saturationMatrix */) override {} | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 232 |  | 
| Chia-I Wu | f846a35 | 2017-11-10 09:22:52 -0800 | [diff] [blame] | 233 | // internal to RenderEngine | 
| Chia-I Wu | 2b6386e | 2017-11-09 13:03:17 -0800 | [diff] [blame] | 234 | EGLDisplay getEGLDisplay() const; | 
| Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 235 | EGLConfig getEGLConfig() const; | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 236 |  | 
|  | 237 | // Common implementation | 
|  | 238 | bool setCurrentSurface(const RE::impl::Surface& surface); | 
|  | 239 | void bindExternalTextureImage(uint32_t texName, const RE::impl::Image& image); | 
|  | 240 |  | 
|  | 241 | void bindNativeBufferAsFrameBuffer(ANativeWindowBuffer* buffer, | 
|  | 242 | RE::BindNativeBufferAsFramebuffer* bindHelper) override; | 
|  | 243 | void unbindNativeBufferAsFrameBuffer(RE::BindNativeBufferAsFramebuffer* bindHelper) override; | 
|  | 244 |  | 
|  | 245 | // Overriden by each specialization | 
|  | 246 | virtual void bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName, uint32_t* fbName, | 
|  | 247 | uint32_t* status) = 0; | 
|  | 248 | virtual void unbindFramebuffer(uint32_t texName, uint32_t fbName) = 0; | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 249 | }; | 
|  | 250 |  | 
| Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 251 | } // namespace impl | 
|  | 252 | } // namespace RE | 
|  | 253 | } // namespace android | 
| Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 254 |  | 
|  | 255 | #endif /* SF_RENDERENGINE_H_ */ |