blob: d795616b48dc3e8d408e02da58ba607183bbc04f [file] [log] [blame]
Alec Mouri6e57f682018-09-29 20:45:08 -07001/*
2 * Copyright 2018 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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Alec Mouri16a99402019-07-29 16:37:30 -070021#include <chrono>
22#include <condition_variable>
Vishnu Nair16efdbf2019-12-10 11:55:42 -080023#include <fstream>
Alec Mouri6e57f682018-09-29 20:45:08 -070024
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080025#include <cutils/properties.h>
Ana Krulec9bc9dc62020-02-26 12:16:40 -080026#include <gtest/gtest.h>
Alec Mouri6e57f682018-09-29 20:45:08 -070027#include <renderengine/RenderEngine.h>
Alec Mouri1089aed2018-10-25 21:33:57 -070028#include <sync/sync.h>
Alec Mouri6e57f682018-09-29 20:45:08 -070029#include <ui/PixelFormat.h>
Alec Mourid43ccab2019-03-13 12:23:45 -070030#include "../gl/GLESRenderEngine.h"
Ana Krulec9bc9dc62020-02-26 12:16:40 -080031#include "../threaded/RenderEngineThreaded.h"
Alec Mouri6e57f682018-09-29 20:45:08 -070032
Alec Mouri1089aed2018-10-25 21:33:57 -070033constexpr int DEFAULT_DISPLAY_WIDTH = 128;
34constexpr int DEFAULT_DISPLAY_HEIGHT = 256;
35constexpr int DEFAULT_DISPLAY_OFFSET = 64;
Vishnu Nair16efdbf2019-12-10 11:55:42 -080036constexpr bool WRITE_BUFFER_TO_FILE_ON_FAILURE = false;
Alec Mouri1089aed2018-10-25 21:33:57 -070037
Alec Mouri6e57f682018-09-29 20:45:08 -070038namespace android {
39
Alec Mouri1089aed2018-10-25 21:33:57 -070040struct RenderEngineTest : public ::testing::Test {
Alec Mourid43ccab2019-03-13 12:23:45 -070041 static void SetUpTestSuite() {
Peiyong Lin4137a1d2019-10-09 10:39:09 -070042 sRE = renderengine::gl::GLESRenderEngine::create(
43 renderengine::RenderEngineCreationArgs::Builder()
Ana Krulec9bc9dc62020-02-26 12:16:40 -080044 .setPixelFormat(static_cast<int>(ui::PixelFormat::RGBA_8888))
45 .setImageCacheSize(1)
46 .setUseColorManagerment(false)
47 .setEnableProtectedContext(false)
48 .setPrecacheToneMapperShaderOnly(false)
49 .setSupportsBackgroundBlur(true)
50 .setContextPriority(renderengine::RenderEngine::ContextPriority::MEDIUM)
51 .setRenderEngineType(renderengine::RenderEngine::RenderEngineType::GLES)
52 .build());
Alec Mourid43ccab2019-03-13 12:23:45 -070053 }
54
55 static void TearDownTestSuite() {
56 // The ordering here is important - sCurrentBuffer must live longer
57 // than RenderEngine to avoid a null reference on tear-down.
58 sRE = nullptr;
59 sCurrentBuffer = nullptr;
60 }
61
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080062 static sp<GraphicBuffer> allocateDefaultBuffer() {
Alec Mouri1089aed2018-10-25 21:33:57 -070063 return new GraphicBuffer(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT,
64 HAL_PIXEL_FORMAT_RGBA_8888, 1,
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080065 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
66 GRALLOC_USAGE_HW_RENDER,
Alec Mouri1089aed2018-10-25 21:33:57 -070067 "output");
Alec Mouri6e57f682018-09-29 20:45:08 -070068 }
69
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080070 // Allocates a 1x1 buffer to fill with a solid color
71 static sp<GraphicBuffer> allocateSourceBuffer(uint32_t width, uint32_t height) {
72 return new GraphicBuffer(width, height, HAL_PIXEL_FORMAT_RGBA_8888, 1,
73 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
74 GRALLOC_USAGE_HW_TEXTURE,
75 "input");
76 }
77
Alec Mouri1089aed2018-10-25 21:33:57 -070078 RenderEngineTest() { mBuffer = allocateDefaultBuffer(); }
79
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080080 ~RenderEngineTest() {
Vishnu Nair16efdbf2019-12-10 11:55:42 -080081 if (WRITE_BUFFER_TO_FILE_ON_FAILURE && ::testing::Test::HasFailure()) {
82 writeBufferToFile("/data/texture_out_");
83 }
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080084 for (uint32_t texName : mTexNames) {
85 sRE->deleteTextures(1, &texName);
Alec Mouri368e1582020-08-13 10:14:29 -070086 EXPECT_FALSE(sRE->isTextureNameKnownForTesting(texName));
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080087 }
88 }
89
Vishnu Nair16efdbf2019-12-10 11:55:42 -080090 void writeBufferToFile(const char* basename) {
91 std::string filename(basename);
92 filename.append(::testing::UnitTest::GetInstance()->current_test_info()->name());
93 filename.append(".ppm");
94 std::ofstream file(filename.c_str(), std::ios::binary);
95 if (!file.is_open()) {
96 ALOGE("Unable to open file: %s", filename.c_str());
97 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
98 "surfaceflinger to write debug images");
99 return;
100 }
101
Alec Mouri1089aed2018-10-25 21:33:57 -0700102 uint8_t* pixels;
103 mBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
104 reinterpret_cast<void**>(&pixels));
105
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800106 file << "P6\n";
107 file << mBuffer->getWidth() << "\n";
108 file << mBuffer->getHeight() << "\n";
109 file << 255 << "\n";
110
111 std::vector<uint8_t> outBuffer(mBuffer->getWidth() * mBuffer->getHeight() * 3);
112 auto outPtr = reinterpret_cast<uint8_t*>(outBuffer.data());
113
114 for (int32_t j = 0; j < mBuffer->getHeight(); j++) {
115 const uint8_t* src = pixels + (mBuffer->getStride() * j) * 4;
116 for (int32_t i = 0; i < mBuffer->getWidth(); i++) {
117 // Only copy R, G and B components
118 outPtr[0] = src[0];
119 outPtr[1] = src[1];
120 outPtr[2] = src[2];
121 outPtr += 3;
122
123 src += 4;
124 }
125 }
126 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
127 mBuffer->unlock();
128 }
129
130 void expectBufferColor(const Region& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
131 size_t c;
132 Rect const* rect = region.getArray(&c);
133 for (size_t i = 0; i < c; i++, rect++) {
134 expectBufferColor(*rect, r, g, b, a);
135 }
136 }
137
138 void expectBufferColor(const Rect& rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a,
139 uint8_t tolerance = 0) {
140 auto colorCompare = [tolerance](const uint8_t* colorA, const uint8_t* colorB) {
141 auto colorBitCompare = [tolerance](uint8_t a, uint8_t b) {
142 uint8_t tmp = a >= b ? a - b : b - a;
143 return tmp <= tolerance;
144 };
145 return std::equal(colorA, colorA + 4, colorB, colorBitCompare);
Alec Mouri1089aed2018-10-25 21:33:57 -0700146 };
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800147
148 expectBufferColor(rect, r, g, b, a, colorCompare);
149 }
150
151 void expectBufferColor(const Rect& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a,
152 std::function<bool(const uint8_t* a, const uint8_t* b)> colorCompare) {
153 uint8_t* pixels;
154 mBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
155 reinterpret_cast<void**>(&pixels));
Alec Mouri1089aed2018-10-25 21:33:57 -0700156 int32_t maxFails = 10;
157 int32_t fails = 0;
158 for (int32_t j = 0; j < region.getHeight(); j++) {
159 const uint8_t* src =
160 pixels + (mBuffer->getStride() * (region.top + j) + region.left) * 4;
161 for (int32_t i = 0; i < region.getWidth(); i++) {
162 const uint8_t expected[4] = {r, g, b, a};
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800163 bool equal = colorCompare(src, expected);
Alec Mouri1089aed2018-10-25 21:33:57 -0700164 EXPECT_TRUE(equal)
165 << "pixel @ (" << region.left + i << ", " << region.top + j << "): "
166 << "expected (" << static_cast<uint32_t>(r) << ", "
167 << static_cast<uint32_t>(g) << ", " << static_cast<uint32_t>(b) << ", "
168 << static_cast<uint32_t>(a) << "), "
169 << "got (" << static_cast<uint32_t>(src[0]) << ", "
170 << static_cast<uint32_t>(src[1]) << ", " << static_cast<uint32_t>(src[2])
171 << ", " << static_cast<uint32_t>(src[3]) << ")";
172 src += 4;
173 if (!equal && ++fails >= maxFails) {
174 break;
175 }
176 }
177 if (fails >= maxFails) {
178 break;
179 }
180 }
181 mBuffer->unlock();
182 }
183
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800184 void expectAlpha(const Rect& rect, uint8_t a) {
185 auto colorCompare = [](const uint8_t* colorA, const uint8_t* colorB) {
186 return colorA[3] == colorB[3];
187 };
188 expectBufferColor(rect, 0.0f /* r */, 0.0f /*g */, 0.0f /* b */, a, colorCompare);
189 }
190
191 void expectShadowColor(const renderengine::LayerSettings& castingLayer,
192 const renderengine::ShadowSettings& shadow, const ubyte4& casterColor,
193 const ubyte4& backgroundColor) {
194 const Rect casterRect(castingLayer.geometry.boundaries);
195 Region casterRegion = Region(casterRect);
196 const float casterCornerRadius = castingLayer.geometry.roundedCornersRadius;
197 if (casterCornerRadius > 0.0f) {
198 // ignore the corners if a corner radius is set
199 Rect cornerRect(casterCornerRadius, casterCornerRadius);
200 casterRegion.subtractSelf(cornerRect.offsetTo(casterRect.left, casterRect.top));
201 casterRegion.subtractSelf(
202 cornerRect.offsetTo(casterRect.right - casterCornerRadius, casterRect.top));
203 casterRegion.subtractSelf(
204 cornerRect.offsetTo(casterRect.left, casterRect.bottom - casterCornerRadius));
205 casterRegion.subtractSelf(cornerRect.offsetTo(casterRect.right - casterCornerRadius,
206 casterRect.bottom - casterCornerRadius));
207 }
208
209 const float shadowInset = shadow.length * -1.0f;
210 const Rect casterWithShadow =
211 Rect(casterRect).inset(shadowInset, shadowInset, shadowInset, shadowInset);
212 const Region shadowRegion = Region(casterWithShadow).subtractSelf(casterRect);
213 const Region backgroundRegion = Region(fullscreenRect()).subtractSelf(casterWithShadow);
214
215 // verify casting layer
216 expectBufferColor(casterRegion, casterColor.r, casterColor.g, casterColor.b, casterColor.a);
217
218 // verify shadows by testing just the alpha since its difficult to validate the shadow color
219 size_t c;
220 Rect const* r = shadowRegion.getArray(&c);
221 for (size_t i = 0; i < c; i++, r++) {
222 expectAlpha(*r, 255);
223 }
224
225 // verify background
226 expectBufferColor(backgroundRegion, backgroundColor.r, backgroundColor.g, backgroundColor.b,
227 backgroundColor.a);
228 }
229
230 static renderengine::ShadowSettings getShadowSettings(const vec2& casterPos, float shadowLength,
231 bool casterIsTranslucent) {
232 renderengine::ShadowSettings shadow;
233 shadow.ambientColor = {0.0f, 0.0f, 0.0f, 0.039f};
234 shadow.spotColor = {0.0f, 0.0f, 0.0f, 0.19f};
235 shadow.lightPos = vec3(casterPos.x, casterPos.y, 0);
236 shadow.lightRadius = 0.0f;
237 shadow.length = shadowLength;
238 shadow.casterIsTranslucent = casterIsTranslucent;
239 return shadow;
240 }
241
Alec Mouri1089aed2018-10-25 21:33:57 -0700242 static Rect fullscreenRect() { return Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT); }
243
244 static Rect offsetRect() {
245 return Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
246 DEFAULT_DISPLAY_HEIGHT);
247 }
248
249 static Rect offsetRectAtZero() {
250 return Rect(DEFAULT_DISPLAY_WIDTH - DEFAULT_DISPLAY_OFFSET,
251 DEFAULT_DISPLAY_HEIGHT - DEFAULT_DISPLAY_OFFSET);
252 }
253
Alec Mourid43ccab2019-03-13 12:23:45 -0700254 void invokeDraw(renderengine::DisplaySettings settings,
Vishnu Nair9b079a22020-01-21 14:36:08 -0800255 std::vector<const renderengine::LayerSettings*> layers,
256 sp<GraphicBuffer> buffer) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700257 base::unique_fd fence;
Ana Krulecfc874ae2020-02-22 15:39:32 -0800258 status_t status =
259 sRE->drawLayers(settings, layers, buffer, true, base::unique_fd(), &fence);
Alec Mourid43ccab2019-03-13 12:23:45 -0700260 sCurrentBuffer = buffer;
Alec Mouri1089aed2018-10-25 21:33:57 -0700261
262 int fd = fence.release();
263 if (fd >= 0) {
264 sync_wait(fd, -1);
265 close(fd);
266 }
267
268 ASSERT_EQ(NO_ERROR, status);
Alec Mourid43ccab2019-03-13 12:23:45 -0700269 if (layers.size() > 0) {
270 ASSERT_TRUE(sRE->isFramebufferImageCachedForTesting(buffer->getId()));
271 }
Alec Mouri1089aed2018-10-25 21:33:57 -0700272 }
273
Alec Mourid43ccab2019-03-13 12:23:45 -0700274 void drawEmptyLayers() {
Alec Mouri6e57f682018-09-29 20:45:08 -0700275 renderengine::DisplaySettings settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800276 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri6e57f682018-09-29 20:45:08 -0700277 // Meaningless buffer since we don't do any drawing
278 sp<GraphicBuffer> buffer = new GraphicBuffer();
Alec Mouri1089aed2018-10-25 21:33:57 -0700279 invokeDraw(settings, layers, buffer);
Alec Mouri6e57f682018-09-29 20:45:08 -0700280 }
281
Alec Mouri1089aed2018-10-25 21:33:57 -0700282 template <typename SourceVariant>
283 void fillBuffer(half r, half g, half b, half a);
284
285 template <typename SourceVariant>
286 void fillRedBuffer();
287
288 template <typename SourceVariant>
289 void fillGreenBuffer();
290
291 template <typename SourceVariant>
292 void fillBlueBuffer();
293
294 template <typename SourceVariant>
295 void fillRedTransparentBuffer();
296
297 template <typename SourceVariant>
298 void fillRedOffsetBuffer();
299
300 template <typename SourceVariant>
301 void fillBufferPhysicalOffset();
302
303 template <typename SourceVariant>
Alec Mouri5a6d8572020-03-23 23:56:15 -0700304 void fillBufferCheckers(uint32_t rotation);
Alec Mouri1089aed2018-10-25 21:33:57 -0700305
306 template <typename SourceVariant>
307 void fillBufferCheckersRotate0();
308
309 template <typename SourceVariant>
310 void fillBufferCheckersRotate90();
311
312 template <typename SourceVariant>
313 void fillBufferCheckersRotate180();
314
315 template <typename SourceVariant>
316 void fillBufferCheckersRotate270();
317
318 template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800319 void fillBufferWithLayerTransform();
320
321 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700322 void fillBufferLayerTransform();
323
324 template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800325 void fillBufferWithColorTransform();
326
327 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700328 void fillBufferColorTransform();
329
Alec Mouri7c94edb2018-12-03 21:23:26 -0800330 template <typename SourceVariant>
331 void fillRedBufferWithRoundedCorners();
332
333 template <typename SourceVariant>
334 void fillBufferWithRoundedCorners();
335
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000336 template <typename SourceVariant>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800337 void fillBufferAndBlurBackground();
338
339 template <typename SourceVariant>
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000340 void overlayCorners();
341
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800342 void fillRedBufferTextureTransform();
343
344 void fillBufferTextureTransform();
345
346 void fillRedBufferWithPremultiplyAlpha();
347
348 void fillBufferWithPremultiplyAlpha();
349
350 void fillRedBufferWithoutPremultiplyAlpha();
351
352 void fillBufferWithoutPremultiplyAlpha();
353
Alec Mouriac335532018-11-12 15:01:33 -0800354 void fillGreenColorBufferThenClearRegion();
355
356 void clearLeftRegion();
357
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000358 void clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -0800359
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800360 template <typename SourceVariant>
361 void drawShadow(const renderengine::LayerSettings& castingLayer,
362 const renderengine::ShadowSettings& shadow, const ubyte4& casterColor,
363 const ubyte4& backgroundColor);
364
Alec Mourid43ccab2019-03-13 12:23:45 -0700365 // Keep around the same renderengine object to save on initialization time.
366 // For now, exercise the GL backend directly so that some caching specifics
367 // can be tested without changing the interface.
368 static std::unique_ptr<renderengine::gl::GLESRenderEngine> sRE;
369 // Dumb hack to avoid NPE in the EGL driver: the GraphicBuffer needs to
370 // be freed *after* RenderEngine is destroyed, so that the EGL image is
371 // destroyed first.
372 static sp<GraphicBuffer> sCurrentBuffer;
Alec Mouri1089aed2018-10-25 21:33:57 -0700373
374 sp<GraphicBuffer> mBuffer;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800375
376 std::vector<uint32_t> mTexNames;
Alec Mouri6e57f682018-09-29 20:45:08 -0700377};
378
Alec Mourid43ccab2019-03-13 12:23:45 -0700379std::unique_ptr<renderengine::gl::GLESRenderEngine> RenderEngineTest::sRE = nullptr;
380sp<GraphicBuffer> RenderEngineTest::sCurrentBuffer = nullptr;
Alec Mouri1089aed2018-10-25 21:33:57 -0700381
382struct ColorSourceVariant {
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800383 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
384 RenderEngineTest* /*fixture*/) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700385 layer.source.solidColor = half3(r, g, b);
386 }
387};
388
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800389struct RelaxOpaqueBufferVariant {
390 static void setOpaqueBit(renderengine::LayerSettings& layer) {
391 layer.source.buffer.isOpaque = false;
392 }
393
394 static uint8_t getAlphaChannel() { return 255; }
395};
396
397struct ForceOpaqueBufferVariant {
398 static void setOpaqueBit(renderengine::LayerSettings& layer) {
399 layer.source.buffer.isOpaque = true;
400 }
401
402 static uint8_t getAlphaChannel() {
403 // The isOpaque bit will override the alpha channel, so this should be
404 // arbitrary.
405 return 10;
406 }
407};
408
409template <typename OpaquenessVariant>
410struct BufferSourceVariant {
411 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
412 RenderEngineTest* fixture) {
413 sp<GraphicBuffer> buf = RenderEngineTest::allocateSourceBuffer(1, 1);
414 uint32_t texName;
Alec Mourid43ccab2019-03-13 12:23:45 -0700415 fixture->sRE->genTextures(1, &texName);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800416 fixture->mTexNames.push_back(texName);
417
418 uint8_t* pixels;
419 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
420 reinterpret_cast<void**>(&pixels));
421
422 for (int32_t j = 0; j < buf->getHeight(); j++) {
423 uint8_t* iter = pixels + (buf->getStride() * j) * 4;
424 for (int32_t i = 0; i < buf->getWidth(); i++) {
425 iter[0] = uint8_t(r * 255);
426 iter[1] = uint8_t(g * 255);
427 iter[2] = uint8_t(b * 255);
428 iter[3] = OpaquenessVariant::getAlphaChannel();
429 iter += 4;
430 }
431 }
432
433 buf->unlock();
434
435 layer.source.buffer.buffer = buf;
436 layer.source.buffer.textureName = texName;
437 OpaquenessVariant::setOpaqueBit(layer);
438 }
439};
440
Alec Mouri1089aed2018-10-25 21:33:57 -0700441template <typename SourceVariant>
442void RenderEngineTest::fillBuffer(half r, half g, half b, half a) {
443 renderengine::DisplaySettings settings;
444 settings.physicalDisplay = fullscreenRect();
445 settings.clip = fullscreenRect();
446
Vishnu Nair9b079a22020-01-21 14:36:08 -0800447 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700448
449 renderengine::LayerSettings layer;
450 layer.geometry.boundaries = fullscreenRect().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800451 SourceVariant::fillColor(layer, r, g, b, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700452 layer.alpha = a;
453
Vishnu Nair9b079a22020-01-21 14:36:08 -0800454 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700455
456 invokeDraw(settings, layers, mBuffer);
457}
458
459template <typename SourceVariant>
460void RenderEngineTest::fillRedBuffer() {
461 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, 1.0f);
462 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
463}
464
465template <typename SourceVariant>
466void RenderEngineTest::fillGreenBuffer() {
467 fillBuffer<SourceVariant>(0.0f, 1.0f, 0.0f, 1.0f);
468 expectBufferColor(fullscreenRect(), 0, 255, 0, 255);
469}
470
471template <typename SourceVariant>
472void RenderEngineTest::fillBlueBuffer() {
473 fillBuffer<SourceVariant>(0.0f, 0.0f, 1.0f, 1.0f);
474 expectBufferColor(fullscreenRect(), 0, 0, 255, 255);
475}
476
477template <typename SourceVariant>
478void RenderEngineTest::fillRedTransparentBuffer() {
479 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, .2f);
480 expectBufferColor(fullscreenRect(), 51, 0, 0, 51);
481}
482
483template <typename SourceVariant>
484void RenderEngineTest::fillRedOffsetBuffer() {
485 renderengine::DisplaySettings settings;
486 settings.physicalDisplay = offsetRect();
487 settings.clip = offsetRectAtZero();
488
Vishnu Nair9b079a22020-01-21 14:36:08 -0800489 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700490
491 renderengine::LayerSettings layer;
492 layer.geometry.boundaries = offsetRectAtZero().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800493 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700494 layer.alpha = 1.0f;
495
Vishnu Nair9b079a22020-01-21 14:36:08 -0800496 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700497 invokeDraw(settings, layers, mBuffer);
498}
499
500template <typename SourceVariant>
501void RenderEngineTest::fillBufferPhysicalOffset() {
502 fillRedOffsetBuffer<SourceVariant>();
503
504 expectBufferColor(Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
505 DEFAULT_DISPLAY_HEIGHT),
506 255, 0, 0, 255);
507 Rect offsetRegionLeft(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_HEIGHT);
508 Rect offsetRegionTop(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_OFFSET);
509
510 expectBufferColor(offsetRegionLeft, 0, 0, 0, 0);
511 expectBufferColor(offsetRegionTop, 0, 0, 0, 0);
512}
513
514template <typename SourceVariant>
Alec Mouri5a6d8572020-03-23 23:56:15 -0700515void RenderEngineTest::fillBufferCheckers(uint32_t orientationFlag) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700516 renderengine::DisplaySettings settings;
517 settings.physicalDisplay = fullscreenRect();
518 // Here logical space is 2x2
519 settings.clip = Rect(2, 2);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700520 settings.orientation = orientationFlag;
Alec Mouri1089aed2018-10-25 21:33:57 -0700521
Vishnu Nair9b079a22020-01-21 14:36:08 -0800522 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700523
524 renderengine::LayerSettings layerOne;
525 Rect rectOne(0, 0, 1, 1);
526 layerOne.geometry.boundaries = rectOne.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800527 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700528 layerOne.alpha = 1.0f;
529
530 renderengine::LayerSettings layerTwo;
531 Rect rectTwo(0, 1, 1, 2);
532 layerTwo.geometry.boundaries = rectTwo.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800533 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700534 layerTwo.alpha = 1.0f;
535
536 renderengine::LayerSettings layerThree;
537 Rect rectThree(1, 0, 2, 1);
538 layerThree.geometry.boundaries = rectThree.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800539 SourceVariant::fillColor(layerThree, 0.0f, 0.0f, 1.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700540 layerThree.alpha = 1.0f;
541
Vishnu Nair9b079a22020-01-21 14:36:08 -0800542 layers.push_back(&layerOne);
543 layers.push_back(&layerTwo);
544 layers.push_back(&layerThree);
Alec Mouri1089aed2018-10-25 21:33:57 -0700545
546 invokeDraw(settings, layers, mBuffer);
547}
548
549template <typename SourceVariant>
550void RenderEngineTest::fillBufferCheckersRotate0() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700551 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_0);
Alec Mouri1089aed2018-10-25 21:33:57 -0700552 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 255, 0, 0,
553 255);
554 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
555 DEFAULT_DISPLAY_HEIGHT / 2),
556 0, 0, 255, 255);
557 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
558 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
559 0, 0, 0, 0);
560 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
561 DEFAULT_DISPLAY_HEIGHT),
562 0, 255, 0, 255);
563}
564
565template <typename SourceVariant>
566void RenderEngineTest::fillBufferCheckersRotate90() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700567 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_90);
Alec Mouri1089aed2018-10-25 21:33:57 -0700568 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 255, 0,
569 255);
570 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
571 DEFAULT_DISPLAY_HEIGHT / 2),
572 255, 0, 0, 255);
573 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
574 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
575 0, 0, 255, 255);
576 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
577 DEFAULT_DISPLAY_HEIGHT),
578 0, 0, 0, 0);
579}
580
581template <typename SourceVariant>
582void RenderEngineTest::fillBufferCheckersRotate180() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700583 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_180);
Alec Mouri1089aed2018-10-25 21:33:57 -0700584 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0,
585 0);
586 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
587 DEFAULT_DISPLAY_HEIGHT / 2),
588 0, 255, 0, 255);
589 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
590 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
591 255, 0, 0, 255);
592 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
593 DEFAULT_DISPLAY_HEIGHT),
594 0, 0, 255, 255);
595}
596
597template <typename SourceVariant>
598void RenderEngineTest::fillBufferCheckersRotate270() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700599 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_270);
Alec Mouri1089aed2018-10-25 21:33:57 -0700600 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 255,
601 255);
602 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
603 DEFAULT_DISPLAY_HEIGHT / 2),
604 0, 0, 0, 0);
605 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
606 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
607 0, 255, 0, 255);
608 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
609 DEFAULT_DISPLAY_HEIGHT),
610 255, 0, 0, 255);
611}
612
613template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800614void RenderEngineTest::fillBufferWithLayerTransform() {
Alec Mouri1089aed2018-10-25 21:33:57 -0700615 renderengine::DisplaySettings settings;
616 settings.physicalDisplay = fullscreenRect();
617 // Here logical space is 2x2
618 settings.clip = Rect(2, 2);
619
Vishnu Nair9b079a22020-01-21 14:36:08 -0800620 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700621
622 renderengine::LayerSettings layer;
623 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
624 // Translate one pixel diagonally
625 layer.geometry.positionTransform = mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800626 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700627 layer.source.solidColor = half3(1.0f, 0.0f, 0.0f);
628 layer.alpha = 1.0f;
629
Vishnu Nair9b079a22020-01-21 14:36:08 -0800630 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700631
632 invokeDraw(settings, layers, mBuffer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800633}
Alec Mouri1089aed2018-10-25 21:33:57 -0700634
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800635template <typename SourceVariant>
636void RenderEngineTest::fillBufferLayerTransform() {
637 fillBufferWithLayerTransform<SourceVariant>();
Alec Mouri1089aed2018-10-25 21:33:57 -0700638 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0, 0);
639 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 0);
640 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
641 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
642 255, 0, 0, 255);
643}
644
645template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800646void RenderEngineTest::fillBufferWithColorTransform() {
Alec Mouri1089aed2018-10-25 21:33:57 -0700647 renderengine::DisplaySettings settings;
648 settings.physicalDisplay = fullscreenRect();
649 settings.clip = Rect(1, 1);
650
Vishnu Nair9b079a22020-01-21 14:36:08 -0800651 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700652
653 renderengine::LayerSettings layer;
654 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800655 SourceVariant::fillColor(layer, 0.5f, 0.25f, 0.125f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700656 layer.alpha = 1.0f;
657
658 // construct a fake color matrix
659 // annihilate green and blue channels
660 settings.colorTransform = mat4::scale(vec4(1, 0, 0, 1));
661 // set red channel to red + green
662 layer.colorTransform = mat4(1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
663
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800664 layer.alpha = 1.0f;
665 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
666
Vishnu Nair9b079a22020-01-21 14:36:08 -0800667 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700668
669 invokeDraw(settings, layers, mBuffer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800670}
Alec Mouri1089aed2018-10-25 21:33:57 -0700671
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800672template <typename SourceVariant>
673void RenderEngineTest::fillBufferColorTransform() {
674 fillBufferWithColorTransform<SourceVariant>();
Alec Mouri1089aed2018-10-25 21:33:57 -0700675 expectBufferColor(fullscreenRect(), 191, 0, 0, 255);
676}
677
Alec Mouri7c94edb2018-12-03 21:23:26 -0800678template <typename SourceVariant>
679void RenderEngineTest::fillRedBufferWithRoundedCorners() {
680 renderengine::DisplaySettings settings;
681 settings.physicalDisplay = fullscreenRect();
682 settings.clip = fullscreenRect();
683
Vishnu Nair9b079a22020-01-21 14:36:08 -0800684 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri7c94edb2018-12-03 21:23:26 -0800685
686 renderengine::LayerSettings layer;
687 layer.geometry.boundaries = fullscreenRect().toFloatRect();
688 layer.geometry.roundedCornersRadius = 5.0f;
689 layer.geometry.roundedCornersCrop = fullscreenRect().toFloatRect();
690 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
691 layer.alpha = 1.0f;
692
Vishnu Nair9b079a22020-01-21 14:36:08 -0800693 layers.push_back(&layer);
Alec Mouri7c94edb2018-12-03 21:23:26 -0800694
695 invokeDraw(settings, layers, mBuffer);
696}
697
698template <typename SourceVariant>
699void RenderEngineTest::fillBufferWithRoundedCorners() {
700 fillRedBufferWithRoundedCorners<SourceVariant>();
701 // Corners should be ignored...
702 expectBufferColor(Rect(0, 0, 1, 1), 0, 0, 0, 0);
703 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH - 1, 0, DEFAULT_DISPLAY_WIDTH, 1), 0, 0, 0, 0);
704 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT - 1, 1, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 0);
705 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH - 1, DEFAULT_DISPLAY_HEIGHT - 1,
706 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
707 0, 0, 0, 0);
708 // ...And the non-rounded portion should be red.
709 // Other pixels may be anti-aliased, so let's not check those.
710 expectBufferColor(Rect(5, 5, DEFAULT_DISPLAY_WIDTH - 5, DEFAULT_DISPLAY_HEIGHT - 5), 255, 0, 0,
711 255);
712}
713
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000714template <typename SourceVariant>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800715void RenderEngineTest::fillBufferAndBlurBackground() {
716 char value[PROPERTY_VALUE_MAX];
717 property_get("ro.surface_flinger.supports_background_blur", value, "0");
718 if (!atoi(value)) {
719 // This device doesn't support blurs, no-op.
720 return;
721 }
722
723 auto blurRadius = 50;
724 auto center = DEFAULT_DISPLAY_WIDTH / 2;
725
726 renderengine::DisplaySettings settings;
727 settings.physicalDisplay = fullscreenRect();
728 settings.clip = fullscreenRect();
729
Vishnu Nair9b079a22020-01-21 14:36:08 -0800730 std::vector<const renderengine::LayerSettings*> layers;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800731
732 renderengine::LayerSettings backgroundLayer;
733 backgroundLayer.geometry.boundaries = fullscreenRect().toFloatRect();
734 SourceVariant::fillColor(backgroundLayer, 0.0f, 1.0f, 0.0f, this);
735 backgroundLayer.alpha = 1.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800736 layers.push_back(&backgroundLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800737
738 renderengine::LayerSettings leftLayer;
739 leftLayer.geometry.boundaries =
740 Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT).toFloatRect();
741 SourceVariant::fillColor(leftLayer, 1.0f, 0.0f, 0.0f, this);
742 leftLayer.alpha = 1.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800743 layers.push_back(&leftLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800744
745 renderengine::LayerSettings blurLayer;
746 blurLayer.geometry.boundaries = fullscreenRect().toFloatRect();
747 blurLayer.backgroundBlurRadius = blurRadius;
748 blurLayer.alpha = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800749 layers.push_back(&blurLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800750
751 invokeDraw(settings, layers, mBuffer);
752
753 expectBufferColor(Rect(center - 1, center - 5, center, center + 5), 150, 150, 0, 255,
754 50 /* tolerance */);
755 expectBufferColor(Rect(center, center - 5, center + 1, center + 5), 150, 150, 0, 255,
756 50 /* tolerance */);
757}
758
759template <typename SourceVariant>
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000760void RenderEngineTest::overlayCorners() {
761 renderengine::DisplaySettings settings;
762 settings.physicalDisplay = fullscreenRect();
763 settings.clip = fullscreenRect();
764
Vishnu Nair9b079a22020-01-21 14:36:08 -0800765 std::vector<const renderengine::LayerSettings*> layersFirst;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000766
767 renderengine::LayerSettings layerOne;
768 layerOne.geometry.boundaries =
769 FloatRect(0, 0, DEFAULT_DISPLAY_WIDTH / 3.0, DEFAULT_DISPLAY_HEIGHT / 3.0);
770 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
771 layerOne.alpha = 0.2;
772
Vishnu Nair9b079a22020-01-21 14:36:08 -0800773 layersFirst.push_back(&layerOne);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000774 invokeDraw(settings, layersFirst, mBuffer);
775 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3, DEFAULT_DISPLAY_HEIGHT / 3), 51, 0, 0, 51);
776 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3 + 1, DEFAULT_DISPLAY_HEIGHT / 3 + 1,
777 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
778 0, 0, 0, 0);
779
Vishnu Nair9b079a22020-01-21 14:36:08 -0800780 std::vector<const renderengine::LayerSettings*> layersSecond;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000781 renderengine::LayerSettings layerTwo;
782 layerTwo.geometry.boundaries =
783 FloatRect(DEFAULT_DISPLAY_WIDTH / 3.0, DEFAULT_DISPLAY_HEIGHT / 3.0,
784 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT);
785 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
786 layerTwo.alpha = 1.0f;
787
Vishnu Nair9b079a22020-01-21 14:36:08 -0800788 layersSecond.push_back(&layerTwo);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000789 invokeDraw(settings, layersSecond, mBuffer);
790
791 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3, DEFAULT_DISPLAY_HEIGHT / 3), 0, 0, 0, 0);
792 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3 + 1, DEFAULT_DISPLAY_HEIGHT / 3 + 1,
793 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
794 0, 255, 0, 255);
795}
796
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800797void RenderEngineTest::fillRedBufferTextureTransform() {
798 renderengine::DisplaySettings settings;
799 settings.physicalDisplay = fullscreenRect();
800 settings.clip = Rect(1, 1);
801
Vishnu Nair9b079a22020-01-21 14:36:08 -0800802 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800803
804 renderengine::LayerSettings layer;
805 // Here will allocate a checker board texture, but transform texture
806 // coordinates so that only the upper left is applied.
807 sp<GraphicBuffer> buf = allocateSourceBuffer(2, 2);
808 uint32_t texName;
809 RenderEngineTest::sRE->genTextures(1, &texName);
810 this->mTexNames.push_back(texName);
811
812 uint8_t* pixels;
813 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
814 reinterpret_cast<void**>(&pixels));
815 // Red top left, Green top right, Blue bottom left, Black bottom right
816 pixels[0] = 255;
817 pixels[1] = 0;
818 pixels[2] = 0;
819 pixels[3] = 255;
820 pixels[4] = 0;
821 pixels[5] = 255;
822 pixels[6] = 0;
823 pixels[7] = 255;
824 pixels[8] = 0;
825 pixels[9] = 0;
826 pixels[10] = 255;
827 pixels[11] = 255;
828 buf->unlock();
829
830 layer.source.buffer.buffer = buf;
831 layer.source.buffer.textureName = texName;
832 // Transform coordinates to only be inside the red quadrant.
833 layer.source.buffer.textureTransform = mat4::scale(vec4(0.2, 0.2, 1, 1));
834 layer.alpha = 1.0f;
835 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
836
Vishnu Nair9b079a22020-01-21 14:36:08 -0800837 layers.push_back(&layer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800838
839 invokeDraw(settings, layers, mBuffer);
840}
841
842void RenderEngineTest::fillBufferTextureTransform() {
843 fillRedBufferTextureTransform();
844 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
845}
846
847void RenderEngineTest::fillRedBufferWithPremultiplyAlpha() {
848 renderengine::DisplaySettings settings;
849 settings.physicalDisplay = fullscreenRect();
850 // Here logical space is 1x1
851 settings.clip = Rect(1, 1);
852
Vishnu Nair9b079a22020-01-21 14:36:08 -0800853 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800854
855 renderengine::LayerSettings layer;
856 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
857 uint32_t texName;
858 RenderEngineTest::sRE->genTextures(1, &texName);
859 this->mTexNames.push_back(texName);
860
861 uint8_t* pixels;
862 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
863 reinterpret_cast<void**>(&pixels));
864 pixels[0] = 255;
865 pixels[1] = 0;
866 pixels[2] = 0;
867 pixels[3] = 255;
868 buf->unlock();
869
870 layer.source.buffer.buffer = buf;
871 layer.source.buffer.textureName = texName;
872 layer.source.buffer.usePremultipliedAlpha = true;
873 layer.alpha = 0.5f;
874 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
875
Vishnu Nair9b079a22020-01-21 14:36:08 -0800876 layers.push_back(&layer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800877
878 invokeDraw(settings, layers, mBuffer);
879}
880
881void RenderEngineTest::fillBufferWithPremultiplyAlpha() {
882 fillRedBufferWithPremultiplyAlpha();
883 expectBufferColor(fullscreenRect(), 128, 0, 0, 128);
884}
885
886void RenderEngineTest::fillRedBufferWithoutPremultiplyAlpha() {
887 renderengine::DisplaySettings settings;
888 settings.physicalDisplay = fullscreenRect();
889 // Here logical space is 1x1
890 settings.clip = Rect(1, 1);
891
Vishnu Nair9b079a22020-01-21 14:36:08 -0800892 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800893
894 renderengine::LayerSettings layer;
895 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
896 uint32_t texName;
897 RenderEngineTest::sRE->genTextures(1, &texName);
898 this->mTexNames.push_back(texName);
899
900 uint8_t* pixels;
901 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
902 reinterpret_cast<void**>(&pixels));
903 pixels[0] = 255;
904 pixels[1] = 0;
905 pixels[2] = 0;
906 pixels[3] = 255;
907 buf->unlock();
908
909 layer.source.buffer.buffer = buf;
910 layer.source.buffer.textureName = texName;
911 layer.source.buffer.usePremultipliedAlpha = false;
912 layer.alpha = 0.5f;
913 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
914
Vishnu Nair9b079a22020-01-21 14:36:08 -0800915 layers.push_back(&layer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800916
917 invokeDraw(settings, layers, mBuffer);
918}
919
920void RenderEngineTest::fillBufferWithoutPremultiplyAlpha() {
921 fillRedBufferWithoutPremultiplyAlpha();
wukui16f3c0bb2020-08-05 20:35:29 +0800922 expectBufferColor(fullscreenRect(), 128, 0, 0, 128, 1);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800923}
924
Alec Mouriac335532018-11-12 15:01:33 -0800925void RenderEngineTest::clearLeftRegion() {
926 renderengine::DisplaySettings settings;
927 settings.physicalDisplay = fullscreenRect();
928 // Here logical space is 4x4
929 settings.clip = Rect(4, 4);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700930 settings.clearRegion = Region(Rect(2, 4));
Vishnu Nair9b079a22020-01-21 14:36:08 -0800931 std::vector<const renderengine::LayerSettings*> layers;
Peiyong Lind8460c82020-07-28 16:04:22 -0700932 // fake layer, without bounds should not render anything
Alec Mouriac335532018-11-12 15:01:33 -0800933 renderengine::LayerSettings layer;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800934 layers.push_back(&layer);
Alec Mouriac335532018-11-12 15:01:33 -0800935 invokeDraw(settings, layers, mBuffer);
936}
937
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000938void RenderEngineTest::clearRegion() {
Alec Mouriac335532018-11-12 15:01:33 -0800939 // Reuse mBuffer
940 clearLeftRegion();
941 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 255);
942 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
943 DEFAULT_DISPLAY_HEIGHT),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000944 0, 0, 0, 0);
Alec Mouriac335532018-11-12 15:01:33 -0800945}
946
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800947template <typename SourceVariant>
948void RenderEngineTest::drawShadow(const renderengine::LayerSettings& castingLayer,
949 const renderengine::ShadowSettings& shadow,
950 const ubyte4& casterColor, const ubyte4& backgroundColor) {
951 renderengine::DisplaySettings settings;
952 settings.physicalDisplay = fullscreenRect();
953 settings.clip = fullscreenRect();
954
Vishnu Nair9b079a22020-01-21 14:36:08 -0800955 std::vector<const renderengine::LayerSettings*> layers;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800956
957 // add background layer
958 renderengine::LayerSettings bgLayer;
959 bgLayer.geometry.boundaries = fullscreenRect().toFloatRect();
960 ColorSourceVariant::fillColor(bgLayer, backgroundColor.r / 255.0f, backgroundColor.g / 255.0f,
961 backgroundColor.b / 255.0f, this);
962 bgLayer.alpha = backgroundColor.a / 255.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800963 layers.push_back(&bgLayer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800964
965 // add shadow layer
966 renderengine::LayerSettings shadowLayer;
967 shadowLayer.geometry.boundaries = castingLayer.geometry.boundaries;
968 shadowLayer.alpha = castingLayer.alpha;
969 shadowLayer.shadow = shadow;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800970 layers.push_back(&shadowLayer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800971
972 // add layer casting the shadow
973 renderengine::LayerSettings layer = castingLayer;
974 SourceVariant::fillColor(layer, casterColor.r / 255.0f, casterColor.g / 255.0f,
975 casterColor.b / 255.0f, this);
Vishnu Nair9b079a22020-01-21 14:36:08 -0800976 layers.push_back(&layer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800977
978 invokeDraw(settings, layers, mBuffer);
979}
980
Alec Mouri1089aed2018-10-25 21:33:57 -0700981TEST_F(RenderEngineTest, drawLayers_noLayersToDraw) {
982 drawEmptyLayers();
983}
984
Alec Mourid43ccab2019-03-13 12:23:45 -0700985TEST_F(RenderEngineTest, drawLayers_nullOutputBuffer) {
986 renderengine::DisplaySettings settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800987 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -0700988 renderengine::LayerSettings layer;
989 layer.geometry.boundaries = fullscreenRect().toFloatRect();
990 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Vishnu Nair9b079a22020-01-21 14:36:08 -0800991 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -0700992 base::unique_fd fence;
Alec Mourife0d72b2019-03-21 14:05:56 -0700993 status_t status = sRE->drawLayers(settings, layers, nullptr, true, base::unique_fd(), &fence);
Alec Mourid43ccab2019-03-13 12:23:45 -0700994
995 ASSERT_EQ(BAD_VALUE, status);
996}
997
998TEST_F(RenderEngineTest, drawLayers_nullOutputFence) {
999 renderengine::DisplaySettings settings;
1000 settings.physicalDisplay = fullscreenRect();
1001 settings.clip = fullscreenRect();
1002
Vishnu Nair9b079a22020-01-21 14:36:08 -08001003 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001004 renderengine::LayerSettings layer;
1005 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1006 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1007 layer.alpha = 1.0;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001008 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001009
Ana Krulecfc874ae2020-02-22 15:39:32 -08001010 status_t status = sRE->drawLayers(settings, layers, mBuffer, true, base::unique_fd(), nullptr);
Alec Mourid43ccab2019-03-13 12:23:45 -07001011 sCurrentBuffer = mBuffer;
1012 ASSERT_EQ(NO_ERROR, status);
1013 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
1014}
1015
Alec Mourife0d72b2019-03-21 14:05:56 -07001016TEST_F(RenderEngineTest, drawLayers_doesNotCacheFramebuffer) {
1017 renderengine::DisplaySettings settings;
1018 settings.physicalDisplay = fullscreenRect();
1019 settings.clip = fullscreenRect();
1020
Vishnu Nair9b079a22020-01-21 14:36:08 -08001021 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourife0d72b2019-03-21 14:05:56 -07001022 renderengine::LayerSettings layer;
1023 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1024 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1025 layer.alpha = 1.0;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001026 layers.push_back(&layer);
Alec Mourife0d72b2019-03-21 14:05:56 -07001027
Ana Krulecfc874ae2020-02-22 15:39:32 -08001028 status_t status = sRE->drawLayers(settings, layers, mBuffer, false, base::unique_fd(), nullptr);
Alec Mourife0d72b2019-03-21 14:05:56 -07001029 sCurrentBuffer = mBuffer;
1030 ASSERT_EQ(NO_ERROR, status);
1031 ASSERT_FALSE(sRE->isFramebufferImageCachedForTesting(mBuffer->getId()));
1032 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
1033}
1034
Alec Mouri1089aed2018-10-25 21:33:57 -07001035TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_colorSource) {
1036 fillRedBuffer<ColorSourceVariant>();
1037}
1038
1039TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_colorSource) {
1040 fillGreenBuffer<ColorSourceVariant>();
1041}
1042
1043TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_colorSource) {
1044 fillBlueBuffer<ColorSourceVariant>();
1045}
1046
1047TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_colorSource) {
1048 fillRedTransparentBuffer<ColorSourceVariant>();
1049}
1050
1051TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_colorSource) {
1052 fillBufferPhysicalOffset<ColorSourceVariant>();
1053}
1054
1055TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_colorSource) {
1056 fillBufferCheckersRotate0<ColorSourceVariant>();
1057}
1058
1059TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_colorSource) {
1060 fillBufferCheckersRotate90<ColorSourceVariant>();
1061}
1062
1063TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_colorSource) {
1064 fillBufferCheckersRotate180<ColorSourceVariant>();
1065}
1066
1067TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_colorSource) {
1068 fillBufferCheckersRotate270<ColorSourceVariant>();
1069}
1070
1071TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_colorSource) {
1072 fillBufferLayerTransform<ColorSourceVariant>();
1073}
1074
1075TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_colorSource) {
1076 fillBufferLayerTransform<ColorSourceVariant>();
Alec Mouri6e57f682018-09-29 20:45:08 -07001077}
1078
Alec Mouri7c94edb2018-12-03 21:23:26 -08001079TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_colorSource) {
1080 fillBufferWithRoundedCorners<ColorSourceVariant>();
1081}
1082
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001083TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_colorSource) {
1084 fillBufferAndBlurBackground<ColorSourceVariant>();
1085}
1086
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001087TEST_F(RenderEngineTest, drawLayers_overlayCorners_colorSource) {
1088 overlayCorners<ColorSourceVariant>();
1089}
1090
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001091TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_opaqueBufferSource) {
1092 fillRedBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1093}
1094
1095TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_opaqueBufferSource) {
1096 fillGreenBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1097}
1098
1099TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_opaqueBufferSource) {
1100 fillBlueBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1101}
1102
1103TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_opaqueBufferSource) {
1104 fillRedTransparentBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1105}
1106
1107TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_opaqueBufferSource) {
1108 fillBufferPhysicalOffset<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1109}
1110
1111TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_opaqueBufferSource) {
1112 fillBufferCheckersRotate0<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1113}
1114
1115TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_opaqueBufferSource) {
1116 fillBufferCheckersRotate90<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1117}
1118
1119TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_opaqueBufferSource) {
1120 fillBufferCheckersRotate180<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1121}
1122
1123TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_opaqueBufferSource) {
1124 fillBufferCheckersRotate270<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1125}
1126
1127TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_opaqueBufferSource) {
1128 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1129}
1130
1131TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_opaqueBufferSource) {
1132 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1133}
1134
Alec Mouri7c94edb2018-12-03 21:23:26 -08001135TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_opaqueBufferSource) {
1136 fillBufferWithRoundedCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1137}
1138
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001139TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_opaqueBufferSource) {
1140 fillBufferAndBlurBackground<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1141}
1142
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001143TEST_F(RenderEngineTest, drawLayers_overlayCorners_opaqueBufferSource) {
1144 overlayCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1145}
1146
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001147TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_bufferSource) {
1148 fillRedBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1149}
1150
1151TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_bufferSource) {
1152 fillGreenBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1153}
1154
1155TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_bufferSource) {
1156 fillBlueBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1157}
1158
1159TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_bufferSource) {
1160 fillRedTransparentBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1161}
1162
1163TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_bufferSource) {
1164 fillBufferPhysicalOffset<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1165}
1166
1167TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_bufferSource) {
1168 fillBufferCheckersRotate0<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1169}
1170
1171TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_bufferSource) {
1172 fillBufferCheckersRotate90<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1173}
1174
1175TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_bufferSource) {
1176 fillBufferCheckersRotate180<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1177}
1178
1179TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_bufferSource) {
1180 fillBufferCheckersRotate270<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1181}
1182
1183TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_bufferSource) {
1184 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1185}
1186
1187TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_bufferSource) {
1188 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1189}
1190
Alec Mouri7c94edb2018-12-03 21:23:26 -08001191TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_bufferSource) {
1192 fillBufferWithRoundedCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1193}
1194
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001195TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_bufferSource) {
1196 fillBufferAndBlurBackground<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1197}
1198
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001199TEST_F(RenderEngineTest, drawLayers_overlayCorners_bufferSource) {
1200 overlayCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1201}
1202
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001203TEST_F(RenderEngineTest, drawLayers_fillBufferTextureTransform) {
1204 fillBufferTextureTransform();
1205}
1206
1207TEST_F(RenderEngineTest, drawLayers_fillBuffer_premultipliesAlpha) {
1208 fillBufferWithPremultiplyAlpha();
1209}
1210
1211TEST_F(RenderEngineTest, drawLayers_fillBuffer_withoutPremultiplyingAlpha) {
1212 fillBufferWithoutPremultiplyAlpha();
1213}
1214
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001215TEST_F(RenderEngineTest, drawLayers_clearRegion) {
1216 clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -08001217}
1218
Alec Mourid43ccab2019-03-13 12:23:45 -07001219TEST_F(RenderEngineTest, drawLayers_fillsBufferAndCachesImages) {
1220 renderengine::DisplaySettings settings;
1221 settings.physicalDisplay = fullscreenRect();
1222 settings.clip = fullscreenRect();
1223
Vishnu Nair9b079a22020-01-21 14:36:08 -08001224 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001225
1226 renderengine::LayerSettings layer;
1227 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1228 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1229
Vishnu Nair9b079a22020-01-21 14:36:08 -08001230 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001231 invokeDraw(settings, layers, mBuffer);
1232 uint64_t bufferId = layer.source.buffer.buffer->getId();
1233 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001234 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1235 sRE->unbindExternalTextureBufferForTesting(bufferId);
1236 std::lock_guard<std::mutex> lock(barrier->mutex);
1237 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1238 [&]() REQUIRES(barrier->mutex) {
1239 return barrier->isOpen;
1240 }));
Alec Mourid43ccab2019-03-13 12:23:45 -07001241 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001242 EXPECT_EQ(NO_ERROR, barrier->result);
Alec Mourid43ccab2019-03-13 12:23:45 -07001243}
1244
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001245TEST_F(RenderEngineTest, cacheExternalBuffer_withNullBuffer) {
Alec Mouri16a99402019-07-29 16:37:30 -07001246 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1247 sRE->cacheExternalTextureBufferForTesting(nullptr);
1248 std::lock_guard<std::mutex> lock(barrier->mutex);
1249 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1250 [&]() REQUIRES(barrier->mutex) {
1251 return barrier->isOpen;
1252 }));
1253 EXPECT_TRUE(barrier->isOpen);
1254 EXPECT_EQ(BAD_VALUE, barrier->result);
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001255}
1256
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001257TEST_F(RenderEngineTest, cacheExternalBuffer_cachesImages) {
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001258 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
1259 uint64_t bufferId = buf->getId();
Alec Mouri16a99402019-07-29 16:37:30 -07001260 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1261 sRE->cacheExternalTextureBufferForTesting(buf);
1262 {
1263 std::lock_guard<std::mutex> lock(barrier->mutex);
1264 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1265 [&]() REQUIRES(barrier->mutex) {
1266 return barrier->isOpen;
1267 }));
1268 EXPECT_EQ(NO_ERROR, barrier->result);
1269 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001270 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001271 barrier = sRE->unbindExternalTextureBufferForTesting(bufferId);
1272 {
1273 std::lock_guard<std::mutex> lock(barrier->mutex);
1274 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1275 [&]() REQUIRES(barrier->mutex) {
1276 return barrier->isOpen;
1277 }));
1278 EXPECT_EQ(NO_ERROR, barrier->result);
1279 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001280 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1281}
1282
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001283TEST_F(RenderEngineTest, drawLayers_fillShadow_casterLayerMinSize) {
1284 const ubyte4 casterColor(255, 0, 0, 255);
1285 const ubyte4 backgroundColor(255, 255, 255, 255);
1286 const float shadowLength = 5.0f;
1287 Rect casterBounds(1, 1);
1288 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1289 renderengine::LayerSettings castingLayer;
1290 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1291 castingLayer.alpha = 1.0f;
1292 renderengine::ShadowSettings settings =
1293 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1294 false /* casterIsTranslucent */);
1295
1296 drawShadow<ColorSourceVariant>(castingLayer, settings, casterColor, backgroundColor);
1297 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1298}
1299
1300TEST_F(RenderEngineTest, drawLayers_fillShadow_casterColorLayer) {
1301 const ubyte4 casterColor(255, 0, 0, 255);
1302 const ubyte4 backgroundColor(255, 255, 255, 255);
1303 const float shadowLength = 5.0f;
1304 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1305 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1306 renderengine::LayerSettings castingLayer;
1307 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1308 castingLayer.alpha = 1.0f;
1309 renderengine::ShadowSettings settings =
1310 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1311 false /* casterIsTranslucent */);
1312
1313 drawShadow<ColorSourceVariant>(castingLayer, settings, casterColor, backgroundColor);
1314 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1315}
1316
1317TEST_F(RenderEngineTest, drawLayers_fillShadow_casterOpaqueBufferLayer) {
1318 const ubyte4 casterColor(255, 0, 0, 255);
1319 const ubyte4 backgroundColor(255, 255, 255, 255);
1320 const float shadowLength = 5.0f;
1321 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1322 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1323 renderengine::LayerSettings castingLayer;
1324 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1325 castingLayer.alpha = 1.0f;
1326 renderengine::ShadowSettings settings =
1327 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1328 false /* casterIsTranslucent */);
1329
1330 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1331 backgroundColor);
1332 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1333}
1334
1335TEST_F(RenderEngineTest, drawLayers_fillShadow_casterWithRoundedCorner) {
1336 const ubyte4 casterColor(255, 0, 0, 255);
1337 const ubyte4 backgroundColor(255, 255, 255, 255);
1338 const float shadowLength = 5.0f;
1339 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1340 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1341 renderengine::LayerSettings castingLayer;
1342 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1343 castingLayer.geometry.roundedCornersRadius = 3.0f;
1344 castingLayer.geometry.roundedCornersCrop = casterBounds.toFloatRect();
1345 castingLayer.alpha = 1.0f;
1346 renderengine::ShadowSettings settings =
1347 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1348 false /* casterIsTranslucent */);
1349
1350 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1351 backgroundColor);
1352 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1353}
1354
1355TEST_F(RenderEngineTest, drawLayers_fillShadow_translucentCasterWithAlpha) {
1356 const ubyte4 casterColor(255, 0, 0, 255);
1357 const ubyte4 backgroundColor(255, 255, 255, 255);
1358 const float shadowLength = 5.0f;
1359 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1360 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1361 renderengine::LayerSettings castingLayer;
1362 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1363 castingLayer.alpha = 0.5f;
1364 renderengine::ShadowSettings settings =
1365 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1366 true /* casterIsTranslucent */);
1367
1368 drawShadow<BufferSourceVariant<RelaxOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1369 backgroundColor);
1370
1371 // verify only the background since the shadow will draw behind the caster
1372 const float shadowInset = settings.length * -1.0f;
1373 const Rect casterWithShadow =
1374 Rect(casterBounds).inset(shadowInset, shadowInset, shadowInset, shadowInset);
1375 const Region backgroundRegion = Region(fullscreenRect()).subtractSelf(casterWithShadow);
1376 expectBufferColor(backgroundRegion, backgroundColor.r, backgroundColor.g, backgroundColor.b,
1377 backgroundColor.a);
1378}
1379
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001380TEST_F(RenderEngineTest, cleanupPostRender_cleansUpOnce) {
1381 renderengine::DisplaySettings settings;
1382 settings.physicalDisplay = fullscreenRect();
1383 settings.clip = fullscreenRect();
1384
1385 std::vector<const renderengine::LayerSettings*> layers;
1386 renderengine::LayerSettings layer;
1387 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1388 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1389 layer.alpha = 1.0;
1390 layers.push_back(&layer);
1391
1392 base::unique_fd fenceOne;
1393 sRE->drawLayers(settings, layers, mBuffer, true, base::unique_fd(), &fenceOne);
1394 base::unique_fd fenceTwo;
1395 sRE->drawLayers(settings, layers, mBuffer, true, std::move(fenceOne), &fenceTwo);
1396
1397 const int fd = fenceTwo.get();
1398 if (fd >= 0) {
1399 sync_wait(fd, -1);
1400 }
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001401 // Only cleanup the first time.
Alec Mouri368e1582020-08-13 10:14:29 -07001402 EXPECT_TRUE(sRE->cleanupPostRender(
1403 renderengine::RenderEngine::CleanupMode::CLEAN_OUTPUT_RESOURCES));
1404 EXPECT_FALSE(sRE->cleanupPostRender(
1405 renderengine::RenderEngine::CleanupMode::CLEAN_OUTPUT_RESOURCES));
1406}
1407
1408TEST_F(RenderEngineTest, cleanupPostRender_whenCleaningAll_replacesTextureMemory) {
1409 renderengine::DisplaySettings settings;
1410 settings.physicalDisplay = fullscreenRect();
1411 settings.clip = fullscreenRect();
1412
1413 std::vector<const renderengine::LayerSettings*> layers;
1414 renderengine::LayerSettings layer;
1415 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1416 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1417 layer.alpha = 1.0;
1418 layers.push_back(&layer);
1419
1420 base::unique_fd fence;
1421 sRE->drawLayers(settings, layers, mBuffer, true, base::unique_fd(), &fence);
1422
1423 const int fd = fence.get();
1424 if (fd >= 0) {
1425 sync_wait(fd, -1);
1426 }
1427
1428 uint64_t bufferId = layer.source.buffer.buffer->getId();
1429 uint32_t texName = layer.source.buffer.textureName;
1430 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
1431 EXPECT_EQ(bufferId, sRE->getBufferIdForTextureNameForTesting(texName));
1432
1433 EXPECT_TRUE(sRE->cleanupPostRender(renderengine::RenderEngine::CleanupMode::CLEAN_ALL));
1434
1435 // Now check that our view of memory is good.
1436 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1437 EXPECT_EQ(std::nullopt, sRE->getBufferIdForTextureNameForTesting(bufferId));
1438 EXPECT_TRUE(sRE->isTextureNameKnownForTesting(texName));
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001439}
1440
Alec Mouri6e57f682018-09-29 20:45:08 -07001441} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001442
1443// TODO(b/129481165): remove the #pragma below and fix conversion issues
1444#pragma clang diagnostic pop // ignored "-Wconversion"