blob: a7f1df9a9b7cca7781bb85a8a9750ad91cd2fdee [file] [log] [blame]
Leon Scroggins III8fcfa662021-10-12 11:33:30 -04001/*
2 * Copyright 2021 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 <RenderEngineBench.h>
18#include <android-base/file.h>
19#include <benchmark/benchmark.h>
20#include <gui/SurfaceComposerClient.h>
21#include <log/log.h>
22#include <renderengine/ExternalTexture.h>
23#include <renderengine/LayerSettings.h>
24#include <renderengine/RenderEngine.h>
Vishnu Nairdbbe3852022-01-12 20:22:11 -080025#include <renderengine/impl/ExternalTexture.h>
Leon Scroggins III8fcfa662021-10-12 11:33:30 -040026
27#include <mutex>
28
29using namespace android;
30using namespace android::renderengine;
31
32///////////////////////////////////////////////////////////////////////////////
33// Helpers for Benchmark::Apply
34///////////////////////////////////////////////////////////////////////////////
35
36std::string RenderEngineTypeName(RenderEngine::RenderEngineType type) {
37 switch (type) {
38 case RenderEngine::RenderEngineType::SKIA_GL_THREADED:
39 return "skiaglthreaded";
40 case RenderEngine::RenderEngineType::SKIA_GL:
41 return "skiagl";
Ian Elliott1f0911e2022-09-09 16:31:47 -060042 case RenderEngine::RenderEngineType::SKIA_VK:
43 return "skiavk";
44 case RenderEngine::RenderEngineType::SKIA_VK_THREADED:
45 return "skiavkthreaded";
Leon Scroggins III8fcfa662021-10-12 11:33:30 -040046 }
47}
48
49/**
Leon Scroggins IIIfd37d2a2021-11-03 15:11:48 -040050 * Passed (indirectly - see RunSkiaGLThreaded) to Benchmark::Apply to create a
51 * Benchmark which specifies which RenderEngineType it uses.
Leon Scroggins III8fcfa662021-10-12 11:33:30 -040052 *
53 * This simplifies calling ->Arg(type)->Arg(type) and provides strings to make
54 * it obvious which version is being run.
55 *
56 * @param b The benchmark family
57 * @param type The type of RenderEngine to use.
58 */
59static void AddRenderEngineType(benchmark::internal::Benchmark* b,
60 RenderEngine::RenderEngineType type) {
61 b->Arg(static_cast<int64_t>(type));
62 b->ArgName(RenderEngineTypeName(type));
63}
64
65/**
Leon Scroggins IIIfd37d2a2021-11-03 15:11:48 -040066 * Run a benchmark once using SKIA_GL_THREADED.
Leon Scroggins III8fcfa662021-10-12 11:33:30 -040067 */
Leon Scroggins IIIfd37d2a2021-11-03 15:11:48 -040068static void RunSkiaGLThreaded(benchmark::internal::Benchmark* b) {
69 AddRenderEngineType(b, RenderEngine::RenderEngineType::SKIA_GL_THREADED);
Leon Scroggins III8fcfa662021-10-12 11:33:30 -040070}
71
72///////////////////////////////////////////////////////////////////////////////
73// Helpers for calling drawLayers
74///////////////////////////////////////////////////////////////////////////////
75
76std::pair<uint32_t, uint32_t> getDisplaySize() {
77 // These will be retrieved from a ui::Size, which stores int32_t, but they will be passed
78 // to GraphicBuffer, which wants uint32_t.
79 static uint32_t width, height;
80 std::once_flag once;
81 std::call_once(once, []() {
82 auto surfaceComposerClient = SurfaceComposerClient::getDefault();
Huihong Luo31b5ac22022-08-15 20:38:10 -070083 auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
84 LOG_ALWAYS_FATAL_IF(ids.empty(), "Failed to get any display!");
85 ui::Size resolution = ui::kEmptySize;
86 // find the largest display resolution
87 for (auto id : ids) {
88 auto displayToken = surfaceComposerClient->getPhysicalDisplayToken(id);
89 ui::DisplayMode displayMode;
90 if (surfaceComposerClient->getActiveDisplayMode(displayToken, &displayMode) < 0) {
91 LOG_ALWAYS_FATAL("Failed to get active display mode!");
92 }
93 auto tw = displayMode.resolution.width;
94 auto th = displayMode.resolution.height;
95 LOG_ALWAYS_FATAL_IF(tw <= 0 || th <= 0, "Invalid display size!");
96 if (resolution.width * resolution.height <
97 displayMode.resolution.width * displayMode.resolution.height) {
98 resolution = displayMode.resolution;
99 }
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400100 }
Huihong Luo31b5ac22022-08-15 20:38:10 -0700101 width = static_cast<uint32_t>(resolution.width);
102 height = static_cast<uint32_t>(resolution.height);
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400103 });
104 return std::pair<uint32_t, uint32_t>(width, height);
105}
106
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400107static std::unique_ptr<RenderEngine> createRenderEngine(RenderEngine::RenderEngineType type) {
108 auto args = RenderEngineCreationArgs::Builder()
109 .setPixelFormat(static_cast<int>(ui::PixelFormat::RGBA_8888))
110 .setImageCacheSize(1)
111 .setEnableProtectedContext(true)
112 .setPrecacheToneMapperShaderOnly(false)
113 .setSupportsBackgroundBlur(true)
114 .setContextPriority(RenderEngine::ContextPriority::REALTIME)
115 .setRenderEngineType(type)
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400116 .build();
117 return RenderEngine::create(args);
118}
119
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400120static std::shared_ptr<ExternalTexture> allocateBuffer(RenderEngine& re, uint32_t width,
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400121 uint32_t height,
122 uint64_t extraUsageFlags = 0,
123 std::string name = "output") {
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800124 return std::make_shared<
Ady Abrahamd11bade2022-08-01 16:18:03 -0700125 impl::ExternalTexture>(sp<GraphicBuffer>::make(width, height,
126 HAL_PIXEL_FORMAT_RGBA_8888, 1u,
127 GRALLOC_USAGE_HW_RENDER |
128 GRALLOC_USAGE_HW_TEXTURE |
129 extraUsageFlags,
130 std::move(name)),
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800131 re,
132 impl::ExternalTexture::Usage::READABLE |
133 impl::ExternalTexture::Usage::WRITEABLE);
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400134}
135
136static std::shared_ptr<ExternalTexture> copyBuffer(RenderEngine& re,
137 std::shared_ptr<ExternalTexture> original,
138 uint64_t extraUsageFlags, std::string name) {
139 const uint32_t width = original->getBuffer()->getWidth();
140 const uint32_t height = original->getBuffer()->getHeight();
141 auto texture = allocateBuffer(re, width, height, extraUsageFlags, name);
142
143 const Rect displayRect(0, 0, static_cast<int32_t>(width), static_cast<int32_t>(height));
144 DisplaySettings display{
145 .physicalDisplay = displayRect,
146 .clip = displayRect,
147 .maxLuminance = 500,
148 };
149
150 const FloatRect layerRect(0, 0, width, height);
151 LayerSettings layer{
152 .geometry =
153 Geometry{
154 .boundaries = layerRect,
155 },
156 .source =
157 PixelSource{
158 .buffer =
159 Buffer{
160 .buffer = original,
161 },
162 },
163 .alpha = half(1.0f),
164 };
165 auto layers = std::vector<LayerSettings>{layer};
166
Alec Mourif29700f2023-08-17 21:53:31 +0000167 sp<Fence> waitFence = re.drawLayers(display, layers, texture, base::unique_fd()).get().value();
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400168 waitFence->waitForever(LOG_TAG);
169 return texture;
170}
171
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400172/**
173 * Helper for timing calls to drawLayers.
174 *
175 * Caller needs to create RenderEngine and the LayerSettings, and this takes
176 * care of setting up the display, starting and stopping the timer, calling
177 * drawLayers, and saving (if --save is used).
178 *
179 * This times both the CPU and GPU work initiated by drawLayers. All work done
180 * outside of the for loop is excluded from the timing measurements.
181 */
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400182static void benchDrawLayers(RenderEngine& re, const std::vector<LayerSettings>& layers,
183 benchmark::State& benchState, const char* saveFileName) {
184 auto [width, height] = getDisplaySize();
185 auto outputBuffer = allocateBuffer(re, width, height);
186
187 const Rect displayRect(0, 0, static_cast<int32_t>(width), static_cast<int32_t>(height));
188 DisplaySettings display{
189 .physicalDisplay = displayRect,
190 .clip = displayRect,
191 .maxLuminance = 500,
192 };
193
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400194 // This loop starts and stops the timer.
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400195 for (auto _ : benchState) {
Alec Mourif29700f2023-08-17 21:53:31 +0000196 sp<Fence> waitFence =
197 re.drawLayers(display, layers, outputBuffer, base::unique_fd()).get().value();
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400198 waitFence->waitForever(LOG_TAG);
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400199 }
200
201 if (renderenginebench::save() && saveFileName) {
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400202 // Copy to a CPU-accessible buffer so we can encode it.
203 outputBuffer = copyBuffer(re, outputBuffer, GRALLOC_USAGE_SW_READ_OFTEN, "to_encode");
204
205 std::string outFile = base::GetExecutableDirectory();
206 outFile.append("/");
207 outFile.append(saveFileName);
208 outFile.append(".jpg");
209 renderenginebench::encodeToJpeg(outFile.c_str(), outputBuffer->getBuffer());
210 }
211}
212
213///////////////////////////////////////////////////////////////////////////////
214// Benchmarks
215///////////////////////////////////////////////////////////////////////////////
216
217void BM_blur(benchmark::State& benchState) {
218 auto re = createRenderEngine(static_cast<RenderEngine::RenderEngineType>(benchState.range()));
219
220 // Initially use cpu access so we can decode into it with AImageDecoder.
221 auto [width, height] = getDisplaySize();
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400222 auto srcBuffer =
223 allocateBuffer(*re, width, height, GRALLOC_USAGE_SW_WRITE_OFTEN, "decoded_source");
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400224 {
225 std::string srcImage = base::GetExecutableDirectory();
226 srcImage.append("/resources/homescreen.png");
227 renderenginebench::decode(srcImage.c_str(), srcBuffer->getBuffer());
228
229 // Now copy into GPU-only buffer for more realistic timing.
230 srcBuffer = copyBuffer(*re, srcBuffer, 0, "source");
231 }
232
233 const FloatRect layerRect(0, 0, width, height);
234 LayerSettings layer{
235 .geometry =
236 Geometry{
237 .boundaries = layerRect,
238 },
239 .source =
240 PixelSource{
241 .buffer =
242 Buffer{
243 .buffer = srcBuffer,
244 },
245 },
246 .alpha = half(1.0f),
247 };
248 LayerSettings blurLayer{
249 .geometry =
250 Geometry{
251 .boundaries = layerRect,
252 },
253 .alpha = half(1.0f),
254 .skipContentDraw = true,
255 .backgroundBlurRadius = 60,
256 };
257
258 auto layers = std::vector<LayerSettings>{layer, blurLayer};
259 benchDrawLayers(*re, layers, benchState, "blurred");
260}
261
Leon Scroggins IIIfd37d2a2021-11-03 15:11:48 -0400262BENCHMARK(BM_blur)->Apply(RunSkiaGLThreaded);