blob: e676740c6a5705f4ebe1514b35205255669580f3 [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,
252 std::vector<renderengine::LayerSettings> layers, sp<GraphicBuffer> buffer) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700253 base::unique_fd fence;
Alec Mourife0d72b2019-03-21 14:05:56 -0700254 status_t status = sRE->drawLayers(settings, layers, buffer->getNativeBuffer(), true,
Alec Mouri6338c9d2019-02-07 16:57:51 -0800255 base::unique_fd(), &fence);
Alec Mourid43ccab2019-03-13 12:23:45 -0700256 sCurrentBuffer = buffer;
Alec Mouri1089aed2018-10-25 21:33:57 -0700257
258 int fd = fence.release();
259 if (fd >= 0) {
260 sync_wait(fd, -1);
261 close(fd);
262 }
263
264 ASSERT_EQ(NO_ERROR, status);
Alec Mourid43ccab2019-03-13 12:23:45 -0700265 if (layers.size() > 0) {
266 ASSERT_TRUE(sRE->isFramebufferImageCachedForTesting(buffer->getId()));
267 }
Alec Mouri1089aed2018-10-25 21:33:57 -0700268 }
269
Alec Mourid43ccab2019-03-13 12:23:45 -0700270 void drawEmptyLayers() {
Alec Mouri6e57f682018-09-29 20:45:08 -0700271 renderengine::DisplaySettings settings;
272 std::vector<renderengine::LayerSettings> layers;
273 // Meaningless buffer since we don't do any drawing
274 sp<GraphicBuffer> buffer = new GraphicBuffer();
Alec Mouri1089aed2018-10-25 21:33:57 -0700275 invokeDraw(settings, layers, buffer);
Alec Mouri6e57f682018-09-29 20:45:08 -0700276 }
277
Alec Mouri1089aed2018-10-25 21:33:57 -0700278 template <typename SourceVariant>
279 void fillBuffer(half r, half g, half b, half a);
280
281 template <typename SourceVariant>
282 void fillRedBuffer();
283
284 template <typename SourceVariant>
285 void fillGreenBuffer();
286
287 template <typename SourceVariant>
288 void fillBlueBuffer();
289
290 template <typename SourceVariant>
291 void fillRedTransparentBuffer();
292
293 template <typename SourceVariant>
294 void fillRedOffsetBuffer();
295
296 template <typename SourceVariant>
297 void fillBufferPhysicalOffset();
298
299 template <typename SourceVariant>
300 void fillBufferCheckers(mat4 transform);
301
302 template <typename SourceVariant>
303 void fillBufferCheckersRotate0();
304
305 template <typename SourceVariant>
306 void fillBufferCheckersRotate90();
307
308 template <typename SourceVariant>
309 void fillBufferCheckersRotate180();
310
311 template <typename SourceVariant>
312 void fillBufferCheckersRotate270();
313
314 template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800315 void fillBufferWithLayerTransform();
316
317 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700318 void fillBufferLayerTransform();
319
320 template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800321 void fillBufferWithColorTransform();
322
323 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700324 void fillBufferColorTransform();
325
Alec Mouri7c94edb2018-12-03 21:23:26 -0800326 template <typename SourceVariant>
327 void fillRedBufferWithRoundedCorners();
328
329 template <typename SourceVariant>
330 void fillBufferWithRoundedCorners();
331
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000332 template <typename SourceVariant>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800333 void fillBufferAndBlurBackground();
334
335 template <typename SourceVariant>
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000336 void overlayCorners();
337
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800338 void fillRedBufferTextureTransform();
339
340 void fillBufferTextureTransform();
341
342 void fillRedBufferWithPremultiplyAlpha();
343
344 void fillBufferWithPremultiplyAlpha();
345
346 void fillRedBufferWithoutPremultiplyAlpha();
347
348 void fillBufferWithoutPremultiplyAlpha();
349
Alec Mouriac335532018-11-12 15:01:33 -0800350 void fillGreenColorBufferThenClearRegion();
351
352 void clearLeftRegion();
353
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000354 void clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -0800355
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800356 template <typename SourceVariant>
357 void drawShadow(const renderengine::LayerSettings& castingLayer,
358 const renderengine::ShadowSettings& shadow, const ubyte4& casterColor,
359 const ubyte4& backgroundColor);
360
Alec Mourid43ccab2019-03-13 12:23:45 -0700361 // Keep around the same renderengine object to save on initialization time.
362 // For now, exercise the GL backend directly so that some caching specifics
363 // can be tested without changing the interface.
364 static std::unique_ptr<renderengine::gl::GLESRenderEngine> sRE;
365 // Dumb hack to avoid NPE in the EGL driver: the GraphicBuffer needs to
366 // be freed *after* RenderEngine is destroyed, so that the EGL image is
367 // destroyed first.
368 static sp<GraphicBuffer> sCurrentBuffer;
Alec Mouri1089aed2018-10-25 21:33:57 -0700369
370 sp<GraphicBuffer> mBuffer;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800371
372 std::vector<uint32_t> mTexNames;
Alec Mouri6e57f682018-09-29 20:45:08 -0700373};
374
Alec Mourid43ccab2019-03-13 12:23:45 -0700375std::unique_ptr<renderengine::gl::GLESRenderEngine> RenderEngineTest::sRE = nullptr;
376sp<GraphicBuffer> RenderEngineTest::sCurrentBuffer = nullptr;
Alec Mouri1089aed2018-10-25 21:33:57 -0700377
378struct ColorSourceVariant {
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800379 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
380 RenderEngineTest* /*fixture*/) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700381 layer.source.solidColor = half3(r, g, b);
382 }
383};
384
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800385struct RelaxOpaqueBufferVariant {
386 static void setOpaqueBit(renderengine::LayerSettings& layer) {
387 layer.source.buffer.isOpaque = false;
388 }
389
390 static uint8_t getAlphaChannel() { return 255; }
391};
392
393struct ForceOpaqueBufferVariant {
394 static void setOpaqueBit(renderengine::LayerSettings& layer) {
395 layer.source.buffer.isOpaque = true;
396 }
397
398 static uint8_t getAlphaChannel() {
399 // The isOpaque bit will override the alpha channel, so this should be
400 // arbitrary.
401 return 10;
402 }
403};
404
405template <typename OpaquenessVariant>
406struct BufferSourceVariant {
407 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
408 RenderEngineTest* fixture) {
409 sp<GraphicBuffer> buf = RenderEngineTest::allocateSourceBuffer(1, 1);
410 uint32_t texName;
Alec Mourid43ccab2019-03-13 12:23:45 -0700411 fixture->sRE->genTextures(1, &texName);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800412 fixture->mTexNames.push_back(texName);
413
414 uint8_t* pixels;
415 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
416 reinterpret_cast<void**>(&pixels));
417
418 for (int32_t j = 0; j < buf->getHeight(); j++) {
419 uint8_t* iter = pixels + (buf->getStride() * j) * 4;
420 for (int32_t i = 0; i < buf->getWidth(); i++) {
421 iter[0] = uint8_t(r * 255);
422 iter[1] = uint8_t(g * 255);
423 iter[2] = uint8_t(b * 255);
424 iter[3] = OpaquenessVariant::getAlphaChannel();
425 iter += 4;
426 }
427 }
428
429 buf->unlock();
430
431 layer.source.buffer.buffer = buf;
432 layer.source.buffer.textureName = texName;
433 OpaquenessVariant::setOpaqueBit(layer);
434 }
435};
436
Alec Mouri1089aed2018-10-25 21:33:57 -0700437template <typename SourceVariant>
438void RenderEngineTest::fillBuffer(half r, half g, half b, half a) {
439 renderengine::DisplaySettings settings;
440 settings.physicalDisplay = fullscreenRect();
441 settings.clip = fullscreenRect();
442
443 std::vector<renderengine::LayerSettings> layers;
444
445 renderengine::LayerSettings layer;
446 layer.geometry.boundaries = fullscreenRect().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800447 SourceVariant::fillColor(layer, r, g, b, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700448 layer.alpha = a;
449
450 layers.push_back(layer);
451
452 invokeDraw(settings, layers, mBuffer);
453}
454
455template <typename SourceVariant>
456void RenderEngineTest::fillRedBuffer() {
457 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, 1.0f);
458 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
459}
460
461template <typename SourceVariant>
462void RenderEngineTest::fillGreenBuffer() {
463 fillBuffer<SourceVariant>(0.0f, 1.0f, 0.0f, 1.0f);
464 expectBufferColor(fullscreenRect(), 0, 255, 0, 255);
465}
466
467template <typename SourceVariant>
468void RenderEngineTest::fillBlueBuffer() {
469 fillBuffer<SourceVariant>(0.0f, 0.0f, 1.0f, 1.0f);
470 expectBufferColor(fullscreenRect(), 0, 0, 255, 255);
471}
472
473template <typename SourceVariant>
474void RenderEngineTest::fillRedTransparentBuffer() {
475 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, .2f);
476 expectBufferColor(fullscreenRect(), 51, 0, 0, 51);
477}
478
479template <typename SourceVariant>
480void RenderEngineTest::fillRedOffsetBuffer() {
481 renderengine::DisplaySettings settings;
482 settings.physicalDisplay = offsetRect();
483 settings.clip = offsetRectAtZero();
484
485 std::vector<renderengine::LayerSettings> layers;
486
487 renderengine::LayerSettings layer;
488 layer.geometry.boundaries = offsetRectAtZero().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800489 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700490 layer.alpha = 1.0f;
491
492 layers.push_back(layer);
493 invokeDraw(settings, layers, mBuffer);
494}
495
496template <typename SourceVariant>
497void RenderEngineTest::fillBufferPhysicalOffset() {
498 fillRedOffsetBuffer<SourceVariant>();
499
500 expectBufferColor(Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
501 DEFAULT_DISPLAY_HEIGHT),
502 255, 0, 0, 255);
503 Rect offsetRegionLeft(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_HEIGHT);
504 Rect offsetRegionTop(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_OFFSET);
505
506 expectBufferColor(offsetRegionLeft, 0, 0, 0, 0);
507 expectBufferColor(offsetRegionTop, 0, 0, 0, 0);
508}
509
510template <typename SourceVariant>
511void RenderEngineTest::fillBufferCheckers(mat4 transform) {
512 renderengine::DisplaySettings settings;
513 settings.physicalDisplay = fullscreenRect();
514 // Here logical space is 2x2
515 settings.clip = Rect(2, 2);
516 settings.globalTransform = transform;
517
518 std::vector<renderengine::LayerSettings> layers;
519
520 renderengine::LayerSettings layerOne;
521 Rect rectOne(0, 0, 1, 1);
522 layerOne.geometry.boundaries = rectOne.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800523 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700524 layerOne.alpha = 1.0f;
525
526 renderengine::LayerSettings layerTwo;
527 Rect rectTwo(0, 1, 1, 2);
528 layerTwo.geometry.boundaries = rectTwo.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800529 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700530 layerTwo.alpha = 1.0f;
531
532 renderengine::LayerSettings layerThree;
533 Rect rectThree(1, 0, 2, 1);
534 layerThree.geometry.boundaries = rectThree.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800535 SourceVariant::fillColor(layerThree, 0.0f, 0.0f, 1.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700536 layerThree.alpha = 1.0f;
537
538 layers.push_back(layerOne);
539 layers.push_back(layerTwo);
540 layers.push_back(layerThree);
541
542 invokeDraw(settings, layers, mBuffer);
543}
544
545template <typename SourceVariant>
546void RenderEngineTest::fillBufferCheckersRotate0() {
547 fillBufferCheckers<SourceVariant>(mat4());
548 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 255, 0, 0,
549 255);
550 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
551 DEFAULT_DISPLAY_HEIGHT / 2),
552 0, 0, 255, 255);
553 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
554 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
555 0, 0, 0, 0);
556 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
557 DEFAULT_DISPLAY_HEIGHT),
558 0, 255, 0, 255);
559}
560
561template <typename SourceVariant>
562void RenderEngineTest::fillBufferCheckersRotate90() {
563 mat4 matrix = mat4(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 1);
564 fillBufferCheckers<SourceVariant>(matrix);
565 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 255, 0,
566 255);
567 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
568 DEFAULT_DISPLAY_HEIGHT / 2),
569 255, 0, 0, 255);
570 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
571 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
572 0, 0, 255, 255);
573 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
574 DEFAULT_DISPLAY_HEIGHT),
575 0, 0, 0, 0);
576}
577
578template <typename SourceVariant>
579void RenderEngineTest::fillBufferCheckersRotate180() {
580 mat4 matrix = mat4(-1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 2, 2, 0, 1);
581 fillBufferCheckers<SourceVariant>(matrix);
582 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0,
583 0);
584 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
585 DEFAULT_DISPLAY_HEIGHT / 2),
586 0, 255, 0, 255);
587 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
588 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
589 255, 0, 0, 255);
590 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
591 DEFAULT_DISPLAY_HEIGHT),
592 0, 0, 255, 255);
593}
594
595template <typename SourceVariant>
596void RenderEngineTest::fillBufferCheckersRotate270() {
597 mat4 matrix = mat4(0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 1);
598 fillBufferCheckers<SourceVariant>(matrix);
599 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
619 std::vector<renderengine::LayerSettings> layers;
620
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
629 layers.push_back(layer);
630
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
650 std::vector<renderengine::LayerSettings> layers;
651
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
Alec Mouri1089aed2018-10-25 21:33:57 -0700666 layers.push_back(layer);
667
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
683 std::vector<renderengine::LayerSettings> layers;
684
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
692 layers.push_back(layer);
693
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
729 std::vector<renderengine::LayerSettings> layers;
730
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;
735 layers.push_back(backgroundLayer);
736
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;
742 layers.push_back(leftLayer);
743
744 renderengine::LayerSettings blurLayer;
745 blurLayer.geometry.boundaries = fullscreenRect().toFloatRect();
746 blurLayer.backgroundBlurRadius = blurRadius;
747 blurLayer.alpha = 0;
748 layers.push_back(blurLayer);
749
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
764 std::vector<renderengine::LayerSettings> layersFirst;
765
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
772 layersFirst.push_back(layerOne);
773 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
779 std::vector<renderengine::LayerSettings> layersSecond;
780 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
787 layersSecond.push_back(layerTwo);
788 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
801 std::vector<renderengine::LayerSettings> layers;
802
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
836 layers.push_back(layer);
837
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
852 std::vector<renderengine::LayerSettings> layers;
853
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
875 layers.push_back(layer);
876
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
891 std::vector<renderengine::LayerSettings> layers;
892
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
914 layers.push_back(layer);
915
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);
929 settings.globalTransform = mat4::scale(vec4(2, 4, 0, 1));
930 settings.clearRegion = Region(Rect(1, 1));
931 std::vector<renderengine::LayerSettings> layers;
932 // dummy layer, without bounds should not render anything
933 renderengine::LayerSettings layer;
934 layers.push_back(layer);
935 invokeDraw(settings, layers, mBuffer);
936}
937
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000938void RenderEngineTest::clearRegion() {
Alec Mouriac335532018-11-12 15:01:33 -0800939 // Reuse mBuffer
940 clearLeftRegion();
941 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 255);
942 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
943 DEFAULT_DISPLAY_HEIGHT),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000944 0, 0, 0, 0);
Alec Mouriac335532018-11-12 15:01:33 -0800945}
946
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800947template <typename SourceVariant>
948void RenderEngineTest::drawShadow(const renderengine::LayerSettings& castingLayer,
949 const renderengine::ShadowSettings& shadow,
950 const ubyte4& casterColor, const ubyte4& backgroundColor) {
951 renderengine::DisplaySettings settings;
952 settings.physicalDisplay = fullscreenRect();
953 settings.clip = fullscreenRect();
954
955 std::vector<renderengine::LayerSettings> layers;
956
957 // add background layer
958 renderengine::LayerSettings bgLayer;
959 bgLayer.geometry.boundaries = fullscreenRect().toFloatRect();
960 ColorSourceVariant::fillColor(bgLayer, backgroundColor.r / 255.0f, backgroundColor.g / 255.0f,
961 backgroundColor.b / 255.0f, this);
962 bgLayer.alpha = backgroundColor.a / 255.0f;
963 layers.push_back(bgLayer);
964
965 // add shadow layer
966 renderengine::LayerSettings shadowLayer;
967 shadowLayer.geometry.boundaries = castingLayer.geometry.boundaries;
968 shadowLayer.alpha = castingLayer.alpha;
969 shadowLayer.shadow = shadow;
970 layers.push_back(shadowLayer);
971
972 // add layer casting the shadow
973 renderengine::LayerSettings layer = castingLayer;
974 SourceVariant::fillColor(layer, casterColor.r / 255.0f, casterColor.g / 255.0f,
975 casterColor.b / 255.0f, this);
976 layers.push_back(layer);
977
978 invokeDraw(settings, layers, mBuffer);
979}
980
Alec Mouri1089aed2018-10-25 21:33:57 -0700981TEST_F(RenderEngineTest, drawLayers_noLayersToDraw) {
982 drawEmptyLayers();
983}
984
Alec Mourid43ccab2019-03-13 12:23:45 -0700985TEST_F(RenderEngineTest, drawLayers_nullOutputBuffer) {
986 renderengine::DisplaySettings settings;
987 std::vector<renderengine::LayerSettings> layers;
988 renderengine::LayerSettings layer;
989 layer.geometry.boundaries = fullscreenRect().toFloatRect();
990 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
991 layers.push_back(layer);
992 base::unique_fd fence;
Alec Mourife0d72b2019-03-21 14:05:56 -0700993 status_t status = sRE->drawLayers(settings, layers, nullptr, true, base::unique_fd(), &fence);
Alec Mourid43ccab2019-03-13 12:23:45 -0700994
995 ASSERT_EQ(BAD_VALUE, status);
996}
997
998TEST_F(RenderEngineTest, drawLayers_nullOutputFence) {
999 renderengine::DisplaySettings settings;
1000 settings.physicalDisplay = fullscreenRect();
1001 settings.clip = fullscreenRect();
1002
1003 std::vector<renderengine::LayerSettings> layers;
1004 renderengine::LayerSettings layer;
1005 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1006 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1007 layer.alpha = 1.0;
1008 layers.push_back(layer);
1009
Alec Mourife0d72b2019-03-21 14:05:56 -07001010 status_t status = sRE->drawLayers(settings, layers, mBuffer->getNativeBuffer(), true,
Alec Mourid43ccab2019-03-13 12:23:45 -07001011 base::unique_fd(), nullptr);
1012 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
1022 std::vector<renderengine::LayerSettings> layers;
1023 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;
1027 layers.push_back(layer);
1028
1029 status_t status = sRE->drawLayers(settings, layers, mBuffer->getNativeBuffer(), false,
1030 base::unique_fd(), nullptr);
1031 sCurrentBuffer = mBuffer;
1032 ASSERT_EQ(NO_ERROR, status);
1033 ASSERT_FALSE(sRE->isFramebufferImageCachedForTesting(mBuffer->getId()));
1034 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
1035}
1036
Alec Mouri1089aed2018-10-25 21:33:57 -07001037TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_colorSource) {
1038 fillRedBuffer<ColorSourceVariant>();
1039}
1040
1041TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_colorSource) {
1042 fillGreenBuffer<ColorSourceVariant>();
1043}
1044
1045TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_colorSource) {
1046 fillBlueBuffer<ColorSourceVariant>();
1047}
1048
1049TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_colorSource) {
1050 fillRedTransparentBuffer<ColorSourceVariant>();
1051}
1052
1053TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_colorSource) {
1054 fillBufferPhysicalOffset<ColorSourceVariant>();
1055}
1056
1057TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_colorSource) {
1058 fillBufferCheckersRotate0<ColorSourceVariant>();
1059}
1060
1061TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_colorSource) {
1062 fillBufferCheckersRotate90<ColorSourceVariant>();
1063}
1064
1065TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_colorSource) {
1066 fillBufferCheckersRotate180<ColorSourceVariant>();
1067}
1068
1069TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_colorSource) {
1070 fillBufferCheckersRotate270<ColorSourceVariant>();
1071}
1072
1073TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_colorSource) {
1074 fillBufferLayerTransform<ColorSourceVariant>();
1075}
1076
1077TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_colorSource) {
1078 fillBufferLayerTransform<ColorSourceVariant>();
Alec Mouri6e57f682018-09-29 20:45:08 -07001079}
1080
Alec Mouri7c94edb2018-12-03 21:23:26 -08001081TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_colorSource) {
1082 fillBufferWithRoundedCorners<ColorSourceVariant>();
1083}
1084
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001085TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_colorSource) {
1086 fillBufferAndBlurBackground<ColorSourceVariant>();
1087}
1088
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001089TEST_F(RenderEngineTest, drawLayers_overlayCorners_colorSource) {
1090 overlayCorners<ColorSourceVariant>();
1091}
1092
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001093TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_opaqueBufferSource) {
1094 fillRedBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1095}
1096
1097TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_opaqueBufferSource) {
1098 fillGreenBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1099}
1100
1101TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_opaqueBufferSource) {
1102 fillBlueBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1103}
1104
1105TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_opaqueBufferSource) {
1106 fillRedTransparentBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1107}
1108
1109TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_opaqueBufferSource) {
1110 fillBufferPhysicalOffset<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1111}
1112
1113TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_opaqueBufferSource) {
1114 fillBufferCheckersRotate0<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1115}
1116
1117TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_opaqueBufferSource) {
1118 fillBufferCheckersRotate90<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1119}
1120
1121TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_opaqueBufferSource) {
1122 fillBufferCheckersRotate180<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1123}
1124
1125TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_opaqueBufferSource) {
1126 fillBufferCheckersRotate270<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1127}
1128
1129TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_opaqueBufferSource) {
1130 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1131}
1132
1133TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_opaqueBufferSource) {
1134 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1135}
1136
Alec Mouri7c94edb2018-12-03 21:23:26 -08001137TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_opaqueBufferSource) {
1138 fillBufferWithRoundedCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1139}
1140
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001141TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_opaqueBufferSource) {
1142 fillBufferAndBlurBackground<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1143}
1144
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001145TEST_F(RenderEngineTest, drawLayers_overlayCorners_opaqueBufferSource) {
1146 overlayCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1147}
1148
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001149TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_bufferSource) {
1150 fillRedBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1151}
1152
1153TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_bufferSource) {
1154 fillGreenBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1155}
1156
1157TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_bufferSource) {
1158 fillBlueBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1159}
1160
1161TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_bufferSource) {
1162 fillRedTransparentBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1163}
1164
1165TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_bufferSource) {
1166 fillBufferPhysicalOffset<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1167}
1168
1169TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_bufferSource) {
1170 fillBufferCheckersRotate0<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1171}
1172
1173TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_bufferSource) {
1174 fillBufferCheckersRotate90<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1175}
1176
1177TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_bufferSource) {
1178 fillBufferCheckersRotate180<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1179}
1180
1181TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_bufferSource) {
1182 fillBufferCheckersRotate270<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1183}
1184
1185TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_bufferSource) {
1186 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1187}
1188
1189TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_bufferSource) {
1190 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1191}
1192
Alec Mouri7c94edb2018-12-03 21:23:26 -08001193TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_bufferSource) {
1194 fillBufferWithRoundedCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1195}
1196
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001197TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_bufferSource) {
1198 fillBufferAndBlurBackground<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1199}
1200
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001201TEST_F(RenderEngineTest, drawLayers_overlayCorners_bufferSource) {
1202 overlayCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1203}
1204
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001205TEST_F(RenderEngineTest, drawLayers_fillBufferTextureTransform) {
1206 fillBufferTextureTransform();
1207}
1208
1209TEST_F(RenderEngineTest, drawLayers_fillBuffer_premultipliesAlpha) {
1210 fillBufferWithPremultiplyAlpha();
1211}
1212
1213TEST_F(RenderEngineTest, drawLayers_fillBuffer_withoutPremultiplyingAlpha) {
1214 fillBufferWithoutPremultiplyAlpha();
1215}
1216
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001217TEST_F(RenderEngineTest, drawLayers_clearRegion) {
1218 clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -08001219}
1220
Alec Mourid43ccab2019-03-13 12:23:45 -07001221TEST_F(RenderEngineTest, drawLayers_fillsBufferAndCachesImages) {
1222 renderengine::DisplaySettings settings;
1223 settings.physicalDisplay = fullscreenRect();
1224 settings.clip = fullscreenRect();
1225
1226 std::vector<renderengine::LayerSettings> layers;
1227
1228 renderengine::LayerSettings layer;
1229 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1230 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1231
1232 layers.push_back(layer);
1233 invokeDraw(settings, layers, mBuffer);
1234 uint64_t bufferId = layer.source.buffer.buffer->getId();
1235 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001236 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1237 sRE->unbindExternalTextureBufferForTesting(bufferId);
1238 std::lock_guard<std::mutex> lock(barrier->mutex);
1239 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1240 [&]() REQUIRES(barrier->mutex) {
1241 return barrier->isOpen;
1242 }));
Alec Mourid43ccab2019-03-13 12:23:45 -07001243 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001244 EXPECT_EQ(NO_ERROR, barrier->result);
Alec Mourid43ccab2019-03-13 12:23:45 -07001245}
1246
1247TEST_F(RenderEngineTest, drawLayers_bindExternalBufferWithNullBuffer) {
1248 status_t result = sRE->bindExternalTextureBuffer(0, nullptr, nullptr);
1249 ASSERT_EQ(BAD_VALUE, result);
1250}
1251
1252TEST_F(RenderEngineTest, drawLayers_bindExternalBufferCachesImages) {
1253 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
1254 uint32_t texName;
1255 sRE->genTextures(1, &texName);
1256 mTexNames.push_back(texName);
1257
1258 sRE->bindExternalTextureBuffer(texName, buf, nullptr);
1259 uint64_t bufferId = buf->getId();
1260 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001261 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1262 sRE->unbindExternalTextureBufferForTesting(bufferId);
1263 std::lock_guard<std::mutex> lock(barrier->mutex);
1264 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1265 [&]() REQUIRES(barrier->mutex) {
1266 return barrier->isOpen;
1267 }));
1268 EXPECT_EQ(NO_ERROR, barrier->result);
Alec Mourid43ccab2019-03-13 12:23:45 -07001269 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1270}
1271
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001272TEST_F(RenderEngineTest, drawLayers_cacheExternalBufferWithNullBuffer) {
Alec Mouri16a99402019-07-29 16:37:30 -07001273 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1274 sRE->cacheExternalTextureBufferForTesting(nullptr);
1275 std::lock_guard<std::mutex> lock(barrier->mutex);
1276 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1277 [&]() REQUIRES(barrier->mutex) {
1278 return barrier->isOpen;
1279 }));
1280 EXPECT_TRUE(barrier->isOpen);
1281 EXPECT_EQ(BAD_VALUE, barrier->result);
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001282}
1283
1284TEST_F(RenderEngineTest, drawLayers_cacheExternalBufferCachesImages) {
1285 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
1286 uint64_t bufferId = buf->getId();
Alec Mouri16a99402019-07-29 16:37:30 -07001287 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1288 sRE->cacheExternalTextureBufferForTesting(buf);
1289 {
1290 std::lock_guard<std::mutex> lock(barrier->mutex);
1291 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1292 [&]() REQUIRES(barrier->mutex) {
1293 return barrier->isOpen;
1294 }));
1295 EXPECT_EQ(NO_ERROR, barrier->result);
1296 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001297 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001298 barrier = sRE->unbindExternalTextureBufferForTesting(bufferId);
1299 {
1300 std::lock_guard<std::mutex> lock(barrier->mutex);
1301 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1302 [&]() REQUIRES(barrier->mutex) {
1303 return barrier->isOpen;
1304 }));
1305 EXPECT_EQ(NO_ERROR, barrier->result);
1306 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001307 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1308}
1309
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001310TEST_F(RenderEngineTest, drawLayers_fillShadow_casterLayerMinSize) {
1311 const ubyte4 casterColor(255, 0, 0, 255);
1312 const ubyte4 backgroundColor(255, 255, 255, 255);
1313 const float shadowLength = 5.0f;
1314 Rect casterBounds(1, 1);
1315 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1316 renderengine::LayerSettings castingLayer;
1317 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1318 castingLayer.alpha = 1.0f;
1319 renderengine::ShadowSettings settings =
1320 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1321 false /* casterIsTranslucent */);
1322
1323 drawShadow<ColorSourceVariant>(castingLayer, settings, casterColor, backgroundColor);
1324 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1325}
1326
1327TEST_F(RenderEngineTest, drawLayers_fillShadow_casterColorLayer) {
1328 const ubyte4 casterColor(255, 0, 0, 255);
1329 const ubyte4 backgroundColor(255, 255, 255, 255);
1330 const float shadowLength = 5.0f;
1331 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1332 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1333 renderengine::LayerSettings castingLayer;
1334 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1335 castingLayer.alpha = 1.0f;
1336 renderengine::ShadowSettings settings =
1337 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1338 false /* casterIsTranslucent */);
1339
1340 drawShadow<ColorSourceVariant>(castingLayer, settings, casterColor, backgroundColor);
1341 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1342}
1343
1344TEST_F(RenderEngineTest, drawLayers_fillShadow_casterOpaqueBufferLayer) {
1345 const ubyte4 casterColor(255, 0, 0, 255);
1346 const ubyte4 backgroundColor(255, 255, 255, 255);
1347 const float shadowLength = 5.0f;
1348 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1349 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1350 renderengine::LayerSettings castingLayer;
1351 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1352 castingLayer.alpha = 1.0f;
1353 renderengine::ShadowSettings settings =
1354 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1355 false /* casterIsTranslucent */);
1356
1357 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1358 backgroundColor);
1359 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1360}
1361
1362TEST_F(RenderEngineTest, drawLayers_fillShadow_casterWithRoundedCorner) {
1363 const ubyte4 casterColor(255, 0, 0, 255);
1364 const ubyte4 backgroundColor(255, 255, 255, 255);
1365 const float shadowLength = 5.0f;
1366 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1367 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1368 renderengine::LayerSettings castingLayer;
1369 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1370 castingLayer.geometry.roundedCornersRadius = 3.0f;
1371 castingLayer.geometry.roundedCornersCrop = casterBounds.toFloatRect();
1372 castingLayer.alpha = 1.0f;
1373 renderengine::ShadowSettings settings =
1374 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1375 false /* casterIsTranslucent */);
1376
1377 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1378 backgroundColor);
1379 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1380}
1381
1382TEST_F(RenderEngineTest, drawLayers_fillShadow_translucentCasterWithAlpha) {
1383 const ubyte4 casterColor(255, 0, 0, 255);
1384 const ubyte4 backgroundColor(255, 255, 255, 255);
1385 const float shadowLength = 5.0f;
1386 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1387 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1388 renderengine::LayerSettings castingLayer;
1389 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1390 castingLayer.alpha = 0.5f;
1391 renderengine::ShadowSettings settings =
1392 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1393 true /* casterIsTranslucent */);
1394
1395 drawShadow<BufferSourceVariant<RelaxOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1396 backgroundColor);
1397
1398 // verify only the background since the shadow will draw behind the caster
1399 const float shadowInset = settings.length * -1.0f;
1400 const Rect casterWithShadow =
1401 Rect(casterBounds).inset(shadowInset, shadowInset, shadowInset, shadowInset);
1402 const Region backgroundRegion = Region(fullscreenRect()).subtractSelf(casterWithShadow);
1403 expectBufferColor(backgroundRegion, backgroundColor.r, backgroundColor.g, backgroundColor.b,
1404 backgroundColor.a);
1405}
1406
Alec Mouri6e57f682018-09-29 20:45:08 -07001407} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001408
1409// TODO(b/129481165): remove the #pragma below and fix conversion issues
1410#pragma clang diagnostic pop // ignored "-Wconversion"