blob: 688aa5d22709db1c78ac21a065824c16f2734a4b [file] [log] [blame]
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +00001
2/*
3 * Copyright 2011 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"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000011#include "SkColorPriv.h"
12#include "SkDevice.h"
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000013#include "SkRegion.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000014#if SK_SUPPORT_GPU
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000015#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000016#else
17class GrContext;
18#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000019
20static const int DEV_W = 100, DEV_H = 100;
21static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
22static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
23 DEV_H * SK_Scalar1);
24static const U8CPU DEV_PAD = 0xee;
25
26namespace {
27SkPMColor getCanvasColor(int x, int y) {
28 SkASSERT(x >= 0 && x < DEV_W);
29 SkASSERT(y >= 0 && y < DEV_H);
30
31 U8CPU r = x;
32 U8CPU g = y;
33 U8CPU b = 0xc;
34
bsalomon@google.com31648eb2011-11-23 15:01:08 +000035 U8CPU a = 0x0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000036 switch ((x+y) % 5) {
37 case 0:
38 a = 0xff;
39 break;
40 case 1:
41 a = 0x80;
42 break;
43 case 2:
44 a = 0xCC;
45 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000046 case 3:
47 a = 0x00;
48 break;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000049 case 4:
50 a = 0x01;
51 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000052 }
53 return SkPremultiplyARGBInline(a, r, g, b);
54}
55
56bool config8888IsPremul(SkCanvas::Config8888 config8888) {
57 switch (config8888) {
58 case SkCanvas::kNative_Premul_Config8888:
59 case SkCanvas::kBGRA_Premul_Config8888:
60 case SkCanvas::kRGBA_Premul_Config8888:
61 return true;
62 case SkCanvas::kNative_Unpremul_Config8888:
63 case SkCanvas::kBGRA_Unpremul_Config8888:
64 case SkCanvas::kRGBA_Unpremul_Config8888:
65 return false;
66 default:
67 SkASSERT(0);
68 return false;
69 }
70}
71
72// assumes any premu/.unpremul has been applied
73uint32_t packConfig8888(SkCanvas::Config8888 config8888,
74 U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
75 uint32_t r32;
76 uint8_t* result = reinterpret_cast<uint8_t*>(&r32);
77 switch (config8888) {
78 case SkCanvas::kNative_Premul_Config8888:
79 case SkCanvas::kNative_Unpremul_Config8888:
80 r32 = SkPackARGB32NoCheck(a, r, g, b);
81 break;
82 case SkCanvas::kBGRA_Premul_Config8888:
83 case SkCanvas::kBGRA_Unpremul_Config8888:
84 result[0] = b;
85 result[1] = g;
86 result[2] = r;
87 result[3] = a;
88 break;
89 case SkCanvas::kRGBA_Premul_Config8888:
90 case SkCanvas::kRGBA_Unpremul_Config8888:
91 result[0] = r;
92 result[1] = g;
93 result[2] = b;
94 result[3] = a;
95 break;
96 default:
97 SkASSERT(0);
98 return 0;
99 }
100 return r32;
101}
102
103uint32_t getBitmapColor(int x, int y, int w, int h, SkCanvas::Config8888 config8888) {
104 int n = y * w + x;
105 U8CPU b = n & 0xff;
106 U8CPU g = (n >> 8) & 0xff;
107 U8CPU r = (n >> 16) & 0xff;
bsalomon@google.com31648eb2011-11-23 15:01:08 +0000108 U8CPU a = 0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000109 switch ((x+y) % 5) {
110 case 4:
111 a = 0xff;
112 break;
113 case 3:
114 a = 0x80;
115 break;
116 case 2:
117 a = 0xCC;
118 break;
119 case 1:
120 a = 0x01;
121 break;
122 case 0:
123 a = 0x00;
124 break;
125 }
126 if (config8888IsPremul(config8888)) {
127 r = SkMulDiv255Ceiling(r, a);
128 g = SkMulDiv255Ceiling(g, a);
129 b = SkMulDiv255Ceiling(b, a);
130 }
131 return packConfig8888(config8888, a, r, g , b);
132}
133
134void fillCanvas(SkCanvas* canvas) {
135 static SkBitmap bmp;
136 if (bmp.isNull()) {
137 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H);
138 bool alloc = bmp.allocPixels();
139 SkASSERT(alloc);
140 SkAutoLockPixels alp(bmp);
141 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
142 for (int y = 0; y < DEV_H; ++y) {
143 for (int x = 0; x < DEV_W; ++x) {
144 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
145 *pixel = getCanvasColor(x, y);
146 }
147 }
148 }
149 canvas->save();
150 canvas->setMatrix(SkMatrix::I());
151 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
152 SkPaint paint;
153 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
154 canvas->drawBitmap(bmp, 0, 0, &paint);
155 canvas->restore();
156}
157
158SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888,
159 uint32_t color,
160 bool* premul) {
161 const uint8_t* c = reinterpret_cast<uint8_t*>(&color);
162 U8CPU a,r,g,b;
163 *premul = false;
164 switch (config8888) {
165 case SkCanvas::kNative_Premul_Config8888:
166 return color;
167 case SkCanvas::kNative_Unpremul_Config8888:
168 *premul = true;
169 a = SkGetPackedA32(color);
170 r = SkGetPackedR32(color);
171 g = SkGetPackedG32(color);
172 b = SkGetPackedB32(color);
173 break;
174 case SkCanvas::kBGRA_Unpremul_Config8888:
175 *premul = true; // fallthru
176 case SkCanvas::kBGRA_Premul_Config8888:
177 a = static_cast<U8CPU>(c[3]);
178 r = static_cast<U8CPU>(c[2]);
179 g = static_cast<U8CPU>(c[1]);
180 b = static_cast<U8CPU>(c[0]);
181 break;
182 case SkCanvas::kRGBA_Unpremul_Config8888:
183 *premul = true; // fallthru
184 case SkCanvas::kRGBA_Premul_Config8888:
185 a = static_cast<U8CPU>(c[3]);
186 r = static_cast<U8CPU>(c[0]);
187 g = static_cast<U8CPU>(c[1]);
188 b = static_cast<U8CPU>(c[2]);
189 break;
190 default:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000191 SkDEBUGFAIL("Unexpected Config8888");
192 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000193 }
194 if (*premul) {
195 r = SkMulDiv255Ceiling(r, a);
196 g = SkMulDiv255Ceiling(g, a);
197 b = SkMulDiv255Ceiling(b, a);
198 }
199 return SkPackARGB32(a, r, g, b);
200}
201
202bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
203 if (!didPremulConversion) {
204 return a == b;
205 }
206 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
207 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
208 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
209 int32_t aB = SkGetPackedB32(a);
210
211 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
212 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
213 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
214 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
215
216 return aA == bA &&
217 SkAbs32(aR - bR) <= 1 &&
218 SkAbs32(aG - bG) <= 1 &&
219 SkAbs32(aB - bB) <= 1;
220}
221
222bool checkWrite(skiatest::Reporter* reporter,
223 SkCanvas* canvas,
224 const SkBitmap& bitmap,
225 int writeX, int writeY,
226 SkCanvas::Config8888 config8888) {
227 SkDevice* dev = canvas->getDevice();
228 if (!dev) {
229 return false;
230 }
231 SkBitmap devBmp = dev->accessBitmap(false);
232 if (devBmp.width() != DEV_W ||
233 devBmp.height() != DEV_H ||
234 devBmp.config() != SkBitmap::kARGB_8888_Config ||
235 devBmp.isNull()) {
236 return false;
237 }
238 SkAutoLockPixels alp(devBmp);
239
240 intptr_t canvasPixels = reinterpret_cast<intptr_t>(devBmp.getPixels());
241 size_t canvasRowBytes = devBmp.rowBytes();
242 SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
243 bool success = true;
244 for (int cy = 0; cy < DEV_H; ++cy) {
245 const SkPMColor* canvasRow = reinterpret_cast<const SkPMColor*>(canvasPixels);
246 for (int cx = 0; cx < DEV_W; ++cx) {
247 SkPMColor canvasPixel = canvasRow[cx];
248 if (writeRect.contains(cx, cy)) {
249 int bx = cx - writeX;
250 int by = cy - writeY;
251 uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(), bitmap.height(), config8888);
252 bool mul;
253 SkPMColor bmpPMColor = convertConfig8888ToPMColor(config8888, bmpColor8888, &mul);
254 bool check;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000255 REPORTER_ASSERT(reporter, check = checkPixel(bmpPMColor, canvasPixel, mul));
256 if (!check) {
257 success = false;
258 }
259 } else {
260 bool check;
261 SkPMColor testColor = getCanvasColor(cx, cy);
262 REPORTER_ASSERT(reporter, check = (canvasPixel == testColor));
263 if (!check) {
264 success = false;
265 }
266 }
267 }
268 if (cy != DEV_H -1) {
269 const char* pad = reinterpret_cast<const char*>(canvasPixels + 4 * DEV_W);
270 for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
271 bool check;
272 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
273 if (!check) {
274 success = false;
275 }
276 }
277 }
278 canvasPixels += canvasRowBytes;
279 }
280
281 return success;
282}
283
284enum DevType {
285 kRaster_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000286#if SK_SUPPORT_GPU
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000287 kGpu_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000288#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000289};
290
291struct CanvasConfig {
292 DevType fDevType;
293 bool fTightRowBytes;
294};
295
296static const CanvasConfig gCanvasConfigs[] = {
297 {kRaster_DevType, true},
298 {kRaster_DevType, false},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000299#if SK_SUPPORT_GPU && defined(SK_SCALAR_IS_FLOAT)
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000300 {kGpu_DevType, true}, // row bytes has no meaning on gpu devices
bsalomon@google.combbce8b22011-11-10 21:20:37 +0000301#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000302};
303
304bool setupCanvas(SkCanvas* canvas, const CanvasConfig& c, GrContext* grCtx) {
305 switch (c.fDevType) {
306 case kRaster_DevType: {
307 SkBitmap bmp;
308 size_t rowBytes = c.fTightRowBytes ? 0 : 4 * DEV_W + 100;
309 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H, rowBytes);
310 if (!bmp.allocPixels()) {
311 return false;
312 }
313 // if rowBytes isn't tight then set the padding to a known value
314 if (rowBytes) {
315 SkAutoLockPixels alp(bmp);
316 memset(bmp.getPixels(), DEV_PAD, bmp.getSafeSize());
317 }
318 canvas->setDevice(new SkDevice(bmp))->unref();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000319 break;
320 }
321#if SK_SUPPORT_GPU
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000322 case kGpu_DevType:
323 canvas->setDevice(new SkGpuDevice(grCtx,
324 SkBitmap::kARGB_8888_Config,
325 DEV_W, DEV_H))->unref();
326 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000327#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000328 }
329 return true;
330}
331
332bool setupBitmap(SkBitmap* bitmap,
333 SkCanvas::Config8888 config8888,
334 int w, int h,
335 bool tightRowBytes) {
336 size_t rowBytes = tightRowBytes ? 0 : 4 * w + 60;
337 bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h, rowBytes);
338 if (!bitmap->allocPixels()) {
339 return false;
340 }
341 SkAutoLockPixels alp(*bitmap);
342 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
343 for (int y = 0; y < h; ++y) {
344 for (int x = 0; x < w; ++x) {
345 uint32_t* pixel = reinterpret_cast<uint32_t*>(pixels + y * bitmap->rowBytes() + x * 4);
346 *pixel = getBitmapColor(x, y, w, h, config8888);
347 }
348 }
349 return true;
350}
351
352void WritePixelsTest(skiatest::Reporter* reporter, GrContext* context) {
353 SkCanvas canvas;
354
355 const SkIRect testRects[] = {
356 // entire thing
357 DEV_RECT,
358 // larger on all sides
359 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
360 // fully contained
361 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
362 // outside top left
363 SkIRect::MakeLTRB(-10, -10, -1, -1),
364 // touching top left corner
365 SkIRect::MakeLTRB(-10, -10, 0, 0),
366 // overlapping top left corner
367 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
368 // overlapping top left and top right corners
369 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
370 // touching entire top edge
371 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
372 // overlapping top right corner
373 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
374 // contained in x, overlapping top edge
375 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
376 // outside top right corner
377 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
378 // touching top right corner
379 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
380 // overlapping top left and bottom left corners
381 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
382 // touching entire left edge
383 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
384 // overlapping bottom left corner
385 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
386 // contained in y, overlapping left edge
387 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
388 // outside bottom left corner
389 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
390 // touching bottom left corner
391 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
392 // overlapping bottom left and bottom right corners
393 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
394 // touching entire left edge
395 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
396 // overlapping bottom right corner
397 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
398 // overlapping top right and bottom right corners
399 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
400 };
401
402 for (size_t i = 0; i < SK_ARRAY_COUNT(gCanvasConfigs); ++i) {
403 REPORTER_ASSERT(reporter, setupCanvas(&canvas, gCanvasConfigs[i], context));
404
405 static const SkCanvas::Config8888 gReadConfigs[] = {
406 SkCanvas::kNative_Premul_Config8888,
407 SkCanvas::kNative_Unpremul_Config8888,
408 SkCanvas::kBGRA_Premul_Config8888,
409 SkCanvas::kBGRA_Unpremul_Config8888,
410 SkCanvas::kRGBA_Premul_Config8888,
411 SkCanvas::kRGBA_Unpremul_Config8888,
412 };
reed@google.comb03db4a2011-11-16 21:36:27 +0000413 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000414 const SkIRect& rect = testRects[r];
415 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
reed@google.comb03db4a2011-11-16 21:36:27 +0000416 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadConfigs); ++c) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000417 fillCanvas(&canvas);
418 SkCanvas::Config8888 config8888 = gReadConfigs[c];
419 SkBitmap bmp;
420 REPORTER_ASSERT(reporter, setupBitmap(&bmp, config8888, rect.width(), rect.height(), SkToBool(tightBmp)));
421 canvas.writePixels(bmp, rect.fLeft, rect.fTop, config8888);
422 REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop, config8888));
423 }
424 }
425 }
426 }
427}
428}
429
borenet@google.com0cb1e2b2012-07-12 19:48:42 +0000430#ifndef SK_BUILD_FOR_ANDROID
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000431#include "TestClassDef.h"
432DEFINE_GPUTESTCLASS("WritePixels", WritePixelsTestClass, WritePixelsTest)
borenet@google.com0cb1e2b2012-07-12 19:48:42 +0000433#endif