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