Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: tests/PictureTest.cpp

Issue 132293002: Expand GatherPixelRefs unit test (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: clean up Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/utils/SkPictureUtils.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "Test.h" 8 #include "Test.h"
9 #include "TestClassDef.h" 9 #include "TestClassDef.h"
10 #include "SkBitmapDevice.h" 10 #include "SkBitmapDevice.h"
11 #include "SkCanvas.h" 11 #include "SkCanvas.h"
12 #include "SkColorPriv.h" 12 #include "SkColorPriv.h"
13 #include "SkData.h" 13 #include "SkData.h"
14 #include "SkDecodingImageGenerator.h" 14 #include "SkDecodingImageGenerator.h"
15 #include "SkError.h" 15 #include "SkError.h"
16 #include "SkImageEncoder.h" 16 #include "SkImageEncoder.h"
17 #include "SkImageGenerator.h" 17 #include "SkImageGenerator.h"
18 #include "SkPaint.h" 18 #include "SkPaint.h"
19 #include "SkPicture.h" 19 #include "SkPicture.h"
20 #include "SkPictureUtils.h" 20 #include "SkPictureUtils.h"
21 #include "SkRandom.h" 21 #include "SkRandom.h"
22 #include "SkRRect.h" 22 #include "SkRRect.h"
23 #include "SkShader.h" 23 #include "SkShader.h"
24 #include "SkStream.h" 24 #include "SkStream.h"
25 25
26 static const int gColorScale = 30;
27 static const int gColorOffset = 60;
26 28
27 static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) { 29 static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) {
28 bm->setConfig(SkBitmap::kARGB_8888_Config, w, h); 30 bm->setConfig(SkBitmap::kARGB_8888_Config, w, h);
29 bm->allocPixels(); 31 bm->allocPixels();
30 bm->eraseColor(color); 32 bm->eraseColor(color);
31 if (immutable) { 33 if (immutable) {
32 bm->setImmutable(); 34 bm->setImmutable();
33 } 35 }
34 } 36 }
35 37
36 typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&, const SkPoint&); 38 void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) {
37 39 SkASSERT(w % 2 == 0);
38 static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm, 40 SkASSERT(h % 2 == 0);
39 const SkPoint& pos) { 41 bm->setConfig(SkBitmap::kA8_Config, w, h);
42 bm->allocPixels();
43 SkAutoLockPixels lock(*bm);
44 for (int y = 0; y < h; y += 2) {
45 uint8_t* s = bm->getAddr8(0, y);
46 for (int x = 0; x < w; x += 2) {
47 *s++ = 0xFF;
48 *s++ = 0x00;
49 }
50 s = bm->getAddr8(0, y + 1);
51 for (int x = 0; x < w; x += 2) {
52 *s++ = 0x00;
53 *s++ = 0xFF;
54 }
55 }
56 if (immutable) {
57 bm->setImmutable();
58 }
59 }
60
61 static void init_paint(SkPaint* paint, const SkBitmap &bm) {
62 SkShader* shader = SkShader::CreateBitmapShader(bm,
63 SkShader::kClamp_TileMode,
64 SkShader::kClamp_TileMode);
65 paint->setShader(shader)->unref();
66 }
67
68 typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&, const SkBitmap&, cons t SkPoint&);
69
70 static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm,
71 const SkBitmap& altBM, const SkPoint& pos) {
72 SkPaint paint;
73 init_paint(&paint, bm);
74
75 canvas->drawPaint(paint);
76 }
77
78 static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm,
79 const SkBitmap& altBM, const SkPoint& pos) {
80 SkPaint paint;
81 init_paint(&paint, bm);
82
83 // draw a slightly inset rect
84 SkPoint points[5] = {
85 { pos.fX + 1, pos.fY + 1 },
86 { pos.fX + bm.width() - 2, pos.fY + 1 },
87 { pos.fX + bm.width() - 2, pos.fY + bm.height() - 2 },
88 { pos.fX + 1, pos.fY + bm.height() - 2 },
89 { pos.fX + 1, pos.fY + 1 },
90 };
91
92 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint);
93 }
94
95 static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm,
96 const SkBitmap& altBM, const SkPoint& pos) {
97 SkPaint paint;
98 init_paint(&paint, bm);
99
100 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
101 r.offset(pos.fX, pos.fY);
102
103 canvas->drawRect(r, paint);
104 }
105
106 static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm,
107 const SkBitmap& altBM, const SkPoint& pos) {
108 SkPaint paint;
109 init_paint(&paint, bm);
110
111 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
112 r.offset(pos.fX, pos.fY);
113
114 canvas->drawOval(r, paint);
115 }
116
117 static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm,
118 const SkBitmap& altBM, const SkPoint& pos) {
119 SkPaint paint;
120 init_paint(&paint, bm);
121
122 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
123 r.offset(pos.fX, pos.fY);
124
125 SkRRect rr;
126 rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4);
127 canvas->drawRRect(rr, paint);
128 }
129
130 static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm,
131 const SkBitmap& altBM, const SkPoint& pos) {
132 SkPaint paint;
133 init_paint(&paint, bm);
134
135 SkPath path;
136 path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height()));
137 path.lineTo(SkIntToScalar(bm.width()), 0);
138 path.close();
139 path.offset(pos.fX, pos.fY);
140
141 canvas->drawPath(path, paint);
142 }
143
144 static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm,
145 const SkBitmap& altBM, const SkPoint& pos) {
40 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL); 146 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL);
41 } 147 }
42 148
43 static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm, 149 static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
44 const SkPoint& pos) { 150 const SkBitmap& altBM, const SkPoint& pos ) {
45 SkRect r = { 151 SkPaint paint;
46 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) 152 init_paint(&paint, bm);
153
154 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
155 canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint);
156 }
157
158 static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm,
159 const SkBitmap& altBM, const SkPoint& pos) {
160 const SkMatrix& ctm = canvas->getTotalMatrix();
161
162 SkPoint p(pos);
163 ctm.mapPoints(&p, 1);
164
165 canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL);
166 }
167
168 #if 0
169 // Although specifiable, this case doesn't seem to make sense (i.e., the
170 // bitmap in the shader is never used).
171 static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
172 const SkBitmap& altBM, const SkPoint& pos ) {
173 SkPaint paint;
174 init_paint(&paint, bm);
175
176 const SkMatrix& ctm = canvas->getTotalMatrix();
177
178 SkPoint p(pos);
179 ctm.mapPoints(&p, 1);
180
181 canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint);
182 }
183 #endif
184
185 static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm,
186 const SkBitmap& altBM, const SkPoint& pos) {
187 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
188
189 r.offset(pos.fX, pos.fY);
190 canvas->drawBitmapRectToRect(bm, NULL, r, NULL);
191 }
192
193 static void drawbitmaprect_withshader_proc(SkCanvas* canvas,
194 const SkBitmap& bm,
195 const SkBitmap& altBM,
196 const SkPoint& pos) {
197 SkPaint paint;
198 init_paint(&paint, bm);
199
200 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
201 r.offset(pos.fX, pos.fY);
202
203 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
204 canvas->drawBitmapRectToRect(altBM, NULL, r, &paint);
205 }
206
207 static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm,
208 const SkBitmap& altBM, const SkPoint& pos) {
209 SkPaint paint;
210 init_paint(&paint, bm);
211 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
212
213 canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint);
214 }
215
216 static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm,
217 const SkBitmap& altBM, const SkPoint& pos) {
218 SkPaint paint;
219 init_paint(&paint, bm);
220 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
221
222 SkPoint point = { pos.fX, pos.fY + bm.height() };
223 canvas->drawPosText("O", 1, &point, paint);
224 }
225
226 static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm,
227 const SkBitmap& altBM, const SkPoint& pos) {
228 SkPaint paint;
229
230 init_paint(&paint, bm);
231 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
232
233 SkPath path;
234 path.lineTo(SkIntToScalar(bm.width()), 0);
235 path.offset(pos.fX, pos.fY+bm.height());
236
237 canvas->drawTextOnPath("O", 1, path, NULL, paint);
238 }
239
240 static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm,
241 const SkBitmap& altBM, const SkPoint& pos) {
242 SkPaint paint;
243 init_paint(&paint, bm);
244
245 SkPoint verts[4] = {
246 { pos.fX+1, pos.fY+1 },
247 { pos.fX + bm.width()-1, pos.fY+1 },
248 { pos.fX + bm.width()-1, pos.fY + bm.height()-1 },
249 { pos.fX+1, pos.fY + bm.height()-1 }
47 }; 250 };
48 r.offset(pos.fX, pos.fY); 251 SkPoint texs[4] = { { 0, 0 },
49 canvas->drawBitmapRectToRect(bm, NULL, r, NULL); 252 { SkIntToScalar(bm.width()), 0 },
50 } 253 { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) },
51 254 { 0, SkIntToScalar(bm.height()) } };
52 static void drawshader_proc(SkCanvas* canvas, const SkBitmap& bm, 255 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 };
53 const SkPoint& pos) { 256
54 SkRect r = { 257 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL,
55 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) 258 indices, 6, paint);
56 };
57 r.offset(pos.fX, pos.fY);
58
59 SkShader* s = SkShader::CreateBitmapShader(bm,
60 SkShader::kClamp_TileMode,
61 SkShader::kClamp_TileMode);
62 SkPaint paint;
63 paint.setShader(s)->unref();
64 canvas->drawRect(r, paint);
65 canvas->drawOval(r, paint);
66 SkRRect rr;
67 rr.setRectXY(r, 10, 10);
68 canvas->drawRRect(rr, paint);
69 } 259 }
70 260
71 // Return a picture with the bitmaps drawn at the specified positions. 261 // Return a picture with the bitmaps drawn at the specified positions.
72 static SkPicture* record_bitmaps(const SkBitmap bm[], const SkPoint pos[], 262 static SkPicture* record_bitmaps(const SkBitmap bm[], const SkPoint pos[],
73 int count, DrawBitmapProc proc) { 263 int count, DrawBitmapProc proc) {
74 SkPicture* pic = new SkPicture; 264 SkPicture* pic = new SkPicture;
75 SkCanvas* canvas = pic->beginRecording(1000, 1000); 265 SkCanvas* canvas = pic->beginRecording(1000, 1000);
76 for (int i = 0; i < count; ++i) { 266 for (int i = 0; i < count; ++i) {
77 proc(canvas, bm[i], pos[i]); 267 canvas->save();
268 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY,
269 SkIntToScalar(bm[i].width()),
270 SkIntToScalar(bm[i].height()));
271 canvas->clipRect(clipRect, SkRegion::kIntersect_Op);
272 proc(canvas, bm[i], bm[count+i], pos[i]);
273 canvas->restore();
78 } 274 }
79 pic->endRecording(); 275 pic->endRecording();
80 return pic; 276 return pic;
81 } 277 }
82 278
83 static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) { 279 static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) {
84 rect->fLeft = rand.nextRangeScalar(-W, 2*W); 280 rect->fLeft = rand.nextRangeScalar(-W, 2*W);
85 rect->fTop = rand.nextRangeScalar(-H, 2*H); 281 rect->fTop = rand.nextRangeScalar(-H, 2*H);
86 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W); 282 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W);
87 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H); 283 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H);
88 284
89 // we integralize rect to make our tests more predictable, since Gather is 285 // we integralize rect to make our tests more predictable, since Gather is
90 // a little sloppy. 286 // a little sloppy.
91 SkIRect ir; 287 SkIRect ir;
92 rect->round(&ir); 288 rect->round(&ir);
93 rect->set(ir); 289 rect->set(ir);
94 } 290 }
95 291
96 // Allocate result to be large enough to hold subset, and then draw the picture 292 static void draw(SkPicture* pic, int width, int height, SkBitmap* result) {
97 // into it, offsetting by subset's top/left corner. 293 make_bm(result, width, height, SK_ColorBLACK, false);
98 static void draw(SkPicture* pic, const SkRect& subset, SkBitmap* result) {
99 SkIRect ir;
100 subset.roundOut(&ir);
101 int w = ir.width();
102 int h = ir.height();
103 make_bm(result, w, h, 0, false);
104 294
105 SkCanvas canvas(*result); 295 SkCanvas canvas(*result);
106 canvas.translate(-SkIntToScalar(ir.left()), -SkIntToScalar(ir.top()));
107 canvas.drawPicture(*pic); 296 canvas.drawPicture(*pic);
108 } 297 }
109 298
110 template <typename T> int find_index(const T* array, T elem, int count) { 299 template <typename T> int find_index(const T* array, T elem, int count) {
111 for (int i = 0; i < count; ++i) { 300 for (int i = 0; i < count; ++i) {
112 if (array[i] == elem) { 301 if (array[i] == elem) {
113 return i; 302 return i;
114 } 303 }
115 } 304 }
116 return -1; 305 return -1;
117 } 306 }
118 307
119 // Return true if 'ref' is found in array[] 308 // Return true if 'ref' is found in array[]
120 static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int c ount) { 309 static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int c ount) {
121 return find_index<const SkPixelRef*>(array, ref, count) >= 0; 310 return find_index<const SkPixelRef*>(array, ref, count) >= 0;
122 } 311 }
123 312
124 // Look at each pixel in bm, and if its color appears in colors[], find the 313 // Look at each pixel that is inside 'subset', and if its color appears in
125 // corresponding value in refs[] and append that ref into array, skipping 314 // colors[], find the corresponding value in refs[] and append that ref into
126 // duplicates of the same value. 315 // array, skipping duplicates of the same value.
127 static void gather_from_colors(const SkBitmap& bm, SkPixelRef* const refs[], 316 // Note that gathering pixelRefs from rendered colors suffers from the problem
128 int count, SkTDArray<SkPixelRef*>* array) { 317 // that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color)
318 // isn't easy to reconstruct.
319 static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[],
320 int count, SkTDArray<SkPixelRef*>* array,
321 const SkRect& subset) {
322 SkIRect ir;
323 subset.roundOut(&ir);
324
325 if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) {
326 return;
327 }
328
129 // Since we only want to return unique values in array, when we scan we just 329 // Since we only want to return unique values in array, when we scan we just
130 // set a bit for each index'd color found. In practice we only have a few 330 // set a bit for each index'd color found. In practice we only have a few
131 // distinct colors, so we just use an int's bits as our array. Hence the 331 // distinct colors, so we just use an int's bits as our array. Hence the
132 // assert that count <= number-of-bits-in-our-int. 332 // assert that count <= number-of-bits-in-our-int.
133 SkASSERT((unsigned)count <= 32); 333 SkASSERT((unsigned)count <= 32);
134 uint32_t bitarray = 0; 334 uint32_t bitarray = 0;
135 335
136 SkAutoLockPixels alp(bm); 336 SkAutoLockPixels alp(bm);
137 337
138 for (int y = 0; y < bm.height(); ++y) { 338 for (int y = ir.fTop; y < ir.fBottom; ++y) {
139 for (int x = 0; x < bm.width(); ++x) { 339 for (int x = ir.fLeft; x < ir.fRight; ++x) {
140 SkPMColor pmc = *bm.getAddr32(x, y); 340 SkPMColor pmc = *bm.getAddr32(x, y);
141 // the only good case where the color is not found would be if 341 // the only good case where the color is not found would be if
142 // the color is transparent, meaning no bitmap was drawn in that 342 // the color is transparent, meaning no bitmap was drawn in that
143 // pixel. 343 // pixel.
144 if (pmc) { 344 if (pmc) {
145 uint32_t index = SkGetPackedR32(pmc); 345 uint32_t index = SkGetPackedR32(pmc);
146 SkASSERT(SkGetPackedG32(pmc) == index); 346 SkASSERT(SkGetPackedG32(pmc) == index);
147 SkASSERT(SkGetPackedB32(pmc) == index); 347 SkASSERT(SkGetPackedB32(pmc) == index);
348 if (0 == index) {
349 continue; // background color
350 }
351 SkASSERT(0 == (index - gColorOffset) % gColorScale);
352 index = (index - gColorOffset) / gColorScale;
148 SkASSERT(static_cast<int>(index) < count); 353 SkASSERT(static_cast<int>(index) < count);
149 bitarray |= 1 << index; 354 bitarray |= 1 << index;
150 } 355 }
151 } 356 }
152 } 357 }
153 358
154 for (int i = 0; i < count; ++i) { 359 for (int i = 0; i < count; ++i) {
155 if (bitarray & (1 << i)) { 360 if (bitarray & (1 << i)) {
156 *array->append() = refs[i]; 361 *array->append() = refs[i];
157 } 362 }
158 } 363 }
159 } 364 }
160 365
161 static void test_gatherpixelrefs(skiatest::Reporter* reporter) { 366 static void test_gatherpixelrefs(skiatest::Reporter* reporter) {
162 const int IW = 8; 367 const int IW = 32;
163 const int IH = IW; 368 const int IH = IW;
164 const SkScalar W = SkIntToScalar(IW); 369 const SkScalar W = SkIntToScalar(IW);
165 const SkScalar H = W; 370 const SkScalar H = W;
166 371
167 static const int N = 4; 372 static const int N = 4;
168 SkBitmap bm[N]; 373 SkBitmap bm[2*N];
169 SkPixelRef* refs[N]; 374 SkPixelRef* refs[2*N];
170 375
171 const SkPoint pos[] = { 376 const SkPoint pos[N] = {
172 { 0, 0 }, { W, 0 }, { 0, H }, { W, H } 377 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
173 }; 378 };
174 379
175 // Our convention is that the color components contain the index of their 380 // Our convention is that the color components contain an encoding of
176 // corresponding bitmap/pixelref 381 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is
382 // reserved for the background
177 for (int i = 0; i < N; ++i) { 383 for (int i = 0; i < N; ++i) {
178 make_bm(&bm[i], IW, IH, SkColorSetARGB(0xFF, i, i, i), true); 384 make_bm(&bm[i], IW, IH,
385 SkColorSetARGB(0xFF,
386 gColorScale*i+gColorOffset,
387 gColorScale*i+gColorOffset,
388 gColorScale*i+gColorOffset),
389 true);
179 refs[i] = bm[i].pixelRef(); 390 refs[i] = bm[i].pixelRef();
180 } 391 }
181 392
393 // The A8 alternate bitmaps are all BW checkerboards
394 for (int i = 0; i < N; ++i) {
395 make_checkerboard(&bm[N+i], IW, IH, true);
396 refs[N+i] = bm[N+i].pixelRef();
397 }
398
182 static const DrawBitmapProc procs[] = { 399 static const DrawBitmapProc procs[] = {
183 drawbitmap_proc, drawbitmaprect_proc, drawshader_proc 400 drawpaint_proc,
401 drawpoints_proc,
402 drawrect_proc,
403 drawoval_proc,
404 drawrrect_proc,
405 drawpath_proc,
406 drawbitmap_proc,
407 drawbitmap_withshader_proc,
408 drawsprite_proc,
409 #if 0
410 drawsprite_withshader_proc,
411 #endif
412 drawbitmaprect_proc,
413 drawbitmaprect_withshader_proc,
414 drawtext_proc,
415 drawpostext_proc,
416 drawtextonpath_proc,
417 drawverts_proc,
184 }; 418 };
185 419
186 SkRandom rand; 420 SkRandom rand;
187 for (size_t k = 0; k < SK_ARRAY_COUNT(procs); ++k) { 421 for (size_t k = 0; k < SK_ARRAY_COUNT(procs); ++k) {
188 SkAutoTUnref<SkPicture> pic(record_bitmaps(bm, pos, N, procs[k])); 422 SkAutoTUnref<SkPicture> pic(record_bitmaps(bm, pos, N, procs[k]));
189 423
190 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); 424 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
191 // quick check for a small piece of each quadrant, which should just 425 // quick check for a small piece of each quadrant, which should just
192 // contain 1 bitmap. 426 // contain 1 bitmap.
193 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { 427 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
194 SkRect r; 428 SkRect r;
195 r.set(2, 2, W - 2, H - 2); 429 r.set(2, 2, W - 2, H - 2);
196 r.offset(pos[i].fX, pos[i].fY); 430 r.offset(pos[i].fX, pos[i].fY);
197 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r)); 431 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r));
198 REPORTER_ASSERT(reporter, data); 432 REPORTER_ASSERT(reporter, data);
199 if (data) { 433 if (data) {
434 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data();
200 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*)) ; 435 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*)) ;
201 REPORTER_ASSERT(reporter, 1 == count); 436 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
202 REPORTER_ASSERT(reporter, *(SkPixelRef**)data->data() == refs[i] ); 437 if (1 == count) {
438 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
439 } else if (2 == count) {
440 REPORTER_ASSERT(reporter,
441 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i +N]) ||
442 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i +N]));
443 }
203 } 444 }
204 } 445 }
205 446
447 SkBitmap image;
448 draw(pic, 2*IW, 2*IH, &image);
449
206 // Test a bunch of random (mostly) rects, and compare the gather results 450 // Test a bunch of random (mostly) rects, and compare the gather results
207 // with a deduced list of refs by looking at the colors drawn. 451 // with a deduced list of refs by looking at the colors drawn.
208 for (int j = 0; j < 100; ++j) { 452 for (int j = 0; j < 100; ++j) {
209 SkRect r; 453 SkRect r;
210 rand_rect(&r, rand, 2*W, 2*H); 454 rand_rect(&r, rand, 2*W, 2*H);
211 455
212 SkBitmap result;
213 draw(pic, r, &result);
214 SkTDArray<SkPixelRef*> array; 456 SkTDArray<SkPixelRef*> array;
215 457
216 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); 458 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
217 size_t dataSize = data ? data->size() : 0; 459 size_t dataSize = data ? data->size() : 0;
218 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*)); 460 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*));
219 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize); 461 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize);
220 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL ; 462 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL ;
221 SkAutoDataUnref adu(data); 463 SkAutoDataUnref adu(data);
222 464
223 gather_from_colors(result, refs, N, &array); 465 gather_from_image(image, refs, N, &array, r);
224 466
225 /* 467 /*
226 * GatherPixelRefs is conservative, so it can return more bitmaps 468 * GatherPixelRefs is conservative, so it can return more bitmaps
227 * that we actually can see (usually because of conservative bounds 469 * that we actually can see (usually because of conservative bounds
228 * inflation for antialiasing). Thus our check here is only that 470 * inflation for antialiasing). Thus our check here is only that
229 * Gather didn't miss any that we actually saw. Even that isn't 471 * Gather didn't miss any that we actually saw. Even that isn't
230 * a strict requirement on Gather, which is meant to be quick and 472 * a strict requirement on Gather, which is meant to be quick and
231 * only mostly-correct, but at the moment this test should work. 473 * only mostly-correct, but at the moment this test should work.
232 */ 474 */
233 for (int i = 0; i < array.count(); ++i) { 475 for (int i = 0; i < array.count(); ++i) {
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 test_bad_bitmap(); 875 test_bad_bitmap();
634 #endif 876 #endif
635 test_peephole(); 877 test_peephole();
636 test_gatherpixelrefs(reporter); 878 test_gatherpixelrefs(reporter);
637 test_bitmap_with_encoded_data(reporter); 879 test_bitmap_with_encoded_data(reporter);
638 test_clone_empty(reporter); 880 test_clone_empty(reporter);
639 test_clip_bound_opt(reporter); 881 test_clip_bound_opt(reporter);
640 test_clip_expansion(reporter); 882 test_clip_expansion(reporter);
641 test_hierarchical(reporter); 883 test_hierarchical(reporter);
642 } 884 }
OLDNEW
« no previous file with comments | « src/utils/SkPictureUtils.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698