Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 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 | |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 17 | #include <renderengine/RenderEngine.h> |
Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 18 | |
Patrick Williams | 8aed5d2 | 2022-10-31 22:18:10 +0000 | [diff] [blame] | 19 | #include "renderengine/ExternalTexture.h" |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 20 | #include "skia/GaneshVkRenderEngine.h" |
| 21 | #include "skia/GraphiteVkRenderEngine.h" |
| 22 | #include "skia/SkiaGLRenderEngine.h" |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 23 | #include "threaded/RenderEngineThreaded.h" |
Alec Mouri | 1b1853f | 2024-07-15 22:46:58 +0000 | [diff] [blame] | 24 | #include "ui/GraphicTypes.h" |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 25 | |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame] | 26 | #include <com_android_graphics_surfaceflinger_flags.h> |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 27 | #include <cutils/properties.h> |
| 28 | #include <log/log.h> |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 29 | |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame] | 30 | // 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 Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 38 | namespace android { |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 39 | namespace renderengine { |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 40 | |
Leon Scroggins III | c77162c | 2021-11-16 17:13:08 -0500 | [diff] [blame] | 41 | std::unique_ptr<RenderEngine> RenderEngine::create(const RenderEngineCreationArgs& args) { |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 42 | threaded::CreateInstanceFactory createInstanceFactory; |
| 43 | |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame] | 44 | // 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 Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 56 | ALOGD("%sRenderEngine with %s Backend (%s)", args.threaded == Threaded::YES ? "Threaded " : "", |
| 57 | args.graphicsApi == GraphicsApi::GL ? "SkiaGL" : "SkiaVK", |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame] | 58 | actualSkiaBackend == SkiaBackend::GANESH ? "Ganesh" : "Graphite"); |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 59 | |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame] | 60 | // TODO: b/341728634 - Clean up conditional compilation. |
| 61 | #if COMPILE_GRAPHITE_RENDERENGINE |
| 62 | if (actualSkiaBackend == SkiaBackend::GRAPHITE) { |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 63 | createInstanceFactory = [args]() { |
| 64 | return android::renderengine::skia::GraphiteVkRenderEngine::create(args); |
| 65 | }; |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame] | 66 | } else |
| 67 | #endif |
| 68 | { // GANESH |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 69 | 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 III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 80 | if (args.threaded == Threaded::YES) { |
| 81 | return renderengine::threaded::RenderEngineThreaded::create(createInstanceFactory); |
| 82 | } else { |
| 83 | return createInstanceFactory(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 84 | } |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 87 | RenderEngine::~RenderEngine() = default; |
| 88 | |
Ady Abraham | 193426d | 2021-02-18 14:01:53 -0800 | [diff] [blame] | 89 | void 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 | |
| 94 | void 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 Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 99 | ftl::Future<FenceResult> RenderEngine::drawLayers(const DisplaySettings& display, |
| 100 | const std::vector<LayerSettings>& layers, |
| 101 | const std::shared_ptr<ExternalTexture>& buffer, |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 102 | base::unique_fd&& bufferFence) { |
| 103 | const auto resultPromise = std::make_shared<std::promise<FenceResult>>(); |
| 104 | std::future<FenceResult> resultFuture = resultPromise->get_future(); |
Alec Mouri | 1b1853f | 2024-07-15 22:46:58 +0000 | [diff] [blame] | 105 | updateProtectedContext(layers, {buffer.get()}); |
Alec Mouri | f29700f | 2023-08-17 21:53:31 +0000 | [diff] [blame] | 106 | drawLayersInternal(std::move(resultPromise), display, layers, buffer, std::move(bufferFence)); |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 107 | return resultFuture; |
| 108 | } |
| 109 | |
Alec Mouri | 1b1853f | 2024-07-15 22:46:58 +0000 | [diff] [blame] | 110 | ftl::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 Williams | 8aed5d2 | 2022-10-31 22:18:10 +0000 | [diff] [blame] | 123 | void RenderEngine::updateProtectedContext(const std::vector<LayerSettings>& layers, |
Alec Mouri | 1b1853f | 2024-07-15 22:46:58 +0000 | [diff] [blame] | 124 | vector<const ExternalTexture*> buffers) { |
Patrick Williams | 8aed5d2 | 2022-10-31 22:18:10 +0000 | [diff] [blame] | 125 | const bool needsProtectedContext = |
Alec Mouri | 1b1853f | 2024-07-15 22:46:58 +0000 | [diff] [blame] | 126 | 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 Williams | 8aed5d2 | 2022-10-31 22:18:10 +0000 | [diff] [blame] | 133 | return buffer && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
| 134 | }); |
| 135 | useProtectedContext(needsProtectedContext); |
| 136 | } |
| 137 | |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 138 | } // namespace renderengine |
| 139 | } // namespace android |