blob: 907590a236b2763a2e6b43f40d969262a08a2936 [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
Peiyong Lincbc184f2018-08-22 13:24:10 -070017#include <renderengine/RenderEngine.h>
Fabien Sanglardc93afd52017-03-13 13:02:42 -070018
Patrick Williams8aed5d22022-10-31 22:18:10 +000019#include "renderengine/ExternalTexture.h"
Nolan Scobie576e77f2024-03-26 10:59:59 -040020#include "skia/GaneshVkRenderEngine.h"
21#include "skia/GraphiteVkRenderEngine.h"
22#include "skia/SkiaGLRenderEngine.h"
Ana Krulec9bc9dc62020-02-26 12:16:40 -080023#include "threaded/RenderEngineThreaded.h"
Alec Mouri1b1853f2024-07-15 22:46:58 +000024#include "ui/GraphicTypes.h"
Jesse Hall19e87292013-12-23 21:02:15 -080025
Nolan Scobie488101b2024-05-20 13:32:13 -040026#include <com_android_graphics_surfaceflinger_flags.h>
Nolan Scobie576e77f2024-03-26 10:59:59 -040027#include <cutils/properties.h>
28#include <log/log.h>
John Reck67b1e2b2020-08-26 13:17:24 -070029
Nolan Scobie488101b2024-05-20 13:32:13 -040030// TODO: b/341728634 - Clean up conditional compilation.
31#if COM_ANDROID_GRAPHICS_SURFACEFLINGER_FLAGS(GRAPHITE_RENDERENGINE) || \
32 COM_ANDROID_GRAPHICS_SURFACEFLINGER_FLAGS(FORCE_COMPILE_GRAPHITE_RENDERENGINE)
33#define COMPILE_GRAPHITE_RENDERENGINE 1
34#else
35#define COMPILE_GRAPHITE_RENDERENGINE 0
36#endif
37
Mathias Agopian875d8e12013-06-07 15:35:48 -070038namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070039namespace renderengine {
Mathias Agopian875d8e12013-06-07 15:35:48 -070040
Leon Scroggins IIIc77162c2021-11-16 17:13:08 -050041std::unique_ptr<RenderEngine> RenderEngine::create(const RenderEngineCreationArgs& args) {
Nolan Scobie576e77f2024-03-26 10:59:59 -040042 threaded::CreateInstanceFactory createInstanceFactory;
43
Nolan Scobie488101b2024-05-20 13:32:13 -040044// TODO: b/341728634 - Clean up conditional compilation.
45#if COMPILE_GRAPHITE_RENDERENGINE
46 const RenderEngine::SkiaBackend actualSkiaBackend = args.skiaBackend;
47#else
48 if (args.skiaBackend == RenderEngine::SkiaBackend::GRAPHITE) {
49 ALOGE("RenderEngine with Graphite Skia backend was requested, but Graphite was not "
50 "included in the build. Falling back to Ganesh (%s)",
51 args.graphicsApi == RenderEngine::GraphicsApi::GL ? "GL" : "Vulkan");
52 }
53 const RenderEngine::SkiaBackend actualSkiaBackend = RenderEngine::SkiaBackend::GANESH;
54#endif
55
Nolan Scobie576e77f2024-03-26 10:59:59 -040056 ALOGD("%sRenderEngine with %s Backend (%s)", args.threaded == Threaded::YES ? "Threaded " : "",
57 args.graphicsApi == GraphicsApi::GL ? "SkiaGL" : "SkiaVK",
Nolan Scobie488101b2024-05-20 13:32:13 -040058 actualSkiaBackend == SkiaBackend::GANESH ? "Ganesh" : "Graphite");
Nolan Scobie576e77f2024-03-26 10:59:59 -040059
Nolan Scobie488101b2024-05-20 13:32:13 -040060// TODO: b/341728634 - Clean up conditional compilation.
61#if COMPILE_GRAPHITE_RENDERENGINE
62 if (actualSkiaBackend == SkiaBackend::GRAPHITE) {
Nolan Scobie576e77f2024-03-26 10:59:59 -040063 createInstanceFactory = [args]() {
64 return android::renderengine::skia::GraphiteVkRenderEngine::create(args);
65 };
Nolan Scobie488101b2024-05-20 13:32:13 -040066 } else
67#endif
68 { // GANESH
Nolan Scobie576e77f2024-03-26 10:59:59 -040069 if (args.graphicsApi == GraphicsApi::VK) {
70 createInstanceFactory = [args]() {
71 return android::renderengine::skia::GaneshVkRenderEngine::create(args);
72 };
73 } else { // GL
74 createInstanceFactory = [args]() {
75 return android::renderengine::skia::SkiaGLRenderEngine::create(args);
76 };
Leon Scroggins III696bf932024-01-24 15:21:05 -050077 }
78 }
79
Nolan Scobie576e77f2024-03-26 10:59:59 -040080 if (args.threaded == Threaded::YES) {
81 return renderengine::threaded::RenderEngineThreaded::create(createInstanceFactory);
82 } else {
83 return createInstanceFactory();
Ana Krulec9bc9dc62020-02-26 12:16:40 -080084 }
Peiyong Linf11f39b2018-09-05 14:37:41 -070085}
86
Lloyd Pique144e1162017-12-20 16:44:52 -080087RenderEngine::~RenderEngine() = default;
88
Ady Abraham193426d2021-02-18 14:01:53 -080089void RenderEngine::validateInputBufferUsage(const sp<GraphicBuffer>& buffer) {
90 LOG_ALWAYS_FATAL_IF(!(buffer->getUsage() & GraphicBuffer::USAGE_HW_TEXTURE),
91 "input buffer not gpu readable");
92}
93
94void RenderEngine::validateOutputBufferUsage(const sp<GraphicBuffer>& buffer) {
95 LOG_ALWAYS_FATAL_IF(!(buffer->getUsage() & GraphicBuffer::USAGE_HW_RENDER),
96 "output buffer not gpu writeable");
97}
98
Patrick Williams2e9748f2022-08-09 22:48:18 +000099ftl::Future<FenceResult> RenderEngine::drawLayers(const DisplaySettings& display,
100 const std::vector<LayerSettings>& layers,
101 const std::shared_ptr<ExternalTexture>& buffer,
Patrick Williams2e9748f2022-08-09 22:48:18 +0000102 base::unique_fd&& bufferFence) {
103 const auto resultPromise = std::make_shared<std::promise<FenceResult>>();
104 std::future<FenceResult> resultFuture = resultPromise->get_future();
Alec Mouri1b1853f2024-07-15 22:46:58 +0000105 updateProtectedContext(layers, {buffer.get()});
Alec Mourif29700f2023-08-17 21:53:31 +0000106 drawLayersInternal(std::move(resultPromise), display, layers, buffer, std::move(bufferFence));
Sally Qi4cabdd02021-08-05 16:45:57 -0700107 return resultFuture;
108}
109
Alec Mouri1b1853f2024-07-15 22:46:58 +0000110ftl::Future<FenceResult> RenderEngine::drawGainmap(
111 const std::shared_ptr<ExternalTexture>& sdr, base::borrowed_fd&& sdrFence,
112 const std::shared_ptr<ExternalTexture>& hdr, base::borrowed_fd&& hdrFence,
113 float hdrSdrRatio, ui::Dataspace dataspace,
114 const std::shared_ptr<ExternalTexture>& gainmap) {
115 const auto resultPromise = std::make_shared<std::promise<FenceResult>>();
116 std::future<FenceResult> resultFuture = resultPromise->get_future();
117 updateProtectedContext({}, {sdr.get(), hdr.get(), gainmap.get()});
118 drawGainmapInternal(std::move(resultPromise), sdr, std::move(sdrFence), hdr,
119 std::move(hdrFence), hdrSdrRatio, dataspace, gainmap);
120 return resultFuture;
121}
122
Patrick Williams8aed5d22022-10-31 22:18:10 +0000123void RenderEngine::updateProtectedContext(const std::vector<LayerSettings>& layers,
Alec Mouri1b1853f2024-07-15 22:46:58 +0000124 vector<const ExternalTexture*> buffers) {
Patrick Williams8aed5d22022-10-31 22:18:10 +0000125 const bool needsProtectedContext =
Alec Mouri1b1853f2024-07-15 22:46:58 +0000126 std::any_of(layers.begin(), layers.end(),
127 [](const LayerSettings& layer) {
128 const std::shared_ptr<ExternalTexture>& buffer =
129 layer.source.buffer.buffer;
130 return buffer && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
131 }) ||
132 std::any_of(buffers.begin(), buffers.end(), [](const ExternalTexture* buffer) {
Patrick Williams8aed5d22022-10-31 22:18:10 +0000133 return buffer && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
134 });
135 useProtectedContext(needsProtectedContext);
136}
137
Peiyong Lin46080ef2018-10-26 18:43:14 -0700138} // namespace renderengine
139} // namespace android