Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -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 | |
Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 17 | #ifndef SF_GLESRENDERENGINE_H_ |
| 18 | #define SF_GLESRENDERENGINE_H_ |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 19 | |
Alec Mouri | 554d06e | 2018-12-20 00:15:33 -0800 | [diff] [blame] | 20 | #include <android-base/thread_annotations.h> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
Alec Mouri | 554d06e | 2018-12-20 00:15:33 -0800 | [diff] [blame] | 23 | #include <condition_variable> |
| 24 | #include <mutex> |
| 25 | #include <queue> |
| 26 | #include <thread> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 27 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 28 | #include <EGL/egl.h> |
| 29 | #include <EGL/eglext.h> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 30 | #include <GLES2/gl2.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 31 | #include <renderengine/RenderEngine.h> |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 32 | #include <renderengine/private/Description.h> |
Alec Mouri | 539319f | 2018-12-19 17:56:23 -0800 | [diff] [blame] | 33 | #include <unordered_map> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 34 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 35 | #define EGL_NO_CONFIG ((EGLConfig)0) |
| 36 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 37 | namespace android { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 38 | |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 39 | namespace renderengine { |
| 40 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 41 | class Mesh; |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 42 | class Texture; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 43 | |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 44 | namespace gl { |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 45 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 46 | class GLImage; |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 47 | |
Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 48 | class GLESRenderEngine : public impl::RenderEngine { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 49 | public: |
Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 50 | static std::unique_ptr<GLESRenderEngine> create(int hwcFormat, uint32_t featureFlags); |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 51 | static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig); |
| 52 | |
Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 53 | GLESRenderEngine(uint32_t featureFlags, // See RenderEngine::FeatureFlag |
Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 54 | EGLDisplay display, EGLConfig config, EGLContext ctxt, EGLSurface dummy, |
| 55 | EGLContext protectedContext, EGLSurface protectedDummy); |
Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 56 | ~GLESRenderEngine() override; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 57 | |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 58 | std::unique_ptr<Framebuffer> createFramebuffer() override; |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 59 | std::unique_ptr<Image> createImage() override; |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 60 | |
| 61 | void primeCache() const override; |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 62 | bool isCurrent() const override; |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 63 | 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 Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 67 | void fillRegionWithColor(const Region& region, float red, float green, float blue, |
| 68 | float alpha) override; |
Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 69 | void setScissor(const Rect& region) override; |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 70 | 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 Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 73 | void bindExternalTextureImage(uint32_t texName, const Image& image) override; |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 74 | status_t bindExternalTextureBuffer(uint32_t texName, sp<GraphicBuffer> buffer, sp<Fence> fence, |
| 75 | bool readCache); |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 76 | status_t bindFrameBuffer(Framebuffer* framebuffer) override; |
| 77 | void unbindFrameBuffer(Framebuffer* framebuffer) override; |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 78 | void checkErrors() const override; |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 79 | |
Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 80 | bool isProtected() const override { return mInProtectedContext; } |
| 81 | bool supportsProtectedContent() const override; |
| 82 | bool useProtectedContext(bool useProtectedContext) override; |
Alec Mouri | 1089aed | 2018-10-25 21:33:57 -0700 | [diff] [blame] | 83 | status_t drawLayers(const DisplaySettings& display, const std::vector<LayerSettings>& layers, |
Alec Mouri | 6338c9d | 2019-02-07 16:57:51 -0800 | [diff] [blame^] | 84 | ANativeWindowBuffer* buffer, base::unique_fd&& bufferFence, |
| 85 | base::unique_fd* drawFence) override; |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 86 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 87 | // internal to RenderEngine |
| 88 | EGLDisplay getEGLDisplay() const { return mEGLDisplay; } |
| 89 | EGLConfig getEGLConfig() const { return mEGLConfig; } |
| 90 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 91 | protected: |
Alec Mouri | 820c740 | 2019-01-23 13:02:39 -0800 | [diff] [blame] | 92 | Framebuffer* getFramebufferForDrawing() override; |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 93 | void dump(std::string& result) override; |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 94 | void setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop, |
| 95 | ui::Transform::orientation_flags rotation) override; |
| 96 | void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture, |
Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 97 | const half4& color, float cornerRadius) override; |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 98 | void setupLayerTexturing(const Texture& texture) override; |
| 99 | void setupLayerBlackedOut() override; |
| 100 | void setupFillWithColor(float r, float g, float b, float a) override; |
Peiyong Lin | d378863 | 2018-09-18 16:01:31 -0700 | [diff] [blame] | 101 | void setColorTransform(const mat4& colorTransform) override; |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 102 | void disableTexturing() override; |
| 103 | void disableBlending() override; |
Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 104 | void setupCornerRadiusCropSize(float width, float height) override; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 105 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 106 | // HDR and color management related functions and state |
| 107 | void setSourceY410BT2020(bool enable) override; |
| 108 | void setSourceDataSpace(ui::Dataspace source) override; |
| 109 | void setOutputDataSpace(ui::Dataspace dataspace) override; |
| 110 | void setDisplayMaxLuminance(const float maxLuminance) override; |
| 111 | |
| 112 | // drawing |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 113 | void drawMesh(const Mesh& mesh) override; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 114 | |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 115 | size_t getMaxTextureSize() const override; |
| 116 | size_t getMaxViewportDims() const override; |
| 117 | |
| 118 | private: |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 119 | enum GlesVersion { |
| 120 | GLES_VERSION_1_0 = 0x10000, |
| 121 | GLES_VERSION_1_1 = 0x10001, |
| 122 | GLES_VERSION_2_0 = 0x20000, |
| 123 | GLES_VERSION_3_0 = 0x30000, |
| 124 | }; |
| 125 | |
| 126 | static GlesVersion parseGlesVersion(const char* str); |
Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 127 | static EGLContext createEglContext(EGLDisplay display, EGLConfig config, |
Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 128 | EGLContext shareContext, bool useContextPriority, |
| 129 | Protection protection); |
Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 130 | static EGLSurface createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config, |
Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 131 | int hwcFormat, Protection protection); |
Alec Mouri | 554d06e | 2018-12-20 00:15:33 -0800 | [diff] [blame] | 132 | bool waitSync(EGLSyncKHR sync, EGLint flags); |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 133 | |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 134 | // A data space is considered HDR data space if it has BT2020 color space |
| 135 | // with PQ or HLG transfer function. |
| 136 | bool isHdrDataSpace(const ui::Dataspace dataSpace) const; |
| 137 | bool needsXYZTransformMatrix() const; |
Alec Mouri | 1089aed | 2018-10-25 21:33:57 -0700 | [diff] [blame] | 138 | // Defines the viewport, and sets the projection matrix to the projection |
| 139 | // defined by the clip. |
| 140 | void setViewportAndProjection(Rect viewport, Rect clip); |
Alec Mouri | 539319f | 2018-12-19 17:56:23 -0800 | [diff] [blame] | 141 | status_t bindExternalTextureBuffer(uint32_t texName, sp<GraphicBuffer> buffer, sp<Fence> fence, |
| 142 | bool readCache, bool persistCache); |
| 143 | // Evicts stale images from the buffer cache. |
| 144 | void evictImages(const std::vector<LayerSettings>& layers); |
Alec Mouri | 7c94edb | 2018-12-03 21:23:26 -0800 | [diff] [blame] | 145 | // Computes the cropping window for the layer and sets up cropping |
| 146 | // coordinates for the mesh. |
| 147 | FloatRect setupLayerCropping(const LayerSettings& layer, Mesh& mesh); |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 148 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 149 | EGLDisplay mEGLDisplay; |
| 150 | EGLConfig mEGLConfig; |
| 151 | EGLContext mEGLContext; |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 152 | EGLSurface mDummySurface; |
Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 153 | EGLContext mProtectedEGLContext; |
| 154 | EGLSurface mProtectedDummySurface; |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 155 | GLuint mProtectedTexName; |
| 156 | GLint mMaxViewportDims[2]; |
| 157 | GLint mMaxTextureSize; |
| 158 | GLuint mVpWidth; |
| 159 | GLuint mVpHeight; |
| 160 | Description mState; |
| 161 | |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 162 | mat4 mSrgbToXyz; |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 163 | mat4 mDisplayP3ToXyz; |
Valerie Hau | eb8e076 | 2018-11-06 10:10:42 -0800 | [diff] [blame] | 164 | mat4 mBt2020ToXyz; |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 165 | mat4 mXyzToSrgb; |
| 166 | mat4 mXyzToDisplayP3; |
| 167 | mat4 mXyzToBt2020; |
Valerie Hau | eb8e076 | 2018-11-06 10:10:42 -0800 | [diff] [blame] | 168 | mat4 mSrgbToDisplayP3; |
| 169 | mat4 mSrgbToBt2020; |
| 170 | mat4 mDisplayP3ToSrgb; |
| 171 | mat4 mDisplayP3ToBt2020; |
| 172 | mat4 mBt2020ToSrgb; |
| 173 | mat4 mBt2020ToDisplayP3; |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 174 | |
Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 175 | bool mInProtectedContext = false; |
Alec Mouri | 554d06e | 2018-12-20 00:15:33 -0800 | [diff] [blame] | 176 | // If set to true, then enables tracing flush() and finish() to systrace. |
| 177 | bool mTraceGpuCompletion = false; |
Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 178 | int32_t mFboHeight = 0; |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 179 | |
| 180 | // Current dataspace of layer being rendered |
| 181 | ui::Dataspace mDataSpace = ui::Dataspace::UNKNOWN; |
| 182 | |
| 183 | // Current output dataspace of the render engine |
| 184 | ui::Dataspace mOutputDataSpace = ui::Dataspace::UNKNOWN; |
| 185 | |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 186 | // Whether device supports color management, currently color management |
| 187 | // supports sRGB, DisplayP3 color spaces. |
| 188 | const bool mUseColorManagement = false; |
Alec Mouri | 554d06e | 2018-12-20 00:15:33 -0800 | [diff] [blame] | 189 | |
Alec Mouri | 539319f | 2018-12-19 17:56:23 -0800 | [diff] [blame] | 190 | // Cache of GL images that we'll store per GraphicBuffer ID |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 191 | // TODO: Layer should call back on destruction instead to clean this up, |
| 192 | // as it has better system utilization at the potential expense of a |
| 193 | // more complicated interface. |
Alec Mouri | 539319f | 2018-12-19 17:56:23 -0800 | [diff] [blame] | 194 | std::unordered_map<uint64_t, std::unique_ptr<Image>> mImageCache; |
| 195 | |
Alec Mouri | 820c740 | 2019-01-23 13:02:39 -0800 | [diff] [blame] | 196 | std::unique_ptr<Framebuffer> mDrawingBuffer; |
| 197 | |
Alec Mouri | 554d06e | 2018-12-20 00:15:33 -0800 | [diff] [blame] | 198 | class FlushTracer { |
| 199 | public: |
| 200 | FlushTracer(GLESRenderEngine* engine); |
| 201 | ~FlushTracer(); |
| 202 | void queueSync(EGLSyncKHR sync) EXCLUDES(mMutex); |
| 203 | |
| 204 | struct QueueEntry { |
| 205 | EGLSyncKHR mSync = nullptr; |
| 206 | uint64_t mFrameNum = 0; |
| 207 | }; |
| 208 | |
| 209 | private: |
| 210 | void loop(); |
| 211 | GLESRenderEngine* const mEngine; |
| 212 | std::thread mThread; |
| 213 | std::condition_variable_any mCondition; |
| 214 | std::mutex mMutex; |
| 215 | std::queue<QueueEntry> mQueue GUARDED_BY(mMutex); |
| 216 | uint64_t mFramesQueued GUARDED_BY(mMutex) = 0; |
| 217 | bool mRunning = true; |
| 218 | }; |
| 219 | friend class FlushTracer; |
| 220 | std::unique_ptr<FlushTracer> mFlushTracer; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 221 | }; |
| 222 | |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 223 | } // namespace gl |
| 224 | } // namespace renderengine |
| 225 | } // namespace android |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 226 | |
Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 227 | #endif /* SF_GLESRENDERENGINE_H_ */ |