blob: 39621cd080775df531eeb9a2af052e6908573089 [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>
Patrick Williams2e9748f2022-08-09 22:48:18 +000021#include <ftl/future.h>
Chia-I Wub027f802017-11-29 14:00:52 -080022#include <math/mat4.h>
Alec Mouri6e57f682018-09-29 20:45:08 -070023#include <renderengine/DisplaySettings.h>
Alec Mouri2daef3c2021-04-02 16:29:27 -070024#include <renderengine/ExternalTexture.h>
Peiyong Line5a9a7f2018-08-30 15:32:13 -070025#include <renderengine/Framebuffer.h>
Peiyong Lin60bedb52018-09-05 10:47:31 -070026#include <renderengine/Image.h>
Alec Mouri6e57f682018-09-29 20:45:08 -070027#include <renderengine/LayerSettings.h>
Alec Mouri2daef3c2021-04-02 16:29:27 -070028#include <stdint.h>
29#include <sys/types.h>
Patrick Williams2e9748f2022-08-09 22:48:18 +000030#include <ui/FenceResult.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070031#include <ui/GraphicTypes.h>
32#include <ui/Transform.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070033
Ady Abrahamfe2a6db2021-06-09 15:41:37 -070034#include <future>
Alec Mouri2daef3c2021-04-02 16:29:27 -070035#include <memory>
36
Peiyong Linb491b3c2018-08-15 14:53:34 -070037/**
Ana Krulec70d15b1b2020-12-01 10:05:15 -080038 * Allows to set RenderEngine backend to GLES (default) or SkiaGL (NOT yet supported).
Peiyong Linb491b3c2018-08-15 14:53:34 -070039 */
40#define PROPERTY_DEBUG_RENDERENGINE_BACKEND "debug.renderengine.backend"
Jesse Hall19e87292013-12-23 21:02:15 -080041
Ana Krulec70d15b1b2020-12-01 10:05:15 -080042/**
43 * Turns on recording of skia commands in SkiaGL version of the RE. This property
44 * defines number of milliseconds for the recording to take place. A non zero value
45 * turns on the recording.
46 */
47#define PROPERTY_DEBUG_RENDERENGINE_CAPTURE_SKIA_MS "debug.renderengine.capture_skia_ms"
48
Leon Scroggins III2eca03f2021-05-25 15:30:43 -040049/**
50 * Set to the most recently saved file once the capture is finished.
51 */
52#define PROPERTY_DEBUG_RENDERENGINE_CAPTURE_FILENAME "debug.renderengine.capture_filename"
53
rnleec6a73642021-06-04 14:16:42 -070054/**
55 * Allows recording of Skia drawing commands with systrace.
56 */
57#define PROPERTY_SKIA_ATRACE_ENABLED "debug.renderengine.skia_atrace_enabled"
58
Chia-I Wueadbaa62017-11-09 11:26:15 -080059struct ANativeWindowBuffer;
60
Mathias Agopian875d8e12013-06-07 15:35:48 -070061namespace android {
Mathias Agopian875d8e12013-06-07 15:35:48 -070062
Mathias Agopian3f844832013-08-07 21:24:32 -070063class Rect;
64class Region;
Peiyong Lin833074a2018-08-28 11:53:54 -070065
66namespace renderengine {
67
Alec Mouri2daef3c2021-04-02 16:29:27 -070068class ExternalTexture;
Chia-I Wu401ef832017-12-01 10:52:22 -080069class Image;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070070class Mesh;
Peiyong Lin833074a2018-08-28 11:53:54 -070071class Texture;
Peiyong Lin4137a1d2019-10-09 10:39:09 -070072struct RenderEngineCreationArgs;
Lloyd Pique144e1162017-12-20 16:44:52 -080073
Ana Krulec9bc9dc62020-02-26 12:16:40 -080074namespace threaded {
75class RenderEngineThreaded;
76}
77
Lloyd Pique144e1162017-12-20 16:44:52 -080078namespace impl {
79class RenderEngine;
Vishnu Nairdbbe3852022-01-12 20:22:11 -080080class ExternalTexture;
Lloyd Pique144e1162017-12-20 16:44:52 -080081}
Chia-I Wuf846a352017-11-10 09:22:52 -080082
Peiyong Linfb530cf2018-12-15 05:07:38 +000083enum class Protection {
84 UNPROTECTED = 1,
85 PROTECTED = 2,
86};
87
Mathias Agopian875d8e12013-06-07 15:35:48 -070088class RenderEngine {
Mathias Agopian875d8e12013-06-07 15:35:48 -070089public:
Peiyong Lin4137a1d2019-10-09 10:39:09 -070090 enum class ContextPriority {
91 LOW = 1,
92 MEDIUM = 2,
93 HIGH = 3,
Alec Mourid6f09462020-12-07 11:18:17 -080094 REALTIME = 4,
Kalle Raitabbdcf1f2017-05-22 15:47:46 -070095 };
Jesse Hall05f8c702013-12-23 20:44:38 -080096
Ana Krulec9bc9dc62020-02-26 12:16:40 -080097 enum class RenderEngineType {
98 GLES = 1,
99 THREADED = 2,
John Reck67b1e2b2020-08-26 13:17:24 -0700100 SKIA_GL = 3,
Ana Krulec243dbc22021-01-06 13:17:08 -0800101 SKIA_GL_THREADED = 4,
Ian Elliott1f0911e2022-09-09 16:31:47 -0600102 SKIA_VK = 5,
103 SKIA_VK_THREADED = 6,
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800104 };
105
Leon Scroggins IIIc77162c2021-11-16 17:13:08 -0500106 static std::unique_ptr<RenderEngine> create(const RenderEngineCreationArgs& args);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700107
Lloyd Pique144e1162017-12-20 16:44:52 -0800108 virtual ~RenderEngine() = 0;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700109
Alec Mouri6e57f682018-09-29 20:45:08 -0700110 // ----- BEGIN DEPRECATED INTERFACE -----
111 // This interface, while still in use until a suitable replacement is built,
112 // should be considered deprecated, minus some methods which still may be
113 // used to support legacy behavior.
Ady Abrahamfe2a6db2021-06-09 15:41:37 -0700114 virtual std::future<void> primeCache() = 0;
Dan Stoza4e637772016-07-28 13:31:51 -0700115
Mathias Agopian458197d2013-08-15 14:56:51 -0700116 // dump the extension strings. always call the base class.
Yiwei Zhang5434a782018-12-05 18:06:32 -0800117 virtual void dump(std::string& result) = 0;
Mathias Agopian458197d2013-08-15 14:56:51 -0700118
Lloyd Pique144e1162017-12-20 16:44:52 -0800119 virtual void genTextures(size_t count, uint32_t* names) = 0;
120 virtual void deleteTextures(size_t count, uint32_t const* names) = 0;
Alec Mouri368e1582020-08-13 10:14:29 -0700121
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400122 // queries that are required to be thread safe
Mathias Agopian875d8e12013-06-07 15:35:48 -0700123 virtual size_t getMaxTextureSize() const = 0;
124 virtual size_t getMaxViewportDims() const = 0;
Alec Mouri6e57f682018-09-29 20:45:08 -0700125
126 // ----- END DEPRECATED INTERFACE -----
127
128 // ----- BEGIN NEW INTERFACE -----
129
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400130 // queries that are required to be thread safe
Peiyong Linfb530cf2018-12-15 05:07:38 +0000131 virtual bool supportsProtectedContent() const = 0;
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400132
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.
Patrick Williams2e9748f2022-08-09 22:48:18 +0000160 // @return A future object of FenceResult indicating whether drawing was
161 // successful in async mode.
162 virtual ftl::Future<FenceResult> drawLayers(const DisplaySettings& display,
163 const std::vector<LayerSettings>& layers,
164 const std::shared_ptr<ExternalTexture>& buffer,
165 const bool useFramebufferCache,
166 base::unique_fd&& bufferFence);
Derek Sollenbergerd3f60652021-06-11 15:34:36 -0400167
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
Marin Shalamanov23c31af2020-09-09 15:01:47 +0200174 virtual void cleanFramebufferCache() = 0;
Ian Elliott1f0911e2022-09-09 16:31:47 -0600175
176 // Returns the priority this context was actually created with. Note: this
177 // may not be the same as specified at context creation time, due to
178 // implementation limits on the number of contexts that can be created at a
179 // specific priority level in the system.
180 //
181 // This should return a valid EGL context priority enum as described by
182 // https://registry.khronos.org/EGL/extensions/IMG/EGL_IMG_context_priority.txt
183 // or
184 // https://registry.khronos.org/EGL/extensions/NV/EGL_NV_context_priority_realtime.txt
Alec Mourid6f09462020-12-07 11:18:17 -0800185 virtual int getContextPriority() = 0;
Alec Mouri6e57f682018-09-29 20:45:08 -0700186
Derek Sollenbergerb3998372021-02-16 15:16:56 -0500187 // Returns true if blur was requested in the RenderEngineCreationArgs and the implementation
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400188 // also supports background blur. If false, no blur will be applied when drawing layers. This
189 // query is required to be thread safe.
Derek Sollenbergerb3998372021-02-16 15:16:56 -0500190 virtual bool supportsBackgroundBlur() = 0;
191
Alec Mouri0d995102021-02-24 16:53:38 -0800192 // Returns the current type of RenderEngine instance that was created.
193 // TODO(b/180767535): This is only implemented to allow for backend-specific behavior, which
194 // we should not allow in general, so remove this.
195 RenderEngineType getRenderEngineType() const { return mRenderEngineType; }
196
Ady Abraham193426d2021-02-18 14:01:53 -0800197 static void validateInputBufferUsage(const sp<GraphicBuffer>&);
198 static void validateOutputBufferUsage(const sp<GraphicBuffer>&);
199
Matt Buckleyef51fba2021-10-12 19:30:12 +0000200 // Allows flinger to get the render engine thread id for power management with ADPF
201 // Returns the tid of the renderengine thread if it's threaded, and std::nullopt otherwise
202 virtual std::optional<pid_t> getRenderEngineTid() const { return std::nullopt; }
203
Leon Scroggins IIIa37ca992022-02-02 18:08:20 -0500204 virtual void setEnableTracing(bool /*tracingEnabled*/) {}
205
Alec Mouri820c7402019-01-23 13:02:39 -0800206protected:
rnleec6a73642021-06-04 14:16:42 -0700207 RenderEngine() : RenderEngine(RenderEngineType::GLES) {}
208
209 RenderEngine(RenderEngineType type) : mRenderEngineType(type) {}
210
Alec Mouri2daef3c2021-04-02 16:29:27 -0700211 // Maps GPU resources for this buffer.
212 // Note that work may be deferred to an additional thread, i.e. this call
213 // is made asynchronously, but the caller can expect that map/unmap calls
214 // are performed in a manner that's conflict serializable, i.e. unmapping
215 // a buffer should never occur before binding the buffer if the caller
216 // called mapExternalTextureBuffer before calling unmap.
217 // Note also that if the buffer contains protected content, then mapping those GPU resources may
218 // be deferred until the buffer is really used for drawing. This is because typical SoCs that
219 // support protected memory only support a limited amount, so optimisitically mapping protected
220 // memory may be too burdensome. If a buffer contains protected content and the RenderEngine
221 // implementation supports protected context, then GPU resources may be mapped into both the
222 // protected and unprotected contexts.
223 // If the buffer may ever be written to by RenderEngine, then isRenderable must be true.
224 virtual void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) = 0;
225 // Unmaps GPU resources used by this buffer. This method should be
226 // invoked when the caller will no longer hold a reference to a GraphicBuffer
227 // and needs to clean up its resources.
228 // Note that if there are multiple callers holding onto the same buffer, then the buffer's
229 // resources may be internally ref-counted to guard against use-after-free errors. Note that
230 // work may be deferred to an additional thread, i.e. this call is expected to be made
231 // asynchronously, but the caller can expect that map/unmap calls are performed in a manner
232 // that's conflict serializable, i.e. unmap a buffer should never occur before binding the
233 // buffer if the caller called mapExternalTextureBuffer before calling unmap.
234 virtual void unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) = 0;
Derek Sollenbergerd3f60652021-06-11 15:34:36 -0400235
236 // A thread safe query to determine if any post rendering cleanup is necessary. Returning true
237 // is a signal that calling the postRenderCleanup method would be a no-op and that callers can
238 // avoid any thread synchronization that may be required by directly calling postRenderCleanup.
239 virtual bool canSkipPostRenderCleanup() const = 0;
240
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800241 friend class impl::ExternalTexture;
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800242 friend class threaded::RenderEngineThreaded;
Derek Sollenbergerd3f60652021-06-11 15:34:36 -0400243 friend class RenderEngineTest_cleanupPostRender_cleansUpOnce_Test;
Alec Mouri0d995102021-02-24 16:53:38 -0800244 const RenderEngineType mRenderEngineType;
Sally Qi4cabdd02021-08-05 16:45:57 -0700245
Patrick Williams8aed5d22022-10-31 22:18:10 +0000246 // Update protectedContext mode depending on whether or not any layer has a protected buffer.
247 void updateProtectedContext(const std::vector<LayerSettings>&,
248 const std::shared_ptr<ExternalTexture>&);
249
250 // Attempt to switch RenderEngine into and out of protectedContext mode
251 virtual void useProtectedContext(bool useProtectedContext) = 0;
252
Sally Qi4cabdd02021-08-05 16:45:57 -0700253 virtual void drawLayersInternal(
Patrick Williams2e9748f2022-08-09 22:48:18 +0000254 const std::shared_ptr<std::promise<FenceResult>>&& resultPromise,
Sally Qi59a9f502021-10-12 18:53:23 +0000255 const DisplaySettings& display, const std::vector<LayerSettings>& layers,
Sally Qi4cabdd02021-08-05 16:45:57 -0700256 const std::shared_ptr<ExternalTexture>& buffer, const bool useFramebufferCache,
257 base::unique_fd&& bufferFence) = 0;
Lloyd Pique144e1162017-12-20 16:44:52 -0800258};
259
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700260struct RenderEngineCreationArgs {
261 int pixelFormat;
262 uint32_t imageCacheSize;
263 bool useColorManagement;
264 bool enableProtectedContext;
265 bool precacheToneMapperShaderOnly;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800266 bool supportsBackgroundBlur;
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700267 RenderEngine::ContextPriority contextPriority;
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800268 RenderEngine::RenderEngineType renderEngineType;
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700269
270 struct Builder;
271
272private:
273 // must be created by Builder via constructor with full argument list
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800274 RenderEngineCreationArgs(int _pixelFormat, uint32_t _imageCacheSize, bool _useColorManagement,
275 bool _enableProtectedContext, bool _precacheToneMapperShaderOnly,
276 bool _supportsBackgroundBlur,
277 RenderEngine::ContextPriority _contextPriority,
278 RenderEngine::RenderEngineType _renderEngineType)
279 : pixelFormat(_pixelFormat),
280 imageCacheSize(_imageCacheSize),
281 useColorManagement(_useColorManagement),
282 enableProtectedContext(_enableProtectedContext),
283 precacheToneMapperShaderOnly(_precacheToneMapperShaderOnly),
284 supportsBackgroundBlur(_supportsBackgroundBlur),
285 contextPriority(_contextPriority),
286 renderEngineType(_renderEngineType) {}
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700287 RenderEngineCreationArgs() = delete;
288};
289
290struct RenderEngineCreationArgs::Builder {
291 Builder() {}
292
293 Builder& setPixelFormat(int pixelFormat) {
294 this->pixelFormat = pixelFormat;
295 return *this;
296 }
297 Builder& setImageCacheSize(uint32_t imageCacheSize) {
298 this->imageCacheSize = imageCacheSize;
299 return *this;
300 }
301 Builder& setUseColorManagerment(bool useColorManagement) {
302 this->useColorManagement = useColorManagement;
303 return *this;
304 }
305 Builder& setEnableProtectedContext(bool enableProtectedContext) {
306 this->enableProtectedContext = enableProtectedContext;
307 return *this;
308 }
309 Builder& setPrecacheToneMapperShaderOnly(bool precacheToneMapperShaderOnly) {
310 this->precacheToneMapperShaderOnly = precacheToneMapperShaderOnly;
311 return *this;
312 }
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800313 Builder& setSupportsBackgroundBlur(bool supportsBackgroundBlur) {
314 this->supportsBackgroundBlur = supportsBackgroundBlur;
315 return *this;
316 }
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700317 Builder& setContextPriority(RenderEngine::ContextPriority contextPriority) {
318 this->contextPriority = contextPriority;
319 return *this;
320 }
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800321 Builder& setRenderEngineType(RenderEngine::RenderEngineType renderEngineType) {
322 this->renderEngineType = renderEngineType;
323 return *this;
324 }
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700325 RenderEngineCreationArgs build() const {
326 return RenderEngineCreationArgs(pixelFormat, imageCacheSize, useColorManagement,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800327 enableProtectedContext, precacheToneMapperShaderOnly,
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800328 supportsBackgroundBlur, contextPriority, renderEngineType);
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700329 }
330
331private:
332 // 1 means RGBA_8888
333 int pixelFormat = 1;
334 uint32_t imageCacheSize = 0;
335 bool useColorManagement = true;
336 bool enableProtectedContext = false;
337 bool precacheToneMapperShaderOnly = false;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800338 bool supportsBackgroundBlur = false;
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700339 RenderEngine::ContextPriority contextPriority = RenderEngine::ContextPriority::MEDIUM;
Derek Sollenberger8b305ef2021-04-09 14:11:47 +0000340 RenderEngine::RenderEngineType renderEngineType =
341 RenderEngine::RenderEngineType::SKIA_GL_THREADED;
Peiyong Lin4137a1d2019-10-09 10:39:09 -0700342};
343
Peiyong Lin46080ef2018-10-26 18:43:14 -0700344} // namespace renderengine
345} // namespace android
Mathias Agopian875d8e12013-06-07 15:35:48 -0700346
347#endif /* SF_RENDERENGINE_H_ */