blob: 3b0d4f7f39e91e8d895cefe16617b0c91518d6d2 [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>
Alec Mouri7c94edb2018-12-03 21:23:26 -0800348 void fillRedBufferWithRoundedCorners();
349
350 template <typename SourceVariant>
351 void fillBufferWithRoundedCorners();
352
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000353 template <typename SourceVariant>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800354 void fillBufferAndBlurBackground();
355
356 template <typename SourceVariant>
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000357 void overlayCorners();
358
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800359 void fillRedBufferTextureTransform();
360
361 void fillBufferTextureTransform();
362
363 void fillRedBufferWithPremultiplyAlpha();
364
365 void fillBufferWithPremultiplyAlpha();
366
367 void fillRedBufferWithoutPremultiplyAlpha();
368
369 void fillBufferWithoutPremultiplyAlpha();
370
Alec Mouriac335532018-11-12 15:01:33 -0800371 void fillGreenColorBufferThenClearRegion();
372
373 void clearLeftRegion();
374
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000375 void clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -0800376
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800377 template <typename SourceVariant>
378 void drawShadow(const renderengine::LayerSettings& castingLayer,
379 const renderengine::ShadowSettings& shadow, const ubyte4& casterColor,
380 const ubyte4& backgroundColor);
381
Alec Mourid43ccab2019-03-13 12:23:45 -0700382 // Keep around the same renderengine object to save on initialization time.
383 // For now, exercise the GL backend directly so that some caching specifics
384 // can be tested without changing the interface.
385 static std::unique_ptr<renderengine::gl::GLESRenderEngine> sRE;
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800386 // renderengine object with Color Management enabled
387 static std::unique_ptr<renderengine::gl::GLESRenderEngine> sRECM;
Alec Mourid43ccab2019-03-13 12:23:45 -0700388 // Dumb hack to avoid NPE in the EGL driver: the GraphicBuffer needs to
389 // be freed *after* RenderEngine is destroyed, so that the EGL image is
390 // destroyed first.
391 static sp<GraphicBuffer> sCurrentBuffer;
Alec Mouri1089aed2018-10-25 21:33:57 -0700392
393 sp<GraphicBuffer> mBuffer;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800394
395 std::vector<uint32_t> mTexNames;
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800396 std::vector<uint32_t> mTexNamesCM;
Alec Mouri6e57f682018-09-29 20:45:08 -0700397};
398
Alec Mourid43ccab2019-03-13 12:23:45 -0700399std::unique_ptr<renderengine::gl::GLESRenderEngine> RenderEngineTest::sRE = nullptr;
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800400std::unique_ptr<renderengine::gl::GLESRenderEngine> RenderEngineTest::sRECM = nullptr;
401
Alec Mourid43ccab2019-03-13 12:23:45 -0700402sp<GraphicBuffer> RenderEngineTest::sCurrentBuffer = nullptr;
Alec Mouri1089aed2018-10-25 21:33:57 -0700403
404struct ColorSourceVariant {
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800405 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800406 RenderEngineTest* /*fixture*/, bool /*useColorManagement*/ = false) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700407 layer.source.solidColor = half3(r, g, b);
408 }
409};
410
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800411struct RelaxOpaqueBufferVariant {
412 static void setOpaqueBit(renderengine::LayerSettings& layer) {
413 layer.source.buffer.isOpaque = false;
414 }
415
416 static uint8_t getAlphaChannel() { return 255; }
417};
418
419struct ForceOpaqueBufferVariant {
420 static void setOpaqueBit(renderengine::LayerSettings& layer) {
421 layer.source.buffer.isOpaque = true;
422 }
423
424 static uint8_t getAlphaChannel() {
425 // The isOpaque bit will override the alpha channel, so this should be
426 // arbitrary.
427 return 10;
428 }
429};
430
431template <typename OpaquenessVariant>
432struct BufferSourceVariant {
433 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800434 RenderEngineTest* fixture,
435 bool useColorManagement = false) {
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800436 sp<GraphicBuffer> buf = RenderEngineTest::allocateSourceBuffer(1, 1);
437 uint32_t texName;
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800438 if (useColorManagement) {
439 fixture->sRECM->genTextures(1, &texName);
440 fixture->mTexNamesCM.push_back(texName);
441 } else {
442 fixture->sRE->genTextures(1, &texName);
443 fixture->mTexNames.push_back(texName);
444 }
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800445
446 uint8_t* pixels;
447 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
448 reinterpret_cast<void**>(&pixels));
449
450 for (int32_t j = 0; j < buf->getHeight(); j++) {
451 uint8_t* iter = pixels + (buf->getStride() * j) * 4;
452 for (int32_t i = 0; i < buf->getWidth(); i++) {
453 iter[0] = uint8_t(r * 255);
454 iter[1] = uint8_t(g * 255);
455 iter[2] = uint8_t(b * 255);
456 iter[3] = OpaquenessVariant::getAlphaChannel();
457 iter += 4;
458 }
459 }
460
461 buf->unlock();
462
463 layer.source.buffer.buffer = buf;
464 layer.source.buffer.textureName = texName;
465 OpaquenessVariant::setOpaqueBit(layer);
466 }
467};
468
Alec Mouri1089aed2018-10-25 21:33:57 -0700469template <typename SourceVariant>
470void RenderEngineTest::fillBuffer(half r, half g, half b, half a) {
471 renderengine::DisplaySettings settings;
472 settings.physicalDisplay = fullscreenRect();
473 settings.clip = fullscreenRect();
474
Vishnu Nair9b079a22020-01-21 14:36:08 -0800475 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700476
477 renderengine::LayerSettings layer;
478 layer.geometry.boundaries = fullscreenRect().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800479 SourceVariant::fillColor(layer, r, g, b, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700480 layer.alpha = a;
481
Vishnu Nair9b079a22020-01-21 14:36:08 -0800482 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700483
484 invokeDraw(settings, layers, mBuffer);
485}
486
487template <typename SourceVariant>
488void RenderEngineTest::fillRedBuffer() {
489 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, 1.0f);
490 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
491}
492
493template <typename SourceVariant>
494void RenderEngineTest::fillGreenBuffer() {
495 fillBuffer<SourceVariant>(0.0f, 1.0f, 0.0f, 1.0f);
496 expectBufferColor(fullscreenRect(), 0, 255, 0, 255);
497}
498
499template <typename SourceVariant>
500void RenderEngineTest::fillBlueBuffer() {
501 fillBuffer<SourceVariant>(0.0f, 0.0f, 1.0f, 1.0f);
502 expectBufferColor(fullscreenRect(), 0, 0, 255, 255);
503}
504
505template <typename SourceVariant>
506void RenderEngineTest::fillRedTransparentBuffer() {
507 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, .2f);
508 expectBufferColor(fullscreenRect(), 51, 0, 0, 51);
509}
510
511template <typename SourceVariant>
512void RenderEngineTest::fillRedOffsetBuffer() {
513 renderengine::DisplaySettings settings;
514 settings.physicalDisplay = offsetRect();
515 settings.clip = offsetRectAtZero();
516
Vishnu Nair9b079a22020-01-21 14:36:08 -0800517 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700518
519 renderengine::LayerSettings layer;
520 layer.geometry.boundaries = offsetRectAtZero().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800521 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700522 layer.alpha = 1.0f;
523
Vishnu Nair9b079a22020-01-21 14:36:08 -0800524 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700525 invokeDraw(settings, layers, mBuffer);
526}
527
528template <typename SourceVariant>
529void RenderEngineTest::fillBufferPhysicalOffset() {
530 fillRedOffsetBuffer<SourceVariant>();
531
532 expectBufferColor(Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
533 DEFAULT_DISPLAY_HEIGHT),
534 255, 0, 0, 255);
535 Rect offsetRegionLeft(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_HEIGHT);
536 Rect offsetRegionTop(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_OFFSET);
537
538 expectBufferColor(offsetRegionLeft, 0, 0, 0, 0);
539 expectBufferColor(offsetRegionTop, 0, 0, 0, 0);
540}
541
542template <typename SourceVariant>
Alec Mouri5a6d8572020-03-23 23:56:15 -0700543void RenderEngineTest::fillBufferCheckers(uint32_t orientationFlag) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700544 renderengine::DisplaySettings settings;
545 settings.physicalDisplay = fullscreenRect();
546 // Here logical space is 2x2
547 settings.clip = Rect(2, 2);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700548 settings.orientation = orientationFlag;
Alec Mouri1089aed2018-10-25 21:33:57 -0700549
Vishnu Nair9b079a22020-01-21 14:36:08 -0800550 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700551
552 renderengine::LayerSettings layerOne;
553 Rect rectOne(0, 0, 1, 1);
554 layerOne.geometry.boundaries = rectOne.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800555 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700556 layerOne.alpha = 1.0f;
557
558 renderengine::LayerSettings layerTwo;
559 Rect rectTwo(0, 1, 1, 2);
560 layerTwo.geometry.boundaries = rectTwo.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800561 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700562 layerTwo.alpha = 1.0f;
563
564 renderengine::LayerSettings layerThree;
565 Rect rectThree(1, 0, 2, 1);
566 layerThree.geometry.boundaries = rectThree.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800567 SourceVariant::fillColor(layerThree, 0.0f, 0.0f, 1.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700568 layerThree.alpha = 1.0f;
569
Vishnu Nair9b079a22020-01-21 14:36:08 -0800570 layers.push_back(&layerOne);
571 layers.push_back(&layerTwo);
572 layers.push_back(&layerThree);
Alec Mouri1089aed2018-10-25 21:33:57 -0700573
574 invokeDraw(settings, layers, mBuffer);
575}
576
577template <typename SourceVariant>
578void RenderEngineTest::fillBufferCheckersRotate0() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700579 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_0);
Alec Mouri1089aed2018-10-25 21:33:57 -0700580 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 255, 0, 0,
581 255);
582 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
583 DEFAULT_DISPLAY_HEIGHT / 2),
584 0, 0, 255, 255);
585 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
586 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
587 0, 0, 0, 0);
588 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
589 DEFAULT_DISPLAY_HEIGHT),
590 0, 255, 0, 255);
591}
592
593template <typename SourceVariant>
594void RenderEngineTest::fillBufferCheckersRotate90() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700595 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_90);
Alec Mouri1089aed2018-10-25 21:33:57 -0700596 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 255, 0,
597 255);
598 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
599 DEFAULT_DISPLAY_HEIGHT / 2),
600 255, 0, 0, 255);
601 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
602 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
603 0, 0, 255, 255);
604 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
605 DEFAULT_DISPLAY_HEIGHT),
606 0, 0, 0, 0);
607}
608
609template <typename SourceVariant>
610void RenderEngineTest::fillBufferCheckersRotate180() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700611 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_180);
Alec Mouri1089aed2018-10-25 21:33:57 -0700612 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0,
613 0);
614 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
615 DEFAULT_DISPLAY_HEIGHT / 2),
616 0, 255, 0, 255);
617 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
618 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
619 255, 0, 0, 255);
620 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
621 DEFAULT_DISPLAY_HEIGHT),
622 0, 0, 255, 255);
623}
624
625template <typename SourceVariant>
626void RenderEngineTest::fillBufferCheckersRotate270() {
Alec Mouri5a6d8572020-03-23 23:56:15 -0700627 fillBufferCheckers<SourceVariant>(ui::Transform::ROT_270);
Alec Mouri1089aed2018-10-25 21:33:57 -0700628 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 255,
629 255);
630 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
631 DEFAULT_DISPLAY_HEIGHT / 2),
632 0, 0, 0, 0);
633 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
634 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
635 0, 255, 0, 255);
636 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
637 DEFAULT_DISPLAY_HEIGHT),
638 255, 0, 0, 255);
639}
640
641template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800642void RenderEngineTest::fillBufferWithLayerTransform() {
Alec Mouri1089aed2018-10-25 21:33:57 -0700643 renderengine::DisplaySettings settings;
644 settings.physicalDisplay = fullscreenRect();
645 // Here logical space is 2x2
646 settings.clip = Rect(2, 2);
647
Vishnu Nair9b079a22020-01-21 14:36:08 -0800648 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700649
650 renderengine::LayerSettings layer;
651 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
652 // Translate one pixel diagonally
653 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 -0800654 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700655 layer.source.solidColor = half3(1.0f, 0.0f, 0.0f);
656 layer.alpha = 1.0f;
657
Vishnu Nair9b079a22020-01-21 14:36:08 -0800658 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700659
660 invokeDraw(settings, layers, mBuffer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800661}
Alec Mouri1089aed2018-10-25 21:33:57 -0700662
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800663template <typename SourceVariant>
664void RenderEngineTest::fillBufferLayerTransform() {
665 fillBufferWithLayerTransform<SourceVariant>();
Alec Mouri1089aed2018-10-25 21:33:57 -0700666 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0, 0);
667 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 0);
668 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
669 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
670 255, 0, 0, 255);
671}
672
673template <typename SourceVariant>
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800674void RenderEngineTest::fillBufferWithColorTransform(bool useColorManagement) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700675 renderengine::DisplaySettings settings;
676 settings.physicalDisplay = fullscreenRect();
677 settings.clip = Rect(1, 1);
678
Vishnu Nair9b079a22020-01-21 14:36:08 -0800679 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri1089aed2018-10-25 21:33:57 -0700680
681 renderengine::LayerSettings layer;
682 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800683 SourceVariant::fillColor(layer, 0.5f, 0.25f, 0.125f, this, useColorManagement);
Alec Mouri1089aed2018-10-25 21:33:57 -0700684 layer.alpha = 1.0f;
685
686 // construct a fake color matrix
687 // annihilate green and blue channels
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800688 settings.colorTransform = mat4::scale(vec4(0.9f, 0, 0, 1));
Alec Mouri1089aed2018-10-25 21:33:57 -0700689 // set red channel to red + green
690 layer.colorTransform = mat4(1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
691
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800692 layer.alpha = 1.0f;
693 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
694
Vishnu Nair9b079a22020-01-21 14:36:08 -0800695 layers.push_back(&layer);
Alec Mouri1089aed2018-10-25 21:33:57 -0700696
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800697 invokeDraw(settings, layers, mBuffer, useColorManagement);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800698}
Alec Mouri1089aed2018-10-25 21:33:57 -0700699
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800700template <typename SourceVariant>
701void RenderEngineTest::fillBufferColorTransform() {
702 fillBufferWithColorTransform<SourceVariant>();
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +0800703 expectBufferColor(fullscreenRect(), 172, 0, 0, 255, 1);
704}
705
706template <typename SourceVariant>
707void RenderEngineTest::fillBufferColorTransformCM() {
708 fillBufferWithColorTransform<SourceVariant>(true);
709 expectBufferColor(fullscreenRect(), 126, 0, 0, 255, 1);
Alec Mouri1089aed2018-10-25 21:33:57 -0700710}
711
Alec Mouri7c94edb2018-12-03 21:23:26 -0800712template <typename SourceVariant>
713void RenderEngineTest::fillRedBufferWithRoundedCorners() {
714 renderengine::DisplaySettings settings;
715 settings.physicalDisplay = fullscreenRect();
716 settings.clip = fullscreenRect();
717
Vishnu Nair9b079a22020-01-21 14:36:08 -0800718 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri7c94edb2018-12-03 21:23:26 -0800719
720 renderengine::LayerSettings layer;
721 layer.geometry.boundaries = fullscreenRect().toFloatRect();
722 layer.geometry.roundedCornersRadius = 5.0f;
723 layer.geometry.roundedCornersCrop = fullscreenRect().toFloatRect();
724 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
725 layer.alpha = 1.0f;
726
Vishnu Nair9b079a22020-01-21 14:36:08 -0800727 layers.push_back(&layer);
Alec Mouri7c94edb2018-12-03 21:23:26 -0800728
729 invokeDraw(settings, layers, mBuffer);
730}
731
732template <typename SourceVariant>
733void RenderEngineTest::fillBufferWithRoundedCorners() {
734 fillRedBufferWithRoundedCorners<SourceVariant>();
735 // Corners should be ignored...
736 expectBufferColor(Rect(0, 0, 1, 1), 0, 0, 0, 0);
737 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH - 1, 0, DEFAULT_DISPLAY_WIDTH, 1), 0, 0, 0, 0);
738 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT - 1, 1, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 0);
739 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH - 1, DEFAULT_DISPLAY_HEIGHT - 1,
740 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
741 0, 0, 0, 0);
742 // ...And the non-rounded portion should be red.
743 // Other pixels may be anti-aliased, so let's not check those.
744 expectBufferColor(Rect(5, 5, DEFAULT_DISPLAY_WIDTH - 5, DEFAULT_DISPLAY_HEIGHT - 5), 255, 0, 0,
745 255);
746}
747
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000748template <typename SourceVariant>
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800749void RenderEngineTest::fillBufferAndBlurBackground() {
750 char value[PROPERTY_VALUE_MAX];
751 property_get("ro.surface_flinger.supports_background_blur", value, "0");
752 if (!atoi(value)) {
753 // This device doesn't support blurs, no-op.
754 return;
755 }
756
757 auto blurRadius = 50;
758 auto center = DEFAULT_DISPLAY_WIDTH / 2;
759
760 renderengine::DisplaySettings settings;
761 settings.physicalDisplay = fullscreenRect();
762 settings.clip = fullscreenRect();
763
Vishnu Nair9b079a22020-01-21 14:36:08 -0800764 std::vector<const renderengine::LayerSettings*> layers;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800765
766 renderengine::LayerSettings backgroundLayer;
767 backgroundLayer.geometry.boundaries = fullscreenRect().toFloatRect();
768 SourceVariant::fillColor(backgroundLayer, 0.0f, 1.0f, 0.0f, this);
769 backgroundLayer.alpha = 1.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800770 layers.push_back(&backgroundLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800771
772 renderengine::LayerSettings leftLayer;
773 leftLayer.geometry.boundaries =
774 Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT).toFloatRect();
775 SourceVariant::fillColor(leftLayer, 1.0f, 0.0f, 0.0f, this);
776 leftLayer.alpha = 1.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800777 layers.push_back(&leftLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800778
779 renderengine::LayerSettings blurLayer;
780 blurLayer.geometry.boundaries = fullscreenRect().toFloatRect();
781 blurLayer.backgroundBlurRadius = blurRadius;
782 blurLayer.alpha = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800783 layers.push_back(&blurLayer);
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800784
785 invokeDraw(settings, layers, mBuffer);
786
787 expectBufferColor(Rect(center - 1, center - 5, center, center + 5), 150, 150, 0, 255,
788 50 /* tolerance */);
789 expectBufferColor(Rect(center, center - 5, center + 1, center + 5), 150, 150, 0, 255,
790 50 /* tolerance */);
791}
792
793template <typename SourceVariant>
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000794void RenderEngineTest::overlayCorners() {
795 renderengine::DisplaySettings settings;
796 settings.physicalDisplay = fullscreenRect();
797 settings.clip = fullscreenRect();
798
Vishnu Nair9b079a22020-01-21 14:36:08 -0800799 std::vector<const renderengine::LayerSettings*> layersFirst;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000800
801 renderengine::LayerSettings layerOne;
802 layerOne.geometry.boundaries =
803 FloatRect(0, 0, DEFAULT_DISPLAY_WIDTH / 3.0, DEFAULT_DISPLAY_HEIGHT / 3.0);
804 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
805 layerOne.alpha = 0.2;
806
Vishnu Nair9b079a22020-01-21 14:36:08 -0800807 layersFirst.push_back(&layerOne);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000808 invokeDraw(settings, layersFirst, mBuffer);
809 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3, DEFAULT_DISPLAY_HEIGHT / 3), 51, 0, 0, 51);
810 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3 + 1, DEFAULT_DISPLAY_HEIGHT / 3 + 1,
811 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
812 0, 0, 0, 0);
813
Vishnu Nair9b079a22020-01-21 14:36:08 -0800814 std::vector<const renderengine::LayerSettings*> layersSecond;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000815 renderengine::LayerSettings layerTwo;
816 layerTwo.geometry.boundaries =
817 FloatRect(DEFAULT_DISPLAY_WIDTH / 3.0, DEFAULT_DISPLAY_HEIGHT / 3.0,
818 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT);
819 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
820 layerTwo.alpha = 1.0f;
821
Vishnu Nair9b079a22020-01-21 14:36:08 -0800822 layersSecond.push_back(&layerTwo);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000823 invokeDraw(settings, layersSecond, mBuffer);
824
825 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3, DEFAULT_DISPLAY_HEIGHT / 3), 0, 0, 0, 0);
826 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 3 + 1, DEFAULT_DISPLAY_HEIGHT / 3 + 1,
827 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
828 0, 255, 0, 255);
829}
830
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800831void RenderEngineTest::fillRedBufferTextureTransform() {
832 renderengine::DisplaySettings settings;
833 settings.physicalDisplay = fullscreenRect();
834 settings.clip = Rect(1, 1);
835
Vishnu Nair9b079a22020-01-21 14:36:08 -0800836 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800837
838 renderengine::LayerSettings layer;
839 // Here will allocate a checker board texture, but transform texture
840 // coordinates so that only the upper left is applied.
841 sp<GraphicBuffer> buf = allocateSourceBuffer(2, 2);
842 uint32_t texName;
843 RenderEngineTest::sRE->genTextures(1, &texName);
844 this->mTexNames.push_back(texName);
845
846 uint8_t* pixels;
847 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
848 reinterpret_cast<void**>(&pixels));
849 // Red top left, Green top right, Blue bottom left, Black bottom right
850 pixels[0] = 255;
851 pixels[1] = 0;
852 pixels[2] = 0;
853 pixels[3] = 255;
854 pixels[4] = 0;
855 pixels[5] = 255;
856 pixels[6] = 0;
857 pixels[7] = 255;
858 pixels[8] = 0;
859 pixels[9] = 0;
860 pixels[10] = 255;
861 pixels[11] = 255;
862 buf->unlock();
863
864 layer.source.buffer.buffer = buf;
865 layer.source.buffer.textureName = texName;
866 // Transform coordinates to only be inside the red quadrant.
867 layer.source.buffer.textureTransform = mat4::scale(vec4(0.2, 0.2, 1, 1));
868 layer.alpha = 1.0f;
869 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
870
Vishnu Nair9b079a22020-01-21 14:36:08 -0800871 layers.push_back(&layer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800872
873 invokeDraw(settings, layers, mBuffer);
874}
875
876void RenderEngineTest::fillBufferTextureTransform() {
877 fillRedBufferTextureTransform();
878 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
879}
880
881void RenderEngineTest::fillRedBufferWithPremultiplyAlpha() {
882 renderengine::DisplaySettings settings;
883 settings.physicalDisplay = fullscreenRect();
884 // Here logical space is 1x1
885 settings.clip = Rect(1, 1);
886
Vishnu Nair9b079a22020-01-21 14:36:08 -0800887 std::vector<const renderengine::LayerSettings*> layers;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800888
889 renderengine::LayerSettings layer;
890 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
891 uint32_t texName;
892 RenderEngineTest::sRE->genTextures(1, &texName);
893 this->mTexNames.push_back(texName);
894
895 uint8_t* pixels;
896 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
897 reinterpret_cast<void**>(&pixels));
898 pixels[0] = 255;
899 pixels[1] = 0;
900 pixels[2] = 0;
901 pixels[3] = 255;
902 buf->unlock();
903
904 layer.source.buffer.buffer = buf;
905 layer.source.buffer.textureName = texName;
906 layer.source.buffer.usePremultipliedAlpha = true;
907 layer.alpha = 0.5f;
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::fillBufferWithPremultiplyAlpha() {
916 fillRedBufferWithPremultiplyAlpha();
917 expectBufferColor(fullscreenRect(), 128, 0, 0, 128);
918}
919
920void RenderEngineTest::fillRedBufferWithoutPremultiplyAlpha() {
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 = false;
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::fillBufferWithoutPremultiplyAlpha() {
955 fillRedBufferWithoutPremultiplyAlpha();
wukui16f3c0bb2020-08-05 20:35:29 +0800956 expectBufferColor(fullscreenRect(), 128, 0, 0, 128, 1);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800957}
958
Alec Mouriac335532018-11-12 15:01:33 -0800959void RenderEngineTest::clearLeftRegion() {
960 renderengine::DisplaySettings settings;
961 settings.physicalDisplay = fullscreenRect();
962 // Here logical space is 4x4
963 settings.clip = Rect(4, 4);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700964 settings.clearRegion = Region(Rect(2, 4));
Vishnu Nair9b079a22020-01-21 14:36:08 -0800965 std::vector<const renderengine::LayerSettings*> layers;
Xin Lie8b4e702020-08-29 01:34:09 -0700966 // fake layer, without bounds should not render anything
Alec Mouriac335532018-11-12 15:01:33 -0800967 renderengine::LayerSettings layer;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800968 layers.push_back(&layer);
Alec Mouriac335532018-11-12 15:01:33 -0800969 invokeDraw(settings, layers, mBuffer);
970}
971
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000972void RenderEngineTest::clearRegion() {
Alec Mouriac335532018-11-12 15:01:33 -0800973 // Reuse mBuffer
974 clearLeftRegion();
975 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 255);
976 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
977 DEFAULT_DISPLAY_HEIGHT),
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000978 0, 0, 0, 0);
Alec Mouriac335532018-11-12 15:01:33 -0800979}
980
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800981template <typename SourceVariant>
982void RenderEngineTest::drawShadow(const renderengine::LayerSettings& castingLayer,
983 const renderengine::ShadowSettings& shadow,
984 const ubyte4& casterColor, const ubyte4& backgroundColor) {
985 renderengine::DisplaySettings settings;
986 settings.physicalDisplay = fullscreenRect();
987 settings.clip = fullscreenRect();
988
Vishnu Nair9b079a22020-01-21 14:36:08 -0800989 std::vector<const renderengine::LayerSettings*> layers;
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800990
991 // add background layer
992 renderengine::LayerSettings bgLayer;
993 bgLayer.geometry.boundaries = fullscreenRect().toFloatRect();
994 ColorSourceVariant::fillColor(bgLayer, backgroundColor.r / 255.0f, backgroundColor.g / 255.0f,
995 backgroundColor.b / 255.0f, this);
996 bgLayer.alpha = backgroundColor.a / 255.0f;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800997 layers.push_back(&bgLayer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -0800998
999 // add shadow layer
1000 renderengine::LayerSettings shadowLayer;
1001 shadowLayer.geometry.boundaries = castingLayer.geometry.boundaries;
1002 shadowLayer.alpha = castingLayer.alpha;
1003 shadowLayer.shadow = shadow;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001004 layers.push_back(&shadowLayer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001005
1006 // add layer casting the shadow
1007 renderengine::LayerSettings layer = castingLayer;
1008 SourceVariant::fillColor(layer, casterColor.r / 255.0f, casterColor.g / 255.0f,
1009 casterColor.b / 255.0f, this);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001010 layers.push_back(&layer);
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001011
1012 invokeDraw(settings, layers, mBuffer);
1013}
1014
Alec Mouri1089aed2018-10-25 21:33:57 -07001015TEST_F(RenderEngineTest, drawLayers_noLayersToDraw) {
1016 drawEmptyLayers();
1017}
1018
Alec Mourid43ccab2019-03-13 12:23:45 -07001019TEST_F(RenderEngineTest, drawLayers_nullOutputBuffer) {
1020 renderengine::DisplaySettings settings;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001021 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001022 renderengine::LayerSettings layer;
1023 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1024 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Vishnu Nair9b079a22020-01-21 14:36:08 -08001025 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001026 base::unique_fd fence;
Alec Mourife0d72b2019-03-21 14:05:56 -07001027 status_t status = sRE->drawLayers(settings, layers, nullptr, true, base::unique_fd(), &fence);
Alec Mourid43ccab2019-03-13 12:23:45 -07001028
1029 ASSERT_EQ(BAD_VALUE, status);
1030}
1031
1032TEST_F(RenderEngineTest, drawLayers_nullOutputFence) {
1033 renderengine::DisplaySettings settings;
1034 settings.physicalDisplay = fullscreenRect();
1035 settings.clip = fullscreenRect();
1036
Vishnu Nair9b079a22020-01-21 14:36:08 -08001037 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001038 renderengine::LayerSettings layer;
1039 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1040 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1041 layer.alpha = 1.0;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001042 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001043
Alec Mourife0d72b2019-03-21 14:05:56 -07001044 status_t status = sRE->drawLayers(settings, layers, mBuffer->getNativeBuffer(), true,
Alec Mourid43ccab2019-03-13 12:23:45 -07001045 base::unique_fd(), nullptr);
1046 sCurrentBuffer = mBuffer;
1047 ASSERT_EQ(NO_ERROR, status);
1048 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
1049}
1050
Alec Mourife0d72b2019-03-21 14:05:56 -07001051TEST_F(RenderEngineTest, drawLayers_doesNotCacheFramebuffer) {
1052 renderengine::DisplaySettings settings;
1053 settings.physicalDisplay = fullscreenRect();
1054 settings.clip = fullscreenRect();
1055
Vishnu Nair9b079a22020-01-21 14:36:08 -08001056 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourife0d72b2019-03-21 14:05:56 -07001057 renderengine::LayerSettings layer;
1058 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1059 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1060 layer.alpha = 1.0;
Vishnu Nair9b079a22020-01-21 14:36:08 -08001061 layers.push_back(&layer);
Alec Mourife0d72b2019-03-21 14:05:56 -07001062
1063 status_t status = sRE->drawLayers(settings, layers, mBuffer->getNativeBuffer(), false,
1064 base::unique_fd(), nullptr);
1065 sCurrentBuffer = mBuffer;
1066 ASSERT_EQ(NO_ERROR, status);
1067 ASSERT_FALSE(sRE->isFramebufferImageCachedForTesting(mBuffer->getId()));
1068 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
1069}
1070
Alec Mouri1089aed2018-10-25 21:33:57 -07001071TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_colorSource) {
1072 fillRedBuffer<ColorSourceVariant>();
1073}
1074
1075TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_colorSource) {
1076 fillGreenBuffer<ColorSourceVariant>();
1077}
1078
1079TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_colorSource) {
1080 fillBlueBuffer<ColorSourceVariant>();
1081}
1082
1083TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_colorSource) {
1084 fillRedTransparentBuffer<ColorSourceVariant>();
1085}
1086
1087TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_colorSource) {
1088 fillBufferPhysicalOffset<ColorSourceVariant>();
1089}
1090
1091TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_colorSource) {
1092 fillBufferCheckersRotate0<ColorSourceVariant>();
1093}
1094
1095TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_colorSource) {
1096 fillBufferCheckersRotate90<ColorSourceVariant>();
1097}
1098
1099TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_colorSource) {
1100 fillBufferCheckersRotate180<ColorSourceVariant>();
1101}
1102
1103TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_colorSource) {
1104 fillBufferCheckersRotate270<ColorSourceVariant>();
1105}
1106
1107TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_colorSource) {
1108 fillBufferLayerTransform<ColorSourceVariant>();
1109}
1110
1111TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_colorSource) {
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +08001112 fillBufferColorTransform<ColorSourceVariant>();
1113}
1114
1115TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransformCM_colorSource) {
1116 fillBufferColorTransformCM<ColorSourceVariant>();
Alec Mouri6e57f682018-09-29 20:45:08 -07001117}
1118
Alec Mouri7c94edb2018-12-03 21:23:26 -08001119TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_colorSource) {
1120 fillBufferWithRoundedCorners<ColorSourceVariant>();
1121}
1122
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001123TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_colorSource) {
1124 fillBufferAndBlurBackground<ColorSourceVariant>();
1125}
1126
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001127TEST_F(RenderEngineTest, drawLayers_overlayCorners_colorSource) {
1128 overlayCorners<ColorSourceVariant>();
1129}
1130
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001131TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_opaqueBufferSource) {
1132 fillRedBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1133}
1134
1135TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_opaqueBufferSource) {
1136 fillGreenBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1137}
1138
1139TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_opaqueBufferSource) {
1140 fillBlueBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1141}
1142
1143TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_opaqueBufferSource) {
1144 fillRedTransparentBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1145}
1146
1147TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_opaqueBufferSource) {
1148 fillBufferPhysicalOffset<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1149}
1150
1151TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_opaqueBufferSource) {
1152 fillBufferCheckersRotate0<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1153}
1154
1155TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_opaqueBufferSource) {
1156 fillBufferCheckersRotate90<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1157}
1158
1159TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_opaqueBufferSource) {
1160 fillBufferCheckersRotate180<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1161}
1162
1163TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_opaqueBufferSource) {
1164 fillBufferCheckersRotate270<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1165}
1166
1167TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_opaqueBufferSource) {
1168 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1169}
1170
1171TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_opaqueBufferSource) {
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +08001172 fillBufferColorTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1173}
1174
1175TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransformCM_opaqueBufferSource) {
1176 fillBufferColorTransformCM<BufferSourceVariant<ForceOpaqueBufferVariant>>();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001177}
1178
Alec Mouri7c94edb2018-12-03 21:23:26 -08001179TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_opaqueBufferSource) {
1180 fillBufferWithRoundedCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1181}
1182
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001183TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_opaqueBufferSource) {
1184 fillBufferAndBlurBackground<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1185}
1186
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001187TEST_F(RenderEngineTest, drawLayers_overlayCorners_opaqueBufferSource) {
1188 overlayCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
1189}
1190
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001191TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_bufferSource) {
1192 fillRedBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1193}
1194
1195TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_bufferSource) {
1196 fillGreenBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1197}
1198
1199TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_bufferSource) {
1200 fillBlueBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1201}
1202
1203TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_bufferSource) {
1204 fillRedTransparentBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1205}
1206
1207TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_bufferSource) {
1208 fillBufferPhysicalOffset<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1209}
1210
1211TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_bufferSource) {
1212 fillBufferCheckersRotate0<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1213}
1214
1215TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_bufferSource) {
1216 fillBufferCheckersRotate90<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1217}
1218
1219TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_bufferSource) {
1220 fillBufferCheckersRotate180<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1221}
1222
1223TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_bufferSource) {
1224 fillBufferCheckersRotate270<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1225}
1226
1227TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_bufferSource) {
1228 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1229}
1230
1231TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_bufferSource) {
KaiChieh Chuangba2f0bc2020-09-07 13:48:42 +08001232 fillBufferColorTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1233}
1234
1235TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransformCM_bufferSource) {
1236 fillBufferColorTransformCM<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001237}
1238
Alec Mouri7c94edb2018-12-03 21:23:26 -08001239TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_bufferSource) {
1240 fillBufferWithRoundedCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1241}
1242
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001243TEST_F(RenderEngineTest, drawLayers_fillBufferAndBlurBackground_bufferSource) {
1244 fillBufferAndBlurBackground<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1245}
1246
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001247TEST_F(RenderEngineTest, drawLayers_overlayCorners_bufferSource) {
1248 overlayCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
1249}
1250
Alec Mouri0d5e1eb2018-11-10 20:40:12 -08001251TEST_F(RenderEngineTest, drawLayers_fillBufferTextureTransform) {
1252 fillBufferTextureTransform();
1253}
1254
1255TEST_F(RenderEngineTest, drawLayers_fillBuffer_premultipliesAlpha) {
1256 fillBufferWithPremultiplyAlpha();
1257}
1258
1259TEST_F(RenderEngineTest, drawLayers_fillBuffer_withoutPremultiplyingAlpha) {
1260 fillBufferWithoutPremultiplyAlpha();
1261}
1262
Alec Mourie7d1d4a2019-02-05 01:13:46 +00001263TEST_F(RenderEngineTest, drawLayers_clearRegion) {
1264 clearRegion();
Alec Mouriac335532018-11-12 15:01:33 -08001265}
1266
Alec Mourid43ccab2019-03-13 12:23:45 -07001267TEST_F(RenderEngineTest, drawLayers_fillsBufferAndCachesImages) {
1268 renderengine::DisplaySettings settings;
1269 settings.physicalDisplay = fullscreenRect();
1270 settings.clip = fullscreenRect();
1271
Vishnu Nair9b079a22020-01-21 14:36:08 -08001272 std::vector<const renderengine::LayerSettings*> layers;
Alec Mourid43ccab2019-03-13 12:23:45 -07001273
1274 renderengine::LayerSettings layer;
1275 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1276 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1277
Vishnu Nair9b079a22020-01-21 14:36:08 -08001278 layers.push_back(&layer);
Alec Mourid43ccab2019-03-13 12:23:45 -07001279 invokeDraw(settings, layers, mBuffer);
1280 uint64_t bufferId = layer.source.buffer.buffer->getId();
1281 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001282 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1283 sRE->unbindExternalTextureBufferForTesting(bufferId);
1284 std::lock_guard<std::mutex> lock(barrier->mutex);
1285 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1286 [&]() REQUIRES(barrier->mutex) {
1287 return barrier->isOpen;
1288 }));
Alec Mourid43ccab2019-03-13 12:23:45 -07001289 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001290 EXPECT_EQ(NO_ERROR, barrier->result);
Alec Mourid43ccab2019-03-13 12:23:45 -07001291}
1292
Alec Mouri4dde1782019-09-30 17:27:13 -07001293TEST_F(RenderEngineTest, bindExternalBuffer_withNullBuffer) {
Alec Mourid43ccab2019-03-13 12:23:45 -07001294 status_t result = sRE->bindExternalTextureBuffer(0, nullptr, nullptr);
1295 ASSERT_EQ(BAD_VALUE, result);
1296}
1297
Alec Mouri4dde1782019-09-30 17:27:13 -07001298TEST_F(RenderEngineTest, bindExternalBuffer_cachesImages) {
Alec Mourid43ccab2019-03-13 12:23:45 -07001299 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
1300 uint32_t texName;
1301 sRE->genTextures(1, &texName);
1302 mTexNames.push_back(texName);
1303
1304 sRE->bindExternalTextureBuffer(texName, buf, nullptr);
1305 uint64_t bufferId = buf->getId();
1306 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001307 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1308 sRE->unbindExternalTextureBufferForTesting(bufferId);
1309 std::lock_guard<std::mutex> lock(barrier->mutex);
1310 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1311 [&]() REQUIRES(barrier->mutex) {
1312 return barrier->isOpen;
1313 }));
1314 EXPECT_EQ(NO_ERROR, barrier->result);
Alec Mourid43ccab2019-03-13 12:23:45 -07001315 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1316}
1317
Alec Mouri4dde1782019-09-30 17:27:13 -07001318TEST_F(RenderEngineTest, cacheExternalBuffer_withNullBuffer) {
Alec Mouri16a99402019-07-29 16:37:30 -07001319 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1320 sRE->cacheExternalTextureBufferForTesting(nullptr);
1321 std::lock_guard<std::mutex> lock(barrier->mutex);
1322 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1323 [&]() REQUIRES(barrier->mutex) {
1324 return barrier->isOpen;
1325 }));
1326 EXPECT_TRUE(barrier->isOpen);
1327 EXPECT_EQ(BAD_VALUE, barrier->result);
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001328}
1329
Alec Mouri4dde1782019-09-30 17:27:13 -07001330TEST_F(RenderEngineTest, cacheExternalBuffer_cachesImages) {
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001331 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
1332 uint64_t bufferId = buf->getId();
Alec Mouri16a99402019-07-29 16:37:30 -07001333 std::shared_ptr<renderengine::gl::ImageManager::Barrier> barrier =
1334 sRE->cacheExternalTextureBufferForTesting(buf);
1335 {
1336 std::lock_guard<std::mutex> lock(barrier->mutex);
1337 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1338 [&]() REQUIRES(barrier->mutex) {
1339 return barrier->isOpen;
1340 }));
1341 EXPECT_EQ(NO_ERROR, barrier->result);
1342 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001343 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
Alec Mouri16a99402019-07-29 16:37:30 -07001344 barrier = sRE->unbindExternalTextureBufferForTesting(bufferId);
1345 {
1346 std::lock_guard<std::mutex> lock(barrier->mutex);
1347 ASSERT_TRUE(barrier->condition.wait_for(barrier->mutex, std::chrono::seconds(5),
1348 [&]() REQUIRES(barrier->mutex) {
1349 return barrier->isOpen;
1350 }));
1351 EXPECT_EQ(NO_ERROR, barrier->result);
1352 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -07001353 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1354}
1355
Vishnu Nair16efdbf2019-12-10 11:55:42 -08001356TEST_F(RenderEngineTest, drawLayers_fillShadow_casterLayerMinSize) {
1357 const ubyte4 casterColor(255, 0, 0, 255);
1358 const ubyte4 backgroundColor(255, 255, 255, 255);
1359 const float shadowLength = 5.0f;
1360 Rect casterBounds(1, 1);
1361 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1362 renderengine::LayerSettings castingLayer;
1363 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1364 castingLayer.alpha = 1.0f;
1365 renderengine::ShadowSettings settings =
1366 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1367 false /* casterIsTranslucent */);
1368
1369 drawShadow<ColorSourceVariant>(castingLayer, settings, casterColor, backgroundColor);
1370 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1371}
1372
1373TEST_F(RenderEngineTest, drawLayers_fillShadow_casterColorLayer) {
1374 const ubyte4 casterColor(255, 0, 0, 255);
1375 const ubyte4 backgroundColor(255, 255, 255, 255);
1376 const float shadowLength = 5.0f;
1377 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1378 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1379 renderengine::LayerSettings castingLayer;
1380 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1381 castingLayer.alpha = 1.0f;
1382 renderengine::ShadowSettings settings =
1383 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1384 false /* casterIsTranslucent */);
1385
1386 drawShadow<ColorSourceVariant>(castingLayer, settings, casterColor, backgroundColor);
1387 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1388}
1389
1390TEST_F(RenderEngineTest, drawLayers_fillShadow_casterOpaqueBufferLayer) {
1391 const ubyte4 casterColor(255, 0, 0, 255);
1392 const ubyte4 backgroundColor(255, 255, 255, 255);
1393 const float shadowLength = 5.0f;
1394 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1395 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1396 renderengine::LayerSettings castingLayer;
1397 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1398 castingLayer.alpha = 1.0f;
1399 renderengine::ShadowSettings settings =
1400 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1401 false /* casterIsTranslucent */);
1402
1403 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1404 backgroundColor);
1405 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1406}
1407
1408TEST_F(RenderEngineTest, drawLayers_fillShadow_casterWithRoundedCorner) {
1409 const ubyte4 casterColor(255, 0, 0, 255);
1410 const ubyte4 backgroundColor(255, 255, 255, 255);
1411 const float shadowLength = 5.0f;
1412 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1413 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1414 renderengine::LayerSettings castingLayer;
1415 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1416 castingLayer.geometry.roundedCornersRadius = 3.0f;
1417 castingLayer.geometry.roundedCornersCrop = casterBounds.toFloatRect();
1418 castingLayer.alpha = 1.0f;
1419 renderengine::ShadowSettings settings =
1420 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1421 false /* casterIsTranslucent */);
1422
1423 drawShadow<BufferSourceVariant<ForceOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1424 backgroundColor);
1425 expectShadowColor(castingLayer, settings, casterColor, backgroundColor);
1426}
1427
1428TEST_F(RenderEngineTest, drawLayers_fillShadow_translucentCasterWithAlpha) {
1429 const ubyte4 casterColor(255, 0, 0, 255);
1430 const ubyte4 backgroundColor(255, 255, 255, 255);
1431 const float shadowLength = 5.0f;
1432 Rect casterBounds(DEFAULT_DISPLAY_WIDTH / 3.0f, DEFAULT_DISPLAY_HEIGHT / 3.0f);
1433 casterBounds.offsetBy(shadowLength + 1, shadowLength + 1);
1434 renderengine::LayerSettings castingLayer;
1435 castingLayer.geometry.boundaries = casterBounds.toFloatRect();
1436 castingLayer.alpha = 0.5f;
1437 renderengine::ShadowSettings settings =
1438 getShadowSettings(vec2(casterBounds.left, casterBounds.top), shadowLength,
1439 true /* casterIsTranslucent */);
1440
1441 drawShadow<BufferSourceVariant<RelaxOpaqueBufferVariant>>(castingLayer, settings, casterColor,
1442 backgroundColor);
1443
1444 // verify only the background since the shadow will draw behind the caster
1445 const float shadowInset = settings.length * -1.0f;
1446 const Rect casterWithShadow =
1447 Rect(casterBounds).inset(shadowInset, shadowInset, shadowInset, shadowInset);
1448 const Region backgroundRegion = Region(fullscreenRect()).subtractSelf(casterWithShadow);
1449 expectBufferColor(backgroundRegion, backgroundColor.r, backgroundColor.g, backgroundColor.b,
1450 backgroundColor.a);
1451}
1452
Alec Mouri4dde1782019-09-30 17:27:13 -07001453TEST_F(RenderEngineTest, cleanupPostRender_cleansUpOnce) {
1454 renderengine::DisplaySettings settings;
1455 settings.physicalDisplay = fullscreenRect();
1456 settings.clip = fullscreenRect();
1457
1458 std::vector<const renderengine::LayerSettings*> layers;
1459 renderengine::LayerSettings layer;
1460 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1461 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1462 layer.alpha = 1.0;
1463 layers.push_back(&layer);
1464
1465 base::unique_fd fenceOne;
1466 sRE->drawLayers(settings, layers, mBuffer->getNativeBuffer(), true, base::unique_fd(),
1467 &fenceOne);
1468 base::unique_fd fenceTwo;
1469 sRE->drawLayers(settings, layers, mBuffer->getNativeBuffer(), true, std::move(fenceOne),
1470 &fenceTwo);
1471
1472 const int fd = fenceTwo.get();
1473 if (fd >= 0) {
1474 sync_wait(fd, -1);
1475 }
Alec Mouri4dde1782019-09-30 17:27:13 -07001476 // Only cleanup the first time.
Alec Mouri56f295e2020-08-13 10:14:29 -07001477 EXPECT_TRUE(sRE->cleanupPostRender(
1478 renderengine::RenderEngine::CleanupMode::CLEAN_OUTPUT_RESOURCES));
1479 EXPECT_FALSE(sRE->cleanupPostRender(
1480 renderengine::RenderEngine::CleanupMode::CLEAN_OUTPUT_RESOURCES));
1481}
1482
1483TEST_F(RenderEngineTest, cleanupPostRender_whenCleaningAll_replacesTextureMemory) {
1484 renderengine::DisplaySettings settings;
1485 settings.physicalDisplay = fullscreenRect();
1486 settings.clip = fullscreenRect();
1487
1488 std::vector<const renderengine::LayerSettings*> layers;
1489 renderengine::LayerSettings layer;
1490 layer.geometry.boundaries = fullscreenRect().toFloatRect();
1491 BufferSourceVariant<ForceOpaqueBufferVariant>::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
1492 layer.alpha = 1.0;
1493 layers.push_back(&layer);
1494
1495 base::unique_fd fence;
1496 sRE->drawLayers(settings, layers, mBuffer->getNativeBuffer(), true, base::unique_fd(), &fence);
1497
1498 const int fd = fence.get();
1499 if (fd >= 0) {
1500 sync_wait(fd, -1);
1501 }
1502
1503 uint64_t bufferId = layer.source.buffer.buffer->getId();
1504 uint32_t texName = layer.source.buffer.textureName;
1505 EXPECT_TRUE(sRE->isImageCachedForTesting(bufferId));
1506 EXPECT_EQ(bufferId, sRE->getBufferIdForTextureNameForTesting(texName));
1507
1508 EXPECT_TRUE(sRE->cleanupPostRender(renderengine::RenderEngine::CleanupMode::CLEAN_ALL));
1509
1510 // Now check that our view of memory is good.
1511 EXPECT_FALSE(sRE->isImageCachedForTesting(bufferId));
1512 EXPECT_EQ(std::nullopt, sRE->getBufferIdForTextureNameForTesting(bufferId));
1513 EXPECT_TRUE(sRE->isTextureNameKnownForTesting(texName));
Alec Mouri4dde1782019-09-30 17:27:13 -07001514}
1515
Alec Mouri6e57f682018-09-29 20:45:08 -07001516} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001517
1518// TODO(b/129481165): remove the #pragma below and fix conversion issues
1519#pragma clang diagnostic pop // ignored "-Wconversion"