OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "../src/image/SkImagePriv.h" | 8 #include "../src/image/SkImagePriv.h" |
9 #include "../src/image/SkSurface_Base.h" | 9 #include "../src/image/SkSurface_Base.h" |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "SkBitmapDevice.h" | 11 #include "SkBitmapDevice.h" |
12 #include "SkBitmapProcShader.h" | 12 #include "SkBitmapProcShader.h" |
13 #include "SkDeferredCanvas.h" | 13 #include "SkDeferredCanvas.h" |
14 #include "SkGradientShader.h" | 14 #include "SkGradientShader.h" |
15 #include "SkShader.h" | 15 #include "SkShader.h" |
16 #include "SkSurface.h" | 16 #include "SkSurface.h" |
17 #include "Test.h" | 17 #include "Test.h" |
18 #include "sk_tool_utils.h" | 18 #include "sk_tool_utils.h" |
19 | 19 |
20 #if SK_SUPPORT_GPU | 20 #if SK_SUPPORT_GPU |
21 #include "GrContextFactory.h" | 21 #include "GrContextFactory.h" |
22 #else | 22 #else |
23 class GrContextFactory; | 23 class GrContextFactory; |
24 #endif | 24 #endif |
25 | 25 |
26 static const int gWidth = 2; | 26 static const int gWidth = 2; |
27 static const int gHeight = 2; | 27 static const int gHeight = 2; |
28 | 28 |
| 29 static void callWritePixels(SkCanvas* canvas, const SkBitmap& src, int x, int y, |
| 30 SkCanvas::Config8888 config) { |
| 31 SkBitmap bm(src); |
| 32 bm.lockPixels(); |
| 33 |
| 34 SkImageInfo info = bm.info(); |
| 35 sk_tool_utils::config8888_to_imagetypes(config, &info.fColorType, &info.fAlp
haType); |
| 36 |
| 37 if (src.isOpaque()) { |
| 38 info.fAlphaType = kOpaque_SkAlphaType; |
| 39 } |
| 40 |
| 41 canvas->writePixels(info, bm.getPixels(), bm.rowBytes(), x, y); |
| 42 } |
| 43 |
29 static void create(SkBitmap* bm, SkColor color) { | 44 static void create(SkBitmap* bm, SkColor color) { |
30 bm->allocN32Pixels(gWidth, gHeight); | 45 bm->allocN32Pixels(gWidth, gHeight); |
31 bm->eraseColor(color); | 46 bm->eraseColor(color); |
32 } | 47 } |
33 | 48 |
34 static SkSurface* createSurface(SkColor color) { | 49 static SkSurface* createSurface(SkColor color) { |
35 SkSurface* surface = SkSurface::NewRasterPMColor(gWidth, gHeight); | 50 SkSurface* surface = SkSurface::NewRasterPMColor(gWidth, gHeight); |
36 surface->getCanvas()->clear(color); | 51 surface->getCanvas()->clear(color); |
37 return surface; | 52 return surface; |
38 } | 53 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 115 |
101 int fDiscardCount, fRetainCount; | 116 int fDiscardCount, fRetainCount; |
102 SkBitmap fBitmap; | 117 SkBitmap fBitmap; |
103 }; | 118 }; |
104 | 119 |
105 static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter)
{ | 120 static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter)
{ |
106 SkAutoTUnref<MockSurface> surface(SkNEW_ARGS(MockSurface, (10, 10))); | 121 SkAutoTUnref<MockSurface> surface(SkNEW_ARGS(MockSurface, (10, 10))); |
107 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); | 122 SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get()
)); |
108 | 123 |
109 SkBitmap srcBitmap; | 124 SkBitmap srcBitmap; |
110 srcBitmap.allocPixels(SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kUnp
remul_SkAlphaType)); | 125 srcBitmap.allocN32Pixels(10, 10); |
111 srcBitmap.eraseColor(SK_ColorGREEN); | 126 srcBitmap.eraseColor(SK_ColorGREEN); |
112 // Tests below depend on this bitmap being recognized as opaque | 127 // Tests below depend on this bitmap being recognized as opaque |
113 | 128 |
114 // Preliminary sanity check: no copy on write if no active snapshot | 129 // Preliminary sanity check: no copy on write if no active snapshot |
115 surface->clearCounts(); | 130 surface->clearCounts(); |
116 canvas->clear(SK_ColorWHITE); | 131 canvas->clear(SK_ColorWHITE); |
117 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 132 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
118 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 133 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
119 | 134 |
120 surface->clearCounts(); | 135 surface->clearCounts(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 167 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
153 | 168 |
154 // Case 4: unpremultiplied opaque writePixels that entirely | 169 // Case 4: unpremultiplied opaque writePixels that entirely |
155 // covers the canvas | 170 // covers the canvas |
156 surface->clearCounts(); | 171 surface->clearCounts(); |
157 SkAutoTUnref<SkImage> image4(canvas->newImageSnapshot()); | 172 SkAutoTUnref<SkImage> image4(canvas->newImageSnapshot()); |
158 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 173 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
159 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 174 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
160 | 175 |
161 surface->clearCounts(); | 176 surface->clearCounts(); |
162 canvas->writePixels(srcBitmap, 0, 0); | 177 callWritePixels(canvas, srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888
); |
163 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); | 178 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); |
164 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 179 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
165 | 180 |
166 surface->clearCounts(); | 181 surface->clearCounts(); |
167 canvas->flush(); | 182 canvas->flush(); |
168 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 183 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
169 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 184 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
170 | 185 |
171 // Case 5: unpremultiplied opaque writePixels that partially | 186 // Case 5: unpremultiplied opaque writePixels that partially |
172 // covers the canvas | 187 // covers the canvas |
173 surface->clearCounts(); | 188 surface->clearCounts(); |
174 SkAutoTUnref<SkImage> image5(canvas->newImageSnapshot()); | 189 SkAutoTUnref<SkImage> image5(canvas->newImageSnapshot()); |
175 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 190 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
176 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 191 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
177 | 192 |
178 surface->clearCounts(); | 193 surface->clearCounts(); |
179 canvas->writePixels(srcBitmap, 5, 0); | 194 callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888
); |
180 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 195 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
181 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); | 196 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); |
182 | 197 |
183 surface->clearCounts(); | 198 surface->clearCounts(); |
184 canvas->flush(); | 199 canvas->flush(); |
185 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 200 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
186 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 201 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
187 | 202 |
188 // Case 6: unpremultiplied opaque writePixels that entirely | 203 // Case 6: unpremultiplied opaque writePixels that entirely |
189 // covers the canvas, preceded by clear | 204 // covers the canvas, preceded by clear |
190 surface->clearCounts(); | 205 surface->clearCounts(); |
191 SkAutoTUnref<SkImage> image6(canvas->newImageSnapshot()); | 206 SkAutoTUnref<SkImage> image6(canvas->newImageSnapshot()); |
192 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 207 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
193 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 208 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
194 | 209 |
195 surface->clearCounts(); | 210 surface->clearCounts(); |
196 canvas->clear(SK_ColorWHITE); | 211 canvas->clear(SK_ColorWHITE); |
197 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 212 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
198 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 213 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
199 | 214 |
200 surface->clearCounts(); | 215 surface->clearCounts(); |
201 canvas->writePixels(srcBitmap, 0, 0); | 216 callWritePixels(canvas, srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888
); |
202 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); | 217 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); |
203 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 218 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
204 | 219 |
205 surface->clearCounts(); | 220 surface->clearCounts(); |
206 canvas->flush(); | 221 canvas->flush(); |
207 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 222 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
208 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 223 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
209 | 224 |
210 // Case 7: unpremultiplied opaque writePixels that partially | 225 // Case 7: unpremultiplied opaque writePixels that partially |
211 // covers the canvas, preceeded by a clear | 226 // covers the canvas, preceeded by a clear |
212 surface->clearCounts(); | 227 surface->clearCounts(); |
213 SkAutoTUnref<SkImage> image7(canvas->newImageSnapshot()); | 228 SkAutoTUnref<SkImage> image7(canvas->newImageSnapshot()); |
214 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 229 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
215 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 230 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
216 | 231 |
217 surface->clearCounts(); | 232 surface->clearCounts(); |
218 canvas->clear(SK_ColorWHITE); | 233 canvas->clear(SK_ColorWHITE); |
219 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 234 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
220 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 235 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
221 | 236 |
222 surface->clearCounts(); | 237 surface->clearCounts(); |
223 canvas->writePixels(srcBitmap, 5, 0); | 238 callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888
); |
224 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); // because of the cl
ear | 239 REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); // because of the cl
ear |
225 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 240 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
226 | 241 |
227 surface->clearCounts(); | 242 surface->clearCounts(); |
228 canvas->flush(); | 243 canvas->flush(); |
229 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 244 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
230 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 245 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
231 | 246 |
232 // Case 8: unpremultiplied opaque writePixels that partially | 247 // Case 8: unpremultiplied opaque writePixels that partially |
233 // covers the canvas, preceeded by a drawREct that partially | 248 // covers the canvas, preceeded by a drawREct that partially |
234 // covers the canvas | 249 // covers the canvas |
235 surface->clearCounts(); | 250 surface->clearCounts(); |
236 SkAutoTUnref<SkImage> image8(canvas->newImageSnapshot()); | 251 SkAutoTUnref<SkImage> image8(canvas->newImageSnapshot()); |
237 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 252 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
238 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 253 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
239 | 254 |
240 surface->clearCounts(); | 255 surface->clearCounts(); |
241 SkPaint paint; | 256 SkPaint paint; |
242 canvas->drawRect(SkRect::MakeLTRB(0, 0, 5, 5), paint); | 257 canvas->drawRect(SkRect::MakeLTRB(0, 0, 5, 5), paint); |
243 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 258 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
244 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 259 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
245 | 260 |
246 surface->clearCounts(); | 261 surface->clearCounts(); |
247 canvas->writePixels(srcBitmap, 5, 0); | 262 callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888
); |
248 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 263 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
249 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); | 264 REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); |
250 | 265 |
251 surface->clearCounts(); | 266 surface->clearCounts(); |
252 canvas->flush(); | 267 canvas->flush(); |
253 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); | 268 REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); |
254 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); | 269 REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); |
255 } | 270 } |
256 | 271 |
257 static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) { | 272 static void TestDeferredCanvasFlush(skiatest::Reporter* reporter) { |
(...skipping 556 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
814 TestDeferredCanvasBitmapSizeThreshold(reporter); | 829 TestDeferredCanvasBitmapSizeThreshold(reporter); |
815 TestDeferredCanvasCreateCompatibleDevice(reporter); | 830 TestDeferredCanvasCreateCompatibleDevice(reporter); |
816 TestDeferredCanvasWritePixelsToSurface(reporter); | 831 TestDeferredCanvasWritePixelsToSurface(reporter); |
817 TestDeferredCanvasSurface(reporter, NULL); | 832 TestDeferredCanvasSurface(reporter, NULL); |
818 TestDeferredCanvasSetSurface(reporter, NULL); | 833 TestDeferredCanvasSetSurface(reporter, NULL); |
819 if (NULL != factory) { | 834 if (NULL != factory) { |
820 TestDeferredCanvasSurface(reporter, factory); | 835 TestDeferredCanvasSurface(reporter, factory); |
821 TestDeferredCanvasSetSurface(reporter, factory); | 836 TestDeferredCanvasSetSurface(reporter, factory); |
822 } | 837 } |
823 } | 838 } |
OLD | NEW |