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