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
17 #include <renderengine/RenderEngine.h>
18
19 #include "renderengine/ExternalTexture.h"
20 #include "skia/GaneshVkRenderEngine.h"
21 #include "skia/GraphiteVkRenderEngine.h"
22 #include "skia/SkiaGLRenderEngine.h"
23 #include "threaded/RenderEngineThreaded.h"
24
25 #include <com_android_graphics_surfaceflinger_flags.h>
26 #include <cutils/properties.h>
27 #include <log/log.h>
28
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
37 namespace android {
38 namespace renderengine {
39
create(const RenderEngineCreationArgs & args)40 std::unique_ptr<RenderEngine> RenderEngine::create(const RenderEngineCreationArgs& args) {
41 threaded::CreateInstanceFactory createInstanceFactory;
42
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
55 ALOGD("%sRenderEngine with %s Backend (%s)", args.threaded == Threaded::YES ? "Threaded " : "",
56 args.graphicsApi == GraphicsApi::GL ? "SkiaGL" : "SkiaVK",
57 actualSkiaBackend == SkiaBackend::GANESH ? "Ganesh" : "Graphite");
58
59 // TODO: b/341728634 - Clean up conditional compilation.
60 #if COMPILE_GRAPHITE_RENDERENGINE
61 if (actualSkiaBackend == SkiaBackend::GRAPHITE) {
62 createInstanceFactory = [args]() {
63 return android::renderengine::skia::GraphiteVkRenderEngine::create(args);
64 };
65 } else
66 #endif
67 { // GANESH
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 };
76 }
77 }
78
79 if (args.threaded == Threaded::YES) {
80 return renderengine::threaded::RenderEngineThreaded::create(createInstanceFactory);
81 } else {
82 return createInstanceFactory();
83 }
84 }
85
86 RenderEngine::~RenderEngine() = default;
87
validateInputBufferUsage(const sp<GraphicBuffer> & buffer)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
validateOutputBufferUsage(const sp<GraphicBuffer> & buffer)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
drawLayers(const DisplaySettings & display,const std::vector<LayerSettings> & layers,const std::shared_ptr<ExternalTexture> & buffer,base::unique_fd && bufferFence)98 ftl::Future<FenceResult> RenderEngine::drawLayers(const DisplaySettings& display,
99 const std::vector<LayerSettings>& layers,
100 const std::shared_ptr<ExternalTexture>& buffer,
101 base::unique_fd&& bufferFence) {
102 const auto resultPromise = std::make_shared<std::promise<FenceResult>>();
103 std::future<FenceResult> resultFuture = resultPromise->get_future();
104 updateProtectedContext(layers, buffer);
105 drawLayersInternal(std::move(resultPromise), display, layers, buffer, std::move(bufferFence));
106 return resultFuture;
107 }
108
updateProtectedContext(const std::vector<LayerSettings> & layers,const std::shared_ptr<ExternalTexture> & buffer)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
120 } // namespace renderengine
121 } // namespace android
122