blob: d20fcc437314398c91fa814dbb96f6d366501a4f [file] [log] [blame]
Alec Mouri6e57f682018-09-29 20:45:08 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Alec Mouri16a99402019-07-29 16:37:30 -070021#include <chrono>
22#include <condition_variable>
Vishnu Nair16efdbf2019-12-10 11:55:42 -080023#include <fstream>
Alec Mouri6e57f682018-09-29 20:45:08 -070024
Lucas Dupin19c8f0e2019-11-25 17:55:44 -080025#include <cutils/properties.h>
Ana Krulec9bc9dc62020-02-26 12:16:40 -080026#include <gtest/gtest.h>
Alec Mouri6e57f682018-09-29 20:45:08 -070027#include <renderengine/RenderEngine.h>
Alec Mouri1089aed2018-10-25 21:33:57 -070028#include <sync/sync.h>
Alec Mouri6e57f682018-09-29 20:45:08 -070029#include <ui/PixelFormat.h>
Alec Mourid43ccab2019-03-13 12:23:45 -070030#include "../gl/GLESRenderEngine.h"
Ana Krulec9bc9dc62020-02-26 12:16:40 -080031#include "../threaded/RenderEngineThreaded.h"
Alec Mouri6e57f682018-09-29 20:45:08 -070032
Alec Mouri1089aed2018-10-25 21:33:57 -070033constexpr int DEFAULT_DISPLAY_WIDTH = 128;
34constexpr int DEFAULT_DISPLAY_HEIGHT = 256;
35constexpr int DEFAULT_DISPLAY_OFFSET = 64;
Vishnu Nair16efdbf2019-12-10 11:55:42 -080036constexpr bool WRITE_BUFFER_TO_FILE_ON_FAILURE = false;
Alec Mouri1089aed2018-10-25 21:33:57 -070037
Alec Mouri6e57f682018-09-29 20:45:08 -070038namespace android {
39
Alec Mouri1089aed2018-10-25 21:33:57 -070040struct RenderEngineTest : public ::testing::Test {
Alec Mourid43ccab2019-03-13 12:23:45 -070041 static void SetUpTestSuite() {
KaiChieh Chuang436fc192020-09-07 13:48:42 +080042 renderengine::RenderEngineCreationArgs reCreationArgs =
Peiyong Lin4137a1d2019-10-09 10:39:09 -070043 renderengine::RenderEngineCreationArgs::Builder()
Ana Krulec9bc9dc62020-02-26 12:16:40 -080044 .setPixelFormat(static_cast<int>(ui::PixelFormat::RGBA_8888))
45 .setImageCacheSize(1)
46 .setUseColorManagerment(false)
47 .setEnableProtectedContext(false)
48 .setPrecacheToneMapperShaderOnly(false)
49 .setSupportsBackgroundBlur(true)
50 .setContextPriority(renderengine::RenderEngine::ContextPriority::MEDIUM)
51 .setRenderEngineType(renderengine::RenderEngine::RenderEngineType::GLES)
KaiChieh Chuang436fc192020-09-07 13:48:42 +080052 .build();
53 sRE = renderengine::gl::GLESRenderEngine::create(reCreationArgs);
54
55 reCreationArgs.useColorManagement = true;
56 sRECM = renderengine::gl::GLESRenderEngine::create(reCreationArgs);
Alec Mourid43ccab2019-03-13 12:23:45 -070057 }
58
59 static void TearDownTestSuite() {
60 // The ordering here is important - sCurrentBuffer must live longer
61 // than RenderEngine to avoid a null reference on tear-down.
62 sRE = nullptr;
KaiChieh Chuang436fc192020-09-07 13:48:42 +080063 sRECM = nullptr;
Alec Mourid43ccab2019-03-13 12:23:45 -070064 sCurrentBuffer = nullptr;
65 }
66
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080067 static sp<GraphicBuffer> allocateDefaultBuffer() {
Alec Mouri1089aed2018-10-25 21:33:57 -070068 return new GraphicBuffer(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT,
69 HAL_PIXEL_FORMAT_RGBA_8888, 1,
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080070 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
71 GRALLOC_USAGE_HW_RENDER,
Alec Mouri1089aed2018-10-25 21:33:57 -070072 "output");
Alec Mouri6e57f682018-09-29 20:45:08 -070073 }
74
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080075 // Allocates a 1x1 buffer to fill with a solid color
76 static sp<GraphicBuffer> allocateSourceBuffer(uint32_t width, uint32_t height) {
77 return new GraphicBuffer(width, height, HAL_PIXEL_FORMAT_RGBA_8888, 1,
78 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
79 GRALLOC_USAGE_HW_TEXTURE,
80 "input");
81 }
82
Alec Mouri1089aed2018-10-25 21:33:57 -070083 RenderEngineTest() { mBuffer = allocateDefaultBuffer(); }
84
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080085 ~RenderEngineTest() {
Vishnu Nair16efdbf2019-12-10 11:55:42 -080086 if (WRITE_BUFFER_TO_FILE_ON_FAILURE && ::testing::Test::HasFailure()) {
87 writeBufferToFile("/data/texture_out_");
88 }
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080089 for (uint32_t texName : mTexNames) {
90 sRE->deleteTextures(1, &texName);
Alec Mouri368e1582020-08-13 10:14:29 -070091 EXPECT_FALSE(sRE->isTextureNameKnownForTesting(texName));
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080092 }
KaiChieh Chuang436fc192020-09-07 13:48:42 +080093 for (uint32_t texName : mTexNamesCM) {
94 sRECM->deleteTextures(1, &texName);
95 }
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080096 }
97
Vishnu Nair16efdbf2019-12-10 11:55:42 -080098 void writeBufferToFile(const char* basename) {
99 std::string filename(basename);
100 filename.append(::testing::UnitTest::GetInstance()->current_test_info()->name());
101 filename.append(".ppm");
102 std::ofstream file(filename.c_str(), std::ios::binary);
103 if (!file.is_open()) {
104 ALOGE("Unable to open file: %s", filename.c_str());
105 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
106 "surfaceflinger to write debug images");
107 return;
108 }
109
Alec Mouri1089aed2018-10-25 21:33:57 -0700110 uint8_t* pixels;
111 mBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
112 reinterpret_cast<void**>(&pixels));
113
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800114 file << "P6\n";
115 file << mBuffer->getWidth() << "\n";
116 file << mBuffer->getHeight() << "\n";
117 file << 255 << "\n";
118
119 std::vector<uint8_t> outBuffer(mBuffer->getWidth() * mBuffer->getHeight() * 3);
120 auto outPtr = reinterpret_cast<uint8_t*>(outBuffer.data());
121
122 for (int32_t j = 0; j < mBuffer->getHeight(); j++) {
123 const uint8_t* src = pixels + (mBuffer->getStride() * j) * 4;
124 for (int32_t i = 0; i < mBuffer->getWidth(); i++) {
125 // Only copy R, G and B components
126 outPtr[0] = src[0];
127 outPtr[1] = src[1];
128 outPtr[2] = src[2];
129 outPtr += 3;
130
131 src += 4;
132 }
133 }
134 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
135 mBuffer->unlock();
136 }
137
138 void expectBufferColor(const Region& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
139 size_t c;
140 Rect const* rect = region.getArray(&c);
141 for (size_t i = 0; i < c; i++, rect++) {
142 expectBufferColor(*rect, r, g, b, a);
143 }
144 }
145
146 void expectBufferColor(const Rect& rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a,
147 uint8_t tolerance = 0) {
148 auto colorCompare = [tolerance](const uint8_t* colorA, const uint8_t* colorB) {
149 auto colorBitCompare = [tolerance](uint8_t a, uint8_t b) {
150 uint8_t tmp = a >= b ? a - b : b - a;
151 return tmp <= tolerance;
152 };
153 return std::equal(colorA, colorA + 4, colorB, colorBitCompare);
Alec Mouri1089aed2018-10-25 21:33:57 -0700154 };
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800155
156 expectBufferColor(rect, r, g, b, a, colorCompare);
157 }
158
159 void expectBufferColor(const Rect& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a,
160 std::function<bool(const uint8_t* a, const uint8_t* b)> colorCompare) {
161 uint8_t* pixels;
162 mBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
163 reinterpret_cast<void**>(&pixels));
Alec Mouri1089aed2018-10-25 21:33:57 -0700164 int32_t maxFails = 10;
165 int32_t fails = 0;
166 for (int32_t j = 0; j < region.getHeight(); j++) {
167 const uint8_t* src =
168 pixels + (mBuffer->getStride() * (region.top + j) + region.left) * 4;
169 for (int32_t i = 0; i < region.getWidth(); i++) {
170 const uint8_t expected[4] = {r, g, b, a};
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800171 bool equal = colorCompare(src, expected);
Alec Mouri1089aed2018-10-25 21:33:57 -0700172 EXPECT_TRUE(equal)
173 << "pixel @ (" << region.left + i << ", " << region.top + j << "): "
174 << "expected (" << static_cast<uint32_t>(r) << ", "
175 << static_cast<uint32_t>(g) << ", " << static_cast<uint32_t>(b) << ", "
176 << static_cast<uint32_t>(a) << "), "
177 << "got (" << static_cast<uint32_t>(src[0]) << ", "
178 << static_cast<uint32_t>(src[1]) << ", " << static_cast<uint32_t>(src[2])
179 << ", " << static_cast<uint32_t>(src[3]) << ")";
180 src += 4;
181 if (!equal && ++fails >= maxFails) {
182 break;
183 }
184 }
185 if (fails >= maxFails) {
186 break;
187 }
188 }
189 mBuffer->unlock();
190 }
191
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800192 void expectAlpha(const Rect& rect, uint8_t a) {
193 auto colorCompare = [](const uint8_t* colorA, const uint8_t* colorB) {
194 return colorA[3] == colorB[3];
195 };
196 expectBufferColor(rect, 0.0f /* r */, 0.0f /*g */, 0.0f /* b */, a, colorCompare);
197 }
198
199 void expectShadowColor(const renderengine::LayerSettings& castingLayer,
200 const renderengine::ShadowSettings& shadow, const ubyte4& casterColor,
201 const ubyte4& backgroundColor) {
202 const Rect casterRect(castingLayer.geometry.boundaries);
203 Region casterRegion = Region(casterRect);
204 const float casterCornerRadius = castingLayer.geometry.roundedCornersRadius;
205 if (casterCornerRadius > 0.0f) {
206 // ignore the corners if a corner radius is set
207 Rect cornerRect(casterCornerRadius, casterCornerRadius);
208 casterRegion.subtractSelf(cornerRect.offsetTo(casterRect.left, casterRect.top));
209 casterRegion.subtractSelf(
210 cornerRect.offsetTo(casterRect.right - casterCornerRadius, casterRect.top));
211 casterRegion.subtractSelf(
212 cornerRect.offsetTo(casterRect.left, casterRect.bottom - casterCornerRadius));
213 casterRegion.subtractSelf(cornerRect.offsetTo(casterRect.right - casterCornerRadius,
214 casterRect.bottom - casterCornerRadius));
215 }
216
217 const float shadowInset = shadow.length * -1.0f;
218 const Rect casterWithShadow =
219 Rect(casterRect).inset(shadowInset, shadowInset, shadowInset, shadowInset);
220 const Region shadowRegion = Region(casterWithShadow).subtractSelf(casterRect);
221 const Region backgroundRegion = Region(fullscreenRect()).subtractSelf(casterWithShadow);
222
223 // verify casting layer
224 expectBufferColor(casterRegion, casterColor.r, casterColor.g, casterColor.b, casterColor.a);
225
226 // verify shadows by testing just the alpha since its difficult to validate the shadow color
227 size_t c;
228 Rect const* r = shadowRegion.getArray(&c);
229 for (size_t i = 0; i < c; i++, r++) {
230 expectAlpha(*r, 255);
231 }
232
233 // verify background
234 expectBufferColor(backgroundRegion, backgroundColor.r, backgroundColor.g, backgroundColor.b,
235 backgroundColor.a);
236 }
237
238 static renderengine::ShadowSettings getShadowSettings(const vec2& casterPos, float shadowLength,
239 bool casterIsTranslucent) {
240 renderengine::ShadowSettings shadow;
241 shadow.ambientColor = {0.0f, 0.0f, 0.0f, 0.039f};
242 shadow.spotColor = {0.0f, 0.0f, 0.0f, 0.19f};
243 shadow.lightPos = vec3(casterPos.x, casterPos.y, 0);
244 shadow.lightRadius = 0.0f;
245 shadow.length = shadowLength;
246 shadow.casterIsTranslucent = casterIsTranslucent;
247 return shadow;
248 }
249
Alec Mouri1089aed2018-10-25 21:33:57 -0700250 static Rect fullscreenRect() { return Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT); }
251
252 static Rect offsetRect() {
253 return Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
254 DEFAULT_DISPLAY_HEIGHT);
255 }
256
257 static Rect offsetRectAtZero() {
258 return Rect(DEFAULT_DISPLAY_WIDTH - DEFAULT_DISPLAY_OFFSET,
259 DEFAULT_DISPLAY_HEIGHT - DEFAULT_DISPLAY_OFFSET);
260 }
261
Alec Mourid43ccab2019-03-13 12:23:45 -0700262 void invokeDraw(renderengine::DisplaySettings settings,
Vishnu Nair9b079a22020-01-21 14:36:08 -0800263 std::vector<const renderengine::LayerSettings*> layers,
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800264 sp<GraphicBuffer> buffer, bool useColorManagement = false) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700265 base::unique_fd fence;
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800266 status_t status = useColorManagement
267 ? sRECM->drawLayers(settings, layers, buffer, true, base::unique_fd(), &fence)
268 : sRE->drawLayers(settings, layers, buffer, true, base::unique_fd(), &fence);
Alec Mourid43ccab2019-03-13 12:23:45 -0700269 sCurrentBuffer = buffer;
Alec Mouri1089aed2018-10-25 21:33:57 -0700270
271 int fd = fence.release();
272 if (fd >= 0) {
273 sync_wait(fd, -1);
274 close(fd);
275 }
276
277 ASSERT_EQ(NO_ERROR, status);
Alec Mourid43ccab2019-03-13 12:23:45 -0700278 if (layers.size() > 0) {
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800279 if (useColorManagement) {
280 ASSERT_TRUE(sRECM->isFramebufferImageCachedForTesting(buffer->getId()));
281 } else {
282 ASSERT_TRUE(sRE->isFramebufferImageCachedForTesting(buffer->getId()));
283 }
Alec Mourid43ccab2019-03-13 12:23:45 -0700284 }
Alec Mouri1089aed2018-10-25 21:33:57 -0700285 }
286
Alec Mourid43ccab2019-03-13 12:23:45 -0700287 void drawEmptyLayers() {
Alec Mouri6e57f682018-09-29 20:45:08 -0700288 renderengine::DisplaySettings settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800289 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri6e57f682018-09-29 20:45:08 -0700290 // Meaningless buffer since we don't do any drawing
291 sp<GraphicBuffer> buffer = new GraphicBuffer();
Alec Mouri1089aed2018-10-25 21:33:57 -0700292 invokeDraw(settings, layers, buffer);
Alec Mouri6e57f682018-09-29 20:45:08 -0700293 }
294
Alec Mouri1089aed2018-10-25 21:33:57 -0700295 template <typename SourceVariant>
296 void fillBuffer(half r, half g, half b, half a);
297
298 template <typename SourceVariant>
299 void fillRedBuffer();
300
301 template <typename SourceVariant>
302 void fillGreenBuffer();
303
304 template <typename SourceVariant>
305 void fillBlueBuffer();
306
307 template <typename SourceVariant>
308 void fillRedTransparentBuffer();
309
310 template <typename SourceVariant>
311 void fillRedOffsetBuffer();
312
313 template <typename SourceVariant>
314 void fillBufferPhysicalOffset();
315
316 template <typename SourceVariant>
Alec Mouri5a6d8572020-03-23 23:56:15 -0700317 void fillBufferCheckers(uint32_t rotation);
Alec Mouri1089aed2018-10-25 21:33:57 -0700318
319 template <typename SourceVariant>
320 void fillBufferCheckersRotate0();
321
322 template <typename SourceVariant>
323 void fillBufferCheckersRotate90();
324
325 template <typename SourceVariant>
326 void fillBufferCheckersRotate180();
327
328 template <typename SourceVariant>
329 void fillBufferCheckersRotate270();
330
331 template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800332 void fillBufferWithLayerTransform();
333
334 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700335 void fillBufferLayerTransform();
336
337 template <typename SourceVariant>
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800338 void fillBufferWithColorTransform(bool useColorManagement = false);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800339
340 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700341 void fillBufferColorTransform();
342
Alec Mouri7c94edb2018-12-03 21:23:26 -0800343 template <typename SourceVariant>
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800344 void fillBufferColorTransformCM();
345
346 template <typename SourceVariant>
Alec Mouri7c94edb2018-12-03 21:23:26 -0800347 void fillRedBufferWithRoundedCorners();
348
349 template <typename SourceVariant>
350 void fillBufferWithRoundedCorners();
351
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000352 template <typename SourceVariant>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800353 void fillBufferAndBlurBackground();
354
355 template <typename SourceVariant>
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000356 void overlayCorners();
357
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800358 void fillRedBufferTextureTransform();
359
360 void fillBufferTextureTransform();
361
362 void fillRedBufferWithPremultiplyAlpha();
363
364 void fillBufferWithPremultiplyAlpha();
365
366 void fillRedBufferWithoutPremultiplyAlpha();
367
368 void fillBufferWithoutPremultiplyAlpha();
369
Alec Mouriac335532018-11-12 15:01:33 -0800370 void fillGreenColorBufferThenClearRegion();
371
372 void clearLeftRegion();
373
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000374 void clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -0800375
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800376 template <typename SourceVariant>
377 void drawShadow(const renderengine::LayerSettings& castingLayer,
378 const renderengine::ShadowSettings& shadow, const ubyte4& casterColor,
379 const ubyte4& backgroundColor);
380
Alec Mourid43ccab2019-03-13 12:23:45 -0700381 // Keep around the same renderengine object to save on initialization time.
382 // For now, exercise the GL backend directly so that some caching specifics
383 // can be tested without changing the interface.
384 static std::unique_ptr<renderengine::gl::GLESRenderEngine> sRE;
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800385 // renderengine object with Color Management enabled
386 static std::unique_ptr<renderengine::gl::GLESRenderEngine> sRECM;
Alec Mourid43ccab2019-03-13 12:23:45 -0700387 // Dumb hack to avoid NPE in the EGL driver: the GraphicBuffer needs to
388 // be freed *after* RenderEngine is destroyed, so that the EGL image is
389 // destroyed first.
390 static sp<GraphicBuffer> sCurrentBuffer;
Alec Mouri1089aed2018-10-25 21:33:57 -0700391
392 sp<GraphicBuffer> mBuffer;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800393
394 std::vector<uint32_t> mTexNames;
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800395 std::vector<uint32_t> mTexNamesCM;
Alec Mouri6e57f682018-09-29 20:45:08 -0700396};
397
Alec Mourid43ccab2019-03-13 12:23:45 -0700398std::unique_ptr<renderengine::gl::GLESRenderEngine> RenderEngineTest::sRE = nullptr;
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800399std::unique_ptr<renderengine::gl::GLESRenderEngine> RenderEngineTest::sRECM = nullptr;
400
Alec Mourid43ccab2019-03-13 12:23:45 -0700401sp<GraphicBuffer> RenderEngineTest::sCurrentBuffer = nullptr;
Alec Mouri1089aed2018-10-25 21:33:57 -0700402
403struct ColorSourceVariant {
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800404 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800405 RenderEngineTest* /*fixture*/, bool /*useColorManagement*/ = false) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700406 layer.source.solidColor = half3(r, g, b);
407 }
408};
409
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800410struct RelaxOpaqueBufferVariant {
411 static void setOpaqueBit(renderengine::LayerSettings& layer) {
412 layer.source.buffer.isOpaque = false;
413 }
414
415 static uint8_t getAlphaChannel() { return 255; }
416};
417
418struct ForceOpaqueBufferVariant {
419 static void setOpaqueBit(renderengine::LayerSettings& layer) {
420 layer.source.buffer.isOpaque = true;
421 }
422
423 static uint8_t getAlphaChannel() {
424 // The isOpaque bit will override the alpha channel, so this should be
425 // arbitrary.
426 return 10;
427 }
428};
429
430template <typename OpaquenessVariant>
431struct BufferSourceVariant {
432 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800433 RenderEngineTest* fixture, bool useColorManagement = false) {
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800434 sp<GraphicBuffer> buf = RenderEngineTest::allocateSourceBuffer(1, 1);
435 uint32_t texName;
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800436 if (useColorManagement) {
437 fixture->sRECM->genTextures(1, &texName);
438 fixture->mTexNamesCM.push_back(texName);
439 } else {
440 fixture->sRE->genTextures(1, &texName);
441 fixture->mTexNames.push_back(texName);
442 }
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800443
444 uint8_t* pixels;
445 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
446 reinterpret_cast<void**>(&pixels));
447
448 for (int32_t j = 0; j < buf->getHeight(); j++) {
449 uint8_t* iter = pixels + (buf->getStride() * j) * 4;
450 for (int32_t i = 0; i < buf->getWidth(); i++) {
451 iter[0] = uint8_t(r * 255);
452 iter[1] = uint8_t(g * 255);
453 iter[2] = uint8_t(b * 255);
454 iter[3] = OpaquenessVariant::getAlphaChannel();
455 iter += 4;
456 }
457 }
458
459 buf->unlock();
460
461 layer.source.buffer.buffer = buf;
462 layer.source.buffer.textureName = texName;
463 OpaquenessVariant::setOpaqueBit(layer);
464 }
465};
466
Alec Mouri1089aed2018-10-25 21:33:57 -0700467template <typename SourceVariant>
468void RenderEngineTest::fillBuffer(half r, half g, half b, half a) {
469 renderengine::DisplaySettings settings;
470 settings.physicalDisplay = fullscreenRect();
471 settings.clip = fullscreenRect();
472
Vishnu Nair9b079a22020-01-21 14:36:08 -0800473 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700474
475 renderengine::LayerSettings layer;
476 layer.geometry.boundaries = fullscreenRect().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800477 SourceVariant::fillColor(layer, r, g, b, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700478 layer.alpha = a;
479
Vishnu Nair9b079a22020-01-21 14:36:08 -0800480 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700481
482 invokeDraw(settings, layers, mBuffer);
483}
484
485template <typename SourceVariant>
486void RenderEngineTest::fillRedBuffer() {
487 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, 1.0f);
488 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
489}
490
491template <typename SourceVariant>
492void RenderEngineTest::fillGreenBuffer() {
493 fillBuffer<SourceVariant>(0.0f, 1.0f, 0.0f, 1.0f);
494 expectBufferColor(fullscreenRect(), 0, 255, 0, 255);
495}
496
497template <typename SourceVariant>
498void RenderEngineTest::fillBlueBuffer() {
499 fillBuffer<SourceVariant>(0.0f, 0.0f, 1.0f, 1.0f);
500 expectBufferColor(fullscreenRect(), 0, 0, 255, 255);
501}
502
503template <typename SourceVariant>
504void RenderEngineTest::fillRedTransparentBuffer() {
505 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, .2f);
506 expectBufferColor(fullscreenRect(), 51, 0, 0, 51);
507}
508
509template <typename SourceVariant>
510void RenderEngineTest::fillRedOffsetBuffer() {
511 renderengine::DisplaySettings settings;
512 settings.physicalDisplay = offsetRect();
513 settings.clip = offsetRectAtZero();
514
Vishnu Nair9b079a22020-01-21 14:36:08 -0800515 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700516
517 renderengine::LayerSettings layer;
518 layer.geometry.boundaries = offsetRectAtZero().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800519 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700520 layer.alpha = 1.0f;
521
Vishnu Nair9b079a22020-01-21 14:36:08 -0800522 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700523 invokeDraw(settings, layers, mBuffer);
524}
525
526template <typename SourceVariant>
527void RenderEngineTest::fillBufferPhysicalOffset() {
528 fillRedOffsetBuffer<SourceVariant>();
529
530 expectBufferColor(Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
531 DEFAULT_DISPLAY_HEIGHT),
532 255, 0, 0, 255);
533 Rect offsetRegionLeft(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_HEIGHT);
534 Rect offsetRegionTop(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_OFFSET);
535
536 expectBufferColor(offsetRegionLeft, 0, 0, 0, 0);
537 expectBufferColor(offsetRegionTop, 0, 0, 0, 0);
538}
539
540template <typename SourceVariant>
Alec Mouri5a6d8572020-03-23 23:56:15 -0700541void RenderEngineTest::fillBufferCheckers(uint32_t orientationFlag) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700542 renderengine::DisplaySettings settings;
543 settings.physicalDisplay = fullscreenRect();
544 // Here logical space is 2x2
545 settings.clip = Rect(2, 2);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700546 settings.orientation = orientationFlag;
Alec Mouri1089aed2018-10-25 21:33:57 -0700547
Vishnu Nair9b079a22020-01-21 14:36:08 -0800548 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700549
550 renderengine::LayerSettings layerOne;
551 Rect rectOne(0, 0, 1, 1);
552 layerOne.geometry.boundaries = rectOne.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800553 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700554 layerOne.alpha = 1.0f;
555
556 renderengine::LayerSettings layerTwo;
557 Rect rectTwo(0, 1, 1, 2);
558 layerTwo.geometry.boundaries = rectTwo.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800559 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700560 layerTwo.alpha = 1.0f;
561
562 renderengine::LayerSettings layerThree;
563 Rect rectThree(1, 0, 2, 1);
564 layerThree.geometry.boundaries = rectThree.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800565 SourceVariant::fillColor(layerThree, 0.0f, 0.0f, 1.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700566 layerThree.alpha = 1.0f;
567
Vishnu Nair9b079a22020-01-21 14:36:08 -0800568 layers.push_back(&layerOne);
569 layers.push_back(&layerTwo);
570 layers.push_back(&layerThree);
Alec Mouri1089aed2018-10-25 21:33:57 -0700571
572 invokeDraw(settings, layers, mBuffer);
573}
574
575template <typename SourceVariant>
576void RenderEngineTest::fillBufferCheckersRotate0() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700577 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_0);
Alec Mouri1089aed2018-10-25 21:33:57 -0700578 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 255, 0, 0,
579 255);
580 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
581 DEFAULT_DISPLAY_HEIGHT / 2),
582 0, 0, 255, 255);
583 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
584 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
585 0, 0, 0, 0);
586 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
587 DEFAULT_DISPLAY_HEIGHT),
588 0, 255, 0, 255);
589}
590
591template <typename SourceVariant>
592void RenderEngineTest::fillBufferCheckersRotate90() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700593 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_90);
Alec Mouri1089aed2018-10-25 21:33:57 -0700594 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 255, 0,
595 255);
596 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
597 DEFAULT_DISPLAY_HEIGHT / 2),
598 255, 0, 0, 255);
599 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
600 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
601 0, 0, 255, 255);
602 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
603 DEFAULT_DISPLAY_HEIGHT),
604 0, 0, 0, 0);
605}
606
607template <typename SourceVariant>
608void RenderEngineTest::fillBufferCheckersRotate180() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700609 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_180);
Alec Mouri1089aed2018-10-25 21:33:57 -0700610 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0,
611 0);
612 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
613 DEFAULT_DISPLAY_HEIGHT / 2),
614 0, 255, 0, 255);
615 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
616 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
617 255, 0, 0, 255);
618 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
619 DEFAULT_DISPLAY_HEIGHT),
620 0, 0, 255, 255);
621}
622
623template <typename SourceVariant>
624void RenderEngineTest::fillBufferCheckersRotate270() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700625 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_270);
Alec Mouri1089aed2018-10-25 21:33:57 -0700626 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 255,
627 255);
628 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
629 DEFAULT_DISPLAY_HEIGHT / 2),
630 0, 0, 0, 0);
631 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
632 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
633 0, 255, 0, 255);
634 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
635 DEFAULT_DISPLAY_HEIGHT),
636 255, 0, 0, 255);
637}
638
639template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800640void RenderEngineTest::fillBufferWithLayerTransform() {
Alec Mouri1089aed2018-10-25 21:33:57 -0700641 renderengine::DisplaySettings settings;
642 settings.physicalDisplay = fullscreenRect();
643 // Here logical space is 2x2
644 settings.clip = Rect(2, 2);
645
Vishnu Nair9b079a22020-01-21 14:36:08 -0800646 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700647
648 renderengine::LayerSettings layer;
649 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
650 // Translate one pixel diagonally
651 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 -0800652 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700653 layer.source.solidColor = half3(1.0f, 0.0f, 0.0f);
654 layer.alpha = 1.0f;
655
Vishnu Nair9b079a22020-01-21 14:36:08 -0800656 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700657
658 invokeDraw(settings, layers, mBuffer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800659}
Alec Mouri1089aed2018-10-25 21:33:57 -0700660
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800661template <typename SourceVariant>
662void RenderEngineTest::fillBufferLayerTransform() {
663 fillBufferWithLayerTransform<SourceVariant>();
Alec Mouri1089aed2018-10-25 21:33:57 -0700664 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0, 0);
665 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 0);
666 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
667 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
668 255, 0, 0, 255);
669}
670
671template <typename SourceVariant>
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800672void RenderEngineTest::fillBufferWithColorTransform(bool useColorManagement) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700673 renderengine::DisplaySettings settings;
674 settings.physicalDisplay = fullscreenRect();
675 settings.clip = Rect(1, 1);
676
Vishnu Nair9b079a22020-01-21 14:36:08 -0800677 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700678
679 renderengine::LayerSettings layer;
680 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800681 SourceVariant::fillColor(layer, 0.5f, 0.25f, 0.125f, this, useColorManagement);
Alec Mouri1089aed2018-10-25 21:33:57 -0700682 layer.alpha = 1.0f;
683
684 // construct a fake color matrix
685 // annihilate green and blue channels
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800686 settings.colorTransform = mat4::scale(vec4(0.9f, 0, 0, 1));
Alec Mouri1089aed2018-10-25 21:33:57 -0700687 // set red channel to red + green
688 layer.colorTransform = mat4(1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
689
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800690 layer.alpha = 1.0f;
691 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
692
Vishnu Nair9b079a22020-01-21 14:36:08 -0800693 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700694
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800695 invokeDraw(settings, layers, mBuffer, useColorManagement);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800696}
Alec Mouri1089aed2018-10-25 21:33:57 -0700697
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800698template <typename SourceVariant>
699void RenderEngineTest::fillBufferColorTransform() {
700 fillBufferWithColorTransform<SourceVariant>();
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800701 expectBufferColor(fullscreenRect(), 172, 0, 0, 255, 1);
702}
703
704template <typename SourceVariant>
705void RenderEngineTest::fillBufferColorTransformCM() {
706 fillBufferWithColorTransform<SourceVariant>(true);
707 expectBufferColor(fullscreenRect(), 126, 0, 0, 255, 1);
Alec Mouri1089aed2018-10-25 21:33:57 -0700708}
709
Alec Mouri7c94edb2018-12-03 21:23:26 -0800710template <typename SourceVariant>
711void RenderEngineTest::fillRedBufferWithRoundedCorners() {
712 renderengine::DisplaySettings settings;
713 settings.physicalDisplay = fullscreenRect();
714 settings.clip = fullscreenRect();
715
Vishnu Nair9b079a22020-01-21 14:36:08 -0800716 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri7c94edb2018-12-03 21:23:26 -0800717
718 renderengine::LayerSettings layer;
719 layer.geometry.boundaries = fullscreenRect().toFloatRect();
720 layer.geometry.roundedCornersRadius = 5.0f;
721 layer.geometry.roundedCornersCrop = fullscreenRect().toFloatRect();
722 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
723 layer.alpha = 1.0f;
724
Vishnu Nair9b079a22020-01-21 14:36:08 -0800725 layers.push_back(&layer);
Alec Mouri7c94edb2018-12-03 21:23:26 -0800726
727 invokeDraw(settings, layers, mBuffer);
728}
729
730template <typename SourceVariant>
731void RenderEngineTest::fillBufferWithRoundedCorners() {
732 fillRedBufferWithRoundedCorners<SourceVariant>();
733 // Corners should be ignored...
734 expectBufferColor(Rect(0, 0, 1, 1), 0, 0, 0, 0);
735 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH - 1, 0, DEFAULT_DISPLAY_WIDTH, 1), 0, 0, 0, 0);
736 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT - 1, 1, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 0);
737 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH - 1, DEFAULT_DISPLAY_HEIGHT - 1,
738 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
739 0, 0, 0, 0);
740 // ...And the non-rounded portion should be red.
741 // Other pixels may be anti-aliased, so let's not check those.
742 expectBufferColor(Rect(5, 5, DEFAULT_DISPLAY_WIDTH - 5, DEFAULT_DISPLAY_HEIGHT - 5), 255, 0, 0,
743 255);
744}
745
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000746template <typename SourceVariant>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800747void RenderEngineTest::fillBufferAndBlurBackground() {
748 char value[PROPERTY_VALUE_MAX];
749 property_get("ro.surface_flinger.supports_background_blur", value, "0");
750 if (!atoi(value)) {
751 // This device doesn't support blurs, no-op.
752 return;
753 }
754
755 auto blurRadius = 50;
756 auto center = DEFAULT_DISPLAY_WIDTH / 2;
757
758 renderengine::DisplaySettings settings;
759 settings.physicalDisplay = fullscreenRect();
760 settings.clip = fullscreenRect();
761
Vishnu Nair9b079a22020-01-21 14:36:08 -0800762 std::vector<const renderengine::LayerSettings*> layers;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800763
764 renderengine::LayerSettings backgroundLayer;
765 backgroundLayer.geometry.boundaries = fullscreenRect().toFloatRect();
766 SourceVariant::fillColor(backgroundLayer, 0.0f, 1.0f, 0.0f, this);
767 backgroundLayer.alpha = 1.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800768 layers.push_back(&backgroundLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800769
770 renderengine::LayerSettings leftLayer;
771 leftLayer.geometry.boundaries =
772 Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT).toFloatRect();
773 SourceVariant::fillColor(leftLayer, 1.0f, 0.0f, 0.0f, this);
774 leftLayer.alpha = 1.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800775 layers.push_back(&leftLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800776
777 renderengine::LayerSettings blurLayer;
778 blurLayer.geometry.boundaries = fullscreenRect().toFloatRect();
779 blurLayer.backgroundBlurRadius = blurRadius;
780 blurLayer.alpha = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800781 layers.push_back(&blurLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800782
783 invokeDraw(settings, layers, mBuffer);
784
785 expectBufferColor(Rect(center - 1, center - 5, center, center + 5), 150, 150, 0, 255,
786 50 /* tolerance */);
787 expectBufferColor(Rect(center, center - 5, center + 1, center + 5), 150, 150, 0, 255,
788 50 /* tolerance */);
789}
790
791template <typename SourceVariant>
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000792void RenderEngineTest::overlayCorners() {
793 renderengine::DisplaySettings settings;
794 settings.physicalDisplay = fullscreenRect();
795 settings.clip = fullscreenRect();
796
Vishnu Nair9b079a22020-01-21 14:36:08 -0800797 std::vector<const renderengine::LayerSettings*> layersFirst;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000798
799 renderengine::LayerSettings layerOne;
800 layerOne.geometry.boundaries =
801 FloatRect(0, 0, DEFAULT_DISPLAY_WIDTH / 3.0, DEFAULT_DISPLAY_HEIGHT / 3.0);
802 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
803 layerOne.alpha = 0.2;
804
Vishnu Nair9b079a22020-01-21 14:36:08 -0800805 layersFirst.push_back(&layerOne);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000806 invokeDraw(settings, layersFirst, mBuffer);
807 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3, DEFAULT_DISPLAY_HEIGHT / 3), 51, 0, 0, 51);
808 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3 + 1, DEFAULT_DISPLAY_HEIGHT / 3 + 1,
809 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
810 0, 0, 0, 0);
811
Vishnu Nair9b079a22020-01-21 14:36:08 -0800812 std::vector<const renderengine::LayerSettings*> layersSecond;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000813 renderengine::LayerSettings layerTwo;
814 layerTwo.geometry.boundaries =
815 FloatRect(DEFAULT_DISPLAY_WIDTH / 3.0, DEFAULT_DISPLAY_HEIGHT / 3.0,
816 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT);
817 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
818 layerTwo.alpha = 1.0f;
819
Vishnu Nair9b079a22020-01-21 14:36:08 -0800820 layersSecond.push_back(&layerTwo);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000821 invokeDraw(settings, layersSecond, mBuffer);
822
823 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3, DEFAULT_DISPLAY_HEIGHT / 3), 0, 0, 0, 0);
824 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3 + 1, DEFAULT_DISPLAY_HEIGHT / 3 + 1,
825 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
826 0, 255, 0, 255);
827}
828
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800829void RenderEngineTest::fillRedBufferTextureTransform() {
830 renderengine::DisplaySettings settings;
831 settings.physicalDisplay = fullscreenRect();
832 settings.clip = Rect(1, 1);
833
Vishnu Nair9b079a22020-01-21 14:36:08 -0800834 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800835
836 renderengine::LayerSettings layer;
837 // Here will allocate a checker board texture, but transform texture
838 // coordinates so that only the upper left is applied.
839 sp<GraphicBuffer> buf = allocateSourceBuffer(2, 2);
840 uint32_t texName;
841 RenderEngineTest::sRE->genTextures(1, &texName);
842 this->mTexNames.push_back(texName);
843
844 uint8_t* pixels;
845 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
846 reinterpret_cast<void**>(&pixels));
847 // Red top left, Green top right, Blue bottom left, Black bottom right
848 pixels[0] = 255;
849 pixels[1] = 0;
850 pixels[2] = 0;
851 pixels[3] = 255;
852 pixels[4] = 0;
853 pixels[5] = 255;
854 pixels[6] = 0;
855 pixels[7] = 255;
856 pixels[8] = 0;
857 pixels[9] = 0;
858 pixels[10] = 255;
859 pixels[11] = 255;
860 buf->unlock();
861
862 layer.source.buffer.buffer = buf;
863 layer.source.buffer.textureName = texName;
864 // Transform coordinates to only be inside the red quadrant.
865 layer.source.buffer.textureTransform = mat4::scale(vec4(0.2, 0.2, 1, 1));
866 layer.alpha = 1.0f;
867 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
868
Vishnu Nair9b079a22020-01-21 14:36:08 -0800869 layers.push_back(&layer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800870
871 invokeDraw(settings, layers, mBuffer);
872}
873
874void RenderEngineTest::fillBufferTextureTransform() {
875 fillRedBufferTextureTransform();
876 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
877}
878
879void RenderEngineTest::fillRedBufferWithPremultiplyAlpha() {
880 renderengine::DisplaySettings settings;
881 settings.physicalDisplay = fullscreenRect();
882 // Here logical space is 1x1
883 settings.clip = Rect(1, 1);
884
Vishnu Nair9b079a22020-01-21 14:36:08 -0800885 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800886
887 renderengine::LayerSettings layer;
888 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
889 uint32_t texName;
890 RenderEngineTest::sRE->genTextures(1, &texName);
891 this->mTexNames.push_back(texName);
892
893 uint8_t* pixels;
894 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
895 reinterpret_cast<void**>(&pixels));
896 pixels[0] = 255;
897 pixels[1] = 0;
898 pixels[2] = 0;
899 pixels[3] = 255;
900 buf->unlock();
901
902 layer.source.buffer.buffer = buf;
903 layer.source.buffer.textureName = texName;
904 layer.source.buffer.usePremultipliedAlpha = true;
905 layer.alpha = 0.5f;
906 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
907
Vishnu Nair9b079a22020-01-21 14:36:08 -0800908 layers.push_back(&layer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800909
910 invokeDraw(settings, layers, mBuffer);
911}
912
913void RenderEngineTest::fillBufferWithPremultiplyAlpha() {
914 fillRedBufferWithPremultiplyAlpha();
915 expectBufferColor(fullscreenRect(), 128, 0, 0, 128);
916}
917
918void RenderEngineTest::fillRedBufferWithoutPremultiplyAlpha() {
919 renderengine::DisplaySettings settings;
920 settings.physicalDisplay = fullscreenRect();
921 // Here logical space is 1x1
922 settings.clip = Rect(1, 1);
923
Vishnu Nair9b079a22020-01-21 14:36:08 -0800924 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800925
926 renderengine::LayerSettings layer;
927 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
928 uint32_t texName;
929 RenderEngineTest::sRE->genTextures(1, &texName);
930 this->mTexNames.push_back(texName);
931
932 uint8_t* pixels;
933 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
934 reinterpret_cast<void**>(&pixels));
935 pixels[0] = 255;
936 pixels[1] = 0;
937 pixels[2] = 0;
938 pixels[3] = 255;
939 buf->unlock();
940
941 layer.source.buffer.buffer = buf;
942 layer.source.buffer.textureName = texName;
943 layer.source.buffer.usePremultipliedAlpha = false;
944 layer.alpha = 0.5f;
945 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
946
Vishnu Nair9b079a22020-01-21 14:36:08 -0800947 layers.push_back(&layer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800948
949 invokeDraw(settings, layers, mBuffer);
950}
951
952void RenderEngineTest::fillBufferWithoutPremultiplyAlpha() {
953 fillRedBufferWithoutPremultiplyAlpha();
wukui16f3c0bb2020-08-05 20:35:29 +0800954 expectBufferColor(fullscreenRect(), 128, 0, 0, 128, 1);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800955}
956
Alec Mouriac335532018-11-12 15:01:33 -0800957void RenderEngineTest::clearLeftRegion() {
958 renderengine::DisplaySettings settings;
959 settings.physicalDisplay = fullscreenRect();
960 // Here logical space is 4x4
961 settings.clip = Rect(4, 4);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700962 settings.clearRegion = Region(Rect(2, 4));
Vishnu Nair9b079a22020-01-21 14:36:08 -0800963 std::vector<const renderengine::LayerSettings*> layers;
Peiyong Lind8460c82020-07-28 16:04:22 -0700964 // fake layer, without bounds should not render anything
Alec Mouriac335532018-11-12 15:01:33 -0800965 renderengine::LayerSettings layer;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800966 layers.push_back(&layer);
Alec Mouriac335532018-11-12 15:01:33 -0800967 invokeDraw(settings, layers, mBuffer);
968}
969
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000970void RenderEngineTest::clearRegion() {
Alec Mouriac335532018-11-12 15:01:33 -0800971 // Reuse mBuffer
972 clearLeftRegion();
973 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 255);
974 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
975 DEFAULT_DISPLAY_HEIGHT),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000976 0, 0, 0, 0);
Alec Mouriac335532018-11-12 15:01:33 -0800977}
978
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800979template <typename SourceVariant>
980void RenderEngineTest::drawShadow(const renderengine::LayerSettings& castingLayer,
981 const renderengine::ShadowSettings& shadow,
982 const ubyte4& casterColor, const ubyte4& backgroundColor) {
983 renderengine::DisplaySettings settings;
984 settings.physicalDisplay = fullscreenRect();
985 settings.clip = fullscreenRect();
986
Vishnu Nair9b079a22020-01-21 14:36:08 -0800987 std::vector<const renderengine::LayerSettings*> layers;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800988
989 // add background layer
990 renderengine::LayerSettings bgLayer;
991 bgLayer.geometry.boundaries = fullscreenRect().toFloatRect();
992 ColorSourceVariant::fillColor(bgLayer, backgroundColor.r / 255.0f, backgroundColor.g / 255.0f,
993 backgroundColor.b / 255.0f, this);
994 bgLayer.alpha = backgroundColor.a / 255.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800995 layers.push_back(&bgLayer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800996
997 // add shadow layer
998 renderengine::LayerSettings shadowLayer;
999 shadowLayer.geometry.boundaries = castingLayer.geometry.boundaries;
1000 shadowLayer.alpha = castingLayer.alpha;
1001 shadowLayer.shadow = shadow;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001002 layers.push_back(&shadowLayer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001003
1004 // add layer casting the shadow
1005 renderengine::LayerSettings layer = castingLayer;
1006 SourceVariant::fillColor(layer, casterColor.r / 255.0f, casterColor.g / 255.0f,
1007 casterColor.b / 255.0f, this);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001008 layers.push_back(&layer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001009
1010 invokeDraw(settings, layers, mBuffer);
1011}
1012
Alec Mouri1089aed2018-10-25 21:33:57 -07001013TEST_F(RenderEngineTest, drawLayers_noLayersToDraw) {
1014 drawEmptyLayers();
1015}
1016
Alec Mourid43ccab2019-03-13 12:23:45 -07001017TEST_F(RenderEngineTest, drawLayers_nullOutputBuffer) {
1018 renderengine::DisplaySettings settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001019 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001020 renderengine::LayerSettings layer;
1021 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1022 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001023 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001024 base::unique_fd fence;
Alec Mourife0d72b2019-03-21 14:05:56 -07001025 status_t status = sRE->drawLayers(settings, layers, nullptr, true, base::unique_fd(), &fence);
Alec Mourid43ccab2019-03-13 12:23:45 -07001026
1027 ASSERT_EQ(BAD_VALUE, status);
1028}
1029
1030TEST_F(RenderEngineTest, drawLayers_nullOutputFence) {
1031 renderengine::DisplaySettings settings;
1032 settings.physicalDisplay = fullscreenRect();
1033 settings.clip = fullscreenRect();
1034
Vishnu Nair9b079a22020-01-21 14:36:08 -08001035 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001036 renderengine::LayerSettings layer;
1037 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1038 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1039 layer.alpha = 1.0;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001040 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001041
Ana Krulecfc874ae2020-02-22 15:39:32 -08001042 status_t status = sRE->drawLayers(settings, layers, mBuffer, true, base::unique_fd(), nullptr);
Alec Mourid43ccab2019-03-13 12:23:45 -07001043 sCurrentBuffer = mBuffer;
1044 ASSERT_EQ(NO_ERROR, status);
1045 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
1046}
1047
Alec Mourife0d72b2019-03-21 14:05:56 -07001048TEST_F(RenderEngineTest, drawLayers_doesNotCacheFramebuffer) {
1049 renderengine::DisplaySettings settings;
1050 settings.physicalDisplay = fullscreenRect();
1051 settings.clip = fullscreenRect();
1052
Vishnu Nair9b079a22020-01-21 14:36:08 -08001053 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourife0d72b2019-03-21 14:05:56 -07001054 renderengine::LayerSettings layer;
1055 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1056 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1057 layer.alpha = 1.0;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001058 layers.push_back(&layer);
Alec Mourife0d72b2019-03-21 14:05:56 -07001059
Ana Krulecfc874ae2020-02-22 15:39:32 -08001060 status_t status = sRE->drawLayers(settings, layers, mBuffer, false, base::unique_fd(), nullptr);
Alec Mourife0d72b2019-03-21 14:05:56 -07001061 sCurrentBuffer = mBuffer;
1062 ASSERT_EQ(NO_ERROR, status);
1063 ASSERT_FALSE(sRE->isFramebufferImageCachedForTesting(mBuffer->getId()));
1064 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
1065}
1066
Alec Mouri1089aed2018-10-25 21:33:57 -07001067TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_colorSource) {
1068 fillRedBuffer<ColorSourceVariant>();
1069}
1070
1071TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_colorSource) {
1072 fillGreenBuffer<ColorSourceVariant>();
1073}
1074
1075TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_colorSource) {
1076 fillBlueBuffer<ColorSourceVariant>();
1077}
1078
1079TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_colorSource) {
1080 fillRedTransparentBuffer<ColorSourceVariant>();
1081}
1082
1083TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_colorSource) {
1084 fillBufferPhysicalOffset<ColorSourceVariant>();
1085}
1086
1087TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_colorSource) {
1088 fillBufferCheckersRotate0<ColorSourceVariant>();
1089}
1090
1091TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_colorSource) {
1092 fillBufferCheckersRotate90<ColorSourceVariant>();
1093}
1094
1095TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_colorSource) {
1096 fillBufferCheckersRotate180<ColorSourceVariant>();
1097}
1098
1099TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_colorSource) {
1100 fillBufferCheckersRotate270<ColorSourceVariant>();
1101}
1102
1103TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_colorSource) {
1104 fillBufferLayerTransform<ColorSourceVariant>();
1105}
1106
1107TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_colorSource) {
KaiChieh Chuang436fc192020-09-07 13:48:42 +08001108 fillBufferColorTransform<ColorSourceVariant>();
1109}
1110
1111TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransformCM_colorSource) {
1112 fillBufferColorTransformCM<ColorSourceVariant>();
Alec Mouri6e57f682018-09-29 20:45:08 -07001113}
1114
Alec Mouri7c94edb2018-12-03 21:23:26 -08001115TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_colorSource) {
1116 fillBufferWithRoundedCorners<ColorSourceVariant>();
1117}
1118
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001119TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_colorSource) {
1120 fillBufferAndBlurBackground<ColorSourceVariant>();
1121}
1122
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001123TEST_F(RenderEngineTest, drawLayers_overlayCorners_colorSource) {
1124 overlayCorners<ColorSourceVariant>();
1125}
1126
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001127TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_opaqueBufferSource) {
1128 fillRedBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1129}
1130
1131TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_opaqueBufferSource) {
1132 fillGreenBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1133}
1134
1135TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_opaqueBufferSource) {
1136 fillBlueBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1137}
1138
1139TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_opaqueBufferSource) {
1140 fillRedTransparentBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1141}
1142
1143TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_opaqueBufferSource) {
1144 fillBufferPhysicalOffset<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1145}
1146
1147TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_opaqueBufferSource) {
1148 fillBufferCheckersRotate0<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1149}
1150
1151TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_opaqueBufferSource) {
1152 fillBufferCheckersRotate90<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1153}
1154
1155TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_opaqueBufferSource) {
1156 fillBufferCheckersRotate180<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1157}
1158
1159TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_opaqueBufferSource) {
1160 fillBufferCheckersRotate270<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1161}
1162
1163TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_opaqueBufferSource) {
1164 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1165}
1166
1167TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_opaqueBufferSource) {
KaiChieh Chuang436fc192020-09-07 13:48:42 +08001168 fillBufferColorTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1169}
1170
1171TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransformCM_opaqueBufferSource) {
1172 fillBufferColorTransformCM<BufferSourceVariant<ForceOpaqueBufferVariant>>();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001173}
1174
Alec Mouri7c94edb2018-12-03 21:23:26 -08001175TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_opaqueBufferSource) {
1176 fillBufferWithRoundedCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1177}
1178
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001179TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_opaqueBufferSource) {
1180 fillBufferAndBlurBackground<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1181}
1182
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001183TEST_F(RenderEngineTest, drawLayers_overlayCorners_opaqueBufferSource) {
1184 overlayCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1185}
1186
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001187TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_bufferSource) {
1188 fillRedBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1189}
1190
1191TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_bufferSource) {
1192 fillGreenBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1193}
1194
1195TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_bufferSource) {
1196 fillBlueBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1197}
1198
1199TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_bufferSource) {
1200 fillRedTransparentBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1201}
1202
1203TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_bufferSource) {
1204 fillBufferPhysicalOffset<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1205}
1206
1207TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_bufferSource) {
1208 fillBufferCheckersRotate0<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1209}
1210
1211TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_bufferSource) {
1212 fillBufferCheckersRotate90<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1213}
1214
1215TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_bufferSource) {
1216 fillBufferCheckersRotate180<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1217}
1218
1219TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_bufferSource) {
1220 fillBufferCheckersRotate270<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1221}
1222
1223TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_bufferSource) {
1224 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1225}
1226
1227TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_bufferSource) {
KaiChieh Chuang436fc192020-09-07 13:48:42 +08001228 fillBufferColorTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1229}
1230
1231TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransformCM_bufferSource) {
1232 fillBufferColorTransformCM<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001233}
1234
Alec Mouri7c94edb2018-12-03 21:23:26 -08001235TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_bufferSource) {
1236 fillBufferWithRoundedCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1237}
1238
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001239TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_bufferSource) {
1240 fillBufferAndBlurBackground<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1241}
1242
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001243TEST_F(RenderEngineTest, drawLayers_overlayCorners_bufferSource) {
1244 overlayCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1245}
1246
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001247TEST_F(RenderEngineTest, drawLayers_fillBufferTextureTransform) {
1248 fillBufferTextureTransform();
1249}
1250
1251TEST_F(RenderEngineTest, drawLayers_fillBuffer_premultipliesAlpha) {
1252 fillBufferWithPremultiplyAlpha();
1253}
1254
1255TEST_F(RenderEngineTest, drawLayers_fillBuffer_withoutPremultiplyingAlpha) {
1256 fillBufferWithoutPremultiplyAlpha();
1257}
1258
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001259TEST_F(RenderEngineTest, drawLayers_clearRegion) {
1260 clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -08001261}
1262
Alec Mourid43ccab2019-03-13 12:23:45 -07001263TEST_F(RenderEngineTest, drawLayers_fillsBufferAndCachesImages) {
1264 renderengine::DisplaySettings settings;
1265 settings.physicalDisplay = fullscreenRect();
1266 settings.clip = fullscreenRect();
1267
Vishnu Nair9b079a22020-01-21 14:36:08 -08001268 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001269
1270 renderengine::LayerSettings layer;
1271 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1272 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1273
Vishnu Nair9b079a22020-01-21 14:36:08 -08001274 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001275 invokeDraw(settings, layers, mBuffer);
1276 uint64_t bufferId = layer.source.buffer.buffer->getId();
1277 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001278 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1279 sRE->unbindExternalTextureBufferForTesting(bufferId);
1280 std::lock_guard<std::mutex> lock(barrier->mutex);
1281 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1282 [&]() REQUIRES(barrier->mutex) {
1283 return barrier->isOpen;
1284 }));
Alec Mourid43ccab2019-03-13 12:23:45 -07001285 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001286 EXPECT_EQ(NO_ERROR, barrier->result);
Alec Mourid43ccab2019-03-13 12:23:45 -07001287}
1288
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001289TEST_F(RenderEngineTest, cacheExternalBuffer_withNullBuffer) {
Alec Mouri16a99402019-07-29 16:37:30 -07001290 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1291 sRE->cacheExternalTextureBufferForTesting(nullptr);
1292 std::lock_guard<std::mutex> lock(barrier->mutex);
1293 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1294 [&]() REQUIRES(barrier->mutex) {
1295 return barrier->isOpen;
1296 }));
1297 EXPECT_TRUE(barrier->isOpen);
1298 EXPECT_EQ(BAD_VALUE, barrier->result);
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001299}
1300
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001301TEST_F(RenderEngineTest, cacheExternalBuffer_cachesImages) {
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001302 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
1303 uint64_t bufferId = buf->getId();
Alec Mouri16a99402019-07-29 16:37:30 -07001304 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1305 sRE->cacheExternalTextureBufferForTesting(buf);
1306 {
1307 std::lock_guard<std::mutex> lock(barrier->mutex);
1308 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1309 [&]() REQUIRES(barrier->mutex) {
1310 return barrier->isOpen;
1311 }));
1312 EXPECT_EQ(NO_ERROR, barrier->result);
1313 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001314 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001315 barrier = sRE->unbindExternalTextureBufferForTesting(bufferId);
1316 {
1317 std::lock_guard<std::mutex> lock(barrier->mutex);
1318 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1319 [&]() REQUIRES(barrier->mutex) {
1320 return barrier->isOpen;
1321 }));
1322 EXPECT_EQ(NO_ERROR, barrier->result);
1323 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001324 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1325}
1326
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001327TEST_F(RenderEngineTest, drawLayers_fillShadow_casterLayerMinSize) {
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(1, 1);
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_casterColorLayer) {
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<ColorSourceVariant>(castingLayer, settings, casterColor, backgroundColor);
1358 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1359}
1360
1361TEST_F(RenderEngineTest, drawLayers_fillShadow_casterOpaqueBufferLayer) {
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.alpha = 1.0f;
1370 renderengine::ShadowSettings settings =
1371 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1372 false /* casterIsTranslucent */);
1373
1374 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1375 backgroundColor);
1376 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1377}
1378
1379TEST_F(RenderEngineTest, drawLayers_fillShadow_casterWithRoundedCorner) {
1380 const ubyte4 casterColor(255, 0, 0, 255);
1381 const ubyte4 backgroundColor(255, 255, 255, 255);
1382 const float shadowLength = 5.0f;
1383 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1384 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1385 renderengine::LayerSettings castingLayer;
1386 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1387 castingLayer.geometry.roundedCornersRadius = 3.0f;
1388 castingLayer.geometry.roundedCornersCrop = casterBounds.toFloatRect();
1389 castingLayer.alpha = 1.0f;
1390 renderengine::ShadowSettings settings =
1391 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1392 false /* casterIsTranslucent */);
1393
1394 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1395 backgroundColor);
1396 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1397}
1398
1399TEST_F(RenderEngineTest, drawLayers_fillShadow_translucentCasterWithAlpha) {
1400 const ubyte4 casterColor(255, 0, 0, 255);
1401 const ubyte4 backgroundColor(255, 255, 255, 255);
1402 const float shadowLength = 5.0f;
1403 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1404 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1405 renderengine::LayerSettings castingLayer;
1406 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1407 castingLayer.alpha = 0.5f;
1408 renderengine::ShadowSettings settings =
1409 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1410 true /* casterIsTranslucent */);
1411
1412 drawShadow<BufferSourceVariant<RelaxOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1413 backgroundColor);
1414
1415 // verify only the background since the shadow will draw behind the caster
1416 const float shadowInset = settings.length * -1.0f;
1417 const Rect casterWithShadow =
1418 Rect(casterBounds).inset(shadowInset, shadowInset, shadowInset, shadowInset);
1419 const Region backgroundRegion = Region(fullscreenRect()).subtractSelf(casterWithShadow);
1420 expectBufferColor(backgroundRegion, backgroundColor.r, backgroundColor.g, backgroundColor.b,
1421 backgroundColor.a);
1422}
1423
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001424TEST_F(RenderEngineTest, cleanupPostRender_cleansUpOnce) {
1425 renderengine::DisplaySettings settings;
1426 settings.physicalDisplay = fullscreenRect();
1427 settings.clip = fullscreenRect();
1428
1429 std::vector<const renderengine::LayerSettings*> layers;
1430 renderengine::LayerSettings layer;
1431 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1432 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1433 layer.alpha = 1.0;
1434 layers.push_back(&layer);
1435
1436 base::unique_fd fenceOne;
1437 sRE->drawLayers(settings, layers, mBuffer, true, base::unique_fd(), &fenceOne);
1438 base::unique_fd fenceTwo;
1439 sRE->drawLayers(settings, layers, mBuffer, true, std::move(fenceOne), &fenceTwo);
1440
1441 const int fd = fenceTwo.get();
1442 if (fd >= 0) {
1443 sync_wait(fd, -1);
1444 }
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001445 // Only cleanup the first time.
Alec Mouri368e1582020-08-13 10:14:29 -07001446 EXPECT_TRUE(sRE->cleanupPostRender(
1447 renderengine::RenderEngine::CleanupMode::CLEAN_OUTPUT_RESOURCES));
1448 EXPECT_FALSE(sRE->cleanupPostRender(
1449 renderengine::RenderEngine::CleanupMode::CLEAN_OUTPUT_RESOURCES));
1450}
1451
1452TEST_F(RenderEngineTest, cleanupPostRender_whenCleaningAll_replacesTextureMemory) {
1453 renderengine::DisplaySettings settings;
1454 settings.physicalDisplay = fullscreenRect();
1455 settings.clip = fullscreenRect();
1456
1457 std::vector<const renderengine::LayerSettings*> layers;
1458 renderengine::LayerSettings layer;
1459 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1460 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1461 layer.alpha = 1.0;
1462 layers.push_back(&layer);
1463
1464 base::unique_fd fence;
1465 sRE->drawLayers(settings, layers, mBuffer, true, base::unique_fd(), &fence);
1466
1467 const int fd = fence.get();
1468 if (fd >= 0) {
1469 sync_wait(fd, -1);
1470 }
1471
1472 uint64_t bufferId = layer.source.buffer.buffer->getId();
1473 uint32_t texName = layer.source.buffer.textureName;
1474 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
1475 EXPECT_EQ(bufferId, sRE->getBufferIdForTextureNameForTesting(texName));
1476
1477 EXPECT_TRUE(sRE->cleanupPostRender(renderengine::RenderEngine::CleanupMode::CLEAN_ALL));
1478
1479 // Now check that our view of memory is good.
1480 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1481 EXPECT_EQ(std::nullopt, sRE->getBufferIdForTextureNameForTesting(bufferId));
1482 EXPECT_TRUE(sRE->isTextureNameKnownForTesting(texName));
Lingfeng Yang2e4fef62020-04-24 14:48:43 +00001483}
1484
Alec Mouri6e57f682018-09-29 20:45:08 -07001485} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001486
1487// TODO(b/129481165): remove the #pragma below and fix conversion issues
1488#pragma clang diagnostic pop // ignored "-Wconversion"