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