PictureTest.cpp revision 3e8232b6ef6b04290f0c280f88b38f8cd2c3f3ec
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 "SkBBoxHierarchy.h" 9#include "SkBlurImageFilter.h" 10#include "SkCanvas.h" 11#include "SkColorPriv.h" 12#include "SkDashPathEffect.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 "SkPictureRecorder.h" 21#include "SkPictureUtils.h" 22#include "SkRRect.h" 23#include "SkRandom.h" 24#include "SkShader.h" 25#include "SkStream.h" 26 27#if SK_SUPPORT_GPU 28#include "SkSurface.h" 29#include "GrContextFactory.h" 30#include "GrPictureUtils.h" 31#endif 32#include "Test.h" 33 34#include "SkLumaColorFilter.h" 35#include "SkColorFilterImageFilter.h" 36 37static const int gColorScale = 30; 38static const int gColorOffset = 60; 39 40static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) { 41 bm->allocN32Pixels(w, h); 42 bm->eraseColor(color); 43 if (immutable) { 44 bm->setImmutable(); 45 } 46} 47 48static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) { 49 SkASSERT(w % 2 == 0); 50 SkASSERT(h % 2 == 0); 51 bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType, 52 kPremul_SkAlphaType)); 53 SkAutoLockPixels lock(*bm); 54 for (int y = 0; y < h; y += 2) { 55 uint8_t* s = bm->getAddr8(0, y); 56 for (int x = 0; x < w; x += 2) { 57 *s++ = 0xFF; 58 *s++ = 0x00; 59 } 60 s = bm->getAddr8(0, y + 1); 61 for (int x = 0; x < w; x += 2) { 62 *s++ = 0x00; 63 *s++ = 0xFF; 64 } 65 } 66 if (immutable) { 67 bm->setImmutable(); 68 } 69} 70 71static void init_paint(SkPaint* paint, const SkBitmap &bm) { 72 SkShader* shader = SkShader::CreateBitmapShader(bm, 73 SkShader::kClamp_TileMode, 74 SkShader::kClamp_TileMode); 75 paint->setShader(shader)->unref(); 76} 77 78typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&, 79 const SkBitmap&, const SkPoint&, 80 SkTDArray<SkPixelRef*>* usedPixRefs); 81 82static void drawpaint_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 canvas->drawPaint(paint); 89 *usedPixRefs->append() = bm.pixelRef(); 90} 91 92static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm, 93 const SkBitmap& altBM, const SkPoint& pos, 94 SkTDArray<SkPixelRef*>* usedPixRefs) { 95 SkPaint paint; 96 init_paint(&paint, bm); 97 98 // draw a rect 99 SkPoint points[5] = { 100 { pos.fX, pos.fY }, 101 { pos.fX + bm.width() - 1, pos.fY }, 102 { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 }, 103 { pos.fX, pos.fY + bm.height() - 1 }, 104 { pos.fX, pos.fY }, 105 }; 106 107 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint); 108 *usedPixRefs->append() = bm.pixelRef(); 109} 110 111static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm, 112 const SkBitmap& altBM, const SkPoint& pos, 113 SkTDArray<SkPixelRef*>* usedPixRefs) { 114 SkPaint paint; 115 init_paint(&paint, bm); 116 117 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 118 r.offset(pos.fX, pos.fY); 119 120 canvas->drawRect(r, paint); 121 *usedPixRefs->append() = bm.pixelRef(); 122} 123 124static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm, 125 const SkBitmap& altBM, const SkPoint& pos, 126 SkTDArray<SkPixelRef*>* usedPixRefs) { 127 SkPaint paint; 128 init_paint(&paint, bm); 129 130 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 131 r.offset(pos.fX, pos.fY); 132 133 canvas->drawOval(r, paint); 134 *usedPixRefs->append() = bm.pixelRef(); 135} 136 137static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm, 138 const SkBitmap& altBM, const SkPoint& pos, 139 SkTDArray<SkPixelRef*>* usedPixRefs) { 140 SkPaint paint; 141 init_paint(&paint, bm); 142 143 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 144 r.offset(pos.fX, pos.fY); 145 146 SkRRect rr; 147 rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4); 148 canvas->drawRRect(rr, paint); 149 *usedPixRefs->append() = bm.pixelRef(); 150} 151 152static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm, 153 const SkBitmap& altBM, const SkPoint& pos, 154 SkTDArray<SkPixelRef*>* usedPixRefs) { 155 SkPaint paint; 156 init_paint(&paint, bm); 157 158 SkPath path; 159 path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height())); 160 path.lineTo(SkIntToScalar(bm.width()), 0); 161 path.close(); 162 path.offset(pos.fX, pos.fY); 163 164 canvas->drawPath(path, paint); 165 *usedPixRefs->append() = bm.pixelRef(); 166} 167 168static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm, 169 const SkBitmap& altBM, const SkPoint& pos, 170 SkTDArray<SkPixelRef*>* usedPixRefs) { 171 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL); 172 *usedPixRefs->append() = bm.pixelRef(); 173} 174 175static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, 176 const SkBitmap& altBM, const SkPoint& pos, 177 SkTDArray<SkPixelRef*>* usedPixRefs) { 178 SkPaint paint; 179 init_paint(&paint, bm); 180 181 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap 182 canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint); 183 *usedPixRefs->append() = bm.pixelRef(); 184 *usedPixRefs->append() = altBM.pixelRef(); 185} 186 187static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm, 188 const SkBitmap& altBM, const SkPoint& pos, 189 SkTDArray<SkPixelRef*>* usedPixRefs) { 190 const SkMatrix& ctm = canvas->getTotalMatrix(); 191 192 SkPoint p(pos); 193 ctm.mapPoints(&p, 1); 194 195 canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL); 196 *usedPixRefs->append() = bm.pixelRef(); 197} 198 199#if 0 200// Although specifiable, this case doesn't seem to make sense (i.e., the 201// bitmap in the shader is never used). 202static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, 203 const SkBitmap& altBM, const SkPoint& pos, 204 SkTDArray<SkPixelRef*>* usedPixRefs) { 205 SkPaint paint; 206 init_paint(&paint, bm); 207 208 const SkMatrix& ctm = canvas->getTotalMatrix(); 209 210 SkPoint p(pos); 211 ctm.mapPoints(&p, 1); 212 213 canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint); 214 *usedPixRefs->append() = bm.pixelRef(); 215 *usedPixRefs->append() = altBM.pixelRef(); 216} 217#endif 218 219static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm, 220 const SkBitmap& altBM, const SkPoint& pos, 221 SkTDArray<SkPixelRef*>* usedPixRefs) { 222 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 223 224 r.offset(pos.fX, pos.fY); 225 canvas->drawBitmapRectToRect(bm, NULL, r, NULL); 226 *usedPixRefs->append() = bm.pixelRef(); 227} 228 229static void drawbitmaprect_withshader_proc(SkCanvas* canvas, 230 const SkBitmap& bm, 231 const SkBitmap& altBM, 232 const SkPoint& pos, 233 SkTDArray<SkPixelRef*>* usedPixRefs) { 234 SkPaint paint; 235 init_paint(&paint, bm); 236 237 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 238 r.offset(pos.fX, pos.fY); 239 240 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap 241 canvas->drawBitmapRectToRect(altBM, NULL, r, &paint); 242 *usedPixRefs->append() = bm.pixelRef(); 243 *usedPixRefs->append() = altBM.pixelRef(); 244} 245 246static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm, 247 const SkBitmap& altBM, const SkPoint& pos, 248 SkTDArray<SkPixelRef*>* usedPixRefs) { 249 SkPaint paint; 250 init_paint(&paint, bm); 251 paint.setTextSize(SkIntToScalar(1.5*bm.width())); 252 253 canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint); 254 *usedPixRefs->append() = bm.pixelRef(); 255} 256 257static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm, 258 const SkBitmap& altBM, const SkPoint& pos, 259 SkTDArray<SkPixelRef*>* usedPixRefs) { 260 SkPaint paint; 261 init_paint(&paint, bm); 262 paint.setTextSize(SkIntToScalar(1.5*bm.width())); 263 264 SkPoint point = { pos.fX, pos.fY + bm.height() }; 265 canvas->drawPosText("O", 1, &point, paint); 266 *usedPixRefs->append() = bm.pixelRef(); 267} 268 269static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm, 270 const SkBitmap& altBM, const SkPoint& pos, 271 SkTDArray<SkPixelRef*>* usedPixRefs) { 272 SkPaint paint; 273 274 init_paint(&paint, bm); 275 paint.setTextSize(SkIntToScalar(1.5*bm.width())); 276 277 SkPath path; 278 path.lineTo(SkIntToScalar(bm.width()), 0); 279 path.offset(pos.fX, pos.fY+bm.height()); 280 281 canvas->drawTextOnPath("O", 1, path, NULL, paint); 282 *usedPixRefs->append() = bm.pixelRef(); 283} 284 285static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm, 286 const SkBitmap& altBM, const SkPoint& pos, 287 SkTDArray<SkPixelRef*>* usedPixRefs) { 288 SkPaint paint; 289 init_paint(&paint, bm); 290 291 SkPoint verts[4] = { 292 { pos.fX, pos.fY }, 293 { pos.fX + bm.width(), pos.fY }, 294 { pos.fX + bm.width(), pos.fY + bm.height() }, 295 { pos.fX, pos.fY + bm.height() } 296 }; 297 SkPoint texs[4] = { { 0, 0 }, 298 { SkIntToScalar(bm.width()), 0 }, 299 { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }, 300 { 0, SkIntToScalar(bm.height()) } }; 301 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 }; 302 303 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL, 304 indices, 6, paint); 305 *usedPixRefs->append() = bm.pixelRef(); 306} 307 308// Return a picture with the bitmaps drawn at the specified positions. 309static SkPicture* record_bitmaps(const SkBitmap bm[], 310 const SkPoint pos[], 311 SkTDArray<SkPixelRef*> analytic[], 312 int count, 313 DrawBitmapProc proc) { 314 SkPictureRecorder recorder; 315 SkCanvas* canvas = recorder.beginRecording(1000, 1000); 316 for (int i = 0; i < count; ++i) { 317 analytic[i].rewind(); 318 canvas->save(); 319 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, 320 SkIntToScalar(bm[i].width()), 321 SkIntToScalar(bm[i].height())); 322 canvas->clipRect(clipRect, SkRegion::kIntersect_Op); 323 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]); 324 canvas->restore(); 325 } 326 return recorder.endRecording(); 327} 328 329static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) { 330 rect->fLeft = rand.nextRangeScalar(-W, 2*W); 331 rect->fTop = rand.nextRangeScalar(-H, 2*H); 332 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W); 333 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H); 334 335 // we integralize rect to make our tests more predictable, since Gather is 336 // a little sloppy. 337 SkIRect ir; 338 rect->round(&ir); 339 rect->set(ir); 340} 341 342static void draw(SkPicture* pic, int width, int height, SkBitmap* result) { 343 make_bm(result, width, height, SK_ColorBLACK, false); 344 345 SkCanvas canvas(*result); 346 canvas.drawPicture(pic); 347} 348 349template <typename T> int find_index(const T* array, T elem, int count) { 350 for (int i = 0; i < count; ++i) { 351 if (array[i] == elem) { 352 return i; 353 } 354 } 355 return -1; 356} 357 358// Return true if 'ref' is found in array[] 359static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) { 360 return find_index<const SkPixelRef*>(array, ref, count) >= 0; 361} 362 363// Look at each pixel that is inside 'subset', and if its color appears in 364// colors[], find the corresponding value in refs[] and append that ref into 365// array, skipping duplicates of the same value. 366// Note that gathering pixelRefs from rendered colors suffers from the problem 367// that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color) 368// isn't easy to reconstruct. 369static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[], 370 int count, SkTDArray<SkPixelRef*>* array, 371 const SkRect& subset) { 372 SkIRect ir; 373 subset.roundOut(&ir); 374 375 if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) { 376 return; 377 } 378 379 // Since we only want to return unique values in array, when we scan we just 380 // set a bit for each index'd color found. In practice we only have a few 381 // distinct colors, so we just use an int's bits as our array. Hence the 382 // assert that count <= number-of-bits-in-our-int. 383 SkASSERT((unsigned)count <= 32); 384 uint32_t bitarray = 0; 385 386 SkAutoLockPixels alp(bm); 387 388 for (int y = ir.fTop; y < ir.fBottom; ++y) { 389 for (int x = ir.fLeft; x < ir.fRight; ++x) { 390 SkPMColor pmc = *bm.getAddr32(x, y); 391 // the only good case where the color is not found would be if 392 // the color is transparent, meaning no bitmap was drawn in that 393 // pixel. 394 if (pmc) { 395 uint32_t index = SkGetPackedR32(pmc); 396 SkASSERT(SkGetPackedG32(pmc) == index); 397 SkASSERT(SkGetPackedB32(pmc) == index); 398 if (0 == index) { 399 continue; // background color 400 } 401 SkASSERT(0 == (index - gColorOffset) % gColorScale); 402 index = (index - gColorOffset) / gColorScale; 403 SkASSERT(static_cast<int>(index) < count); 404 bitarray |= 1 << index; 405 } 406 } 407 } 408 409 for (int i = 0; i < count; ++i) { 410 if (bitarray & (1 << i)) { 411 *array->append() = refs[i]; 412 } 413 } 414} 415 416static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h, 417 const SkTDArray<SkPixelRef*> analytic[], 418 int count, 419 SkTDArray<SkPixelRef*>* result, 420 const SkRect& subset) { 421 for (int i = 0; i < count; ++i) { 422 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h); 423 424 if (SkRect::Intersects(subset, rect)) { 425 result->append(analytic[i].count(), analytic[i].begin()); 426 } 427 } 428} 429 430 431static const struct { 432 const DrawBitmapProc proc; 433 const char* const desc; 434} gProcs[] = { 435 {drawpaint_proc, "drawpaint"}, 436 {drawpoints_proc, "drawpoints"}, 437 {drawrect_proc, "drawrect"}, 438 {drawoval_proc, "drawoval"}, 439 {drawrrect_proc, "drawrrect"}, 440 {drawpath_proc, "drawpath"}, 441 {drawbitmap_proc, "drawbitmap"}, 442 {drawbitmap_withshader_proc, "drawbitmap_withshader"}, 443 {drawsprite_proc, "drawsprite"}, 444#if 0 445 {drawsprite_withshader_proc, "drawsprite_withshader"}, 446#endif 447 {drawbitmaprect_proc, "drawbitmaprect"}, 448 {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"}, 449 {drawtext_proc, "drawtext"}, 450 {drawpostext_proc, "drawpostext"}, 451 {drawtextonpath_proc, "drawtextonpath"}, 452 {drawverts_proc, "drawverts"}, 453}; 454 455static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) { 456 // Our convention is that the color components contain an encoding of 457 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is 458 // reserved for the background 459 for (int i = 0; i < num; ++i) { 460 make_bm(&bm[i], w, h, 461 SkColorSetARGB(0xFF, 462 gColorScale*i+gColorOffset, 463 gColorScale*i+gColorOffset, 464 gColorScale*i+gColorOffset), 465 true); 466 refs[i] = bm[i].pixelRef(); 467 } 468 469 // The A8 alternate bitmaps are all BW checkerboards 470 for (int i = 0; i < num; ++i) { 471 make_checkerboard(&bm[num+i], w, h, true); 472 refs[num+i] = bm[num+i].pixelRef(); 473 } 474} 475 476static void test_gatherpixelrefs(skiatest::Reporter* reporter) { 477 const int IW = 32; 478 const int IH = IW; 479 const SkScalar W = SkIntToScalar(IW); 480 const SkScalar H = W; 481 482 static const int N = 4; 483 SkBitmap bm[2*N]; 484 SkPixelRef* refs[2*N]; 485 SkTDArray<SkPixelRef*> analytic[N]; 486 487 const SkPoint pos[N] = { 488 { 0, 0 }, { W, 0 }, { 0, H }, { W, H } 489 }; 490 491 create_textures(bm, refs, N, IW, IH); 492 493 SkRandom rand; 494 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { 495 SkAutoTUnref<SkPicture> pic( 496 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); 497 498 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); 499 // quick check for a small piece of each quadrant, which should just 500 // contain 1 or 2 bitmaps. 501 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { 502 SkRect r; 503 r.set(2, 2, W - 2, H - 2); 504 r.offset(pos[i].fX, pos[i].fY); 505 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r)); 506 if (!data) { 507 ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned " 508 "NULL for %s.", gProcs[k].desc); 509 continue; 510 } 511 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data(); 512 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*)); 513 REPORTER_ASSERT(reporter, 1 == count || 2 == count); 514 if (1 == count) { 515 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); 516 } else if (2 == count) { 517 REPORTER_ASSERT(reporter, 518 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || 519 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); 520 } 521 } 522 523 SkBitmap image; 524 draw(pic, 2*IW, 2*IH, &image); 525 526 // Test a bunch of random (mostly) rects, and compare the gather results 527 // with a deduced list of refs by looking at the colors drawn. 528 for (int j = 0; j < 100; ++j) { 529 SkRect r; 530 rand_rect(&r, rand, 2*W, 2*H); 531 532 SkTDArray<SkPixelRef*> fromImage; 533 gather_from_image(image, refs, N, &fromImage, r); 534 535 SkTDArray<SkPixelRef*> fromAnalytic; 536 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); 537 538 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); 539 size_t dataSize = data ? data->size() : 0; 540 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*)); 541 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize); 542 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL; 543 SkAutoDataUnref adu(data); 544 545 // Everything that we saw drawn should appear in the analytic list 546 // but the analytic list may contain some pixelRefs that were not 547 // seen in the image (e.g., A8 textures used as masks) 548 for (int i = 0; i < fromImage.count(); ++i) { 549 if (-1 == fromAnalytic.find(fromImage[i])) { 550 ERRORF(reporter, "PixelRef missing %d %s", 551 i, gProcs[k].desc); 552 } 553 } 554 555 /* 556 * GatherPixelRefs is conservative, so it can return more bitmaps 557 * than are strictly required. Thus our check here is only that 558 * Gather didn't miss any that we actually needed. Even that isn't 559 * a strict requirement on Gather, which is meant to be quick and 560 * only mostly-correct, but at the moment this test should work. 561 */ 562 for (int i = 0; i < fromAnalytic.count(); ++i) { 563 bool found = find(gatherRefs, fromAnalytic[i], gatherCount); 564 if (!found) { 565 ERRORF(reporter, "PixelRef missing %d %s", 566 i, gProcs[k].desc); 567 } 568#if 0 569 // enable this block of code to debug failures, as it will rerun 570 // the case that failed. 571 if (!found) { 572 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); 573 size_t dataSize = data ? data->size() : 0; 574 } 575#endif 576 } 577 } 578 } 579} 580 581static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) { 582 const int IW = 32; 583 const int IH = IW; 584 const SkScalar W = SkIntToScalar(IW); 585 const SkScalar H = W; 586 587 static const int N = 4; 588 SkBitmap bm[2*N]; 589 SkPixelRef* refs[2*N]; 590 SkTDArray<SkPixelRef*> analytic[N]; 591 592 const SkPoint pos[N] = { 593 { 0, 0 }, { W, 0 }, { 0, H }, { W, H } 594 }; 595 596 create_textures(bm, refs, N, IW, IH); 597 598 SkRandom rand; 599 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { 600 SkAutoTUnref<SkPicture> pic( 601 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); 602 603 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); 604 605 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont( 606 new SkPictureUtils::SkPixelRefsAndRectsList); 607 608 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont); 609 610 // quick check for a small piece of each quadrant, which should just 611 // contain 1 or 2 bitmaps. 612 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { 613 SkRect r; 614 r.set(2, 2, W - 2, H - 2); 615 r.offset(pos[i].fX, pos[i].fY); 616 617 SkTDArray<SkPixelRef*> gatheredRefs; 618 prCont->query(r, &gatheredRefs); 619 620 int count = gatheredRefs.count(); 621 REPORTER_ASSERT(reporter, 1 == count || 2 == count); 622 if (1 == count) { 623 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); 624 } else if (2 == count) { 625 REPORTER_ASSERT(reporter, 626 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || 627 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); 628 } 629 } 630 631 SkBitmap image; 632 draw(pic, 2*IW, 2*IH, &image); 633 634 // Test a bunch of random (mostly) rects, and compare the gather results 635 // with the analytic results and the pixel refs seen in a rendering. 636 for (int j = 0; j < 100; ++j) { 637 SkRect r; 638 rand_rect(&r, rand, 2*W, 2*H); 639 640 SkTDArray<SkPixelRef*> fromImage; 641 gather_from_image(image, refs, N, &fromImage, r); 642 643 SkTDArray<SkPixelRef*> fromAnalytic; 644 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); 645 646 SkTDArray<SkPixelRef*> gatheredRefs; 647 prCont->query(r, &gatheredRefs); 648 649 // Everything that we saw drawn should appear in the analytic list 650 // but the analytic list may contain some pixelRefs that were not 651 // seen in the image (e.g., A8 textures used as masks) 652 for (int i = 0; i < fromImage.count(); ++i) { 653 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i])); 654 } 655 656 // Everything in the analytic list should appear in the gathered 657 // list. 658 for (int i = 0; i < fromAnalytic.count(); ++i) { 659 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i])); 660 } 661 } 662 } 663} 664 665#ifdef SK_DEBUG 666// Ensure that deleting an empty SkPicture does not assert. Asserts only fire 667// in debug mode, so only run in debug mode. 668static void test_deleting_empty_picture() { 669 SkPictureRecorder recorder; 670 // Creates an SkPictureRecord 671 recorder.beginRecording(0, 0); 672 // Turns that into an SkPicture 673 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 674 // Ceates a new SkPictureRecord 675 recorder.beginRecording(0, 0); 676} 677 678// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. 679static void test_serializing_empty_picture() { 680 SkPictureRecorder recorder; 681 recorder.beginRecording(0, 0); 682 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 683 SkDynamicMemoryWStream stream; 684 picture->serialize(&stream); 685} 686#endif 687 688static void rand_op(SkCanvas* canvas, SkRandom& rand) { 689 SkPaint paint; 690 SkRect rect = SkRect::MakeWH(50, 50); 691 692 SkScalar unit = rand.nextUScalar1(); 693 if (unit <= 0.3) { 694// SkDebugf("save\n"); 695 canvas->save(); 696 } else if (unit <= 0.6) { 697// SkDebugf("restore\n"); 698 canvas->restore(); 699 } else if (unit <= 0.9) { 700// SkDebugf("clip\n"); 701 canvas->clipRect(rect); 702 } else { 703// SkDebugf("draw\n"); 704 canvas->drawPaint(paint); 705 } 706} 707 708#if SK_SUPPORT_GPU 709static void test_gpu_veto(skiatest::Reporter* reporter) { 710 711 SkPictureRecorder recorder; 712 713 SkCanvas* canvas = recorder.beginRecording(100, 100); 714 { 715 SkPath path; 716 path.moveTo(0, 0); 717 path.lineTo(50, 50); 718 719 SkScalar intervals[] = { 1.0f, 1.0f }; 720 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0)); 721 722 SkPaint paint; 723 paint.setStyle(SkPaint::kStroke_Style); 724 paint.setPathEffect(dash); 725 726 canvas->drawPath(path, paint); 727 } 728 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 729 // path effects currently render an SkPicture undesireable for GPU rendering 730 731 const char *reason = NULL; 732 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL, &reason)); 733 REPORTER_ASSERT(reporter, NULL != reason); 734 735 canvas = recorder.beginRecording(100, 100); 736 { 737 SkPath path; 738 739 path.moveTo(0, 0); 740 path.lineTo(0, 50); 741 path.lineTo(25, 25); 742 path.lineTo(50, 50); 743 path.lineTo(50, 0); 744 path.close(); 745 REPORTER_ASSERT(reporter, !path.isConvex()); 746 747 SkPaint paint; 748 paint.setAntiAlias(true); 749 for (int i = 0; i < 50; ++i) { 750 canvas->drawPath(path, paint); 751 } 752 } 753 picture.reset(recorder.endRecording()); 754 // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering 755 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL)); 756 757 canvas = recorder.beginRecording(100, 100); 758 { 759 SkPath path; 760 761 path.moveTo(0, 0); 762 path.lineTo(0, 50); 763 path.lineTo(25, 25); 764 path.lineTo(50, 50); 765 path.lineTo(50, 0); 766 path.close(); 767 REPORTER_ASSERT(reporter, !path.isConvex()); 768 769 SkPaint paint; 770 paint.setAntiAlias(true); 771 paint.setStyle(SkPaint::kStroke_Style); 772 paint.setStrokeWidth(0); 773 for (int i = 0; i < 50; ++i) { 774 canvas->drawPath(path, paint); 775 } 776 } 777 picture.reset(recorder.endRecording()); 778 // hairline stroked AA concave paths are fine for GPU rendering 779 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL)); 780} 781 782static void test_gpu_picture_optimization(skiatest::Reporter* reporter, 783 GrContextFactory* factory) { 784 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { 785 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; 786 787 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { 788 continue; 789 } 790 791 GrContext* context = factory->get(glCtxType); 792 793 if (NULL == context) { 794 continue; 795 } 796 797 static const int kWidth = 100; 798 static const int kHeight = 100; 799 800 SkAutoTUnref<SkPicture> pict; 801 802 // create a picture with the structure: 803 // 1) 804 // SaveLayer 805 // Restore 806 // 2) 807 // SaveLayer 808 // Translate 809 // SaveLayer w/ bound 810 // Restore 811 // Restore 812 // 3) 813 // SaveLayer w/ copyable paint 814 // Restore 815 // 4) 816 // SaveLayer w/ non-copyable paint 817 // Restore 818 { 819 SkPictureRecorder recorder; 820 821 SkCanvas* c = recorder.beginRecording(kWidth, kHeight); 822 // 1) 823 c->saveLayer(NULL, NULL); 824 c->restore(); 825 826 // 2) 827 c->saveLayer(NULL, NULL); 828 c->translate(kWidth/2, kHeight/2); 829 SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2); 830 c->saveLayer(&r, NULL); 831 c->restore(); 832 c->restore(); 833 834 // 3) 835 { 836 SkPaint p; 837 p.setColor(SK_ColorRED); 838 c->saveLayer(NULL, &p); 839 c->restore(); 840 } 841 // 4) 842 // TODO: this case will need to be removed once the paint's are immutable 843 { 844 SkPaint p; 845 SkAutoTUnref<SkColorFilter> cf(SkLumaColorFilter::Create()); 846 p.setImageFilter(SkColorFilterImageFilter::Create(cf.get()))->unref(); 847 c->saveLayer(NULL, &p); 848 c->restore(); 849 } 850 851 pict.reset(recorder.endRecording()); 852 } 853 854 // Now test out the SaveLayer extraction 855 { 856 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); 857 858 SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info)); 859 860 SkCanvas* canvas = surface->getCanvas(); 861 862 canvas->EXPERIMENTAL_optimize(pict); 863 864 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); 865 866 const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key); 867 REPORTER_ASSERT(reporter, NULL != data); 868 869 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data); 870 REPORTER_ASSERT(reporter, 5 == gpuData->numSaveLayers()); 871 872 const GrAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0); 873 // The parent/child layer appear in reverse order 874 const GrAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2); 875 const GrAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1); 876 const GrAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3); 877// const GrAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(4); 878 879 REPORTER_ASSERT(reporter, info0.fValid); 880 REPORTER_ASSERT(reporter, kWidth == info0.fSize.fWidth && kHeight == info0.fSize.fHeight); 881 REPORTER_ASSERT(reporter, info0.fCTM.isIdentity()); 882 REPORTER_ASSERT(reporter, 0 == info0.fOffset.fX && 0 == info0.fOffset.fY); 883 REPORTER_ASSERT(reporter, NULL != info0.fPaint); 884 REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers); 885 886 REPORTER_ASSERT(reporter, info1.fValid); 887 REPORTER_ASSERT(reporter, kWidth == info1.fSize.fWidth && kHeight == info1.fSize.fHeight); 888 REPORTER_ASSERT(reporter, info1.fCTM.isIdentity()); 889 REPORTER_ASSERT(reporter, 0 == info1.fOffset.fX && 0 == info1.fOffset.fY); 890 REPORTER_ASSERT(reporter, NULL != info1.fPaint); 891 REPORTER_ASSERT(reporter, !info1.fIsNested && info1.fHasNestedLayers); // has a nested SL 892 893 REPORTER_ASSERT(reporter, info2.fValid); 894 REPORTER_ASSERT(reporter, kWidth/2 == info2.fSize.fWidth && 895 kHeight/2 == info2.fSize.fHeight); // bound reduces size 896 REPORTER_ASSERT(reporter, info2.fCTM.isIdentity()); // translated 897 REPORTER_ASSERT(reporter, kWidth/2 == info2.fOffset.fX && 898 kHeight/2 == info2.fOffset.fY); 899 REPORTER_ASSERT(reporter, NULL != info1.fPaint); 900 REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested 901 902 REPORTER_ASSERT(reporter, info3.fValid); 903 REPORTER_ASSERT(reporter, kWidth == info3.fSize.fWidth && kHeight == info3.fSize.fHeight); 904 REPORTER_ASSERT(reporter, info3.fCTM.isIdentity()); 905 REPORTER_ASSERT(reporter, 0 == info3.fOffset.fX && 0 == info3.fOffset.fY); 906 REPORTER_ASSERT(reporter, NULL != info3.fPaint); 907 REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers); 908 909 #if 0 // needs more though for GrGatherCanvas 910 REPORTER_ASSERT(reporter, !info4.fValid); // paint is/was uncopyable 911 REPORTER_ASSERT(reporter, kWidth == info4.fSize.fWidth && kHeight == info4.fSize.fHeight); 912 REPORTER_ASSERT(reporter, 0 == info4.fOffset.fX && 0 == info4.fOffset.fY); 913 REPORTER_ASSERT(reporter, info4.fCTM.isIdentity()); 914 REPORTER_ASSERT(reporter, NULL == info4.fPaint); // paint is/was uncopyable 915 REPORTER_ASSERT(reporter, !info4.fIsNested && !info4.fHasNestedLayers); 916 #endif 917 } 918 } 919} 920 921#endif 922 923static void test_has_text(skiatest::Reporter* reporter) { 924 SkPictureRecorder recorder; 925 SkPaint paint; 926 paint.setColor(SK_ColorBLUE); 927 SkPoint point = SkPoint::Make(10, 10); 928 929 SkCanvas* canvas = recorder.beginRecording(100, 100); 930 { 931 canvas->drawRect(SkRect::MakeWH(20, 20), paint); 932 } 933 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 934 REPORTER_ASSERT(reporter, !picture->hasText()); 935 936 canvas = recorder.beginRecording(100, 100); 937 { 938 canvas->drawText("Q", 1, point.fX, point.fY, paint); 939 } 940 picture.reset(recorder.endRecording()); 941 REPORTER_ASSERT(reporter, picture->hasText()); 942 943 canvas = recorder.beginRecording(100, 100); 944 { 945 canvas->drawPosText("Q", 1, &point, paint); 946 } 947 picture.reset(recorder.endRecording()); 948 REPORTER_ASSERT(reporter, picture->hasText()); 949 950 canvas = recorder.beginRecording(100, 100); 951 { 952 canvas->drawPosTextH("Q", 1, &point.fX, point.fY, paint); 953 } 954 picture.reset(recorder.endRecording()); 955 REPORTER_ASSERT(reporter, picture->hasText()); 956 957 canvas = recorder.beginRecording(100, 100); 958 { 959 SkPath path; 960 path.moveTo(0, 0); 961 path.lineTo(50, 50); 962 963 canvas->drawTextOnPathHV("Q", 1, path, point.fX, point.fY, paint); 964 } 965 picture.reset(recorder.endRecording()); 966 REPORTER_ASSERT(reporter, picture->hasText()); 967 968 canvas = recorder.beginRecording(100, 100); 969 { 970 SkPath path; 971 path.moveTo(0, 0); 972 path.lineTo(50, 50); 973 974 canvas->drawTextOnPath("Q", 1, path, NULL, paint); 975 } 976 picture.reset(recorder.endRecording()); 977 REPORTER_ASSERT(reporter, picture->hasText()); 978} 979 980static void set_canvas_to_save_count_4(SkCanvas* canvas) { 981 canvas->restoreToCount(1); 982 canvas->save(); 983 canvas->save(); 984 canvas->save(); 985} 986 987/** 988 * A canvas that records the number of saves, saveLayers and restores. 989 */ 990class SaveCountingCanvas : public SkCanvas { 991public: 992 SaveCountingCanvas(int width, int height) 993 : INHERITED(width, height) 994 , fSaveCount(0) 995 , fSaveLayerCount(0) 996 , fRestoreCount(0){ 997 } 998 999 virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint, 1000 SaveFlags flags) SK_OVERRIDE { 1001 ++fSaveLayerCount; 1002 return this->INHERITED::willSaveLayer(bounds, paint, flags); 1003 } 1004 1005 virtual void willSave() SK_OVERRIDE { 1006 ++fSaveCount; 1007 this->INHERITED::willSave(); 1008 } 1009 1010 virtual void willRestore() SK_OVERRIDE { 1011 ++fRestoreCount; 1012 this->INHERITED::willRestore(); 1013 } 1014 1015 unsigned int getSaveCount() const { return fSaveCount; } 1016 unsigned int getSaveLayerCount() const { return fSaveLayerCount; } 1017 unsigned int getRestoreCount() const { return fRestoreCount; } 1018 1019private: 1020 unsigned int fSaveCount; 1021 unsigned int fSaveLayerCount; 1022 unsigned int fRestoreCount; 1023 1024 typedef SkCanvas INHERITED; 1025}; 1026 1027void check_save_state(skiatest::Reporter* reporter, SkPicture* picture, 1028 unsigned int numSaves, unsigned int numSaveLayers, 1029 unsigned int numRestores) { 1030 SaveCountingCanvas canvas(picture->width(), picture->height()); 1031 1032 picture->draw(&canvas); 1033 1034 REPORTER_ASSERT(reporter, numSaves == canvas.getSaveCount()); 1035 REPORTER_ASSERT(reporter, numSaveLayers == canvas.getSaveLayerCount()); 1036 REPORTER_ASSERT(reporter, numRestores == canvas.getRestoreCount()); 1037} 1038 1039// This class exists so SkPicture can friend it and give it access to 1040// the 'partialReplay' method. 1041class SkPictureRecorderReplayTester { 1042public: 1043 static SkPicture* Copy(SkPictureRecorder* recorder) { 1044 SkPictureRecorder recorder2; 1045 1046 SkCanvas* canvas = recorder2.beginRecording(10, 10); 1047 1048 recorder->partialReplay(canvas); 1049 1050 return recorder2.endRecording(); 1051 } 1052}; 1053 1054static void create_imbalance(SkCanvas* canvas) { 1055 SkRect clipRect = SkRect::MakeWH(2, 2); 1056 SkRect drawRect = SkRect::MakeWH(10, 10); 1057 canvas->save(); 1058 canvas->clipRect(clipRect, SkRegion::kReplace_Op); 1059 canvas->translate(1.0f, 1.0f); 1060 SkPaint p; 1061 p.setColor(SK_ColorGREEN); 1062 canvas->drawRect(drawRect, p); 1063 // no restore 1064} 1065 1066// This tests that replaying a potentially unbalanced picture into a canvas 1067// doesn't affect the canvas' save count or matrix/clip state. 1068static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) { 1069 SkBitmap bm; 1070 bm.allocN32Pixels(4, 3); 1071 SkCanvas canvas(bm); 1072 1073 int beforeSaveCount = canvas.getSaveCount(); 1074 1075 SkMatrix beforeMatrix = canvas.getTotalMatrix(); 1076 1077 SkRect beforeClip; 1078 1079 canvas.getClipBounds(&beforeClip); 1080 1081 canvas.drawPicture(picture); 1082 1083 REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount()); 1084 REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix()); 1085 1086 SkRect afterClip; 1087 1088 canvas.getClipBounds(&afterClip); 1089 1090 REPORTER_ASSERT(reporter, afterClip == beforeClip); 1091} 1092 1093// Test out SkPictureRecorder::partialReplay 1094DEF_TEST(PictureRecorder_replay, reporter) { 1095 // check save/saveLayer state 1096 { 1097 SkPictureRecorder recorder; 1098 1099 SkCanvas* canvas = recorder.beginRecording(10, 10); 1100 1101 canvas->saveLayer(NULL, NULL); 1102 1103 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); 1104 1105 // The extra save and restore comes from the Copy process. 1106 check_save_state(reporter, copy, 2, 1, 3); 1107 1108 canvas->saveLayer(NULL, NULL); 1109 1110 SkAutoTUnref<SkPicture> final(recorder.endRecording()); 1111 1112 check_save_state(reporter, final, 1, 2, 3); 1113 1114 // The copy shouldn't pick up any operations added after it was made 1115 check_save_state(reporter, copy, 2, 1, 3); 1116 } 1117 1118 // (partially) check leakage of draw ops 1119 { 1120 SkPictureRecorder recorder; 1121 1122 SkCanvas* canvas = recorder.beginRecording(10, 10); 1123 1124 SkRect r = SkRect::MakeWH(5, 5); 1125 SkPaint p; 1126 1127 canvas->drawRect(r, p); 1128 1129 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); 1130 1131 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); 1132 1133 SkBitmap bm; 1134 make_bm(&bm, 10, 10, SK_ColorRED, true); 1135 1136 r.offset(5.0f, 5.0f); 1137 canvas->drawBitmapRectToRect(bm, NULL, r); 1138 1139 SkAutoTUnref<SkPicture> final(recorder.endRecording()); 1140 REPORTER_ASSERT(reporter, final->willPlayBackBitmaps()); 1141 1142 REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID()); 1143 1144 // The snapshot shouldn't pick up any operations added after it was made 1145 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); 1146 } 1147 1148 // Recreate the Android partialReplay test case 1149 { 1150 SkPictureRecorder recorder; 1151 1152 SkCanvas* canvas = recorder.beginRecording(4, 3, NULL, 0); 1153 create_imbalance(canvas); 1154 1155 int expectedSaveCount = canvas->getSaveCount(); 1156 1157 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); 1158 check_balance(reporter, copy); 1159 1160 REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount()); 1161 1162 // End the recording of source to test the picture finalization 1163 // process isn't complicated by the partialReplay step 1164 SkAutoTUnref<SkPicture> final(recorder.endRecording()); 1165 } 1166} 1167 1168static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { 1169 SkCanvas testCanvas(100, 100); 1170 set_canvas_to_save_count_4(&testCanvas); 1171 1172 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); 1173 1174 SkPaint paint; 1175 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); 1176 1177 SkPictureRecorder recorder; 1178 1179 { 1180 // Create picture with 2 unbalanced saves 1181 SkCanvas* canvas = recorder.beginRecording(100, 100); 1182 canvas->save(); 1183 canvas->translate(10, 10); 1184 canvas->drawRect(rect, paint); 1185 canvas->save(); 1186 canvas->translate(10, 10); 1187 canvas->drawRect(rect, paint); 1188 SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording()); 1189 1190 testCanvas.drawPicture(extraSavePicture); 1191 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); 1192 } 1193 1194 set_canvas_to_save_count_4(&testCanvas); 1195 1196 { 1197 // Create picture with 2 unbalanced restores 1198 SkCanvas* canvas = recorder.beginRecording(100, 100); 1199 canvas->save(); 1200 canvas->translate(10, 10); 1201 canvas->drawRect(rect, paint); 1202 canvas->save(); 1203 canvas->translate(10, 10); 1204 canvas->drawRect(rect, paint); 1205 canvas->restore(); 1206 canvas->restore(); 1207 canvas->restore(); 1208 canvas->restore(); 1209 SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording()); 1210 1211 testCanvas.drawPicture(extraRestorePicture); 1212 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); 1213 } 1214 1215 set_canvas_to_save_count_4(&testCanvas); 1216 1217 { 1218 SkCanvas* canvas = recorder.beginRecording(100, 100); 1219 canvas->translate(10, 10); 1220 canvas->drawRect(rect, paint); 1221 SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording()); 1222 1223 testCanvas.drawPicture(noSavePicture); 1224 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); 1225 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity()); 1226 } 1227} 1228 1229static void test_peephole() { 1230 SkRandom rand; 1231 1232 SkPictureRecorder recorder; 1233 1234 for (int j = 0; j < 100; j++) { 1235 SkRandom rand2(rand); // remember the seed 1236 1237 SkCanvas* canvas = recorder.beginRecording(100, 100); 1238 1239 for (int i = 0; i < 1000; ++i) { 1240 rand_op(canvas, rand); 1241 } 1242 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1243 1244 rand = rand2; 1245 } 1246 1247 { 1248 SkCanvas* canvas = recorder.beginRecording(100, 100); 1249 SkRect rect = SkRect::MakeWH(50, 50); 1250 1251 for (int i = 0; i < 100; ++i) { 1252 canvas->save(); 1253 } 1254 while (canvas->getSaveCount() > 1) { 1255 canvas->clipRect(rect); 1256 canvas->restore(); 1257 } 1258 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1259 } 1260} 1261 1262#ifndef SK_DEBUG 1263// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller 1264// should never do this. 1265static void test_bad_bitmap() { 1266 // This bitmap has a width and height but no pixels. As a result, attempting to record it will 1267 // fail. 1268 SkBitmap bm; 1269 bm.setInfo(SkImageInfo::MakeN32Premul(100, 100)); 1270 SkPictureRecorder recorder; 1271 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100); 1272 recordingCanvas->drawBitmap(bm, 0, 0); 1273 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1274 1275 SkCanvas canvas; 1276 canvas.drawPicture(picture); 1277} 1278#endif 1279 1280static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { 1281 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); 1282} 1283 1284static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { 1285 SkPictureRecorder recorder; 1286 SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height()); 1287 canvas->drawBitmap(bitmap, 0, 0); 1288 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1289 1290 SkDynamicMemoryWStream wStream; 1291 picture->serialize(&wStream, &encode_bitmap_to_data); 1292 return wStream.copyToData(); 1293} 1294 1295struct ErrorContext { 1296 int fErrors; 1297 skiatest::Reporter* fReporter; 1298}; 1299 1300static void assert_one_parse_error_cb(SkError error, void* context) { 1301 ErrorContext* errorContext = static_cast<ErrorContext*>(context); 1302 errorContext->fErrors++; 1303 // This test only expects one error, and that is a kParseError. If there are others, 1304 // there is some unknown problem. 1305 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors, 1306 "This threw more errors than expected."); 1307 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error, 1308 SkGetLastErrorString()); 1309} 1310 1311static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { 1312 // Create a bitmap that will be encoded. 1313 SkBitmap original; 1314 make_bm(&original, 100, 100, SK_ColorBLUE, true); 1315 SkDynamicMemoryWStream wStream; 1316 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { 1317 return; 1318 } 1319 SkAutoDataUnref data(wStream.copyToData()); 1320 1321 SkBitmap bm; 1322 bool installSuccess = SkInstallDiscardablePixelRef( 1323 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm); 1324 REPORTER_ASSERT(reporter, installSuccess); 1325 1326 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same. 1327 // Flattening original will follow the old path of performing an encode, while flattening bm 1328 // will use the already encoded data. 1329 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); 1330 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); 1331 REPORTER_ASSERT(reporter, picture1->equals(picture2)); 1332 // Now test that a parse error was generated when trying to create a new SkPicture without 1333 // providing a function to decode the bitmap. 1334 ErrorContext context; 1335 context.fErrors = 0; 1336 context.fReporter = reporter; 1337 SkSetErrorCallback(assert_one_parse_error_cb, &context); 1338 SkMemoryStream pictureStream(picture1); 1339 SkClearLastError(); 1340 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL)); 1341 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL); 1342 SkClearLastError(); 1343 SkSetErrorCallback(NULL, NULL); 1344} 1345 1346static void test_draw_empty(skiatest::Reporter* reporter) { 1347 SkBitmap result; 1348 make_bm(&result, 2, 2, SK_ColorBLACK, false); 1349 1350 SkCanvas canvas(result); 1351 1352 { 1353 // stock SkPicture 1354 SkPictureRecorder recorder; 1355 recorder.beginRecording(1, 1); 1356 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1357 1358 canvas.drawPicture(picture); 1359 } 1360 1361 { 1362 // tile grid 1363 SkTileGridFactory::TileGridInfo gridInfo; 1364 gridInfo.fMargin.setEmpty(); 1365 gridInfo.fOffset.setZero(); 1366 gridInfo.fTileInterval.set(1, 1); 1367 1368 SkTileGridFactory factory(gridInfo); 1369 SkPictureRecorder recorder; 1370 recorder.beginRecording(1, 1, &factory); 1371 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1372 1373 canvas.drawPicture(picture); 1374 } 1375 1376 { 1377 // RTree 1378 SkRTreeFactory factory; 1379 SkPictureRecorder recorder; 1380 recorder.beginRecording(1, 1, &factory); 1381 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1382 1383 canvas.drawPicture(picture); 1384 } 1385 1386 { 1387 // quad tree 1388 SkQuadTreeFactory factory; 1389 SkPictureRecorder recorder; 1390 recorder.beginRecording(1, 1, &factory); 1391 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1392 1393 canvas.drawPicture(picture); 1394 } 1395} 1396 1397static void test_clip_bound_opt(skiatest::Reporter* reporter) { 1398 // Test for crbug.com/229011 1399 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), 1400 SkIntToScalar(2), SkIntToScalar(2)); 1401 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), 1402 SkIntToScalar(1), SkIntToScalar(1)); 1403 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), 1404 SkIntToScalar(1), SkIntToScalar(1)); 1405 1406 SkPath invPath; 1407 invPath.addOval(rect1); 1408 invPath.setFillType(SkPath::kInverseEvenOdd_FillType); 1409 SkPath path; 1410 path.addOval(rect2); 1411 SkPath path2; 1412 path2.addOval(rect3); 1413 SkIRect clipBounds; 1414 SkPictureRecorder recorder; 1415 // Minimalist test set for 100% code coverage of 1416 // SkPictureRecord::updateClipConservativelyUsingBounds 1417 { 1418 SkCanvas* canvas = recorder.beginRecording(10, 10); 1419 canvas->clipPath(invPath, SkRegion::kIntersect_Op); 1420 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1421 REPORTER_ASSERT(reporter, true == nonEmpty); 1422 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); 1423 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); 1424 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); 1425 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); 1426 } 1427 { 1428 SkCanvas* canvas = recorder.beginRecording(10, 10); 1429 canvas->clipPath(path, SkRegion::kIntersect_Op); 1430 canvas->clipPath(invPath, SkRegion::kIntersect_Op); 1431 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1432 REPORTER_ASSERT(reporter, true == nonEmpty); 1433 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); 1434 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); 1435 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); 1436 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); 1437 } 1438 { 1439 SkCanvas* canvas = recorder.beginRecording(10, 10); 1440 canvas->clipPath(path, SkRegion::kIntersect_Op); 1441 canvas->clipPath(invPath, SkRegion::kUnion_Op); 1442 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1443 REPORTER_ASSERT(reporter, true == nonEmpty); 1444 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); 1445 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); 1446 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); 1447 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); 1448 } 1449 { 1450 SkCanvas* canvas = recorder.beginRecording(10, 10); 1451 canvas->clipPath(path, SkRegion::kDifference_Op); 1452 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1453 REPORTER_ASSERT(reporter, true == nonEmpty); 1454 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); 1455 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); 1456 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); 1457 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); 1458 } 1459 { 1460 SkCanvas* canvas = recorder.beginRecording(10, 10); 1461 canvas->clipPath(path, SkRegion::kReverseDifference_Op); 1462 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1463 // True clip is actually empty in this case, but the best 1464 // determination we can make using only bounds as input is that the 1465 // clip is included in the bounds of 'path'. 1466 REPORTER_ASSERT(reporter, true == nonEmpty); 1467 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); 1468 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); 1469 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); 1470 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); 1471 } 1472 { 1473 SkCanvas* canvas = recorder.beginRecording(10, 10); 1474 canvas->clipPath(path, SkRegion::kIntersect_Op); 1475 canvas->clipPath(path2, SkRegion::kXOR_Op); 1476 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1477 REPORTER_ASSERT(reporter, true == nonEmpty); 1478 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft); 1479 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop); 1480 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); 1481 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); 1482 } 1483} 1484 1485/** 1486 * A canvas that records the number of clip commands. 1487 */ 1488class ClipCountingCanvas : public SkCanvas { 1489public: 1490 ClipCountingCanvas(int width, int height) 1491 : INHERITED(width, height) 1492 , fClipCount(0){ 1493 } 1494 1495 virtual void onClipRect(const SkRect& r, 1496 SkRegion::Op op, 1497 ClipEdgeStyle edgeStyle) SK_OVERRIDE { 1498 fClipCount += 1; 1499 this->INHERITED::onClipRect(r, op, edgeStyle); 1500 } 1501 1502 virtual void onClipRRect(const SkRRect& rrect, 1503 SkRegion::Op op, 1504 ClipEdgeStyle edgeStyle)SK_OVERRIDE { 1505 fClipCount += 1; 1506 this->INHERITED::onClipRRect(rrect, op, edgeStyle); 1507 } 1508 1509 virtual void onClipPath(const SkPath& path, 1510 SkRegion::Op op, 1511 ClipEdgeStyle edgeStyle) SK_OVERRIDE { 1512 fClipCount += 1; 1513 this->INHERITED::onClipPath(path, op, edgeStyle); 1514 } 1515 1516 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE { 1517 fClipCount += 1; 1518 this->INHERITED::onClipRegion(deviceRgn, op); 1519 } 1520 1521 unsigned getClipCount() const { return fClipCount; } 1522 1523private: 1524 unsigned fClipCount; 1525 1526 typedef SkCanvas INHERITED; 1527}; 1528 1529static void test_clip_expansion(skiatest::Reporter* reporter) { 1530 SkPictureRecorder recorder; 1531 SkCanvas* canvas = recorder.beginRecording(10, 10); 1532 1533 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op); 1534 // The following expanding clip should not be skipped. 1535 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op); 1536 // Draw something so the optimizer doesn't just fold the world. 1537 SkPaint p; 1538 p.setColor(SK_ColorBLUE); 1539 canvas->drawPaint(p); 1540 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1541 1542 ClipCountingCanvas testCanvas(10, 10); 1543 picture->draw(&testCanvas); 1544 1545 // Both clips should be present on playback. 1546 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2); 1547} 1548 1549static void test_hierarchical(skiatest::Reporter* reporter) { 1550 SkBitmap bm; 1551 make_bm(&bm, 10, 10, SK_ColorRED, true); 1552 1553 SkPictureRecorder recorder; 1554 1555 recorder.beginRecording(10, 10); 1556 SkAutoTUnref<SkPicture> childPlain(recorder.endRecording()); 1557 REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0 1558 1559 recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0); 1560 SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording()); 1561 REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1 1562 1563 { 1564 SkCanvas* canvas = recorder.beginRecording(10, 10); 1565 canvas->drawPicture(childPlain); 1566 SkAutoTUnref<SkPicture> parentPP(recorder.endRecording()); 1567 REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0 1568 } 1569 { 1570 SkCanvas* canvas = recorder.beginRecording(10, 10); 1571 canvas->drawPicture(childWithBitmap); 1572 SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording()); 1573 REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1 1574 } 1575 { 1576 SkCanvas* canvas = recorder.beginRecording(10, 10); 1577 canvas->drawBitmap(bm, 0, 0); 1578 canvas->drawPicture(childPlain); 1579 SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording()); 1580 REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1 1581 } 1582 { 1583 SkCanvas* canvas = recorder.beginRecording(10, 10); 1584 canvas->drawBitmap(bm, 0, 0); 1585 canvas->drawPicture(childWithBitmap); 1586 SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording()); 1587 REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2 1588 } 1589} 1590 1591static void test_gen_id(skiatest::Reporter* reporter) { 1592 1593 SkPictureRecorder recorder; 1594 recorder.beginRecording(0, 0); 1595 SkAutoTUnref<SkPicture> empty(recorder.endRecording()); 1596 1597 // Empty pictures should still have a valid ID 1598 REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID); 1599 1600 SkCanvas* canvas = recorder.beginRecording(1, 1); 1601 canvas->drawARGB(255, 255, 255, 255); 1602 SkAutoTUnref<SkPicture> hasData(recorder.endRecording()); 1603 // picture should have a non-zero id after recording 1604 REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID); 1605 1606 // both pictures should have different ids 1607 REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID()); 1608} 1609 1610DEF_TEST(Picture, reporter) { 1611#ifdef SK_DEBUG 1612 test_deleting_empty_picture(); 1613 test_serializing_empty_picture(); 1614#else 1615 test_bad_bitmap(); 1616#endif 1617 test_unbalanced_save_restores(reporter); 1618 test_peephole(); 1619#if SK_SUPPORT_GPU 1620 test_gpu_veto(reporter); 1621#endif 1622 test_has_text(reporter); 1623 test_gatherpixelrefs(reporter); 1624 test_gatherpixelrefsandrects(reporter); 1625 test_bitmap_with_encoded_data(reporter); 1626 test_draw_empty(reporter); 1627 test_clip_bound_opt(reporter); 1628 test_clip_expansion(reporter); 1629 test_hierarchical(reporter); 1630 test_gen_id(reporter); 1631} 1632 1633#if SK_SUPPORT_GPU 1634DEF_GPUTEST(GPUPicture, reporter, factory) { 1635 test_gpu_picture_optimization(reporter, factory); 1636} 1637#endif 1638 1639static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) { 1640 const SkPaint paint; 1641 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f }; 1642 const SkIRect irect = { 2, 2, 3, 3 }; 1643 1644 // Don't care what these record, as long as they're legal. 1645 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint); 1646 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag); 1647 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint); 1648 canvas->drawBitmapNine(bitmap, irect, rect, &paint); 1649 canvas->drawSprite(bitmap, 1, 1); 1650} 1651 1652static void test_draw_bitmaps(SkCanvas* canvas) { 1653 SkBitmap empty; 1654 draw_bitmaps(empty, canvas); 1655 empty.setInfo(SkImageInfo::MakeN32Premul(10, 10)); 1656 draw_bitmaps(empty, canvas); 1657} 1658 1659DEF_TEST(Picture_EmptyBitmap, r) { 1660 SkPictureRecorder recorder; 1661 test_draw_bitmaps(recorder.beginRecording(10, 10)); 1662 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1663} 1664 1665DEF_TEST(Canvas_EmptyBitmap, r) { 1666 SkBitmap dst; 1667 dst.allocN32Pixels(10, 10); 1668 SkCanvas canvas(dst); 1669 1670 test_draw_bitmaps(&canvas); 1671} 1672 1673DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) { 1674 // This test is from crbug.com/344987. 1675 // The commands are: 1676 // saveLayer with paint that modifies alpha 1677 // drawBitmapRectToRect 1678 // drawBitmapRectToRect 1679 // restore 1680 // The bug was that this structure was modified so that: 1681 // - The saveLayer and restore were eliminated 1682 // - The alpha was only applied to the first drawBitmapRectToRect 1683 1684 // This test draws blue and red squares inside a 50% transparent 1685 // layer. Both colours should show up muted. 1686 // When the bug is present, the red square (the second bitmap) 1687 // shows upwith full opacity. 1688 1689 SkBitmap blueBM; 1690 make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true); 1691 SkBitmap redBM; 1692 make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true); 1693 SkPaint semiTransparent; 1694 semiTransparent.setAlpha(0x80); 1695 1696 SkPictureRecorder recorder; 1697 SkCanvas* canvas = recorder.beginRecording(100, 100); 1698 canvas->drawARGB(0, 0, 0, 0); 1699 1700 canvas->saveLayer(0, &semiTransparent); 1701 canvas->drawBitmap(blueBM, 25, 25); 1702 canvas->drawBitmap(redBM, 50, 50); 1703 canvas->restore(); 1704 1705 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1706 1707 // Now replay the picture back on another canvas 1708 // and check a couple of its pixels. 1709 SkBitmap replayBM; 1710 make_bm(&replayBM, 100, 100, SK_ColorBLACK, false); 1711 SkCanvas replayCanvas(replayBM); 1712 picture->draw(&replayCanvas); 1713 replayCanvas.flush(); 1714 1715 // With the bug present, at (55, 55) we would get a fully opaque red 1716 // intead of a dark red. 1717 REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080); 1718 REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000); 1719} 1720 1721struct CountingBBH : public SkBBoxHierarchy { 1722 mutable int searchCalls; 1723 1724 CountingBBH() : searchCalls(0) {} 1725 1726 virtual void search(const SkIRect& query, SkTDArray<void*>* results) const { 1727 this->searchCalls++; 1728 } 1729 1730 // All other methods unimplemented. 1731 virtual void insert(void* data, const SkIRect& bounds, bool defer) {} 1732 virtual void flushDeferredInserts() {} 1733 virtual void clear() {} 1734 virtual int getCount() const { return 0; } 1735 virtual int getDepth() const { return 0; } 1736 virtual void rewindInserts() {} 1737}; 1738 1739class SpoonFedBBHFactory : public SkBBHFactory { 1740public: 1741 explicit SpoonFedBBHFactory(SkBBoxHierarchy* bbh) : fBBH(bbh) {} 1742 virtual SkBBoxHierarchy* operator()(int width, int height) const { 1743 return SkRef(fBBH); 1744 } 1745private: 1746 SkBBoxHierarchy* fBBH; 1747}; 1748 1749// When the canvas clip covers the full picture, we don't need to call the BBH. 1750DEF_TEST(Picture_SkipBBH, r) { 1751 CountingBBH bbh; 1752 SpoonFedBBHFactory factory(&bbh); 1753 1754 SkPictureRecorder recorder; 1755 recorder.beginRecording(320, 240, &factory); 1756 SkAutoTUnref<const SkPicture> picture(recorder.endRecording()); 1757 1758 SkCanvas big(640, 480), small(300, 200); 1759 1760 picture->draw(&big); 1761 REPORTER_ASSERT(r, bbh.searchCalls == 0); 1762 1763 picture->draw(&small); 1764 REPORTER_ASSERT(r, bbh.searchCalls == 1); 1765} 1766