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 <stdint.h> 21 #include <sys/types.h> 22 #include <memory> 23 24 #include <android-base/unique_fd.h> 25 #include <math/mat4.h> 26 #include <renderengine/DisplaySettings.h> 27 #include <renderengine/Framebuffer.h> 28 #include <renderengine/Image.h> 29 #include <renderengine/LayerSettings.h> 30 #include <ui/GraphicTypes.h> 31 #include <ui/Transform.h> 32 33 /** 34 * Allows to set RenderEngine backend to GLES (default) or Vulkan (NOT yet supported). 35 */ 36 #define PROPERTY_DEBUG_RENDERENGINE_BACKEND "debug.renderengine.backend" 37 38 struct ANativeWindowBuffer; 39 40 namespace android { 41 42 class Rect; 43 class Region; 44 45 namespace renderengine { 46 47 class BindNativeBufferAsFramebuffer; 48 class Image; 49 class Mesh; 50 class Texture; 51 struct RenderEngineCreationArgs; 52 53 namespace impl { 54 class RenderEngine; 55 } 56 57 enum class Protection { 58 UNPROTECTED = 1, 59 PROTECTED = 2, 60 }; 61 62 class RenderEngine { 63 public: 64 enum class ContextPriority { 65 LOW = 1, 66 MEDIUM = 2, 67 HIGH = 3, 68 }; 69 70 static std::unique_ptr<impl::RenderEngine> create(const RenderEngineCreationArgs& args); 71 72 virtual ~RenderEngine() = 0; 73 74 // ----- BEGIN DEPRECATED INTERFACE ----- 75 // This interface, while still in use until a suitable replacement is built, 76 // should be considered deprecated, minus some methods which still may be 77 // used to support legacy behavior. 78 virtual void primeCache() const = 0; 79 80 // dump the extension strings. always call the base class. 81 virtual void dump(std::string& result) = 0; 82 83 virtual bool useNativeFenceSync() const = 0; 84 virtual bool useWaitSync() const = 0; 85 virtual void genTextures(size_t count, uint32_t* names) = 0; 86 virtual void deleteTextures(size_t count, uint32_t const* names) = 0; 87 virtual void bindExternalTextureImage(uint32_t texName, const Image& image) = 0; 88 // Legacy public method used by devices that don't support native fence 89 // synchronization in their GPU driver, as this method provides implicit 90 // synchronization for latching buffers. 91 virtual status_t bindExternalTextureBuffer(uint32_t texName, const sp<GraphicBuffer>& buffer, 92 const sp<Fence>& fence) = 0; 93 // Caches Image resources for this buffer, but does not bind the buffer to 94 // a particular texture. 95 // Note that work is deferred to an additional thread, i.e. this call 96 // is made asynchronously, but the caller can expect that cache/unbind calls 97 // are performed in a manner that's conflict serializable, i.e. unbinding 98 // a buffer should never occur before binding the buffer if the caller 99 // called {bind, cache}ExternalTextureBuffer before calling unbind. 100 virtual void cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) = 0; 101 // Removes internal resources referenced by the bufferId. This method should be 102 // invoked when the caller will no longer hold a reference to a GraphicBuffer 103 // and needs to clean up its resources. 104 // Note that work is deferred to an additional thread, i.e. this call 105 // is made asynchronously, but the caller can expect that cache/unbind calls 106 // are performed in a manner that's conflict serializable, i.e. unbinding 107 // a buffer should never occur before binding the buffer if the caller 108 // called {bind, cache}ExternalTextureBuffer before calling unbind. 109 virtual void unbindExternalTextureBuffer(uint64_t bufferId) = 0; 110 // When binding a native buffer, it must be done before setViewportAndProjection 111 // Returns NO_ERROR when binds successfully, NO_MEMORY when there's no memory for allocation. 112 virtual status_t bindFrameBuffer(Framebuffer* framebuffer) = 0; 113 virtual void unbindFrameBuffer(Framebuffer* framebuffer) = 0; 114 115 enum class CleanupMode { 116 CLEAN_OUTPUT_RESOURCES, 117 CLEAN_ALL, 118 }; 119 // Clean-up method that should be called on the main thread after the 120 // drawFence returned by drawLayers fires. This method will free up 121 // resources used by the most recently drawn frame. If the frame is still 122 // being drawn, then this call is silently ignored. 123 // 124 // If mode is CLEAN_OUTPUT_RESOURCES, then only resources related to the 125 // output framebuffer are cleaned up, including the sibling texture. 126 // 127 // If mode is CLEAN_ALL, then we also cleanup resources related to any input 128 // buffers. 129 // 130 // Returns true if resources were cleaned up, and false if we didn't need to 131 // do any work. 132 virtual bool cleanupPostRender(CleanupMode mode = CleanupMode::CLEAN_OUTPUT_RESOURCES) = 0; 133 134 // queries 135 virtual size_t getMaxTextureSize() const = 0; 136 virtual size_t getMaxViewportDims() const = 0; 137 138 // ----- END DEPRECATED INTERFACE ----- 139 140 // ----- BEGIN NEW INTERFACE ----- 141 142 virtual bool isProtected() const = 0; 143 virtual bool supportsProtectedContent() const = 0; 144 virtual bool useProtectedContext(bool useProtectedContext) = 0; 145 146 // Renders layers for a particular display via GPU composition. This method 147 // should be called for every display that needs to be rendered via the GPU. 148 // @param display The display-wide settings that should be applied prior to 149 // drawing any layers. 150 // 151 // Assumptions when calling this method: 152 // 1. There is exactly one caller - i.e. multi-threading is not supported. 153 // 2. Additional threads may be calling the {bind,cache}ExternalTexture 154 // methods above. But the main thread is responsible for holding resources 155 // such that Image destruction does not occur while this method is called. 156 // 157 // TODO(b/136806342): This should behavior should ideally be fixed since 158 // the above two assumptions are brittle, as conditional thread safetyness 159 // may be insufficient when maximizing rendering performance in the future. 160 // 161 // @param layers The layers to draw onto the display, in Z-order. 162 // @param buffer The buffer which will be drawn to. This buffer will be 163 // ready once drawFence fires. 164 // @param useFramebufferCache True if the framebuffer cache should be used. 165 // If an implementation does not cache output framebuffers, then this 166 // parameter does nothing. 167 // @param bufferFence Fence signalling that the buffer is ready to be drawn 168 // to. 169 // @param drawFence A pointer to a fence, which will fire when the buffer 170 // has been drawn to and is ready to be examined. The fence will be 171 // initialized by this method. The caller will be responsible for owning the 172 // fence. 173 // @return An error code indicating whether drawing was successful. For 174 // now, this always returns NO_ERROR. 175 virtual status_t drawLayers(const DisplaySettings& display, 176 const std::vector<const LayerSettings*>& layers, 177 ANativeWindowBuffer* buffer, const bool useFramebufferCache, 178 base::unique_fd&& bufferFence, base::unique_fd* drawFence) = 0; 179 180 protected: 181 // Gets a framebuffer to render to. This framebuffer may or may not be 182 // cached depending on the implementation. 183 // 184 // Note that this method does not transfer ownership, so the caller most not 185 // live longer than RenderEngine. 186 virtual Framebuffer* getFramebufferForDrawing() = 0; 187 friend class BindNativeBufferAsFramebuffer; 188 }; 189 190 struct RenderEngineCreationArgs { 191 int pixelFormat; 192 uint32_t imageCacheSize; 193 bool useColorManagement; 194 bool enableProtectedContext; 195 bool precacheToneMapperShaderOnly; 196 bool supportsBackgroundBlur; 197 RenderEngine::ContextPriority contextPriority; 198 199 struct Builder; 200 201 private: 202 // must be created by Builder via constructor with full argument list RenderEngineCreationArgsRenderEngineCreationArgs203 RenderEngineCreationArgs( 204 int _pixelFormat, 205 uint32_t _imageCacheSize, 206 bool _useColorManagement, 207 bool _enableProtectedContext, 208 bool _precacheToneMapperShaderOnly, 209 bool _supportsBackgroundBlur, 210 RenderEngine::ContextPriority _contextPriority) 211 : pixelFormat(_pixelFormat) 212 , imageCacheSize(_imageCacheSize) 213 , useColorManagement(_useColorManagement) 214 , enableProtectedContext(_enableProtectedContext) 215 , precacheToneMapperShaderOnly(_precacheToneMapperShaderOnly) 216 , supportsBackgroundBlur(_supportsBackgroundBlur) 217 , contextPriority(_contextPriority) {} 218 RenderEngineCreationArgs() = delete; 219 }; 220 221 struct RenderEngineCreationArgs::Builder { BuilderBuilder222 Builder() {} 223 setPixelFormatBuilder224 Builder& setPixelFormat(int pixelFormat) { 225 this->pixelFormat = pixelFormat; 226 return *this; 227 } setImageCacheSizeBuilder228 Builder& setImageCacheSize(uint32_t imageCacheSize) { 229 this->imageCacheSize = imageCacheSize; 230 return *this; 231 } setUseColorManagermentBuilder232 Builder& setUseColorManagerment(bool useColorManagement) { 233 this->useColorManagement = useColorManagement; 234 return *this; 235 } setEnableProtectedContextBuilder236 Builder& setEnableProtectedContext(bool enableProtectedContext) { 237 this->enableProtectedContext = enableProtectedContext; 238 return *this; 239 } setPrecacheToneMapperShaderOnlyBuilder240 Builder& setPrecacheToneMapperShaderOnly(bool precacheToneMapperShaderOnly) { 241 this->precacheToneMapperShaderOnly = precacheToneMapperShaderOnly; 242 return *this; 243 } setSupportsBackgroundBlurBuilder244 Builder& setSupportsBackgroundBlur(bool supportsBackgroundBlur) { 245 this->supportsBackgroundBlur = supportsBackgroundBlur; 246 return *this; 247 } setContextPriorityBuilder248 Builder& setContextPriority(RenderEngine::ContextPriority contextPriority) { 249 this->contextPriority = contextPriority; 250 return *this; 251 } buildBuilder252 RenderEngineCreationArgs build() const { 253 return RenderEngineCreationArgs(pixelFormat, imageCacheSize, useColorManagement, 254 enableProtectedContext, precacheToneMapperShaderOnly, 255 supportsBackgroundBlur, contextPriority); 256 } 257 258 private: 259 // 1 means RGBA_8888 260 int pixelFormat = 1; 261 uint32_t imageCacheSize = 0; 262 bool useColorManagement = true; 263 bool enableProtectedContext = false; 264 bool precacheToneMapperShaderOnly = false; 265 bool supportsBackgroundBlur = false; 266 RenderEngine::ContextPriority contextPriority = RenderEngine::ContextPriority::MEDIUM; 267 }; 268 269 class BindNativeBufferAsFramebuffer { 270 public: BindNativeBufferAsFramebuffer(RenderEngine & engine,ANativeWindowBuffer * buffer,const bool useFramebufferCache)271 BindNativeBufferAsFramebuffer(RenderEngine& engine, ANativeWindowBuffer* buffer, 272 const bool useFramebufferCache) 273 : mEngine(engine), mFramebuffer(mEngine.getFramebufferForDrawing()), mStatus(NO_ERROR) { 274 mStatus = mFramebuffer->setNativeWindowBuffer(buffer, mEngine.isProtected(), 275 useFramebufferCache) 276 ? mEngine.bindFrameBuffer(mFramebuffer) 277 : NO_MEMORY; 278 } ~BindNativeBufferAsFramebuffer()279 ~BindNativeBufferAsFramebuffer() { 280 mFramebuffer->setNativeWindowBuffer(nullptr, false, /*arbitrary*/ true); 281 mEngine.unbindFrameBuffer(mFramebuffer); 282 } getStatus()283 status_t getStatus() const { return mStatus; } 284 285 private: 286 RenderEngine& mEngine; 287 Framebuffer* mFramebuffer; 288 status_t mStatus; 289 }; 290 291 namespace impl { 292 293 // impl::RenderEngine contains common implementation that is graphics back-end agnostic. 294 class RenderEngine : public renderengine::RenderEngine { 295 public: 296 virtual ~RenderEngine() = 0; 297 298 bool useNativeFenceSync() const override; 299 bool useWaitSync() const override; 300 301 protected: 302 RenderEngine(const RenderEngineCreationArgs& args); 303 const RenderEngineCreationArgs mArgs; 304 }; 305 306 } // namespace impl 307 } // namespace renderengine 308 } // namespace android 309 310 #endif /* SF_RENDERENGINE_H_ */ 311