PictureTest.cpp revision 2e9a7157ee8ce47dae6e692162440c4f94a05574
1/* 2 * Copyright 2012 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8#include "Test.h" 9#include "TestClassDef.h" 10#include "SkBitmapDevice.h" 11#include "SkCanvas.h" 12#include "SkColorPriv.h" 13#include "SkData.h" 14#include "SkDecodingImageGenerator.h" 15#include "SkError.h" 16#include "SkImageEncoder.h" 17#include "SkImageGenerator.h" 18#include "SkPaint.h" 19#include "SkPicture.h" 20#include "SkPictureUtils.h" 21#include "SkRandom.h" 22#include "SkRRect.h" 23#include "SkShader.h" 24#include "SkStream.h" 25 26static const int gColorScale = 30; 27static const int gColorOffset = 60; 28 29static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) { 30 bm->setConfig(SkBitmap::kARGB_8888_Config, w, h); 31 bm->allocPixels(); 32 bm->eraseColor(color); 33 if (immutable) { 34 bm->setImmutable(); 35 } 36} 37 38static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) { 39 SkASSERT(w % 2 == 0); 40 SkASSERT(h % 2 == 0); 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 61static 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 68typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&, 69 const SkBitmap&, const SkPoint&, 70 SkTDArray<SkPixelRef*>* usedPixRefs); 71 72static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm, 73 const SkBitmap& altBM, const SkPoint& pos, 74 SkTDArray<SkPixelRef*>* usedPixRefs) { 75 SkPaint paint; 76 init_paint(&paint, bm); 77 78 canvas->drawPaint(paint); 79 *usedPixRefs->append() = bm.pixelRef(); 80} 81 82static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm, 83 const SkBitmap& altBM, const SkPoint& pos, 84 SkTDArray<SkPixelRef*>* usedPixRefs) { 85 SkPaint paint; 86 init_paint(&paint, bm); 87 88 // draw a rect 89 SkPoint points[5] = { 90 { pos.fX, pos.fY }, 91 { pos.fX + bm.width() - 1, pos.fY }, 92 { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 }, 93 { pos.fX, pos.fY + bm.height() - 1 }, 94 { pos.fX, pos.fY }, 95 }; 96 97 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint); 98 *usedPixRefs->append() = bm.pixelRef(); 99} 100 101static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm, 102 const SkBitmap& altBM, const SkPoint& pos, 103 SkTDArray<SkPixelRef*>* usedPixRefs) { 104 SkPaint paint; 105 init_paint(&paint, bm); 106 107 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 108 r.offset(pos.fX, pos.fY); 109 110 canvas->drawRect(r, paint); 111 *usedPixRefs->append() = bm.pixelRef(); 112} 113 114static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm, 115 const SkBitmap& altBM, const SkPoint& pos, 116 SkTDArray<SkPixelRef*>* usedPixRefs) { 117 SkPaint paint; 118 init_paint(&paint, bm); 119 120 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 121 r.offset(pos.fX, pos.fY); 122 123 canvas->drawOval(r, paint); 124 *usedPixRefs->append() = bm.pixelRef(); 125} 126 127static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm, 128 const SkBitmap& altBM, const SkPoint& pos, 129 SkTDArray<SkPixelRef*>* usedPixRefs) { 130 SkPaint paint; 131 init_paint(&paint, bm); 132 133 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 134 r.offset(pos.fX, pos.fY); 135 136 SkRRect rr; 137 rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4); 138 canvas->drawRRect(rr, paint); 139 *usedPixRefs->append() = bm.pixelRef(); 140} 141 142static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm, 143 const SkBitmap& altBM, const SkPoint& pos, 144 SkTDArray<SkPixelRef*>* usedPixRefs) { 145 SkPaint paint; 146 init_paint(&paint, bm); 147 148 SkPath path; 149 path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height())); 150 path.lineTo(SkIntToScalar(bm.width()), 0); 151 path.close(); 152 path.offset(pos.fX, pos.fY); 153 154 canvas->drawPath(path, paint); 155 *usedPixRefs->append() = bm.pixelRef(); 156} 157 158static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm, 159 const SkBitmap& altBM, const SkPoint& pos, 160 SkTDArray<SkPixelRef*>* usedPixRefs) { 161 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL); 162 *usedPixRefs->append() = bm.pixelRef(); 163} 164 165static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, 166 const SkBitmap& altBM, const SkPoint& pos, 167 SkTDArray<SkPixelRef*>* usedPixRefs) { 168 SkPaint paint; 169 init_paint(&paint, bm); 170 171 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap 172 canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint); 173 *usedPixRefs->append() = bm.pixelRef(); 174 *usedPixRefs->append() = altBM.pixelRef(); 175} 176 177static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm, 178 const SkBitmap& altBM, const SkPoint& pos, 179 SkTDArray<SkPixelRef*>* usedPixRefs) { 180 const SkMatrix& ctm = canvas->getTotalMatrix(); 181 182 SkPoint p(pos); 183 ctm.mapPoints(&p, 1); 184 185 canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL); 186 *usedPixRefs->append() = bm.pixelRef(); 187} 188 189#if 0 190// Although specifiable, this case doesn't seem to make sense (i.e., the 191// bitmap in the shader is never used). 192static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, 193 const SkBitmap& altBM, const SkPoint& pos, 194 SkTDArray<SkPixelRef*>* usedPixRefs) { 195 SkPaint paint; 196 init_paint(&paint, bm); 197 198 const SkMatrix& ctm = canvas->getTotalMatrix(); 199 200 SkPoint p(pos); 201 ctm.mapPoints(&p, 1); 202 203 canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint); 204 *usedPixRefs->append() = bm.pixelRef(); 205 *usedPixRefs->append() = altBM.pixelRef(); 206} 207#endif 208 209static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm, 210 const SkBitmap& altBM, const SkPoint& pos, 211 SkTDArray<SkPixelRef*>* usedPixRefs) { 212 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 213 214 r.offset(pos.fX, pos.fY); 215 canvas->drawBitmapRectToRect(bm, NULL, r, NULL); 216 *usedPixRefs->append() = bm.pixelRef(); 217} 218 219static void drawbitmaprect_withshader_proc(SkCanvas* canvas, 220 const SkBitmap& bm, 221 const SkBitmap& altBM, 222 const SkPoint& pos, 223 SkTDArray<SkPixelRef*>* usedPixRefs) { 224 SkPaint paint; 225 init_paint(&paint, bm); 226 227 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 228 r.offset(pos.fX, pos.fY); 229 230 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap 231 canvas->drawBitmapRectToRect(altBM, NULL, r, &paint); 232 *usedPixRefs->append() = bm.pixelRef(); 233 *usedPixRefs->append() = altBM.pixelRef(); 234} 235 236static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm, 237 const SkBitmap& altBM, const SkPoint& pos, 238 SkTDArray<SkPixelRef*>* usedPixRefs) { 239 SkPaint paint; 240 init_paint(&paint, bm); 241 paint.setTextSize(SkIntToScalar(1.5*bm.width())); 242 243 canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint); 244 *usedPixRefs->append() = bm.pixelRef(); 245} 246 247static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm, 248 const SkBitmap& altBM, const SkPoint& pos, 249 SkTDArray<SkPixelRef*>* usedPixRefs) { 250 SkPaint paint; 251 init_paint(&paint, bm); 252 paint.setTextSize(SkIntToScalar(1.5*bm.width())); 253 254 SkPoint point = { pos.fX, pos.fY + bm.height() }; 255 canvas->drawPosText("O", 1, &point, paint); 256 *usedPixRefs->append() = bm.pixelRef(); 257} 258 259static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm, 260 const SkBitmap& altBM, const SkPoint& pos, 261 SkTDArray<SkPixelRef*>* usedPixRefs) { 262 SkPaint paint; 263 264 init_paint(&paint, bm); 265 paint.setTextSize(SkIntToScalar(1.5*bm.width())); 266 267 SkPath path; 268 path.lineTo(SkIntToScalar(bm.width()), 0); 269 path.offset(pos.fX, pos.fY+bm.height()); 270 271 canvas->drawTextOnPath("O", 1, path, NULL, paint); 272 *usedPixRefs->append() = bm.pixelRef(); 273} 274 275static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm, 276 const SkBitmap& altBM, const SkPoint& pos, 277 SkTDArray<SkPixelRef*>* usedPixRefs) { 278 SkPaint paint; 279 init_paint(&paint, bm); 280 281 SkPoint verts[4] = { 282 { pos.fX, pos.fY }, 283 { pos.fX + bm.width(), pos.fY }, 284 { pos.fX + bm.width(), pos.fY + bm.height() }, 285 { pos.fX, pos.fY + bm.height() } 286 }; 287 SkPoint texs[4] = { { 0, 0 }, 288 { SkIntToScalar(bm.width()), 0 }, 289 { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }, 290 { 0, SkIntToScalar(bm.height()) } }; 291 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 }; 292 293 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL, 294 indices, 6, paint); 295 *usedPixRefs->append() = bm.pixelRef(); 296} 297 298// Return a picture with the bitmaps drawn at the specified positions. 299static SkPicture* record_bitmaps(const SkBitmap bm[], 300 const SkPoint pos[], 301 SkTDArray<SkPixelRef*> analytic[], 302 int count, 303 DrawBitmapProc proc) { 304 SkPicture* pic = new SkPicture; 305 SkCanvas* canvas = pic->beginRecording(1000, 1000); 306 for (int i = 0; i < count; ++i) { 307 analytic[i].rewind(); 308 canvas->save(); 309 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, 310 SkIntToScalar(bm[i].width()), 311 SkIntToScalar(bm[i].height())); 312 canvas->clipRect(clipRect, SkRegion::kIntersect_Op); 313 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]); 314 canvas->restore(); 315 } 316 pic->endRecording(); 317 return pic; 318} 319 320static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) { 321 rect->fLeft = rand.nextRangeScalar(-W, 2*W); 322 rect->fTop = rand.nextRangeScalar(-H, 2*H); 323 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W); 324 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H); 325 326 // we integralize rect to make our tests more predictable, since Gather is 327 // a little sloppy. 328 SkIRect ir; 329 rect->round(&ir); 330 rect->set(ir); 331} 332 333static void draw(SkPicture* pic, int width, int height, SkBitmap* result) { 334 make_bm(result, width, height, SK_ColorBLACK, false); 335 336 SkCanvas canvas(*result); 337 canvas.drawPicture(*pic); 338} 339 340template <typename T> int find_index(const T* array, T elem, int count) { 341 for (int i = 0; i < count; ++i) { 342 if (array[i] == elem) { 343 return i; 344 } 345 } 346 return -1; 347} 348 349// Return true if 'ref' is found in array[] 350static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) { 351 return find_index<const SkPixelRef*>(array, ref, count) >= 0; 352} 353 354// Look at each pixel that is inside 'subset', and if its color appears in 355// colors[], find the corresponding value in refs[] and append that ref into 356// array, skipping duplicates of the same value. 357// Note that gathering pixelRefs from rendered colors suffers from the problem 358// that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color) 359// isn't easy to reconstruct. 360static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[], 361 int count, SkTDArray<SkPixelRef*>* array, 362 const SkRect& subset) { 363 SkIRect ir; 364 subset.roundOut(&ir); 365 366 if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) { 367 return; 368 } 369 370 // Since we only want to return unique values in array, when we scan we just 371 // set a bit for each index'd color found. In practice we only have a few 372 // distinct colors, so we just use an int's bits as our array. Hence the 373 // assert that count <= number-of-bits-in-our-int. 374 SkASSERT((unsigned)count <= 32); 375 uint32_t bitarray = 0; 376 377 SkAutoLockPixels alp(bm); 378 379 for (int y = ir.fTop; y < ir.fBottom; ++y) { 380 for (int x = ir.fLeft; x < ir.fRight; ++x) { 381 SkPMColor pmc = *bm.getAddr32(x, y); 382 // the only good case where the color is not found would be if 383 // the color is transparent, meaning no bitmap was drawn in that 384 // pixel. 385 if (pmc) { 386 uint32_t index = SkGetPackedR32(pmc); 387 SkASSERT(SkGetPackedG32(pmc) == index); 388 SkASSERT(SkGetPackedB32(pmc) == index); 389 if (0 == index) { 390 continue; // background color 391 } 392 SkASSERT(0 == (index - gColorOffset) % gColorScale); 393 index = (index - gColorOffset) / gColorScale; 394 SkASSERT(static_cast<int>(index) < count); 395 bitarray |= 1 << index; 396 } 397 } 398 } 399 400 for (int i = 0; i < count; ++i) { 401 if (bitarray & (1 << i)) { 402 *array->append() = refs[i]; 403 } 404 } 405} 406 407static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h, 408 const SkTDArray<SkPixelRef*> analytic[], 409 int count, 410 SkTDArray<SkPixelRef*>* result, 411 const SkRect& subset) { 412 for (int i = 0; i < count; ++i) { 413 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h); 414 415 if (SkRect::Intersects(subset, rect)) { 416 result->append(analytic[i].count(), analytic[i].begin()); 417 } 418 } 419} 420 421static const DrawBitmapProc gProcs[] = { 422 drawpaint_proc, 423 drawpoints_proc, 424 drawrect_proc, 425 drawoval_proc, 426 drawrrect_proc, 427 drawpath_proc, 428 drawbitmap_proc, 429 drawbitmap_withshader_proc, 430 drawsprite_proc, 431#if 0 432 drawsprite_withshader_proc, 433#endif 434 drawbitmaprect_proc, 435 drawbitmaprect_withshader_proc, 436 drawtext_proc, 437 drawpostext_proc, 438 drawtextonpath_proc, 439 drawverts_proc, 440}; 441 442static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) { 443 // Our convention is that the color components contain an encoding of 444 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is 445 // reserved for the background 446 for (int i = 0; i < num; ++i) { 447 make_bm(&bm[i], w, h, 448 SkColorSetARGB(0xFF, 449 gColorScale*i+gColorOffset, 450 gColorScale*i+gColorOffset, 451 gColorScale*i+gColorOffset), 452 true); 453 refs[i] = bm[i].pixelRef(); 454 } 455 456 // The A8 alternate bitmaps are all BW checkerboards 457 for (int i = 0; i < num; ++i) { 458 make_checkerboard(&bm[num+i], w, h, true); 459 refs[num+i] = bm[num+i].pixelRef(); 460 } 461} 462 463static void test_gatherpixelrefs(skiatest::Reporter* reporter) { 464 const int IW = 32; 465 const int IH = IW; 466 const SkScalar W = SkIntToScalar(IW); 467 const SkScalar H = W; 468 469 static const int N = 4; 470 SkBitmap bm[2*N]; 471 SkPixelRef* refs[2*N]; 472 SkTDArray<SkPixelRef*> analytic[N]; 473 474 const SkPoint pos[N] = { 475 { 0, 0 }, { W, 0 }, { 0, H }, { W, H } 476 }; 477 478 create_textures(bm, refs, N, IW, IH); 479 480 SkRandom rand; 481 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { 482 SkAutoTUnref<SkPicture> pic(record_bitmaps(bm, pos, analytic, N, gProcs[k])); 483 484 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); 485 // quick check for a small piece of each quadrant, which should just 486 // contain 1 or 2 bitmaps. 487 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { 488 SkRect r; 489 r.set(2, 2, W - 2, H - 2); 490 r.offset(pos[i].fX, pos[i].fY); 491 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r)); 492 REPORTER_ASSERT(reporter, data); 493 if (data) { 494 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data(); 495 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*)); 496 REPORTER_ASSERT(reporter, 1 == count || 2 == count); 497 if (1 == count) { 498 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); 499 } else if (2 == count) { 500 REPORTER_ASSERT(reporter, 501 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || 502 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); 503 } 504 } 505 } 506 507 SkBitmap image; 508 draw(pic, 2*IW, 2*IH, &image); 509 510 // Test a bunch of random (mostly) rects, and compare the gather results 511 // with a deduced list of refs by looking at the colors drawn. 512 for (int j = 0; j < 100; ++j) { 513 SkRect r; 514 rand_rect(&r, rand, 2*W, 2*H); 515 516 SkTDArray<SkPixelRef*> fromImage; 517 gather_from_image(image, refs, N, &fromImage, r); 518 519 SkTDArray<SkPixelRef*> fromAnalytic; 520 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); 521 522 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); 523 size_t dataSize = data ? data->size() : 0; 524 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*)); 525 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize); 526 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL; 527 SkAutoDataUnref adu(data); 528 529 // Everything that we saw drawn should appear in the analytic list 530 // but the analytic list may contain some pixelRefs that were not 531 // seen in the image (e.g., A8 textures used as masks) 532 for (int i = 0; i < fromImage.count(); ++i) { 533 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i])); 534 } 535 536 /* 537 * GatherPixelRefs is conservative, so it can return more bitmaps 538 * than are strictly required. Thus our check here is only that 539 * Gather didn't miss any that we actually needed. Even that isn't 540 * a strict requirement on Gather, which is meant to be quick and 541 * only mostly-correct, but at the moment this test should work. 542 */ 543 for (int i = 0; i < fromAnalytic.count(); ++i) { 544 bool found = find(gatherRefs, fromAnalytic[i], gatherCount); 545 REPORTER_ASSERT(reporter, found); 546#if 0 547 // enable this block of code to debug failures, as it will rerun 548 // the case that failed. 549 if (!found) { 550 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); 551 size_t dataSize = data ? data->size() : 0; 552 } 553#endif 554 } 555 } 556 } 557} 558 559static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) { 560 const int IW = 32; 561 const int IH = IW; 562 const SkScalar W = SkIntToScalar(IW); 563 const SkScalar H = W; 564 565 static const int N = 4; 566 SkBitmap bm[2*N]; 567 SkPixelRef* refs[2*N]; 568 SkTDArray<SkPixelRef*> analytic[N]; 569 570 const SkPoint pos[N] = { 571 { 0, 0 }, { W, 0 }, { 0, H }, { W, H } 572 }; 573 574 create_textures(bm, refs, N, IW, IH); 575 576 SkRandom rand; 577 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { 578 SkAutoTUnref<SkPicture> pic(record_bitmaps(bm, pos, analytic, N, gProcs[k])); 579 580 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); 581 582 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont( 583 new SkPictureUtils::SkPixelRefsAndRectsList); 584 585 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont); 586 587 // quick check for a small piece of each quadrant, which should just 588 // contain 1 or 2 bitmaps. 589 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { 590 SkRect r; 591 r.set(2, 2, W - 2, H - 2); 592 r.offset(pos[i].fX, pos[i].fY); 593 594 SkTDArray<SkPixelRef*> gatheredRefs; 595 prCont->query(r, &gatheredRefs); 596 597 int count = gatheredRefs.count(); 598 REPORTER_ASSERT(reporter, 1 == count || 2 == count); 599 if (1 == count) { 600 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); 601 } else if (2 == count) { 602 REPORTER_ASSERT(reporter, 603 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || 604 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); 605 } 606 } 607 608 SkBitmap image; 609 draw(pic, 2*IW, 2*IH, &image); 610 611 // Test a bunch of random (mostly) rects, and compare the gather results 612 // with the analytic results and the pixel refs seen in a rendering. 613 for (int j = 0; j < 100; ++j) { 614 SkRect r; 615 rand_rect(&r, rand, 2*W, 2*H); 616 617 SkTDArray<SkPixelRef*> fromImage; 618 gather_from_image(image, refs, N, &fromImage, r); 619 620 SkTDArray<SkPixelRef*> fromAnalytic; 621 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); 622 623 SkTDArray<SkPixelRef*> gatheredRefs; 624 prCont->query(r, &gatheredRefs); 625 626 // Everything that we saw drawn should appear in the analytic list 627 // but the analytic list may contain some pixelRefs that were not 628 // seen in the image (e.g., A8 textures used as masks) 629 for (int i = 0; i < fromImage.count(); ++i) { 630 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i])); 631 } 632 633 // Everything in the analytic list should appear in the gathered 634 // list. 635 for (int i = 0; i < fromAnalytic.count(); ++i) { 636 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i])); 637 } 638 } 639 } 640} 641 642#ifdef SK_DEBUG 643// Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only 644// run in debug mode. 645static void test_deleting_empty_playback() { 646 SkPicture picture; 647 // Creates an SkPictureRecord 648 picture.beginRecording(0, 0); 649 // Turns that into an SkPicturePlayback 650 picture.endRecording(); 651 // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord 652 picture.beginRecording(0, 0); 653} 654 655// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. 656static void test_serializing_empty_picture() { 657 SkPicture picture; 658 picture.beginRecording(0, 0); 659 picture.endRecording(); 660 SkDynamicMemoryWStream stream; 661 picture.serialize(&stream); 662} 663#endif 664 665static void rand_op(SkCanvas* canvas, SkRandom& rand) { 666 SkPaint paint; 667 SkRect rect = SkRect::MakeWH(50, 50); 668 669 SkScalar unit = rand.nextUScalar1(); 670 if (unit <= 0.3) { 671// SkDebugf("save\n"); 672 canvas->save(); 673 } else if (unit <= 0.6) { 674// SkDebugf("restore\n"); 675 canvas->restore(); 676 } else if (unit <= 0.9) { 677// SkDebugf("clip\n"); 678 canvas->clipRect(rect); 679 } else { 680// SkDebugf("draw\n"); 681 canvas->drawPaint(paint); 682 } 683} 684 685static void test_peephole() { 686 SkRandom rand; 687 688 for (int j = 0; j < 100; j++) { 689 SkRandom rand2(rand); // remember the seed 690 691 SkPicture picture; 692 SkCanvas* canvas = picture.beginRecording(100, 100); 693 694 for (int i = 0; i < 1000; ++i) { 695 rand_op(canvas, rand); 696 } 697 picture.endRecording(); 698 699 rand = rand2; 700 } 701 702 { 703 SkPicture picture; 704 SkCanvas* canvas = picture.beginRecording(100, 100); 705 SkRect rect = SkRect::MakeWH(50, 50); 706 707 for (int i = 0; i < 100; ++i) { 708 canvas->save(); 709 } 710 while (canvas->getSaveCount() > 1) { 711 canvas->clipRect(rect); 712 canvas->restore(); 713 } 714 picture.endRecording(); 715 } 716} 717 718#ifndef SK_DEBUG 719// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller 720// should never do this. 721static void test_bad_bitmap() { 722 // This bitmap has a width and height but no pixels. As a result, attempting to record it will 723 // fail. 724 SkBitmap bm; 725 bm.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); 726 SkPicture picture; 727 SkCanvas* recordingCanvas = picture.beginRecording(100, 100); 728 recordingCanvas->drawBitmap(bm, 0, 0); 729 picture.endRecording(); 730 731 SkCanvas canvas; 732 canvas.drawPicture(picture); 733} 734#endif 735 736static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { 737 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); 738} 739 740static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { 741 SkPicture picture; 742 SkCanvas* canvas = picture.beginRecording(bitmap.width(), bitmap.height()); 743 canvas->drawBitmap(bitmap, 0, 0); 744 SkDynamicMemoryWStream wStream; 745 picture.serialize(&wStream, &encode_bitmap_to_data); 746 return wStream.copyToData(); 747} 748 749struct ErrorContext { 750 int fErrors; 751 skiatest::Reporter* fReporter; 752}; 753 754static void assert_one_parse_error_cb(SkError error, void* context) { 755 ErrorContext* errorContext = static_cast<ErrorContext*>(context); 756 errorContext->fErrors++; 757 // This test only expects one error, and that is a kParseError. If there are others, 758 // there is some unknown problem. 759 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors, 760 "This threw more errors than expected."); 761 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error, 762 SkGetLastErrorString()); 763} 764 765static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { 766 // Create a bitmap that will be encoded. 767 SkBitmap original; 768 make_bm(&original, 100, 100, SK_ColorBLUE, true); 769 SkDynamicMemoryWStream wStream; 770 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { 771 return; 772 } 773 SkAutoDataUnref data(wStream.copyToData()); 774 775 SkBitmap bm; 776 bool installSuccess = SkInstallDiscardablePixelRef( 777 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm, NULL); 778 REPORTER_ASSERT(reporter, installSuccess); 779 780 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same. 781 // Flattening original will follow the old path of performing an encode, while flattening bm 782 // will use the already encoded data. 783 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); 784 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); 785 REPORTER_ASSERT(reporter, picture1->equals(picture2)); 786 // Now test that a parse error was generated when trying to create a new SkPicture without 787 // providing a function to decode the bitmap. 788 ErrorContext context; 789 context.fErrors = 0; 790 context.fReporter = reporter; 791 SkSetErrorCallback(assert_one_parse_error_cb, &context); 792 SkMemoryStream pictureStream(picture1); 793 SkClearLastError(); 794 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL)); 795 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL); 796 SkClearLastError(); 797 SkSetErrorCallback(NULL, NULL); 798} 799 800static void test_clone_empty(skiatest::Reporter* reporter) { 801 // This is a regression test for crbug.com/172062 802 // Before the fix, we used to crash accessing a null pointer when we 803 // had a picture with no paints. This test passes by not crashing. 804 { 805 SkPicture picture; 806 picture.beginRecording(1, 1); 807 picture.endRecording(); 808 SkPicture* destPicture = picture.clone(); 809 REPORTER_ASSERT(reporter, NULL != destPicture); 810 destPicture->unref(); 811 } 812 { 813 // Test without call to endRecording 814 SkPicture picture; 815 picture.beginRecording(1, 1); 816 SkPicture* destPicture = picture.clone(); 817 REPORTER_ASSERT(reporter, NULL != destPicture); 818 destPicture->unref(); 819 } 820} 821 822static void test_clip_bound_opt(skiatest::Reporter* reporter) { 823 // Test for crbug.com/229011 824 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), 825 SkIntToScalar(2), SkIntToScalar(2)); 826 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), 827 SkIntToScalar(1), SkIntToScalar(1)); 828 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), 829 SkIntToScalar(1), SkIntToScalar(1)); 830 831 SkPath invPath; 832 invPath.addOval(rect1); 833 invPath.setFillType(SkPath::kInverseEvenOdd_FillType); 834 SkPath path; 835 path.addOval(rect2); 836 SkPath path2; 837 path2.addOval(rect3); 838 SkIRect clipBounds; 839 // Minimalist test set for 100% code coverage of 840 // SkPictureRecord::updateClipConservativelyUsingBounds 841 { 842 SkPicture picture; 843 SkCanvas* canvas = picture.beginRecording(10, 10, 844 SkPicture::kUsePathBoundsForClip_RecordingFlag); 845 canvas->clipPath(invPath, SkRegion::kIntersect_Op); 846 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 847 REPORTER_ASSERT(reporter, true == nonEmpty); 848 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); 849 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); 850 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); 851 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); 852 } 853 { 854 SkPicture picture; 855 SkCanvas* canvas = picture.beginRecording(10, 10, 856 SkPicture::kUsePathBoundsForClip_RecordingFlag); 857 canvas->clipPath(path, SkRegion::kIntersect_Op); 858 canvas->clipPath(invPath, SkRegion::kIntersect_Op); 859 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 860 REPORTER_ASSERT(reporter, true == nonEmpty); 861 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); 862 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); 863 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); 864 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); 865 } 866 { 867 SkPicture picture; 868 SkCanvas* canvas = picture.beginRecording(10, 10, 869 SkPicture::kUsePathBoundsForClip_RecordingFlag); 870 canvas->clipPath(path, SkRegion::kIntersect_Op); 871 canvas->clipPath(invPath, SkRegion::kUnion_Op); 872 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 873 REPORTER_ASSERT(reporter, true == nonEmpty); 874 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); 875 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); 876 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); 877 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); 878 } 879 { 880 SkPicture picture; 881 SkCanvas* canvas = picture.beginRecording(10, 10, 882 SkPicture::kUsePathBoundsForClip_RecordingFlag); 883 canvas->clipPath(path, SkRegion::kDifference_Op); 884 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 885 REPORTER_ASSERT(reporter, true == nonEmpty); 886 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); 887 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); 888 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); 889 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); 890 } 891 { 892 SkPicture picture; 893 SkCanvas* canvas = picture.beginRecording(10, 10, 894 SkPicture::kUsePathBoundsForClip_RecordingFlag); 895 canvas->clipPath(path, SkRegion::kReverseDifference_Op); 896 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 897 // True clip is actually empty in this case, but the best 898 // determination we can make using only bounds as input is that the 899 // clip is included in the bounds of 'path'. 900 REPORTER_ASSERT(reporter, true == nonEmpty); 901 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); 902 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); 903 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); 904 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); 905 } 906 { 907 SkPicture picture; 908 SkCanvas* canvas = picture.beginRecording(10, 10, 909 SkPicture::kUsePathBoundsForClip_RecordingFlag); 910 canvas->clipPath(path, SkRegion::kIntersect_Op); 911 canvas->clipPath(path2, SkRegion::kXOR_Op); 912 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 913 REPORTER_ASSERT(reporter, true == nonEmpty); 914 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft); 915 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop); 916 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); 917 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); 918 } 919} 920 921/** 922 * A canvas that records the number of clip commands. 923 */ 924class ClipCountingCanvas : public SkCanvas { 925public: 926 explicit ClipCountingCanvas(SkBaseDevice* device) 927 : SkCanvas(device) 928 , fClipCount(0){ 929 } 930 931 virtual bool clipRect(const SkRect& r, SkRegion::Op op, bool doAA) 932 SK_OVERRIDE { 933 fClipCount += 1; 934 return this->INHERITED::clipRect(r, op, doAA); 935 } 936 937 virtual bool clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) 938 SK_OVERRIDE { 939 fClipCount += 1; 940 return this->INHERITED::clipRRect(rrect, op, doAA); 941 } 942 943 virtual bool clipPath(const SkPath& path, SkRegion::Op op, bool doAA) 944 SK_OVERRIDE { 945 fClipCount += 1; 946 return this->INHERITED::clipPath(path, op, doAA); 947 } 948 949 unsigned getClipCount() const { return fClipCount; } 950 951private: 952 unsigned fClipCount; 953 954 typedef SkCanvas INHERITED; 955}; 956 957static void test_clip_expansion(skiatest::Reporter* reporter) { 958 SkPicture picture; 959 SkCanvas* canvas = picture.beginRecording(10, 10, 0); 960 961 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op); 962 // The following expanding clip should not be skipped. 963 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op); 964 // Draw something so the optimizer doesn't just fold the world. 965 SkPaint p; 966 p.setColor(SK_ColorBLUE); 967 canvas->drawPaint(p); 968 969 SkBitmapDevice testDevice(SkBitmap::kNo_Config, 10, 10); 970 ClipCountingCanvas testCanvas(&testDevice); 971 picture.draw(&testCanvas); 972 973 // Both clips should be present on playback. 974 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2); 975} 976 977static void test_hierarchical(skiatest::Reporter* reporter) { 978 SkBitmap bm; 979 make_bm(&bm, 10, 10, SK_ColorRED, true); 980 981 SkCanvas* canvas; 982 983 SkPicture childPlain; 984 childPlain.beginRecording(10, 10); 985 childPlain.endRecording(); 986 REPORTER_ASSERT(reporter, !childPlain.willPlayBackBitmaps()); // 0 987 988 SkPicture childWithBitmap; 989 childWithBitmap.beginRecording(10, 10)->drawBitmap(bm, 0, 0); 990 childWithBitmap.endRecording(); 991 REPORTER_ASSERT(reporter, childWithBitmap.willPlayBackBitmaps()); // 1 992 993 SkPicture parentPP; 994 canvas = parentPP.beginRecording(10, 10); 995 canvas->drawPicture(childPlain); 996 parentPP.endRecording(); 997 REPORTER_ASSERT(reporter, !parentPP.willPlayBackBitmaps()); // 0 998 999 SkPicture parentPWB; 1000 canvas = parentPWB.beginRecording(10, 10); 1001 canvas->drawPicture(childWithBitmap); 1002 parentPWB.endRecording(); 1003 REPORTER_ASSERT(reporter, parentPWB.willPlayBackBitmaps()); // 1 1004 1005 SkPicture parentWBP; 1006 canvas = parentWBP.beginRecording(10, 10); 1007 canvas->drawBitmap(bm, 0, 0); 1008 canvas->drawPicture(childPlain); 1009 parentWBP.endRecording(); 1010 REPORTER_ASSERT(reporter, parentWBP.willPlayBackBitmaps()); // 1 1011 1012 SkPicture parentWBWB; 1013 canvas = parentWBWB.beginRecording(10, 10); 1014 canvas->drawBitmap(bm, 0, 0); 1015 canvas->drawPicture(childWithBitmap); 1016 parentWBWB.endRecording(); 1017 REPORTER_ASSERT(reporter, parentWBWB.willPlayBackBitmaps()); // 2 1018} 1019 1020DEF_TEST(Picture, reporter) { 1021#ifdef SK_DEBUG 1022 test_deleting_empty_playback(); 1023 test_serializing_empty_picture(); 1024#else 1025 test_bad_bitmap(); 1026#endif 1027 test_peephole(); 1028 test_gatherpixelrefs(reporter); 1029 test_gatherpixelrefsandrects(reporter); 1030 test_bitmap_with_encoded_data(reporter); 1031 test_clone_empty(reporter); 1032 test_clip_bound_opt(reporter); 1033 test_clip_expansion(reporter); 1034 test_hierarchical(reporter); 1035} 1036