blob: bef25a83c7c0e57e83b772c2ceff2e4765fc285c [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
17#include <gtest/gtest.h>
18
19#include <renderengine/RenderEngine.h>
Alec Mouri1089aed2018-10-25 21:33:57 -070020#include <sync/sync.h>
Alec Mouri6e57f682018-09-29 20:45:08 -070021#include <ui/PixelFormat.h>
22
Alec Mouri1089aed2018-10-25 21:33:57 -070023constexpr int DEFAULT_DISPLAY_WIDTH = 128;
24constexpr int DEFAULT_DISPLAY_HEIGHT = 256;
25constexpr int DEFAULT_DISPLAY_OFFSET = 64;
26
Alec Mouri6e57f682018-09-29 20:45:08 -070027namespace android {
28
Alec Mouri1089aed2018-10-25 21:33:57 -070029struct RenderEngineTest : public ::testing::Test {
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080030 static sp<GraphicBuffer> allocateDefaultBuffer() {
Alec Mouri1089aed2018-10-25 21:33:57 -070031 return new GraphicBuffer(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT,
32 HAL_PIXEL_FORMAT_RGBA_8888, 1,
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080033 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
34 GRALLOC_USAGE_HW_RENDER,
Alec Mouri1089aed2018-10-25 21:33:57 -070035 "output");
Alec Mouri6e57f682018-09-29 20:45:08 -070036 }
37
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080038 // Allocates a 1x1 buffer to fill with a solid color
39 static sp<GraphicBuffer> allocateSourceBuffer(uint32_t width, uint32_t height) {
40 return new GraphicBuffer(width, height, HAL_PIXEL_FORMAT_RGBA_8888, 1,
41 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
42 GRALLOC_USAGE_HW_TEXTURE,
43 "input");
44 }
45
Alec Mouri1089aed2018-10-25 21:33:57 -070046 RenderEngineTest() { mBuffer = allocateDefaultBuffer(); }
47
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080048 ~RenderEngineTest() {
49 for (uint32_t texName : mTexNames) {
50 sRE->deleteTextures(1, &texName);
51 }
52 }
53
Alec Mouri1089aed2018-10-25 21:33:57 -070054 void expectBufferColor(const Rect& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a,
55 uint8_t tolerance = 0) {
56 uint8_t* pixels;
57 mBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
58 reinterpret_cast<void**>(&pixels));
59
60 auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
61 uint8_t tmp = a >= b ? a - b : b - a;
62 return tmp <= tolerance;
63 };
64 int32_t maxFails = 10;
65 int32_t fails = 0;
66 for (int32_t j = 0; j < region.getHeight(); j++) {
67 const uint8_t* src =
68 pixels + (mBuffer->getStride() * (region.top + j) + region.left) * 4;
69 for (int32_t i = 0; i < region.getWidth(); i++) {
70 const uint8_t expected[4] = {r, g, b, a};
71 bool equal = std::equal(src, src + 4, expected, colorCompare);
72 EXPECT_TRUE(equal)
73 << "pixel @ (" << region.left + i << ", " << region.top + j << "): "
74 << "expected (" << static_cast<uint32_t>(r) << ", "
75 << static_cast<uint32_t>(g) << ", " << static_cast<uint32_t>(b) << ", "
76 << static_cast<uint32_t>(a) << "), "
77 << "got (" << static_cast<uint32_t>(src[0]) << ", "
78 << static_cast<uint32_t>(src[1]) << ", " << static_cast<uint32_t>(src[2])
79 << ", " << static_cast<uint32_t>(src[3]) << ")";
80 src += 4;
81 if (!equal && ++fails >= maxFails) {
82 break;
83 }
84 }
85 if (fails >= maxFails) {
86 break;
87 }
88 }
89 mBuffer->unlock();
90 }
91
92 static Rect fullscreenRect() { return Rect(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT); }
93
94 static Rect offsetRect() {
95 return Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
96 DEFAULT_DISPLAY_HEIGHT);
97 }
98
99 static Rect offsetRectAtZero() {
100 return Rect(DEFAULT_DISPLAY_WIDTH - DEFAULT_DISPLAY_OFFSET,
101 DEFAULT_DISPLAY_HEIGHT - DEFAULT_DISPLAY_OFFSET);
102 }
103
104 static void invokeDraw(renderengine::DisplaySettings settings,
105 std::vector<renderengine::LayerSettings> layers,
106 sp<GraphicBuffer> buffer) {
107 base::unique_fd fence;
108 status_t status = sRE->drawLayers(settings, layers, buffer->getNativeBuffer(), &fence);
109
110 int fd = fence.release();
111 if (fd >= 0) {
112 sync_wait(fd, -1);
113 close(fd);
114 }
115
116 ASSERT_EQ(NO_ERROR, status);
117 }
118
119 static void drawEmptyLayers() {
Alec Mouri6e57f682018-09-29 20:45:08 -0700120 renderengine::DisplaySettings settings;
121 std::vector<renderengine::LayerSettings> layers;
122 // Meaningless buffer since we don't do any drawing
123 sp<GraphicBuffer> buffer = new GraphicBuffer();
Alec Mouri1089aed2018-10-25 21:33:57 -0700124 invokeDraw(settings, layers, buffer);
Alec Mouri6e57f682018-09-29 20:45:08 -0700125 }
126
Alec Mouri1089aed2018-10-25 21:33:57 -0700127 template <typename SourceVariant>
128 void fillBuffer(half r, half g, half b, half a);
129
130 template <typename SourceVariant>
131 void fillRedBuffer();
132
133 template <typename SourceVariant>
134 void fillGreenBuffer();
135
136 template <typename SourceVariant>
137 void fillBlueBuffer();
138
139 template <typename SourceVariant>
140 void fillRedTransparentBuffer();
141
142 template <typename SourceVariant>
143 void fillRedOffsetBuffer();
144
145 template <typename SourceVariant>
146 void fillBufferPhysicalOffset();
147
148 template <typename SourceVariant>
149 void fillBufferCheckers(mat4 transform);
150
151 template <typename SourceVariant>
152 void fillBufferCheckersRotate0();
153
154 template <typename SourceVariant>
155 void fillBufferCheckersRotate90();
156
157 template <typename SourceVariant>
158 void fillBufferCheckersRotate180();
159
160 template <typename SourceVariant>
161 void fillBufferCheckersRotate270();
162
163 template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800164 void fillBufferWithLayerTransform();
165
166 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700167 void fillBufferLayerTransform();
168
169 template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800170 void fillBufferWithColorTransform();
171
172 template <typename SourceVariant>
Alec Mouri1089aed2018-10-25 21:33:57 -0700173 void fillBufferColorTransform();
174
Alec Mouri7c94edb2018-12-03 21:23:26 -0800175 template <typename SourceVariant>
176 void fillRedBufferWithRoundedCorners();
177
178 template <typename SourceVariant>
179 void fillBufferWithRoundedCorners();
180
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800181 void fillRedBufferTextureTransform();
182
183 void fillBufferTextureTransform();
184
185 void fillRedBufferWithPremultiplyAlpha();
186
187 void fillBufferWithPremultiplyAlpha();
188
189 void fillRedBufferWithoutPremultiplyAlpha();
190
191 void fillBufferWithoutPremultiplyAlpha();
192
Alec Mouriac335532018-11-12 15:01:33 -0800193 void fillGreenColorBufferThenClearRegion();
194
195 void clearLeftRegion();
196
197 void fillBufferThenClearRegion();
198
Alec Mouri1089aed2018-10-25 21:33:57 -0700199 // Dumb hack to get aroud the fact that tear-down for renderengine isn't
200 // well defined right now, so we can't create multiple instances
201 static std::unique_ptr<renderengine::RenderEngine> sRE;
202
203 sp<GraphicBuffer> mBuffer;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800204
205 std::vector<uint32_t> mTexNames;
Alec Mouri6e57f682018-09-29 20:45:08 -0700206};
207
Alec Mouri1089aed2018-10-25 21:33:57 -0700208std::unique_ptr<renderengine::RenderEngine> RenderEngineTest::sRE =
209 renderengine::RenderEngine::create(static_cast<int32_t>(ui::PixelFormat::RGBA_8888), 0);
210
211struct ColorSourceVariant {
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800212 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
213 RenderEngineTest* /*fixture*/) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700214 layer.source.solidColor = half3(r, g, b);
215 }
216};
217
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800218struct RelaxOpaqueBufferVariant {
219 static void setOpaqueBit(renderengine::LayerSettings& layer) {
220 layer.source.buffer.isOpaque = false;
221 }
222
223 static uint8_t getAlphaChannel() { return 255; }
224};
225
226struct ForceOpaqueBufferVariant {
227 static void setOpaqueBit(renderengine::LayerSettings& layer) {
228 layer.source.buffer.isOpaque = true;
229 }
230
231 static uint8_t getAlphaChannel() {
232 // The isOpaque bit will override the alpha channel, so this should be
233 // arbitrary.
234 return 10;
235 }
236};
237
238template <typename OpaquenessVariant>
239struct BufferSourceVariant {
240 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
241 RenderEngineTest* fixture) {
242 sp<GraphicBuffer> buf = RenderEngineTest::allocateSourceBuffer(1, 1);
243 uint32_t texName;
244 RenderEngineTest::sRE->genTextures(1, &texName);
245 fixture->mTexNames.push_back(texName);
246
247 uint8_t* pixels;
248 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
249 reinterpret_cast<void**>(&pixels));
250
251 for (int32_t j = 0; j < buf->getHeight(); j++) {
252 uint8_t* iter = pixels + (buf->getStride() * j) * 4;
253 for (int32_t i = 0; i < buf->getWidth(); i++) {
254 iter[0] = uint8_t(r * 255);
255 iter[1] = uint8_t(g * 255);
256 iter[2] = uint8_t(b * 255);
257 iter[3] = OpaquenessVariant::getAlphaChannel();
258 iter += 4;
259 }
260 }
261
262 buf->unlock();
263
264 layer.source.buffer.buffer = buf;
265 layer.source.buffer.textureName = texName;
266 OpaquenessVariant::setOpaqueBit(layer);
267 }
268};
269
Alec Mouri1089aed2018-10-25 21:33:57 -0700270template <typename SourceVariant>
271void RenderEngineTest::fillBuffer(half r, half g, half b, half a) {
272 renderengine::DisplaySettings settings;
273 settings.physicalDisplay = fullscreenRect();
274 settings.clip = fullscreenRect();
275
276 std::vector<renderengine::LayerSettings> layers;
277
278 renderengine::LayerSettings layer;
279 layer.geometry.boundaries = fullscreenRect().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800280 SourceVariant::fillColor(layer, r, g, b, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700281 layer.alpha = a;
282
283 layers.push_back(layer);
284
285 invokeDraw(settings, layers, mBuffer);
286}
287
288template <typename SourceVariant>
289void RenderEngineTest::fillRedBuffer() {
290 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, 1.0f);
291 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
292}
293
294template <typename SourceVariant>
295void RenderEngineTest::fillGreenBuffer() {
296 fillBuffer<SourceVariant>(0.0f, 1.0f, 0.0f, 1.0f);
297 expectBufferColor(fullscreenRect(), 0, 255, 0, 255);
298}
299
300template <typename SourceVariant>
301void RenderEngineTest::fillBlueBuffer() {
302 fillBuffer<SourceVariant>(0.0f, 0.0f, 1.0f, 1.0f);
303 expectBufferColor(fullscreenRect(), 0, 0, 255, 255);
304}
305
306template <typename SourceVariant>
307void RenderEngineTest::fillRedTransparentBuffer() {
308 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, .2f);
309 expectBufferColor(fullscreenRect(), 51, 0, 0, 51);
310}
311
312template <typename SourceVariant>
313void RenderEngineTest::fillRedOffsetBuffer() {
314 renderengine::DisplaySettings settings;
315 settings.physicalDisplay = offsetRect();
316 settings.clip = offsetRectAtZero();
317
318 std::vector<renderengine::LayerSettings> layers;
319
320 renderengine::LayerSettings layer;
321 layer.geometry.boundaries = offsetRectAtZero().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800322 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700323 layer.alpha = 1.0f;
324
325 layers.push_back(layer);
326 invokeDraw(settings, layers, mBuffer);
327}
328
329template <typename SourceVariant>
330void RenderEngineTest::fillBufferPhysicalOffset() {
331 fillRedOffsetBuffer<SourceVariant>();
332
333 expectBufferColor(Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
334 DEFAULT_DISPLAY_HEIGHT),
335 255, 0, 0, 255);
336 Rect offsetRegionLeft(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_HEIGHT);
337 Rect offsetRegionTop(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_OFFSET);
338
339 expectBufferColor(offsetRegionLeft, 0, 0, 0, 0);
340 expectBufferColor(offsetRegionTop, 0, 0, 0, 0);
341}
342
343template <typename SourceVariant>
344void RenderEngineTest::fillBufferCheckers(mat4 transform) {
345 renderengine::DisplaySettings settings;
346 settings.physicalDisplay = fullscreenRect();
347 // Here logical space is 2x2
348 settings.clip = Rect(2, 2);
349 settings.globalTransform = transform;
350
351 std::vector<renderengine::LayerSettings> layers;
352
353 renderengine::LayerSettings layerOne;
354 Rect rectOne(0, 0, 1, 1);
355 layerOne.geometry.boundaries = rectOne.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800356 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700357 layerOne.alpha = 1.0f;
358
359 renderengine::LayerSettings layerTwo;
360 Rect rectTwo(0, 1, 1, 2);
361 layerTwo.geometry.boundaries = rectTwo.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800362 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700363 layerTwo.alpha = 1.0f;
364
365 renderengine::LayerSettings layerThree;
366 Rect rectThree(1, 0, 2, 1);
367 layerThree.geometry.boundaries = rectThree.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800368 SourceVariant::fillColor(layerThree, 0.0f, 0.0f, 1.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700369 layerThree.alpha = 1.0f;
370
371 layers.push_back(layerOne);
372 layers.push_back(layerTwo);
373 layers.push_back(layerThree);
374
375 invokeDraw(settings, layers, mBuffer);
376}
377
378template <typename SourceVariant>
379void RenderEngineTest::fillBufferCheckersRotate0() {
380 fillBufferCheckers<SourceVariant>(mat4());
381 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 255, 0, 0,
382 255);
383 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
384 DEFAULT_DISPLAY_HEIGHT / 2),
385 0, 0, 255, 255);
386 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
387 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
388 0, 0, 0, 0);
389 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
390 DEFAULT_DISPLAY_HEIGHT),
391 0, 255, 0, 255);
392}
393
394template <typename SourceVariant>
395void RenderEngineTest::fillBufferCheckersRotate90() {
396 mat4 matrix = mat4(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 1);
397 fillBufferCheckers<SourceVariant>(matrix);
398 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 255, 0,
399 255);
400 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
401 DEFAULT_DISPLAY_HEIGHT / 2),
402 255, 0, 0, 255);
403 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
404 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
405 0, 0, 255, 255);
406 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
407 DEFAULT_DISPLAY_HEIGHT),
408 0, 0, 0, 0);
409}
410
411template <typename SourceVariant>
412void RenderEngineTest::fillBufferCheckersRotate180() {
413 mat4 matrix = mat4(-1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 2, 2, 0, 1);
414 fillBufferCheckers<SourceVariant>(matrix);
415 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0,
416 0);
417 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
418 DEFAULT_DISPLAY_HEIGHT / 2),
419 0, 255, 0, 255);
420 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
421 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
422 255, 0, 0, 255);
423 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
424 DEFAULT_DISPLAY_HEIGHT),
425 0, 0, 255, 255);
426}
427
428template <typename SourceVariant>
429void RenderEngineTest::fillBufferCheckersRotate270() {
430 mat4 matrix = mat4(0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 1);
431 fillBufferCheckers<SourceVariant>(matrix);
432 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 255,
433 255);
434 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
435 DEFAULT_DISPLAY_HEIGHT / 2),
436 0, 0, 0, 0);
437 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
438 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
439 0, 255, 0, 255);
440 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
441 DEFAULT_DISPLAY_HEIGHT),
442 255, 0, 0, 255);
443}
444
445template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800446void RenderEngineTest::fillBufferWithLayerTransform() {
Alec Mouri1089aed2018-10-25 21:33:57 -0700447 renderengine::DisplaySettings settings;
448 settings.physicalDisplay = fullscreenRect();
449 // Here logical space is 2x2
450 settings.clip = Rect(2, 2);
451
452 std::vector<renderengine::LayerSettings> layers;
453
454 renderengine::LayerSettings layer;
455 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
456 // Translate one pixel diagonally
457 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 -0800458 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700459 layer.source.solidColor = half3(1.0f, 0.0f, 0.0f);
460 layer.alpha = 1.0f;
461
462 layers.push_back(layer);
463
464 invokeDraw(settings, layers, mBuffer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800465}
Alec Mouri1089aed2018-10-25 21:33:57 -0700466
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800467template <typename SourceVariant>
468void RenderEngineTest::fillBufferLayerTransform() {
469 fillBufferWithLayerTransform<SourceVariant>();
Alec Mouri1089aed2018-10-25 21:33:57 -0700470 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0, 0);
471 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 0);
472 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
473 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
474 255, 0, 0, 255);
475}
476
477template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800478void RenderEngineTest::fillBufferWithColorTransform() {
Alec Mouri1089aed2018-10-25 21:33:57 -0700479 renderengine::DisplaySettings settings;
480 settings.physicalDisplay = fullscreenRect();
481 settings.clip = Rect(1, 1);
482
483 std::vector<renderengine::LayerSettings> layers;
484
485 renderengine::LayerSettings layer;
486 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800487 SourceVariant::fillColor(layer, 0.5f, 0.25f, 0.125f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700488 layer.alpha = 1.0f;
489
490 // construct a fake color matrix
491 // annihilate green and blue channels
492 settings.colorTransform = mat4::scale(vec4(1, 0, 0, 1));
493 // set red channel to red + green
494 layer.colorTransform = mat4(1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
495
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800496 layer.alpha = 1.0f;
497 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
498
Alec Mouri1089aed2018-10-25 21:33:57 -0700499 layers.push_back(layer);
500
501 invokeDraw(settings, layers, mBuffer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800502}
Alec Mouri1089aed2018-10-25 21:33:57 -0700503
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800504template <typename SourceVariant>
505void RenderEngineTest::fillBufferColorTransform() {
506 fillBufferWithColorTransform<SourceVariant>();
Alec Mouri1089aed2018-10-25 21:33:57 -0700507 expectBufferColor(fullscreenRect(), 191, 0, 0, 255);
508}
509
Alec Mouri7c94edb2018-12-03 21:23:26 -0800510template <typename SourceVariant>
511void RenderEngineTest::fillRedBufferWithRoundedCorners() {
512 renderengine::DisplaySettings settings;
513 settings.physicalDisplay = fullscreenRect();
514 settings.clip = fullscreenRect();
515
516 std::vector<renderengine::LayerSettings> layers;
517
518 renderengine::LayerSettings layer;
519 layer.geometry.boundaries = fullscreenRect().toFloatRect();
520 layer.geometry.roundedCornersRadius = 5.0f;
521 layer.geometry.roundedCornersCrop = fullscreenRect().toFloatRect();
522 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
523 layer.alpha = 1.0f;
524
525 layers.push_back(layer);
526
527 invokeDraw(settings, layers, mBuffer);
528}
529
530template <typename SourceVariant>
531void RenderEngineTest::fillBufferWithRoundedCorners() {
532 fillRedBufferWithRoundedCorners<SourceVariant>();
533 // Corners should be ignored...
534 expectBufferColor(Rect(0, 0, 1, 1), 0, 0, 0, 0);
535 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH - 1, 0, DEFAULT_DISPLAY_WIDTH, 1), 0, 0, 0, 0);
536 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT - 1, 1, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 0);
537 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH - 1, DEFAULT_DISPLAY_HEIGHT - 1,
538 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
539 0, 0, 0, 0);
540 // ...And the non-rounded portion should be red.
541 // Other pixels may be anti-aliased, so let's not check those.
542 expectBufferColor(Rect(5, 5, DEFAULT_DISPLAY_WIDTH - 5, DEFAULT_DISPLAY_HEIGHT - 5), 255, 0, 0,
543 255);
544}
545
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800546void RenderEngineTest::fillRedBufferTextureTransform() {
547 renderengine::DisplaySettings settings;
548 settings.physicalDisplay = fullscreenRect();
549 settings.clip = Rect(1, 1);
550
551 std::vector<renderengine::LayerSettings> layers;
552
553 renderengine::LayerSettings layer;
554 // Here will allocate a checker board texture, but transform texture
555 // coordinates so that only the upper left is applied.
556 sp<GraphicBuffer> buf = allocateSourceBuffer(2, 2);
557 uint32_t texName;
558 RenderEngineTest::sRE->genTextures(1, &texName);
559 this->mTexNames.push_back(texName);
560
561 uint8_t* pixels;
562 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
563 reinterpret_cast<void**>(&pixels));
564 // Red top left, Green top right, Blue bottom left, Black bottom right
565 pixels[0] = 255;
566 pixels[1] = 0;
567 pixels[2] = 0;
568 pixels[3] = 255;
569 pixels[4] = 0;
570 pixels[5] = 255;
571 pixels[6] = 0;
572 pixels[7] = 255;
573 pixels[8] = 0;
574 pixels[9] = 0;
575 pixels[10] = 255;
576 pixels[11] = 255;
577 buf->unlock();
578
579 layer.source.buffer.buffer = buf;
580 layer.source.buffer.textureName = texName;
581 // Transform coordinates to only be inside the red quadrant.
582 layer.source.buffer.textureTransform = mat4::scale(vec4(0.2, 0.2, 1, 1));
583 layer.alpha = 1.0f;
584 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
585
586 layers.push_back(layer);
587
588 invokeDraw(settings, layers, mBuffer);
589}
590
591void RenderEngineTest::fillBufferTextureTransform() {
592 fillRedBufferTextureTransform();
593 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
594}
595
596void RenderEngineTest::fillRedBufferWithPremultiplyAlpha() {
597 renderengine::DisplaySettings settings;
598 settings.physicalDisplay = fullscreenRect();
599 // Here logical space is 1x1
600 settings.clip = Rect(1, 1);
601
602 std::vector<renderengine::LayerSettings> layers;
603
604 renderengine::LayerSettings layer;
605 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
606 uint32_t texName;
607 RenderEngineTest::sRE->genTextures(1, &texName);
608 this->mTexNames.push_back(texName);
609
610 uint8_t* pixels;
611 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
612 reinterpret_cast<void**>(&pixels));
613 pixels[0] = 255;
614 pixels[1] = 0;
615 pixels[2] = 0;
616 pixels[3] = 255;
617 buf->unlock();
618
619 layer.source.buffer.buffer = buf;
620 layer.source.buffer.textureName = texName;
621 layer.source.buffer.usePremultipliedAlpha = true;
622 layer.alpha = 0.5f;
623 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
624
625 layers.push_back(layer);
626
627 invokeDraw(settings, layers, mBuffer);
628}
629
630void RenderEngineTest::fillBufferWithPremultiplyAlpha() {
631 fillRedBufferWithPremultiplyAlpha();
632 expectBufferColor(fullscreenRect(), 128, 0, 0, 128);
633}
634
635void RenderEngineTest::fillRedBufferWithoutPremultiplyAlpha() {
636 renderengine::DisplaySettings settings;
637 settings.physicalDisplay = fullscreenRect();
638 // Here logical space is 1x1
639 settings.clip = Rect(1, 1);
640
641 std::vector<renderengine::LayerSettings> layers;
642
643 renderengine::LayerSettings layer;
644 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
645 uint32_t texName;
646 RenderEngineTest::sRE->genTextures(1, &texName);
647 this->mTexNames.push_back(texName);
648
649 uint8_t* pixels;
650 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
651 reinterpret_cast<void**>(&pixels));
652 pixels[0] = 255;
653 pixels[1] = 0;
654 pixels[2] = 0;
655 pixels[3] = 255;
656 buf->unlock();
657
658 layer.source.buffer.buffer = buf;
659 layer.source.buffer.textureName = texName;
660 layer.source.buffer.usePremultipliedAlpha = false;
661 layer.alpha = 0.5f;
662 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
663
664 layers.push_back(layer);
665
666 invokeDraw(settings, layers, mBuffer);
667}
668
669void RenderEngineTest::fillBufferWithoutPremultiplyAlpha() {
670 fillRedBufferWithoutPremultiplyAlpha();
671 expectBufferColor(fullscreenRect(), 128, 0, 0, 64, 1);
672}
673
Alec Mouriac335532018-11-12 15:01:33 -0800674void RenderEngineTest::clearLeftRegion() {
675 renderengine::DisplaySettings settings;
676 settings.physicalDisplay = fullscreenRect();
677 // Here logical space is 4x4
678 settings.clip = Rect(4, 4);
679 settings.globalTransform = mat4::scale(vec4(2, 4, 0, 1));
680 settings.clearRegion = Region(Rect(1, 1));
681 std::vector<renderengine::LayerSettings> layers;
682 // dummy layer, without bounds should not render anything
683 renderengine::LayerSettings layer;
684 layers.push_back(layer);
685 invokeDraw(settings, layers, mBuffer);
686}
687
688void RenderEngineTest::fillBufferThenClearRegion() {
689 fillGreenBuffer<ColorSourceVariant>();
690 // Reuse mBuffer
691 clearLeftRegion();
692 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 255);
693 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
694 DEFAULT_DISPLAY_HEIGHT),
695 0, 255, 0, 255);
696}
697
Alec Mouri1089aed2018-10-25 21:33:57 -0700698TEST_F(RenderEngineTest, drawLayers_noLayersToDraw) {
699 drawEmptyLayers();
700}
701
702TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_colorSource) {
703 fillRedBuffer<ColorSourceVariant>();
704}
705
706TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_colorSource) {
707 fillGreenBuffer<ColorSourceVariant>();
708}
709
710TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_colorSource) {
711 fillBlueBuffer<ColorSourceVariant>();
712}
713
714TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_colorSource) {
715 fillRedTransparentBuffer<ColorSourceVariant>();
716}
717
718TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_colorSource) {
719 fillBufferPhysicalOffset<ColorSourceVariant>();
720}
721
722TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_colorSource) {
723 fillBufferCheckersRotate0<ColorSourceVariant>();
724}
725
726TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_colorSource) {
727 fillBufferCheckersRotate90<ColorSourceVariant>();
728}
729
730TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_colorSource) {
731 fillBufferCheckersRotate180<ColorSourceVariant>();
732}
733
734TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_colorSource) {
735 fillBufferCheckersRotate270<ColorSourceVariant>();
736}
737
738TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_colorSource) {
739 fillBufferLayerTransform<ColorSourceVariant>();
740}
741
742TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_colorSource) {
743 fillBufferLayerTransform<ColorSourceVariant>();
Alec Mouri6e57f682018-09-29 20:45:08 -0700744}
745
Alec Mouri7c94edb2018-12-03 21:23:26 -0800746TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_colorSource) {
747 fillBufferWithRoundedCorners<ColorSourceVariant>();
748}
749
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800750TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_opaqueBufferSource) {
751 fillRedBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
752}
753
754TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_opaqueBufferSource) {
755 fillGreenBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
756}
757
758TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_opaqueBufferSource) {
759 fillBlueBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
760}
761
762TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_opaqueBufferSource) {
763 fillRedTransparentBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
764}
765
766TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_opaqueBufferSource) {
767 fillBufferPhysicalOffset<BufferSourceVariant<ForceOpaqueBufferVariant>>();
768}
769
770TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_opaqueBufferSource) {
771 fillBufferCheckersRotate0<BufferSourceVariant<ForceOpaqueBufferVariant>>();
772}
773
774TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_opaqueBufferSource) {
775 fillBufferCheckersRotate90<BufferSourceVariant<ForceOpaqueBufferVariant>>();
776}
777
778TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_opaqueBufferSource) {
779 fillBufferCheckersRotate180<BufferSourceVariant<ForceOpaqueBufferVariant>>();
780}
781
782TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_opaqueBufferSource) {
783 fillBufferCheckersRotate270<BufferSourceVariant<ForceOpaqueBufferVariant>>();
784}
785
786TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_opaqueBufferSource) {
787 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
788}
789
790TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_opaqueBufferSource) {
791 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
792}
793
Alec Mouri7c94edb2018-12-03 21:23:26 -0800794TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_opaqueBufferSource) {
795 fillBufferWithRoundedCorners<BufferSourceVariant<ForceOpaqueBufferVariant>>();
796}
797
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800798TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_bufferSource) {
799 fillRedBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
800}
801
802TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_bufferSource) {
803 fillGreenBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
804}
805
806TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_bufferSource) {
807 fillBlueBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
808}
809
810TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_bufferSource) {
811 fillRedTransparentBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
812}
813
814TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_bufferSource) {
815 fillBufferPhysicalOffset<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
816}
817
818TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_bufferSource) {
819 fillBufferCheckersRotate0<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
820}
821
822TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_bufferSource) {
823 fillBufferCheckersRotate90<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
824}
825
826TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_bufferSource) {
827 fillBufferCheckersRotate180<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
828}
829
830TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_bufferSource) {
831 fillBufferCheckersRotate270<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
832}
833
834TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_bufferSource) {
835 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
836}
837
838TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_bufferSource) {
839 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
840}
841
Alec Mouri7c94edb2018-12-03 21:23:26 -0800842TEST_F(RenderEngineTest, drawLayers_fillBufferRoundedCorners_bufferSource) {
843 fillBufferWithRoundedCorners<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
844}
845
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800846TEST_F(RenderEngineTest, drawLayers_fillBufferTextureTransform) {
847 fillBufferTextureTransform();
848}
849
850TEST_F(RenderEngineTest, drawLayers_fillBuffer_premultipliesAlpha) {
851 fillBufferWithPremultiplyAlpha();
852}
853
854TEST_F(RenderEngineTest, drawLayers_fillBuffer_withoutPremultiplyingAlpha) {
855 fillBufferWithoutPremultiplyAlpha();
856}
857
Alec Mouriac335532018-11-12 15:01:33 -0800858TEST_F(RenderEngineTest, drawLayers_fillBufferThenClearRegion) {
859 fillBufferThenClearRegion();
860}
861
Alec Mouri6e57f682018-09-29 20:45:08 -0700862} // namespace android