blob: ead97cf5dfb3d17659228e94463aa35af52723ad [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";
42 case RenderEngine::RenderEngineType::GLES:
43 case RenderEngine::RenderEngineType::THREADED:
44 LOG_ALWAYS_FATAL("GLESRenderEngine is deprecated - why time it?");
45 return "unused";
46 }
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();
83 auto displayToken = surfaceComposerClient->getInternalDisplayToken();
84 ui::DisplayMode displayMode;
85 if (surfaceComposerClient->getActiveDisplayMode(displayToken, &displayMode) < 0) {
86 LOG_ALWAYS_FATAL("Failed to get active display mode!");
87 }
88 auto w = displayMode.resolution.width;
89 auto h = displayMode.resolution.height;
90 LOG_ALWAYS_FATAL_IF(w <= 0 || h <= 0, "Invalid display size!");
91 width = static_cast<uint32_t>(w);
92 height = static_cast<uint32_t>(h);
93 });
94 return std::pair<uint32_t, uint32_t>(width, height);
95}
96
97// This value doesn't matter, as it's not read. TODO(b/199918329): Once we remove
98// GLESRenderEngine we can remove this, too.
99static constexpr const bool kUseFrameBufferCache = false;
100
101static std::unique_ptr<RenderEngine> createRenderEngine(RenderEngine::RenderEngineType type) {
102 auto args = RenderEngineCreationArgs::Builder()
103 .setPixelFormat(static_cast<int>(ui::PixelFormat::RGBA_8888))
104 .setImageCacheSize(1)
105 .setEnableProtectedContext(true)
106 .setPrecacheToneMapperShaderOnly(false)
107 .setSupportsBackgroundBlur(true)
108 .setContextPriority(RenderEngine::ContextPriority::REALTIME)
109 .setRenderEngineType(type)
110 .setUseColorManagerment(true)
111 .build();
112 return RenderEngine::create(args);
113}
114
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400115static std::shared_ptr<ExternalTexture> allocateBuffer(RenderEngine& re, uint32_t width,
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400116 uint32_t height,
117 uint64_t extraUsageFlags = 0,
118 std::string name = "output") {
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800119 return std::make_shared<
120 impl::ExternalTexture>(new GraphicBuffer(width, height, HAL_PIXEL_FORMAT_RGBA_8888, 1,
121 GRALLOC_USAGE_HW_RENDER |
122 GRALLOC_USAGE_HW_TEXTURE |
123 extraUsageFlags,
124 std::move(name)),
125 re,
126 impl::ExternalTexture::Usage::READABLE |
127 impl::ExternalTexture::Usage::WRITEABLE);
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400128}
129
130static std::shared_ptr<ExternalTexture> copyBuffer(RenderEngine& re,
131 std::shared_ptr<ExternalTexture> original,
132 uint64_t extraUsageFlags, std::string name) {
133 const uint32_t width = original->getBuffer()->getWidth();
134 const uint32_t height = original->getBuffer()->getHeight();
135 auto texture = allocateBuffer(re, width, height, extraUsageFlags, name);
136
137 const Rect displayRect(0, 0, static_cast<int32_t>(width), static_cast<int32_t>(height));
138 DisplaySettings display{
139 .physicalDisplay = displayRect,
140 .clip = displayRect,
141 .maxLuminance = 500,
142 };
143
144 const FloatRect layerRect(0, 0, width, height);
145 LayerSettings layer{
146 .geometry =
147 Geometry{
148 .boundaries = layerRect,
149 },
150 .source =
151 PixelSource{
152 .buffer =
153 Buffer{
154 .buffer = original,
155 },
156 },
157 .alpha = half(1.0f),
158 };
159 auto layers = std::vector<LayerSettings>{layer};
160
161 auto [status, drawFence] =
162 re.drawLayers(display, layers, texture, kUseFrameBufferCache, base::unique_fd()).get();
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400163 sp<Fence> waitFence = sp<Fence>::make(std::move(drawFence));
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400164 waitFence->waitForever(LOG_TAG);
165 return texture;
166}
167
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400168/**
169 * Helper for timing calls to drawLayers.
170 *
171 * Caller needs to create RenderEngine and the LayerSettings, and this takes
172 * care of setting up the display, starting and stopping the timer, calling
173 * drawLayers, and saving (if --save is used).
174 *
175 * This times both the CPU and GPU work initiated by drawLayers. All work done
176 * outside of the for loop is excluded from the timing measurements.
177 */
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400178static void benchDrawLayers(RenderEngine& re, const std::vector<LayerSettings>& layers,
179 benchmark::State& benchState, const char* saveFileName) {
180 auto [width, height] = getDisplaySize();
181 auto outputBuffer = allocateBuffer(re, width, height);
182
183 const Rect displayRect(0, 0, static_cast<int32_t>(width), static_cast<int32_t>(height));
184 DisplaySettings display{
185 .physicalDisplay = displayRect,
186 .clip = displayRect,
187 .maxLuminance = 500,
188 };
189
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400190 // This loop starts and stops the timer.
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400191 for (auto _ : benchState) {
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400192 auto [status, drawFence] = re.drawLayers(display, layers, outputBuffer,
193 kUseFrameBufferCache, base::unique_fd())
194 .get();
195 sp<Fence> waitFence = sp<Fence>::make(std::move(drawFence));
196 waitFence->waitForever(LOG_TAG);
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400197 }
198
199 if (renderenginebench::save() && saveFileName) {
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400200 // Copy to a CPU-accessible buffer so we can encode it.
201 outputBuffer = copyBuffer(re, outputBuffer, GRALLOC_USAGE_SW_READ_OFTEN, "to_encode");
202
203 std::string outFile = base::GetExecutableDirectory();
204 outFile.append("/");
205 outFile.append(saveFileName);
206 outFile.append(".jpg");
207 renderenginebench::encodeToJpeg(outFile.c_str(), outputBuffer->getBuffer());
208 }
209}
210
211///////////////////////////////////////////////////////////////////////////////
212// Benchmarks
213///////////////////////////////////////////////////////////////////////////////
214
215void BM_blur(benchmark::State& benchState) {
216 auto re = createRenderEngine(static_cast<RenderEngine::RenderEngineType>(benchState.range()));
217
218 // Initially use cpu access so we can decode into it with AImageDecoder.
219 auto [width, height] = getDisplaySize();
Leon Scroggins IIIb76f17d2021-11-03 15:04:18 -0400220 auto srcBuffer =
221 allocateBuffer(*re, width, height, GRALLOC_USAGE_SW_WRITE_OFTEN, "decoded_source");
Leon Scroggins III8fcfa662021-10-12 11:33:30 -0400222 {
223 std::string srcImage = base::GetExecutableDirectory();
224 srcImage.append("/resources/homescreen.png");
225 renderenginebench::decode(srcImage.c_str(), srcBuffer->getBuffer());
226
227 // Now copy into GPU-only buffer for more realistic timing.
228 srcBuffer = copyBuffer(*re, srcBuffer, 0, "source");
229 }
230
231 const FloatRect layerRect(0, 0, width, height);
232 LayerSettings layer{
233 .geometry =
234 Geometry{
235 .boundaries = layerRect,
236 },
237 .source =
238 PixelSource{
239 .buffer =
240 Buffer{
241 .buffer = srcBuffer,
242 },
243 },
244 .alpha = half(1.0f),
245 };
246 LayerSettings blurLayer{
247 .geometry =
248 Geometry{
249 .boundaries = layerRect,
250 },
251 .alpha = half(1.0f),
252 .skipContentDraw = true,
253 .backgroundBlurRadius = 60,
254 };
255
256 auto layers = std::vector<LayerSettings>{layer, blurLayer};
257 benchDrawLayers(*re, layers, benchState, "blurred");
258}
259
Leon Scroggins IIIfd37d2a2021-11-03 15:11:48 -0400260BENCHMARK(BM_blur)->Apply(RunSkiaGLThreaded);