blob: d646756fc400a1298ede19a68c5723bef47a313e [file] [log] [blame]
Mathias Agopian875d8e12013-06-07 15:35:48 -07001/*
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
Mathias Agopian875d8e12013-06-07 15:35:48 -070017#ifndef SF_RENDERENGINE_H_
18#define SF_RENDERENGINE_H_
19
Chia-I Wu767fcf72017-11-30 22:07:38 -080020#include <android-base/unique_fd.h>
Chia-I Wub027f802017-11-29 14:00:52 -080021#include <math/mat4.h>
Alec Mouri6e57f682018-09-29 20:45:08 -070022#include <renderengine/DisplaySettings.h>
Alec Mouri2daef3c2021-04-02 16:29:27 -070023#include <renderengine/ExternalTexture.h>
Peiyong Line5a9a7f2018-08-30 15:32:13 -070024#include <renderengine/Framebuffer.h>
Peiyong Lin60bedb52018-09-05 10:47:31 -070025#include <renderengine/Image.h>
Alec Mouri6e57f682018-09-29 20:45:08 -070026#include <renderengine/LayerSettings.h>
Alec Mouri2daef3c2021-04-02 16:29:27 -070027#include <stdint.h>
28#include <sys/types.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070029#include <ui/GraphicTypes.h>
30#include <ui/Transform.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070031
Ady Abrahamfe2a6db2021-06-09 15:41:37 -070032#include <future>
Alec Mouri2daef3c2021-04-02 16:29:27 -070033#include <memory>
34
Peiyong Linb491b3c2018-08-15 14:53:34 -070035/**
Ana Krulec70d15b1b2020-12-01 10:05:15 -080036 * Allows to set RenderEngine backend to GLES (default) or SkiaGL (NOT yet supported).
Peiyong Linb491b3c2018-08-15 14:53:34 -070037 */
38#define PROPERTY_DEBUG_RENDERENGINE_BACKEND "debug.renderengine.backend"
Jesse Hall19e87292013-12-23 21:02:15 -080039
Ana Krulec70d15b1b2020-12-01 10:05:15 -080040/**
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
Leon Scroggins III2eca03f2021-05-25 15:30:43 -040047/**
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
rnleec6a73642021-06-04 14:16:42 -070052/**
53 * Allows recording of Skia drawing commands with systrace.
54 */
55#define PROPERTY_SKIA_ATRACE_ENABLED "debug.renderengine.skia_atrace_enabled"
56
Chia-I Wueadbaa62017-11-09 11:26:15 -080057struct ANativeWindowBuffer;
58
Mathias Agopian875d8e12013-06-07 15:35:48 -070059namespace android {
Mathias Agopian875d8e12013-06-07 15:35:48 -070060
Mathias Agopian3f844832013-08-07 21:24:32 -070061class Rect;
62class Region;
Peiyong Lin833074a2018-08-28 11:53:54 -070063
64namespace renderengine {
65
Alec Mouri2daef3c2021-04-02 16:29:27 -070066class ExternalTexture;
Chia-I Wu401ef832017-12-01 10:52:22 -080067class Image;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070068class Mesh;
Peiyong Lin833074a2018-08-28 11:53:54 -070069class Texture;
Peiyong Lin4137a1d2019-10-09 10:39:09 -070070struct RenderEngineCreationArgs;
Sally Qi4cabdd02021-08-05 16:45:57 -070071struct RenderEngineResult;
Lloyd Pique144e1162017-12-20 16:44:52 -080072
Ana Krulec9bc9dc62020-02-26 12:16:40 -080073namespace threaded {
74class RenderEngineThreaded;
75}
76
Lloyd Pique144e1162017-12-20 16:44:52 -080077namespace impl {
78class RenderEngine;
79}
Chia-I Wuf846a352017-11-10 09:22:52 -080080
Peiyong Linfb530cf2018-12-15 05:07:38 +000081enum class Protection {
82 UNPROTECTED = 1,
83 PROTECTED = 2,
84};
85
Mathias Agopian875d8e12013-06-07 15:35:48 -070086class RenderEngine {
Mathias Agopian875d8e12013-06-07 15:35:48 -070087public:
Peiyong Lin4137a1d2019-10-09 10:39:09 -070088 enum class ContextPriority {
89 LOW = 1,
90 MEDIUM = 2,
91 HIGH = 3,
Alec Mourid6f09462020-12-07 11:18:17 -080092 REALTIME = 4,
Kalle Raitabbdcf1f2017-05-22 15:47:46 -070093 };
Jesse Hall05f8c702013-12-23 20:44:38 -080094
Ana Krulec9bc9dc62020-02-26 12:16:40 -080095 enum class RenderEngineType {
96 GLES = 1,
97 THREADED = 2,
John Reck67b1e2b2020-08-26 13:17:24 -070098 SKIA_GL = 3,
Ana Krulec243dbc22021-01-06 13:17:08 -080099 SKIA_GL_THREADED = 4,
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800100 };
101
Leon Scroggins IIIc77162c2021-11-16 17:13:08 -0500102 static std::unique_ptr<RenderEngine> create(const RenderEngineCreationArgs& args);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700103
Lloyd Pique144e1162017-12-20 16:44:52 -0800104 virtual ~RenderEngine() = 0;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700105
Alec Mouri6e57f682018-09-29 20:45:08 -0700106 // ----- BEGIN DEPRECATED INTERFACE -----
107 // This interface, while still in use until a suitable replacement is built,
108 // should be considered deprecated, minus some methods which still may be
109 // used to support legacy behavior.
Ady Abrahamfe2a6db2021-06-09 15:41:37 -0700110 virtual std::future<void> primeCache() = 0;
Dan Stoza4e637772016-07-28 13:31:51 -0700111
Mathias Agopian458197d2013-08-15 14:56:51 -0700112 // dump the extension strings. always call the base class.
Yiwei Zhang5434a782018-12-05 18:06:32 -0800113 virtual void dump(std::string& result) = 0;
Mathias Agopian458197d2013-08-15 14:56:51 -0700114
Lloyd Pique144e1162017-12-20 16:44:52 -0800115 virtual void genTextures(size_t count, uint32_t* names) = 0;
116 virtual void deleteTextures(size_t count, uint32_t const* names) = 0;
Alec Mouri368e1582020-08-13 10:14:29 -0700117
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400118 // queries that are required to be thread safe
Mathias Agopian875d8e12013-06-07 15:35:48 -0700119 virtual size_t getMaxTextureSize() const = 0;
120 virtual size_t getMaxViewportDims() const = 0;
Alec Mouri6e57f682018-09-29 20:45:08 -0700121
122 // ----- END DEPRECATED INTERFACE -----
123
124 // ----- BEGIN NEW INTERFACE -----
125
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400126 // queries that are required to be thread safe
Peiyong Linfb530cf2018-12-15 05:07:38 +0000127 virtual bool isProtected() const = 0;
128 virtual bool supportsProtectedContent() const = 0;
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400129
130 // Attempt to switch RenderEngine into and out of protectedContext mode
Derek Sollenberger1ec2fb52021-06-16 15:11:27 -0400131 virtual void useProtectedContext(bool useProtectedContext) = 0;
Peiyong Linfb530cf2018-12-15 05:07:38 +0000132
Ady Abrahamed3290f2021-05-17 15:12:14 -0700133 // Notify RenderEngine of changes to the dimensions of the active display
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -0400134 // so that it can configure its internal caches accordingly.
Ady Abrahamed3290f2021-05-17 15:12:14 -0700135 virtual void onActiveDisplaySizeChanged(ui::Size size) = 0;
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -0400136
Alec Mouri6e57f682018-09-29 20:45:08 -0700137 // 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.
Alec Mouri1089aed2018-10-25 21:33:57 -0700139 // @param display The display-wide settings that should be applied prior to
Alec Mouri6e57f682018-09-29 20:45:08 -0700140 // drawing any layers.
Alec Mouri3d7c5612019-07-09 13:51:37 -0700141 //
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 //
Alec Mouri6e57f682018-09-29 20:45:08 -0700152 // @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
Alec Mouri6338c9d2019-02-07 16:57:51 -0800154 // ready once drawFence fires.
Alec Mourife0d72b2019-03-21 14:05:56 -0700155 // @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.
Alec Mouri6338c9d2019-02-07 16:57:51 -0800158 // @param bufferFence Fence signalling that the buffer is ready to be drawn
159 // to.
Sally Qi4cabdd02021-08-05 16:45:57 -0700160 // @return A future object of RenderEngineResult struct indicating whether
161 // drawing was successful in async mode.
162 virtual std::future<RenderEngineResult> drawLayers(
Sally Qi59a9f502021-10-12 18:53:23 +0000163 const DisplaySettings& display, const std::vector<LayerSettings>& layers,
Sally Qi4cabdd02021-08-05 16:45:57 -0700164 const std::shared_ptr<ExternalTexture>& buffer, const bool useFramebufferCache,
165 base::unique_fd&& bufferFence);
Derek Sollenbergerd3f60652021-06-11 15:34:36 -0400166
167 // Clean-up method that should be called on the main thread after the
168 // drawFence returned by drawLayers fires. This method will free up
169 // resources used by the most recently drawn frame. If the frame is still
170 // being drawn, then the implementation is free to silently ignore this call.
171 virtual void cleanupPostRender() = 0;
172
Marin Shalamanov23c31af2020-09-09 15:01:47 +0200173 virtual void cleanFramebufferCache() = 0;
Alec Mourid6f09462020-12-07 11:18:17 -0800174 // Returns the priority this context was actually created with. Note: this may not be
175 // the same as specified at context creation time, due to implementation limits on the
176 // number of contexts that can be created at a specific priority level in the system.
177 virtual int getContextPriority() = 0;
Alec Mouri6e57f682018-09-29 20:45:08 -0700178
Derek Sollenbergerb3998372021-02-16 15:16:56 -0500179 // Returns true if blur was requested in the RenderEngineCreationArgs and the implementation
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400180 // also supports background blur. If false, no blur will be applied when drawing layers. This
181 // query is required to be thread safe.
Derek Sollenbergerb3998372021-02-16 15:16:56 -0500182 virtual bool supportsBackgroundBlur() = 0;
183
Alec Mouri0d995102021-02-24 16:53:38 -0800184 // Returns the current type of RenderEngine instance that was created.
185 // TODO(b/180767535): This is only implemented to allow for backend-specific behavior, which
186 // we should not allow in general, so remove this.
187 RenderEngineType getRenderEngineType() const { return mRenderEngineType; }
188
Ady Abraham193426d2021-02-18 14:01:53 -0800189 static void validateInputBufferUsage(const sp<GraphicBuffer>&);
190 static void validateOutputBufferUsage(const sp<GraphicBuffer>&);
191
Matt Buckleyef51fba2021-10-12 19:30:12 +0000192 // Allows flinger to get the render engine thread id for power management with ADPF
193 // Returns the tid of the renderengine thread if it's threaded, and std::nullopt otherwise
194 virtual std::optional<pid_t> getRenderEngineTid() const { return std::nullopt; }
195
Alec Mouri820c7402019-01-23 13:02:39 -0800196protected:
rnleec6a73642021-06-04 14:16:42 -0700197 RenderEngine() : RenderEngine(RenderEngineType::GLES) {}
198
199 RenderEngine(RenderEngineType type) : mRenderEngineType(type) {}
200
Alec Mouri2daef3c2021-04-02 16:29:27 -0700201 // 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;
Derek Sollenbergerd3f60652021-06-11 15:34:36 -0400225
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
Alec Mouri2daef3c2021-04-02 16:29:27 -0700231 friend class ExternalTexture;
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800232 friend class threaded::RenderEngineThreaded;
Derek Sollenbergerd3f60652021-06-11 15:34:36 -0400233 friend class RenderEngineTest_cleanupPostRender_cleansUpOnce_Test;
Alec Mouri0d995102021-02-24 16:53:38 -0800234 const RenderEngineType mRenderEngineType;
Sally Qi4cabdd02021-08-05 16:45:57 -0700235
236 virtual void drawLayersInternal(
237 const std::shared_ptr<std::promise<RenderEngineResult>>&& resultPromise,
Sally Qi59a9f502021-10-12 18:53:23 +0000238 const DisplaySettings& display, const std::vector<LayerSettings>& layers,
Sally Qi4cabdd02021-08-05 16:45:57 -0700239 const std::shared_ptr<ExternalTexture>& buffer, const bool useFramebufferCache,
240 base::unique_fd&& bufferFence) = 0;
Lloyd Pique144e1162017-12-20 16:44:52 -0800241};
242
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700243struct RenderEngineCreationArgs {
244 int pixelFormat;
245 uint32_t imageCacheSize;
246 bool useColorManagement;
247 bool enableProtectedContext;
248 bool precacheToneMapperShaderOnly;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800249 bool supportsBackgroundBlur;
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700250 RenderEngine::ContextPriority contextPriority;
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800251 RenderEngine::RenderEngineType renderEngineType;
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700252
253 struct Builder;
254
255private:
256 // must be created by Builder via constructor with full argument list
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800257 RenderEngineCreationArgs(int _pixelFormat, uint32_t _imageCacheSize, bool _useColorManagement,
258 bool _enableProtectedContext, bool _precacheToneMapperShaderOnly,
259 bool _supportsBackgroundBlur,
260 RenderEngine::ContextPriority _contextPriority,
261 RenderEngine::RenderEngineType _renderEngineType)
262 : pixelFormat(_pixelFormat),
263 imageCacheSize(_imageCacheSize),
264 useColorManagement(_useColorManagement),
265 enableProtectedContext(_enableProtectedContext),
266 precacheToneMapperShaderOnly(_precacheToneMapperShaderOnly),
267 supportsBackgroundBlur(_supportsBackgroundBlur),
268 contextPriority(_contextPriority),
269 renderEngineType(_renderEngineType) {}
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700270 RenderEngineCreationArgs() = delete;
271};
272
273struct RenderEngineCreationArgs::Builder {
274 Builder() {}
275
276 Builder& setPixelFormat(int pixelFormat) {
277 this->pixelFormat = pixelFormat;
278 return *this;
279 }
280 Builder& setImageCacheSize(uint32_t imageCacheSize) {
281 this->imageCacheSize = imageCacheSize;
282 return *this;
283 }
284 Builder& setUseColorManagerment(bool useColorManagement) {
285 this->useColorManagement = useColorManagement;
286 return *this;
287 }
288 Builder& setEnableProtectedContext(bool enableProtectedContext) {
289 this->enableProtectedContext = enableProtectedContext;
290 return *this;
291 }
292 Builder& setPrecacheToneMapperShaderOnly(bool precacheToneMapperShaderOnly) {
293 this->precacheToneMapperShaderOnly = precacheToneMapperShaderOnly;
294 return *this;
295 }
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800296 Builder& setSupportsBackgroundBlur(bool supportsBackgroundBlur) {
297 this->supportsBackgroundBlur = supportsBackgroundBlur;
298 return *this;
299 }
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700300 Builder& setContextPriority(RenderEngine::ContextPriority contextPriority) {
301 this->contextPriority = contextPriority;
302 return *this;
303 }
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800304 Builder& setRenderEngineType(RenderEngine::RenderEngineType renderEngineType) {
305 this->renderEngineType = renderEngineType;
306 return *this;
307 }
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700308 RenderEngineCreationArgs build() const {
309 return RenderEngineCreationArgs(pixelFormat, imageCacheSize, useColorManagement,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800310 enableProtectedContext, precacheToneMapperShaderOnly,
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800311 supportsBackgroundBlur, contextPriority, renderEngineType);
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700312 }
313
314private:
315 // 1 means RGBA_8888
316 int pixelFormat = 1;
317 uint32_t imageCacheSize = 0;
318 bool useColorManagement = true;
319 bool enableProtectedContext = false;
320 bool precacheToneMapperShaderOnly = false;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800321 bool supportsBackgroundBlur = false;
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700322 RenderEngine::ContextPriority contextPriority = RenderEngine::ContextPriority::MEDIUM;
Derek Sollenberger8b305ef2021-04-09 14:11:47 +0000323 RenderEngine::RenderEngineType renderEngineType =
324 RenderEngine::RenderEngineType::SKIA_GL_THREADED;
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700325};
326
Sally Qi4cabdd02021-08-05 16:45:57 -0700327struct RenderEngineResult {
328 // status indicates if drawing is successful
329 status_t status;
330 // drawFence will fire when the buffer has been drawn to and is ready to be examined.
331 base::unique_fd drawFence;
332};
333
Peiyong Lin46080ef2018-10-26 18:43:14 -0700334} // namespace renderengine
335} // namespace android
Mathias Agopian875d8e12013-06-07 15:35:48 -0700336
337#endif /* SF_RENDERENGINE_H_ */