blob: 1edfb615a6b3aaf008a9911bfe697a6d39246801 [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
Alec Mouri16a99402019-07-29 16:37:30 -070025#include <gtest/gtest.h>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080026#include <cutils/properties.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"
Alec Mouri6e57f682018-09-29 20:45:08 -070031
Alec Mouri1089aed2018-10-25 21:33:57 -070032constexpr int DEFAULT_DISPLAY_WIDTH = 128;
33constexpr int DEFAULT_DISPLAY_HEIGHT = 256;
34constexpr int DEFAULT_DISPLAY_OFFSET = 64;
Vishnu Nair16efdbf2019-12-10 11:55:42 -080035constexpr bool WRITE_BUFFER_TO_FILE_ON_FAILURE = false;
Alec Mouri1089aed2018-10-25 21:33:57 -070036
Alec Mouri6e57f682018-09-29 20:45:08 -070037namespace android {
38
Alec Mouri1089aed2018-10-25 21:33:57 -070039struct RenderEngineTest : public ::testing::Test {
Alec Mourid43ccab2019-03-13 12:23:45 -070040 static void SetUpTestSuite() {
Peiyong Lin4137a1d2019-10-09 10:39:09 -070041 sRE = renderengine::gl::GLESRenderEngine::create(
42 renderengine::RenderEngineCreationArgs::Builder()
43 .setPixelFormat(static_cast<int>(ui::PixelFormat::RGBA_8888))
44 .setImageCacheSize(1)
45 .setUseColorManagerment(false)
46 .setEnableProtectedContext(false)
47 .setPrecacheToneMapperShaderOnly(false)
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080048 .setSupportsBackgroundBlur(true)
Peiyong Lin4137a1d2019-10-09 10:39:09 -070049 .setContextPriority(renderengine::RenderEngine::ContextPriority::MEDIUM)
50 .build());
Alec Mourid43ccab2019-03-13 12:23:45 -070051 }
52
53 static void TearDownTestSuite() {
54 // The ordering here is important - sCurrentBuffer must live longer
55 // than RenderEngine to avoid a null reference on tear-down.
56 sRE = nullptr;
57 sCurrentBuffer = nullptr;
58 }
59
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080060 static sp<GraphicBuffer> allocateDefaultBuffer() {
Alec Mouri1089aed2018-10-25 21:33:57 -070061 return new GraphicBuffer(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT,
62 HAL_PIXEL_FORMAT_RGBA_8888, 1,
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080063 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
64 GRALLOC_USAGE_HW_RENDER,
Alec Mouri1089aed2018-10-25 21:33:57 -070065 "output");
Alec Mouri6e57f682018-09-29 20:45:08 -070066 }
67
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080068 // Allocates a 1x1 buffer to fill with a solid color
69 static sp<GraphicBuffer> allocateSourceBuffer(uint32_t width, uint32_t height) {
70 return new GraphicBuffer(width, height, HAL_PIXEL_FORMAT_RGBA_8888, 1,
71 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
72 GRALLOC_USAGE_HW_TEXTURE,
73 "input");
74 }
75
Alec Mouri1089aed2018-10-25 21:33:57 -070076 RenderEngineTest() { mBuffer = allocateDefaultBuffer(); }
77
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080078 ~RenderEngineTest() {
Vishnu Nair16efdbf2019-12-10 11:55:42 -080079 if (WRITE_BUFFER_TO_FILE_ON_FAILURE && ::testing::Test::HasFailure()) {
80 writeBufferToFile("/data/texture_out_");
81 }
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080082 for (uint32_t texName : mTexNames) {
83 sRE->deleteTextures(1, &texName);
84 }
85 }
86
Vishnu Nair16efdbf2019-12-10 11:55:42 -080087 void writeBufferToFile(const char* basename) {
88 std::string filename(basename);
89 filename.append(::testing::UnitTest::GetInstance()->current_test_info()->name());
90 filename.append(".ppm");
91 std::ofstream file(filename.c_str(), std::ios::binary);
92 if (!file.is_open()) {
93 ALOGE("Unable to open file: %s", filename.c_str());
94 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
95 "surfaceflinger to write debug images");
96 return;
97 }
98
Alec Mouri1089aed2018-10-25 21:33:57 -070099 uint8_t* pixels;
100 mBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
101 reinterpret_cast<void**>(&pixels));
102
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800103 file << "P6\n";
104 file << mBuffer->getWidth() << "\n";
105 file << mBuffer->getHeight() << "\n";
106 file << 255 << "\n";
107
108 std::vector<uint8_t> outBuffer(mBuffer->getWidth() * mBuffer->getHeight() * 3);
109 auto outPtr = reinterpret_cast<uint8_t*>(outBuffer.data());
110
111 for (int32_t j = 0; j < mBuffer->getHeight(); j++) {
112 const uint8_t* src = pixels + (mBuffer->getStride() * j) * 4;
113 for (int32_t i = 0; i < mBuffer->getWidth(); i++) {
114 // Only copy R, G and B components
115 outPtr[0] = src[0];
116 outPtr[1] = src[1];
117 outPtr[2] = src[2];
118 outPtr += 3;
119
120 src += 4;
121 }
122 }
123 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
124 mBuffer->unlock();
125 }
126
127 void expectBufferColor(const Region& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
128 size_t c;
129 Rect const* rect = region.getArray(&c);
130 for (size_t i = 0; i < c; i++, rect++) {
131 expectBufferColor(*rect, r, g, b, a);
132 }
133 }
134
135 void expectBufferColor(const Rect& rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a,
136 uint8_t tolerance = 0) {
137 auto colorCompare = [tolerance](const uint8_t* colorA, const uint8_t* colorB) {
138 auto colorBitCompare = [tolerance](uint8_t a, uint8_t b) {
139 uint8_t tmp = a >= b ? a - b : b - a;
140 return tmp <= tolerance;
141 };
142 return std::equal(colorA, colorA + 4, colorB, colorBitCompare);
Alec Mouri1089aed2018-10-25 21:33:57 -0700143 };
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800144
145 expectBufferColor(rect, r, g, b, a, colorCompare);
146 }
147
148 void expectBufferColor(const Rect& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a,
149 std::function<bool(const uint8_t* a, const uint8_t* b)> colorCompare) {
150 uint8_t* pixels;
151 mBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
152 reinterpret_cast<void**>(&pixels));
Alec Mouri1089aed2018-10-25 21:33:57 -0700153 int32_t maxFails = 10;
154 int32_t fails = 0;
155 for (int32_t j = 0; j < region.getHeight(); j++) {
156 const uint8_t* src =
157 pixels + (mBuffer->getStride() * (region.top + j) + region.left) * 4;
158 for (int32_t i = 0; i < region.getWidth(); i++) {
159 const uint8_t expected[4] = {r, g, b, a};
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800160 bool equal = colorCompare(src, expected);
Alec Mouri1089aed2018-10-25 21:33:57 -0700161 EXPECT_TRUE(equal)
162 << "pixel @ (" << region.left + i << ", " << region.top + j << "): "
163 << "expected (" << static_cast<uint32_t>(r) << ", "
164 << static_cast<uint32_t>(g) << ", " << static_cast<uint32_t>(b) << ", "
165 << static_cast<uint32_t>(a) << "), "
166 << "got (" << static_cast<uint32_t>(src[0]) << ", "
167 << static_cast<uint32_t>(src[1]) << ", " << static_cast<uint32_t>(src[2])
168 << ", " << static_cast<uint32_t>(src[3]) << ")";
169 src += 4;
170 if (!equal && ++fails >= maxFails) {
171 break;
172 }
173 }
174 if (fails >= maxFails) {
175 break;
176 }
177 }
178 mBuffer->unlock();
179 }
180
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800181 void expectAlpha(const Rect& rect, uint8_t a) {
182 auto colorCompare = [](const uint8_t* colorA, const uint8_t* colorB) {
183 return colorA[3] == colorB[3];
184 };
185 expectBufferColor(rect, 0.0f /* r */, 0.0f /*g */, 0.0f /* b */, a, colorCompare);
186 }
187
188 void expectShadowColor(const renderengine::LayerSettings& castingLayer,
189 const renderengine::ShadowSettings& shadow, const ubyte4& casterColor,
190 const ubyte4& backgroundColor) {
191 const Rect casterRect(castingLayer.geometry.boundaries);
192 Region casterRegion = Region(casterRect);
193 const float casterCornerRadius = castingLayer.geometry.roundedCornersRadius;
194 if (casterCornerRadius > 0.0f) {
195 // ignore the corners if a corner radius is set
196 Rect cornerRect(casterCornerRadius, casterCornerRadius);
197 casterRegion.subtractSelf(cornerRect.offsetTo(casterRect.left, casterRect.top));
198 casterRegion.subtractSelf(
199 cornerRect.offsetTo(casterRect.right - casterCornerRadius, casterRect.top));
200 casterRegion.subtractSelf(
201 cornerRect.offsetTo(casterRect.left, casterRect.bottom - casterCornerRadius));
202 casterRegion.subtractSelf(cornerRect.offsetTo(casterRect.right - casterCornerRadius,
203 casterRect.bottom - casterCornerRadius));
204 }
205
206 const float shadowInset = shadow.length * -1.0f;
207 const Rect casterWithShadow =
208 Rect(casterRect).inset(shadowInset, shadowInset, shadowInset, shadowInset);
209 const Region shadowRegion = Region(casterWithShadow).subtractSelf(casterRect);
210 const Region backgroundRegion = Region(fullscreenRect()).subtractSelf(casterWithShadow);
211
212 // verify casting layer
213 expectBufferColor(casterRegion, casterColor.r, casterColor.g, casterColor.b, casterColor.a);
214
215 // verify shadows by testing just the alpha since its difficult to validate the shadow color
216 size_t c;
217 Rect const* r = shadowRegion.getArray(&c);
218 for (size_t i = 0; i < c; i++, r++) {
219 expectAlpha(*r, 255);
220 }
221
222 // verify background
223 expectBufferColor(backgroundRegion, backgroundColor.r, backgroundColor.g, backgroundColor.b,
224 backgroundColor.a);
225 }
226
227 static renderengine::ShadowSettings getShadowSettings(const vec2& casterPos, float shadowLength,
228 bool casterIsTranslucent) {
229 renderengine::ShadowSettings shadow;
230 shadow.ambientColor = {0.0f, 0.0f, 0.0f, 0.039f};
231 shadow.spotColor = {0.0f, 0.0f, 0.0f, 0.19f};
232 shadow.lightPos = vec3(casterPos.x, casterPos.y, 0);
233 shadow.lightRadius = 0.0f;
234 shadow.length = shadowLength;
235 shadow.casterIsTranslucent = casterIsTranslucent;
236 return shadow;
237 }
238
Alec Mouri1089aed2018-10-25 21:33:57 -0700239 static Rect fullscreenRect() { return Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT); }
240
241 static Rect offsetRect() {
242 return Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
243 DEFAULT_DISPLAY_HEIGHT);
244 }
245
246 static Rect offsetRectAtZero() {
247 return Rect(DEFAULT_DISPLAY_WIDTH - DEFAULT_DISPLAY_OFFSET,
248 DEFAULT_DISPLAY_HEIGHT - DEFAULT_DISPLAY_OFFSET);
249 }
250
Alec Mourid43ccab2019-03-13 12:23:45 -0700251 void invokeDraw(renderengine::DisplaySettings settings,
Vishnu Nair9b079a22020-01-21 14:36:08 -0800252 std::vector<const renderengine::LayerSettings*> layers,
253 sp<GraphicBuffer> buffer) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700254 base::unique_fd fence;
Ana Krulecfc874ae2020-02-22 15:39:32 -0800255 status_t status =
256 sRE->drawLayers(settings, layers, buffer, true, base::unique_fd(), &fence);
Alec Mourid43ccab2019-03-13 12:23:45 -0700257 sCurrentBuffer = buffer;
Alec Mouri1089aed2018-10-25 21:33:57 -0700258
259 int fd = fence.release();
260 if (fd >= 0) {
261 sync_wait(fd, -1);
262 close(fd);
263 }
264
265 ASSERT_EQ(NO_ERROR, status);
Alec Mourid43ccab2019-03-13 12:23:45 -0700266 if (layers.size() > 0) {
267 ASSERT_TRUE(sRE->isFramebufferImageCachedForTesting(buffer->getId()));
268 }
Alec Mouri1089aed2018-10-25 21:33:57 -0700269 }
270
Alec Mourid43ccab2019-03-13 12:23:45 -0700271 void drawEmptyLayers() {
Alec Mouri6e57f682018-09-29 20:45:08 -0700272 renderengine::DisplaySettings settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800273 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri6e57f682018-09-29 20:45:08 -0700274 // Meaningless buffer since we don't do any drawing
275 sp<GraphicBuffer> buffer = new GraphicBuffer();
Alec Mouri1089aed2018-10-25 21:33:57 -0700276 invokeDraw(settings, layers, buffer);
Alec Mouri6e57f682018-09-29 20:45:08 -0700277 }
278
Alec Mouri1089aed2018-10-25 21:33:57 -0700279 template <typename SourceVariant>
280 void fillBuffer(half r, half g, half b, half a);
281
282 template <typename SourceVariant>
283 void fillRedBuffer();
284
285 template <typename SourceVariant>
286 void fillGreenBuffer();
287
288 template <typename SourceVariant>
289 void fillBlueBuffer();
290
291 template <typename SourceVariant>
292 void fillRedTransparentBuffer();
293
294 template <typename SourceVariant>
295 void fillRedOffsetBuffer();
296
297 template <typename SourceVariant>
298 void fillBufferPhysicalOffset();
299
300 template <typename SourceVariant>
301 void fillBufferCheckers(mat4 transform);
302
303 template <typename SourceVariant>
304 void fillBufferCheckersRotate0();
305
306 template <typename SourceVariant>
307 void fillBufferCheckersRotate90();
308
309 template <typename SourceVariant>
310 void fillBufferCheckersRotate180();
311
312 template <typename SourceVariant>
313 void fillBufferCheckersRotate270();
314
315 template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800316 void fillBufferWithLayerTransform();
317
318 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700319 void fillBufferLayerTransform();
320
321 template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800322 void fillBufferWithColorTransform();
323
324 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700325 void fillBufferColorTransform();
326
Alec Mouri7c94edb2018-12-03 21:23:26 -0800327 template <typename SourceVariant>
328 void fillRedBufferWithRoundedCorners();
329
330 template <typename SourceVariant>
331 void fillBufferWithRoundedCorners();
332
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000333 template <typename SourceVariant>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800334 void fillBufferAndBlurBackground();
335
336 template <typename SourceVariant>
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000337 void overlayCorners();
338
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800339 void fillRedBufferTextureTransform();
340
341 void fillBufferTextureTransform();
342
343 void fillRedBufferWithPremultiplyAlpha();
344
345 void fillBufferWithPremultiplyAlpha();
346
347 void fillRedBufferWithoutPremultiplyAlpha();
348
349 void fillBufferWithoutPremultiplyAlpha();
350
Alec Mouriac335532018-11-12 15:01:33 -0800351 void fillGreenColorBufferThenClearRegion();
352
353 void clearLeftRegion();
354
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000355 void clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -0800356
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800357 template <typename SourceVariant>
358 void drawShadow(const renderengine::LayerSettings& castingLayer,
359 const renderengine::ShadowSettings& shadow, const ubyte4& casterColor,
360 const ubyte4& backgroundColor);
361
Alec Mourid43ccab2019-03-13 12:23:45 -0700362 // Keep around the same renderengine object to save on initialization time.
363 // For now, exercise the GL backend directly so that some caching specifics
364 // can be tested without changing the interface.
365 static std::unique_ptr<renderengine::gl::GLESRenderEngine> sRE;
366 // Dumb hack to avoid NPE in the EGL driver: the GraphicBuffer needs to
367 // be freed *after* RenderEngine is destroyed, so that the EGL image is
368 // destroyed first.
369 static sp<GraphicBuffer> sCurrentBuffer;
Alec Mouri1089aed2018-10-25 21:33:57 -0700370
371 sp<GraphicBuffer> mBuffer;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800372
373 std::vector<uint32_t> mTexNames;
Alec Mouri6e57f682018-09-29 20:45:08 -0700374};
375
Alec Mourid43ccab2019-03-13 12:23:45 -0700376std::unique_ptr<renderengine::gl::GLESRenderEngine> RenderEngineTest::sRE = nullptr;
377sp<GraphicBuffer> RenderEngineTest::sCurrentBuffer = nullptr;
Alec Mouri1089aed2018-10-25 21:33:57 -0700378
379struct ColorSourceVariant {
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800380 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
381 RenderEngineTest* /*fixture*/) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700382 layer.source.solidColor = half3(r, g, b);
383 }
384};
385
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800386struct RelaxOpaqueBufferVariant {
387 static void setOpaqueBit(renderengine::LayerSettings& layer) {
388 layer.source.buffer.isOpaque = false;
389 }
390
391 static uint8_t getAlphaChannel() { return 255; }
392};
393
394struct ForceOpaqueBufferVariant {
395 static void setOpaqueBit(renderengine::LayerSettings& layer) {
396 layer.source.buffer.isOpaque = true;
397 }
398
399 static uint8_t getAlphaChannel() {
400 // The isOpaque bit will override the alpha channel, so this should be
401 // arbitrary.
402 return 10;
403 }
404};
405
406template <typename OpaquenessVariant>
407struct BufferSourceVariant {
408 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
409 RenderEngineTest* fixture) {
410 sp<GraphicBuffer> buf = RenderEngineTest::allocateSourceBuffer(1, 1);
411 uint32_t texName;
Alec Mourid43ccab2019-03-13 12:23:45 -0700412 fixture->sRE->genTextures(1, &texName);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800413 fixture->mTexNames.push_back(texName);
414
415 uint8_t* pixels;
416 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
417 reinterpret_cast<void**>(&pixels));
418
419 for (int32_t j = 0; j < buf->getHeight(); j++) {
420 uint8_t* iter = pixels + (buf->getStride() * j) * 4;
421 for (int32_t i = 0; i < buf->getWidth(); i++) {
422 iter[0] = uint8_t(r * 255);
423 iter[1] = uint8_t(g * 255);
424 iter[2] = uint8_t(b * 255);
425 iter[3] = OpaquenessVariant::getAlphaChannel();
426 iter += 4;
427 }
428 }
429
430 buf->unlock();
431
432 layer.source.buffer.buffer = buf;
433 layer.source.buffer.textureName = texName;
434 OpaquenessVariant::setOpaqueBit(layer);
435 }
436};
437
Alec Mouri1089aed2018-10-25 21:33:57 -0700438template <typename SourceVariant>
439void RenderEngineTest::fillBuffer(half r, half g, half b, half a) {
440 renderengine::DisplaySettings settings;
441 settings.physicalDisplay = fullscreenRect();
442 settings.clip = fullscreenRect();
443
Vishnu Nair9b079a22020-01-21 14:36:08 -0800444 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700445
446 renderengine::LayerSettings layer;
447 layer.geometry.boundaries = fullscreenRect().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800448 SourceVariant::fillColor(layer, r, g, b, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700449 layer.alpha = a;
450
Vishnu Nair9b079a22020-01-21 14:36:08 -0800451 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700452
453 invokeDraw(settings, layers, mBuffer);
454}
455
456template <typename SourceVariant>
457void RenderEngineTest::fillRedBuffer() {
458 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, 1.0f);
459 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
460}
461
462template <typename SourceVariant>
463void RenderEngineTest::fillGreenBuffer() {
464 fillBuffer<SourceVariant>(0.0f, 1.0f, 0.0f, 1.0f);
465 expectBufferColor(fullscreenRect(), 0, 255, 0, 255);
466}
467
468template <typename SourceVariant>
469void RenderEngineTest::fillBlueBuffer() {
470 fillBuffer<SourceVariant>(0.0f, 0.0f, 1.0f, 1.0f);
471 expectBufferColor(fullscreenRect(), 0, 0, 255, 255);
472}
473
474template <typename SourceVariant>
475void RenderEngineTest::fillRedTransparentBuffer() {
476 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, .2f);
477 expectBufferColor(fullscreenRect(), 51, 0, 0, 51);
478}
479
480template <typename SourceVariant>
481void RenderEngineTest::fillRedOffsetBuffer() {
482 renderengine::DisplaySettings settings;
483 settings.physicalDisplay = offsetRect();
484 settings.clip = offsetRectAtZero();
485
Vishnu Nair9b079a22020-01-21 14:36:08 -0800486 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700487
488 renderengine::LayerSettings layer;
489 layer.geometry.boundaries = offsetRectAtZero().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800490 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700491 layer.alpha = 1.0f;
492
Vishnu Nair9b079a22020-01-21 14:36:08 -0800493 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700494 invokeDraw(settings, layers, mBuffer);
495}
496
497template <typename SourceVariant>
498void RenderEngineTest::fillBufferPhysicalOffset() {
499 fillRedOffsetBuffer<SourceVariant>();
500
501 expectBufferColor(Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
502 DEFAULT_DISPLAY_HEIGHT),
503 255, 0, 0, 255);
504 Rect offsetRegionLeft(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_HEIGHT);
505 Rect offsetRegionTop(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_OFFSET);
506
507 expectBufferColor(offsetRegionLeft, 0, 0, 0, 0);
508 expectBufferColor(offsetRegionTop, 0, 0, 0, 0);
509}
510
511template <typename SourceVariant>
512void RenderEngineTest::fillBufferCheckers(mat4 transform) {
513 renderengine::DisplaySettings settings;
514 settings.physicalDisplay = fullscreenRect();
515 // Here logical space is 2x2
516 settings.clip = Rect(2, 2);
517 settings.globalTransform = transform;
518
Vishnu Nair9b079a22020-01-21 14:36:08 -0800519 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700520
521 renderengine::LayerSettings layerOne;
522 Rect rectOne(0, 0, 1, 1);
523 layerOne.geometry.boundaries = rectOne.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800524 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700525 layerOne.alpha = 1.0f;
526
527 renderengine::LayerSettings layerTwo;
528 Rect rectTwo(0, 1, 1, 2);
529 layerTwo.geometry.boundaries = rectTwo.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800530 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700531 layerTwo.alpha = 1.0f;
532
533 renderengine::LayerSettings layerThree;
534 Rect rectThree(1, 0, 2, 1);
535 layerThree.geometry.boundaries = rectThree.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800536 SourceVariant::fillColor(layerThree, 0.0f, 0.0f, 1.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700537 layerThree.alpha = 1.0f;
538
Vishnu Nair9b079a22020-01-21 14:36:08 -0800539 layers.push_back(&layerOne);
540 layers.push_back(&layerTwo);
541 layers.push_back(&layerThree);
Alec Mouri1089aed2018-10-25 21:33:57 -0700542
543 invokeDraw(settings, layers, mBuffer);
544}
545
546template <typename SourceVariant>
547void RenderEngineTest::fillBufferCheckersRotate0() {
548 fillBufferCheckers<SourceVariant>(mat4());
549 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 255, 0, 0,
550 255);
551 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
552 DEFAULT_DISPLAY_HEIGHT / 2),
553 0, 0, 255, 255);
554 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
555 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
556 0, 0, 0, 0);
557 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
558 DEFAULT_DISPLAY_HEIGHT),
559 0, 255, 0, 255);
560}
561
562template <typename SourceVariant>
563void RenderEngineTest::fillBufferCheckersRotate90() {
564 mat4 matrix = mat4(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 1);
565 fillBufferCheckers<SourceVariant>(matrix);
566 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 255, 0,
567 255);
568 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
569 DEFAULT_DISPLAY_HEIGHT / 2),
570 255, 0, 0, 255);
571 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
572 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
573 0, 0, 255, 255);
574 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
575 DEFAULT_DISPLAY_HEIGHT),
576 0, 0, 0, 0);
577}
578
579template <typename SourceVariant>
580void RenderEngineTest::fillBufferCheckersRotate180() {
581 mat4 matrix = mat4(-1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 2, 2, 0, 1);
582 fillBufferCheckers<SourceVariant>(matrix);
583 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() {
598 mat4 matrix = mat4(0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 1);
599 fillBufferCheckers<SourceVariant>(matrix);
600 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();
922 expectBufferColor(fullscreenRect(), 128, 0, 0, 64, 1);
923}
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);
930 settings.globalTransform = mat4::scale(vec4(2, 4, 0, 1));
931 settings.clearRegion = Region(Rect(1, 1));
Vishnu Nair9b079a22020-01-21 14:36:08 -0800932 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouriac335532018-11-12 15:01:33 -0800933 // dummy layer, without bounds should not render anything
934 renderengine::LayerSettings layer;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800935 layers.push_back(&layer);
Alec Mouriac335532018-11-12 15:01:33 -0800936 invokeDraw(settings, layers, mBuffer);
937}
938
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000939void RenderEngineTest::clearRegion() {
Alec Mouriac335532018-11-12 15:01:33 -0800940 // Reuse mBuffer
941 clearLeftRegion();
942 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 255);
943 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
944 DEFAULT_DISPLAY_HEIGHT),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000945 0, 0, 0, 0);
Alec Mouriac335532018-11-12 15:01:33 -0800946}
947
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800948template <typename SourceVariant>
949void RenderEngineTest::drawShadow(const renderengine::LayerSettings& castingLayer,
950 const renderengine::ShadowSettings& shadow,
951 const ubyte4& casterColor, const ubyte4& backgroundColor) {
952 renderengine::DisplaySettings settings;
953 settings.physicalDisplay = fullscreenRect();
954 settings.clip = fullscreenRect();
955
Vishnu Nair9b079a22020-01-21 14:36:08 -0800956 std::vector<const renderengine::LayerSettings*> layers;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800957
958 // add background layer
959 renderengine::LayerSettings bgLayer;
960 bgLayer.geometry.boundaries = fullscreenRect().toFloatRect();
961 ColorSourceVariant::fillColor(bgLayer, backgroundColor.r / 255.0f, backgroundColor.g / 255.0f,
962 backgroundColor.b / 255.0f, this);
963 bgLayer.alpha = backgroundColor.a / 255.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800964 layers.push_back(&bgLayer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800965
966 // add shadow layer
967 renderengine::LayerSettings shadowLayer;
968 shadowLayer.geometry.boundaries = castingLayer.geometry.boundaries;
969 shadowLayer.alpha = castingLayer.alpha;
970 shadowLayer.shadow = shadow;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800971 layers.push_back(&shadowLayer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800972
973 // add layer casting the shadow
974 renderengine::LayerSettings layer = castingLayer;
975 SourceVariant::fillColor(layer, casterColor.r / 255.0f, casterColor.g / 255.0f,
976 casterColor.b / 255.0f, this);
Vishnu Nair9b079a22020-01-21 14:36:08 -0800977 layers.push_back(&layer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800978
979 invokeDraw(settings, layers, mBuffer);
980}
981
Alec Mouri1089aed2018-10-25 21:33:57 -0700982TEST_F(RenderEngineTest, drawLayers_noLayersToDraw) {
983 drawEmptyLayers();
984}
985
Alec Mourid43ccab2019-03-13 12:23:45 -0700986TEST_F(RenderEngineTest, drawLayers_nullOutputBuffer) {
987 renderengine::DisplaySettings settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800988 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -0700989 renderengine::LayerSettings layer;
990 layer.geometry.boundaries = fullscreenRect().toFloatRect();
991 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Vishnu Nair9b079a22020-01-21 14:36:08 -0800992 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -0700993 base::unique_fd fence;
Alec Mourife0d72b2019-03-21 14:05:56 -0700994 status_t status = sRE->drawLayers(settings, layers, nullptr, true, base::unique_fd(), &fence);
Alec Mourid43ccab2019-03-13 12:23:45 -0700995
996 ASSERT_EQ(BAD_VALUE, status);
997}
998
999TEST_F(RenderEngineTest, drawLayers_nullOutputFence) {
1000 renderengine::DisplaySettings settings;
1001 settings.physicalDisplay = fullscreenRect();
1002 settings.clip = fullscreenRect();
1003
Vishnu Nair9b079a22020-01-21 14:36:08 -08001004 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001005 renderengine::LayerSettings layer;
1006 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1007 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1008 layer.alpha = 1.0;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001009 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001010
Ana Krulecfc874ae2020-02-22 15:39:32 -08001011 status_t status = sRE->drawLayers(settings, layers, mBuffer, true, base::unique_fd(), nullptr);
Alec Mourid43ccab2019-03-13 12:23:45 -07001012 sCurrentBuffer = mBuffer;
1013 ASSERT_EQ(NO_ERROR, status);
1014 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
1015}
1016
Alec Mourife0d72b2019-03-21 14:05:56 -07001017TEST_F(RenderEngineTest, drawLayers_doesNotCacheFramebuffer) {
1018 renderengine::DisplaySettings settings;
1019 settings.physicalDisplay = fullscreenRect();
1020 settings.clip = fullscreenRect();
1021
Vishnu Nair9b079a22020-01-21 14:36:08 -08001022 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourife0d72b2019-03-21 14:05:56 -07001023 renderengine::LayerSettings layer;
1024 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1025 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1026 layer.alpha = 1.0;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001027 layers.push_back(&layer);
Alec Mourife0d72b2019-03-21 14:05:56 -07001028
Ana Krulecfc874ae2020-02-22 15:39:32 -08001029 status_t status = sRE->drawLayers(settings, layers, mBuffer, false, base::unique_fd(), nullptr);
Alec Mourife0d72b2019-03-21 14:05:56 -07001030 sCurrentBuffer = mBuffer;
1031 ASSERT_EQ(NO_ERROR, status);
1032 ASSERT_FALSE(sRE->isFramebufferImageCachedForTesting(mBuffer->getId()));
1033 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
1034}
1035
Alec Mouri1089aed2018-10-25 21:33:57 -07001036TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_colorSource) {
1037 fillRedBuffer<ColorSourceVariant>();
1038}
1039
1040TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_colorSource) {
1041 fillGreenBuffer<ColorSourceVariant>();
1042}
1043
1044TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_colorSource) {
1045 fillBlueBuffer<ColorSourceVariant>();
1046}
1047
1048TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_colorSource) {
1049 fillRedTransparentBuffer<ColorSourceVariant>();
1050}
1051
1052TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_colorSource) {
1053 fillBufferPhysicalOffset<ColorSourceVariant>();
1054}
1055
1056TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_colorSource) {
1057 fillBufferCheckersRotate0<ColorSourceVariant>();
1058}
1059
1060TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_colorSource) {
1061 fillBufferCheckersRotate90<ColorSourceVariant>();
1062}
1063
1064TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_colorSource) {
1065 fillBufferCheckersRotate180<ColorSourceVariant>();
1066}
1067
1068TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_colorSource) {
1069 fillBufferCheckersRotate270<ColorSourceVariant>();
1070}
1071
1072TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_colorSource) {
1073 fillBufferLayerTransform<ColorSourceVariant>();
1074}
1075
1076TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_colorSource) {
1077 fillBufferLayerTransform<ColorSourceVariant>();
Alec Mouri6e57f682018-09-29 20:45:08 -07001078}
1079
Alec Mouri7c94edb2018-12-03 21:23:26 -08001080TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_colorSource) {
1081 fillBufferWithRoundedCorners<ColorSourceVariant>();
1082}
1083
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001084TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_colorSource) {
1085 fillBufferAndBlurBackground<ColorSourceVariant>();
1086}
1087
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001088TEST_F(RenderEngineTest, drawLayers_overlayCorners_colorSource) {
1089 overlayCorners<ColorSourceVariant>();
1090}
1091
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001092TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_opaqueBufferSource) {
1093 fillRedBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1094}
1095
1096TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_opaqueBufferSource) {
1097 fillGreenBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1098}
1099
1100TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_opaqueBufferSource) {
1101 fillBlueBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1102}
1103
1104TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_opaqueBufferSource) {
1105 fillRedTransparentBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1106}
1107
1108TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_opaqueBufferSource) {
1109 fillBufferPhysicalOffset<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1110}
1111
1112TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_opaqueBufferSource) {
1113 fillBufferCheckersRotate0<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1114}
1115
1116TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_opaqueBufferSource) {
1117 fillBufferCheckersRotate90<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1118}
1119
1120TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_opaqueBufferSource) {
1121 fillBufferCheckersRotate180<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1122}
1123
1124TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_opaqueBufferSource) {
1125 fillBufferCheckersRotate270<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1126}
1127
1128TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_opaqueBufferSource) {
1129 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1130}
1131
1132TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_opaqueBufferSource) {
1133 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1134}
1135
Alec Mouri7c94edb2018-12-03 21:23:26 -08001136TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_opaqueBufferSource) {
1137 fillBufferWithRoundedCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1138}
1139
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001140TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_opaqueBufferSource) {
1141 fillBufferAndBlurBackground<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1142}
1143
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001144TEST_F(RenderEngineTest, drawLayers_overlayCorners_opaqueBufferSource) {
1145 overlayCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1146}
1147
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001148TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_bufferSource) {
1149 fillRedBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1150}
1151
1152TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_bufferSource) {
1153 fillGreenBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1154}
1155
1156TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_bufferSource) {
1157 fillBlueBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1158}
1159
1160TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_bufferSource) {
1161 fillRedTransparentBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1162}
1163
1164TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_bufferSource) {
1165 fillBufferPhysicalOffset<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1166}
1167
1168TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_bufferSource) {
1169 fillBufferCheckersRotate0<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1170}
1171
1172TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_bufferSource) {
1173 fillBufferCheckersRotate90<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1174}
1175
1176TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_bufferSource) {
1177 fillBufferCheckersRotate180<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1178}
1179
1180TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_bufferSource) {
1181 fillBufferCheckersRotate270<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1182}
1183
1184TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_bufferSource) {
1185 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1186}
1187
1188TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_bufferSource) {
1189 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1190}
1191
Alec Mouri7c94edb2018-12-03 21:23:26 -08001192TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_bufferSource) {
1193 fillBufferWithRoundedCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1194}
1195
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001196TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_bufferSource) {
1197 fillBufferAndBlurBackground<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1198}
1199
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001200TEST_F(RenderEngineTest, drawLayers_overlayCorners_bufferSource) {
1201 overlayCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1202}
1203
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001204TEST_F(RenderEngineTest, drawLayers_fillBufferTextureTransform) {
1205 fillBufferTextureTransform();
1206}
1207
1208TEST_F(RenderEngineTest, drawLayers_fillBuffer_premultipliesAlpha) {
1209 fillBufferWithPremultiplyAlpha();
1210}
1211
1212TEST_F(RenderEngineTest, drawLayers_fillBuffer_withoutPremultiplyingAlpha) {
1213 fillBufferWithoutPremultiplyAlpha();
1214}
1215
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001216TEST_F(RenderEngineTest, drawLayers_clearRegion) {
1217 clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -08001218}
1219
Alec Mourid43ccab2019-03-13 12:23:45 -07001220TEST_F(RenderEngineTest, drawLayers_fillsBufferAndCachesImages) {
1221 renderengine::DisplaySettings settings;
1222 settings.physicalDisplay = fullscreenRect();
1223 settings.clip = fullscreenRect();
1224
Vishnu Nair9b079a22020-01-21 14:36:08 -08001225 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001226
1227 renderengine::LayerSettings layer;
1228 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1229 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1230
Vishnu Nair9b079a22020-01-21 14:36:08 -08001231 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001232 invokeDraw(settings, layers, mBuffer);
1233 uint64_t bufferId = layer.source.buffer.buffer->getId();
1234 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001235 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1236 sRE->unbindExternalTextureBufferForTesting(bufferId);
1237 std::lock_guard<std::mutex> lock(barrier->mutex);
1238 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1239 [&]() REQUIRES(barrier->mutex) {
1240 return barrier->isOpen;
1241 }));
Alec Mourid43ccab2019-03-13 12:23:45 -07001242 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001243 EXPECT_EQ(NO_ERROR, barrier->result);
Alec Mourid43ccab2019-03-13 12:23:45 -07001244}
1245
1246TEST_F(RenderEngineTest, drawLayers_bindExternalBufferWithNullBuffer) {
1247 status_t result = sRE->bindExternalTextureBuffer(0, nullptr, nullptr);
1248 ASSERT_EQ(BAD_VALUE, result);
1249}
1250
1251TEST_F(RenderEngineTest, drawLayers_bindExternalBufferCachesImages) {
1252 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
1253 uint32_t texName;
1254 sRE->genTextures(1, &texName);
1255 mTexNames.push_back(texName);
1256
1257 sRE->bindExternalTextureBuffer(texName, buf, nullptr);
1258 uint64_t bufferId = buf->getId();
1259 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001260 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1261 sRE->unbindExternalTextureBufferForTesting(bufferId);
1262 std::lock_guard<std::mutex> lock(barrier->mutex);
1263 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1264 [&]() REQUIRES(barrier->mutex) {
1265 return barrier->isOpen;
1266 }));
1267 EXPECT_EQ(NO_ERROR, barrier->result);
Alec Mourid43ccab2019-03-13 12:23:45 -07001268 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1269}
1270
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001271TEST_F(RenderEngineTest, drawLayers_cacheExternalBufferWithNullBuffer) {
Alec Mouri16a99402019-07-29 16:37:30 -07001272 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1273 sRE->cacheExternalTextureBufferForTesting(nullptr);
1274 std::lock_guard<std::mutex> lock(barrier->mutex);
1275 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1276 [&]() REQUIRES(barrier->mutex) {
1277 return barrier->isOpen;
1278 }));
1279 EXPECT_TRUE(barrier->isOpen);
1280 EXPECT_EQ(BAD_VALUE, barrier->result);
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001281}
1282
1283TEST_F(RenderEngineTest, drawLayers_cacheExternalBufferCachesImages) {
1284 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
1285 uint64_t bufferId = buf->getId();
Alec Mouri16a99402019-07-29 16:37:30 -07001286 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1287 sRE->cacheExternalTextureBufferForTesting(buf);
1288 {
1289 std::lock_guard<std::mutex> lock(barrier->mutex);
1290 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1291 [&]() REQUIRES(barrier->mutex) {
1292 return barrier->isOpen;
1293 }));
1294 EXPECT_EQ(NO_ERROR, barrier->result);
1295 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001296 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001297 barrier = sRE->unbindExternalTextureBufferForTesting(bufferId);
1298 {
1299 std::lock_guard<std::mutex> lock(barrier->mutex);
1300 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1301 [&]() REQUIRES(barrier->mutex) {
1302 return barrier->isOpen;
1303 }));
1304 EXPECT_EQ(NO_ERROR, barrier->result);
1305 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001306 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1307}
1308
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001309TEST_F(RenderEngineTest, drawLayers_fillShadow_casterLayerMinSize) {
1310 const ubyte4 casterColor(255, 0, 0, 255);
1311 const ubyte4 backgroundColor(255, 255, 255, 255);
1312 const float shadowLength = 5.0f;
1313 Rect casterBounds(1, 1);
1314 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1315 renderengine::LayerSettings castingLayer;
1316 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1317 castingLayer.alpha = 1.0f;
1318 renderengine::ShadowSettings settings =
1319 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1320 false /* casterIsTranslucent */);
1321
1322 drawShadow<ColorSourceVariant>(castingLayer, settings, casterColor, backgroundColor);
1323 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1324}
1325
1326TEST_F(RenderEngineTest, drawLayers_fillShadow_casterColorLayer) {
1327 const ubyte4 casterColor(255, 0, 0, 255);
1328 const ubyte4 backgroundColor(255, 255, 255, 255);
1329 const float shadowLength = 5.0f;
1330 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1331 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1332 renderengine::LayerSettings castingLayer;
1333 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1334 castingLayer.alpha = 1.0f;
1335 renderengine::ShadowSettings settings =
1336 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1337 false /* casterIsTranslucent */);
1338
1339 drawShadow<ColorSourceVariant>(castingLayer, settings, casterColor, backgroundColor);
1340 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1341}
1342
1343TEST_F(RenderEngineTest, drawLayers_fillShadow_casterOpaqueBufferLayer) {
1344 const ubyte4 casterColor(255, 0, 0, 255);
1345 const ubyte4 backgroundColor(255, 255, 255, 255);
1346 const float shadowLength = 5.0f;
1347 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1348 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1349 renderengine::LayerSettings castingLayer;
1350 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1351 castingLayer.alpha = 1.0f;
1352 renderengine::ShadowSettings settings =
1353 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1354 false /* casterIsTranslucent */);
1355
1356 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1357 backgroundColor);
1358 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1359}
1360
1361TEST_F(RenderEngineTest, drawLayers_fillShadow_casterWithRoundedCorner) {
1362 const ubyte4 casterColor(255, 0, 0, 255);
1363 const ubyte4 backgroundColor(255, 255, 255, 255);
1364 const float shadowLength = 5.0f;
1365 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1366 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1367 renderengine::LayerSettings castingLayer;
1368 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1369 castingLayer.geometry.roundedCornersRadius = 3.0f;
1370 castingLayer.geometry.roundedCornersCrop = casterBounds.toFloatRect();
1371 castingLayer.alpha = 1.0f;
1372 renderengine::ShadowSettings settings =
1373 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1374 false /* casterIsTranslucent */);
1375
1376 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1377 backgroundColor);
1378 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1379}
1380
1381TEST_F(RenderEngineTest, drawLayers_fillShadow_translucentCasterWithAlpha) {
1382 const ubyte4 casterColor(255, 0, 0, 255);
1383 const ubyte4 backgroundColor(255, 255, 255, 255);
1384 const float shadowLength = 5.0f;
1385 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1386 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1387 renderengine::LayerSettings castingLayer;
1388 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1389 castingLayer.alpha = 0.5f;
1390 renderengine::ShadowSettings settings =
1391 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1392 true /* casterIsTranslucent */);
1393
1394 drawShadow<BufferSourceVariant<RelaxOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1395 backgroundColor);
1396
1397 // verify only the background since the shadow will draw behind the caster
1398 const float shadowInset = settings.length * -1.0f;
1399 const Rect casterWithShadow =
1400 Rect(casterBounds).inset(shadowInset, shadowInset, shadowInset, shadowInset);
1401 const Region backgroundRegion = Region(fullscreenRect()).subtractSelf(casterWithShadow);
1402 expectBufferColor(backgroundRegion, backgroundColor.r, backgroundColor.g, backgroundColor.b,
1403 backgroundColor.a);
1404}
1405
Alec Mouri6e57f682018-09-29 20:45:08 -07001406} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001407
1408// TODO(b/129481165): remove the #pragma below and fix conversion issues
1409#pragma clang diagnostic pop // ignored "-Wconversion"