blob: c39e03b85de8373bcd4d5bf9b9e4a635a0b496b5 [file] [log] [blame]
bsalomon@google.comc6980972011-11-02 19:57:21 +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"
11#include "SkRegion.h"
12#include "SkGpuDevice.h"
13
14
15static const int DEV_W = 100, DEV_H = 100;
16static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
17static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
18 DEV_H * SK_Scalar1);
19
20namespace {
21SkPMColor getCanvasColor(int x, int y) {
22 SkASSERT(x >= 0 && x < DEV_W);
23 SkASSERT(y >= 0 && y < DEV_H);
24 return SkPackARGB32(0xff, x, y, 0x0);
25}
26
27SkPMColor getBitmapColor(int x, int y, int w, int h) {
28 int n = y * w + x;
29
30 U8CPU b = n & 0xff;
31 U8CPU g = (n >> 8) & 0xff;
32 U8CPU r = (n >> 16) & 0xff;
33 return SkPackARGB32(0xff, r, g , b);
34}
35
36void fillCanvas(SkCanvas* canvas) {
37 static SkBitmap bmp;
38 if (bmp.isNull()) {
39 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H);
40 bool alloc = bmp.allocPixels();
41 SkASSERT(alloc);
42 SkAutoLockPixels alp(bmp);
43 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
44 for (int y = 0; y < DEV_H; ++y) {
45 for (int x = 0; x < DEV_W; ++x) {
46 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
47 *pixel = getCanvasColor(x, y);
48 }
49 }
50 }
51 canvas->save();
52 canvas->setMatrix(SkMatrix::I());
53 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
54 SkPaint paint;
55 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
56 canvas->drawBitmap(bmp, 0, 0, &paint);
57 canvas->restore();
58}
59
60void fillBitmap(SkBitmap* bitmap) {
61 SkASSERT(bitmap->lockPixelsAreWritable());
62 SkAutoLockPixels alp(*bitmap);
63 int w = bitmap->width();
64 int h = bitmap->height();
65 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
66 for (int y = 0; y < h; ++y) {
67 for (int x = 0; x < w; ++x) {
68 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
69 *pixel = getBitmapColor(x, y, w, h);
70 }
71 }
72}
73
74// checks the bitmap contains correct pixels after the readPixels
75// if the bitmap was prefilled with pixels it checks that these weren't
76// overwritten in the area outside the readPixels.
77bool checkRead(skiatest::Reporter* reporter,
78 const SkBitmap& bitmap,
79 int x, int y,
80 bool preFilledBmp) {
81 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
82 SkASSERT(!bitmap.isNull());
83
84 int bw = bitmap.width();
85 int bh = bitmap.height();
86
87 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
88 SkIRect clippedSrcRect = DEV_RECT;
89 if (!clippedSrcRect.intersect(srcRect)) {
90 clippedSrcRect.setEmpty();
91 }
92
93 SkAutoLockPixels alp(bitmap);
94 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap.getPixels());
95 for (int by = 0; by < bh; ++by) {
96 for (int bx = 0; bx < bw; ++bx) {
97 int devx = bx + srcRect.fLeft;
98 int devy = by + srcRect.fTop;
99
100 SkPMColor pixel = *reinterpret_cast<SkPMColor*>(pixels + by * bitmap.rowBytes() + bx * bitmap.bytesPerPixel());
101
102 if (clippedSrcRect.contains(devx, devy)) {
103 REPORTER_ASSERT(reporter, getCanvasColor(devx, devy) == pixel);
104 if (getCanvasColor(devx, devy) != pixel) {
105 return false;
106 }
107 } else if (preFilledBmp) {
108 REPORTER_ASSERT(reporter, getBitmapColor(bx, by, bw, bh) == pixel);
109 if (getBitmapColor(bx, by, bw, bh) != pixel) {
110 return false;
111 }
112 }
113 }
114 }
115 return true;
116}
117
118enum BitmapInit {
119 kFirstBitmapInit = 0,
120
121 kNoPixels_BitmapInit = kFirstBitmapInit,
122 kTight_BitmapInit,
123 kRowBytes_BitmapInit,
124
125 kBitmapInitCnt
126};
127
128BitmapInit nextBMI(BitmapInit bmi) {
129 int x = bmi;
130 return static_cast<BitmapInit>(++x);
131}
132
133
134void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init) {
135 int w = rect.width();
136 int h = rect.height();
137 int rowBytes = 0;
138 bool alloc = true;
139 switch (init) {
140 case kNoPixels_BitmapInit:
141 alloc = false;
142 case kTight_BitmapInit:
143 break;
144 case kRowBytes_BitmapInit:
145 rowBytes = w * sizeof(SkPMColor) + 16 * sizeof(SkPMColor);
146 break;
147 default:
148 SkASSERT(0);
149 break;
150 }
151 bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h, rowBytes);
152 if (alloc) {
153 bitmap->allocPixels();
154 }
155}
156
157void ReadPixelsTest(skiatest::Reporter* reporter, GrContext* context) {
158 SkCanvas canvas;
159
160 const SkIRect testRects[] = {
161 // entire thing
162 DEV_RECT,
163 // larger on all sides
164 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
165 // fully contained
166 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
167 // outside top left
168 SkIRect::MakeLTRB(-10, -10, -1, -1),
169 // touching top left corner
170 SkIRect::MakeLTRB(-10, -10, 0, 0),
171 // overlapping top left corner
172 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
173 // overlapping top left and top right corners
174 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
175 // touching entire top edge
176 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
177 // overlapping top right corner
178 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
179 // contained in x, overlapping top edge
180 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
181 // outside top right corner
182 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
183 // touching top right corner
184 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
185 // overlapping top left and bottom left corners
186 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
187 // touching entire left edge
188 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
189 // overlapping bottom left corner
190 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
191 // contained in y, overlapping left edge
192 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
193 // outside bottom left corner
194 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
195 // touching bottom left corner
196 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
197 // overlapping bottom left and bottom right corners
198 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
199 // touching entire left edge
200 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
201 // overlapping bottom right corner
202 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
203 // overlapping top right and bottom right corners
204 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
205 };
206
207 for (int dtype = 0; dtype < 2; ++dtype) {
208
209 if (0 == dtype) {
210 canvas.setDevice(new SkDevice(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H, false))->unref();
211 } else {
212#if SK_SCALAR_IS_FIXED
213 // GPU device known not to work in the fixed pt build.
214 continue;
215#endif
216 canvas.setDevice(new SkGpuDevice(context, SkBitmap::kARGB_8888_Config, DEV_W, DEV_H))->unref();
217 }
218 fillCanvas(&canvas);
219
220 for (int rect = 0; rect < SK_ARRAY_COUNT(testRects); ++rect) {
221 SkBitmap bmp;
222 for (BitmapInit bmi = kFirstBitmapInit; bmi < kBitmapInitCnt; bmi = nextBMI(bmi)) {
223
224 const SkIRect& srcRect = testRects[rect];
225
226 init_bitmap(&bmp, srcRect, bmi);
227
228 // if the bitmap has pixels allocated before the readPixels, note
229 // that and fill them with pattern
230 bool startsWithPixels = !bmp.isNull();
231 if (startsWithPixels) {
232 fillBitmap(&bmp);
233 }
234
235 bool success = canvas.readPixels(&bmp, srcRect.fLeft, srcRect.fTop);
236
237 // determine whether we expected the read to succeed.
238 REPORTER_ASSERT(reporter, success == SkIRect::Intersects(srcRect, DEV_RECT));
239
240 if (success || startsWithPixels) {
241 checkRead(reporter, bmp, srcRect.fLeft, srcRect.fTop, startsWithPixels);
242 } else {
243 // if we had no pixels beforehand and the readPixels failed then
244 // our bitmap should still not have any pixels
245 REPORTER_ASSERT(reporter, bmp.isNull());
246 }
247
248 // check the old webkit version of readPixels that clips the bitmap size
249 SkBitmap wkbmp;
250 success = canvas.readPixels(srcRect, &wkbmp);
251 SkIRect clippedRect = DEV_RECT;
252 if (clippedRect.intersect(srcRect)) {
253 REPORTER_ASSERT(reporter, success);
254 checkRead(reporter, wkbmp, clippedRect.fLeft, clippedRect.fTop, false);
255 } else {
256 REPORTER_ASSERT(reporter, !success);
257 }
258 }
259 }
260 }
261}
262}
263
264#include "TestClassDef.h"
265DEFINE_GPUTESTCLASS("ReadPixels", ReadPixelsTestClass, ReadPixelsTest)
266