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