OLD | NEW |
1 | |
2 /* | 1 /* |
3 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
4 * | 3 * |
5 * 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 |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
8 | 7 |
9 #include "Test.h" | 8 #include "Test.h" |
10 #include "SkBitmapDevice.h" | 9 #include "SkBitmapDevice.h" |
11 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
12 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
13 #include "SkMathPriv.h" | 12 #include "SkMathPriv.h" |
14 #include "SkRegion.h" | 13 #include "SkRegion.h" |
15 #if SK_SUPPORT_GPU | 14 #if SK_SUPPORT_GPU |
16 #include "SkGpuDevice.h" | 15 #include "SkGpuDevice.h" |
17 #include "GrContextFactory.h" | 16 #include "GrContextFactory.h" |
18 #else | 17 #else |
19 class GrContext; | 18 class GrContext; |
20 class GrContextFactory; | 19 class GrContextFactory; |
21 #endif | 20 #endif |
22 | 21 |
23 static const int DEV_W = 100, DEV_H = 100; | 22 static const int DEV_W = 100, DEV_H = 100; |
24 static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); | 23 static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
25 static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1, | 24 static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1, |
26 DEV_H * SK_Scalar1); | 25 DEV_H * SK_Scalar1); |
27 static const U8CPU DEV_PAD = 0xee; | 26 static const U8CPU DEV_PAD = 0xee; |
28 | 27 |
29 namespace { | 28 static SkPMColor getCanvasColor(int x, int y) { |
30 SkPMColor getCanvasColor(int x, int y) { | |
31 SkASSERT(x >= 0 && x < DEV_W); | 29 SkASSERT(x >= 0 && x < DEV_W); |
32 SkASSERT(y >= 0 && y < DEV_H); | 30 SkASSERT(y >= 0 && y < DEV_H); |
33 | 31 |
34 U8CPU r = x; | 32 U8CPU r = x; |
35 U8CPU g = y; | 33 U8CPU g = y; |
36 U8CPU b = 0xc; | 34 U8CPU b = 0xc; |
37 | 35 |
38 U8CPU a = 0x0; | 36 U8CPU a = 0x0; |
39 switch ((x+y) % 5) { | 37 switch ((x+y) % 5) { |
40 case 0: | 38 case 0: |
41 a = 0xff; | 39 a = 0xff; |
42 break; | 40 break; |
43 case 1: | 41 case 1: |
44 a = 0x80; | 42 a = 0x80; |
45 break; | 43 break; |
46 case 2: | 44 case 2: |
47 a = 0xCC; | 45 a = 0xCC; |
48 break; | 46 break; |
49 case 3: | 47 case 3: |
50 a = 0x00; | 48 a = 0x00; |
51 break; | 49 break; |
52 case 4: | 50 case 4: |
53 a = 0x01; | 51 a = 0x01; |
54 break; | 52 break; |
55 } | 53 } |
56 return SkPremultiplyARGBInline(a, r, g, b); | 54 return SkPremultiplyARGBInline(a, r, g, b); |
57 } | 55 } |
58 | 56 |
59 bool config8888IsPremul(SkCanvas::Config8888 config8888) { | 57 static bool config8888IsPremul(SkCanvas::Config8888 config8888) { |
60 switch (config8888) { | 58 switch (config8888) { |
61 case SkCanvas::kNative_Premul_Config8888: | 59 case SkCanvas::kNative_Premul_Config8888: |
62 case SkCanvas::kBGRA_Premul_Config8888: | 60 case SkCanvas::kBGRA_Premul_Config8888: |
63 case SkCanvas::kRGBA_Premul_Config8888: | 61 case SkCanvas::kRGBA_Premul_Config8888: |
64 return true; | 62 return true; |
65 case SkCanvas::kNative_Unpremul_Config8888: | 63 case SkCanvas::kNative_Unpremul_Config8888: |
66 case SkCanvas::kBGRA_Unpremul_Config8888: | 64 case SkCanvas::kBGRA_Unpremul_Config8888: |
67 case SkCanvas::kRGBA_Unpremul_Config8888: | 65 case SkCanvas::kRGBA_Unpremul_Config8888: |
68 return false; | 66 return false; |
69 default: | 67 default: |
70 SkASSERT(0); | 68 SkASSERT(0); |
71 return false; | 69 return false; |
72 } | 70 } |
73 } | 71 } |
74 | 72 |
75 // assumes any premu/.unpremul has been applied | 73 // assumes any premu/.unpremul has been applied |
76 uint32_t packConfig8888(SkCanvas::Config8888 config8888, | 74 static uint32_t packConfig8888(SkCanvas::Config8888 config8888, |
77 U8CPU a, U8CPU r, U8CPU g, U8CPU b) { | 75 U8CPU a, U8CPU r, U8CPU g, U8CPU b) { |
78 uint32_t r32; | 76 uint32_t r32; |
79 uint8_t* result = reinterpret_cast<uint8_t*>(&r32); | 77 uint8_t* result = reinterpret_cast<uint8_t*>(&r32); |
80 switch (config8888) { | 78 switch (config8888) { |
81 case SkCanvas::kNative_Premul_Config8888: | 79 case SkCanvas::kNative_Premul_Config8888: |
82 case SkCanvas::kNative_Unpremul_Config8888: | 80 case SkCanvas::kNative_Unpremul_Config8888: |
83 r32 = SkPackARGB32NoCheck(a, r, g, b); | 81 r32 = SkPackARGB32NoCheck(a, r, g, b); |
84 break; | 82 break; |
85 case SkCanvas::kBGRA_Premul_Config8888: | 83 case SkCanvas::kBGRA_Premul_Config8888: |
86 case SkCanvas::kBGRA_Unpremul_Config8888: | 84 case SkCanvas::kBGRA_Unpremul_Config8888: |
87 result[0] = b; | 85 result[0] = b; |
88 result[1] = g; | 86 result[1] = g; |
89 result[2] = r; | 87 result[2] = r; |
90 result[3] = a; | 88 result[3] = a; |
91 break; | 89 break; |
92 case SkCanvas::kRGBA_Premul_Config8888: | 90 case SkCanvas::kRGBA_Premul_Config8888: |
93 case SkCanvas::kRGBA_Unpremul_Config8888: | 91 case SkCanvas::kRGBA_Unpremul_Config8888: |
94 result[0] = r; | 92 result[0] = r; |
95 result[1] = g; | 93 result[1] = g; |
96 result[2] = b; | 94 result[2] = b; |
97 result[3] = a; | 95 result[3] = a; |
98 break; | 96 break; |
99 default: | 97 default: |
100 SkASSERT(0); | 98 SkASSERT(0); |
101 return 0; | 99 return 0; |
102 } | 100 } |
103 return r32; | 101 return r32; |
104 } | 102 } |
105 | 103 |
106 uint32_t getBitmapColor(int x, int y, int w, SkCanvas::Config8888 config8888) { | 104 static uint32_t getBitmapColor(int x, int y, int w, SkCanvas::Config8888 config8
888) { |
107 int n = y * w + x; | 105 int n = y * w + x; |
108 U8CPU b = n & 0xff; | 106 U8CPU b = n & 0xff; |
109 U8CPU g = (n >> 8) & 0xff; | 107 U8CPU g = (n >> 8) & 0xff; |
110 U8CPU r = (n >> 16) & 0xff; | 108 U8CPU r = (n >> 16) & 0xff; |
111 U8CPU a = 0; | 109 U8CPU a = 0; |
112 switch ((x+y) % 5) { | 110 switch ((x+y) % 5) { |
113 case 4: | 111 case 4: |
114 a = 0xff; | 112 a = 0xff; |
115 break; | 113 break; |
116 case 3: | 114 case 3: |
(...skipping 10 matching lines...) Expand all Loading... |
127 break; | 125 break; |
128 } | 126 } |
129 if (config8888IsPremul(config8888)) { | 127 if (config8888IsPremul(config8888)) { |
130 r = SkMulDiv255Ceiling(r, a); | 128 r = SkMulDiv255Ceiling(r, a); |
131 g = SkMulDiv255Ceiling(g, a); | 129 g = SkMulDiv255Ceiling(g, a); |
132 b = SkMulDiv255Ceiling(b, a); | 130 b = SkMulDiv255Ceiling(b, a); |
133 } | 131 } |
134 return packConfig8888(config8888, a, r, g , b); | 132 return packConfig8888(config8888, a, r, g , b); |
135 } | 133 } |
136 | 134 |
137 void fillCanvas(SkCanvas* canvas) { | 135 static void fillCanvas(SkCanvas* canvas) { |
138 static SkBitmap bmp; | 136 static SkBitmap bmp; |
139 if (bmp.isNull()) { | 137 if (bmp.isNull()) { |
140 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H); | 138 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H); |
141 SkDEBUGCODE(bool alloc = ) bmp.allocPixels(); | 139 SkDEBUGCODE(bool alloc = ) bmp.allocPixels(); |
142 SkASSERT(alloc); | 140 SkASSERT(alloc); |
143 SkAutoLockPixels alp(bmp); | 141 SkAutoLockPixels alp(bmp); |
144 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); | 142 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); |
145 for (int y = 0; y < DEV_H; ++y) { | 143 for (int y = 0; y < DEV_H; ++y) { |
146 for (int x = 0; x < DEV_W; ++x) { | 144 for (int x = 0; x < DEV_W; ++x) { |
147 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp
.rowBytes() + x * bmp.bytesPerPixel()); | 145 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp
.rowBytes() + x * bmp.bytesPerPixel()); |
148 *pixel = getCanvasColor(x, y); | 146 *pixel = getCanvasColor(x, y); |
149 } | 147 } |
150 } | 148 } |
151 } | 149 } |
152 canvas->save(); | 150 canvas->save(); |
153 canvas->setMatrix(SkMatrix::I()); | 151 canvas->setMatrix(SkMatrix::I()); |
154 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op); | 152 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op); |
155 SkPaint paint; | 153 SkPaint paint; |
156 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 154 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
157 canvas->drawBitmap(bmp, 0, 0, &paint); | 155 canvas->drawBitmap(bmp, 0, 0, &paint); |
158 canvas->restore(); | 156 canvas->restore(); |
159 } | 157 } |
160 | 158 |
161 SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888, | 159 static SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888, |
162 uint32_t color, | 160 uint32_t color, |
163 bool* premul) { | 161 bool* premul) { |
164 const uint8_t* c = reinterpret_cast<uint8_t*>(&color); | 162 const uint8_t* c = reinterpret_cast<uint8_t*>(&color); |
165 U8CPU a,r,g,b; | 163 U8CPU a,r,g,b; |
166 *premul = false; | 164 *premul = false; |
167 switch (config8888) { | 165 switch (config8888) { |
168 case SkCanvas::kNative_Premul_Config8888: | 166 case SkCanvas::kNative_Premul_Config8888: |
169 return color; | 167 return color; |
170 case SkCanvas::kNative_Unpremul_Config8888: | 168 case SkCanvas::kNative_Unpremul_Config8888: |
171 *premul = true; | 169 *premul = true; |
172 a = SkGetPackedA32(color); | 170 a = SkGetPackedA32(color); |
173 r = SkGetPackedR32(color); | 171 r = SkGetPackedR32(color); |
(...skipping 21 matching lines...) Expand all Loading... |
195 return 0; | 193 return 0; |
196 } | 194 } |
197 if (*premul) { | 195 if (*premul) { |
198 r = SkMulDiv255Ceiling(r, a); | 196 r = SkMulDiv255Ceiling(r, a); |
199 g = SkMulDiv255Ceiling(g, a); | 197 g = SkMulDiv255Ceiling(g, a); |
200 b = SkMulDiv255Ceiling(b, a); | 198 b = SkMulDiv255Ceiling(b, a); |
201 } | 199 } |
202 return SkPackARGB32(a, r, g, b); | 200 return SkPackARGB32(a, r, g, b); |
203 } | 201 } |
204 | 202 |
205 bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { | 203 static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { |
206 if (!didPremulConversion) { | 204 if (!didPremulConversion) { |
207 return a == b; | 205 return a == b; |
208 } | 206 } |
209 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); | 207 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); |
210 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); | 208 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); |
211 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); | 209 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); |
212 int32_t aB = SkGetPackedB32(a); | 210 int32_t aB = SkGetPackedB32(a); |
213 | 211 |
214 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); | 212 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); |
215 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); | 213 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); |
216 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); | 214 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); |
217 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); | 215 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); |
218 | 216 |
219 return aA == bA && | 217 return aA == bA && |
220 SkAbs32(aR - bR) <= 1 && | 218 SkAbs32(aR - bR) <= 1 && |
221 SkAbs32(aG - bG) <= 1 && | 219 SkAbs32(aG - bG) <= 1 && |
222 SkAbs32(aB - bB) <= 1; | 220 SkAbs32(aB - bB) <= 1; |
223 } | 221 } |
224 | 222 |
225 bool checkWrite(skiatest::Reporter* reporter, | 223 static bool checkWrite(skiatest::Reporter* reporter, |
226 SkCanvas* canvas, | 224 SkCanvas* canvas, |
227 const SkBitmap& bitmap, | 225 const SkBitmap& bitmap, |
228 int writeX, int writeY, | 226 int writeX, int writeY, |
229 SkCanvas::Config8888 config8888) { | 227 SkCanvas::Config8888 config8888) { |
230 SkBaseDevice* dev = canvas->getDevice(); | 228 SkBaseDevice* dev = canvas->getDevice(); |
231 if (!dev) { | 229 if (!dev) { |
232 return false; | 230 return false; |
233 } | 231 } |
234 SkBitmap devBmp = dev->accessBitmap(false); | 232 SkBitmap devBmp = dev->accessBitmap(false); |
235 if (devBmp.width() != DEV_W || | 233 if (devBmp.width() != DEV_W || |
236 devBmp.height() != DEV_H || | 234 devBmp.height() != DEV_H || |
237 devBmp.config() != SkBitmap::kARGB_8888_Config || | 235 devBmp.config() != SkBitmap::kARGB_8888_Config || |
238 devBmp.isNull()) { | 236 devBmp.isNull()) { |
239 return false; | 237 return false; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 | 296 |
299 static const CanvasConfig gCanvasConfigs[] = { | 297 static const CanvasConfig gCanvasConfigs[] = { |
300 {kRaster_DevType, true}, | 298 {kRaster_DevType, true}, |
301 {kRaster_DevType, false}, | 299 {kRaster_DevType, false}, |
302 #if SK_SUPPORT_GPU && defined(SK_SCALAR_IS_FLOAT) | 300 #if SK_SUPPORT_GPU && defined(SK_SCALAR_IS_FLOAT) |
303 {kGpu_BottomLeft_DevType, true}, // row bytes has no meaning on gpu devices | 301 {kGpu_BottomLeft_DevType, true}, // row bytes has no meaning on gpu devices |
304 {kGpu_TopLeft_DevType, true}, // row bytes has no meaning on gpu devices | 302 {kGpu_TopLeft_DevType, true}, // row bytes has no meaning on gpu devices |
305 #endif | 303 #endif |
306 }; | 304 }; |
307 | 305 |
308 SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) { | 306 static SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) { |
309 switch (c.fDevType) { | 307 switch (c.fDevType) { |
310 case kRaster_DevType: { | 308 case kRaster_DevType: { |
311 SkBitmap bmp; | 309 SkBitmap bmp; |
312 size_t rowBytes = c.fTightRowBytes ? 0 : 4 * DEV_W + 100; | 310 size_t rowBytes = c.fTightRowBytes ? 0 : 4 * DEV_W + 100; |
313 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H, rowBytes); | 311 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H, rowBytes); |
314 if (!bmp.allocPixels()) { | 312 if (!bmp.allocPixels()) { |
315 sk_throw(); | 313 sk_throw(); |
316 return NULL; | 314 return NULL; |
317 } | 315 } |
318 // if rowBytes isn't tight then set the padding to a known value | 316 // if rowBytes isn't tight then set the padding to a known value |
(...skipping 14 matching lines...) Expand all Loading... |
333 desc.fOrigin = kGpu_TopLeft_DevType == c.fDevType ? | 331 desc.fOrigin = kGpu_TopLeft_DevType == c.fDevType ? |
334 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; | 332 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; |
335 GrAutoScratchTexture ast(grCtx, desc, GrContext::kExact_ScratchTexMa
tch); | 333 GrAutoScratchTexture ast(grCtx, desc, GrContext::kExact_ScratchTexMa
tch); |
336 SkAutoTUnref<GrTexture> tex(ast.detach()); | 334 SkAutoTUnref<GrTexture> tex(ast.detach()); |
337 return new SkGpuDevice(grCtx, tex); | 335 return new SkGpuDevice(grCtx, tex); |
338 #endif | 336 #endif |
339 } | 337 } |
340 return NULL; | 338 return NULL; |
341 } | 339 } |
342 | 340 |
343 bool setupBitmap(SkBitmap* bitmap, | 341 static bool setupBitmap(SkBitmap* bitmap, |
344 SkCanvas::Config8888 config8888, | 342 SkCanvas::Config8888 config8888, |
345 int w, int h, | 343 int w, int h, |
346 bool tightRowBytes) { | 344 bool tightRowBytes) { |
347 size_t rowBytes = tightRowBytes ? 0 : 4 * w + 60; | 345 size_t rowBytes = tightRowBytes ? 0 : 4 * w + 60; |
348 bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h, rowBytes); | 346 bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h, rowBytes); |
349 if (!bitmap->allocPixels()) { | 347 if (!bitmap->allocPixels()) { |
350 return false; | 348 return false; |
351 } | 349 } |
352 SkAutoLockPixels alp(*bitmap); | 350 SkAutoLockPixels alp(*bitmap); |
353 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); | 351 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); |
354 for (int y = 0; y < h; ++y) { | 352 for (int y = 0; y < h; ++y) { |
355 for (int x = 0; x < w; ++x) { | 353 for (int x = 0; x < w; ++x) { |
356 uint32_t* pixel = reinterpret_cast<uint32_t*>(pixels + y * bitmap->r
owBytes() + x * 4); | 354 uint32_t* pixel = reinterpret_cast<uint32_t*>(pixels + y * bitmap->r
owBytes() + x * 4); |
357 *pixel = getBitmapColor(x, y, w, config8888); | 355 *pixel = getBitmapColor(x, y, w, config8888); |
358 } | 356 } |
359 } | 357 } |
360 return true; | 358 return true; |
361 } | 359 } |
362 | 360 |
363 void WritePixelsTest(skiatest::Reporter* reporter, GrContextFactory* factory) { | 361 static void WritePixelsTest(skiatest::Reporter* reporter, GrContextFactory* fact
ory) { |
364 SkCanvas canvas; | 362 SkCanvas canvas; |
365 | 363 |
366 const SkIRect testRects[] = { | 364 const SkIRect testRects[] = { |
367 // entire thing | 365 // entire thing |
368 DEV_RECT, | 366 DEV_RECT, |
369 // larger on all sides | 367 // larger on all sides |
370 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), | 368 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), |
371 // fully contained | 369 // fully contained |
372 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), | 370 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), |
373 // outside top left | 371 // outside top left |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.f
Top, | 462 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.f
Top, |
465 bmp.width(), bmp.h
eight()); | 463 bmp.width(), bmp.h
eight()); |
466 bool intersects = SkIRect::Intersects(canvasRect, writeR
ect) ; | 464 bool intersects = SkIRect::Intersects(canvasRect, writeR
ect) ; |
467 REPORTER_ASSERT(reporter, intersects == (idBefore != idA
fter)); | 465 REPORTER_ASSERT(reporter, intersects == (idBefore != idA
fter)); |
468 } | 466 } |
469 } | 467 } |
470 } | 468 } |
471 } | 469 } |
472 } | 470 } |
473 } | 471 } |
474 } | |
475 | 472 |
476 #include "TestClassDef.h" | 473 #include "TestClassDef.h" |
477 DEFINE_GPUTESTCLASS("WritePixels", WritePixelsTestClass, WritePixelsTest) | 474 DEFINE_GPUTESTCLASS("WritePixels", WritePixelsTestClass, WritePixelsTest) |
OLD | NEW |