1 /*
2 * Copyright (C) 2016 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 <VectorDrawable.h>
18 #include <gtest/gtest.h>
19
20 #include <SkClipStack.h>
21 #include <SkSurface_Base.h>
22 #include <string.h>
23 #include "AnimationContext.h"
24 #include "DamageAccumulator.h"
25 #include "IContextFactory.h"
26 #include "hwui/Paint.h"
27 #include "SkiaCanvas.h"
28 #include "pipeline/skia/SkiaDisplayList.h"
29 #include "pipeline/skia/SkiaOpenGLPipeline.h"
30 #include "pipeline/skia/SkiaRecordingCanvas.h"
31 #include "pipeline/skia/SkiaUtils.h"
32 #include "renderthread/CanvasContext.h"
33 #include "tests/common/TestContext.h"
34 #include "tests/common/TestUtils.h"
35
36 #include <gui/BufferItemConsumer.h>
37 #include <gui/Surface.h>
38
39 using namespace android;
40 using namespace android::uirenderer;
41 using namespace android::uirenderer::renderthread;
42 using namespace android::uirenderer::skiapipeline;
43
RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline,renderFrame)44 RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, renderFrame) {
45 auto redNode = TestUtils::createSkiaNode(
46 0, 0, 1, 1, [](RenderProperties& props, SkiaRecordingCanvas& redCanvas) {
47 redCanvas.drawColor(SK_ColorRED, SkBlendMode::kSrcOver);
48 });
49 LayerUpdateQueue layerUpdateQueue;
50 SkRect dirty = SkRectMakeLargest();
51 std::vector<sp<RenderNode>> renderNodes;
52 renderNodes.push_back(redNode);
53 bool opaque = true;
54 android::uirenderer::Rect contentDrawBounds(0, 0, 1, 1);
55 auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread);
56 auto surface = SkSurface::MakeRasterN32Premul(1, 1);
57 surface->getCanvas()->drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver);
58 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE);
59 pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
60 SkMatrix::I());
61 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorRED);
62 }
63
RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline,renderFrameCheckOpaque)64 RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, renderFrameCheckOpaque) {
65 auto halfGreenNode = TestUtils::createSkiaNode(
66 0, 0, 2, 2, [](RenderProperties& props, SkiaRecordingCanvas& bottomHalfGreenCanvas) {
67 Paint greenPaint;
68 greenPaint.setColor(SK_ColorGREEN);
69 greenPaint.setStyle(SkPaint::kFill_Style);
70 bottomHalfGreenCanvas.drawRect(0, 1, 2, 2, greenPaint);
71 });
72 LayerUpdateQueue layerUpdateQueue;
73 SkRect dirty = SkRectMakeLargest();
74 std::vector<sp<RenderNode>> renderNodes;
75 renderNodes.push_back(halfGreenNode);
76 android::uirenderer::Rect contentDrawBounds(0, 0, 2, 2);
77 auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread);
78 auto surface = SkSurface::MakeRasterN32Premul(2, 2);
79 surface->getCanvas()->drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver);
80 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE);
81 pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, true, contentDrawBounds, surface,
82 SkMatrix::I());
83 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE);
84 ASSERT_EQ(TestUtils::getColor(surface, 0, 1), SK_ColorGREEN);
85 pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, false, contentDrawBounds, surface,
86 SkMatrix::I());
87 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned int)SK_ColorTRANSPARENT);
88 ASSERT_EQ(TestUtils::getColor(surface, 0, 1), SK_ColorGREEN);
89 }
90
RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline,renderFrameCheckDirtyRect)91 RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, renderFrameCheckDirtyRect) {
92 auto redNode = TestUtils::createSkiaNode(
93 0, 0, 2, 2, [](RenderProperties& props, SkiaRecordingCanvas& redCanvas) {
94 redCanvas.drawColor(SK_ColorRED, SkBlendMode::kSrcOver);
95 });
96 LayerUpdateQueue layerUpdateQueue;
97 SkRect dirty = SkRect::MakeXYWH(0, 1, 2, 1);
98 std::vector<sp<RenderNode>> renderNodes;
99 renderNodes.push_back(redNode);
100 android::uirenderer::Rect contentDrawBounds(0, 0, 2, 2);
101 auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread);
102 auto surface = SkSurface::MakeRasterN32Premul(2, 2);
103 surface->getCanvas()->drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver);
104 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE);
105 pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, true, contentDrawBounds, surface,
106 SkMatrix::I());
107 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE);
108 ASSERT_EQ(TestUtils::getColor(surface, 1, 0), SK_ColorBLUE);
109 ASSERT_EQ(TestUtils::getColor(surface, 0, 1), SK_ColorRED);
110 ASSERT_EQ(TestUtils::getColor(surface, 1, 1), SK_ColorRED);
111 }
112
RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline,renderLayer)113 RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, renderLayer) {
114 auto redNode = TestUtils::createSkiaNode(
115 0, 0, 1, 1, [](RenderProperties& props, SkiaRecordingCanvas& redCanvas) {
116 redCanvas.drawColor(SK_ColorRED, SkBlendMode::kSrcOver);
117 });
118 auto surfaceLayer1 = SkSurface::MakeRasterN32Premul(1, 1);
119 surfaceLayer1->getCanvas()->drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver);
120 ASSERT_EQ(TestUtils::getColor(surfaceLayer1, 0, 0), SK_ColorWHITE);
121 redNode->setLayerSurface(surfaceLayer1);
122
123 // create a 2nd 2x2 layer and add it to the queue as well.
124 // make the layer's dirty area one half of the layer and verify only the dirty half is updated.
125 auto blueNode = TestUtils::createSkiaNode(
126 0, 0, 2, 2, [](RenderProperties& props, SkiaRecordingCanvas& blueCanvas) {
127 blueCanvas.drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver);
128 });
129 auto surfaceLayer2 = SkSurface::MakeRasterN32Premul(2, 2);
130 surfaceLayer2->getCanvas()->drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver);
131 ASSERT_EQ(TestUtils::getColor(surfaceLayer2, 0, 0), SK_ColorWHITE);
132 blueNode->setLayerSurface(surfaceLayer2);
133
134 // attach both layers to the update queue
135 LayerUpdateQueue layerUpdateQueue;
136 SkRect dirty = SkRectMakeLargest();
137 layerUpdateQueue.enqueueLayerWithDamage(redNode.get(), dirty);
138 layerUpdateQueue.enqueueLayerWithDamage(blueNode.get(), SkRect::MakeWH(2, 1));
139 ASSERT_EQ(layerUpdateQueue.entries().size(), 2UL);
140
141 bool opaque = true;
142 LightGeometry lightGeometry;
143 lightGeometry.radius = 1.0f;
144 lightGeometry.center = {0.0f, 0.0f, 0.0f};
145 LightInfo lightInfo;
146 auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread);
147 pipeline->renderLayers(lightGeometry, &layerUpdateQueue, opaque, lightInfo);
148 ASSERT_EQ(TestUtils::getColor(surfaceLayer1, 0, 0), SK_ColorRED);
149 ASSERT_EQ(TestUtils::getColor(surfaceLayer2, 0, 0), SK_ColorBLUE);
150 ASSERT_EQ(TestUtils::getColor(surfaceLayer2, 0, 1), SK_ColorWHITE);
151 ASSERT_TRUE(layerUpdateQueue.entries().empty());
152 redNode->setLayerSurface(sk_sp<SkSurface>());
153 blueNode->setLayerSurface(sk_sp<SkSurface>());
154 }
155
RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline,renderOverdraw)156 RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, renderOverdraw) {
157 ScopedProperty<bool> prop(Properties::debugOverdraw, true);
158
159 auto whiteNode = TestUtils::createSkiaNode(
160 0, 0, 1, 1, [](RenderProperties& props, SkiaRecordingCanvas& canvas) {
161 canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver);
162 });
163 LayerUpdateQueue layerUpdateQueue;
164 SkRect dirty = SkRect::MakeXYWH(0, 0, 1, 1);
165 std::vector<sp<RenderNode>> renderNodes;
166 renderNodes.push_back(whiteNode);
167 bool opaque = true;
168 // empty contentDrawBounds is avoiding backdrop/content logic, which would lead to less overdraw
169 android::uirenderer::Rect contentDrawBounds(0, 0, 0, 0);
170 auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread);
171 auto surface = SkSurface::MakeRasterN32Premul(1, 1);
172
173 // Initialize the canvas to blue.
174 surface->getCanvas()->drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver);
175 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE);
176
177 // Single draw, should be white.
178 pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
179 SkMatrix::I());
180 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorWHITE);
181
182 // 1 Overdraw, should be blue blended onto white.
183 renderNodes.push_back(whiteNode);
184 pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
185 SkMatrix::I());
186 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffd0d0ff);
187
188 // 2 Overdraw, should be green blended onto white
189 renderNodes.push_back(whiteNode);
190 pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
191 SkMatrix::I());
192 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffd0ffd0);
193
194 // 3 Overdraw, should be pink blended onto white.
195 renderNodes.push_back(whiteNode);
196 pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
197 SkMatrix::I());
198 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffffc0c0);
199
200 // 4 Overdraw, should be red blended onto white.
201 renderNodes.push_back(whiteNode);
202 pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
203 SkMatrix::I());
204 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffff8080);
205
206 // 5 Overdraw, should be red blended onto white.
207 renderNodes.push_back(whiteNode);
208 pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
209 SkMatrix::I());
210 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffff8080);
211 }
212
213 namespace {
214 template <typename T>
215 class DeferLayer : public SkSurface_Base {
216 public:
DeferLayer()217 DeferLayer() : SkSurface_Base(T().imageInfo(), nullptr) {}
~DeferLayer()218 virtual ~DeferLayer() {}
219
onNewCanvas()220 SkCanvas* onNewCanvas() override { return new T(); }
onNewSurface(const SkImageInfo &)221 sk_sp<SkSurface> onNewSurface(const SkImageInfo&) override { return nullptr; }
onNewImageSnapshot(const SkIRect * bounds)222 sk_sp<SkImage> onNewImageSnapshot(const SkIRect* bounds) override { return nullptr; }
canvas()223 T* canvas() { return static_cast<T*>(getCanvas()); }
onCopyOnWrite(ContentChangeMode)224 void onCopyOnWrite(ContentChangeMode) override {}
onWritePixels(const SkPixmap &,int x,int y)225 void onWritePixels(const SkPixmap&, int x, int y) override {}
226 };
227 }
228
RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline,deferRenderNodeScene)229 RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, deferRenderNodeScene) {
230 class DeferTestCanvas : public SkCanvas {
231 public:
232 DeferTestCanvas() : SkCanvas(800, 600) {}
233 void onDrawRect(const SkRect& rect, const SkPaint& paint) override {
234 SkMatrix expected;
235 switch (mDrawCounter++) {
236 case 0:
237 // background - left side
238 EXPECT_EQ(SkRect::MakeLTRB(600, 100, 700, 500), TestUtils::getClipBounds(this));
239 expected.setTranslate(100, 100);
240 break;
241 case 1:
242 // background - top side
243 EXPECT_EQ(SkRect::MakeLTRB(100, 400, 600, 500), TestUtils::getClipBounds(this));
244 expected.setTranslate(100, 100);
245 break;
246 case 2:
247 // content
248 EXPECT_EQ(SkRect::MakeLTRB(100, 100, 700, 500), TestUtils::getClipBounds(this));
249 expected.setTranslate(-50, -50);
250 break;
251 case 3:
252 // overlay
253 EXPECT_EQ(SkRect::MakeLTRB(0, 0, 800, 600), TestUtils::getClipBounds(this));
254 expected.reset();
255 break;
256 default:
257 ADD_FAILURE() << "Too many rects observed";
258 }
259 EXPECT_EQ(expected, getTotalMatrix());
260 }
261 int mDrawCounter = 0;
262 };
263
264 std::vector<sp<RenderNode>> nodes;
265 Paint transparentPaint;
266 transparentPaint.setAlpha(128);
267
268 // backdrop
269 nodes.push_back(TestUtils::createSkiaNode(
270 100, 100, 700, 500, // 600x400
271 [&transparentPaint](RenderProperties& props, SkiaRecordingCanvas& canvas) {
272 canvas.drawRect(0, 0, 600, 400, transparentPaint);
273 }));
274
275 // content
276 android::uirenderer::Rect contentDrawBounds(150, 150, 650, 450); // 500x300
277 nodes.push_back(TestUtils::createSkiaNode(
278 0, 0, 800, 600,
279 [&transparentPaint](RenderProperties& props, SkiaRecordingCanvas& canvas) {
280 canvas.drawRect(0, 0, 800, 600, transparentPaint);
281 }));
282
283 // overlay
284 nodes.push_back(TestUtils::createSkiaNode(
285 0, 0, 800, 600,
286 [&transparentPaint](RenderProperties& props, SkiaRecordingCanvas& canvas) {
287 canvas.drawRect(0, 0, 800, 200, transparentPaint);
288 }));
289
290 LayerUpdateQueue layerUpdateQueue;
291 SkRect dirty = SkRect::MakeWH(800, 600);
292 auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread);
293 sk_sp<DeferLayer<DeferTestCanvas>> surface(new DeferLayer<DeferTestCanvas>());
294 pipeline->renderFrame(layerUpdateQueue, dirty, nodes, true, contentDrawBounds, surface,
295 SkMatrix::I());
296 EXPECT_EQ(4, surface->canvas()->mDrawCounter);
297 }
298
RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline,clipped)299 RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, clipped) {
300 static const int CANVAS_WIDTH = 200;
301 static const int CANVAS_HEIGHT = 200;
302 class ClippedTestCanvas : public SkCanvas {
303 public:
304 ClippedTestCanvas() : SkCanvas(CANVAS_WIDTH, CANVAS_HEIGHT) {}
305 void onDrawImage(const SkImage*, SkScalar dx, SkScalar dy, const SkPaint*) override {
306 EXPECT_EQ(0, mDrawCounter++);
307 EXPECT_EQ(SkRect::MakeLTRB(10, 20, 30, 40), TestUtils::getClipBounds(this));
308 EXPECT_TRUE(getTotalMatrix().isIdentity());
309 }
310 int mDrawCounter = 0;
311 };
312
313 std::vector<sp<RenderNode>> nodes;
314 nodes.push_back(TestUtils::createSkiaNode(
315 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT,
316 [](RenderProperties& props, SkiaRecordingCanvas& canvas) {
317 sk_sp<Bitmap> bitmap(TestUtils::createBitmap(CANVAS_WIDTH, CANVAS_HEIGHT));
318 canvas.drawBitmap(*bitmap, 0, 0, nullptr);
319 }));
320
321 LayerUpdateQueue layerUpdateQueue;
322 SkRect dirty = SkRect::MakeLTRB(10, 20, 30, 40);
323 auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread);
324 sk_sp<DeferLayer<ClippedTestCanvas>> surface(new DeferLayer<ClippedTestCanvas>());
325 pipeline->renderFrame(layerUpdateQueue, dirty, nodes, true,
326 SkRect::MakeWH(CANVAS_WIDTH, CANVAS_HEIGHT), surface, SkMatrix::I());
327 EXPECT_EQ(1, surface->canvas()->mDrawCounter);
328 }
329
330 // Test renderFrame with a dirty clip and a pre-transform matrix.
RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline,clipped_rotated)331 RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, clipped_rotated) {
332 static const int CANVAS_WIDTH = 200;
333 static const int CANVAS_HEIGHT = 100;
334 static const SkMatrix rotateMatrix = SkMatrix::MakeAll(0, -1, CANVAS_HEIGHT, 1, 0, 0, 0, 0, 1);
335 static const SkRect dirty = SkRect::MakeLTRB(10, 20, 20, 40);
336 class ClippedTestCanvas : public SkCanvas {
337 public:
338 ClippedTestCanvas() : SkCanvas(CANVAS_WIDTH, CANVAS_HEIGHT) {}
339 void onDrawImage(const SkImage*, SkScalar dx, SkScalar dy, const SkPaint*) override {
340 EXPECT_EQ(0, mDrawCounter++);
341 // Expect clip to be rotated.
342 EXPECT_EQ(SkRect::MakeLTRB(CANVAS_HEIGHT - dirty.fTop - dirty.height(), dirty.fLeft,
343 CANVAS_HEIGHT - dirty.fTop, dirty.fLeft + dirty.width()),
344 TestUtils::getClipBounds(this));
345 EXPECT_EQ(rotateMatrix, getTotalMatrix());
346 }
347 int mDrawCounter = 0;
348 };
349
350 std::vector<sp<RenderNode>> nodes;
351 nodes.push_back(TestUtils::createSkiaNode(
352 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT,
353 [](RenderProperties& props, SkiaRecordingCanvas& canvas) {
354 sk_sp<Bitmap> bitmap(TestUtils::createBitmap(CANVAS_WIDTH, CANVAS_HEIGHT));
355 canvas.drawBitmap(*bitmap, 0, 0, nullptr);
356 }));
357
358 LayerUpdateQueue layerUpdateQueue;
359 auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread);
360 sk_sp<DeferLayer<ClippedTestCanvas>> surface(new DeferLayer<ClippedTestCanvas>());
361 pipeline->renderFrame(layerUpdateQueue, dirty, nodes, true,
362 SkRect::MakeWH(CANVAS_WIDTH, CANVAS_HEIGHT), surface, rotateMatrix);
363 EXPECT_EQ(1, surface->canvas()->mDrawCounter);
364 }
365
RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline,clip_replace)366 RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, clip_replace) {
367 static const int CANVAS_WIDTH = 50;
368 static const int CANVAS_HEIGHT = 50;
369 class ClipReplaceTestCanvas : public SkCanvas {
370 public:
371 ClipReplaceTestCanvas() : SkCanvas(CANVAS_WIDTH, CANVAS_HEIGHT) {}
372 void onDrawPaint(const SkPaint&) {
373 EXPECT_EQ(0, mDrawCounter++);
374 EXPECT_EQ(SkRect::MakeLTRB(20, 10, 30, 40), TestUtils::getClipBounds(this))
375 << "Expect resolved clip to be intersection of viewport clip and clip op";
376 }
377 int mDrawCounter = 0;
378 };
379
380 std::vector<sp<RenderNode>> nodes;
381 nodes.push_back(TestUtils::createSkiaNode(
382 20, 20, 30, 30, [](RenderProperties& props, SkiaRecordingCanvas& canvas) {
383 canvas.clipRect(0, -20, 10, 30, SkClipOp::kReplace_deprecated);
384 canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver);
385 }));
386
387 LayerUpdateQueue layerUpdateQueue;
388 SkRect dirty = SkRect::MakeLTRB(10, 10, 40, 40);
389 auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread);
390 sk_sp<DeferLayer<ClipReplaceTestCanvas>> surface(new DeferLayer<ClipReplaceTestCanvas>());
391 pipeline->renderFrame(layerUpdateQueue, dirty, nodes, true,
392 SkRect::MakeWH(CANVAS_WIDTH, CANVAS_HEIGHT), surface, SkMatrix::I());
393 EXPECT_EQ(1, surface->canvas()->mDrawCounter);
394 }
395
RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline,context_lost)396 RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, context_lost) {
397 test::TestContext context;
398 auto surface = context.surface();
399 auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread);
400 EXPECT_FALSE(pipeline->isSurfaceReady());
401 EXPECT_TRUE(pipeline->setSurface(surface.get(), SwapBehavior::kSwap_default));
402 EXPECT_TRUE(pipeline->isSurfaceReady());
403 renderThread.destroyRenderingContext();
404 EXPECT_FALSE(pipeline->isSurfaceReady());
405 }
406
RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline,pictureCallback)407 RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, pictureCallback) {
408 // create a pipeline and add a picture callback
409 auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread);
410 int callbackCount = 0;
411 pipeline->setPictureCapturedCallback(
412 [&callbackCount](sk_sp<SkPicture>&& picture) { callbackCount += 1; });
413
414 // create basic red frame and render it
415 auto redNode = TestUtils::createSkiaNode(
416 0, 0, 1, 1, [](RenderProperties& props, SkiaRecordingCanvas& redCanvas) {
417 redCanvas.drawColor(SK_ColorRED, SkBlendMode::kSrcOver);
418 });
419 LayerUpdateQueue layerUpdateQueue;
420 SkRect dirty = SkRectMakeLargest();
421 std::vector<sp<RenderNode>> renderNodes;
422 renderNodes.push_back(redNode);
423 bool opaque = true;
424 android::uirenderer::Rect contentDrawBounds(0, 0, 1, 1);
425 auto surface = SkSurface::MakeRasterN32Premul(1, 1);
426 pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
427 SkMatrix::I());
428
429 // verify the callback was called
430 EXPECT_EQ(1, callbackCount);
431
432 // render a second frame and check the callback count
433 pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
434 SkMatrix::I());
435 EXPECT_EQ(2, callbackCount);
436
437 // unset the callback, render another frame, check callback was not invoked
438 pipeline->setPictureCapturedCallback(nullptr);
439 pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
440 SkMatrix::I());
441 EXPECT_EQ(2, callbackCount);
442 }
443