• 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 <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 struct RenderEngineResult;
72 
73 namespace threaded {
74 class RenderEngineThreaded;
75 }
76 
77 namespace impl {
78 class RenderEngine;
79 class ExternalTexture;
80 }
81 
82 enum class Protection {
83     UNPROTECTED = 1,
84     PROTECTED = 2,
85 };
86 
87 class RenderEngine {
88 public:
89     enum class ContextPriority {
90         LOW = 1,
91         MEDIUM = 2,
92         HIGH = 3,
93         REALTIME = 4,
94     };
95 
96     enum class RenderEngineType {
97         GLES = 1,
98         THREADED = 2,
99         SKIA_GL = 3,
100         SKIA_GL_THREADED = 4,
101     };
102 
103     static std::unique_ptr<RenderEngine> create(const RenderEngineCreationArgs& args);
104 
105     virtual ~RenderEngine() = 0;
106 
107     // ----- BEGIN DEPRECATED INTERFACE -----
108     // This interface, while still in use until a suitable replacement is built,
109     // should be considered deprecated, minus some methods which still may be
110     // used to support legacy behavior.
111     virtual std::future<void> primeCache() = 0;
112 
113     // dump the extension strings. always call the base class.
114     virtual void dump(std::string& result) = 0;
115 
116     virtual void genTextures(size_t count, uint32_t* names) = 0;
117     virtual void deleteTextures(size_t count, uint32_t const* names) = 0;
118 
119     // queries that are required to be thread safe
120     virtual size_t getMaxTextureSize() const = 0;
121     virtual size_t getMaxViewportDims() const = 0;
122 
123     // ----- END DEPRECATED INTERFACE -----
124 
125     // ----- BEGIN NEW INTERFACE -----
126 
127     // queries that are required to be thread safe
128     virtual bool isProtected() const = 0;
129     virtual bool supportsProtectedContent() const = 0;
130 
131     // Attempt to switch RenderEngine into and out of protectedContext mode
132     virtual void useProtectedContext(bool useProtectedContext) = 0;
133 
134     // Notify RenderEngine of changes to the dimensions of the active display
135     // so that it can configure its internal caches accordingly.
136     virtual void onActiveDisplaySizeChanged(ui::Size size) = 0;
137 
138     // Renders layers for a particular display via GPU composition. This method
139     // should be called for every display that needs to be rendered via the GPU.
140     // @param display The display-wide settings that should be applied prior to
141     // drawing any layers.
142     //
143     // Assumptions when calling this method:
144     // 1. There is exactly one caller - i.e. multi-threading is not supported.
145     // 2. Additional threads may be calling the {bind,cache}ExternalTexture
146     // methods above. But the main thread is responsible for holding resources
147     // such that Image destruction does not occur while this method is called.
148     //
149     // TODO(b/136806342): This should behavior should ideally be fixed since
150     // the above two assumptions are brittle, as conditional thread safetyness
151     // may be insufficient when maximizing rendering performance in the future.
152     //
153     // @param layers The layers to draw onto the display, in Z-order.
154     // @param buffer The buffer which will be drawn to. This buffer will be
155     // ready once drawFence fires.
156     // @param useFramebufferCache True if the framebuffer cache should be used.
157     // If an implementation does not cache output framebuffers, then this
158     // parameter does nothing.
159     // @param bufferFence Fence signalling that the buffer is ready to be drawn
160     // to.
161     // @return A future object of RenderEngineResult struct indicating whether
162     // drawing was successful in async mode.
163     virtual std::future<RenderEngineResult> drawLayers(
164             const DisplaySettings& display, const std::vector<LayerSettings>& layers,
165             const std::shared_ptr<ExternalTexture>& buffer, 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     // Returns the priority this context was actually created with. Note: this may not be
176     // the same as specified at context creation time, due to implementation limits on the
177     // number of contexts that can be created at a specific priority level in the system.
178     virtual int getContextPriority() = 0;
179 
180     // Returns true if blur was requested in the RenderEngineCreationArgs and the implementation
181     // also supports background blur.  If false, no blur will be applied when drawing layers. This
182     // query is required to be thread safe.
183     virtual bool supportsBackgroundBlur() = 0;
184 
185     // Returns the current type of RenderEngine instance that was created.
186     // TODO(b/180767535): This is only implemented to allow for backend-specific behavior, which
187     // we should not allow in general, so remove this.
getRenderEngineType()188     RenderEngineType getRenderEngineType() const { return mRenderEngineType; }
189 
190     static void validateInputBufferUsage(const sp<GraphicBuffer>&);
191     static void validateOutputBufferUsage(const sp<GraphicBuffer>&);
192 
193     // Allows flinger to get the render engine thread id for power management with ADPF
194     // Returns the tid of the renderengine thread if it's threaded, and std::nullopt otherwise
getRenderEngineTid()195     virtual std::optional<pid_t> getRenderEngineTid() const { return std::nullopt; }
196 
setEnableTracing(bool)197     virtual void setEnableTracing(bool /*tracingEnabled*/) {}
198 
199 protected:
RenderEngine()200     RenderEngine() : RenderEngine(RenderEngineType::GLES) {}
201 
RenderEngine(RenderEngineType type)202     RenderEngine(RenderEngineType type) : mRenderEngineType(type) {}
203 
204     // Maps GPU resources for this buffer.
205     // Note that work may be deferred to an additional thread, i.e. this call
206     // is made asynchronously, but the caller can expect that map/unmap calls
207     // are performed in a manner that's conflict serializable, i.e. unmapping
208     // a buffer should never occur before binding the buffer if the caller
209     // called mapExternalTextureBuffer before calling unmap.
210     // Note also that if the buffer contains protected content, then mapping those GPU resources may
211     // be deferred until the buffer is really used for drawing. This is because typical SoCs that
212     // support protected memory only support a limited amount, so optimisitically mapping protected
213     // memory may be too burdensome. If a buffer contains protected content and the RenderEngine
214     // implementation supports protected context, then GPU resources may be mapped into both the
215     // protected and unprotected contexts.
216     // If the buffer may ever be written to by RenderEngine, then isRenderable must be true.
217     virtual void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) = 0;
218     // Unmaps GPU resources used by this buffer. This method should be
219     // invoked when the caller will no longer hold a reference to a GraphicBuffer
220     // and needs to clean up its resources.
221     // Note that if there are multiple callers holding onto the same buffer, then the buffer's
222     // resources may be internally ref-counted to guard against use-after-free errors. Note that
223     // work may be deferred to an additional thread, i.e. this call is expected to be made
224     // asynchronously, but the caller can expect that map/unmap calls are performed in a manner
225     // that's conflict serializable, i.e. unmap a buffer should never occur before binding the
226     // buffer if the caller called mapExternalTextureBuffer before calling unmap.
227     virtual void unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) = 0;
228 
229     // A thread safe query to determine if any post rendering cleanup is necessary.  Returning true
230     // is a signal that calling the postRenderCleanup method would be a no-op and that callers can
231     // avoid any thread synchronization that may be required by directly calling postRenderCleanup.
232     virtual bool canSkipPostRenderCleanup() const = 0;
233 
234     friend class impl::ExternalTexture;
235     friend class threaded::RenderEngineThreaded;
236     friend class RenderEngineTest_cleanupPostRender_cleansUpOnce_Test;
237     const RenderEngineType mRenderEngineType;
238 
239     virtual void drawLayersInternal(
240             const std::shared_ptr<std::promise<RenderEngineResult>>&& resultPromise,
241             const DisplaySettings& display, const std::vector<LayerSettings>& layers,
242             const std::shared_ptr<ExternalTexture>& buffer, const bool useFramebufferCache,
243             base::unique_fd&& bufferFence) = 0;
244 };
245 
246 struct RenderEngineCreationArgs {
247     int pixelFormat;
248     uint32_t imageCacheSize;
249     bool useColorManagement;
250     bool enableProtectedContext;
251     bool precacheToneMapperShaderOnly;
252     bool supportsBackgroundBlur;
253     RenderEngine::ContextPriority contextPriority;
254     RenderEngine::RenderEngineType renderEngineType;
255 
256     struct Builder;
257 
258 private:
259     // must be created by Builder via constructor with full argument list
RenderEngineCreationArgsRenderEngineCreationArgs260     RenderEngineCreationArgs(int _pixelFormat, uint32_t _imageCacheSize, bool _useColorManagement,
261                              bool _enableProtectedContext, bool _precacheToneMapperShaderOnly,
262                              bool _supportsBackgroundBlur,
263                              RenderEngine::ContextPriority _contextPriority,
264                              RenderEngine::RenderEngineType _renderEngineType)
265           : pixelFormat(_pixelFormat),
266             imageCacheSize(_imageCacheSize),
267             useColorManagement(_useColorManagement),
268             enableProtectedContext(_enableProtectedContext),
269             precacheToneMapperShaderOnly(_precacheToneMapperShaderOnly),
270             supportsBackgroundBlur(_supportsBackgroundBlur),
271             contextPriority(_contextPriority),
272             renderEngineType(_renderEngineType) {}
273     RenderEngineCreationArgs() = delete;
274 };
275 
276 struct RenderEngineCreationArgs::Builder {
BuilderBuilder277     Builder() {}
278 
setPixelFormatBuilder279     Builder& setPixelFormat(int pixelFormat) {
280         this->pixelFormat = pixelFormat;
281         return *this;
282     }
setImageCacheSizeBuilder283     Builder& setImageCacheSize(uint32_t imageCacheSize) {
284         this->imageCacheSize = imageCacheSize;
285         return *this;
286     }
setUseColorManagermentBuilder287     Builder& setUseColorManagerment(bool useColorManagement) {
288         this->useColorManagement = useColorManagement;
289         return *this;
290     }
setEnableProtectedContextBuilder291     Builder& setEnableProtectedContext(bool enableProtectedContext) {
292         this->enableProtectedContext = enableProtectedContext;
293         return *this;
294     }
setPrecacheToneMapperShaderOnlyBuilder295     Builder& setPrecacheToneMapperShaderOnly(bool precacheToneMapperShaderOnly) {
296         this->precacheToneMapperShaderOnly = precacheToneMapperShaderOnly;
297         return *this;
298     }
setSupportsBackgroundBlurBuilder299     Builder& setSupportsBackgroundBlur(bool supportsBackgroundBlur) {
300         this->supportsBackgroundBlur = supportsBackgroundBlur;
301         return *this;
302     }
setContextPriorityBuilder303     Builder& setContextPriority(RenderEngine::ContextPriority contextPriority) {
304         this->contextPriority = contextPriority;
305         return *this;
306     }
setRenderEngineTypeBuilder307     Builder& setRenderEngineType(RenderEngine::RenderEngineType renderEngineType) {
308         this->renderEngineType = renderEngineType;
309         return *this;
310     }
buildBuilder311     RenderEngineCreationArgs build() const {
312         return RenderEngineCreationArgs(pixelFormat, imageCacheSize, useColorManagement,
313                                         enableProtectedContext, precacheToneMapperShaderOnly,
314                                         supportsBackgroundBlur, contextPriority, renderEngineType);
315     }
316 
317 private:
318     // 1 means RGBA_8888
319     int pixelFormat = 1;
320     uint32_t imageCacheSize = 0;
321     bool useColorManagement = true;
322     bool enableProtectedContext = false;
323     bool precacheToneMapperShaderOnly = false;
324     bool supportsBackgroundBlur = false;
325     RenderEngine::ContextPriority contextPriority = RenderEngine::ContextPriority::MEDIUM;
326     RenderEngine::RenderEngineType renderEngineType =
327             RenderEngine::RenderEngineType::SKIA_GL_THREADED;
328 };
329 
330 struct RenderEngineResult {
331     // status indicates if drawing is successful
332     status_t status;
333     // drawFence will fire when the buffer has been drawn to and is ready to be examined.
334     base::unique_fd drawFence;
335 };
336 
337 } // namespace renderengine
338 } // namespace android
339 
340 #endif /* SF_RENDERENGINE_H_ */
341