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" |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 24 | |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame^] | 25 | #include <com_android_graphics_surfaceflinger_flags.h> |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 26 | #include <cutils/properties.h> |
| 27 | #include <log/log.h> |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 28 | |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame^] | 29 | // TODO: b/341728634 - Clean up conditional compilation. |
| 30 | #if COM_ANDROID_GRAPHICS_SURFACEFLINGER_FLAGS(GRAPHITE_RENDERENGINE) || \ |
| 31 | COM_ANDROID_GRAPHICS_SURFACEFLINGER_FLAGS(FORCE_COMPILE_GRAPHITE_RENDERENGINE) |
| 32 | #define COMPILE_GRAPHITE_RENDERENGINE 1 |
| 33 | #else |
| 34 | #define COMPILE_GRAPHITE_RENDERENGINE 0 |
| 35 | #endif |
| 36 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 37 | namespace android { |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 38 | namespace renderengine { |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 39 | |
Leon Scroggins III | c77162c | 2021-11-16 17:13:08 -0500 | [diff] [blame] | 40 | std::unique_ptr<RenderEngine> RenderEngine::create(const RenderEngineCreationArgs& args) { |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 41 | threaded::CreateInstanceFactory createInstanceFactory; |
| 42 | |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame^] | 43 | // TODO: b/341728634 - Clean up conditional compilation. |
| 44 | #if COMPILE_GRAPHITE_RENDERENGINE |
| 45 | const RenderEngine::SkiaBackend actualSkiaBackend = args.skiaBackend; |
| 46 | #else |
| 47 | if (args.skiaBackend == RenderEngine::SkiaBackend::GRAPHITE) { |
| 48 | ALOGE("RenderEngine with Graphite Skia backend was requested, but Graphite was not " |
| 49 | "included in the build. Falling back to Ganesh (%s)", |
| 50 | args.graphicsApi == RenderEngine::GraphicsApi::GL ? "GL" : "Vulkan"); |
| 51 | } |
| 52 | const RenderEngine::SkiaBackend actualSkiaBackend = RenderEngine::SkiaBackend::GANESH; |
| 53 | #endif |
| 54 | |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 55 | ALOGD("%sRenderEngine with %s Backend (%s)", args.threaded == Threaded::YES ? "Threaded " : "", |
| 56 | args.graphicsApi == GraphicsApi::GL ? "SkiaGL" : "SkiaVK", |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame^] | 57 | actualSkiaBackend == SkiaBackend::GANESH ? "Ganesh" : "Graphite"); |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 58 | |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame^] | 59 | // TODO: b/341728634 - Clean up conditional compilation. |
| 60 | #if COMPILE_GRAPHITE_RENDERENGINE |
| 61 | if (actualSkiaBackend == SkiaBackend::GRAPHITE) { |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 62 | createInstanceFactory = [args]() { |
| 63 | return android::renderengine::skia::GraphiteVkRenderEngine::create(args); |
| 64 | }; |
Nolan Scobie | 488101b | 2024-05-20 13:32:13 -0400 | [diff] [blame^] | 65 | } else |
| 66 | #endif |
| 67 | { // GANESH |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 68 | if (args.graphicsApi == GraphicsApi::VK) { |
| 69 | createInstanceFactory = [args]() { |
| 70 | return android::renderengine::skia::GaneshVkRenderEngine::create(args); |
| 71 | }; |
| 72 | } else { // GL |
| 73 | createInstanceFactory = [args]() { |
| 74 | return android::renderengine::skia::SkiaGLRenderEngine::create(args); |
| 75 | }; |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Nolan Scobie | 576e77f | 2024-03-26 10:59:59 -0400 | [diff] [blame] | 79 | if (args.threaded == Threaded::YES) { |
| 80 | return renderengine::threaded::RenderEngineThreaded::create(createInstanceFactory); |
| 81 | } else { |
| 82 | return createInstanceFactory(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 83 | } |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 86 | RenderEngine::~RenderEngine() = default; |
| 87 | |
Ady Abraham | 193426d | 2021-02-18 14:01:53 -0800 | [diff] [blame] | 88 | void RenderEngine::validateInputBufferUsage(const sp<GraphicBuffer>& buffer) { |
| 89 | LOG_ALWAYS_FATAL_IF(!(buffer->getUsage() & GraphicBuffer::USAGE_HW_TEXTURE), |
| 90 | "input buffer not gpu readable"); |
| 91 | } |
| 92 | |
| 93 | void RenderEngine::validateOutputBufferUsage(const sp<GraphicBuffer>& buffer) { |
| 94 | LOG_ALWAYS_FATAL_IF(!(buffer->getUsage() & GraphicBuffer::USAGE_HW_RENDER), |
| 95 | "output buffer not gpu writeable"); |
| 96 | } |
| 97 | |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 98 | ftl::Future<FenceResult> RenderEngine::drawLayers(const DisplaySettings& display, |
| 99 | const std::vector<LayerSettings>& layers, |
| 100 | const std::shared_ptr<ExternalTexture>& buffer, |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 101 | base::unique_fd&& bufferFence) { |
| 102 | const auto resultPromise = std::make_shared<std::promise<FenceResult>>(); |
| 103 | std::future<FenceResult> resultFuture = resultPromise->get_future(); |
Patrick Williams | 8aed5d2 | 2022-10-31 22:18:10 +0000 | [diff] [blame] | 104 | updateProtectedContext(layers, buffer); |
Alec Mouri | f29700f | 2023-08-17 21:53:31 +0000 | [diff] [blame] | 105 | drawLayersInternal(std::move(resultPromise), display, layers, buffer, std::move(bufferFence)); |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 106 | return resultFuture; |
| 107 | } |
| 108 | |
Patrick Williams | 8aed5d2 | 2022-10-31 22:18:10 +0000 | [diff] [blame] | 109 | void RenderEngine::updateProtectedContext(const std::vector<LayerSettings>& layers, |
| 110 | const std::shared_ptr<ExternalTexture>& buffer) { |
| 111 | const bool needsProtectedContext = |
| 112 | (buffer && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED)) || |
| 113 | std::any_of(layers.begin(), layers.end(), [](const LayerSettings& layer) { |
| 114 | const std::shared_ptr<ExternalTexture>& buffer = layer.source.buffer.buffer; |
| 115 | return buffer && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
| 116 | }); |
| 117 | useProtectedContext(needsProtectedContext); |
| 118 | } |
| 119 | |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 120 | } // namespace renderengine |
| 121 | } // namespace android |