xref: /aosp_15_r20/external/skia/tests/DebugLayerManagerTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2019 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkImage.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPicture.h"
13 #include "include/core/SkPictureRecorder.h"
14 #include "include/core/SkPixmap.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "tests/Test.h"
18 #include "tools/debugger/DebugLayerManager.h"
19 
20 #include <vector>
21 
22 // Adds one full update, one partial update, and requests one image a few frames later.
test_basic(skiatest::Reporter * reporter)23 static void test_basic(skiatest::Reporter* reporter) {
24   // prepare supporting objects
25   int layerWidth = 100;
26   int layerHeight = 100;
27 
28   // make a picture that fully updates the layer
29   SkPictureRecorder rec;
30   SkCanvas* canvas = rec.beginRecording(layerWidth, layerHeight);
31   canvas->clear(0x00000000);
32   SkPaint paint;
33   paint.setColor(SK_ColorBLUE);
34   canvas->drawOval(SkRect::MakeLTRB(1,1,99,99), paint);
35   auto picture1 = rec.finishRecordingAsPicture();
36   SkIRect dirtyRectFull = SkIRect::MakeLTRB(0, 0, layerWidth, layerHeight);
37 
38   // make a second picture that acts as a partial update.
39   SkPictureRecorder rec2;
40   canvas = rec2.beginRecording(layerWidth, layerHeight);
41   paint.setColor(SK_ColorGREEN);
42   canvas->drawOval(SkRect::MakeLTRB(40,40,60,60), paint);
43   auto picture2 = rec2.finishRecordingAsPicture();
44   SkIRect dirtyRectPartial = SkIRect::MakeLTRB(40,40,60,60);
45 
46   int node = 2;
47 
48   // create and exercise DebugLayerManager
49   DebugLayerManager dlm;
50   dlm.storeSkPicture(node, 0, picture1, dirtyRectFull);
51   dlm.storeSkPicture(node, 10, picture2, dirtyRectPartial);
52   auto frames = dlm.listFramesForNode(node);
53 
54   // Confirm the layer manager stored these at the right places.
55   REPORTER_ASSERT(reporter, frames.size() == 2);
56   REPORTER_ASSERT(reporter, frames[0] == 0);
57   REPORTER_ASSERT(reporter, frames[1] == 10);
58 
59   SkPixmap pixmap;
60   // request an image of the layer between the two updates.
61   for (int i=0; i<10; i++) {
62     auto image = dlm.getLayerAsImage(node, i);
63     REPORTER_ASSERT(reporter, image->width() == layerWidth);
64     REPORTER_ASSERT(reporter, image->height() == layerHeight);
65     // confirm center is blue, proving only first pic was drawn.
66     image->peekPixels(&pixmap);
67     SkColor paintColor = pixmap.getColor(50, 50);
68     REPORTER_ASSERT(reporter, paintColor == SK_ColorBLUE);
69   }
70 
71   // For any images after the second draw, confirm the center is green, but the area just outside
72   // that smaller circle is still blue, proving dlm drew both pictures.
73   for (int i=10; i<12; i++) {
74     auto image = dlm.getLayerAsImage(node, i);
75     REPORTER_ASSERT(reporter, image->width() == layerWidth);
76     REPORTER_ASSERT(reporter, image->height() == layerHeight);
77     image->peekPixels(&pixmap);
78     auto innerColor = pixmap.getColor(50, 50);
79     REPORTER_ASSERT(reporter, innerColor == SK_ColorGREEN);
80     auto outerColor = pixmap.getColor(10, 50);
81     REPORTER_ASSERT(reporter, outerColor == SK_ColorBLUE);
82   }
83 
84 
85 }
86 
DEF_TEST(DebugLayerManagerTest,reporter)87 DEF_TEST(DebugLayerManagerTest, reporter) {
88   test_basic(reporter);
89 }
90