OLD | NEW |
---|---|
(Empty) | |
1 | |
2 /* | |
3 * Copyright 2013 Google Inc. | |
4 * | |
5 * Use of this source code is governed by a BSD-style license that can be | |
6 * found in the LICENSE file. | |
7 */ | |
8 | |
9 #include "Test.h" | |
10 #include "SkCanvas.h" | |
11 #include "SkCanvasStateUtils.h" | |
12 #include "SkDevice.h" | |
13 #include "SkDrawFilter.h" | |
14 #include "SkPaint.h" | |
15 #include "SkRect.h" | |
16 #include "SkRRect.h" | |
17 | |
18 static void test_complex_layers(skiatest::Reporter* reporter) { | |
19 | |
20 const int WIDTH = 400; | |
21 const int HEIGHT = 400; | |
22 const int SPACER = 10; | |
23 | |
24 SkRect rect = SkRect::MakeXYWH(SPACER, SPACER, WIDTH-(2*SPACER), (HEIGHT-(2* SPACER)) / 7); | |
25 | |
26 const SkBitmap::Config configs[] = { SkBitmap::kRGB_565_Config, | |
27 SkBitmap::kARGB_8888_Config | |
28 }; | |
29 const int configCount = sizeof(configs) / sizeof(SkBitmap::Config); | |
scroggo
2013/08/29 17:24:07
Won't this cause a warning somewhere (size_t to in
| |
30 | |
31 const int layerAlpha[] = { 255, 255, 0 }; | |
32 const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag, | |
33 SkCanvas::kARGB_ClipLayer_SaveFlag, | |
34 SkCanvas::kARGB_NoClipLayer_SaveFlag | |
35 }; | |
36 REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags)); | |
37 const int layerCombinations = sizeof(layerAlpha) / sizeof(int); | |
scroggo
2013/08/29 17:24:07
Warning?
| |
38 | |
39 for (int i = 0; i < configCount; ++i) { | |
40 SkBitmap bitmaps[2]; | |
41 for (int j = 0; j < 2; ++j) { | |
42 bitmaps[j].setConfig(configs[i], WIDTH, HEIGHT); | |
43 bitmaps[j].allocPixels(); | |
44 | |
45 SkCanvas canvas(bitmaps[j]); | |
46 | |
47 canvas.drawColor(SK_ColorRED); | |
48 | |
49 for (int k = 0; k < layerCombinations; ++k) { | |
50 // draw a rect within the layer's bounds and again outside the l ayer's bounds | |
51 canvas.saveLayerAlpha(&rect, layerAlpha[k], flags[k]); | |
52 | |
53 SkCanvasState* state = NULL; | |
54 SkCanvas* tmpCanvas = NULL; | |
55 if (j) { | |
56 state = SkCanvasStateUtils::CaptureCanvasState(&canvas); | |
57 REPORTER_ASSERT(reporter, state); | |
58 tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state) ; | |
59 REPORTER_ASSERT(reporter, tmpCanvas); | |
60 } else { | |
61 tmpCanvas = SkRef(&canvas); | |
62 } | |
63 | |
64 SkPaint bluePaint; | |
65 bluePaint.setColor(SK_ColorBLUE); | |
66 bluePaint.setStyle(SkPaint::kFill_Style); | |
67 | |
68 tmpCanvas->drawRect(rect, bluePaint); | |
69 tmpCanvas->translate(0, rect.height() + SPACER); | |
70 tmpCanvas->drawRect(rect, bluePaint); | |
71 | |
72 tmpCanvas->unref(); | |
73 SkCanvasStateUtils::ReleaseCanvasState(state); | |
74 | |
75 canvas.restore(); | |
76 | |
77 // translate the canvas for the next iteration | |
78 canvas.translate(0, 2*(rect.height() + SPACER)); | |
79 } | |
80 } | |
81 | |
82 // now we memcmp the two bitmaps | |
83 REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize()); | |
84 REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(), | |
85 bitmaps[1].getPixels(), | |
86 bitmaps[0].getSize())); | |
87 } | |
88 } | |
89 | |
90 //////////////////////////////////////////////////////////////////////////////// | |
91 | |
92 class TestDrawFilter : public SkDrawFilter { | |
93 public: | |
94 virtual bool filter(SkPaint*, Type) SK_OVERRIDE { return true; } | |
95 }; | |
96 | |
97 static void test_draw_filters(skiatest::Reporter* reporter) { | |
98 TestDrawFilter drawFilter; | |
99 SkDevice device(SkBitmap::kARGB_8888_Config, 10, 10); | |
100 SkCanvas canvas(&device); | |
101 | |
102 canvas.setDrawFilter(&drawFilter); | |
103 | |
104 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); | |
105 REPORTER_ASSERT(reporter, state); | |
106 SkCanvas* tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state); | |
107 REPORTER_ASSERT(reporter, tmpCanvas); | |
108 | |
109 REPORTER_ASSERT(reporter, NULL != canvas.getDrawFilter()); | |
110 REPORTER_ASSERT(reporter, NULL == tmpCanvas->getDrawFilter()); | |
111 | |
112 tmpCanvas->unref(); | |
113 SkCanvasStateUtils::ReleaseCanvasState(state); | |
114 } | |
115 | |
116 //////////////////////////////////////////////////////////////////////////////// | |
117 | |
118 static void test_soft_clips(skiatest::Reporter* reporter) { | |
119 SkDevice device(SkBitmap::kARGB_8888_Config, 10, 10); | |
120 SkCanvas canvas(&device); | |
121 | |
122 SkRRect roundRect; | |
123 roundRect.setOval(SkRect::MakeWH(5, 5)); | |
124 | |
125 canvas.clipRRect(roundRect, SkRegion::kIntersect_Op, true); | |
126 | |
127 SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); | |
128 REPORTER_ASSERT(reporter, !state); | |
129 } | |
130 | |
131 //////////////////////////////////////////////////////////////////////////////// | |
132 | |
133 static void test_canvas_state_utils(skiatest::Reporter* reporter) { | |
134 test_complex_layers(reporter); | |
135 test_draw_filters(reporter); | |
136 test_soft_clips(reporter); | |
137 } | |
138 | |
139 #include "TestClassDef.h" | |
140 DEFINE_TESTCLASS("CanvasState", TestCanvasStateClass, test_canvas_state_utils) | |
OLD | NEW |