blob: 791d4c94b464da9370a6bc083173060a1af0d4cb [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 case RenderEngine::RenderEngineType::GLES:
47 case RenderEngine::RenderEngineType::THREADED:
48 LOG_ALWAYS_FATAL("GLESRenderEngine is deprecated - why time it?");
49 return "unused";
50 }
51}
52
53/**
Leon Scroggins IIIfd37d2a2021-11-03 15:11:48 -040054 * Passed (indirectly - see RunSkiaGLThreaded) to Benchmark::Apply to create a
55 * Benchmark which specifies which RenderEngineType it uses.
Leon Scroggins III8fcfa662021-10-12 11:33:30 -040056 *
57 * This simplifies calling ->Arg(type)->Arg(type) and provides strings to make
58 * it obvious which version is being run.
59 *
60 * @param b The benchmark family
61 * @param type The type of RenderEngine to use.
62 */
63static void AddRenderEngineType(benchmark::internal::Benchmark* b,
64 RenderEngine::RenderEngineType type) {
65 b->Arg(static_cast<int64_t>(type));
66 b->ArgName(RenderEngineTypeName(type));
67}
68
69/**
Leon Scroggins IIIfd37d2a2021-11-03 15:11:48 -040070 * Run a benchmark once using SKIA_GL_THREADED.
Leon Scroggins III8fcfa662021-10-12 11:33:30 -040071 */
Leon Scroggins IIIfd37d2a2021-11-03 15:11:48 -040072static void RunSkiaGLThreaded(benchmark::internal::Benchmark* b) {
73 AddRenderEngineType(b, RenderEngine::RenderEngineType::SKIA_GL_THREADED);
Leon Scroggins III8fcfa662021-10-12 11:33:30 -040074}
75
76///////////////////////////////////////////////////////////////////////////////
77// Helpers for calling drawLayers
78///////////////////////////////////////////////////////////////////////////////
79
80std::pair<uint32_t, uint32_t> getDisplaySize() {
81 // These will be retrieved from a ui::Size, which stores int32_t, but they will be passed
82 // to GraphicBuffer, which wants uint32_t.
83 static uint32_t width, height;
84 std::once_flag once;
85 std::call_once(once, []() {
86 auto surfaceComposerClient = SurfaceComposerClient::getDefault();
Huihong Luo31b5ac22022-08-15 20:38:10 -070087 auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
88 LOG_ALWAYS_FATAL_IF(ids.empty(), "Failed to get any display!");
89 ui::Size resolution = ui::kEmptySize;
90 // find the largest display resolution
91 for (auto id : ids) {
92 auto displayToken = surfaceComposerClient->getPhysicalDisplayToken(id);
93 ui::DisplayMode displayMode;
94 if (surfaceComposerClient->getActiveDisplayMode(displayToken, &displayMode) < 0) {
95 LOG_ALWAYS_FATAL("Failed to get active display mode!");
96 }
97 auto tw = displayMode.resolution.width;
98 auto th = displayMode.resolution.height;
99 LOG_ALWAYS_FATAL_IF(tw <= 0 || th <= 0, "Invalid display size!");
100 if (resolution.width * resolution.height <
101 displayMode.resolution.width * displayMode.resolution.height) {
102 resolution = displayMode.resolution;
103 }
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400104 }
Huihong Luo31b5ac22022-08-15 20:38:10 -0700105 width = static_cast<uint32_t>(resolution.width);
106 height = static_cast<uint32_t>(resolution.height);
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400107 });
108 return std::pair<uint32_t, uint32_t>(width, height);
109}
110
111// This value doesn't matter, as it's not read. TODO(b/199918329): Once we remove
112// GLESRenderEngine we can remove this, too.
113static constexpr const bool kUseFrameBufferCache = false;
114
115static std::unique_ptr<RenderEngine> createRenderEngine(RenderEngine::RenderEngineType type) {
116 auto args = RenderEngineCreationArgs::Builder()
117 .setPixelFormat(static_cast<int>(ui::PixelFormat::RGBA_8888))
118 .setImageCacheSize(1)
119 .setEnableProtectedContext(true)
120 .setPrecacheToneMapperShaderOnly(false)
121 .setSupportsBackgroundBlur(true)
122 .setContextPriority(RenderEngine::ContextPriority::REALTIME)
123 .setRenderEngineType(type)
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400124 .build();
125 return RenderEngine::create(args);
126}
127
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400128static std::shared_ptr<ExternalTexture> allocateBuffer(RenderEngine& re, uint32_t width,
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400129 uint32_t height,
130 uint64_t extraUsageFlags = 0,
131 std::string name = "output") {
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800132 return std::make_shared<
Ady Abrahamd11bade2022-08-01 16:18:03 -0700133 impl::ExternalTexture>(sp<GraphicBuffer>::make(width, height,
134 HAL_PIXEL_FORMAT_RGBA_8888, 1u,
135 GRALLOC_USAGE_HW_RENDER |
136 GRALLOC_USAGE_HW_TEXTURE |
137 extraUsageFlags,
138 std::move(name)),
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800139 re,
140 impl::ExternalTexture::Usage::READABLE |
141 impl::ExternalTexture::Usage::WRITEABLE);
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400142}
143
144static std::shared_ptr<ExternalTexture> copyBuffer(RenderEngine& re,
145 std::shared_ptr<ExternalTexture> original,
146 uint64_t extraUsageFlags, std::string name) {
147 const uint32_t width = original->getBuffer()->getWidth();
148 const uint32_t height = original->getBuffer()->getHeight();
149 auto texture = allocateBuffer(re, width, height, extraUsageFlags, name);
150
151 const Rect displayRect(0, 0, static_cast<int32_t>(width), static_cast<int32_t>(height));
152 DisplaySettings display{
153 .physicalDisplay = displayRect,
154 .clip = displayRect,
155 .maxLuminance = 500,
156 };
157
158 const FloatRect layerRect(0, 0, width, height);
159 LayerSettings layer{
160 .geometry =
161 Geometry{
162 .boundaries = layerRect,
163 },
164 .source =
165 PixelSource{
166 .buffer =
167 Buffer{
168 .buffer = original,
169 },
170 },
171 .alpha = half(1.0f),
172 };
173 auto layers = std::vector<LayerSettings>{layer};
174
Patrick Williams2e9748f2022-08-09 22:48:18 +0000175 sp<Fence> waitFence =
176 re.drawLayers(display, layers, texture, kUseFrameBufferCache, base::unique_fd())
177 .get()
178 .value();
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400179 waitFence->waitForever(LOG_TAG);
180 return texture;
181}
182
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400183/**
184 * Helper for timing calls to drawLayers.
185 *
186 * Caller needs to create RenderEngine and the LayerSettings, and this takes
187 * care of setting up the display, starting and stopping the timer, calling
188 * drawLayers, and saving (if --save is used).
189 *
190 * This times both the CPU and GPU work initiated by drawLayers. All work done
191 * outside of the for loop is excluded from the timing measurements.
192 */
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400193static void benchDrawLayers(RenderEngine& re, const std::vector<LayerSettings>& layers,
194 benchmark::State& benchState, const char* saveFileName) {
195 auto [width, height] = getDisplaySize();
196 auto outputBuffer = allocateBuffer(re, width, height);
197
198 const Rect displayRect(0, 0, static_cast<int32_t>(width), static_cast<int32_t>(height));
199 DisplaySettings display{
200 .physicalDisplay = displayRect,
201 .clip = displayRect,
202 .maxLuminance = 500,
203 };
204
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400205 // This loop starts and stops the timer.
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400206 for (auto _ : benchState) {
Patrick Williams2e9748f2022-08-09 22:48:18 +0000207 sp<Fence> waitFence = re.drawLayers(display, layers, outputBuffer, kUseFrameBufferCache,
208 base::unique_fd())
209 .get()
210 .value();
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400211 waitFence->waitForever(LOG_TAG);
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400212 }
213
214 if (renderenginebench::save() && saveFileName) {
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400215 // Copy to a CPU-accessible buffer so we can encode it.
216 outputBuffer = copyBuffer(re, outputBuffer, GRALLOC_USAGE_SW_READ_OFTEN, "to_encode");
217
218 std::string outFile = base::GetExecutableDirectory();
219 outFile.append("/");
220 outFile.append(saveFileName);
221 outFile.append(".jpg");
222 renderenginebench::encodeToJpeg(outFile.c_str(), outputBuffer->getBuffer());
223 }
224}
225
226///////////////////////////////////////////////////////////////////////////////
227// Benchmarks
228///////////////////////////////////////////////////////////////////////////////
229
230void BM_blur(benchmark::State& benchState) {
231 auto re = createRenderEngine(static_cast<RenderEngine::RenderEngineType>(benchState.range()));
232
233 // Initially use cpu access so we can decode into it with AImageDecoder.
234 auto [width, height] = getDisplaySize();
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400235 auto srcBuffer =
236 allocateBuffer(*re, width, height, GRALLOC_USAGE_SW_WRITE_OFTEN, "decoded_source");
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400237 {
238 std::string srcImage = base::GetExecutableDirectory();
239 srcImage.append("/resources/homescreen.png");
240 renderenginebench::decode(srcImage.c_str(), srcBuffer->getBuffer());
241
242 // Now copy into GPU-only buffer for more realistic timing.
243 srcBuffer = copyBuffer(*re, srcBuffer, 0, "source");
244 }
245
246 const FloatRect layerRect(0, 0, width, height);
247 LayerSettings layer{
248 .geometry =
249 Geometry{
250 .boundaries = layerRect,
251 },
252 .source =
253 PixelSource{
254 .buffer =
255 Buffer{
256 .buffer = srcBuffer,
257 },
258 },
259 .alpha = half(1.0f),
260 };
261 LayerSettings blurLayer{
262 .geometry =
263 Geometry{
264 .boundaries = layerRect,
265 },
266 .alpha = half(1.0f),
267 .skipContentDraw = true,
268 .backgroundBlurRadius = 60,
269 };
270
271 auto layers = std::vector<LayerSettings>{layer, blurLayer};
272 benchDrawLayers(*re, layers, benchState, "blurred");
273}
274
Leon Scroggins IIIfd37d2a2021-11-03 15:11:48 -0400275BENCHMARK(BM_blur)->Apply(RunSkiaGLThreaded);