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 17 #ifndef SF_RENDERENGINE_H_ 18 #define SF_RENDERENGINE_H_ 19 20 #include <android-base/unique_fd.h> 21 #include <ftl/future.h> 22 #include <math/mat4.h> 23 #include <renderengine/DisplaySettings.h> 24 #include <renderengine/ExternalTexture.h> 25 #include <renderengine/Framebuffer.h> 26 #include <renderengine/Image.h> 27 #include <renderengine/LayerSettings.h> 28 #include <stdint.h> 29 #include <sys/types.h> 30 #include <ui/FenceResult.h> 31 #include <ui/GraphicTypes.h> 32 #include <ui/Transform.h> 33 34 #include <future> 35 #include <memory> 36 37 /** 38 * Allows to set RenderEngine backend to GLES (default) or SkiaGL (NOT yet supported). 39 */ 40 #define PROPERTY_DEBUG_RENDERENGINE_BACKEND "debug.renderengine.backend" 41 42 /** 43 * Turns on recording of skia commands in SkiaGL version of the RE. This property 44 * defines number of milliseconds for the recording to take place. A non zero value 45 * turns on the recording. 46 */ 47 #define PROPERTY_DEBUG_RENDERENGINE_CAPTURE_SKIA_MS "debug.renderengine.capture_skia_ms" 48 49 /** 50 * Set to the most recently saved file once the capture is finished. 51 */ 52 #define PROPERTY_DEBUG_RENDERENGINE_CAPTURE_FILENAME "debug.renderengine.capture_filename" 53 54 /** 55 * Allows recording of Skia drawing commands with systrace. 56 */ 57 #define PROPERTY_SKIA_ATRACE_ENABLED "debug.renderengine.skia_atrace_enabled" 58 59 struct ANativeWindowBuffer; 60 61 namespace android { 62 63 class Rect; 64 class Region; 65 66 namespace renderengine { 67 68 class ExternalTexture; 69 class Image; 70 class Mesh; 71 class Texture; 72 struct RenderEngineCreationArgs; 73 74 namespace threaded { 75 class RenderEngineThreaded; 76 } 77 78 namespace impl { 79 class RenderEngine; 80 class ExternalTexture; 81 } 82 83 enum class Protection { 84 UNPROTECTED = 1, 85 PROTECTED = 2, 86 }; 87 88 class RenderEngine { 89 public: 90 enum class ContextPriority { 91 LOW = 1, 92 MEDIUM = 2, 93 HIGH = 3, 94 REALTIME = 4, 95 }; 96 97 enum class RenderEngineType { 98 GLES = 1, 99 THREADED = 2, 100 SKIA_GL = 3, 101 SKIA_GL_THREADED = 4, 102 SKIA_VK = 5, 103 SKIA_VK_THREADED = 6, 104 }; 105 106 static std::unique_ptr<RenderEngine> create(const RenderEngineCreationArgs& args); 107 108 virtual ~RenderEngine() = 0; 109 110 // ----- BEGIN DEPRECATED INTERFACE ----- 111 // This interface, while still in use until a suitable replacement is built, 112 // should be considered deprecated, minus some methods which still may be 113 // used to support legacy behavior. 114 virtual std::future<void> primeCache() = 0; 115 116 // dump the extension strings. always call the base class. 117 virtual void dump(std::string& result) = 0; 118 119 virtual void genTextures(size_t count, uint32_t* names) = 0; 120 virtual void deleteTextures(size_t count, uint32_t const* names) = 0; 121 122 // queries that are required to be thread safe 123 virtual size_t getMaxTextureSize() const = 0; 124 virtual size_t getMaxViewportDims() const = 0; 125 126 // ----- END DEPRECATED INTERFACE ----- 127 128 // ----- BEGIN NEW INTERFACE ----- 129 130 // queries that are required to be thread safe 131 virtual bool supportsProtectedContent() const = 0; 132 133 // Notify RenderEngine of changes to the dimensions of the active display 134 // so that it can configure its internal caches accordingly. 135 virtual void onActiveDisplaySizeChanged(ui::Size size) = 0; 136 137 // Renders layers for a particular display via GPU composition. This method 138 // should be called for every display that needs to be rendered via the GPU. 139 // @param display The display-wide settings that should be applied prior to 140 // drawing any layers. 141 // 142 // Assumptions when calling this method: 143 // 1. There is exactly one caller - i.e. multi-threading is not supported. 144 // 2. Additional threads may be calling the {bind,cache}ExternalTexture 145 // methods above. But the main thread is responsible for holding resources 146 // such that Image destruction does not occur while this method is called. 147 // 148 // TODO(b/136806342): This should behavior should ideally be fixed since 149 // the above two assumptions are brittle, as conditional thread safetyness 150 // may be insufficient when maximizing rendering performance in the future. 151 // 152 // @param layers The layers to draw onto the display, in Z-order. 153 // @param buffer The buffer which will be drawn to. This buffer will be 154 // ready once drawFence fires. 155 // @param useFramebufferCache True if the framebuffer cache should be used. 156 // If an implementation does not cache output framebuffers, then this 157 // parameter does nothing. 158 // @param bufferFence Fence signalling that the buffer is ready to be drawn 159 // to. 160 // @return A future object of FenceResult indicating whether drawing was 161 // successful in async mode. 162 virtual ftl::Future<FenceResult> drawLayers(const DisplaySettings& display, 163 const std::vector<LayerSettings>& layers, 164 const std::shared_ptr<ExternalTexture>& buffer, 165 const bool useFramebufferCache, 166 base::unique_fd&& bufferFence); 167 168 // Clean-up method that should be called on the main thread after the 169 // drawFence returned by drawLayers fires. This method will free up 170 // resources used by the most recently drawn frame. If the frame is still 171 // being drawn, then the implementation is free to silently ignore this call. 172 virtual void cleanupPostRender() = 0; 173 174 virtual void cleanFramebufferCache() = 0; 175 176 // Returns the priority this context was actually created with. Note: this 177 // may not be the same as specified at context creation time, due to 178 // implementation limits on the number of contexts that can be created at a 179 // specific priority level in the system. 180 // 181 // This should return a valid EGL context priority enum as described by 182 // https://registry.khronos.org/EGL/extensions/IMG/EGL_IMG_context_priority.txt 183 // or 184 // https://registry.khronos.org/EGL/extensions/NV/EGL_NV_context_priority_realtime.txt 185 virtual int getContextPriority() = 0; 186 187 // Returns true if blur was requested in the RenderEngineCreationArgs and the implementation 188 // also supports background blur. If false, no blur will be applied when drawing layers. This 189 // query is required to be thread safe. 190 virtual bool supportsBackgroundBlur() = 0; 191 192 // Returns the current type of RenderEngine instance that was created. 193 // TODO(b/180767535): This is only implemented to allow for backend-specific behavior, which 194 // we should not allow in general, so remove this. getRenderEngineType()195 RenderEngineType getRenderEngineType() const { return mRenderEngineType; } 196 197 static void validateInputBufferUsage(const sp<GraphicBuffer>&); 198 static void validateOutputBufferUsage(const sp<GraphicBuffer>&); 199 200 // Allows flinger to get the render engine thread id for power management with ADPF 201 // Returns the tid of the renderengine thread if it's threaded, and std::nullopt otherwise getRenderEngineTid()202 virtual std::optional<pid_t> getRenderEngineTid() const { return std::nullopt; } 203 setEnableTracing(bool)204 virtual void setEnableTracing(bool /*tracingEnabled*/) {} 205 206 protected: RenderEngine()207 RenderEngine() : RenderEngine(RenderEngineType::GLES) {} 208 RenderEngine(RenderEngineType type)209 RenderEngine(RenderEngineType type) : mRenderEngineType(type) {} 210 211 // Maps GPU resources for this buffer. 212 // Note that work may be deferred to an additional thread, i.e. this call 213 // is made asynchronously, but the caller can expect that map/unmap calls 214 // are performed in a manner that's conflict serializable, i.e. unmapping 215 // a buffer should never occur before binding the buffer if the caller 216 // called mapExternalTextureBuffer before calling unmap. 217 // Note also that if the buffer contains protected content, then mapping those GPU resources may 218 // be deferred until the buffer is really used for drawing. This is because typical SoCs that 219 // support protected memory only support a limited amount, so optimisitically mapping protected 220 // memory may be too burdensome. If a buffer contains protected content and the RenderEngine 221 // implementation supports protected context, then GPU resources may be mapped into both the 222 // protected and unprotected contexts. 223 // If the buffer may ever be written to by RenderEngine, then isRenderable must be true. 224 virtual void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) = 0; 225 // Unmaps GPU resources used by this buffer. This method should be 226 // invoked when the caller will no longer hold a reference to a GraphicBuffer 227 // and needs to clean up its resources. 228 // Note that if there are multiple callers holding onto the same buffer, then the buffer's 229 // resources may be internally ref-counted to guard against use-after-free errors. Note that 230 // work may be deferred to an additional thread, i.e. this call is expected to be made 231 // asynchronously, but the caller can expect that map/unmap calls are performed in a manner 232 // that's conflict serializable, i.e. unmap a buffer should never occur before binding the 233 // buffer if the caller called mapExternalTextureBuffer before calling unmap. 234 virtual void unmapExternalTextureBuffer(sp<GraphicBuffer>&& buffer) = 0; 235 236 // A thread safe query to determine if any post rendering cleanup is necessary. Returning true 237 // is a signal that calling the postRenderCleanup method would be a no-op and that callers can 238 // avoid any thread synchronization that may be required by directly calling postRenderCleanup. 239 virtual bool canSkipPostRenderCleanup() const = 0; 240 241 friend class impl::ExternalTexture; 242 friend class threaded::RenderEngineThreaded; 243 friend class RenderEngineTest_cleanupPostRender_cleansUpOnce_Test; 244 const RenderEngineType mRenderEngineType; 245 246 // Update protectedContext mode depending on whether or not any layer has a protected buffer. 247 void updateProtectedContext(const std::vector<LayerSettings>&, 248 const std::shared_ptr<ExternalTexture>&); 249 250 // Attempt to switch RenderEngine into and out of protectedContext mode 251 virtual void useProtectedContext(bool useProtectedContext) = 0; 252 253 virtual void drawLayersInternal( 254 const std::shared_ptr<std::promise<FenceResult>>&& resultPromise, 255 const DisplaySettings& display, const std::vector<LayerSettings>& layers, 256 const std::shared_ptr<ExternalTexture>& buffer, const bool useFramebufferCache, 257 base::unique_fd&& bufferFence) = 0; 258 }; 259 260 struct RenderEngineCreationArgs { 261 int pixelFormat; 262 uint32_t imageCacheSize; 263 bool useColorManagement; 264 bool enableProtectedContext; 265 bool precacheToneMapperShaderOnly; 266 bool supportsBackgroundBlur; 267 RenderEngine::ContextPriority contextPriority; 268 RenderEngine::RenderEngineType renderEngineType; 269 270 struct Builder; 271 272 private: 273 // must be created by Builder via constructor with full argument list RenderEngineCreationArgsRenderEngineCreationArgs274 RenderEngineCreationArgs(int _pixelFormat, uint32_t _imageCacheSize, bool _useColorManagement, 275 bool _enableProtectedContext, bool _precacheToneMapperShaderOnly, 276 bool _supportsBackgroundBlur, 277 RenderEngine::ContextPriority _contextPriority, 278 RenderEngine::RenderEngineType _renderEngineType) 279 : pixelFormat(_pixelFormat), 280 imageCacheSize(_imageCacheSize), 281 useColorManagement(_useColorManagement), 282 enableProtectedContext(_enableProtectedContext), 283 precacheToneMapperShaderOnly(_precacheToneMapperShaderOnly), 284 supportsBackgroundBlur(_supportsBackgroundBlur), 285 contextPriority(_contextPriority), 286 renderEngineType(_renderEngineType) {} 287 RenderEngineCreationArgs() = delete; 288 }; 289 290 struct RenderEngineCreationArgs::Builder { BuilderBuilder291 Builder() {} 292 setPixelFormatBuilder293 Builder& setPixelFormat(int pixelFormat) { 294 this->pixelFormat = pixelFormat; 295 return *this; 296 } setImageCacheSizeBuilder297 Builder& setImageCacheSize(uint32_t imageCacheSize) { 298 this->imageCacheSize = imageCacheSize; 299 return *this; 300 } setUseColorManagermentBuilder301 Builder& setUseColorManagerment(bool useColorManagement) { 302 this->useColorManagement = useColorManagement; 303 return *this; 304 } setEnableProtectedContextBuilder305 Builder& setEnableProtectedContext(bool enableProtectedContext) { 306 this->enableProtectedContext = enableProtectedContext; 307 return *this; 308 } setPrecacheToneMapperShaderOnlyBuilder309 Builder& setPrecacheToneMapperShaderOnly(bool precacheToneMapperShaderOnly) { 310 this->precacheToneMapperShaderOnly = precacheToneMapperShaderOnly; 311 return *this; 312 } setSupportsBackgroundBlurBuilder313 Builder& setSupportsBackgroundBlur(bool supportsBackgroundBlur) { 314 this->supportsBackgroundBlur = supportsBackgroundBlur; 315 return *this; 316 } setContextPriorityBuilder317 Builder& setContextPriority(RenderEngine::ContextPriority contextPriority) { 318 this->contextPriority = contextPriority; 319 return *this; 320 } setRenderEngineTypeBuilder321 Builder& setRenderEngineType(RenderEngine::RenderEngineType renderEngineType) { 322 this->renderEngineType = renderEngineType; 323 return *this; 324 } buildBuilder325 RenderEngineCreationArgs build() const { 326 return RenderEngineCreationArgs(pixelFormat, imageCacheSize, useColorManagement, 327 enableProtectedContext, precacheToneMapperShaderOnly, 328 supportsBackgroundBlur, contextPriority, renderEngineType); 329 } 330 331 private: 332 // 1 means RGBA_8888 333 int pixelFormat = 1; 334 uint32_t imageCacheSize = 0; 335 bool useColorManagement = true; 336 bool enableProtectedContext = false; 337 bool precacheToneMapperShaderOnly = false; 338 bool supportsBackgroundBlur = false; 339 RenderEngine::ContextPriority contextPriority = RenderEngine::ContextPriority::MEDIUM; 340 RenderEngine::RenderEngineType renderEngineType = 341 RenderEngine::RenderEngineType::SKIA_GL_THREADED; 342 }; 343 344 } // namespace renderengine 345 } // namespace android 346 347 #endif /* SF_RENDERENGINE_H_ */ 348