blob: 1ec941201951c3c180ce692775cbd294984ff869 [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() {
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +080041 renderengine::RenderEngineCreationArgs reCreationArgs =
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)
48 .setSupportsBackgroundBlur(true)
49 .setContextPriority(renderengine::RenderEngine::ContextPriority::MEDIUM)
50 .build();
51 sRE = renderengine::gl::GLESRenderEngine::create(reCreationArgs);
52
53 reCreationArgs.useColorManagement = true;
54 sRECM = renderengine::gl::GLESRenderEngine::create(reCreationArgs);
Alec Mourid43ccab2019-03-13 12:23:45 -070055 }
56
57 static void TearDownTestSuite() {
58 // The ordering here is important - sCurrentBuffer must live longer
59 // than RenderEngine to avoid a null reference on tear-down.
60 sRE = nullptr;
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +080061 sRECM = nullptr;
Alec Mourid43ccab2019-03-13 12:23:45 -070062 sCurrentBuffer = nullptr;
63 }
64
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080065 static sp<GraphicBuffer> allocateDefaultBuffer() {
Alec Mouri1089aed2018-10-25 21:33:57 -070066 return new GraphicBuffer(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT,
67 HAL_PIXEL_FORMAT_RGBA_8888, 1,
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080068 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
69 GRALLOC_USAGE_HW_RENDER,
Alec Mouri1089aed2018-10-25 21:33:57 -070070 "output");
Alec Mouri6e57f682018-09-29 20:45:08 -070071 }
72
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080073 // Allocates a 1x1 buffer to fill with a solid color
74 static sp<GraphicBuffer> allocateSourceBuffer(uint32_t width, uint32_t height) {
75 return new GraphicBuffer(width, height, HAL_PIXEL_FORMAT_RGBA_8888, 1,
76 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
77 GRALLOC_USAGE_HW_TEXTURE,
78 "input");
79 }
80
Alec Mouri1089aed2018-10-25 21:33:57 -070081 RenderEngineTest() { mBuffer = allocateDefaultBuffer(); }
82
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080083 ~RenderEngineTest() {
Vishnu Nair16efdbf2019-12-10 11:55:42 -080084 if (WRITE_BUFFER_TO_FILE_ON_FAILURE && ::testing::Test::HasFailure()) {
85 writeBufferToFile("/data/texture_out_");
86 }
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080087 for (uint32_t texName : mTexNames) {
88 sRE->deleteTextures(1, &texName);
Alec Mouri56f295e2020-08-13 10:14:29 -070089 EXPECT_FALSE(sRE->isTextureNameKnownForTesting(texName));
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080090 }
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +080091 for (uint32_t texName : mTexNamesCM) {
92 sRECM->deleteTextures(1, &texName);
93 }
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080094 }
95
Vishnu Nair16efdbf2019-12-10 11:55:42 -080096 void writeBufferToFile(const char* basename) {
97 std::string filename(basename);
98 filename.append(::testing::UnitTest::GetInstance()->current_test_info()->name());
99 filename.append(".ppm");
100 std::ofstream file(filename.c_str(), std::ios::binary);
101 if (!file.is_open()) {
102 ALOGE("Unable to open file: %s", filename.c_str());
103 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
104 "surfaceflinger to write debug images");
105 return;
106 }
107
Alec Mouri1089aed2018-10-25 21:33:57 -0700108 uint8_t* pixels;
109 mBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
110 reinterpret_cast<void**>(&pixels));
111
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800112 file << "P6\n";
113 file << mBuffer->getWidth() << "\n";
114 file << mBuffer->getHeight() << "\n";
115 file << 255 << "\n";
116
117 std::vector<uint8_t> outBuffer(mBuffer->getWidth() * mBuffer->getHeight() * 3);
118 auto outPtr = reinterpret_cast<uint8_t*>(outBuffer.data());
119
120 for (int32_t j = 0; j < mBuffer->getHeight(); j++) {
121 const uint8_t* src = pixels + (mBuffer->getStride() * j) * 4;
122 for (int32_t i = 0; i < mBuffer->getWidth(); i++) {
123 // Only copy R, G and B components
124 outPtr[0] = src[0];
125 outPtr[1] = src[1];
126 outPtr[2] = src[2];
127 outPtr += 3;
128
129 src += 4;
130 }
131 }
132 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
133 mBuffer->unlock();
134 }
135
136 void expectBufferColor(const Region& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
137 size_t c;
138 Rect const* rect = region.getArray(&c);
139 for (size_t i = 0; i < c; i++, rect++) {
140 expectBufferColor(*rect, r, g, b, a);
141 }
142 }
143
144 void expectBufferColor(const Rect& rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a,
145 uint8_t tolerance = 0) {
146 auto colorCompare = [tolerance](const uint8_t* colorA, const uint8_t* colorB) {
147 auto colorBitCompare = [tolerance](uint8_t a, uint8_t b) {
148 uint8_t tmp = a >= b ? a - b : b - a;
149 return tmp <= tolerance;
150 };
151 return std::equal(colorA, colorA + 4, colorB, colorBitCompare);
Alec Mouri1089aed2018-10-25 21:33:57 -0700152 };
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800153
154 expectBufferColor(rect, r, g, b, a, colorCompare);
155 }
156
157 void expectBufferColor(const Rect& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a,
158 std::function<bool(const uint8_t* a, const uint8_t* b)> colorCompare) {
159 uint8_t* pixels;
160 mBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
161 reinterpret_cast<void**>(&pixels));
Alec Mouri1089aed2018-10-25 21:33:57 -0700162 int32_t maxFails = 10;
163 int32_t fails = 0;
164 for (int32_t j = 0; j < region.getHeight(); j++) {
165 const uint8_t* src =
166 pixels + (mBuffer->getStride() * (region.top + j) + region.left) * 4;
167 for (int32_t i = 0; i < region.getWidth(); i++) {
168 const uint8_t expected[4] = {r, g, b, a};
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800169 bool equal = colorCompare(src, expected);
Alec Mouri1089aed2018-10-25 21:33:57 -0700170 EXPECT_TRUE(equal)
171 << "pixel @ (" << region.left + i << ", " << region.top + j << "): "
172 << "expected (" << static_cast<uint32_t>(r) << ", "
173 << static_cast<uint32_t>(g) << ", " << static_cast<uint32_t>(b) << ", "
174 << static_cast<uint32_t>(a) << "), "
175 << "got (" << static_cast<uint32_t>(src[0]) << ", "
176 << static_cast<uint32_t>(src[1]) << ", " << static_cast<uint32_t>(src[2])
177 << ", " << static_cast<uint32_t>(src[3]) << ")";
178 src += 4;
179 if (!equal && ++fails >= maxFails) {
180 break;
181 }
182 }
183 if (fails >= maxFails) {
184 break;
185 }
186 }
187 mBuffer->unlock();
188 }
189
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800190 void expectAlpha(const Rect& rect, uint8_t a) {
191 auto colorCompare = [](const uint8_t* colorA, const uint8_t* colorB) {
192 return colorA[3] == colorB[3];
193 };
194 expectBufferColor(rect, 0.0f /* r */, 0.0f /*g */, 0.0f /* b */, a, colorCompare);
195 }
196
197 void expectShadowColor(const renderengine::LayerSettings& castingLayer,
198 const renderengine::ShadowSettings& shadow, const ubyte4& casterColor,
199 const ubyte4& backgroundColor) {
200 const Rect casterRect(castingLayer.geometry.boundaries);
201 Region casterRegion = Region(casterRect);
202 const float casterCornerRadius = castingLayer.geometry.roundedCornersRadius;
203 if (casterCornerRadius > 0.0f) {
204 // ignore the corners if a corner radius is set
205 Rect cornerRect(casterCornerRadius, casterCornerRadius);
206 casterRegion.subtractSelf(cornerRect.offsetTo(casterRect.left, casterRect.top));
207 casterRegion.subtractSelf(
208 cornerRect.offsetTo(casterRect.right - casterCornerRadius, casterRect.top));
209 casterRegion.subtractSelf(
210 cornerRect.offsetTo(casterRect.left, casterRect.bottom - casterCornerRadius));
211 casterRegion.subtractSelf(cornerRect.offsetTo(casterRect.right - casterCornerRadius,
212 casterRect.bottom - casterCornerRadius));
213 }
214
215 const float shadowInset = shadow.length * -1.0f;
216 const Rect casterWithShadow =
217 Rect(casterRect).inset(shadowInset, shadowInset, shadowInset, shadowInset);
218 const Region shadowRegion = Region(casterWithShadow).subtractSelf(casterRect);
219 const Region backgroundRegion = Region(fullscreenRect()).subtractSelf(casterWithShadow);
220
221 // verify casting layer
222 expectBufferColor(casterRegion, casterColor.r, casterColor.g, casterColor.b, casterColor.a);
223
224 // verify shadows by testing just the alpha since its difficult to validate the shadow color
225 size_t c;
226 Rect const* r = shadowRegion.getArray(&c);
227 for (size_t i = 0; i < c; i++, r++) {
228 expectAlpha(*r, 255);
229 }
230
231 // verify background
232 expectBufferColor(backgroundRegion, backgroundColor.r, backgroundColor.g, backgroundColor.b,
233 backgroundColor.a);
234 }
235
236 static renderengine::ShadowSettings getShadowSettings(const vec2& casterPos, float shadowLength,
237 bool casterIsTranslucent) {
238 renderengine::ShadowSettings shadow;
239 shadow.ambientColor = {0.0f, 0.0f, 0.0f, 0.039f};
240 shadow.spotColor = {0.0f, 0.0f, 0.0f, 0.19f};
241 shadow.lightPos = vec3(casterPos.x, casterPos.y, 0);
242 shadow.lightRadius = 0.0f;
243 shadow.length = shadowLength;
244 shadow.casterIsTranslucent = casterIsTranslucent;
245 return shadow;
246 }
247
Alec Mouri1089aed2018-10-25 21:33:57 -0700248 static Rect fullscreenRect() { return Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT); }
249
250 static Rect offsetRect() {
251 return Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
252 DEFAULT_DISPLAY_HEIGHT);
253 }
254
255 static Rect offsetRectAtZero() {
256 return Rect(DEFAULT_DISPLAY_WIDTH - DEFAULT_DISPLAY_OFFSET,
257 DEFAULT_DISPLAY_HEIGHT - DEFAULT_DISPLAY_OFFSET);
258 }
259
Alec Mourid43ccab2019-03-13 12:23:45 -0700260 void invokeDraw(renderengine::DisplaySettings settings,
Vishnu Nair9b079a22020-01-21 14:36:08 -0800261 std::vector<const renderengine::LayerSettings*> layers,
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800262 sp<GraphicBuffer> buffer,
263 bool useColorManagement = false) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700264 base::unique_fd fence;
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800265 status_t status = useColorManagement ?
266 sRECM ->drawLayers(settings, layers, buffer->getNativeBuffer(), true,
267 base::unique_fd(), &fence) :
268 sRE->drawLayers(settings, layers, buffer->getNativeBuffer(), true,
Alec Mouri6338c9d2019-02-07 16:57:51 -0800269 base::unique_fd(), &fence);
Alec Mourid43ccab2019-03-13 12:23:45 -0700270 sCurrentBuffer = buffer;
Alec Mouri1089aed2018-10-25 21:33:57 -0700271
272 int fd = fence.release();
273 if (fd >= 0) {
274 sync_wait(fd, -1);
275 close(fd);
276 }
277
278 ASSERT_EQ(NO_ERROR, status);
Alec Mourid43ccab2019-03-13 12:23:45 -0700279 if (layers.size() > 0) {
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800280 if (useColorManagement) {
281 ASSERT_TRUE(sRECM->isFramebufferImageCachedForTesting(buffer->getId()));
282 } else {
283 ASSERT_TRUE(sRE->isFramebufferImageCachedForTesting(buffer->getId()));
284 }
Alec Mourid43ccab2019-03-13 12:23:45 -0700285 }
Alec Mouri1089aed2018-10-25 21:33:57 -0700286 }
287
Alec Mourid43ccab2019-03-13 12:23:45 -0700288 void drawEmptyLayers() {
Alec Mouri6e57f682018-09-29 20:45:08 -0700289 renderengine::DisplaySettings settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800290 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri6e57f682018-09-29 20:45:08 -0700291 // Meaningless buffer since we don't do any drawing
292 sp<GraphicBuffer> buffer = new GraphicBuffer();
Alec Mouri1089aed2018-10-25 21:33:57 -0700293 invokeDraw(settings, layers, buffer);
Alec Mouri6e57f682018-09-29 20:45:08 -0700294 }
295
Alec Mouri1089aed2018-10-25 21:33:57 -0700296 template <typename SourceVariant>
297 void fillBuffer(half r, half g, half b, half a);
298
299 template <typename SourceVariant>
300 void fillRedBuffer();
301
302 template <typename SourceVariant>
303 void fillGreenBuffer();
304
305 template <typename SourceVariant>
306 void fillBlueBuffer();
307
308 template <typename SourceVariant>
309 void fillRedTransparentBuffer();
310
311 template <typename SourceVariant>
312 void fillRedOffsetBuffer();
313
314 template <typename SourceVariant>
315 void fillBufferPhysicalOffset();
316
317 template <typename SourceVariant>
Alec Mouri5a6d8572020-03-23 23:56:15 -0700318 void fillBufferCheckers(uint32_t rotation);
Alec Mouri1089aed2018-10-25 21:33:57 -0700319
320 template <typename SourceVariant>
321 void fillBufferCheckersRotate0();
322
323 template <typename SourceVariant>
324 void fillBufferCheckersRotate90();
325
326 template <typename SourceVariant>
327 void fillBufferCheckersRotate180();
328
329 template <typename SourceVariant>
330 void fillBufferCheckersRotate270();
331
332 template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800333 void fillBufferWithLayerTransform();
334
335 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700336 void fillBufferLayerTransform();
337
338 template <typename SourceVariant>
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800339 void fillBufferWithColorTransform(bool useColorManagement = false);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800340
341 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700342 void fillBufferColorTransform();
343
Alec Mouri7c94edb2018-12-03 21:23:26 -0800344 template <typename SourceVariant>
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800345 void fillBufferColorTransformCM();
346
347 template <typename SourceVariant>
KaiChieh Chuangde09f102020-12-14 16:49:38 +0800348 void fillBufferWithColorTransformZeroLayerAlpha();
349
350 template <typename SourceVariant>
351 void fillBufferColorTransformZeroLayerAlpha();
352
353 template <typename SourceVariant>
Alec Mouri7c94edb2018-12-03 21:23:26 -0800354 void fillRedBufferWithRoundedCorners();
355
356 template <typename SourceVariant>
357 void fillBufferWithRoundedCorners();
358
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000359 template <typename SourceVariant>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800360 void fillBufferAndBlurBackground();
361
362 template <typename SourceVariant>
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000363 void overlayCorners();
364
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800365 void fillRedBufferTextureTransform();
366
367 void fillBufferTextureTransform();
368
369 void fillRedBufferWithPremultiplyAlpha();
370
371 void fillBufferWithPremultiplyAlpha();
372
373 void fillRedBufferWithoutPremultiplyAlpha();
374
375 void fillBufferWithoutPremultiplyAlpha();
376
Alec Mouriac335532018-11-12 15:01:33 -0800377 void fillGreenColorBufferThenClearRegion();
378
379 void clearLeftRegion();
380
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000381 void clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -0800382
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800383 template <typename SourceVariant>
384 void drawShadow(const renderengine::LayerSettings& castingLayer,
385 const renderengine::ShadowSettings& shadow, const ubyte4& casterColor,
386 const ubyte4& backgroundColor);
387
Alec Mourid43ccab2019-03-13 12:23:45 -0700388 // Keep around the same renderengine object to save on initialization time.
389 // For now, exercise the GL backend directly so that some caching specifics
390 // can be tested without changing the interface.
391 static std::unique_ptr<renderengine::gl::GLESRenderEngine> sRE;
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800392 // renderengine object with Color Management enabled
393 static std::unique_ptr<renderengine::gl::GLESRenderEngine> sRECM;
Alec Mourid43ccab2019-03-13 12:23:45 -0700394 // Dumb hack to avoid NPE in the EGL driver: the GraphicBuffer needs to
395 // be freed *after* RenderEngine is destroyed, so that the EGL image is
396 // destroyed first.
397 static sp<GraphicBuffer> sCurrentBuffer;
Alec Mouri1089aed2018-10-25 21:33:57 -0700398
399 sp<GraphicBuffer> mBuffer;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800400
401 std::vector<uint32_t> mTexNames;
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800402 std::vector<uint32_t> mTexNamesCM;
Alec Mouri6e57f682018-09-29 20:45:08 -0700403};
404
Alec Mourid43ccab2019-03-13 12:23:45 -0700405std::unique_ptr<renderengine::gl::GLESRenderEngine> RenderEngineTest::sRE = nullptr;
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800406std::unique_ptr<renderengine::gl::GLESRenderEngine> RenderEngineTest::sRECM = nullptr;
407
Alec Mourid43ccab2019-03-13 12:23:45 -0700408sp<GraphicBuffer> RenderEngineTest::sCurrentBuffer = nullptr;
Alec Mouri1089aed2018-10-25 21:33:57 -0700409
410struct ColorSourceVariant {
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800411 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800412 RenderEngineTest* /*fixture*/, bool /*useColorManagement*/ = false) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700413 layer.source.solidColor = half3(r, g, b);
414 }
415};
416
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800417struct RelaxOpaqueBufferVariant {
418 static void setOpaqueBit(renderengine::LayerSettings& layer) {
419 layer.source.buffer.isOpaque = false;
420 }
421
422 static uint8_t getAlphaChannel() { return 255; }
423};
424
425struct ForceOpaqueBufferVariant {
426 static void setOpaqueBit(renderengine::LayerSettings& layer) {
427 layer.source.buffer.isOpaque = true;
428 }
429
430 static uint8_t getAlphaChannel() {
431 // The isOpaque bit will override the alpha channel, so this should be
432 // arbitrary.
433 return 10;
434 }
435};
436
437template <typename OpaquenessVariant>
438struct BufferSourceVariant {
439 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800440 RenderEngineTest* fixture,
441 bool useColorManagement = false) {
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800442 sp<GraphicBuffer> buf = RenderEngineTest::allocateSourceBuffer(1, 1);
443 uint32_t texName;
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800444 if (useColorManagement) {
445 fixture->sRECM->genTextures(1, &texName);
446 fixture->mTexNamesCM.push_back(texName);
447 } else {
448 fixture->sRE->genTextures(1, &texName);
449 fixture->mTexNames.push_back(texName);
450 }
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800451
452 uint8_t* pixels;
453 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
454 reinterpret_cast<void**>(&pixels));
455
456 for (int32_t j = 0; j < buf->getHeight(); j++) {
457 uint8_t* iter = pixels + (buf->getStride() * j) * 4;
458 for (int32_t i = 0; i < buf->getWidth(); i++) {
459 iter[0] = uint8_t(r * 255);
460 iter[1] = uint8_t(g * 255);
461 iter[2] = uint8_t(b * 255);
462 iter[3] = OpaquenessVariant::getAlphaChannel();
463 iter += 4;
464 }
465 }
466
467 buf->unlock();
468
469 layer.source.buffer.buffer = buf;
470 layer.source.buffer.textureName = texName;
471 OpaquenessVariant::setOpaqueBit(layer);
472 }
473};
474
Alec Mouri1089aed2018-10-25 21:33:57 -0700475template <typename SourceVariant>
476void RenderEngineTest::fillBuffer(half r, half g, half b, half a) {
477 renderengine::DisplaySettings settings;
478 settings.physicalDisplay = fullscreenRect();
479 settings.clip = fullscreenRect();
480
Vishnu Nair9b079a22020-01-21 14:36:08 -0800481 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700482
483 renderengine::LayerSettings layer;
484 layer.geometry.boundaries = fullscreenRect().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800485 SourceVariant::fillColor(layer, r, g, b, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700486 layer.alpha = a;
487
Vishnu Nair9b079a22020-01-21 14:36:08 -0800488 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700489
490 invokeDraw(settings, layers, mBuffer);
491}
492
493template <typename SourceVariant>
494void RenderEngineTest::fillRedBuffer() {
495 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, 1.0f);
496 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
497}
498
499template <typename SourceVariant>
500void RenderEngineTest::fillGreenBuffer() {
501 fillBuffer<SourceVariant>(0.0f, 1.0f, 0.0f, 1.0f);
502 expectBufferColor(fullscreenRect(), 0, 255, 0, 255);
503}
504
505template <typename SourceVariant>
506void RenderEngineTest::fillBlueBuffer() {
507 fillBuffer<SourceVariant>(0.0f, 0.0f, 1.0f, 1.0f);
508 expectBufferColor(fullscreenRect(), 0, 0, 255, 255);
509}
510
511template <typename SourceVariant>
512void RenderEngineTest::fillRedTransparentBuffer() {
513 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, .2f);
514 expectBufferColor(fullscreenRect(), 51, 0, 0, 51);
515}
516
517template <typename SourceVariant>
518void RenderEngineTest::fillRedOffsetBuffer() {
519 renderengine::DisplaySettings settings;
520 settings.physicalDisplay = offsetRect();
521 settings.clip = offsetRectAtZero();
522
Vishnu Nair9b079a22020-01-21 14:36:08 -0800523 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700524
525 renderengine::LayerSettings layer;
526 layer.geometry.boundaries = offsetRectAtZero().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800527 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700528 layer.alpha = 1.0f;
529
Vishnu Nair9b079a22020-01-21 14:36:08 -0800530 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700531 invokeDraw(settings, layers, mBuffer);
532}
533
534template <typename SourceVariant>
535void RenderEngineTest::fillBufferPhysicalOffset() {
536 fillRedOffsetBuffer<SourceVariant>();
537
538 expectBufferColor(Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
539 DEFAULT_DISPLAY_HEIGHT),
540 255, 0, 0, 255);
541 Rect offsetRegionLeft(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_HEIGHT);
542 Rect offsetRegionTop(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_OFFSET);
543
544 expectBufferColor(offsetRegionLeft, 0, 0, 0, 0);
545 expectBufferColor(offsetRegionTop, 0, 0, 0, 0);
546}
547
548template <typename SourceVariant>
Alec Mouri5a6d8572020-03-23 23:56:15 -0700549void RenderEngineTest::fillBufferCheckers(uint32_t orientationFlag) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700550 renderengine::DisplaySettings settings;
551 settings.physicalDisplay = fullscreenRect();
552 // Here logical space is 2x2
553 settings.clip = Rect(2, 2);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700554 settings.orientation = orientationFlag;
Alec Mouri1089aed2018-10-25 21:33:57 -0700555
Vishnu Nair9b079a22020-01-21 14:36:08 -0800556 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700557
558 renderengine::LayerSettings layerOne;
559 Rect rectOne(0, 0, 1, 1);
560 layerOne.geometry.boundaries = rectOne.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800561 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700562 layerOne.alpha = 1.0f;
563
564 renderengine::LayerSettings layerTwo;
565 Rect rectTwo(0, 1, 1, 2);
566 layerTwo.geometry.boundaries = rectTwo.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800567 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700568 layerTwo.alpha = 1.0f;
569
570 renderengine::LayerSettings layerThree;
571 Rect rectThree(1, 0, 2, 1);
572 layerThree.geometry.boundaries = rectThree.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800573 SourceVariant::fillColor(layerThree, 0.0f, 0.0f, 1.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700574 layerThree.alpha = 1.0f;
575
Vishnu Nair9b079a22020-01-21 14:36:08 -0800576 layers.push_back(&layerOne);
577 layers.push_back(&layerTwo);
578 layers.push_back(&layerThree);
Alec Mouri1089aed2018-10-25 21:33:57 -0700579
580 invokeDraw(settings, layers, mBuffer);
581}
582
583template <typename SourceVariant>
584void RenderEngineTest::fillBufferCheckersRotate0() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700585 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_0);
Alec Mouri1089aed2018-10-25 21:33:57 -0700586 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 255, 0, 0,
587 255);
588 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
589 DEFAULT_DISPLAY_HEIGHT / 2),
590 0, 0, 255, 255);
591 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
592 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
593 0, 0, 0, 0);
594 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
595 DEFAULT_DISPLAY_HEIGHT),
596 0, 255, 0, 255);
597}
598
599template <typename SourceVariant>
600void RenderEngineTest::fillBufferCheckersRotate90() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700601 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_90);
Alec Mouri1089aed2018-10-25 21:33:57 -0700602 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 255, 0,
603 255);
604 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
605 DEFAULT_DISPLAY_HEIGHT / 2),
606 255, 0, 0, 255);
607 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
608 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
609 0, 0, 255, 255);
610 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
611 DEFAULT_DISPLAY_HEIGHT),
612 0, 0, 0, 0);
613}
614
615template <typename SourceVariant>
616void RenderEngineTest::fillBufferCheckersRotate180() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700617 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_180);
Alec Mouri1089aed2018-10-25 21:33:57 -0700618 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0,
619 0);
620 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
621 DEFAULT_DISPLAY_HEIGHT / 2),
622 0, 255, 0, 255);
623 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
624 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
625 255, 0, 0, 255);
626 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
627 DEFAULT_DISPLAY_HEIGHT),
628 0, 0, 255, 255);
629}
630
631template <typename SourceVariant>
632void RenderEngineTest::fillBufferCheckersRotate270() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700633 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_270);
Alec Mouri1089aed2018-10-25 21:33:57 -0700634 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 255,
635 255);
636 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
637 DEFAULT_DISPLAY_HEIGHT / 2),
638 0, 0, 0, 0);
639 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
640 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
641 0, 255, 0, 255);
642 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
643 DEFAULT_DISPLAY_HEIGHT),
644 255, 0, 0, 255);
645}
646
647template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800648void RenderEngineTest::fillBufferWithLayerTransform() {
Alec Mouri1089aed2018-10-25 21:33:57 -0700649 renderengine::DisplaySettings settings;
650 settings.physicalDisplay = fullscreenRect();
651 // Here logical space is 2x2
652 settings.clip = Rect(2, 2);
653
Vishnu Nair9b079a22020-01-21 14:36:08 -0800654 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700655
656 renderengine::LayerSettings layer;
657 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
658 // Translate one pixel diagonally
659 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 -0800660 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700661 layer.source.solidColor = half3(1.0f, 0.0f, 0.0f);
662 layer.alpha = 1.0f;
663
Vishnu Nair9b079a22020-01-21 14:36:08 -0800664 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700665
666 invokeDraw(settings, layers, mBuffer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800667}
Alec Mouri1089aed2018-10-25 21:33:57 -0700668
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800669template <typename SourceVariant>
670void RenderEngineTest::fillBufferLayerTransform() {
671 fillBufferWithLayerTransform<SourceVariant>();
Alec Mouri1089aed2018-10-25 21:33:57 -0700672 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0, 0);
673 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 0);
674 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
675 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
676 255, 0, 0, 255);
677}
678
679template <typename SourceVariant>
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800680void RenderEngineTest::fillBufferWithColorTransform(bool useColorManagement) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700681 renderengine::DisplaySettings settings;
682 settings.physicalDisplay = fullscreenRect();
683 settings.clip = Rect(1, 1);
684
Vishnu Nair9b079a22020-01-21 14:36:08 -0800685 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700686
687 renderengine::LayerSettings layer;
688 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800689 SourceVariant::fillColor(layer, 0.5f, 0.25f, 0.125f, this, useColorManagement);
Alec Mouri1089aed2018-10-25 21:33:57 -0700690 layer.alpha = 1.0f;
691
692 // construct a fake color matrix
693 // annihilate green and blue channels
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800694 settings.colorTransform = mat4::scale(vec4(0.9f, 0, 0, 1));
Alec Mouri1089aed2018-10-25 21:33:57 -0700695 // set red channel to red + green
696 layer.colorTransform = mat4(1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
697
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800698 layer.alpha = 1.0f;
699 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
700
Vishnu Nair9b079a22020-01-21 14:36:08 -0800701 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700702
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800703 invokeDraw(settings, layers, mBuffer, useColorManagement);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800704}
Alec Mouri1089aed2018-10-25 21:33:57 -0700705
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800706template <typename SourceVariant>
707void RenderEngineTest::fillBufferColorTransform() {
708 fillBufferWithColorTransform<SourceVariant>();
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800709 expectBufferColor(fullscreenRect(), 172, 0, 0, 255, 1);
710}
711
712template <typename SourceVariant>
713void RenderEngineTest::fillBufferColorTransformCM() {
714 fillBufferWithColorTransform<SourceVariant>(true);
715 expectBufferColor(fullscreenRect(), 126, 0, 0, 255, 1);
Alec Mouri1089aed2018-10-25 21:33:57 -0700716}
717
Alec Mouri7c94edb2018-12-03 21:23:26 -0800718template <typename SourceVariant>
KaiChieh Chuangde09f102020-12-14 16:49:38 +0800719void RenderEngineTest::fillBufferWithColorTransformZeroLayerAlpha() {
720 renderengine::DisplaySettings settings;
721 settings.physicalDisplay = fullscreenRect();
722 settings.clip = Rect(1, 1);
723
724 std::vector<const renderengine::LayerSettings*> layers;
725
726 renderengine::LayerSettings layer;
727 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
728 SourceVariant::fillColor(layer, 0.5f, 0.25f, 0.125f, this);
729 layer.alpha = 0;
730
731 // construct a fake color matrix
732 // simple inverse color
733 settings.colorTransform = mat4(-1, 0, 0, 0,
734 0, -1, 0, 0,
735 0, 0, -1, 0,
736 1, 1, 1, 1);
737
738 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
739
740 layers.push_back(&layer);
741
742 invokeDraw(settings, layers, mBuffer);
743}
744
745template <typename SourceVariant>
746void RenderEngineTest::fillBufferColorTransformZeroLayerAlpha() {
747 fillBufferWithColorTransformZeroLayerAlpha<SourceVariant>();
748 expectBufferColor(fullscreenRect(), 0, 0, 0, 0);
749}
750
751template <typename SourceVariant>
Alec Mouri7c94edb2018-12-03 21:23:26 -0800752void RenderEngineTest::fillRedBufferWithRoundedCorners() {
753 renderengine::DisplaySettings settings;
754 settings.physicalDisplay = fullscreenRect();
755 settings.clip = fullscreenRect();
756
Vishnu Nair9b079a22020-01-21 14:36:08 -0800757 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri7c94edb2018-12-03 21:23:26 -0800758
759 renderengine::LayerSettings layer;
760 layer.geometry.boundaries = fullscreenRect().toFloatRect();
761 layer.geometry.roundedCornersRadius = 5.0f;
762 layer.geometry.roundedCornersCrop = fullscreenRect().toFloatRect();
763 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
764 layer.alpha = 1.0f;
765
Vishnu Nair9b079a22020-01-21 14:36:08 -0800766 layers.push_back(&layer);
Alec Mouri7c94edb2018-12-03 21:23:26 -0800767
768 invokeDraw(settings, layers, mBuffer);
769}
770
771template <typename SourceVariant>
772void RenderEngineTest::fillBufferWithRoundedCorners() {
773 fillRedBufferWithRoundedCorners<SourceVariant>();
774 // Corners should be ignored...
775 expectBufferColor(Rect(0, 0, 1, 1), 0, 0, 0, 0);
776 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH - 1, 0, DEFAULT_DISPLAY_WIDTH, 1), 0, 0, 0, 0);
777 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT - 1, 1, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 0);
778 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH - 1, DEFAULT_DISPLAY_HEIGHT - 1,
779 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
780 0, 0, 0, 0);
781 // ...And the non-rounded portion should be red.
782 // Other pixels may be anti-aliased, so let's not check those.
783 expectBufferColor(Rect(5, 5, DEFAULT_DISPLAY_WIDTH - 5, DEFAULT_DISPLAY_HEIGHT - 5), 255, 0, 0,
784 255);
785}
786
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000787template <typename SourceVariant>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800788void RenderEngineTest::fillBufferAndBlurBackground() {
789 char value[PROPERTY_VALUE_MAX];
790 property_get("ro.surface_flinger.supports_background_blur", value, "0");
791 if (!atoi(value)) {
792 // This device doesn't support blurs, no-op.
793 return;
794 }
795
796 auto blurRadius = 50;
797 auto center = DEFAULT_DISPLAY_WIDTH / 2;
798
799 renderengine::DisplaySettings settings;
800 settings.physicalDisplay = fullscreenRect();
801 settings.clip = fullscreenRect();
802
Vishnu Nair9b079a22020-01-21 14:36:08 -0800803 std::vector<const renderengine::LayerSettings*> layers;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800804
805 renderengine::LayerSettings backgroundLayer;
806 backgroundLayer.geometry.boundaries = fullscreenRect().toFloatRect();
807 SourceVariant::fillColor(backgroundLayer, 0.0f, 1.0f, 0.0f, this);
808 backgroundLayer.alpha = 1.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800809 layers.push_back(&backgroundLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800810
811 renderengine::LayerSettings leftLayer;
812 leftLayer.geometry.boundaries =
813 Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT).toFloatRect();
814 SourceVariant::fillColor(leftLayer, 1.0f, 0.0f, 0.0f, this);
815 leftLayer.alpha = 1.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800816 layers.push_back(&leftLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800817
818 renderengine::LayerSettings blurLayer;
819 blurLayer.geometry.boundaries = fullscreenRect().toFloatRect();
820 blurLayer.backgroundBlurRadius = blurRadius;
821 blurLayer.alpha = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800822 layers.push_back(&blurLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800823
824 invokeDraw(settings, layers, mBuffer);
825
826 expectBufferColor(Rect(center - 1, center - 5, center, center + 5), 150, 150, 0, 255,
827 50 /* tolerance */);
828 expectBufferColor(Rect(center, center - 5, center + 1, center + 5), 150, 150, 0, 255,
829 50 /* tolerance */);
830}
831
832template <typename SourceVariant>
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000833void RenderEngineTest::overlayCorners() {
834 renderengine::DisplaySettings settings;
835 settings.physicalDisplay = fullscreenRect();
836 settings.clip = fullscreenRect();
837
Vishnu Nair9b079a22020-01-21 14:36:08 -0800838 std::vector<const renderengine::LayerSettings*> layersFirst;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000839
840 renderengine::LayerSettings layerOne;
841 layerOne.geometry.boundaries =
842 FloatRect(0, 0, DEFAULT_DISPLAY_WIDTH / 3.0, DEFAULT_DISPLAY_HEIGHT / 3.0);
843 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
844 layerOne.alpha = 0.2;
845
Vishnu Nair9b079a22020-01-21 14:36:08 -0800846 layersFirst.push_back(&layerOne);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000847 invokeDraw(settings, layersFirst, mBuffer);
848 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3, DEFAULT_DISPLAY_HEIGHT / 3), 51, 0, 0, 51);
849 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3 + 1, DEFAULT_DISPLAY_HEIGHT / 3 + 1,
850 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
851 0, 0, 0, 0);
852
Vishnu Nair9b079a22020-01-21 14:36:08 -0800853 std::vector<const renderengine::LayerSettings*> layersSecond;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000854 renderengine::LayerSettings layerTwo;
855 layerTwo.geometry.boundaries =
856 FloatRect(DEFAULT_DISPLAY_WIDTH / 3.0, DEFAULT_DISPLAY_HEIGHT / 3.0,
857 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT);
858 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
859 layerTwo.alpha = 1.0f;
860
Vishnu Nair9b079a22020-01-21 14:36:08 -0800861 layersSecond.push_back(&layerTwo);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000862 invokeDraw(settings, layersSecond, mBuffer);
863
864 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3, DEFAULT_DISPLAY_HEIGHT / 3), 0, 0, 0, 0);
865 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3 + 1, DEFAULT_DISPLAY_HEIGHT / 3 + 1,
866 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
867 0, 255, 0, 255);
868}
869
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800870void RenderEngineTest::fillRedBufferTextureTransform() {
871 renderengine::DisplaySettings settings;
872 settings.physicalDisplay = fullscreenRect();
873 settings.clip = Rect(1, 1);
874
Vishnu Nair9b079a22020-01-21 14:36:08 -0800875 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800876
877 renderengine::LayerSettings layer;
878 // Here will allocate a checker board texture, but transform texture
879 // coordinates so that only the upper left is applied.
880 sp<GraphicBuffer> buf = allocateSourceBuffer(2, 2);
881 uint32_t texName;
882 RenderEngineTest::sRE->genTextures(1, &texName);
883 this->mTexNames.push_back(texName);
884
885 uint8_t* pixels;
886 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
887 reinterpret_cast<void**>(&pixels));
888 // Red top left, Green top right, Blue bottom left, Black bottom right
889 pixels[0] = 255;
890 pixels[1] = 0;
891 pixels[2] = 0;
892 pixels[3] = 255;
893 pixels[4] = 0;
894 pixels[5] = 255;
895 pixels[6] = 0;
896 pixels[7] = 255;
897 pixels[8] = 0;
898 pixels[9] = 0;
899 pixels[10] = 255;
900 pixels[11] = 255;
901 buf->unlock();
902
903 layer.source.buffer.buffer = buf;
904 layer.source.buffer.textureName = texName;
905 // Transform coordinates to only be inside the red quadrant.
906 layer.source.buffer.textureTransform = mat4::scale(vec4(0.2, 0.2, 1, 1));
907 layer.alpha = 1.0f;
908 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
909
Vishnu Nair9b079a22020-01-21 14:36:08 -0800910 layers.push_back(&layer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800911
912 invokeDraw(settings, layers, mBuffer);
913}
914
915void RenderEngineTest::fillBufferTextureTransform() {
916 fillRedBufferTextureTransform();
917 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
918}
919
920void RenderEngineTest::fillRedBufferWithPremultiplyAlpha() {
921 renderengine::DisplaySettings settings;
922 settings.physicalDisplay = fullscreenRect();
923 // Here logical space is 1x1
924 settings.clip = Rect(1, 1);
925
Vishnu Nair9b079a22020-01-21 14:36:08 -0800926 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800927
928 renderengine::LayerSettings layer;
929 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
930 uint32_t texName;
931 RenderEngineTest::sRE->genTextures(1, &texName);
932 this->mTexNames.push_back(texName);
933
934 uint8_t* pixels;
935 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
936 reinterpret_cast<void**>(&pixels));
937 pixels[0] = 255;
938 pixels[1] = 0;
939 pixels[2] = 0;
940 pixels[3] = 255;
941 buf->unlock();
942
943 layer.source.buffer.buffer = buf;
944 layer.source.buffer.textureName = texName;
945 layer.source.buffer.usePremultipliedAlpha = true;
946 layer.alpha = 0.5f;
947 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
948
Vishnu Nair9b079a22020-01-21 14:36:08 -0800949 layers.push_back(&layer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800950
951 invokeDraw(settings, layers, mBuffer);
952}
953
954void RenderEngineTest::fillBufferWithPremultiplyAlpha() {
955 fillRedBufferWithPremultiplyAlpha();
956 expectBufferColor(fullscreenRect(), 128, 0, 0, 128);
957}
958
959void RenderEngineTest::fillRedBufferWithoutPremultiplyAlpha() {
960 renderengine::DisplaySettings settings;
961 settings.physicalDisplay = fullscreenRect();
962 // Here logical space is 1x1
963 settings.clip = Rect(1, 1);
964
Vishnu Nair9b079a22020-01-21 14:36:08 -0800965 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800966
967 renderengine::LayerSettings layer;
968 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
969 uint32_t texName;
970 RenderEngineTest::sRE->genTextures(1, &texName);
971 this->mTexNames.push_back(texName);
972
973 uint8_t* pixels;
974 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
975 reinterpret_cast<void**>(&pixels));
976 pixels[0] = 255;
977 pixels[1] = 0;
978 pixels[2] = 0;
979 pixels[3] = 255;
980 buf->unlock();
981
982 layer.source.buffer.buffer = buf;
983 layer.source.buffer.textureName = texName;
984 layer.source.buffer.usePremultipliedAlpha = false;
985 layer.alpha = 0.5f;
986 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
987
Vishnu Nair9b079a22020-01-21 14:36:08 -0800988 layers.push_back(&layer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800989
990 invokeDraw(settings, layers, mBuffer);
991}
992
993void RenderEngineTest::fillBufferWithoutPremultiplyAlpha() {
994 fillRedBufferWithoutPremultiplyAlpha();
wukui16f3c0bb2020-08-05 20:35:29 +0800995 expectBufferColor(fullscreenRect(), 128, 0, 0, 128, 1);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800996}
997
Alec Mouriac335532018-11-12 15:01:33 -0800998void RenderEngineTest::clearLeftRegion() {
999 renderengine::DisplaySettings settings;
1000 settings.physicalDisplay = fullscreenRect();
1001 // Here logical space is 4x4
1002 settings.clip = Rect(4, 4);
Alec Mouri5a6d8572020-03-23 23:56:15 -07001003 settings.clearRegion = Region(Rect(2, 4));
Vishnu Nair9b079a22020-01-21 14:36:08 -08001004 std::vector<const renderengine::LayerSettings*> layers;
Xin Lie8b4e702020-08-29 01:34:09 -07001005 // fake layer, without bounds should not render anything
Alec Mouriac335532018-11-12 15:01:33 -08001006 renderengine::LayerSettings layer;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001007 layers.push_back(&layer);
Alec Mouriac335532018-11-12 15:01:33 -08001008 invokeDraw(settings, layers, mBuffer);
1009}
1010
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001011void RenderEngineTest::clearRegion() {
Alec Mouriac335532018-11-12 15:01:33 -08001012 // Reuse mBuffer
1013 clearLeftRegion();
1014 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 255);
1015 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
1016 DEFAULT_DISPLAY_HEIGHT),
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001017 0, 0, 0, 0);
Alec Mouriac335532018-11-12 15:01:33 -08001018}
1019
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001020template <typename SourceVariant>
1021void RenderEngineTest::drawShadow(const renderengine::LayerSettings& castingLayer,
1022 const renderengine::ShadowSettings& shadow,
1023 const ubyte4& casterColor, const ubyte4& backgroundColor) {
1024 renderengine::DisplaySettings settings;
1025 settings.physicalDisplay = fullscreenRect();
1026 settings.clip = fullscreenRect();
1027
Vishnu Nair9b079a22020-01-21 14:36:08 -08001028 std::vector<const renderengine::LayerSettings*> layers;
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001029
1030 // add background layer
1031 renderengine::LayerSettings bgLayer;
1032 bgLayer.geometry.boundaries = fullscreenRect().toFloatRect();
1033 ColorSourceVariant::fillColor(bgLayer, backgroundColor.r / 255.0f, backgroundColor.g / 255.0f,
1034 backgroundColor.b / 255.0f, this);
1035 bgLayer.alpha = backgroundColor.a / 255.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001036 layers.push_back(&bgLayer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001037
1038 // add shadow layer
1039 renderengine::LayerSettings shadowLayer;
1040 shadowLayer.geometry.boundaries = castingLayer.geometry.boundaries;
1041 shadowLayer.alpha = castingLayer.alpha;
1042 shadowLayer.shadow = shadow;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001043 layers.push_back(&shadowLayer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001044
1045 // add layer casting the shadow
1046 renderengine::LayerSettings layer = castingLayer;
1047 SourceVariant::fillColor(layer, casterColor.r / 255.0f, casterColor.g / 255.0f,
1048 casterColor.b / 255.0f, this);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001049 layers.push_back(&layer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001050
1051 invokeDraw(settings, layers, mBuffer);
1052}
1053
Alec Mouri1089aed2018-10-25 21:33:57 -07001054TEST_F(RenderEngineTest, drawLayers_noLayersToDraw) {
1055 drawEmptyLayers();
1056}
1057
Alec Mourid43ccab2019-03-13 12:23:45 -07001058TEST_F(RenderEngineTest, drawLayers_nullOutputBuffer) {
1059 renderengine::DisplaySettings settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001060 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001061 renderengine::LayerSettings layer;
1062 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1063 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001064 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001065 base::unique_fd fence;
Alec Mourife0d72b2019-03-21 14:05:56 -07001066 status_t status = sRE->drawLayers(settings, layers, nullptr, true, base::unique_fd(), &fence);
Alec Mourid43ccab2019-03-13 12:23:45 -07001067
1068 ASSERT_EQ(BAD_VALUE, status);
1069}
1070
1071TEST_F(RenderEngineTest, drawLayers_nullOutputFence) {
1072 renderengine::DisplaySettings settings;
1073 settings.physicalDisplay = fullscreenRect();
1074 settings.clip = fullscreenRect();
1075
Vishnu Nair9b079a22020-01-21 14:36:08 -08001076 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001077 renderengine::LayerSettings layer;
1078 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1079 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1080 layer.alpha = 1.0;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001081 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001082
Alec Mourife0d72b2019-03-21 14:05:56 -07001083 status_t status = sRE->drawLayers(settings, layers, mBuffer->getNativeBuffer(), true,
Alec Mourid43ccab2019-03-13 12:23:45 -07001084 base::unique_fd(), nullptr);
1085 sCurrentBuffer = mBuffer;
1086 ASSERT_EQ(NO_ERROR, status);
1087 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
1088}
1089
Alec Mourife0d72b2019-03-21 14:05:56 -07001090TEST_F(RenderEngineTest, drawLayers_doesNotCacheFramebuffer) {
1091 renderengine::DisplaySettings settings;
1092 settings.physicalDisplay = fullscreenRect();
1093 settings.clip = fullscreenRect();
1094
Vishnu Nair9b079a22020-01-21 14:36:08 -08001095 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourife0d72b2019-03-21 14:05:56 -07001096 renderengine::LayerSettings layer;
1097 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1098 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1099 layer.alpha = 1.0;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001100 layers.push_back(&layer);
Alec Mourife0d72b2019-03-21 14:05:56 -07001101
1102 status_t status = sRE->drawLayers(settings, layers, mBuffer->getNativeBuffer(), false,
1103 base::unique_fd(), nullptr);
1104 sCurrentBuffer = mBuffer;
1105 ASSERT_EQ(NO_ERROR, status);
1106 ASSERT_FALSE(sRE->isFramebufferImageCachedForTesting(mBuffer->getId()));
1107 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
1108}
1109
Alec Mouri1089aed2018-10-25 21:33:57 -07001110TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_colorSource) {
1111 fillRedBuffer<ColorSourceVariant>();
1112}
1113
1114TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_colorSource) {
1115 fillGreenBuffer<ColorSourceVariant>();
1116}
1117
1118TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_colorSource) {
1119 fillBlueBuffer<ColorSourceVariant>();
1120}
1121
1122TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_colorSource) {
1123 fillRedTransparentBuffer<ColorSourceVariant>();
1124}
1125
1126TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_colorSource) {
1127 fillBufferPhysicalOffset<ColorSourceVariant>();
1128}
1129
1130TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_colorSource) {
1131 fillBufferCheckersRotate0<ColorSourceVariant>();
1132}
1133
1134TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_colorSource) {
1135 fillBufferCheckersRotate90<ColorSourceVariant>();
1136}
1137
1138TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_colorSource) {
1139 fillBufferCheckersRotate180<ColorSourceVariant>();
1140}
1141
1142TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_colorSource) {
1143 fillBufferCheckersRotate270<ColorSourceVariant>();
1144}
1145
1146TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_colorSource) {
1147 fillBufferLayerTransform<ColorSourceVariant>();
1148}
1149
1150TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_colorSource) {
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +08001151 fillBufferColorTransform<ColorSourceVariant>();
1152}
1153
1154TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransformCM_colorSource) {
1155 fillBufferColorTransformCM<ColorSourceVariant>();
Alec Mouri6e57f682018-09-29 20:45:08 -07001156}
1157
KaiChieh Chuangde09f102020-12-14 16:49:38 +08001158TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransformZeroLayerAlpha_colorSource) {
1159 fillBufferColorTransformZeroLayerAlpha<ColorSourceVariant>();
1160}
1161
Alec Mouri7c94edb2018-12-03 21:23:26 -08001162TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_colorSource) {
1163 fillBufferWithRoundedCorners<ColorSourceVariant>();
1164}
1165
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001166TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_colorSource) {
1167 fillBufferAndBlurBackground<ColorSourceVariant>();
1168}
1169
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001170TEST_F(RenderEngineTest, drawLayers_overlayCorners_colorSource) {
1171 overlayCorners<ColorSourceVariant>();
1172}
1173
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001174TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_opaqueBufferSource) {
1175 fillRedBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1176}
1177
1178TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_opaqueBufferSource) {
1179 fillGreenBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1180}
1181
1182TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_opaqueBufferSource) {
1183 fillBlueBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1184}
1185
1186TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_opaqueBufferSource) {
1187 fillRedTransparentBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1188}
1189
1190TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_opaqueBufferSource) {
1191 fillBufferPhysicalOffset<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1192}
1193
1194TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_opaqueBufferSource) {
1195 fillBufferCheckersRotate0<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1196}
1197
1198TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_opaqueBufferSource) {
1199 fillBufferCheckersRotate90<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1200}
1201
1202TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_opaqueBufferSource) {
1203 fillBufferCheckersRotate180<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1204}
1205
1206TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_opaqueBufferSource) {
1207 fillBufferCheckersRotate270<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1208}
1209
1210TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_opaqueBufferSource) {
1211 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1212}
1213
1214TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_opaqueBufferSource) {
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +08001215 fillBufferColorTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1216}
1217
1218TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransformCM_opaqueBufferSource) {
1219 fillBufferColorTransformCM<BufferSourceVariant<ForceOpaqueBufferVariant>>();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001220}
1221
KaiChieh Chuangde09f102020-12-14 16:49:38 +08001222TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransformZeroLayerAlpha_opaqueBufferSource) {
1223 fillBufferColorTransformZeroLayerAlpha<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1224}
1225
Alec Mouri7c94edb2018-12-03 21:23:26 -08001226TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_opaqueBufferSource) {
1227 fillBufferWithRoundedCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1228}
1229
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001230TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_opaqueBufferSource) {
1231 fillBufferAndBlurBackground<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1232}
1233
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001234TEST_F(RenderEngineTest, drawLayers_overlayCorners_opaqueBufferSource) {
1235 overlayCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1236}
1237
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001238TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_bufferSource) {
1239 fillRedBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1240}
1241
1242TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_bufferSource) {
1243 fillGreenBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1244}
1245
1246TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_bufferSource) {
1247 fillBlueBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1248}
1249
1250TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_bufferSource) {
1251 fillRedTransparentBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1252}
1253
1254TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_bufferSource) {
1255 fillBufferPhysicalOffset<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1256}
1257
1258TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_bufferSource) {
1259 fillBufferCheckersRotate0<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1260}
1261
1262TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_bufferSource) {
1263 fillBufferCheckersRotate90<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1264}
1265
1266TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_bufferSource) {
1267 fillBufferCheckersRotate180<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1268}
1269
1270TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_bufferSource) {
1271 fillBufferCheckersRotate270<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1272}
1273
1274TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_bufferSource) {
1275 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1276}
1277
1278TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_bufferSource) {
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +08001279 fillBufferColorTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1280}
1281
1282TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransformCM_bufferSource) {
1283 fillBufferColorTransformCM<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001284}
1285
KaiChieh Chuangde09f102020-12-14 16:49:38 +08001286TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransformZeroLayerAlpha_bufferSource) {
1287 fillBufferColorTransformZeroLayerAlpha<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1288}
1289
Alec Mouri7c94edb2018-12-03 21:23:26 -08001290TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_bufferSource) {
1291 fillBufferWithRoundedCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1292}
1293
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001294TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_bufferSource) {
1295 fillBufferAndBlurBackground<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1296}
1297
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001298TEST_F(RenderEngineTest, drawLayers_overlayCorners_bufferSource) {
1299 overlayCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1300}
1301
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001302TEST_F(RenderEngineTest, drawLayers_fillBufferTextureTransform) {
1303 fillBufferTextureTransform();
1304}
1305
1306TEST_F(RenderEngineTest, drawLayers_fillBuffer_premultipliesAlpha) {
1307 fillBufferWithPremultiplyAlpha();
1308}
1309
1310TEST_F(RenderEngineTest, drawLayers_fillBuffer_withoutPremultiplyingAlpha) {
1311 fillBufferWithoutPremultiplyAlpha();
1312}
1313
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001314TEST_F(RenderEngineTest, drawLayers_clearRegion) {
1315 clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -08001316}
1317
Alec Mourid43ccab2019-03-13 12:23:45 -07001318TEST_F(RenderEngineTest, drawLayers_fillsBufferAndCachesImages) {
1319 renderengine::DisplaySettings settings;
1320 settings.physicalDisplay = fullscreenRect();
1321 settings.clip = fullscreenRect();
1322
Vishnu Nair9b079a22020-01-21 14:36:08 -08001323 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001324
1325 renderengine::LayerSettings layer;
1326 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1327 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1328
Vishnu Nair9b079a22020-01-21 14:36:08 -08001329 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001330 invokeDraw(settings, layers, mBuffer);
1331 uint64_t bufferId = layer.source.buffer.buffer->getId();
1332 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001333 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1334 sRE->unbindExternalTextureBufferForTesting(bufferId);
1335 std::lock_guard<std::mutex> lock(barrier->mutex);
1336 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1337 [&]() REQUIRES(barrier->mutex) {
1338 return barrier->isOpen;
1339 }));
Alec Mourid43ccab2019-03-13 12:23:45 -07001340 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001341 EXPECT_EQ(NO_ERROR, barrier->result);
Alec Mourid43ccab2019-03-13 12:23:45 -07001342}
1343
Alec Mouri4dde1782019-09-30 17:27:13 -07001344TEST_F(RenderEngineTest, bindExternalBuffer_withNullBuffer) {
Alec Mourid43ccab2019-03-13 12:23:45 -07001345 status_t result = sRE->bindExternalTextureBuffer(0, nullptr, nullptr);
1346 ASSERT_EQ(BAD_VALUE, result);
1347}
1348
Alec Mouri4dde1782019-09-30 17:27:13 -07001349TEST_F(RenderEngineTest, bindExternalBuffer_cachesImages) {
Alec Mourid43ccab2019-03-13 12:23:45 -07001350 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
1351 uint32_t texName;
1352 sRE->genTextures(1, &texName);
1353 mTexNames.push_back(texName);
1354
1355 sRE->bindExternalTextureBuffer(texName, buf, nullptr);
1356 uint64_t bufferId = buf->getId();
1357 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001358 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1359 sRE->unbindExternalTextureBufferForTesting(bufferId);
1360 std::lock_guard<std::mutex> lock(barrier->mutex);
1361 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1362 [&]() REQUIRES(barrier->mutex) {
1363 return barrier->isOpen;
1364 }));
1365 EXPECT_EQ(NO_ERROR, barrier->result);
Alec Mourid43ccab2019-03-13 12:23:45 -07001366 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1367}
1368
Alec Mouri4dde1782019-09-30 17:27:13 -07001369TEST_F(RenderEngineTest, cacheExternalBuffer_withNullBuffer) {
Alec Mouri16a99402019-07-29 16:37:30 -07001370 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1371 sRE->cacheExternalTextureBufferForTesting(nullptr);
1372 std::lock_guard<std::mutex> lock(barrier->mutex);
1373 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1374 [&]() REQUIRES(barrier->mutex) {
1375 return barrier->isOpen;
1376 }));
1377 EXPECT_TRUE(barrier->isOpen);
1378 EXPECT_EQ(BAD_VALUE, barrier->result);
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001379}
1380
Alec Mouri4dde1782019-09-30 17:27:13 -07001381TEST_F(RenderEngineTest, cacheExternalBuffer_cachesImages) {
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001382 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
1383 uint64_t bufferId = buf->getId();
Alec Mouri16a99402019-07-29 16:37:30 -07001384 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1385 sRE->cacheExternalTextureBufferForTesting(buf);
1386 {
1387 std::lock_guard<std::mutex> lock(barrier->mutex);
1388 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1389 [&]() REQUIRES(barrier->mutex) {
1390 return barrier->isOpen;
1391 }));
1392 EXPECT_EQ(NO_ERROR, barrier->result);
1393 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001394 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001395 barrier = sRE->unbindExternalTextureBufferForTesting(bufferId);
1396 {
1397 std::lock_guard<std::mutex> lock(barrier->mutex);
1398 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1399 [&]() REQUIRES(barrier->mutex) {
1400 return barrier->isOpen;
1401 }));
1402 EXPECT_EQ(NO_ERROR, barrier->result);
1403 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001404 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1405}
1406
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001407TEST_F(RenderEngineTest, drawLayers_fillShadow_casterLayerMinSize) {
1408 const ubyte4 casterColor(255, 0, 0, 255);
1409 const ubyte4 backgroundColor(255, 255, 255, 255);
1410 const float shadowLength = 5.0f;
1411 Rect casterBounds(1, 1);
1412 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1413 renderengine::LayerSettings castingLayer;
1414 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1415 castingLayer.alpha = 1.0f;
1416 renderengine::ShadowSettings settings =
1417 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1418 false /* casterIsTranslucent */);
1419
1420 drawShadow<ColorSourceVariant>(castingLayer, settings, casterColor, backgroundColor);
1421 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1422}
1423
1424TEST_F(RenderEngineTest, drawLayers_fillShadow_casterColorLayer) {
1425 const ubyte4 casterColor(255, 0, 0, 255);
1426 const ubyte4 backgroundColor(255, 255, 255, 255);
1427 const float shadowLength = 5.0f;
1428 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1429 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1430 renderengine::LayerSettings castingLayer;
1431 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1432 castingLayer.alpha = 1.0f;
1433 renderengine::ShadowSettings settings =
1434 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1435 false /* casterIsTranslucent */);
1436
1437 drawShadow<ColorSourceVariant>(castingLayer, settings, casterColor, backgroundColor);
1438 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1439}
1440
1441TEST_F(RenderEngineTest, drawLayers_fillShadow_casterOpaqueBufferLayer) {
1442 const ubyte4 casterColor(255, 0, 0, 255);
1443 const ubyte4 backgroundColor(255, 255, 255, 255);
1444 const float shadowLength = 5.0f;
1445 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1446 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1447 renderengine::LayerSettings castingLayer;
1448 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1449 castingLayer.alpha = 1.0f;
1450 renderengine::ShadowSettings settings =
1451 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1452 false /* casterIsTranslucent */);
1453
1454 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1455 backgroundColor);
1456 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1457}
1458
1459TEST_F(RenderEngineTest, drawLayers_fillShadow_casterWithRoundedCorner) {
1460 const ubyte4 casterColor(255, 0, 0, 255);
1461 const ubyte4 backgroundColor(255, 255, 255, 255);
1462 const float shadowLength = 5.0f;
1463 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1464 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1465 renderengine::LayerSettings castingLayer;
1466 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1467 castingLayer.geometry.roundedCornersRadius = 3.0f;
1468 castingLayer.geometry.roundedCornersCrop = casterBounds.toFloatRect();
1469 castingLayer.alpha = 1.0f;
1470 renderengine::ShadowSettings settings =
1471 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1472 false /* casterIsTranslucent */);
1473
1474 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1475 backgroundColor);
1476 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1477}
1478
1479TEST_F(RenderEngineTest, drawLayers_fillShadow_translucentCasterWithAlpha) {
1480 const ubyte4 casterColor(255, 0, 0, 255);
1481 const ubyte4 backgroundColor(255, 255, 255, 255);
1482 const float shadowLength = 5.0f;
1483 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1484 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1485 renderengine::LayerSettings castingLayer;
1486 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1487 castingLayer.alpha = 0.5f;
1488 renderengine::ShadowSettings settings =
1489 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1490 true /* casterIsTranslucent */);
1491
1492 drawShadow<BufferSourceVariant<RelaxOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1493 backgroundColor);
1494
1495 // verify only the background since the shadow will draw behind the caster
1496 const float shadowInset = settings.length * -1.0f;
1497 const Rect casterWithShadow =
1498 Rect(casterBounds).inset(shadowInset, shadowInset, shadowInset, shadowInset);
1499 const Region backgroundRegion = Region(fullscreenRect()).subtractSelf(casterWithShadow);
1500 expectBufferColor(backgroundRegion, backgroundColor.r, backgroundColor.g, backgroundColor.b,
1501 backgroundColor.a);
1502}
1503
Alec Mouri4dde1782019-09-30 17:27:13 -07001504TEST_F(RenderEngineTest, cleanupPostRender_cleansUpOnce) {
1505 renderengine::DisplaySettings settings;
1506 settings.physicalDisplay = fullscreenRect();
1507 settings.clip = fullscreenRect();
1508
1509 std::vector<const renderengine::LayerSettings*> layers;
1510 renderengine::LayerSettings layer;
1511 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1512 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1513 layer.alpha = 1.0;
1514 layers.push_back(&layer);
1515
1516 base::unique_fd fenceOne;
1517 sRE->drawLayers(settings, layers, mBuffer->getNativeBuffer(), true, base::unique_fd(),
1518 &fenceOne);
1519 base::unique_fd fenceTwo;
1520 sRE->drawLayers(settings, layers, mBuffer->getNativeBuffer(), true, std::move(fenceOne),
1521 &fenceTwo);
1522
1523 const int fd = fenceTwo.get();
1524 if (fd >= 0) {
1525 sync_wait(fd, -1);
1526 }
Alec Mouri4dde1782019-09-30 17:27:13 -07001527 // Only cleanup the first time.
Alec Mouri56f295e2020-08-13 10:14:29 -07001528 EXPECT_TRUE(sRE->cleanupPostRender(
1529 renderengine::RenderEngine::CleanupMode::CLEAN_OUTPUT_RESOURCES));
1530 EXPECT_FALSE(sRE->cleanupPostRender(
1531 renderengine::RenderEngine::CleanupMode::CLEAN_OUTPUT_RESOURCES));
1532}
1533
1534TEST_F(RenderEngineTest, cleanupPostRender_whenCleaningAll_replacesTextureMemory) {
1535 renderengine::DisplaySettings settings;
1536 settings.physicalDisplay = fullscreenRect();
1537 settings.clip = fullscreenRect();
1538
1539 std::vector<const renderengine::LayerSettings*> layers;
1540 renderengine::LayerSettings layer;
1541 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1542 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1543 layer.alpha = 1.0;
1544 layers.push_back(&layer);
1545
1546 base::unique_fd fence;
1547 sRE->drawLayers(settings, layers, mBuffer->getNativeBuffer(), true, base::unique_fd(), &fence);
1548
1549 const int fd = fence.get();
1550 if (fd >= 0) {
1551 sync_wait(fd, -1);
1552 }
1553
1554 uint64_t bufferId = layer.source.buffer.buffer->getId();
1555 uint32_t texName = layer.source.buffer.textureName;
1556 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
1557 EXPECT_EQ(bufferId, sRE->getBufferIdForTextureNameForTesting(texName));
1558
1559 EXPECT_TRUE(sRE->cleanupPostRender(renderengine::RenderEngine::CleanupMode::CLEAN_ALL));
1560
1561 // Now check that our view of memory is good.
1562 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1563 EXPECT_EQ(std::nullopt, sRE->getBufferIdForTextureNameForTesting(bufferId));
1564 EXPECT_TRUE(sRE->isTextureNameKnownForTesting(texName));
Alec Mouri4dde1782019-09-30 17:27:13 -07001565}
1566
Alec Mouri6e57f682018-09-29 20:45:08 -07001567} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001568
1569// TODO(b/129481165): remove the #pragma below and fix conversion issues
1570#pragma clang diagnostic pop // ignored "-Wconversion"