2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkBBoxHierarchy.h"
9 #include "SkBlurImageFilter.h"
11 #include "SkColorPriv.h"
12 #include "SkDashPathEffect.h"
14 #include "SkDecodingImageGenerator.h"
16 #include "SkImageEncoder.h"
17 #include "SkImageGenerator.h"
19 #include "SkPicture.h"
20 #include "SkPictureRecorder.h"
21 #include "SkPictureUtils.h"
22 #include "SkPixelRef.h"
29 #include "SkSurface.h"
30 #include "GrContextFactory.h"
31 #include "GrPictureUtils.h"
35 #include "SkLumaColorFilter.h"
36 #include "SkColorFilterImageFilter.h"
38 static const int gColorScale = 30;
39 static const int gColorOffset = 60;
41 static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) {
42 bm->allocN32Pixels(w, h);
43 bm->eraseColor(color);
49 static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) {
52 bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType,
53 kPremul_SkAlphaType));
54 SkAutoLockPixels lock(*bm);
55 for (int y = 0; y < h; y += 2) {
56 uint8_t* s = bm->getAddr8(0, y);
57 for (int x = 0; x < w; x += 2) {
61 s = bm->getAddr8(0, y + 1);
62 for (int x = 0; x < w; x += 2) {
72 static void init_paint(SkPaint* paint, const SkBitmap &bm) {
73 SkShader* shader = SkShader::CreateBitmapShader(bm,
74 SkShader::kClamp_TileMode,
75 SkShader::kClamp_TileMode);
76 paint->setShader(shader)->unref();
79 typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&,
80 const SkBitmap&, const SkPoint&,
81 SkTDArray<SkPixelRef*>* usedPixRefs);
83 static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm,
84 const SkBitmap& altBM, const SkPoint& pos,
85 SkTDArray<SkPixelRef*>* usedPixRefs) {
87 init_paint(&paint, bm);
89 canvas->drawPaint(paint);
90 *usedPixRefs->append() = bm.pixelRef();
93 static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm,
94 const SkBitmap& altBM, const SkPoint& pos,
95 SkTDArray<SkPixelRef*>* usedPixRefs) {
97 init_paint(&paint, bm);
100 SkPoint points[5] = {
102 { pos.fX + bm.width() - 1, pos.fY },
103 { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 },
104 { pos.fX, pos.fY + bm.height() - 1 },
108 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint);
109 *usedPixRefs->append() = bm.pixelRef();
112 static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm,
113 const SkBitmap& altBM, const SkPoint& pos,
114 SkTDArray<SkPixelRef*>* usedPixRefs) {
116 init_paint(&paint, bm);
118 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
119 r.offset(pos.fX, pos.fY);
121 canvas->drawRect(r, paint);
122 *usedPixRefs->append() = bm.pixelRef();
125 static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm,
126 const SkBitmap& altBM, const SkPoint& pos,
127 SkTDArray<SkPixelRef*>* usedPixRefs) {
129 init_paint(&paint, bm);
131 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
132 r.offset(pos.fX, pos.fY);
134 canvas->drawOval(r, paint);
135 *usedPixRefs->append() = bm.pixelRef();
138 static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm,
139 const SkBitmap& altBM, const SkPoint& pos,
140 SkTDArray<SkPixelRef*>* usedPixRefs) {
142 init_paint(&paint, bm);
144 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
145 r.offset(pos.fX, pos.fY);
148 rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4);
149 canvas->drawRRect(rr, paint);
150 *usedPixRefs->append() = bm.pixelRef();
153 static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm,
154 const SkBitmap& altBM, const SkPoint& pos,
155 SkTDArray<SkPixelRef*>* usedPixRefs) {
157 init_paint(&paint, bm);
160 path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height()));
161 path.lineTo(SkIntToScalar(bm.width()), 0);
163 path.offset(pos.fX, pos.fY);
165 canvas->drawPath(path, paint);
166 *usedPixRefs->append() = bm.pixelRef();
169 static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm,
170 const SkBitmap& altBM, const SkPoint& pos,
171 SkTDArray<SkPixelRef*>* usedPixRefs) {
172 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL);
173 *usedPixRefs->append() = bm.pixelRef();
176 static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
177 const SkBitmap& altBM, const SkPoint& pos,
178 SkTDArray<SkPixelRef*>* usedPixRefs) {
180 init_paint(&paint, bm);
182 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
183 canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint);
184 *usedPixRefs->append() = bm.pixelRef();
185 *usedPixRefs->append() = altBM.pixelRef();
188 static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm,
189 const SkBitmap& altBM, const SkPoint& pos,
190 SkTDArray<SkPixelRef*>* usedPixRefs) {
191 const SkMatrix& ctm = canvas->getTotalMatrix();
194 ctm.mapPoints(&p, 1);
196 canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL);
197 *usedPixRefs->append() = bm.pixelRef();
201 // Although specifiable, this case doesn't seem to make sense (i.e., the
202 // bitmap in the shader is never used).
203 static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
204 const SkBitmap& altBM, const SkPoint& pos,
205 SkTDArray<SkPixelRef*>* usedPixRefs) {
207 init_paint(&paint, bm);
209 const SkMatrix& ctm = canvas->getTotalMatrix();
212 ctm.mapPoints(&p, 1);
214 canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint);
215 *usedPixRefs->append() = bm.pixelRef();
216 *usedPixRefs->append() = altBM.pixelRef();
220 static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm,
221 const SkBitmap& altBM, const SkPoint& pos,
222 SkTDArray<SkPixelRef*>* usedPixRefs) {
223 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
225 r.offset(pos.fX, pos.fY);
226 canvas->drawBitmapRectToRect(bm, NULL, r, NULL);
227 *usedPixRefs->append() = bm.pixelRef();
230 static void drawbitmaprect_withshader_proc(SkCanvas* canvas,
232 const SkBitmap& altBM,
234 SkTDArray<SkPixelRef*>* usedPixRefs) {
236 init_paint(&paint, bm);
238 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
239 r.offset(pos.fX, pos.fY);
241 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
242 canvas->drawBitmapRectToRect(altBM, NULL, r, &paint);
243 *usedPixRefs->append() = bm.pixelRef();
244 *usedPixRefs->append() = altBM.pixelRef();
247 static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm,
248 const SkBitmap& altBM, const SkPoint& pos,
249 SkTDArray<SkPixelRef*>* usedPixRefs) {
251 init_paint(&paint, bm);
252 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
254 canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint);
255 *usedPixRefs->append() = bm.pixelRef();
258 static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm,
259 const SkBitmap& altBM, const SkPoint& pos,
260 SkTDArray<SkPixelRef*>* usedPixRefs) {
262 init_paint(&paint, bm);
263 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
265 SkPoint point = { pos.fX, pos.fY + bm.height() };
266 canvas->drawPosText("O", 1, &point, paint);
267 *usedPixRefs->append() = bm.pixelRef();
270 static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm,
271 const SkBitmap& altBM, const SkPoint& pos,
272 SkTDArray<SkPixelRef*>* usedPixRefs) {
275 init_paint(&paint, bm);
276 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
279 path.lineTo(SkIntToScalar(bm.width()), 0);
280 path.offset(pos.fX, pos.fY+bm.height());
282 canvas->drawTextOnPath("O", 1, path, NULL, paint);
283 *usedPixRefs->append() = bm.pixelRef();
286 static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm,
287 const SkBitmap& altBM, const SkPoint& pos,
288 SkTDArray<SkPixelRef*>* usedPixRefs) {
290 init_paint(&paint, bm);
294 { pos.fX + bm.width(), pos.fY },
295 { pos.fX + bm.width(), pos.fY + bm.height() },
296 { pos.fX, pos.fY + bm.height() }
298 SkPoint texs[4] = { { 0, 0 },
299 { SkIntToScalar(bm.width()), 0 },
300 { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) },
301 { 0, SkIntToScalar(bm.height()) } };
302 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 };
304 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL,
306 *usedPixRefs->append() = bm.pixelRef();
309 // Return a picture with the bitmaps drawn at the specified positions.
310 static SkPicture* record_bitmaps(const SkBitmap bm[],
312 SkTDArray<SkPixelRef*> analytic[],
314 DrawBitmapProc proc) {
315 SkPictureRecorder recorder;
316 SkCanvas* canvas = recorder.beginRecording(1000, 1000);
317 for (int i = 0; i < count; ++i) {
318 analytic[i].rewind();
320 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY,
321 SkIntToScalar(bm[i].width()),
322 SkIntToScalar(bm[i].height()));
323 canvas->clipRect(clipRect, SkRegion::kIntersect_Op);
324 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]);
327 return recorder.endRecording();
330 static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) {
331 rect->fLeft = rand.nextRangeScalar(-W, 2*W);
332 rect->fTop = rand.nextRangeScalar(-H, 2*H);
333 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W);
334 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H);
336 // we integralize rect to make our tests more predictable, since Gather is
343 static void draw(SkPicture* pic, int width, int height, SkBitmap* result) {
344 make_bm(result, width, height, SK_ColorBLACK, false);
346 SkCanvas canvas(*result);
347 canvas.drawPicture(pic);
350 template <typename T> int find_index(const T* array, T elem, int count) {
351 for (int i = 0; i < count; ++i) {
352 if (array[i] == elem) {
359 // Return true if 'ref' is found in array[]
360 static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) {
361 return find_index<const SkPixelRef*>(array, ref, count) >= 0;
364 // Look at each pixel that is inside 'subset', and if its color appears in
365 // colors[], find the corresponding value in refs[] and append that ref into
366 // array, skipping duplicates of the same value.
367 // Note that gathering pixelRefs from rendered colors suffers from the problem
368 // that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color)
369 // isn't easy to reconstruct.
370 static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[],
371 int count, SkTDArray<SkPixelRef*>* array,
372 const SkRect& subset) {
374 subset.roundOut(&ir);
376 if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) {
380 // Since we only want to return unique values in array, when we scan we just
381 // set a bit for each index'd color found. In practice we only have a few
382 // distinct colors, so we just use an int's bits as our array. Hence the
383 // assert that count <= number-of-bits-in-our-int.
384 SkASSERT((unsigned)count <= 32);
385 uint32_t bitarray = 0;
387 SkAutoLockPixels alp(bm);
389 for (int y = ir.fTop; y < ir.fBottom; ++y) {
390 for (int x = ir.fLeft; x < ir.fRight; ++x) {
391 SkPMColor pmc = *bm.getAddr32(x, y);
392 // the only good case where the color is not found would be if
393 // the color is transparent, meaning no bitmap was drawn in that
396 uint32_t index = SkGetPackedR32(pmc);
397 SkASSERT(SkGetPackedG32(pmc) == index);
398 SkASSERT(SkGetPackedB32(pmc) == index);
400 continue; // background color
402 SkASSERT(0 == (index - gColorOffset) % gColorScale);
403 index = (index - gColorOffset) / gColorScale;
404 SkASSERT(static_cast<int>(index) < count);
405 bitarray |= 1 << index;
410 for (int i = 0; i < count; ++i) {
411 if (bitarray & (1 << i)) {
412 *array->append() = refs[i];
417 static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h,
418 const SkTDArray<SkPixelRef*> analytic[],
420 SkTDArray<SkPixelRef*>* result,
421 const SkRect& subset) {
422 for (int i = 0; i < count; ++i) {
423 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h);
425 if (SkRect::Intersects(subset, rect)) {
426 result->append(analytic[i].count(), analytic[i].begin());
432 static const struct {
433 const DrawBitmapProc proc;
434 const char* const desc;
436 {drawpaint_proc, "drawpaint"},
437 {drawpoints_proc, "drawpoints"},
438 {drawrect_proc, "drawrect"},
439 {drawoval_proc, "drawoval"},
440 {drawrrect_proc, "drawrrect"},
441 {drawpath_proc, "drawpath"},
442 {drawbitmap_proc, "drawbitmap"},
443 {drawbitmap_withshader_proc, "drawbitmap_withshader"},
444 {drawsprite_proc, "drawsprite"},
446 {drawsprite_withshader_proc, "drawsprite_withshader"},
448 {drawbitmaprect_proc, "drawbitmaprect"},
449 {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"},
450 {drawtext_proc, "drawtext"},
451 {drawpostext_proc, "drawpostext"},
452 {drawtextonpath_proc, "drawtextonpath"},
453 {drawverts_proc, "drawverts"},
456 static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) {
457 // Our convention is that the color components contain an encoding of
458 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is
459 // reserved for the background
460 for (int i = 0; i < num; ++i) {
461 make_bm(&bm[i], w, h,
463 gColorScale*i+gColorOffset,
464 gColorScale*i+gColorOffset,
465 gColorScale*i+gColorOffset),
467 refs[i] = bm[i].pixelRef();
470 // The A8 alternate bitmaps are all BW checkerboards
471 for (int i = 0; i < num; ++i) {
472 make_checkerboard(&bm[num+i], w, h, true);
473 refs[num+i] = bm[num+i].pixelRef();
477 static void test_gatherpixelrefs(skiatest::Reporter* reporter) {
480 const SkScalar W = SkIntToScalar(IW);
481 const SkScalar H = W;
483 static const int N = 4;
485 SkPixelRef* refs[2*N];
486 SkTDArray<SkPixelRef*> analytic[N];
488 const SkPoint pos[N] = {
489 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
492 create_textures(bm, refs, N, IW, IH);
495 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
496 SkAutoTUnref<SkPicture> pic(
497 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
499 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
500 // quick check for a small piece of each quadrant, which should just
501 // contain 1 or 2 bitmaps.
502 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
504 r.set(2, 2, W - 2, H - 2);
505 r.offset(pos[i].fX, pos[i].fY);
506 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r));
508 ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned "
509 "NULL for %s.", gProcs[k].desc);
512 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data();
513 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*));
514 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
516 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
517 } else if (2 == count) {
518 REPORTER_ASSERT(reporter,
519 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
520 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
525 draw(pic, 2*IW, 2*IH, &image);
527 // Test a bunch of random (mostly) rects, and compare the gather results
528 // with a deduced list of refs by looking at the colors drawn.
529 for (int j = 0; j < 100; ++j) {
531 rand_rect(&r, rand, 2*W, 2*H);
533 SkTDArray<SkPixelRef*> fromImage;
534 gather_from_image(image, refs, N, &fromImage, r);
536 SkTDArray<SkPixelRef*> fromAnalytic;
537 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
539 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
540 size_t dataSize = data ? data->size() : 0;
541 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*));
542 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize);
543 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL;
544 SkAutoDataUnref adu(data);
546 // Everything that we saw drawn should appear in the analytic list
547 // but the analytic list may contain some pixelRefs that were not
548 // seen in the image (e.g., A8 textures used as masks)
549 for (int i = 0; i < fromImage.count(); ++i) {
550 if (-1 == fromAnalytic.find(fromImage[i])) {
551 ERRORF(reporter, "PixelRef missing %d %s",
557 * GatherPixelRefs is conservative, so it can return more bitmaps
558 * than are strictly required. Thus our check here is only that
559 * Gather didn't miss any that we actually needed. Even that isn't
560 * a strict requirement on Gather, which is meant to be quick and
561 * only mostly-correct, but at the moment this test should work.
563 for (int i = 0; i < fromAnalytic.count(); ++i) {
564 bool found = find(gatherRefs, fromAnalytic[i], gatherCount);
566 ERRORF(reporter, "PixelRef missing %d %s",
570 // enable this block of code to debug failures, as it will rerun
571 // the case that failed.
573 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
574 size_t dataSize = data ? data->size() : 0;
582 #define GENERATE_CANVAS(recorder, x) \
583 (x) ? recorder.EXPERIMENTAL_beginRecording(100, 100) \
584 : recorder. DEPRECATED_beginRecording(100,100);
586 /* Hit a few SkPicture::Analysis cases not handled elsewhere. */
587 static void test_analysis(skiatest::Reporter* reporter, bool useNewPath) {
588 SkPictureRecorder recorder;
590 SkCanvas* canvas = GENERATE_CANVAS(recorder, useNewPath);
592 canvas->drawRect(SkRect::MakeWH(10, 10), SkPaint ());
594 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
595 REPORTER_ASSERT(reporter, !picture->willPlayBackBitmaps());
597 canvas = GENERATE_CANVAS(recorder, useNewPath);
600 // CreateBitmapShader is too smart for us; an empty (or 1x1) bitmap shader
601 // gets optimized into a non-bitmap form, so we create a 2x2 bitmap here.
603 bitmap.allocPixels(SkImageInfo::MakeN32Premul(2, 2));
604 bitmap.eraseColor(SK_ColorBLUE);
605 *(bitmap.getAddr32(0, 0)) = SK_ColorGREEN;
606 SkShader* shader = SkShader::CreateBitmapShader(bitmap, SkShader::kClamp_TileMode,
607 SkShader::kClamp_TileMode);
608 paint.setShader(shader)->unref();
609 REPORTER_ASSERT(reporter,
610 shader->asABitmap(NULL, NULL, NULL) == SkShader::kDefault_BitmapType);
612 canvas->drawRect(SkRect::MakeWH(10, 10), paint);
614 picture.reset(recorder.endRecording());
615 REPORTER_ASSERT(reporter, picture->willPlayBackBitmaps());
619 static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) {
622 const SkScalar W = SkIntToScalar(IW);
623 const SkScalar H = W;
625 static const int N = 4;
627 SkPixelRef* refs[2*N];
628 SkTDArray<SkPixelRef*> analytic[N];
630 const SkPoint pos[N] = {
631 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
634 create_textures(bm, refs, N, IW, IH);
637 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
638 SkAutoTUnref<SkPicture> pic(
639 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
641 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
643 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont(
644 new SkPictureUtils::SkPixelRefsAndRectsList);
646 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont);
648 // quick check for a small piece of each quadrant, which should just
649 // contain 1 or 2 bitmaps.
650 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
652 r.set(2, 2, W - 2, H - 2);
653 r.offset(pos[i].fX, pos[i].fY);
655 SkTDArray<SkPixelRef*> gatheredRefs;
656 prCont->query(r, &gatheredRefs);
658 int count = gatheredRefs.count();
659 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
661 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
662 } else if (2 == count) {
663 REPORTER_ASSERT(reporter,
664 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
665 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
670 draw(pic, 2*IW, 2*IH, &image);
672 // Test a bunch of random (mostly) rects, and compare the gather results
673 // with the analytic results and the pixel refs seen in a rendering.
674 for (int j = 0; j < 100; ++j) {
676 rand_rect(&r, rand, 2*W, 2*H);
678 SkTDArray<SkPixelRef*> fromImage;
679 gather_from_image(image, refs, N, &fromImage, r);
681 SkTDArray<SkPixelRef*> fromAnalytic;
682 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
684 SkTDArray<SkPixelRef*> gatheredRefs;
685 prCont->query(r, &gatheredRefs);
687 // Everything that we saw drawn should appear in the analytic list
688 // but the analytic list may contain some pixelRefs that were not
689 // seen in the image (e.g., A8 textures used as masks)
690 for (int i = 0; i < fromImage.count(); ++i) {
691 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]));
694 // Everything in the analytic list should appear in the gathered
696 for (int i = 0; i < fromAnalytic.count(); ++i) {
697 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i]));
704 // Ensure that deleting an empty SkPicture does not assert. Asserts only fire
705 // in debug mode, so only run in debug mode.
706 static void test_deleting_empty_picture() {
707 SkPictureRecorder recorder;
708 // Creates an SkPictureRecord
709 recorder.beginRecording(0, 0);
710 // Turns that into an SkPicture
711 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
712 // Ceates a new SkPictureRecord
713 recorder.beginRecording(0, 0);
716 // Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
717 static void test_serializing_empty_picture() {
718 SkPictureRecorder recorder;
719 recorder.beginRecording(0, 0);
720 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
721 SkDynamicMemoryWStream stream;
722 picture->serialize(&stream);
726 static void rand_op(SkCanvas* canvas, SkRandom& rand) {
728 SkRect rect = SkRect::MakeWH(50, 50);
730 SkScalar unit = rand.nextUScalar1();
732 // SkDebugf("save\n");
734 } else if (unit <= 0.6) {
735 // SkDebugf("restore\n");
737 } else if (unit <= 0.9) {
738 // SkDebugf("clip\n");
739 canvas->clipRect(rect);
741 // SkDebugf("draw\n");
742 canvas->drawPaint(paint);
748 static void test_gpu_veto(skiatest::Reporter* reporter,
751 SkPictureRecorder recorder;
753 SkCanvas* canvas = GENERATE_CANVAS(recorder, useNewPath);
759 SkScalar intervals[] = { 1.0f, 1.0f };
760 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0));
763 paint.setStyle(SkPaint::kStroke_Style);
764 paint.setPathEffect(dash);
766 canvas->drawPath(path, paint);
768 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
769 // path effects currently render an SkPicture undesireable for GPU rendering
771 const char *reason = NULL;
772 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL, &reason));
773 REPORTER_ASSERT(reporter, reason);
775 canvas = GENERATE_CANVAS(recorder, useNewPath);
785 REPORTER_ASSERT(reporter, !path.isConvex());
788 paint.setAntiAlias(true);
789 for (int i = 0; i < 50; ++i) {
790 canvas->drawPath(path, paint);
793 picture.reset(recorder.endRecording());
794 // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering
795 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
797 canvas = GENERATE_CANVAS(recorder, useNewPath);
807 REPORTER_ASSERT(reporter, !path.isConvex());
810 paint.setAntiAlias(true);
811 paint.setStyle(SkPaint::kStroke_Style);
812 paint.setStrokeWidth(0);
813 for (int i = 0; i < 50; ++i) {
814 canvas->drawPath(path, paint);
817 picture.reset(recorder.endRecording());
818 // hairline stroked AA concave paths are fine for GPU rendering
819 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL));
821 canvas = GENERATE_CANVAS(recorder, useNewPath);
824 SkScalar intervals [] = { 10, 20 };
825 SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25);
826 paint.setPathEffect(pe)->unref();
828 SkPoint points [2] = { { 0, 0 }, { 100, 0 } };
829 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, points, paint);
831 picture.reset(recorder.endRecording());
832 // fast-path dashed effects are fine for GPU rendering ...
833 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL));
835 canvas = GENERATE_CANVAS(recorder, useNewPath);
838 SkScalar intervals [] = { 10, 20 };
839 SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25);
840 paint.setPathEffect(pe)->unref();
842 canvas->drawRect(SkRect::MakeWH(10, 10), paint);
844 picture.reset(recorder.endRecording());
845 // ... but only when applied to drawPoint() calls
846 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
848 // Nest the previous picture inside a new one.
849 // This doesn't work in the old backend.
851 canvas = GENERATE_CANVAS(recorder, useNewPath);
853 canvas->drawPicture(picture.get());
855 picture.reset(recorder.endRecording());
856 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
860 #undef GENERATE_CANVAS
862 static void test_gpu_picture_optimization(skiatest::Reporter* reporter,
863 GrContextFactory* factory) {
864 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
865 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
867 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
871 GrContext* context = factory->get(glCtxType);
873 if (NULL == context) {
877 static const int kWidth = 100;
878 static const int kHeight = 100;
880 SkAutoTUnref<SkPicture> pict, child;
883 SkPictureRecorder recorder;
885 SkCanvas* c = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
887 c->saveLayer(NULL, NULL);
890 child.reset(recorder.endRecording());
893 // create a picture with the structure:
900 // SaveLayer w/ bound
904 // SaveLayer w/ copyable paint
908 // DrawPicture (which has a SaveLayer/Restore pair)
912 // DrawPicture with Matrix & Paint (with SaveLayer/Restore pair)
915 SkPictureRecorder recorder;
917 SkCanvas* c = recorder.beginRecording(SkIntToScalar(kWidth),
918 SkIntToScalar(kHeight));
920 c->saveLayer(NULL, NULL); // layer #0
924 c->saveLayer(NULL, NULL); // layer #1
925 c->translate(kWidth/2.0f, kHeight/2.0f);
926 SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2);
927 c->saveLayer(&r, NULL); // layer #2
934 p.setColor(SK_ColorRED);
935 c->saveLayer(NULL, &p); // layer #3
940 layerPaint.setColor(SK_ColorRED); // Non-alpha only to avoid SaveLayerDrawRestoreNooper
943 c->saveLayer(NULL, &layerPaint); // layer #4
944 c->drawPicture(child); // layer #5 inside picture
949 SkPaint picturePaint;
951 trans.setTranslate(10, 10);
953 c->saveLayer(NULL, &layerPaint); // layer #6
954 c->drawPicture(child, &trans, &picturePaint); // layer #7 inside picture
958 pict.reset(recorder.endRecording());
961 // Now test out the SaveLayer extraction
963 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
965 SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info));
967 SkCanvas* canvas = surface->getCanvas();
969 canvas->EXPERIMENTAL_optimize(pict);
971 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
973 const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key);
974 REPORTER_ASSERT(reporter, data);
976 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data);
977 REPORTER_ASSERT(reporter, 8 == gpuData->numSaveLayers());
979 const GrAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0);
980 // The parent/child layers appear in reverse order
981 const GrAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2);
982 const GrAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1);
984 const GrAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3);
986 // The parent/child layers appear in reverse order
987 const GrAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(5);
988 const GrAccelData::SaveLayerInfo& info5 = gpuData->saveLayerInfo(4);
990 // The parent/child layers appear in reverse order
991 const GrAccelData::SaveLayerInfo& info6 = gpuData->saveLayerInfo(7);
992 const GrAccelData::SaveLayerInfo& info7 = gpuData->saveLayerInfo(6);
994 REPORTER_ASSERT(reporter, info0.fValid);
995 REPORTER_ASSERT(reporter, NULL == info0.fPicture);
996 REPORTER_ASSERT(reporter, kWidth == info0.fSize.fWidth &&
997 kHeight == info0.fSize.fHeight);
998 REPORTER_ASSERT(reporter, info0.fOriginXform.isIdentity());
999 REPORTER_ASSERT(reporter, 0 == info0.fOffset.fX && 0 == info0.fOffset.fY);
1000 REPORTER_ASSERT(reporter, NULL == info0.fPaint);
1001 REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers);
1003 REPORTER_ASSERT(reporter, info1.fValid);
1004 REPORTER_ASSERT(reporter, NULL == info1.fPicture);
1005 REPORTER_ASSERT(reporter, kWidth == info1.fSize.fWidth &&
1006 kHeight == info1.fSize.fHeight);
1007 REPORTER_ASSERT(reporter, info1.fOriginXform.isIdentity());
1008 REPORTER_ASSERT(reporter, 0 == info1.fOffset.fX && 0 == info1.fOffset.fY);
1009 REPORTER_ASSERT(reporter, NULL == info1.fPaint);
1010 REPORTER_ASSERT(reporter, !info1.fIsNested &&
1011 info1.fHasNestedLayers); // has a nested SL
1013 REPORTER_ASSERT(reporter, info2.fValid);
1014 REPORTER_ASSERT(reporter, NULL == info2.fPicture);
1015 REPORTER_ASSERT(reporter, kWidth / 2 == info2.fSize.fWidth &&
1016 kHeight/2 == info2.fSize.fHeight); // bound reduces size
1017 REPORTER_ASSERT(reporter, !info2.fOriginXform.isIdentity());
1018 REPORTER_ASSERT(reporter, kWidth/2 == info2.fOffset.fX && // translated
1019 kHeight/2 == info2.fOffset.fY);
1020 REPORTER_ASSERT(reporter, NULL == info1.fPaint);
1021 REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested
1023 REPORTER_ASSERT(reporter, info3.fValid);
1024 REPORTER_ASSERT(reporter, NULL == info3.fPicture);
1025 REPORTER_ASSERT(reporter, kWidth == info3.fSize.fWidth &&
1026 kHeight == info3.fSize.fHeight);
1027 REPORTER_ASSERT(reporter, info3.fOriginXform.isIdentity());
1028 REPORTER_ASSERT(reporter, 0 == info3.fOffset.fX && 0 == info3.fOffset.fY);
1029 REPORTER_ASSERT(reporter, info3.fPaint);
1030 REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers);
1032 REPORTER_ASSERT(reporter, info4.fValid);
1033 REPORTER_ASSERT(reporter, NULL == info4.fPicture);
1034 REPORTER_ASSERT(reporter, kWidth == info4.fSize.fWidth &&
1035 kHeight == info4.fSize.fHeight);
1036 REPORTER_ASSERT(reporter, 0 == info4.fOffset.fX && 0 == info4.fOffset.fY);
1037 REPORTER_ASSERT(reporter, info4.fOriginXform.isIdentity());
1038 REPORTER_ASSERT(reporter, info4.fPaint);
1039 REPORTER_ASSERT(reporter, !info4.fIsNested &&
1040 info4.fHasNestedLayers); // has a nested SL
1042 REPORTER_ASSERT(reporter, info5.fValid);
1043 REPORTER_ASSERT(reporter, child == info5.fPicture); // in a child picture
1044 REPORTER_ASSERT(reporter, kWidth == info5.fSize.fWidth &&
1045 kHeight == info5.fSize.fHeight);
1046 REPORTER_ASSERT(reporter, 0 == info5.fOffset.fX && 0 == info5.fOffset.fY);
1047 REPORTER_ASSERT(reporter, info5.fOriginXform.isIdentity());
1048 REPORTER_ASSERT(reporter, NULL == info5.fPaint);
1049 REPORTER_ASSERT(reporter, info5.fIsNested && !info5.fHasNestedLayers); // is nested
1051 REPORTER_ASSERT(reporter, info6.fValid);
1052 REPORTER_ASSERT(reporter, NULL == info6.fPicture);
1053 REPORTER_ASSERT(reporter, kWidth == info6.fSize.fWidth &&
1054 kHeight == info6.fSize.fHeight);
1055 REPORTER_ASSERT(reporter, 0 == info6.fOffset.fX && 0 == info6.fOffset.fY);
1056 REPORTER_ASSERT(reporter, info6.fOriginXform.isIdentity());
1057 REPORTER_ASSERT(reporter, info6.fPaint);
1058 REPORTER_ASSERT(reporter, !info6.fIsNested &&
1059 info6.fHasNestedLayers); // has a nested SL
1061 REPORTER_ASSERT(reporter, info7.fValid);
1062 REPORTER_ASSERT(reporter, child == info7.fPicture); // in a child picture
1063 REPORTER_ASSERT(reporter, kWidth == info7.fSize.fWidth &&
1064 kHeight == info7.fSize.fHeight);
1065 REPORTER_ASSERT(reporter, 0 == info7.fOffset.fX && 0 == info7.fOffset.fY);
1066 REPORTER_ASSERT(reporter, info7.fOriginXform.isIdentity());
1067 REPORTER_ASSERT(reporter, NULL == info7.fPaint);
1068 REPORTER_ASSERT(reporter, info7.fIsNested && !info7.fHasNestedLayers); // is nested
1075 static void test_has_text(skiatest::Reporter* reporter, bool useNewPath) {
1076 SkPictureRecorder recorder;
1077 #define BEGIN_RECORDING useNewPath ? recorder.EXPERIMENTAL_beginRecording(100, 100) \
1078 : recorder. DEPRECATED_beginRecording(100, 100)
1080 SkCanvas* canvas = BEGIN_RECORDING;
1082 canvas->drawRect(SkRect::MakeWH(20, 20), SkPaint());
1084 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1085 REPORTER_ASSERT(reporter, !picture->hasText());
1087 SkPoint point = SkPoint::Make(10, 10);
1088 canvas = BEGIN_RECORDING;
1090 canvas->drawText("Q", 1, point.fX, point.fY, SkPaint());
1092 picture.reset(recorder.endRecording());
1093 REPORTER_ASSERT(reporter, picture->hasText());
1095 canvas = BEGIN_RECORDING;
1097 canvas->drawPosText("Q", 1, &point, SkPaint());
1099 picture.reset(recorder.endRecording());
1100 REPORTER_ASSERT(reporter, picture->hasText());
1102 canvas = BEGIN_RECORDING;
1104 canvas->drawPosTextH("Q", 1, &point.fX, point.fY, SkPaint());
1106 picture.reset(recorder.endRecording());
1107 REPORTER_ASSERT(reporter, picture->hasText());
1109 canvas = BEGIN_RECORDING;
1113 path.lineTo(50, 50);
1115 canvas->drawTextOnPathHV("Q", 1, path, point.fX, point.fY, SkPaint());
1117 picture.reset(recorder.endRecording());
1118 REPORTER_ASSERT(reporter, picture->hasText());
1120 canvas = BEGIN_RECORDING;
1124 path.lineTo(50, 50);
1126 canvas->drawTextOnPath("Q", 1, path, NULL, SkPaint());
1128 picture.reset(recorder.endRecording());
1129 REPORTER_ASSERT(reporter, picture->hasText());
1131 // Nest the previous picture inside a new one.
1132 // This doesn't work in the old backend.
1134 canvas = BEGIN_RECORDING;
1136 canvas->drawPicture(picture.get());
1138 picture.reset(recorder.endRecording());
1139 REPORTER_ASSERT(reporter, picture->hasText());
1141 #undef BEGIN_RECORDING
1144 static void set_canvas_to_save_count_4(SkCanvas* canvas) {
1145 canvas->restoreToCount(1);
1152 * A canvas that records the number of saves, saveLayers and restores.
1154 class SaveCountingCanvas : public SkCanvas {
1156 SaveCountingCanvas(int width, int height)
1157 : INHERITED(width, height)
1159 , fSaveLayerCount(0)
1163 virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint,
1164 SaveFlags flags) SK_OVERRIDE {
1166 return this->INHERITED::willSaveLayer(bounds, paint, flags);
1169 virtual void willSave() SK_OVERRIDE {
1171 this->INHERITED::willSave();
1174 virtual void willRestore() SK_OVERRIDE {
1176 this->INHERITED::willRestore();
1179 unsigned int getSaveCount() const { return fSaveCount; }
1180 unsigned int getSaveLayerCount() const { return fSaveLayerCount; }
1181 unsigned int getRestoreCount() const { return fRestoreCount; }
1184 unsigned int fSaveCount;
1185 unsigned int fSaveLayerCount;
1186 unsigned int fRestoreCount;
1188 typedef SkCanvas INHERITED;
1191 void check_save_state(skiatest::Reporter* reporter, SkPicture* picture,
1192 unsigned int numSaves, unsigned int numSaveLayers,
1193 unsigned int numRestores) {
1194 SaveCountingCanvas canvas(SkScalarCeilToInt(picture->cullRect().width()),
1195 SkScalarCeilToInt(picture->cullRect().height()));
1197 picture->playback(&canvas);
1199 // Optimizations may have removed these,
1200 // so expect to have seen no more than num{Saves,SaveLayers,Restores}.
1201 REPORTER_ASSERT(reporter, numSaves >= canvas.getSaveCount());
1202 REPORTER_ASSERT(reporter, numSaveLayers >= canvas.getSaveLayerCount());
1203 REPORTER_ASSERT(reporter, numRestores >= canvas.getRestoreCount());
1206 // This class exists so SkPicture can friend it and give it access to
1207 // the 'partialReplay' method.
1208 class SkPictureRecorderReplayTester {
1210 static SkPicture* Copy(SkPictureRecorder* recorder) {
1211 SkPictureRecorder recorder2;
1213 SkCanvas* canvas = recorder2.beginRecording(10, 10);
1215 recorder->partialReplay(canvas);
1217 return recorder2.endRecording();
1221 static void create_imbalance(SkCanvas* canvas) {
1222 SkRect clipRect = SkRect::MakeWH(2, 2);
1223 SkRect drawRect = SkRect::MakeWH(10, 10);
1225 canvas->clipRect(clipRect, SkRegion::kReplace_Op);
1226 canvas->translate(1.0f, 1.0f);
1228 p.setColor(SK_ColorGREEN);
1229 canvas->drawRect(drawRect, p);
1233 // This tests that replaying a potentially unbalanced picture into a canvas
1234 // doesn't affect the canvas' save count or matrix/clip state.
1235 static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) {
1237 bm.allocN32Pixels(4, 3);
1238 SkCanvas canvas(bm);
1240 int beforeSaveCount = canvas.getSaveCount();
1242 SkMatrix beforeMatrix = canvas.getTotalMatrix();
1246 canvas.getClipBounds(&beforeClip);
1248 canvas.drawPicture(picture);
1250 REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount());
1251 REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix());
1255 canvas.getClipBounds(&afterClip);
1257 REPORTER_ASSERT(reporter, afterClip == beforeClip);
1260 // Test out SkPictureRecorder::partialReplay
1261 DEF_TEST(PictureRecorder_replay, reporter) {
1262 // check save/saveLayer state
1264 SkPictureRecorder recorder;
1266 SkCanvas* canvas = recorder.beginRecording(10, 10);
1268 canvas->saveLayer(NULL, NULL);
1270 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1272 // The extra save and restore comes from the Copy process.
1273 check_save_state(reporter, copy, 2, 1, 3);
1275 canvas->saveLayer(NULL, NULL);
1277 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1279 check_save_state(reporter, final, 1, 2, 3);
1281 // The copy shouldn't pick up any operations added after it was made
1282 check_save_state(reporter, copy, 2, 1, 3);
1285 // (partially) check leakage of draw ops
1287 SkPictureRecorder recorder;
1289 SkCanvas* canvas = recorder.beginRecording(10, 10);
1291 SkRect r = SkRect::MakeWH(5, 5);
1294 canvas->drawRect(r, p);
1296 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1298 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1301 make_bm(&bm, 10, 10, SK_ColorRED, true);
1303 r.offset(5.0f, 5.0f);
1304 canvas->drawBitmapRectToRect(bm, NULL, r);
1306 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1307 REPORTER_ASSERT(reporter, final->willPlayBackBitmaps());
1309 REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID());
1311 // The snapshot shouldn't pick up any operations added after it was made
1312 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1315 // Recreate the Android partialReplay test case
1317 SkPictureRecorder recorder;
1319 SkCanvas* canvas = recorder.beginRecording(4, 3, NULL, 0);
1320 create_imbalance(canvas);
1322 int expectedSaveCount = canvas->getSaveCount();
1324 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1325 check_balance(reporter, copy);
1327 REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount());
1329 // End the recording of source to test the picture finalization
1330 // process isn't complicated by the partialReplay step
1331 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1335 static void test_unbalanced_save_restores(skiatest::Reporter* reporter) {
1336 SkCanvas testCanvas(100, 100);
1337 set_canvas_to_save_count_4(&testCanvas);
1339 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1342 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000);
1344 SkPictureRecorder recorder;
1347 // Create picture with 2 unbalanced saves
1348 SkCanvas* canvas = recorder.beginRecording(100, 100);
1350 canvas->translate(10, 10);
1351 canvas->drawRect(rect, paint);
1353 canvas->translate(10, 10);
1354 canvas->drawRect(rect, paint);
1355 SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording());
1357 testCanvas.drawPicture(extraSavePicture);
1358 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1361 set_canvas_to_save_count_4(&testCanvas);
1364 // Create picture with 2 unbalanced restores
1365 SkCanvas* canvas = recorder.beginRecording(100, 100);
1367 canvas->translate(10, 10);
1368 canvas->drawRect(rect, paint);
1370 canvas->translate(10, 10);
1371 canvas->drawRect(rect, paint);
1376 SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording());
1378 testCanvas.drawPicture(extraRestorePicture);
1379 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1382 set_canvas_to_save_count_4(&testCanvas);
1385 SkCanvas* canvas = recorder.beginRecording(100, 100);
1386 canvas->translate(10, 10);
1387 canvas->drawRect(rect, paint);
1388 SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording());
1390 testCanvas.drawPicture(noSavePicture);
1391 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1392 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity());
1396 static void test_peephole() {
1399 SkPictureRecorder recorder;
1401 for (int j = 0; j < 100; j++) {
1402 SkRandom rand2(rand); // remember the seed
1404 SkCanvas* canvas = recorder.beginRecording(100, 100);
1406 for (int i = 0; i < 1000; ++i) {
1407 rand_op(canvas, rand);
1409 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1415 SkCanvas* canvas = recorder.beginRecording(100, 100);
1416 SkRect rect = SkRect::MakeWH(50, 50);
1418 for (int i = 0; i < 100; ++i) {
1421 while (canvas->getSaveCount() > 1) {
1422 canvas->clipRect(rect);
1425 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1430 // Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
1431 // should never do this.
1432 static void test_bad_bitmap() {
1433 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
1436 bm.setInfo(SkImageInfo::MakeN32Premul(100, 100));
1437 SkPictureRecorder recorder;
1438 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100);
1439 recordingCanvas->drawBitmap(bm, 0, 0);
1440 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1443 canvas.drawPicture(picture);
1447 static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
1448 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
1451 static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
1452 SkPictureRecorder recorder;
1453 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bitmap.width()),
1454 SkIntToScalar(bitmap.height()));
1455 canvas->drawBitmap(bitmap, 0, 0);
1456 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1458 SkDynamicMemoryWStream wStream;
1459 picture->serialize(&wStream, &encode_bitmap_to_data);
1460 return wStream.copyToData();
1463 struct ErrorContext {
1465 skiatest::Reporter* fReporter;
1468 static void assert_one_parse_error_cb(SkError error, void* context) {
1469 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
1470 errorContext->fErrors++;
1471 // This test only expects one error, and that is a kParseError. If there are others,
1472 // there is some unknown problem.
1473 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors,
1474 "This threw more errors than expected.");
1475 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error,
1476 SkGetLastErrorString());
1479 static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
1480 // Create a bitmap that will be encoded.
1482 make_bm(&original, 100, 100, SK_ColorBLUE, true);
1483 SkDynamicMemoryWStream wStream;
1484 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
1487 SkAutoDataUnref data(wStream.copyToData());
1490 bool installSuccess = SkInstallDiscardablePixelRef(
1491 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm);
1492 REPORTER_ASSERT(reporter, installSuccess);
1494 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
1495 // Flattening original will follow the old path of performing an encode, while flattening bm
1496 // will use the already encoded data.
1497 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
1498 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
1499 REPORTER_ASSERT(reporter, picture1->equals(picture2));
1500 // Now test that a parse error was generated when trying to create a new SkPicture without
1501 // providing a function to decode the bitmap.
1502 ErrorContext context;
1503 context.fErrors = 0;
1504 context.fReporter = reporter;
1505 SkSetErrorCallback(assert_one_parse_error_cb, &context);
1506 SkMemoryStream pictureStream(picture1);
1508 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL));
1509 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
1511 SkSetErrorCallback(NULL, NULL);
1514 static void test_draw_empty(skiatest::Reporter* reporter) {
1516 make_bm(&result, 2, 2, SK_ColorBLACK, false);
1518 SkCanvas canvas(result);
1522 SkPictureRecorder recorder;
1523 recorder.beginRecording(1, 1);
1524 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1526 canvas.drawPicture(picture);
1531 SkTileGridFactory::TileGridInfo gridInfo;
1532 gridInfo.fMargin.setEmpty();
1533 gridInfo.fOffset.setZero();
1534 gridInfo.fTileInterval.set(1, 1);
1536 SkTileGridFactory factory(gridInfo);
1537 SkPictureRecorder recorder;
1538 recorder.beginRecording(1, 1, &factory);
1539 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1541 canvas.drawPicture(picture);
1546 SkRTreeFactory factory;
1547 SkPictureRecorder recorder;
1548 recorder.beginRecording(1, 1, &factory);
1549 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1551 canvas.drawPicture(picture);
1555 static void test_clip_bound_opt(skiatest::Reporter* reporter) {
1556 // Test for crbug.com/229011
1557 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
1558 SkIntToScalar(2), SkIntToScalar(2));
1559 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
1560 SkIntToScalar(1), SkIntToScalar(1));
1561 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
1562 SkIntToScalar(1), SkIntToScalar(1));
1565 invPath.addOval(rect1);
1566 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
1568 path.addOval(rect2);
1570 path2.addOval(rect3);
1572 SkPictureRecorder recorder;
1574 // Testing conservative-raster-clip that is enabled by PictureRecord
1576 SkCanvas* canvas = recorder.beginRecording(10, 10);
1577 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1578 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1579 REPORTER_ASSERT(reporter, true == nonEmpty);
1580 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1581 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1582 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1583 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1586 SkCanvas* canvas = recorder.beginRecording(10, 10);
1587 canvas->clipPath(path, SkRegion::kIntersect_Op);
1588 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1589 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1590 REPORTER_ASSERT(reporter, true == nonEmpty);
1591 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1592 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1593 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1594 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1597 SkCanvas* canvas = recorder.beginRecording(10, 10);
1598 canvas->clipPath(path, SkRegion::kIntersect_Op);
1599 canvas->clipPath(invPath, SkRegion::kUnion_Op);
1600 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1601 REPORTER_ASSERT(reporter, true == nonEmpty);
1602 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1603 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1604 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1605 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1608 SkCanvas* canvas = recorder.beginRecording(10, 10);
1609 canvas->clipPath(path, SkRegion::kDifference_Op);
1610 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1611 REPORTER_ASSERT(reporter, true == nonEmpty);
1612 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1613 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1614 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1615 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1618 SkCanvas* canvas = recorder.beginRecording(10, 10);
1619 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
1620 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1621 // True clip is actually empty in this case, but the best
1622 // determination we can make using only bounds as input is that the
1623 // clip is included in the bounds of 'path'.
1624 REPORTER_ASSERT(reporter, true == nonEmpty);
1625 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1626 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1627 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1628 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1631 SkCanvas* canvas = recorder.beginRecording(10, 10);
1632 canvas->clipPath(path, SkRegion::kIntersect_Op);
1633 canvas->clipPath(path2, SkRegion::kXOR_Op);
1634 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1635 REPORTER_ASSERT(reporter, true == nonEmpty);
1636 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
1637 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
1638 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1639 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1644 * A canvas that records the number of clip commands.
1646 class ClipCountingCanvas : public SkCanvas {
1648 ClipCountingCanvas(int width, int height)
1649 : INHERITED(width, height)
1653 virtual void onClipRect(const SkRect& r,
1655 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
1657 this->INHERITED::onClipRect(r, op, edgeStyle);
1660 virtual void onClipRRect(const SkRRect& rrect,
1662 ClipEdgeStyle edgeStyle)SK_OVERRIDE {
1664 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
1667 virtual void onClipPath(const SkPath& path,
1669 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
1671 this->INHERITED::onClipPath(path, op, edgeStyle);
1674 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE {
1676 this->INHERITED::onClipRegion(deviceRgn, op);
1679 unsigned getClipCount() const { return fClipCount; }
1682 unsigned fClipCount;
1684 typedef SkCanvas INHERITED;
1687 static void test_clip_expansion(skiatest::Reporter* reporter) {
1688 SkPictureRecorder recorder;
1689 SkCanvas* canvas = recorder.beginRecording(10, 10);
1691 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
1692 // The following expanding clip should not be skipped.
1693 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
1694 // Draw something so the optimizer doesn't just fold the world.
1696 p.setColor(SK_ColorBLUE);
1697 canvas->drawPaint(p);
1698 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1700 ClipCountingCanvas testCanvas(10, 10);
1701 picture->playback(&testCanvas);
1703 // Both clips should be present on playback.
1704 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
1707 static void test_hierarchical(skiatest::Reporter* reporter) {
1709 make_bm(&bm, 10, 10, SK_ColorRED, true);
1711 SkPictureRecorder recorder;
1713 recorder.beginRecording(10, 10);
1714 SkAutoTUnref<SkPicture> childPlain(recorder.endRecording());
1715 REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0
1717 recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0);
1718 SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording());
1719 REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1
1722 SkCanvas* canvas = recorder.beginRecording(10, 10);
1723 canvas->drawPicture(childPlain);
1724 SkAutoTUnref<SkPicture> parentPP(recorder.endRecording());
1725 REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0
1728 SkCanvas* canvas = recorder.beginRecording(10, 10);
1729 canvas->drawPicture(childWithBitmap);
1730 SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording());
1731 REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1
1734 SkCanvas* canvas = recorder.beginRecording(10, 10);
1735 canvas->drawBitmap(bm, 0, 0);
1736 canvas->drawPicture(childPlain);
1737 SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording());
1738 REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1
1741 SkCanvas* canvas = recorder.beginRecording(10, 10);
1742 canvas->drawBitmap(bm, 0, 0);
1743 canvas->drawPicture(childWithBitmap);
1744 SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording());
1745 REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2
1749 static void test_gen_id(skiatest::Reporter* reporter) {
1751 SkPictureRecorder recorder;
1752 recorder.beginRecording(0, 0);
1753 SkAutoTUnref<SkPicture> empty(recorder.endRecording());
1755 // Empty pictures should still have a valid ID
1756 REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID);
1758 SkCanvas* canvas = recorder.beginRecording(1, 1);
1759 canvas->drawARGB(255, 255, 255, 255);
1760 SkAutoTUnref<SkPicture> hasData(recorder.endRecording());
1761 // picture should have a non-zero id after recording
1762 REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID);
1764 // both pictures should have different ids
1765 REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID());
1768 DEF_TEST(Picture, reporter) {
1770 test_deleting_empty_picture();
1771 test_serializing_empty_picture();
1775 test_unbalanced_save_restores(reporter);
1778 test_gpu_veto(reporter, false);
1779 test_gpu_veto(reporter, true);
1781 test_has_text(reporter, false);
1782 test_has_text(reporter, true);
1783 test_analysis(reporter, false);
1784 test_analysis(reporter, true);
1785 test_gatherpixelrefs(reporter);
1786 test_gatherpixelrefsandrects(reporter);
1787 test_bitmap_with_encoded_data(reporter);
1788 test_draw_empty(reporter);
1789 test_clip_bound_opt(reporter);
1790 test_clip_expansion(reporter);
1791 test_hierarchical(reporter);
1792 test_gen_id(reporter);
1796 DEF_GPUTEST(GPUPicture, reporter, factory) {
1797 test_gpu_picture_optimization(reporter, factory);
1801 static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) {
1802 const SkPaint paint;
1803 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f };
1804 const SkIRect irect = { 2, 2, 3, 3 };
1806 // Don't care what these record, as long as they're legal.
1807 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint);
1808 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag);
1809 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint);
1810 canvas->drawBitmapNine(bitmap, irect, rect, &paint);
1811 canvas->drawSprite(bitmap, 1, 1);
1814 static void test_draw_bitmaps(SkCanvas* canvas) {
1816 draw_bitmaps(empty, canvas);
1817 empty.setInfo(SkImageInfo::MakeN32Premul(10, 10));
1818 draw_bitmaps(empty, canvas);
1821 DEF_TEST(Picture_EmptyBitmap, r) {
1822 SkPictureRecorder recorder;
1823 test_draw_bitmaps(recorder.beginRecording(10, 10));
1824 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1827 DEF_TEST(Canvas_EmptyBitmap, r) {
1829 dst.allocN32Pixels(10, 10);
1830 SkCanvas canvas(dst);
1832 test_draw_bitmaps(&canvas);
1835 DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) {
1836 // This test is from crbug.com/344987.
1837 // The commands are:
1838 // saveLayer with paint that modifies alpha
1839 // drawBitmapRectToRect
1840 // drawBitmapRectToRect
1842 // The bug was that this structure was modified so that:
1843 // - The saveLayer and restore were eliminated
1844 // - The alpha was only applied to the first drawBitmapRectToRect
1846 // This test draws blue and red squares inside a 50% transparent
1847 // layer. Both colours should show up muted.
1848 // When the bug is present, the red square (the second bitmap)
1849 // shows upwith full opacity.
1852 make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true);
1854 make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true);
1855 SkPaint semiTransparent;
1856 semiTransparent.setAlpha(0x80);
1858 SkPictureRecorder recorder;
1859 SkCanvas* canvas = recorder.beginRecording(100, 100);
1860 canvas->drawARGB(0, 0, 0, 0);
1862 canvas->saveLayer(0, &semiTransparent);
1863 canvas->drawBitmap(blueBM, 25, 25);
1864 canvas->drawBitmap(redBM, 50, 50);
1867 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1869 // Now replay the picture back on another canvas
1870 // and check a couple of its pixels.
1872 make_bm(&replayBM, 100, 100, SK_ColorBLACK, false);
1873 SkCanvas replayCanvas(replayBM);
1874 picture->playback(&replayCanvas);
1875 replayCanvas.flush();
1877 // With the bug present, at (55, 55) we would get a fully opaque red
1878 // intead of a dark red.
1879 REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080);
1880 REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000);
1883 struct CountingBBH : public SkBBoxHierarchy {
1884 mutable int searchCalls;
1886 CountingBBH() : searchCalls(0) {}
1888 virtual void search(const SkRect& query, SkTDArray<void*>* results) const {
1889 this->searchCalls++;
1892 // All other methods unimplemented.
1893 virtual void insert(void* data, const SkRect& bounds, bool defer) {}
1894 virtual void flushDeferredInserts() {}
1895 virtual void clear() {}
1896 virtual int getCount() const { return 0; }
1897 virtual int getDepth() const { return 0; }
1898 virtual void rewindInserts() {}
1901 class SpoonFedBBHFactory : public SkBBHFactory {
1903 explicit SpoonFedBBHFactory(SkBBoxHierarchy* bbh) : fBBH(bbh) {}
1904 virtual SkBBoxHierarchy* operator()(int width, int height) const {
1908 SkBBoxHierarchy* fBBH;
1911 // When the canvas clip covers the full picture, we don't need to call the BBH.
1912 DEF_TEST(Picture_SkipBBH, r) {
1914 SpoonFedBBHFactory factory(&bbh);
1916 SkPictureRecorder recorder;
1917 recorder.beginRecording(320, 240, &factory);
1918 SkAutoTUnref<const SkPicture> picture(recorder.endRecording());
1920 SkCanvas big(640, 480), small(300, 200);
1922 picture->playback(&big);
1923 REPORTER_ASSERT(r, bbh.searchCalls == 0);
1925 picture->playback(&small);
1926 REPORTER_ASSERT(r, bbh.searchCalls == 1);
1929 DEF_TEST(Picture_BitmapLeak, r) {
1930 SkBitmap mut, immut;
1931 mut.allocN32Pixels(300, 200);
1932 immut.allocN32Pixels(300, 200);
1933 immut.setImmutable();
1934 SkASSERT(!mut.isImmutable());
1935 SkASSERT(immut.isImmutable());
1937 // No one can hold a ref on our pixels yet.
1938 REPORTER_ASSERT(r, mut.pixelRef()->unique());
1939 REPORTER_ASSERT(r, immut.pixelRef()->unique());
1941 SkPictureRecorder rec;
1942 SkCanvas* canvas = rec.beginRecording(1920, 1200);
1943 canvas->drawBitmap(mut, 0, 0);
1944 canvas->drawBitmap(immut, 800, 600);
1945 SkAutoTDelete<const SkPicture> pic(rec.endRecording());
1947 // The picture shares the immutable pixels but copies the mutable ones.
1948 REPORTER_ASSERT(r, mut.pixelRef()->unique());
1949 REPORTER_ASSERT(r, !immut.pixelRef()->unique());
1951 // When the picture goes away, it's just our bitmaps holding the refs.
1953 REPORTER_ASSERT(r, mut.pixelRef()->unique());
1954 REPORTER_ASSERT(r, immut.pixelRef()->unique());