blob: 77b6c0f4f5ee18c7d5270a067dd9b2cba7565464 [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);
86 }
87 }
88
Vishnu Nair16efdbf2019-12-10 11:55:42 -080089 void writeBufferToFile(const char* basename) {
90 std::string filename(basename);
91 filename.append(::testing::UnitTest::GetInstance()->current_test_info()->name());
92 filename.append(".ppm");
93 std::ofstream file(filename.c_str(), std::ios::binary);
94 if (!file.is_open()) {
95 ALOGE("Unable to open file: %s", filename.c_str());
96 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
97 "surfaceflinger to write debug images");
98 return;
99 }
100
Alec Mouri1089aed2018-10-25 21:33:57 -0700101 uint8_t* pixels;
102 mBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
103 reinterpret_cast<void**>(&pixels));
104
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800105 file << "P6\n";
106 file << mBuffer->getWidth() << "\n";
107 file << mBuffer->getHeight() << "\n";
108 file << 255 << "\n";
109
110 std::vector<uint8_t> outBuffer(mBuffer->getWidth() * mBuffer->getHeight() * 3);
111 auto outPtr = reinterpret_cast<uint8_t*>(outBuffer.data());
112
113 for (int32_t j = 0; j < mBuffer->getHeight(); j++) {
114 const uint8_t* src = pixels + (mBuffer->getStride() * j) * 4;
115 for (int32_t i = 0; i < mBuffer->getWidth(); i++) {
116 // Only copy R, G and B components
117 outPtr[0] = src[0];
118 outPtr[1] = src[1];
119 outPtr[2] = src[2];
120 outPtr += 3;
121
122 src += 4;
123 }
124 }
125 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
126 mBuffer->unlock();
127 }
128
129 void expectBufferColor(const Region& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
130 size_t c;
131 Rect const* rect = region.getArray(&c);
132 for (size_t i = 0; i < c; i++, rect++) {
133 expectBufferColor(*rect, r, g, b, a);
134 }
135 }
136
137 void expectBufferColor(const Rect& rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a,
138 uint8_t tolerance = 0) {
139 auto colorCompare = [tolerance](const uint8_t* colorA, const uint8_t* colorB) {
140 auto colorBitCompare = [tolerance](uint8_t a, uint8_t b) {
141 uint8_t tmp = a >= b ? a - b : b - a;
142 return tmp <= tolerance;
143 };
144 return std::equal(colorA, colorA + 4, colorB, colorBitCompare);
Alec Mouri1089aed2018-10-25 21:33:57 -0700145 };
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800146
147 expectBufferColor(rect, r, g, b, a, colorCompare);
148 }
149
150 void expectBufferColor(const Rect& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a,
151 std::function<bool(const uint8_t* a, const uint8_t* b)> colorCompare) {
152 uint8_t* pixels;
153 mBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
154 reinterpret_cast<void**>(&pixels));
Alec Mouri1089aed2018-10-25 21:33:57 -0700155 int32_t maxFails = 10;
156 int32_t fails = 0;
157 for (int32_t j = 0; j < region.getHeight(); j++) {
158 const uint8_t* src =
159 pixels + (mBuffer->getStride() * (region.top + j) + region.left) * 4;
160 for (int32_t i = 0; i < region.getWidth(); i++) {
161 const uint8_t expected[4] = {r, g, b, a};
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800162 bool equal = colorCompare(src, expected);
Alec Mouri1089aed2018-10-25 21:33:57 -0700163 EXPECT_TRUE(equal)
164 << "pixel @ (" << region.left + i << ", " << region.top + j << "): "
165 << "expected (" << static_cast<uint32_t>(r) << ", "
166 << static_cast<uint32_t>(g) << ", " << static_cast<uint32_t>(b) << ", "
167 << static_cast<uint32_t>(a) << "), "
168 << "got (" << static_cast<uint32_t>(src[0]) << ", "
169 << static_cast<uint32_t>(src[1]) << ", " << static_cast<uint32_t>(src[2])
170 << ", " << static_cast<uint32_t>(src[3]) << ")";
171 src += 4;
172 if (!equal && ++fails >= maxFails) {
173 break;
174 }
175 }
176 if (fails >= maxFails) {
177 break;
178 }
179 }
180 mBuffer->unlock();
181 }
182
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800183 void expectAlpha(const Rect& rect, uint8_t a) {
184 auto colorCompare = [](const uint8_t* colorA, const uint8_t* colorB) {
185 return colorA[3] == colorB[3];
186 };
187 expectBufferColor(rect, 0.0f /* r */, 0.0f /*g */, 0.0f /* b */, a, colorCompare);
188 }
189
190 void expectShadowColor(const renderengine::LayerSettings& castingLayer,
191 const renderengine::ShadowSettings& shadow, const ubyte4& casterColor,
192 const ubyte4& backgroundColor) {
193 const Rect casterRect(castingLayer.geometry.boundaries);
194 Region casterRegion = Region(casterRect);
195 const float casterCornerRadius = castingLayer.geometry.roundedCornersRadius;
196 if (casterCornerRadius > 0.0f) {
197 // ignore the corners if a corner radius is set
198 Rect cornerRect(casterCornerRadius, casterCornerRadius);
199 casterRegion.subtractSelf(cornerRect.offsetTo(casterRect.left, casterRect.top));
200 casterRegion.subtractSelf(
201 cornerRect.offsetTo(casterRect.right - casterCornerRadius, casterRect.top));
202 casterRegion.subtractSelf(
203 cornerRect.offsetTo(casterRect.left, casterRect.bottom - casterCornerRadius));
204 casterRegion.subtractSelf(cornerRect.offsetTo(casterRect.right - casterCornerRadius,
205 casterRect.bottom - casterCornerRadius));
206 }
207
208 const float shadowInset = shadow.length * -1.0f;
209 const Rect casterWithShadow =
210 Rect(casterRect).inset(shadowInset, shadowInset, shadowInset, shadowInset);
211 const Region shadowRegion = Region(casterWithShadow).subtractSelf(casterRect);
212 const Region backgroundRegion = Region(fullscreenRect()).subtractSelf(casterWithShadow);
213
214 // verify casting layer
215 expectBufferColor(casterRegion, casterColor.r, casterColor.g, casterColor.b, casterColor.a);
216
217 // verify shadows by testing just the alpha since its difficult to validate the shadow color
218 size_t c;
219 Rect const* r = shadowRegion.getArray(&c);
220 for (size_t i = 0; i < c; i++, r++) {
221 expectAlpha(*r, 255);
222 }
223
224 // verify background
225 expectBufferColor(backgroundRegion, backgroundColor.r, backgroundColor.g, backgroundColor.b,
226 backgroundColor.a);
227 }
228
229 static renderengine::ShadowSettings getShadowSettings(const vec2& casterPos, float shadowLength,
230 bool casterIsTranslucent) {
231 renderengine::ShadowSettings shadow;
232 shadow.ambientColor = {0.0f, 0.0f, 0.0f, 0.039f};
233 shadow.spotColor = {0.0f, 0.0f, 0.0f, 0.19f};
234 shadow.lightPos = vec3(casterPos.x, casterPos.y, 0);
235 shadow.lightRadius = 0.0f;
236 shadow.length = shadowLength;
237 shadow.casterIsTranslucent = casterIsTranslucent;
238 return shadow;
239 }
240
Alec Mouri1089aed2018-10-25 21:33:57 -0700241 static Rect fullscreenRect() { return Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT); }
242
243 static Rect offsetRect() {
244 return Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
245 DEFAULT_DISPLAY_HEIGHT);
246 }
247
248 static Rect offsetRectAtZero() {
249 return Rect(DEFAULT_DISPLAY_WIDTH - DEFAULT_DISPLAY_OFFSET,
250 DEFAULT_DISPLAY_HEIGHT - DEFAULT_DISPLAY_OFFSET);
251 }
252
Alec Mourid43ccab2019-03-13 12:23:45 -0700253 void invokeDraw(renderengine::DisplaySettings settings,
Vishnu Nair9b079a22020-01-21 14:36:08 -0800254 std::vector<const renderengine::LayerSettings*> layers,
255 sp<GraphicBuffer> buffer) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700256 base::unique_fd fence;
Ana Krulecfc874ae2020-02-22 15:39:32 -0800257 status_t status =
258 sRE->drawLayers(settings, layers, buffer, true, base::unique_fd(), &fence);
Alec Mourid43ccab2019-03-13 12:23:45 -0700259 sCurrentBuffer = buffer;
Alec Mouri1089aed2018-10-25 21:33:57 -0700260
261 int fd = fence.release();
262 if (fd >= 0) {
263 sync_wait(fd, -1);
264 close(fd);
265 }
266
267 ASSERT_EQ(NO_ERROR, status);
Alec Mourid43ccab2019-03-13 12:23:45 -0700268 if (layers.size() > 0) {
269 ASSERT_TRUE(sRE->isFramebufferImageCachedForTesting(buffer->getId()));
270 }
Alec Mouri1089aed2018-10-25 21:33:57 -0700271 }
272
Alec Mourid43ccab2019-03-13 12:23:45 -0700273 void drawEmptyLayers() {
Alec Mouri6e57f682018-09-29 20:45:08 -0700274 renderengine::DisplaySettings settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800275 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri6e57f682018-09-29 20:45:08 -0700276 // Meaningless buffer since we don't do any drawing
277 sp<GraphicBuffer> buffer = new GraphicBuffer();
Alec Mouri1089aed2018-10-25 21:33:57 -0700278 invokeDraw(settings, layers, buffer);
Alec Mouri6e57f682018-09-29 20:45:08 -0700279 }
280
Alec Mouri1089aed2018-10-25 21:33:57 -0700281 template <typename SourceVariant>
282 void fillBuffer(half r, half g, half b, half a);
283
284 template <typename SourceVariant>
285 void fillRedBuffer();
286
287 template <typename SourceVariant>
288 void fillGreenBuffer();
289
290 template <typename SourceVariant>
291 void fillBlueBuffer();
292
293 template <typename SourceVariant>
294 void fillRedTransparentBuffer();
295
296 template <typename SourceVariant>
297 void fillRedOffsetBuffer();
298
299 template <typename SourceVariant>
300 void fillBufferPhysicalOffset();
301
302 template <typename SourceVariant>
Alec Mouri5a6d8572020-03-23 23:56:15 -0700303 void fillBufferCheckers(uint32_t rotation);
Alec Mouri1089aed2018-10-25 21:33:57 -0700304
305 template <typename SourceVariant>
306 void fillBufferCheckersRotate0();
307
308 template <typename SourceVariant>
309 void fillBufferCheckersRotate90();
310
311 template <typename SourceVariant>
312 void fillBufferCheckersRotate180();
313
314 template <typename SourceVariant>
315 void fillBufferCheckersRotate270();
316
317 template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800318 void fillBufferWithLayerTransform();
319
320 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700321 void fillBufferLayerTransform();
322
323 template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800324 void fillBufferWithColorTransform();
325
326 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700327 void fillBufferColorTransform();
328
Alec Mouri7c94edb2018-12-03 21:23:26 -0800329 template <typename SourceVariant>
330 void fillRedBufferWithRoundedCorners();
331
332 template <typename SourceVariant>
333 void fillBufferWithRoundedCorners();
334
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000335 template <typename SourceVariant>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800336 void fillBufferAndBlurBackground();
337
338 template <typename SourceVariant>
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000339 void overlayCorners();
340
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800341 void fillRedBufferTextureTransform();
342
343 void fillBufferTextureTransform();
344
345 void fillRedBufferWithPremultiplyAlpha();
346
347 void fillBufferWithPremultiplyAlpha();
348
349 void fillRedBufferWithoutPremultiplyAlpha();
350
351 void fillBufferWithoutPremultiplyAlpha();
352
Alec Mouriac335532018-11-12 15:01:33 -0800353 void fillGreenColorBufferThenClearRegion();
354
355 void clearLeftRegion();
356
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000357 void clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -0800358
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800359 template <typename SourceVariant>
360 void drawShadow(const renderengine::LayerSettings& castingLayer,
361 const renderengine::ShadowSettings& shadow, const ubyte4& casterColor,
362 const ubyte4& backgroundColor);
363
Alec Mourid43ccab2019-03-13 12:23:45 -0700364 // Keep around the same renderengine object to save on initialization time.
365 // For now, exercise the GL backend directly so that some caching specifics
366 // can be tested without changing the interface.
367 static std::unique_ptr<renderengine::gl::GLESRenderEngine> sRE;
368 // Dumb hack to avoid NPE in the EGL driver: the GraphicBuffer needs to
369 // be freed *after* RenderEngine is destroyed, so that the EGL image is
370 // destroyed first.
371 static sp<GraphicBuffer> sCurrentBuffer;
Alec Mouri1089aed2018-10-25 21:33:57 -0700372
373 sp<GraphicBuffer> mBuffer;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800374
375 std::vector<uint32_t> mTexNames;
Alec Mouri6e57f682018-09-29 20:45:08 -0700376};
377
Alec Mourid43ccab2019-03-13 12:23:45 -0700378std::unique_ptr<renderengine::gl::GLESRenderEngine> RenderEngineTest::sRE = nullptr;
379sp<GraphicBuffer> RenderEngineTest::sCurrentBuffer = nullptr;
Alec Mouri1089aed2018-10-25 21:33:57 -0700380
381struct ColorSourceVariant {
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800382 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
383 RenderEngineTest* /*fixture*/) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700384 layer.source.solidColor = half3(r, g, b);
385 }
386};
387
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800388struct RelaxOpaqueBufferVariant {
389 static void setOpaqueBit(renderengine::LayerSettings& layer) {
390 layer.source.buffer.isOpaque = false;
391 }
392
393 static uint8_t getAlphaChannel() { return 255; }
394};
395
396struct ForceOpaqueBufferVariant {
397 static void setOpaqueBit(renderengine::LayerSettings& layer) {
398 layer.source.buffer.isOpaque = true;
399 }
400
401 static uint8_t getAlphaChannel() {
402 // The isOpaque bit will override the alpha channel, so this should be
403 // arbitrary.
404 return 10;
405 }
406};
407
408template <typename OpaquenessVariant>
409struct BufferSourceVariant {
410 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
411 RenderEngineTest* fixture) {
412 sp<GraphicBuffer> buf = RenderEngineTest::allocateSourceBuffer(1, 1);
413 uint32_t texName;
Alec Mourid43ccab2019-03-13 12:23:45 -0700414 fixture->sRE->genTextures(1, &texName);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800415 fixture->mTexNames.push_back(texName);
416
417 uint8_t* pixels;
418 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
419 reinterpret_cast<void**>(&pixels));
420
421 for (int32_t j = 0; j < buf->getHeight(); j++) {
422 uint8_t* iter = pixels + (buf->getStride() * j) * 4;
423 for (int32_t i = 0; i < buf->getWidth(); i++) {
424 iter[0] = uint8_t(r * 255);
425 iter[1] = uint8_t(g * 255);
426 iter[2] = uint8_t(b * 255);
427 iter[3] = OpaquenessVariant::getAlphaChannel();
428 iter += 4;
429 }
430 }
431
432 buf->unlock();
433
434 layer.source.buffer.buffer = buf;
435 layer.source.buffer.textureName = texName;
436 OpaquenessVariant::setOpaqueBit(layer);
437 }
438};
439
Alec Mouri1089aed2018-10-25 21:33:57 -0700440template <typename SourceVariant>
441void RenderEngineTest::fillBuffer(half r, half g, half b, half a) {
442 renderengine::DisplaySettings settings;
443 settings.physicalDisplay = fullscreenRect();
444 settings.clip = fullscreenRect();
445
Vishnu Nair9b079a22020-01-21 14:36:08 -0800446 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700447
448 renderengine::LayerSettings layer;
449 layer.geometry.boundaries = fullscreenRect().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800450 SourceVariant::fillColor(layer, r, g, b, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700451 layer.alpha = a;
452
Vishnu Nair9b079a22020-01-21 14:36:08 -0800453 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700454
455 invokeDraw(settings, layers, mBuffer);
456}
457
458template <typename SourceVariant>
459void RenderEngineTest::fillRedBuffer() {
460 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, 1.0f);
461 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
462}
463
464template <typename SourceVariant>
465void RenderEngineTest::fillGreenBuffer() {
466 fillBuffer<SourceVariant>(0.0f, 1.0f, 0.0f, 1.0f);
467 expectBufferColor(fullscreenRect(), 0, 255, 0, 255);
468}
469
470template <typename SourceVariant>
471void RenderEngineTest::fillBlueBuffer() {
472 fillBuffer<SourceVariant>(0.0f, 0.0f, 1.0f, 1.0f);
473 expectBufferColor(fullscreenRect(), 0, 0, 255, 255);
474}
475
476template <typename SourceVariant>
477void RenderEngineTest::fillRedTransparentBuffer() {
478 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, .2f);
479 expectBufferColor(fullscreenRect(), 51, 0, 0, 51);
480}
481
482template <typename SourceVariant>
483void RenderEngineTest::fillRedOffsetBuffer() {
484 renderengine::DisplaySettings settings;
485 settings.physicalDisplay = offsetRect();
486 settings.clip = offsetRectAtZero();
487
Vishnu Nair9b079a22020-01-21 14:36:08 -0800488 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700489
490 renderengine::LayerSettings layer;
491 layer.geometry.boundaries = offsetRectAtZero().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800492 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700493 layer.alpha = 1.0f;
494
Vishnu Nair9b079a22020-01-21 14:36:08 -0800495 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700496 invokeDraw(settings, layers, mBuffer);
497}
498
499template <typename SourceVariant>
500void RenderEngineTest::fillBufferPhysicalOffset() {
501 fillRedOffsetBuffer<SourceVariant>();
502
503 expectBufferColor(Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
504 DEFAULT_DISPLAY_HEIGHT),
505 255, 0, 0, 255);
506 Rect offsetRegionLeft(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_HEIGHT);
507 Rect offsetRegionTop(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_OFFSET);
508
509 expectBufferColor(offsetRegionLeft, 0, 0, 0, 0);
510 expectBufferColor(offsetRegionTop, 0, 0, 0, 0);
511}
512
513template <typename SourceVariant>
Alec Mouri5a6d8572020-03-23 23:56:15 -0700514void RenderEngineTest::fillBufferCheckers(uint32_t orientationFlag) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700515 renderengine::DisplaySettings settings;
516 settings.physicalDisplay = fullscreenRect();
517 // Here logical space is 2x2
518 settings.clip = Rect(2, 2);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700519 settings.orientation = orientationFlag;
Alec Mouri1089aed2018-10-25 21:33:57 -0700520
Vishnu Nair9b079a22020-01-21 14:36:08 -0800521 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700522
523 renderengine::LayerSettings layerOne;
524 Rect rectOne(0, 0, 1, 1);
525 layerOne.geometry.boundaries = rectOne.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800526 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700527 layerOne.alpha = 1.0f;
528
529 renderengine::LayerSettings layerTwo;
530 Rect rectTwo(0, 1, 1, 2);
531 layerTwo.geometry.boundaries = rectTwo.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800532 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700533 layerTwo.alpha = 1.0f;
534
535 renderengine::LayerSettings layerThree;
536 Rect rectThree(1, 0, 2, 1);
537 layerThree.geometry.boundaries = rectThree.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800538 SourceVariant::fillColor(layerThree, 0.0f, 0.0f, 1.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700539 layerThree.alpha = 1.0f;
540
Vishnu Nair9b079a22020-01-21 14:36:08 -0800541 layers.push_back(&layerOne);
542 layers.push_back(&layerTwo);
543 layers.push_back(&layerThree);
Alec Mouri1089aed2018-10-25 21:33:57 -0700544
545 invokeDraw(settings, layers, mBuffer);
546}
547
548template <typename SourceVariant>
549void RenderEngineTest::fillBufferCheckersRotate0() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700550 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_0);
Alec Mouri1089aed2018-10-25 21:33:57 -0700551 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 255, 0, 0,
552 255);
553 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
554 DEFAULT_DISPLAY_HEIGHT / 2),
555 0, 0, 255, 255);
556 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
557 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
558 0, 0, 0, 0);
559 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
560 DEFAULT_DISPLAY_HEIGHT),
561 0, 255, 0, 255);
562}
563
564template <typename SourceVariant>
565void RenderEngineTest::fillBufferCheckersRotate90() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700566 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_90);
Alec Mouri1089aed2018-10-25 21:33:57 -0700567 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 255, 0,
568 255);
569 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
570 DEFAULT_DISPLAY_HEIGHT / 2),
571 255, 0, 0, 255);
572 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
573 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
574 0, 0, 255, 255);
575 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
576 DEFAULT_DISPLAY_HEIGHT),
577 0, 0, 0, 0);
578}
579
580template <typename SourceVariant>
581void RenderEngineTest::fillBufferCheckersRotate180() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700582 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_180);
Alec Mouri1089aed2018-10-25 21:33:57 -0700583 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0,
584 0);
585 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
586 DEFAULT_DISPLAY_HEIGHT / 2),
587 0, 255, 0, 255);
588 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
589 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
590 255, 0, 0, 255);
591 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
592 DEFAULT_DISPLAY_HEIGHT),
593 0, 0, 255, 255);
594}
595
596template <typename SourceVariant>
597void RenderEngineTest::fillBufferCheckersRotate270() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700598 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_270);
Alec Mouri1089aed2018-10-25 21:33:57 -0700599 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 255,
600 255);
601 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
602 DEFAULT_DISPLAY_HEIGHT / 2),
603 0, 0, 0, 0);
604 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
605 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
606 0, 255, 0, 255);
607 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
608 DEFAULT_DISPLAY_HEIGHT),
609 255, 0, 0, 255);
610}
611
612template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800613void RenderEngineTest::fillBufferWithLayerTransform() {
Alec Mouri1089aed2018-10-25 21:33:57 -0700614 renderengine::DisplaySettings settings;
615 settings.physicalDisplay = fullscreenRect();
616 // Here logical space is 2x2
617 settings.clip = Rect(2, 2);
618
Vishnu Nair9b079a22020-01-21 14:36:08 -0800619 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700620
621 renderengine::LayerSettings layer;
622 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
623 // Translate one pixel diagonally
624 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 -0800625 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700626 layer.source.solidColor = half3(1.0f, 0.0f, 0.0f);
627 layer.alpha = 1.0f;
628
Vishnu Nair9b079a22020-01-21 14:36:08 -0800629 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700630
631 invokeDraw(settings, layers, mBuffer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800632}
Alec Mouri1089aed2018-10-25 21:33:57 -0700633
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800634template <typename SourceVariant>
635void RenderEngineTest::fillBufferLayerTransform() {
636 fillBufferWithLayerTransform<SourceVariant>();
Alec Mouri1089aed2018-10-25 21:33:57 -0700637 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0, 0);
638 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 0);
639 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
640 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
641 255, 0, 0, 255);
642}
643
644template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800645void RenderEngineTest::fillBufferWithColorTransform() {
Alec Mouri1089aed2018-10-25 21:33:57 -0700646 renderengine::DisplaySettings settings;
647 settings.physicalDisplay = fullscreenRect();
648 settings.clip = Rect(1, 1);
649
Vishnu Nair9b079a22020-01-21 14:36:08 -0800650 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700651
652 renderengine::LayerSettings layer;
653 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800654 SourceVariant::fillColor(layer, 0.5f, 0.25f, 0.125f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700655 layer.alpha = 1.0f;
656
657 // construct a fake color matrix
658 // annihilate green and blue channels
659 settings.colorTransform = mat4::scale(vec4(1, 0, 0, 1));
660 // set red channel to red + green
661 layer.colorTransform = mat4(1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
662
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800663 layer.alpha = 1.0f;
664 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
665
Vishnu Nair9b079a22020-01-21 14:36:08 -0800666 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700667
668 invokeDraw(settings, layers, mBuffer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800669}
Alec Mouri1089aed2018-10-25 21:33:57 -0700670
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800671template <typename SourceVariant>
672void RenderEngineTest::fillBufferColorTransform() {
673 fillBufferWithColorTransform<SourceVariant>();
Alec Mouri1089aed2018-10-25 21:33:57 -0700674 expectBufferColor(fullscreenRect(), 191, 0, 0, 255);
675}
676
Alec Mouri7c94edb2018-12-03 21:23:26 -0800677template <typename SourceVariant>
678void RenderEngineTest::fillRedBufferWithRoundedCorners() {
679 renderengine::DisplaySettings settings;
680 settings.physicalDisplay = fullscreenRect();
681 settings.clip = fullscreenRect();
682
Vishnu Nair9b079a22020-01-21 14:36:08 -0800683 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri7c94edb2018-12-03 21:23:26 -0800684
685 renderengine::LayerSettings layer;
686 layer.geometry.boundaries = fullscreenRect().toFloatRect();
687 layer.geometry.roundedCornersRadius = 5.0f;
688 layer.geometry.roundedCornersCrop = fullscreenRect().toFloatRect();
689 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
690 layer.alpha = 1.0f;
691
Vishnu Nair9b079a22020-01-21 14:36:08 -0800692 layers.push_back(&layer);
Alec Mouri7c94edb2018-12-03 21:23:26 -0800693
694 invokeDraw(settings, layers, mBuffer);
695}
696
697template <typename SourceVariant>
698void RenderEngineTest::fillBufferWithRoundedCorners() {
699 fillRedBufferWithRoundedCorners<SourceVariant>();
700 // Corners should be ignored...
701 expectBufferColor(Rect(0, 0, 1, 1), 0, 0, 0, 0);
702 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH - 1, 0, DEFAULT_DISPLAY_WIDTH, 1), 0, 0, 0, 0);
703 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT - 1, 1, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 0);
704 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH - 1, DEFAULT_DISPLAY_HEIGHT - 1,
705 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
706 0, 0, 0, 0);
707 // ...And the non-rounded portion should be red.
708 // Other pixels may be anti-aliased, so let's not check those.
709 expectBufferColor(Rect(5, 5, DEFAULT_DISPLAY_WIDTH - 5, DEFAULT_DISPLAY_HEIGHT - 5), 255, 0, 0,
710 255);
711}
712
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000713template <typename SourceVariant>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800714void RenderEngineTest::fillBufferAndBlurBackground() {
715 char value[PROPERTY_VALUE_MAX];
716 property_get("ro.surface_flinger.supports_background_blur", value, "0");
717 if (!atoi(value)) {
718 // This device doesn't support blurs, no-op.
719 return;
720 }
721
722 auto blurRadius = 50;
723 auto center = DEFAULT_DISPLAY_WIDTH / 2;
724
725 renderengine::DisplaySettings settings;
726 settings.physicalDisplay = fullscreenRect();
727 settings.clip = fullscreenRect();
728
Vishnu Nair9b079a22020-01-21 14:36:08 -0800729 std::vector<const renderengine::LayerSettings*> layers;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800730
731 renderengine::LayerSettings backgroundLayer;
732 backgroundLayer.geometry.boundaries = fullscreenRect().toFloatRect();
733 SourceVariant::fillColor(backgroundLayer, 0.0f, 1.0f, 0.0f, this);
734 backgroundLayer.alpha = 1.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800735 layers.push_back(&backgroundLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800736
737 renderengine::LayerSettings leftLayer;
738 leftLayer.geometry.boundaries =
739 Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT).toFloatRect();
740 SourceVariant::fillColor(leftLayer, 1.0f, 0.0f, 0.0f, this);
741 leftLayer.alpha = 1.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800742 layers.push_back(&leftLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800743
744 renderengine::LayerSettings blurLayer;
745 blurLayer.geometry.boundaries = fullscreenRect().toFloatRect();
746 blurLayer.backgroundBlurRadius = blurRadius;
747 blurLayer.alpha = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800748 layers.push_back(&blurLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800749
750 invokeDraw(settings, layers, mBuffer);
751
752 expectBufferColor(Rect(center - 1, center - 5, center, center + 5), 150, 150, 0, 255,
753 50 /* tolerance */);
754 expectBufferColor(Rect(center, center - 5, center + 1, center + 5), 150, 150, 0, 255,
755 50 /* tolerance */);
756}
757
758template <typename SourceVariant>
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000759void RenderEngineTest::overlayCorners() {
760 renderengine::DisplaySettings settings;
761 settings.physicalDisplay = fullscreenRect();
762 settings.clip = fullscreenRect();
763
Vishnu Nair9b079a22020-01-21 14:36:08 -0800764 std::vector<const renderengine::LayerSettings*> layersFirst;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000765
766 renderengine::LayerSettings layerOne;
767 layerOne.geometry.boundaries =
768 FloatRect(0, 0, DEFAULT_DISPLAY_WIDTH / 3.0, DEFAULT_DISPLAY_HEIGHT / 3.0);
769 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
770 layerOne.alpha = 0.2;
771
Vishnu Nair9b079a22020-01-21 14:36:08 -0800772 layersFirst.push_back(&layerOne);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000773 invokeDraw(settings, layersFirst, mBuffer);
774 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3, DEFAULT_DISPLAY_HEIGHT / 3), 51, 0, 0, 51);
775 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3 + 1, DEFAULT_DISPLAY_HEIGHT / 3 + 1,
776 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
777 0, 0, 0, 0);
778
Vishnu Nair9b079a22020-01-21 14:36:08 -0800779 std::vector<const renderengine::LayerSettings*> layersSecond;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000780 renderengine::LayerSettings layerTwo;
781 layerTwo.geometry.boundaries =
782 FloatRect(DEFAULT_DISPLAY_WIDTH / 3.0, DEFAULT_DISPLAY_HEIGHT / 3.0,
783 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT);
784 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
785 layerTwo.alpha = 1.0f;
786
Vishnu Nair9b079a22020-01-21 14:36:08 -0800787 layersSecond.push_back(&layerTwo);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000788 invokeDraw(settings, layersSecond, mBuffer);
789
790 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3, DEFAULT_DISPLAY_HEIGHT / 3), 0, 0, 0, 0);
791 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3 + 1, DEFAULT_DISPLAY_HEIGHT / 3 + 1,
792 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
793 0, 255, 0, 255);
794}
795
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800796void RenderEngineTest::fillRedBufferTextureTransform() {
797 renderengine::DisplaySettings settings;
798 settings.physicalDisplay = fullscreenRect();
799 settings.clip = Rect(1, 1);
800
Vishnu Nair9b079a22020-01-21 14:36:08 -0800801 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800802
803 renderengine::LayerSettings layer;
804 // Here will allocate a checker board texture, but transform texture
805 // coordinates so that only the upper left is applied.
806 sp<GraphicBuffer> buf = allocateSourceBuffer(2, 2);
807 uint32_t texName;
808 RenderEngineTest::sRE->genTextures(1, &texName);
809 this->mTexNames.push_back(texName);
810
811 uint8_t* pixels;
812 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
813 reinterpret_cast<void**>(&pixels));
814 // Red top left, Green top right, Blue bottom left, Black bottom right
815 pixels[0] = 255;
816 pixels[1] = 0;
817 pixels[2] = 0;
818 pixels[3] = 255;
819 pixels[4] = 0;
820 pixels[5] = 255;
821 pixels[6] = 0;
822 pixels[7] = 255;
823 pixels[8] = 0;
824 pixels[9] = 0;
825 pixels[10] = 255;
826 pixels[11] = 255;
827 buf->unlock();
828
829 layer.source.buffer.buffer = buf;
830 layer.source.buffer.textureName = texName;
831 // Transform coordinates to only be inside the red quadrant.
832 layer.source.buffer.textureTransform = mat4::scale(vec4(0.2, 0.2, 1, 1));
833 layer.alpha = 1.0f;
834 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
835
Vishnu Nair9b079a22020-01-21 14:36:08 -0800836 layers.push_back(&layer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800837
838 invokeDraw(settings, layers, mBuffer);
839}
840
841void RenderEngineTest::fillBufferTextureTransform() {
842 fillRedBufferTextureTransform();
843 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
844}
845
846void RenderEngineTest::fillRedBufferWithPremultiplyAlpha() {
847 renderengine::DisplaySettings settings;
848 settings.physicalDisplay = fullscreenRect();
849 // Here logical space is 1x1
850 settings.clip = Rect(1, 1);
851
Vishnu Nair9b079a22020-01-21 14:36:08 -0800852 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800853
854 renderengine::LayerSettings layer;
855 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
856 uint32_t texName;
857 RenderEngineTest::sRE->genTextures(1, &texName);
858 this->mTexNames.push_back(texName);
859
860 uint8_t* pixels;
861 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
862 reinterpret_cast<void**>(&pixels));
863 pixels[0] = 255;
864 pixels[1] = 0;
865 pixels[2] = 0;
866 pixels[3] = 255;
867 buf->unlock();
868
869 layer.source.buffer.buffer = buf;
870 layer.source.buffer.textureName = texName;
871 layer.source.buffer.usePremultipliedAlpha = true;
872 layer.alpha = 0.5f;
873 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
874
Vishnu Nair9b079a22020-01-21 14:36:08 -0800875 layers.push_back(&layer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800876
877 invokeDraw(settings, layers, mBuffer);
878}
879
880void RenderEngineTest::fillBufferWithPremultiplyAlpha() {
881 fillRedBufferWithPremultiplyAlpha();
882 expectBufferColor(fullscreenRect(), 128, 0, 0, 128);
883}
884
885void RenderEngineTest::fillRedBufferWithoutPremultiplyAlpha() {
886 renderengine::DisplaySettings settings;
887 settings.physicalDisplay = fullscreenRect();
888 // Here logical space is 1x1
889 settings.clip = Rect(1, 1);
890
Vishnu Nair9b079a22020-01-21 14:36:08 -0800891 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800892
893 renderengine::LayerSettings layer;
894 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
895 uint32_t texName;
896 RenderEngineTest::sRE->genTextures(1, &texName);
897 this->mTexNames.push_back(texName);
898
899 uint8_t* pixels;
900 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
901 reinterpret_cast<void**>(&pixels));
902 pixels[0] = 255;
903 pixels[1] = 0;
904 pixels[2] = 0;
905 pixels[3] = 255;
906 buf->unlock();
907
908 layer.source.buffer.buffer = buf;
909 layer.source.buffer.textureName = texName;
910 layer.source.buffer.usePremultipliedAlpha = false;
911 layer.alpha = 0.5f;
912 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
913
Vishnu Nair9b079a22020-01-21 14:36:08 -0800914 layers.push_back(&layer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800915
916 invokeDraw(settings, layers, mBuffer);
917}
918
919void RenderEngineTest::fillBufferWithoutPremultiplyAlpha() {
920 fillRedBufferWithoutPremultiplyAlpha();
921 expectBufferColor(fullscreenRect(), 128, 0, 0, 64, 1);
922}
923
Alec Mouriac335532018-11-12 15:01:33 -0800924void RenderEngineTest::clearLeftRegion() {
925 renderengine::DisplaySettings settings;
926 settings.physicalDisplay = fullscreenRect();
927 // Here logical space is 4x4
928 settings.clip = Rect(4, 4);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700929 settings.clearRegion = Region(Rect(2, 4));
Vishnu Nair9b079a22020-01-21 14:36:08 -0800930 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouriac335532018-11-12 15:01:33 -0800931 // dummy layer, without bounds should not render anything
932 renderengine::LayerSettings layer;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800933 layers.push_back(&layer);
Alec Mouriac335532018-11-12 15:01:33 -0800934 invokeDraw(settings, layers, mBuffer);
935}
936
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000937void RenderEngineTest::clearRegion() {
Alec Mouriac335532018-11-12 15:01:33 -0800938 // Reuse mBuffer
939 clearLeftRegion();
940 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 255);
941 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
942 DEFAULT_DISPLAY_HEIGHT),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000943 0, 0, 0, 0);
Alec Mouriac335532018-11-12 15:01:33 -0800944}
945
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800946template <typename SourceVariant>
947void RenderEngineTest::drawShadow(const renderengine::LayerSettings& castingLayer,
948 const renderengine::ShadowSettings& shadow,
949 const ubyte4& casterColor, const ubyte4& backgroundColor) {
950 renderengine::DisplaySettings settings;
951 settings.physicalDisplay = fullscreenRect();
952 settings.clip = fullscreenRect();
953
Vishnu Nair9b079a22020-01-21 14:36:08 -0800954 std::vector<const renderengine::LayerSettings*> layers;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800955
956 // add background layer
957 renderengine::LayerSettings bgLayer;
958 bgLayer.geometry.boundaries = fullscreenRect().toFloatRect();
959 ColorSourceVariant::fillColor(bgLayer, backgroundColor.r / 255.0f, backgroundColor.g / 255.0f,
960 backgroundColor.b / 255.0f, this);
961 bgLayer.alpha = backgroundColor.a / 255.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800962 layers.push_back(&bgLayer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800963
964 // add shadow layer
965 renderengine::LayerSettings shadowLayer;
966 shadowLayer.geometry.boundaries = castingLayer.geometry.boundaries;
967 shadowLayer.alpha = castingLayer.alpha;
968 shadowLayer.shadow = shadow;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800969 layers.push_back(&shadowLayer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800970
971 // add layer casting the shadow
972 renderengine::LayerSettings layer = castingLayer;
973 SourceVariant::fillColor(layer, casterColor.r / 255.0f, casterColor.g / 255.0f,
974 casterColor.b / 255.0f, this);
Vishnu Nair9b079a22020-01-21 14:36:08 -0800975 layers.push_back(&layer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800976
977 invokeDraw(settings, layers, mBuffer);
978}
979
Alec Mouri1089aed2018-10-25 21:33:57 -0700980TEST_F(RenderEngineTest, drawLayers_noLayersToDraw) {
981 drawEmptyLayers();
982}
983
Alec Mourid43ccab2019-03-13 12:23:45 -0700984TEST_F(RenderEngineTest, drawLayers_nullOutputBuffer) {
985 renderengine::DisplaySettings settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800986 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -0700987 renderengine::LayerSettings layer;
988 layer.geometry.boundaries = fullscreenRect().toFloatRect();
989 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Vishnu Nair9b079a22020-01-21 14:36:08 -0800990 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -0700991 base::unique_fd fence;
Alec Mourife0d72b2019-03-21 14:05:56 -0700992 status_t status = sRE->drawLayers(settings, layers, nullptr, true, base::unique_fd(), &fence);
Alec Mourid43ccab2019-03-13 12:23:45 -0700993
994 ASSERT_EQ(BAD_VALUE, status);
995}
996
997TEST_F(RenderEngineTest, drawLayers_nullOutputFence) {
998 renderengine::DisplaySettings settings;
999 settings.physicalDisplay = fullscreenRect();
1000 settings.clip = fullscreenRect();
1001
Vishnu Nair9b079a22020-01-21 14:36:08 -08001002 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001003 renderengine::LayerSettings layer;
1004 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1005 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1006 layer.alpha = 1.0;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001007 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001008
Ana Krulecfc874ae2020-02-22 15:39:32 -08001009 status_t status = sRE->drawLayers(settings, layers, mBuffer, true, base::unique_fd(), nullptr);
Alec Mourid43ccab2019-03-13 12:23:45 -07001010 sCurrentBuffer = mBuffer;
1011 ASSERT_EQ(NO_ERROR, status);
1012 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
1013}
1014
Alec Mourife0d72b2019-03-21 14:05:56 -07001015TEST_F(RenderEngineTest, drawLayers_doesNotCacheFramebuffer) {
1016 renderengine::DisplaySettings settings;
1017 settings.physicalDisplay = fullscreenRect();
1018 settings.clip = fullscreenRect();
1019
Vishnu Nair9b079a22020-01-21 14:36:08 -08001020 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourife0d72b2019-03-21 14:05:56 -07001021 renderengine::LayerSettings layer;
1022 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1023 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1024 layer.alpha = 1.0;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001025 layers.push_back(&layer);
Alec Mourife0d72b2019-03-21 14:05:56 -07001026
Ana Krulecfc874ae2020-02-22 15:39:32 -08001027 status_t status = sRE->drawLayers(settings, layers, mBuffer, false, base::unique_fd(), nullptr);
Alec Mourife0d72b2019-03-21 14:05:56 -07001028 sCurrentBuffer = mBuffer;
1029 ASSERT_EQ(NO_ERROR, status);
1030 ASSERT_FALSE(sRE->isFramebufferImageCachedForTesting(mBuffer->getId()));
1031 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
1032}
1033
Alec Mouri1089aed2018-10-25 21:33:57 -07001034TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_colorSource) {
1035 fillRedBuffer<ColorSourceVariant>();
1036}
1037
1038TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_colorSource) {
1039 fillGreenBuffer<ColorSourceVariant>();
1040}
1041
1042TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_colorSource) {
1043 fillBlueBuffer<ColorSourceVariant>();
1044}
1045
1046TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_colorSource) {
1047 fillRedTransparentBuffer<ColorSourceVariant>();
1048}
1049
1050TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_colorSource) {
1051 fillBufferPhysicalOffset<ColorSourceVariant>();
1052}
1053
1054TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_colorSource) {
1055 fillBufferCheckersRotate0<ColorSourceVariant>();
1056}
1057
1058TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_colorSource) {
1059 fillBufferCheckersRotate90<ColorSourceVariant>();
1060}
1061
1062TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_colorSource) {
1063 fillBufferCheckersRotate180<ColorSourceVariant>();
1064}
1065
1066TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_colorSource) {
1067 fillBufferCheckersRotate270<ColorSourceVariant>();
1068}
1069
1070TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_colorSource) {
1071 fillBufferLayerTransform<ColorSourceVariant>();
1072}
1073
1074TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_colorSource) {
1075 fillBufferLayerTransform<ColorSourceVariant>();
Alec Mouri6e57f682018-09-29 20:45:08 -07001076}
1077
Alec Mouri7c94edb2018-12-03 21:23:26 -08001078TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_colorSource) {
1079 fillBufferWithRoundedCorners<ColorSourceVariant>();
1080}
1081
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001082TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_colorSource) {
1083 fillBufferAndBlurBackground<ColorSourceVariant>();
1084}
1085
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001086TEST_F(RenderEngineTest, drawLayers_overlayCorners_colorSource) {
1087 overlayCorners<ColorSourceVariant>();
1088}
1089
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001090TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_opaqueBufferSource) {
1091 fillRedBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1092}
1093
1094TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_opaqueBufferSource) {
1095 fillGreenBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1096}
1097
1098TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_opaqueBufferSource) {
1099 fillBlueBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1100}
1101
1102TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_opaqueBufferSource) {
1103 fillRedTransparentBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1104}
1105
1106TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_opaqueBufferSource) {
1107 fillBufferPhysicalOffset<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1108}
1109
1110TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_opaqueBufferSource) {
1111 fillBufferCheckersRotate0<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1112}
1113
1114TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_opaqueBufferSource) {
1115 fillBufferCheckersRotate90<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1116}
1117
1118TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_opaqueBufferSource) {
1119 fillBufferCheckersRotate180<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1120}
1121
1122TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_opaqueBufferSource) {
1123 fillBufferCheckersRotate270<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1124}
1125
1126TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_opaqueBufferSource) {
1127 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1128}
1129
1130TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_opaqueBufferSource) {
1131 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1132}
1133
Alec Mouri7c94edb2018-12-03 21:23:26 -08001134TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_opaqueBufferSource) {
1135 fillBufferWithRoundedCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1136}
1137
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001138TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_opaqueBufferSource) {
1139 fillBufferAndBlurBackground<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1140}
1141
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001142TEST_F(RenderEngineTest, drawLayers_overlayCorners_opaqueBufferSource) {
1143 overlayCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1144}
1145
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001146TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_bufferSource) {
1147 fillRedBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1148}
1149
1150TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_bufferSource) {
1151 fillGreenBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1152}
1153
1154TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_bufferSource) {
1155 fillBlueBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1156}
1157
1158TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_bufferSource) {
1159 fillRedTransparentBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1160}
1161
1162TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_bufferSource) {
1163 fillBufferPhysicalOffset<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1164}
1165
1166TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_bufferSource) {
1167 fillBufferCheckersRotate0<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1168}
1169
1170TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_bufferSource) {
1171 fillBufferCheckersRotate90<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1172}
1173
1174TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_bufferSource) {
1175 fillBufferCheckersRotate180<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1176}
1177
1178TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_bufferSource) {
1179 fillBufferCheckersRotate270<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1180}
1181
1182TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_bufferSource) {
1183 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1184}
1185
1186TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_bufferSource) {
1187 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1188}
1189
Alec Mouri7c94edb2018-12-03 21:23:26 -08001190TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_bufferSource) {
1191 fillBufferWithRoundedCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1192}
1193
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001194TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_bufferSource) {
1195 fillBufferAndBlurBackground<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1196}
1197
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001198TEST_F(RenderEngineTest, drawLayers_overlayCorners_bufferSource) {
1199 overlayCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1200}
1201
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001202TEST_F(RenderEngineTest, drawLayers_fillBufferTextureTransform) {
1203 fillBufferTextureTransform();
1204}
1205
1206TEST_F(RenderEngineTest, drawLayers_fillBuffer_premultipliesAlpha) {
1207 fillBufferWithPremultiplyAlpha();
1208}
1209
1210TEST_F(RenderEngineTest, drawLayers_fillBuffer_withoutPremultiplyingAlpha) {
1211 fillBufferWithoutPremultiplyAlpha();
1212}
1213
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001214TEST_F(RenderEngineTest, drawLayers_clearRegion) {
1215 clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -08001216}
1217
Alec Mourid43ccab2019-03-13 12:23:45 -07001218TEST_F(RenderEngineTest, drawLayers_fillsBufferAndCachesImages) {
1219 renderengine::DisplaySettings settings;
1220 settings.physicalDisplay = fullscreenRect();
1221 settings.clip = fullscreenRect();
1222
Vishnu Nair9b079a22020-01-21 14:36:08 -08001223 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001224
1225 renderengine::LayerSettings layer;
1226 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1227 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1228
Vishnu Nair9b079a22020-01-21 14:36:08 -08001229 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001230 invokeDraw(settings, layers, mBuffer);
1231 uint64_t bufferId = layer.source.buffer.buffer->getId();
1232 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001233 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1234 sRE->unbindExternalTextureBufferForTesting(bufferId);
1235 std::lock_guard<std::mutex> lock(barrier->mutex);
1236 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1237 [&]() REQUIRES(barrier->mutex) {
1238 return barrier->isOpen;
1239 }));
Alec Mourid43ccab2019-03-13 12:23:45 -07001240 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001241 EXPECT_EQ(NO_ERROR, barrier->result);
Alec Mourid43ccab2019-03-13 12:23:45 -07001242}
1243
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001244TEST_F(RenderEngineTest, bindExternalBuffer_withNullBuffer) {
Alec Mourid43ccab2019-03-13 12:23:45 -07001245 status_t result = sRE->bindExternalTextureBuffer(0, nullptr, nullptr);
1246 ASSERT_EQ(BAD_VALUE, result);
1247}
1248
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001249TEST_F(RenderEngineTest, bindExternalBuffer_cachesImages) {
Alec Mourid43ccab2019-03-13 12:23:45 -07001250 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
1251 uint32_t texName;
1252 sRE->genTextures(1, &texName);
1253 mTexNames.push_back(texName);
1254
1255 sRE->bindExternalTextureBuffer(texName, buf, nullptr);
1256 uint64_t bufferId = buf->getId();
1257 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001258 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1259 sRE->unbindExternalTextureBufferForTesting(bufferId);
1260 std::lock_guard<std::mutex> lock(barrier->mutex);
1261 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1262 [&]() REQUIRES(barrier->mutex) {
1263 return barrier->isOpen;
1264 }));
1265 EXPECT_EQ(NO_ERROR, barrier->result);
Alec Mourid43ccab2019-03-13 12:23:45 -07001266 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1267}
1268
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001269TEST_F(RenderEngineTest, cacheExternalBuffer_withNullBuffer) {
Alec Mouri16a99402019-07-29 16:37:30 -07001270 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1271 sRE->cacheExternalTextureBufferForTesting(nullptr);
1272 std::lock_guard<std::mutex> lock(barrier->mutex);
1273 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1274 [&]() REQUIRES(barrier->mutex) {
1275 return barrier->isOpen;
1276 }));
1277 EXPECT_TRUE(barrier->isOpen);
1278 EXPECT_EQ(BAD_VALUE, barrier->result);
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001279}
1280
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001281TEST_F(RenderEngineTest, cacheExternalBuffer_cachesImages) {
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001282 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
1283 uint64_t bufferId = buf->getId();
Alec Mouri16a99402019-07-29 16:37:30 -07001284 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1285 sRE->cacheExternalTextureBufferForTesting(buf);
1286 {
1287 std::lock_guard<std::mutex> lock(barrier->mutex);
1288 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1289 [&]() REQUIRES(barrier->mutex) {
1290 return barrier->isOpen;
1291 }));
1292 EXPECT_EQ(NO_ERROR, barrier->result);
1293 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001294 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001295 barrier = sRE->unbindExternalTextureBufferForTesting(bufferId);
1296 {
1297 std::lock_guard<std::mutex> lock(barrier->mutex);
1298 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1299 [&]() REQUIRES(barrier->mutex) {
1300 return barrier->isOpen;
1301 }));
1302 EXPECT_EQ(NO_ERROR, barrier->result);
1303 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001304 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1305}
1306
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001307TEST_F(RenderEngineTest, drawLayers_fillShadow_casterLayerMinSize) {
1308 const ubyte4 casterColor(255, 0, 0, 255);
1309 const ubyte4 backgroundColor(255, 255, 255, 255);
1310 const float shadowLength = 5.0f;
1311 Rect casterBounds(1, 1);
1312 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1313 renderengine::LayerSettings castingLayer;
1314 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1315 castingLayer.alpha = 1.0f;
1316 renderengine::ShadowSettings settings =
1317 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1318 false /* casterIsTranslucent */);
1319
1320 drawShadow<ColorSourceVariant>(castingLayer, settings, casterColor, backgroundColor);
1321 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1322}
1323
1324TEST_F(RenderEngineTest, drawLayers_fillShadow_casterColorLayer) {
1325 const ubyte4 casterColor(255, 0, 0, 255);
1326 const ubyte4 backgroundColor(255, 255, 255, 255);
1327 const float shadowLength = 5.0f;
1328 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1329 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1330 renderengine::LayerSettings castingLayer;
1331 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1332 castingLayer.alpha = 1.0f;
1333 renderengine::ShadowSettings settings =
1334 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1335 false /* casterIsTranslucent */);
1336
1337 drawShadow<ColorSourceVariant>(castingLayer, settings, casterColor, backgroundColor);
1338 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1339}
1340
1341TEST_F(RenderEngineTest, drawLayers_fillShadow_casterOpaqueBufferLayer) {
1342 const ubyte4 casterColor(255, 0, 0, 255);
1343 const ubyte4 backgroundColor(255, 255, 255, 255);
1344 const float shadowLength = 5.0f;
1345 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1346 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1347 renderengine::LayerSettings castingLayer;
1348 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1349 castingLayer.alpha = 1.0f;
1350 renderengine::ShadowSettings settings =
1351 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1352 false /* casterIsTranslucent */);
1353
1354 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1355 backgroundColor);
1356 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1357}
1358
1359TEST_F(RenderEngineTest, drawLayers_fillShadow_casterWithRoundedCorner) {
1360 const ubyte4 casterColor(255, 0, 0, 255);
1361 const ubyte4 backgroundColor(255, 255, 255, 255);
1362 const float shadowLength = 5.0f;
1363 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1364 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1365 renderengine::LayerSettings castingLayer;
1366 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1367 castingLayer.geometry.roundedCornersRadius = 3.0f;
1368 castingLayer.geometry.roundedCornersCrop = casterBounds.toFloatRect();
1369 castingLayer.alpha = 1.0f;
1370 renderengine::ShadowSettings settings =
1371 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1372 false /* casterIsTranslucent */);
1373
1374 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1375 backgroundColor);
1376 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1377}
1378
1379TEST_F(RenderEngineTest, drawLayers_fillShadow_translucentCasterWithAlpha) {
1380 const ubyte4 casterColor(255, 0, 0, 255);
1381 const ubyte4 backgroundColor(255, 255, 255, 255);
1382 const float shadowLength = 5.0f;
1383 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1384 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1385 renderengine::LayerSettings castingLayer;
1386 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1387 castingLayer.alpha = 0.5f;
1388 renderengine::ShadowSettings settings =
1389 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1390 true /* casterIsTranslucent */);
1391
1392 drawShadow<BufferSourceVariant<RelaxOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1393 backgroundColor);
1394
1395 // verify only the background since the shadow will draw behind the caster
1396 const float shadowInset = settings.length * -1.0f;
1397 const Rect casterWithShadow =
1398 Rect(casterBounds).inset(shadowInset, shadowInset, shadowInset, shadowInset);
1399 const Region backgroundRegion = Region(fullscreenRect()).subtractSelf(casterWithShadow);
1400 expectBufferColor(backgroundRegion, backgroundColor.r, backgroundColor.g, backgroundColor.b,
1401 backgroundColor.a);
1402}
1403
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001404TEST_F(RenderEngineTest, cleanupPostRender_cleansUpOnce) {
1405 renderengine::DisplaySettings settings;
1406 settings.physicalDisplay = fullscreenRect();
1407 settings.clip = fullscreenRect();
1408
1409 std::vector<const renderengine::LayerSettings*> layers;
1410 renderengine::LayerSettings layer;
1411 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1412 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1413 layer.alpha = 1.0;
1414 layers.push_back(&layer);
1415
1416 base::unique_fd fenceOne;
1417 sRE->drawLayers(settings, layers, mBuffer, true, base::unique_fd(), &fenceOne);
1418 base::unique_fd fenceTwo;
1419 sRE->drawLayers(settings, layers, mBuffer, true, std::move(fenceOne), &fenceTwo);
1420
1421 const int fd = fenceTwo.get();
1422 if (fd >= 0) {
1423 sync_wait(fd, -1);
1424 }
1425
1426 // Only cleanup the first time.
1427 EXPECT_TRUE(sRE->cleanupPostRender());
1428 EXPECT_FALSE(sRE->cleanupPostRender());
1429}
1430
Alec Mouri6e57f682018-09-29 20:45:08 -07001431} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001432
1433// TODO(b/129481165): remove the #pragma below and fix conversion issues
1434#pragma clang diagnostic pop // ignored "-Wconversion"