blob: b6e4edbb093c03b52381ea2a7a630d97cb7835bb [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 Mouri0d5e1eb2018-11-10 20:40:12 -0800175 void fillRedBufferTextureTransform();
176
177 void fillBufferTextureTransform();
178
179 void fillRedBufferWithPremultiplyAlpha();
180
181 void fillBufferWithPremultiplyAlpha();
182
183 void fillRedBufferWithoutPremultiplyAlpha();
184
185 void fillBufferWithoutPremultiplyAlpha();
186
Alec Mouriac335532018-11-12 15:01:33 -0800187 void fillGreenColorBufferThenClearRegion();
188
189 void clearLeftRegion();
190
191 void fillBufferThenClearRegion();
192
Alec Mouri1089aed2018-10-25 21:33:57 -0700193 // Dumb hack to get aroud the fact that tear-down for renderengine isn't
194 // well defined right now, so we can't create multiple instances
195 static std::unique_ptr<renderengine::RenderEngine> sRE;
196
197 sp<GraphicBuffer> mBuffer;
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800198
199 std::vector<uint32_t> mTexNames;
Alec Mouri6e57f682018-09-29 20:45:08 -0700200};
201
Alec Mouri1089aed2018-10-25 21:33:57 -0700202std::unique_ptr<renderengine::RenderEngine> RenderEngineTest::sRE =
203 renderengine::RenderEngine::create(static_cast<int32_t>(ui::PixelFormat::RGBA_8888), 0);
204
205struct ColorSourceVariant {
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800206 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
207 RenderEngineTest* /*fixture*/) {
Alec Mouri1089aed2018-10-25 21:33:57 -0700208 layer.source.solidColor = half3(r, g, b);
209 }
210};
211
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800212struct RelaxOpaqueBufferVariant {
213 static void setOpaqueBit(renderengine::LayerSettings& layer) {
214 layer.source.buffer.isOpaque = false;
215 }
216
217 static uint8_t getAlphaChannel() { return 255; }
218};
219
220struct ForceOpaqueBufferVariant {
221 static void setOpaqueBit(renderengine::LayerSettings& layer) {
222 layer.source.buffer.isOpaque = true;
223 }
224
225 static uint8_t getAlphaChannel() {
226 // The isOpaque bit will override the alpha channel, so this should be
227 // arbitrary.
228 return 10;
229 }
230};
231
232template <typename OpaquenessVariant>
233struct BufferSourceVariant {
234 static void fillColor(renderengine::LayerSettings& layer, half r, half g, half b,
235 RenderEngineTest* fixture) {
236 sp<GraphicBuffer> buf = RenderEngineTest::allocateSourceBuffer(1, 1);
237 uint32_t texName;
238 RenderEngineTest::sRE->genTextures(1, &texName);
239 fixture->mTexNames.push_back(texName);
240
241 uint8_t* pixels;
242 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
243 reinterpret_cast<void**>(&pixels));
244
245 for (int32_t j = 0; j < buf->getHeight(); j++) {
246 uint8_t* iter = pixels + (buf->getStride() * j) * 4;
247 for (int32_t i = 0; i < buf->getWidth(); i++) {
248 iter[0] = uint8_t(r * 255);
249 iter[1] = uint8_t(g * 255);
250 iter[2] = uint8_t(b * 255);
251 iter[3] = OpaquenessVariant::getAlphaChannel();
252 iter += 4;
253 }
254 }
255
256 buf->unlock();
257
258 layer.source.buffer.buffer = buf;
259 layer.source.buffer.textureName = texName;
260 OpaquenessVariant::setOpaqueBit(layer);
261 }
262};
263
Alec Mouri1089aed2018-10-25 21:33:57 -0700264template <typename SourceVariant>
265void RenderEngineTest::fillBuffer(half r, half g, half b, half a) {
266 renderengine::DisplaySettings settings;
267 settings.physicalDisplay = fullscreenRect();
268 settings.clip = fullscreenRect();
269
270 std::vector<renderengine::LayerSettings> layers;
271
272 renderengine::LayerSettings layer;
273 layer.geometry.boundaries = fullscreenRect().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800274 SourceVariant::fillColor(layer, r, g, b, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700275 layer.alpha = a;
276
277 layers.push_back(layer);
278
279 invokeDraw(settings, layers, mBuffer);
280}
281
282template <typename SourceVariant>
283void RenderEngineTest::fillRedBuffer() {
284 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, 1.0f);
285 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
286}
287
288template <typename SourceVariant>
289void RenderEngineTest::fillGreenBuffer() {
290 fillBuffer<SourceVariant>(0.0f, 1.0f, 0.0f, 1.0f);
291 expectBufferColor(fullscreenRect(), 0, 255, 0, 255);
292}
293
294template <typename SourceVariant>
295void RenderEngineTest::fillBlueBuffer() {
296 fillBuffer<SourceVariant>(0.0f, 0.0f, 1.0f, 1.0f);
297 expectBufferColor(fullscreenRect(), 0, 0, 255, 255);
298}
299
300template <typename SourceVariant>
301void RenderEngineTest::fillRedTransparentBuffer() {
302 fillBuffer<SourceVariant>(1.0f, 0.0f, 0.0f, .2f);
303 expectBufferColor(fullscreenRect(), 51, 0, 0, 51);
304}
305
306template <typename SourceVariant>
307void RenderEngineTest::fillRedOffsetBuffer() {
308 renderengine::DisplaySettings settings;
309 settings.physicalDisplay = offsetRect();
310 settings.clip = offsetRectAtZero();
311
312 std::vector<renderengine::LayerSettings> layers;
313
314 renderengine::LayerSettings layer;
315 layer.geometry.boundaries = offsetRectAtZero().toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800316 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700317 layer.alpha = 1.0f;
318
319 layers.push_back(layer);
320 invokeDraw(settings, layers, mBuffer);
321}
322
323template <typename SourceVariant>
324void RenderEngineTest::fillBufferPhysicalOffset() {
325 fillRedOffsetBuffer<SourceVariant>();
326
327 expectBufferColor(Rect(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_WIDTH,
328 DEFAULT_DISPLAY_HEIGHT),
329 255, 0, 0, 255);
330 Rect offsetRegionLeft(DEFAULT_DISPLAY_OFFSET, DEFAULT_DISPLAY_HEIGHT);
331 Rect offsetRegionTop(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_OFFSET);
332
333 expectBufferColor(offsetRegionLeft, 0, 0, 0, 0);
334 expectBufferColor(offsetRegionTop, 0, 0, 0, 0);
335}
336
337template <typename SourceVariant>
338void RenderEngineTest::fillBufferCheckers(mat4 transform) {
339 renderengine::DisplaySettings settings;
340 settings.physicalDisplay = fullscreenRect();
341 // Here logical space is 2x2
342 settings.clip = Rect(2, 2);
343 settings.globalTransform = transform;
344
345 std::vector<renderengine::LayerSettings> layers;
346
347 renderengine::LayerSettings layerOne;
348 Rect rectOne(0, 0, 1, 1);
349 layerOne.geometry.boundaries = rectOne.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800350 SourceVariant::fillColor(layerOne, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700351 layerOne.alpha = 1.0f;
352
353 renderengine::LayerSettings layerTwo;
354 Rect rectTwo(0, 1, 1, 2);
355 layerTwo.geometry.boundaries = rectTwo.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800356 SourceVariant::fillColor(layerTwo, 0.0f, 1.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700357 layerTwo.alpha = 1.0f;
358
359 renderengine::LayerSettings layerThree;
360 Rect rectThree(1, 0, 2, 1);
361 layerThree.geometry.boundaries = rectThree.toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800362 SourceVariant::fillColor(layerThree, 0.0f, 0.0f, 1.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700363 layerThree.alpha = 1.0f;
364
365 layers.push_back(layerOne);
366 layers.push_back(layerTwo);
367 layers.push_back(layerThree);
368
369 invokeDraw(settings, layers, mBuffer);
370}
371
372template <typename SourceVariant>
373void RenderEngineTest::fillBufferCheckersRotate0() {
374 fillBufferCheckers<SourceVariant>(mat4());
375 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 255, 0, 0,
376 255);
377 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
378 DEFAULT_DISPLAY_HEIGHT / 2),
379 0, 0, 255, 255);
380 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
381 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
382 0, 0, 0, 0);
383 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
384 DEFAULT_DISPLAY_HEIGHT),
385 0, 255, 0, 255);
386}
387
388template <typename SourceVariant>
389void RenderEngineTest::fillBufferCheckersRotate90() {
390 mat4 matrix = mat4(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 1);
391 fillBufferCheckers<SourceVariant>(matrix);
392 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 255, 0,
393 255);
394 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
395 DEFAULT_DISPLAY_HEIGHT / 2),
396 255, 0, 0, 255);
397 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
398 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
399 0, 0, 255, 255);
400 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
401 DEFAULT_DISPLAY_HEIGHT),
402 0, 0, 0, 0);
403}
404
405template <typename SourceVariant>
406void RenderEngineTest::fillBufferCheckersRotate180() {
407 mat4 matrix = mat4(-1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 2, 2, 0, 1);
408 fillBufferCheckers<SourceVariant>(matrix);
409 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0,
410 0);
411 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
412 DEFAULT_DISPLAY_HEIGHT / 2),
413 0, 255, 0, 255);
414 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
415 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
416 255, 0, 0, 255);
417 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
418 DEFAULT_DISPLAY_HEIGHT),
419 0, 0, 255, 255);
420}
421
422template <typename SourceVariant>
423void RenderEngineTest::fillBufferCheckersRotate270() {
424 mat4 matrix = mat4(0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 1);
425 fillBufferCheckers<SourceVariant>(matrix);
426 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 255,
427 255);
428 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
429 DEFAULT_DISPLAY_HEIGHT / 2),
430 0, 0, 0, 0);
431 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
432 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
433 0, 255, 0, 255);
434 expectBufferColor(Rect(0, DEFAULT_DISPLAY_HEIGHT / 2, DEFAULT_DISPLAY_WIDTH / 2,
435 DEFAULT_DISPLAY_HEIGHT),
436 255, 0, 0, 255);
437}
438
439template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800440void RenderEngineTest::fillBufferWithLayerTransform() {
Alec Mouri1089aed2018-10-25 21:33:57 -0700441 renderengine::DisplaySettings settings;
442 settings.physicalDisplay = fullscreenRect();
443 // Here logical space is 2x2
444 settings.clip = Rect(2, 2);
445
446 std::vector<renderengine::LayerSettings> layers;
447
448 renderengine::LayerSettings layer;
449 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
450 // Translate one pixel diagonally
451 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 -0800452 SourceVariant::fillColor(layer, 1.0f, 0.0f, 0.0f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700453 layer.source.solidColor = half3(1.0f, 0.0f, 0.0f);
454 layer.alpha = 1.0f;
455
456 layers.push_back(layer);
457
458 invokeDraw(settings, layers, mBuffer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800459}
Alec Mouri1089aed2018-10-25 21:33:57 -0700460
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800461template <typename SourceVariant>
462void RenderEngineTest::fillBufferLayerTransform() {
463 fillBufferWithLayerTransform<SourceVariant>();
Alec Mouri1089aed2018-10-25 21:33:57 -0700464 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT / 2), 0, 0, 0, 0);
465 expectBufferColor(Rect(0, 0, DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 0);
466 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT / 2,
467 DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
468 255, 0, 0, 255);
469}
470
471template <typename SourceVariant>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800472void RenderEngineTest::fillBufferWithColorTransform() {
Alec Mouri1089aed2018-10-25 21:33:57 -0700473 renderengine::DisplaySettings settings;
474 settings.physicalDisplay = fullscreenRect();
475 settings.clip = Rect(1, 1);
476
477 std::vector<renderengine::LayerSettings> layers;
478
479 renderengine::LayerSettings layer;
480 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800481 SourceVariant::fillColor(layer, 0.5f, 0.25f, 0.125f, this);
Alec Mouri1089aed2018-10-25 21:33:57 -0700482 layer.alpha = 1.0f;
483
484 // construct a fake color matrix
485 // annihilate green and blue channels
486 settings.colorTransform = mat4::scale(vec4(1, 0, 0, 1));
487 // set red channel to red + green
488 layer.colorTransform = mat4(1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
489
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800490 layer.alpha = 1.0f;
491 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
492
Alec Mouri1089aed2018-10-25 21:33:57 -0700493 layers.push_back(layer);
494
495 invokeDraw(settings, layers, mBuffer);
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800496}
Alec Mouri1089aed2018-10-25 21:33:57 -0700497
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800498template <typename SourceVariant>
499void RenderEngineTest::fillBufferColorTransform() {
500 fillBufferWithColorTransform<SourceVariant>();
Alec Mouri1089aed2018-10-25 21:33:57 -0700501 expectBufferColor(fullscreenRect(), 191, 0, 0, 255);
502}
503
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800504void RenderEngineTest::fillRedBufferTextureTransform() {
505 renderengine::DisplaySettings settings;
506 settings.physicalDisplay = fullscreenRect();
507 settings.clip = Rect(1, 1);
508
509 std::vector<renderengine::LayerSettings> layers;
510
511 renderengine::LayerSettings layer;
512 // Here will allocate a checker board texture, but transform texture
513 // coordinates so that only the upper left is applied.
514 sp<GraphicBuffer> buf = allocateSourceBuffer(2, 2);
515 uint32_t texName;
516 RenderEngineTest::sRE->genTextures(1, &texName);
517 this->mTexNames.push_back(texName);
518
519 uint8_t* pixels;
520 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
521 reinterpret_cast<void**>(&pixels));
522 // Red top left, Green top right, Blue bottom left, Black bottom right
523 pixels[0] = 255;
524 pixels[1] = 0;
525 pixels[2] = 0;
526 pixels[3] = 255;
527 pixels[4] = 0;
528 pixels[5] = 255;
529 pixels[6] = 0;
530 pixels[7] = 255;
531 pixels[8] = 0;
532 pixels[9] = 0;
533 pixels[10] = 255;
534 pixels[11] = 255;
535 buf->unlock();
536
537 layer.source.buffer.buffer = buf;
538 layer.source.buffer.textureName = texName;
539 // Transform coordinates to only be inside the red quadrant.
540 layer.source.buffer.textureTransform = mat4::scale(vec4(0.2, 0.2, 1, 1));
541 layer.alpha = 1.0f;
542 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
543
544 layers.push_back(layer);
545
546 invokeDraw(settings, layers, mBuffer);
547}
548
549void RenderEngineTest::fillBufferTextureTransform() {
550 fillRedBufferTextureTransform();
551 expectBufferColor(fullscreenRect(), 255, 0, 0, 255);
552}
553
554void RenderEngineTest::fillRedBufferWithPremultiplyAlpha() {
555 renderengine::DisplaySettings settings;
556 settings.physicalDisplay = fullscreenRect();
557 // Here logical space is 1x1
558 settings.clip = Rect(1, 1);
559
560 std::vector<renderengine::LayerSettings> layers;
561
562 renderengine::LayerSettings layer;
563 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
564 uint32_t texName;
565 RenderEngineTest::sRE->genTextures(1, &texName);
566 this->mTexNames.push_back(texName);
567
568 uint8_t* pixels;
569 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
570 reinterpret_cast<void**>(&pixels));
571 pixels[0] = 255;
572 pixels[1] = 0;
573 pixels[2] = 0;
574 pixels[3] = 255;
575 buf->unlock();
576
577 layer.source.buffer.buffer = buf;
578 layer.source.buffer.textureName = texName;
579 layer.source.buffer.usePremultipliedAlpha = true;
580 layer.alpha = 0.5f;
581 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
582
583 layers.push_back(layer);
584
585 invokeDraw(settings, layers, mBuffer);
586}
587
588void RenderEngineTest::fillBufferWithPremultiplyAlpha() {
589 fillRedBufferWithPremultiplyAlpha();
590 expectBufferColor(fullscreenRect(), 128, 0, 0, 128);
591}
592
593void RenderEngineTest::fillRedBufferWithoutPremultiplyAlpha() {
594 renderengine::DisplaySettings settings;
595 settings.physicalDisplay = fullscreenRect();
596 // Here logical space is 1x1
597 settings.clip = Rect(1, 1);
598
599 std::vector<renderengine::LayerSettings> layers;
600
601 renderengine::LayerSettings layer;
602 sp<GraphicBuffer> buf = allocateSourceBuffer(1, 1);
603 uint32_t texName;
604 RenderEngineTest::sRE->genTextures(1, &texName);
605 this->mTexNames.push_back(texName);
606
607 uint8_t* pixels;
608 buf->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
609 reinterpret_cast<void**>(&pixels));
610 pixels[0] = 255;
611 pixels[1] = 0;
612 pixels[2] = 0;
613 pixels[3] = 255;
614 buf->unlock();
615
616 layer.source.buffer.buffer = buf;
617 layer.source.buffer.textureName = texName;
618 layer.source.buffer.usePremultipliedAlpha = false;
619 layer.alpha = 0.5f;
620 layer.geometry.boundaries = Rect(1, 1).toFloatRect();
621
622 layers.push_back(layer);
623
624 invokeDraw(settings, layers, mBuffer);
625}
626
627void RenderEngineTest::fillBufferWithoutPremultiplyAlpha() {
628 fillRedBufferWithoutPremultiplyAlpha();
629 expectBufferColor(fullscreenRect(), 128, 0, 0, 64, 1);
630}
631
Alec Mouriac335532018-11-12 15:01:33 -0800632void RenderEngineTest::clearLeftRegion() {
633 renderengine::DisplaySettings settings;
634 settings.physicalDisplay = fullscreenRect();
635 // Here logical space is 4x4
636 settings.clip = Rect(4, 4);
637 settings.globalTransform = mat4::scale(vec4(2, 4, 0, 1));
638 settings.clearRegion = Region(Rect(1, 1));
639 std::vector<renderengine::LayerSettings> layers;
640 // dummy layer, without bounds should not render anything
641 renderengine::LayerSettings layer;
642 layers.push_back(layer);
643 invokeDraw(settings, layers, mBuffer);
644}
645
646void RenderEngineTest::fillBufferThenClearRegion() {
647 fillGreenBuffer<ColorSourceVariant>();
648 // Reuse mBuffer
649 clearLeftRegion();
650 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, DEFAULT_DISPLAY_HEIGHT), 0, 0, 0, 255);
651 expectBufferColor(Rect(DEFAULT_DISPLAY_WIDTH / 2, 0, DEFAULT_DISPLAY_WIDTH,
652 DEFAULT_DISPLAY_HEIGHT),
653 0, 255, 0, 255);
654}
655
Alec Mouri1089aed2018-10-25 21:33:57 -0700656TEST_F(RenderEngineTest, drawLayers_noLayersToDraw) {
657 drawEmptyLayers();
658}
659
660TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_colorSource) {
661 fillRedBuffer<ColorSourceVariant>();
662}
663
664TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_colorSource) {
665 fillGreenBuffer<ColorSourceVariant>();
666}
667
668TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_colorSource) {
669 fillBlueBuffer<ColorSourceVariant>();
670}
671
672TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_colorSource) {
673 fillRedTransparentBuffer<ColorSourceVariant>();
674}
675
676TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_colorSource) {
677 fillBufferPhysicalOffset<ColorSourceVariant>();
678}
679
680TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_colorSource) {
681 fillBufferCheckersRotate0<ColorSourceVariant>();
682}
683
684TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_colorSource) {
685 fillBufferCheckersRotate90<ColorSourceVariant>();
686}
687
688TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_colorSource) {
689 fillBufferCheckersRotate180<ColorSourceVariant>();
690}
691
692TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_colorSource) {
693 fillBufferCheckersRotate270<ColorSourceVariant>();
694}
695
696TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_colorSource) {
697 fillBufferLayerTransform<ColorSourceVariant>();
698}
699
700TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_colorSource) {
701 fillBufferLayerTransform<ColorSourceVariant>();
Alec Mouri6e57f682018-09-29 20:45:08 -0700702}
703
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800704TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_opaqueBufferSource) {
705 fillRedBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
706}
707
708TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_opaqueBufferSource) {
709 fillGreenBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
710}
711
712TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_opaqueBufferSource) {
713 fillBlueBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
714}
715
716TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_opaqueBufferSource) {
717 fillRedTransparentBuffer<BufferSourceVariant<ForceOpaqueBufferVariant>>();
718}
719
720TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_opaqueBufferSource) {
721 fillBufferPhysicalOffset<BufferSourceVariant<ForceOpaqueBufferVariant>>();
722}
723
724TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_opaqueBufferSource) {
725 fillBufferCheckersRotate0<BufferSourceVariant<ForceOpaqueBufferVariant>>();
726}
727
728TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_opaqueBufferSource) {
729 fillBufferCheckersRotate90<BufferSourceVariant<ForceOpaqueBufferVariant>>();
730}
731
732TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_opaqueBufferSource) {
733 fillBufferCheckersRotate180<BufferSourceVariant<ForceOpaqueBufferVariant>>();
734}
735
736TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_opaqueBufferSource) {
737 fillBufferCheckersRotate270<BufferSourceVariant<ForceOpaqueBufferVariant>>();
738}
739
740TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_opaqueBufferSource) {
741 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
742}
743
744TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_opaqueBufferSource) {
745 fillBufferLayerTransform<BufferSourceVariant<ForceOpaqueBufferVariant>>();
746}
747
748TEST_F(RenderEngineTest, drawLayers_fillRedBuffer_bufferSource) {
749 fillRedBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
750}
751
752TEST_F(RenderEngineTest, drawLayers_fillGreenBuffer_bufferSource) {
753 fillGreenBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
754}
755
756TEST_F(RenderEngineTest, drawLayers_fillBlueBuffer_bufferSource) {
757 fillBlueBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
758}
759
760TEST_F(RenderEngineTest, drawLayers_fillRedTransparentBuffer_bufferSource) {
761 fillRedTransparentBuffer<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
762}
763
764TEST_F(RenderEngineTest, drawLayers_fillBufferPhysicalOffset_bufferSource) {
765 fillBufferPhysicalOffset<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
766}
767
768TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate0_bufferSource) {
769 fillBufferCheckersRotate0<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
770}
771
772TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate90_bufferSource) {
773 fillBufferCheckersRotate90<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
774}
775
776TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate180_bufferSource) {
777 fillBufferCheckersRotate180<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
778}
779
780TEST_F(RenderEngineTest, drawLayers_fillBufferCheckersRotate270_bufferSource) {
781 fillBufferCheckersRotate270<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
782}
783
784TEST_F(RenderEngineTest, drawLayers_fillBufferLayerTransform_bufferSource) {
785 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
786}
787
788TEST_F(RenderEngineTest, drawLayers_fillBufferColorTransform_bufferSource) {
789 fillBufferLayerTransform<BufferSourceVariant<RelaxOpaqueBufferVariant>>();
790}
791
792TEST_F(RenderEngineTest, drawLayers_fillBufferTextureTransform) {
793 fillBufferTextureTransform();
794}
795
796TEST_F(RenderEngineTest, drawLayers_fillBuffer_premultipliesAlpha) {
797 fillBufferWithPremultiplyAlpha();
798}
799
800TEST_F(RenderEngineTest, drawLayers_fillBuffer_withoutPremultiplyingAlpha) {
801 fillBufferWithoutPremultiplyAlpha();
802}
803
Alec Mouriac335532018-11-12 15:01:33 -0800804TEST_F(RenderEngineTest, drawLayers_fillBufferThenClearRegion) {
805 fillBufferThenClearRegion();
806}
807
Alec Mouri6e57f682018-09-29 20:45:08 -0700808} // namespace android