blob: 3cb145e7a4822019b5158b628ea76fca3e4806a3 [file] [log] [blame]
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001/*
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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
mtklein3e8232b2014-08-18 13:39:11 -07008#include "SkBBoxHierarchy.h"
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00009#include "SkBlurImageFilter.h"
reed@google.com21b519d2012-10-02 17:42:15 +000010#include "SkCanvas.h"
robertphillipsd8aa7b72014-10-30 16:45:02 -070011#include "SkColorMatrixFilter.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000012#include "SkColorPriv.h"
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +000013#include "SkDashPathEffect.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000014#include "SkData.h"
reed@google.combf790232013-12-13 19:45:58 +000015#include "SkDecodingImageGenerator.h"
scroggo@google.com49ce11b2013-04-25 18:29:32 +000016#include "SkError.h"
halcanary@google.com3d50ea12014-01-02 13:15:13 +000017#include "SkImageEncoder.h"
18#include "SkImageGenerator.h"
reed@google.com21b519d2012-10-02 17:42:15 +000019#include "SkPaint.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000020#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000021#include "SkPictureRecorder.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000022#include "SkPictureUtils.h"
mtkleind72094d2014-08-27 12:12:23 -070023#include "SkPixelRef.h"
reed@google.com72aa79c2013-01-24 18:27:42 +000024#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000025#include "SkRandom.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000026#include "SkShader.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000027#include "SkStream.h"
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000028
29#if SK_SUPPORT_GPU
30#include "SkSurface.h"
31#include "GrContextFactory.h"
32#include "GrPictureUtils.h"
33#endif
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000034#include "Test.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000035
reed@google.com47b679b2014-05-14 18:58:16 +000036#include "SkLumaColorFilter.h"
37#include "SkColorFilterImageFilter.h"
38
robertphillips@google.comed9866c2014-01-09 19:20:45 +000039static const int gColorScale = 30;
40static const int gColorOffset = 60;
reed@google.comfe7b1ed2012-11-29 21:00:39 +000041
42static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000043 bm->allocN32Pixels(w, h);
reed@google.comfe7b1ed2012-11-29 21:00:39 +000044 bm->eraseColor(color);
45 if (immutable) {
46 bm->setImmutable();
47 }
48}
49
robertphillips@google.comfe5824a2014-01-09 19:45:29 +000050static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000051 SkASSERT(w % 2 == 0);
52 SkASSERT(h % 2 == 0);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000053 bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType,
54 kPremul_SkAlphaType));
robertphillips@google.comed9866c2014-01-09 19:20:45 +000055 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}
reed@google.comfe7b1ed2012-11-29 21:00:39 +000072
robertphillips@google.comed9866c2014-01-09 19:20:45 +000073static 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
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +000080typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000081 const SkBitmap&, const SkPoint&,
82 SkTDArray<SkPixelRef*>* usedPixRefs);
robertphillips@google.comed9866c2014-01-09 19:20:45 +000083
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000084static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000085 const SkBitmap& altBM, const SkPoint& pos,
86 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000087 SkPaint paint;
88 init_paint(&paint, bm);
89
90 canvas->drawPaint(paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000091 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +000092}
93
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000094static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000095 const SkBitmap& altBM, const SkPoint& pos,
96 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000097 SkPaint paint;
98 init_paint(&paint, bm);
99
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000100 // draw a rect
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000101 SkPoint points[5] = {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000102 { 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 },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000107 };
108
109 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000110 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000111}
112
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000113static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000114 const SkBitmap& altBM, const SkPoint& pos,
115 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000116 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);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000123 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000124}
125
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000126static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000127 const SkBitmap& altBM, const SkPoint& pos,
128 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000129 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);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000136 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000137}
138
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000139static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000140 const SkBitmap& altBM, const SkPoint& pos,
141 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000142 SkPaint paint;
143 init_paint(&paint, bm);
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000144
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000145 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);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000151 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000152}
153
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000154static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000155 const SkBitmap& altBM, const SkPoint& pos,
156 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000157 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);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000167 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000168}
169
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000170static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000171 const SkBitmap& altBM, const SkPoint& pos,
172 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000173 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000174 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000175}
176
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000177static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000178 const SkBitmap& altBM, const SkPoint& pos,
179 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000180 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);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000185 *usedPixRefs->append() = bm.pixelRef();
186 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000187}
188
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000189static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000190 const SkBitmap& altBM, const SkPoint& pos,
191 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000192 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);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000198 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000199}
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).
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000204static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000205 const SkBitmap& altBM, const SkPoint& pos,
206 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000207 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);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000216 *usedPixRefs->append() = bm.pixelRef();
217 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000218}
219#endif
220
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000221static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000222 const SkBitmap& altBM, const SkPoint& pos,
223 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000224 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
225
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000226 r.offset(pos.fX, pos.fY);
227 canvas->drawBitmapRectToRect(bm, NULL, r, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000228 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000229}
230
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000231static void drawbitmaprect_withshader_proc(SkCanvas* canvas,
232 const SkBitmap& bm,
233 const SkBitmap& altBM,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000234 const SkPoint& pos,
235 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000236 SkPaint paint;
237 init_paint(&paint, bm);
238
239 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000240 r.offset(pos.fX, pos.fY);
241
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000242 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
243 canvas->drawBitmapRectToRect(altBM, NULL, r, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000244 *usedPixRefs->append() = bm.pixelRef();
245 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000246}
247
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000248static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000249 const SkBitmap& altBM, const SkPoint& pos,
250 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000251 SkPaint paint;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000252 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);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000256 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000257}
258
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000259static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000260 const SkBitmap& altBM, const SkPoint& pos,
261 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000262 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);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000268 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000269}
270
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000271static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000272 const SkBitmap& altBM, const SkPoint& pos,
273 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000274 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);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000284 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000285}
286
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000287static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000288 const SkBitmap& altBM, const SkPoint& pos,
289 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000290 SkPaint paint;
291 init_paint(&paint, bm);
292
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000293 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() }
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000298 };
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000299 SkPoint texs[4] = { { 0, 0 },
300 { SkIntToScalar(bm.width()), 0 },
301 { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000302 { 0, SkIntToScalar(bm.height()) } };
303 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 };
304
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000305 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL,
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000306 indices, 6, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000307 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000308}
309
310// Return a picture with the bitmaps drawn at the specified positions.
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000311static SkPicture* record_bitmaps(const SkBitmap bm[],
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000312 const SkPoint pos[],
313 SkTDArray<SkPixelRef*> analytic[],
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000314 int count,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000315 DrawBitmapProc proc) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000316 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -0700317 SkCanvas* canvas = recorder.beginRecording(1000, 1000);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000318 for (int i = 0; i < count; ++i) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000319 analytic[i].rewind();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000320 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);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000325 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000326 canvas->restore();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000327 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000328 return recorder.endRecording();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000329}
330
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000331static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000332 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
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000344static void draw(SkPicture* pic, int width, int height, SkBitmap* result) {
345 make_bm(result, width, height, SK_ColorBLACK, false);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000346
347 SkCanvas canvas(*result);
robertphillips9b14f262014-06-04 05:40:44 -0700348 canvas.drawPicture(pic);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000349}
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
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000365// 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
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000367// 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
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000381 // 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
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000390 for (int y = ir.fTop; y < ir.fBottom; ++y) {
391 for (int x = ir.fLeft; x < ir.fRight; ++x) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000392 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) {
bsalomon@google.comc3d753e2013-01-08 17:24:44 +0000397 uint32_t index = SkGetPackedR32(pmc);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000398 SkASSERT(SkGetPackedG32(pmc) == index);
399 SkASSERT(SkGetPackedB32(pmc) == index);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000400 if (0 == index) {
401 continue; // background color
402 }
403 SkASSERT(0 == (index - gColorOffset) % gColorScale);
404 index = (index - gColorOffset) / gColorScale;
bsalomon@google.com5f429b02013-01-08 18:42:20 +0000405 SkASSERT(static_cast<int>(index) < count);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000406 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
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000418static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h,
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000419 const SkTDArray<SkPixelRef*> analytic[],
420 int count,
421 SkTDArray<SkPixelRef*>* result,
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000422 const SkRect& subset) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000423 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
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000432
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"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000446#if 0
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000447 {drawsprite_withshader_proc, "drawsprite_withshader"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000448#endif
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000449 {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"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000455};
456
457static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000458 // Our convention is that the color components contain an encoding of
459 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000460 // reserved for the background
461 for (int i = 0; i < num; ++i) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000462 make_bm(&bm[i], w, h,
463 SkColorSetARGB(0xFF,
464 gColorScale*i+gColorOffset,
465 gColorScale*i+gColorOffset,
466 gColorScale*i+gColorOffset),
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000467 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
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000478static void test_gatherpixelrefs(skiatest::Reporter* reporter) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000479 const int IW = 32;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000480 const int IH = IW;
481 const SkScalar W = SkIntToScalar(IW);
482 const SkScalar H = W;
483
484 static const int N = 4;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000485 SkBitmap bm[2*N];
486 SkPixelRef* refs[2*N];
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000487 SkTDArray<SkPixelRef*> analytic[N];
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000488
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000489 const SkPoint pos[N] = {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000490 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
491 };
492
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000493 create_textures(bm, refs, N, IW, IH);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000494
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000495 SkRandom rand;
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000496 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000497 SkAutoTUnref<SkPicture> pic(
498 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000499
tomhudson@google.com381010e2013-10-24 11:12:47 +0000500 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000501 // quick check for a small piece of each quadrant, which should just
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000502 // contain 1 or 2 bitmaps.
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000503 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));
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000508 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]));
commit-bot@chromium.orgfe433c12013-07-09 16:04:32 +0000522 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000523 }
524
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000525 SkBitmap image;
526 draw(pic, 2*IW, 2*IH, &image);
527
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000528 // 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
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000534 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);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000539
540 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
541 size_t dataSize = data ? data->size() : 0;
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000542 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000543 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize);
544 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL;
545 SkAutoDataUnref adu(data);
546
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000547 // 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) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000551 if (-1 == fromAnalytic.find(fromImage[i])) {
552 ERRORF(reporter, "PixelRef missing %d %s",
553 i, gProcs[k].desc);
554 }
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000555 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000556
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000557 /*
558 * GatherPixelRefs is conservative, so it can return more bitmaps
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000559 * 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
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000561 * 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 */
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000564 for (int i = 0; i < fromAnalytic.count(); ++i) {
565 bool found = find(gatherRefs, fromAnalytic[i], gatherCount);
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000566 if (!found) {
567 ERRORF(reporter, "PixelRef missing %d %s",
568 i, gProcs[k].desc);
569 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000570#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
tomhudson3a0f2792014-08-20 05:29:41 -0700583/* Hit a few SkPicture::Analysis cases not handled elsewhere. */
mtklein8e126562014-10-01 09:29:35 -0700584static void test_analysis(skiatest::Reporter* reporter) {
tomhudson3a0f2792014-08-20 05:29:41 -0700585 SkPictureRecorder recorder;
586
mtklein8e126562014-10-01 09:29:35 -0700587 SkCanvas* canvas = recorder.beginRecording(100, 100);
tomhudson3a0f2792014-08-20 05:29:41 -0700588 {
589 canvas->drawRect(SkRect::MakeWH(10, 10), SkPaint ());
590 }
591 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
592 REPORTER_ASSERT(reporter, !picture->willPlayBackBitmaps());
593
mtklein8e126562014-10-01 09:29:35 -0700594 canvas = recorder.beginRecording(100, 100);
tomhudson3a0f2792014-08-20 05:29:41 -0700595 {
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
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000616static 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) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000635 SkAutoTUnref<SkPicture> pic(
636 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000637
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) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000660 REPORTER_ASSERT(reporter,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000661 (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
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000692 // list.
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000693 for (int i = 0; i < fromAnalytic.count(); ++i) {
694 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i]));
695 }
696 }
697 }
698}
699
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000700#ifdef SK_DEBUG
mtklein3e8232b2014-08-18 13:39:11 -0700701// Ensure that deleting an empty SkPicture does not assert. Asserts only fire
robertphillipsdb539902014-07-01 08:47:04 -0700702// in debug mode, so only run in debug mode.
703static void test_deleting_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000704 SkPictureRecorder recorder;
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000705 // Creates an SkPictureRecord
robertphillips9f1c2412014-06-09 06:25:34 -0700706 recorder.beginRecording(0, 0);
robertphillipsdb539902014-07-01 08:47:04 -0700707 // Turns that into an SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000708 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
robertphillipsdb539902014-07-01 08:47:04 -0700709 // Ceates a new SkPictureRecord
robertphillips9f1c2412014-06-09 06:25:34 -0700710 recorder.beginRecording(0, 0);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000711}
712
713// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
714static void test_serializing_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000715 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -0700716 recorder.beginRecording(0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000717 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000718 SkDynamicMemoryWStream stream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000719 picture->serialize(&stream);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000720}
721#endif
722
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000723static void rand_op(SkCanvas* canvas, SkRandom& rand) {
reed@google.com21b519d2012-10-02 17:42:15 +0000724 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
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000743#if SK_SUPPORT_GPU
tomhudson3a0f2792014-08-20 05:29:41 -0700744
mtklein8e126562014-10-01 09:29:35 -0700745static void test_gpu_veto(skiatest::Reporter* reporter) {
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000746 SkPictureRecorder recorder;
747
mtklein8e126562014-10-01 09:29:35 -0700748 SkCanvas* canvas = recorder.beginRecording(100, 100);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000749 {
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
commit-bot@chromium.orga1ff26a2014-05-30 21:52:52 +0000765
766 const char *reason = NULL;
767 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL, &reason));
bsalomon49f085d2014-09-05 13:34:00 -0700768 REPORTER_ASSERT(reporter, reason);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000769
mtklein8e126562014-10-01 09:29:35 -0700770 canvas = recorder.beginRecording(100, 100);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000771 {
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());
jvanverthd86b07a2014-11-04 08:50:15 -0800789 // 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
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000812 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
813
mtklein8e126562014-10-01 09:29:35 -0700814 canvas = recorder.beginRecording(100, 100);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000815 {
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));
tomhudson3a0f2792014-08-20 05:29:41 -0700837
mtklein8e126562014-10-01 09:29:35 -0700838 canvas = recorder.beginRecording(100, 100);
tomhudson3a0f2792014-08-20 05:29:41 -0700839 {
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
mtklein8e126562014-10-01 09:29:35 -0700852 canvas = recorder.beginRecording(100, 100);
tomhudson3a0f2792014-08-20 05:29:41 -0700853 {
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));
mtklein53fecfb2014-08-21 09:11:37 -0700864
865 // Nest the previous picture inside a new one.
mtklein8e126562014-10-01 09:29:35 -0700866 canvas = recorder.beginRecording(100, 100);
867 {
868 canvas->drawPicture(picture.get());
mtklein53fecfb2014-08-21 09:11:37 -0700869 }
mtklein8e126562014-10-01 09:29:35 -0700870 picture.reset(recorder.endRecording());
871 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000872}
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000873
874static void test_gpu_picture_optimization(skiatest::Reporter* reporter,
875 GrContextFactory* factory) {
bsalomone904c092014-07-17 10:50:59 -0700876 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
877 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000878
bsalomone904c092014-07-17 10:50:59 -0700879 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
880 continue;
881 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000882
bsalomone904c092014-07-17 10:50:59 -0700883 GrContext* context = factory->get(glCtxType);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000884
bsalomone904c092014-07-17 10:50:59 -0700885 if (NULL == context) {
886 continue;
887 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000888
bsalomone904c092014-07-17 10:50:59 -0700889 static const int kWidth = 100;
890 static const int kHeight = 100;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000891
robertphillipsd8aa7b72014-10-30 16:45:02 -0700892 // 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
robertphillipsd6283302014-08-27 11:53:28 -0700902 SkAutoTUnref<SkPicture> pict, child;
robertphillips81f71b62014-11-11 04:54:49 -0800903 SkRTreeFactory bbhFactory;
robertphillipsd6283302014-08-27 11:53:28 -0700904
905 {
906 SkPictureRecorder recorder;
907
robertphillips81f71b62014-11-11 04:54:49 -0800908 SkCanvas* c = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight),
909 &bbhFactory,
910 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag);
robertphillipsd6283302014-08-27 11:53:28 -0700911
robertphillipsd8aa7b72014-10-30 16:45:02 -0700912 c->saveLayer(NULL, &complexPaint);
robertphillipsd6283302014-08-27 11:53:28 -0700913 c->restore();
914
915 child.reset(recorder.endRecording());
916 }
bsalomone904c092014-07-17 10:50:59 -0700917
918 // create a picture with the structure:
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000919 // 1)
bsalomone904c092014-07-17 10:50:59 -0700920 // SaveLayer
921 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000922 // 2)
bsalomone904c092014-07-17 10:50:59 -0700923 // SaveLayer
924 // Translate
925 // SaveLayer w/ bound
926 // Restore
927 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000928 // 3)
bsalomone904c092014-07-17 10:50:59 -0700929 // SaveLayer w/ copyable paint
930 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000931 // 4)
robertphillipsd6283302014-08-27 11:53:28 -0700932 // SaveLayer
933 // DrawPicture (which has a SaveLayer/Restore pair)
934 // Restore
935 // 5)
936 // SaveLayer
937 // DrawPicture with Matrix & Paint (with SaveLayer/Restore pair)
bsalomone904c092014-07-17 10:50:59 -0700938 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000939 {
bsalomone904c092014-07-17 10:50:59 -0700940 SkPictureRecorder recorder;
941
mtklein87c41382014-09-08 07:31:18 -0700942 SkCanvas* c = recorder.beginRecording(SkIntToScalar(kWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800943 SkIntToScalar(kHeight),
944 &bbhFactory,
945 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag);
bsalomone904c092014-07-17 10:50:59 -0700946 // 1)
robertphillipsd8aa7b72014-10-30 16:45:02 -0700947 c->saveLayer(NULL, &complexPaint); // layer #0
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000948 c->restore();
bsalomone904c092014-07-17 10:50:59 -0700949
950 // 2)
robertphillipsd6283302014-08-27 11:53:28 -0700951 c->saveLayer(NULL, NULL); // layer #1
952 c->translate(kWidth/2.0f, kHeight/2.0f);
bsalomone904c092014-07-17 10:50:59 -0700953 SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2);
robertphillipsd8aa7b72014-10-30 16:45:02 -0700954 c->saveLayer(&r, &complexPaint); // layer #2
bsalomone904c092014-07-17 10:50:59 -0700955 c->restore();
956 c->restore();
957
958 // 3)
959 {
robertphillipsd8aa7b72014-10-30 16:45:02 -0700960 c->saveLayer(NULL, &complexPaint); // layer #3
bsalomone904c092014-07-17 10:50:59 -0700961 c->restore();
962 }
mtklein93f52a62014-09-08 08:41:37 -0700963
964 SkPaint layerPaint;
965 layerPaint.setColor(SK_ColorRED); // Non-alpha only to avoid SaveLayerDrawRestoreNooper
bsalomone904c092014-07-17 10:50:59 -0700966 // 4)
robertphillipsd6283302014-08-27 11:53:28 -0700967 {
mtklein93f52a62014-09-08 08:41:37 -0700968 c->saveLayer(NULL, &layerPaint); // layer #4
robertphillipsd6283302014-08-27 11:53:28 -0700969 c->drawPicture(child); // layer #5 inside picture
970 c->restore();
971 }
972 // 5
bsalomone904c092014-07-17 10:50:59 -0700973 {
mtklein93f52a62014-09-08 08:41:37 -0700974 SkPaint picturePaint;
robertphillipsd6283302014-08-27 11:53:28 -0700975 SkMatrix trans;
976 trans.setTranslate(10, 10);
977
mtklein93f52a62014-09-08 08:41:37 -0700978 c->saveLayer(NULL, &layerPaint); // layer #6
979 c->drawPicture(child, &trans, &picturePaint); // layer #7 inside picture
bsalomone904c092014-07-17 10:50:59 -0700980 c->restore();
981 }
982
983 pict.reset(recorder.endRecording());
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000984 }
985
bsalomone904c092014-07-17 10:50:59 -0700986 // Now test out the SaveLayer extraction
987 {
robertphillips6617d502014-08-18 09:39:34 -0700988 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000989
bsalomone904c092014-07-17 10:50:59 -0700990 const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key);
bsalomon49f085d2014-09-05 13:34:00 -0700991 REPORTER_ASSERT(reporter, data);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000992
robertphillips6617d502014-08-18 09:39:34 -0700993 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data);
robertphillipsd6283302014-08-27 11:53:28 -0700994 REPORTER_ASSERT(reporter, 8 == gpuData->numSaveLayers());
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000995
robertphillips6617d502014-08-18 09:39:34 -0700996 const GrAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0);
robertphillipsd6283302014-08-27 11:53:28 -0700997 // The parent/child layers appear in reverse order
robertphillips6617d502014-08-18 09:39:34 -0700998 const GrAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2);
999 const GrAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1);
robertphillipsd6283302014-08-27 11:53:28 -07001000
robertphillips6617d502014-08-18 09:39:34 -07001001 const GrAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3);
robertphillipsd6283302014-08-27 11:53:28 -07001002
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);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001010
robertphillips30d2cc62014-09-24 08:52:18 -07001011 REPORTER_ASSERT(reporter, NULL == info0.fPicture);
robertphillips3aac6e02014-10-20 08:52:40 -07001012 REPORTER_ASSERT(reporter, kWidth == info0.fBounds.width() &&
1013 kHeight == info0.fBounds.height());
robertphillips9e6835d2014-10-22 05:33:52 -07001014 REPORTER_ASSERT(reporter, info0.fLocalMat.isIdentity());
1015 REPORTER_ASSERT(reporter, info0.fPreMat.isIdentity());
robertphillips3aac6e02014-10-20 08:52:40 -07001016 REPORTER_ASSERT(reporter, 0 == info0.fBounds.fLeft && 0 == info0.fBounds.fTop);
robertphillipsd8aa7b72014-10-30 16:45:02 -07001017 REPORTER_ASSERT(reporter, NULL != info0.fPaint);
bsalomone904c092014-07-17 10:50:59 -07001018 REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001019
robertphillips30d2cc62014-09-24 08:52:18 -07001020 REPORTER_ASSERT(reporter, NULL == info1.fPicture);
robertphillips4e8e3422014-11-12 06:46:08 -08001021 REPORTER_ASSERT(reporter, kWidth/2.0 == info1.fBounds.width() &&
1022 kHeight/2.0 == info1.fBounds.height());
robertphillips9e6835d2014-10-22 05:33:52 -07001023 REPORTER_ASSERT(reporter, info1.fLocalMat.isIdentity());
1024 REPORTER_ASSERT(reporter, info1.fPreMat.isIdentity());
robertphillips4e8e3422014-11-12 06:46:08 -08001025 REPORTER_ASSERT(reporter, kWidth/2.0 == info1.fBounds.fLeft &&
1026 kHeight/2.0 == info1.fBounds.fTop);
robertphillipsd6283302014-08-27 11:53:28 -07001027 REPORTER_ASSERT(reporter, NULL == info1.fPaint);
mtklein87c41382014-09-08 07:31:18 -07001028 REPORTER_ASSERT(reporter, !info1.fIsNested &&
robertphillipsd6283302014-08-27 11:53:28 -07001029 info1.fHasNestedLayers); // has a nested SL
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001030
robertphillips30d2cc62014-09-24 08:52:18 -07001031 REPORTER_ASSERT(reporter, NULL == info2.fPicture);
robertphillips3aac6e02014-10-20 08:52:40 -07001032 REPORTER_ASSERT(reporter, kWidth / 2 == info2.fBounds.width() &&
1033 kHeight / 2 == info2.fBounds.height()); // bound reduces size
robertphillips9e6835d2014-10-22 05:33:52 -07001034 REPORTER_ASSERT(reporter, !info2.fLocalMat.isIdentity());
1035 REPORTER_ASSERT(reporter, info2.fPreMat.isIdentity());
robertphillips3aac6e02014-10-20 08:52:40 -07001036 REPORTER_ASSERT(reporter, kWidth / 2 == info2.fBounds.fLeft && // translated
1037 kHeight / 2 == info2.fBounds.fTop);
robertphillipsd8aa7b72014-10-30 16:45:02 -07001038 REPORTER_ASSERT(reporter, NULL != info2.fPaint);
bsalomone904c092014-07-17 10:50:59 -07001039 REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001040
robertphillips30d2cc62014-09-24 08:52:18 -07001041 REPORTER_ASSERT(reporter, NULL == info3.fPicture);
robertphillips3aac6e02014-10-20 08:52:40 -07001042 REPORTER_ASSERT(reporter, kWidth == info3.fBounds.width() &&
1043 kHeight == info3.fBounds.height());
robertphillips9e6835d2014-10-22 05:33:52 -07001044 REPORTER_ASSERT(reporter, info3.fLocalMat.isIdentity());
1045 REPORTER_ASSERT(reporter, info3.fPreMat.isIdentity());
robertphillips3aac6e02014-10-20 08:52:40 -07001046 REPORTER_ASSERT(reporter, 0 == info3.fBounds.fLeft && 0 == info3.fBounds.fTop);
bsalomon49f085d2014-09-05 13:34:00 -07001047 REPORTER_ASSERT(reporter, info3.fPaint);
bsalomone904c092014-07-17 10:50:59 -07001048 REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001049
robertphillips30d2cc62014-09-24 08:52:18 -07001050 REPORTER_ASSERT(reporter, NULL == info4.fPicture);
robertphillips3aac6e02014-10-20 08:52:40 -07001051 REPORTER_ASSERT(reporter, kWidth == info4.fBounds.width() &&
1052 kHeight == info4.fBounds.height());
1053 REPORTER_ASSERT(reporter, 0 == info4.fBounds.fLeft && 0 == info4.fBounds.fTop);
robertphillips9e6835d2014-10-22 05:33:52 -07001054 REPORTER_ASSERT(reporter, info4.fLocalMat.isIdentity());
1055 REPORTER_ASSERT(reporter, info4.fPreMat.isIdentity());
mtklein93f52a62014-09-08 08:41:37 -07001056 REPORTER_ASSERT(reporter, info4.fPaint);
mtklein87c41382014-09-08 07:31:18 -07001057 REPORTER_ASSERT(reporter, !info4.fIsNested &&
robertphillipsd6283302014-08-27 11:53:28 -07001058 info4.fHasNestedLayers); // has a nested SL
1059
robertphillips30d2cc62014-09-24 08:52:18 -07001060 REPORTER_ASSERT(reporter, child == info5.fPicture); // in a child picture
robertphillips3aac6e02014-10-20 08:52:40 -07001061 REPORTER_ASSERT(reporter, kWidth == info5.fBounds.width() &&
1062 kHeight == info5.fBounds.height());
1063 REPORTER_ASSERT(reporter, 0 == info5.fBounds.fLeft && 0 == info5.fBounds.fTop);
robertphillips9e6835d2014-10-22 05:33:52 -07001064 REPORTER_ASSERT(reporter, info5.fLocalMat.isIdentity());
1065 REPORTER_ASSERT(reporter, info5.fPreMat.isIdentity());
robertphillipsd8aa7b72014-10-30 16:45:02 -07001066 REPORTER_ASSERT(reporter, NULL != info5.fPaint);
robertphillipsd6283302014-08-27 11:53:28 -07001067 REPORTER_ASSERT(reporter, info5.fIsNested && !info5.fHasNestedLayers); // is nested
1068
robertphillips30d2cc62014-09-24 08:52:18 -07001069 REPORTER_ASSERT(reporter, NULL == info6.fPicture);
robertphillipsd8aa7b72014-10-30 16:45:02 -07001070 REPORTER_ASSERT(reporter, kWidth-10 == info6.fBounds.width() &&
1071 kHeight-10 == info6.fBounds.height());
1072 REPORTER_ASSERT(reporter, 10 == info6.fBounds.fLeft && 10 == info6.fBounds.fTop);
robertphillips9e6835d2014-10-22 05:33:52 -07001073 REPORTER_ASSERT(reporter, info6.fLocalMat.isIdentity());
1074 REPORTER_ASSERT(reporter, info6.fPreMat.isIdentity());
mtklein93f52a62014-09-08 08:41:37 -07001075 REPORTER_ASSERT(reporter, info6.fPaint);
mtklein87c41382014-09-08 07:31:18 -07001076 REPORTER_ASSERT(reporter, !info6.fIsNested &&
robertphillipsd6283302014-08-27 11:53:28 -07001077 info6.fHasNestedLayers); // has a nested SL
1078
robertphillips30d2cc62014-09-24 08:52:18 -07001079 REPORTER_ASSERT(reporter, child == info7.fPicture); // in a child picture
robertphillips3aac6e02014-10-20 08:52:40 -07001080 REPORTER_ASSERT(reporter, kWidth == info7.fBounds.width() &&
1081 kHeight == info7.fBounds.height());
1082 REPORTER_ASSERT(reporter, 0 == info7.fBounds.fLeft && 0 == info7.fBounds.fTop);
robertphillips9e6835d2014-10-22 05:33:52 -07001083 REPORTER_ASSERT(reporter, info7.fLocalMat.isIdentity());
1084 REPORTER_ASSERT(reporter, info7.fPreMat.isIdentity());
robertphillipsd8aa7b72014-10-30 16:45:02 -07001085 REPORTER_ASSERT(reporter, NULL != info7.fPaint);
robertphillipsd6283302014-08-27 11:53:28 -07001086 REPORTER_ASSERT(reporter, info7.fIsNested && !info7.fHasNestedLayers); // is nested
bsalomone904c092014-07-17 10:50:59 -07001087 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001088 }
1089}
1090
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001091#endif
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +00001092
mtklein8e126562014-10-01 09:29:35 -07001093static void test_has_text(skiatest::Reporter* reporter) {
ajuma750ae262014-08-18 12:59:55 -07001094 SkPictureRecorder recorder;
ajuma750ae262014-08-18 12:59:55 -07001095
mtklein8e126562014-10-01 09:29:35 -07001096 SkCanvas* canvas = recorder.beginRecording(100,100);
ajuma750ae262014-08-18 12:59:55 -07001097 {
mtkleinc551d9f2014-08-20 08:09:46 -07001098 canvas->drawRect(SkRect::MakeWH(20, 20), SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001099 }
1100 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1101 REPORTER_ASSERT(reporter, !picture->hasText());
1102
mtkleinc551d9f2014-08-20 08:09:46 -07001103 SkPoint point = SkPoint::Make(10, 10);
mtklein8e126562014-10-01 09:29:35 -07001104 canvas = recorder.beginRecording(100,100);
ajuma750ae262014-08-18 12:59:55 -07001105 {
mtkleinc551d9f2014-08-20 08:09:46 -07001106 canvas->drawText("Q", 1, point.fX, point.fY, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001107 }
1108 picture.reset(recorder.endRecording());
1109 REPORTER_ASSERT(reporter, picture->hasText());
1110
mtklein8e126562014-10-01 09:29:35 -07001111 canvas = recorder.beginRecording(100,100);
ajuma750ae262014-08-18 12:59:55 -07001112 {
mtkleinc551d9f2014-08-20 08:09:46 -07001113 canvas->drawPosText("Q", 1, &point, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001114 }
1115 picture.reset(recorder.endRecording());
1116 REPORTER_ASSERT(reporter, picture->hasText());
1117
mtklein8e126562014-10-01 09:29:35 -07001118 canvas = recorder.beginRecording(100,100);
ajuma750ae262014-08-18 12:59:55 -07001119 {
mtkleinc551d9f2014-08-20 08:09:46 -07001120 canvas->drawPosTextH("Q", 1, &point.fX, point.fY, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001121 }
1122 picture.reset(recorder.endRecording());
1123 REPORTER_ASSERT(reporter, picture->hasText());
1124
mtklein8e126562014-10-01 09:29:35 -07001125 canvas = recorder.beginRecording(100,100);
ajuma750ae262014-08-18 12:59:55 -07001126 {
1127 SkPath path;
1128 path.moveTo(0, 0);
1129 path.lineTo(50, 50);
1130
mtkleinc551d9f2014-08-20 08:09:46 -07001131 canvas->drawTextOnPathHV("Q", 1, path, point.fX, point.fY, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001132 }
1133 picture.reset(recorder.endRecording());
1134 REPORTER_ASSERT(reporter, picture->hasText());
1135
mtklein8e126562014-10-01 09:29:35 -07001136 canvas = recorder.beginRecording(100,100);
ajuma750ae262014-08-18 12:59:55 -07001137 {
1138 SkPath path;
1139 path.moveTo(0, 0);
1140 path.lineTo(50, 50);
1141
mtkleinc551d9f2014-08-20 08:09:46 -07001142 canvas->drawTextOnPath("Q", 1, path, NULL, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001143 }
1144 picture.reset(recorder.endRecording());
1145 REPORTER_ASSERT(reporter, picture->hasText());
mtklein53fecfb2014-08-21 09:11:37 -07001146
1147 // Nest the previous picture inside a new one.
mtklein8e126562014-10-01 09:29:35 -07001148 canvas = recorder.beginRecording(100,100);
1149 {
1150 canvas->drawPicture(picture.get());
mtklein53fecfb2014-08-21 09:11:37 -07001151 }
mtklein8e126562014-10-01 09:29:35 -07001152 picture.reset(recorder.endRecording());
1153 REPORTER_ASSERT(reporter, picture->hasText());
ajuma750ae262014-08-18 12:59:55 -07001154}
1155
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001156static void set_canvas_to_save_count_4(SkCanvas* canvas) {
1157 canvas->restoreToCount(1);
1158 canvas->save();
1159 canvas->save();
1160 canvas->save();
1161}
1162
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001163/**
1164 * A canvas that records the number of saves, saveLayers and restores.
1165 */
1166class SaveCountingCanvas : public SkCanvas {
1167public:
1168 SaveCountingCanvas(int width, int height)
1169 : INHERITED(width, height)
1170 , fSaveCount(0)
1171 , fSaveLayerCount(0)
1172 , fRestoreCount(0){
1173 }
1174
1175 virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint,
1176 SaveFlags flags) SK_OVERRIDE {
1177 ++fSaveLayerCount;
1178 return this->INHERITED::willSaveLayer(bounds, paint, flags);
1179 }
1180
Florin Malita5f6102d2014-06-30 10:13:28 -04001181 virtual void willSave() SK_OVERRIDE {
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001182 ++fSaveCount;
Florin Malita5f6102d2014-06-30 10:13:28 -04001183 this->INHERITED::willSave();
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001184 }
1185
1186 virtual void willRestore() SK_OVERRIDE {
1187 ++fRestoreCount;
1188 this->INHERITED::willRestore();
1189 }
1190
1191 unsigned int getSaveCount() const { return fSaveCount; }
1192 unsigned int getSaveLayerCount() const { return fSaveLayerCount; }
1193 unsigned int getRestoreCount() const { return fRestoreCount; }
1194
1195private:
1196 unsigned int fSaveCount;
1197 unsigned int fSaveLayerCount;
1198 unsigned int fRestoreCount;
1199
1200 typedef SkCanvas INHERITED;
1201};
1202
skia.committer@gmail.com8e7d37d2014-05-28 03:06:06 +00001203void check_save_state(skiatest::Reporter* reporter, SkPicture* picture,
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001204 unsigned int numSaves, unsigned int numSaveLayers,
1205 unsigned int numRestores) {
mtklein87c41382014-09-08 07:31:18 -07001206 SaveCountingCanvas canvas(SkScalarCeilToInt(picture->cullRect().width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -07001207 SkScalarCeilToInt(picture->cullRect().height()));
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001208
robertphillipsc5ba71d2014-09-04 08:42:50 -07001209 picture->playback(&canvas);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001210
mtklein87c41382014-09-08 07:31:18 -07001211 // Optimizations may have removed these,
1212 // so expect to have seen no more than num{Saves,SaveLayers,Restores}.
1213 REPORTER_ASSERT(reporter, numSaves >= canvas.getSaveCount());
1214 REPORTER_ASSERT(reporter, numSaveLayers >= canvas.getSaveLayerCount());
1215 REPORTER_ASSERT(reporter, numRestores >= canvas.getRestoreCount());
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001216}
1217
1218// This class exists so SkPicture can friend it and give it access to
1219// the 'partialReplay' method.
1220class SkPictureRecorderReplayTester {
1221public:
1222 static SkPicture* Copy(SkPictureRecorder* recorder) {
1223 SkPictureRecorder recorder2;
1224
robertphillips9f1c2412014-06-09 06:25:34 -07001225 SkCanvas* canvas = recorder2.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001226
1227 recorder->partialReplay(canvas);
1228
1229 return recorder2.endRecording();
1230 }
1231};
1232
robertphillips9058d602014-06-10 11:45:46 -07001233static void create_imbalance(SkCanvas* canvas) {
1234 SkRect clipRect = SkRect::MakeWH(2, 2);
1235 SkRect drawRect = SkRect::MakeWH(10, 10);
1236 canvas->save();
1237 canvas->clipRect(clipRect, SkRegion::kReplace_Op);
1238 canvas->translate(1.0f, 1.0f);
1239 SkPaint p;
1240 p.setColor(SK_ColorGREEN);
1241 canvas->drawRect(drawRect, p);
1242 // no restore
1243}
1244
1245// This tests that replaying a potentially unbalanced picture into a canvas
1246// doesn't affect the canvas' save count or matrix/clip state.
1247static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) {
1248 SkBitmap bm;
1249 bm.allocN32Pixels(4, 3);
1250 SkCanvas canvas(bm);
1251
1252 int beforeSaveCount = canvas.getSaveCount();
1253
1254 SkMatrix beforeMatrix = canvas.getTotalMatrix();
1255
1256 SkRect beforeClip;
1257
1258 canvas.getClipBounds(&beforeClip);
1259
1260 canvas.drawPicture(picture);
1261
1262 REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount());
1263 REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix());
1264
1265 SkRect afterClip;
1266
1267 canvas.getClipBounds(&afterClip);
1268
1269 REPORTER_ASSERT(reporter, afterClip == beforeClip);
1270}
1271
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001272// Test out SkPictureRecorder::partialReplay
1273DEF_TEST(PictureRecorder_replay, reporter) {
1274 // check save/saveLayer state
1275 {
1276 SkPictureRecorder recorder;
1277
robertphillips9f1c2412014-06-09 06:25:34 -07001278 SkCanvas* canvas = recorder.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001279
1280 canvas->saveLayer(NULL, NULL);
1281
1282 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1283
1284 // The extra save and restore comes from the Copy process.
1285 check_save_state(reporter, copy, 2, 1, 3);
1286
1287 canvas->saveLayer(NULL, NULL);
1288
1289 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1290
1291 check_save_state(reporter, final, 1, 2, 3);
1292
1293 // The copy shouldn't pick up any operations added after it was made
1294 check_save_state(reporter, copy, 2, 1, 3);
1295 }
1296
1297 // (partially) check leakage of draw ops
1298 {
1299 SkPictureRecorder recorder;
1300
robertphillips9f1c2412014-06-09 06:25:34 -07001301 SkCanvas* canvas = recorder.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001302
1303 SkRect r = SkRect::MakeWH(5, 5);
1304 SkPaint p;
1305
1306 canvas->drawRect(r, p);
1307
1308 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1309
1310 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1311
1312 SkBitmap bm;
1313 make_bm(&bm, 10, 10, SK_ColorRED, true);
1314
1315 r.offset(5.0f, 5.0f);
1316 canvas->drawBitmapRectToRect(bm, NULL, r);
1317
1318 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1319 REPORTER_ASSERT(reporter, final->willPlayBackBitmaps());
1320
1321 REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID());
1322
1323 // The snapshot shouldn't pick up any operations added after it was made
1324 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1325 }
robertphillips9058d602014-06-10 11:45:46 -07001326
1327 // Recreate the Android partialReplay test case
1328 {
1329 SkPictureRecorder recorder;
1330
1331 SkCanvas* canvas = recorder.beginRecording(4, 3, NULL, 0);
1332 create_imbalance(canvas);
1333
1334 int expectedSaveCount = canvas->getSaveCount();
1335
1336 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1337 check_balance(reporter, copy);
1338
1339 REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount());
1340
1341 // End the recording of source to test the picture finalization
1342 // process isn't complicated by the partialReplay step
1343 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1344 }
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001345}
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001346
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001347static void test_unbalanced_save_restores(skiatest::Reporter* reporter) {
1348 SkCanvas testCanvas(100, 100);
1349 set_canvas_to_save_count_4(&testCanvas);
1350
1351 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1352
1353 SkPaint paint;
1354 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000);
1355
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001356 SkPictureRecorder recorder;
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001357
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001358 {
1359 // Create picture with 2 unbalanced saves
robertphillips9f1c2412014-06-09 06:25:34 -07001360 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001361 canvas->save();
1362 canvas->translate(10, 10);
1363 canvas->drawRect(rect, paint);
1364 canvas->save();
1365 canvas->translate(10, 10);
1366 canvas->drawRect(rect, paint);
1367 SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording());
1368
robertphillips9b14f262014-06-04 05:40:44 -07001369 testCanvas.drawPicture(extraSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001370 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1371 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001372
1373 set_canvas_to_save_count_4(&testCanvas);
1374
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001375 {
1376 // Create picture with 2 unbalanced restores
robertphillips9f1c2412014-06-09 06:25:34 -07001377 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001378 canvas->save();
1379 canvas->translate(10, 10);
1380 canvas->drawRect(rect, paint);
1381 canvas->save();
1382 canvas->translate(10, 10);
1383 canvas->drawRect(rect, paint);
1384 canvas->restore();
1385 canvas->restore();
1386 canvas->restore();
1387 canvas->restore();
1388 SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording());
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001389
robertphillips9b14f262014-06-04 05:40:44 -07001390 testCanvas.drawPicture(extraRestorePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001391 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1392 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001393
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001394 set_canvas_to_save_count_4(&testCanvas);
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001395
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001396 {
robertphillips9f1c2412014-06-09 06:25:34 -07001397 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001398 canvas->translate(10, 10);
1399 canvas->drawRect(rect, paint);
1400 SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording());
1401
robertphillips9b14f262014-06-04 05:40:44 -07001402 testCanvas.drawPicture(noSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001403 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1404 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity());
1405 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001406}
1407
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001408static void test_peephole() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001409 SkRandom rand;
reed@google.com21b519d2012-10-02 17:42:15 +00001410
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001411 SkPictureRecorder recorder;
1412
reed@google.com21b519d2012-10-02 17:42:15 +00001413 for (int j = 0; j < 100; j++) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001414 SkRandom rand2(rand); // remember the seed
reed@google.com21b519d2012-10-02 17:42:15 +00001415
robertphillips9f1c2412014-06-09 06:25:34 -07001416 SkCanvas* canvas = recorder.beginRecording(100, 100);
reed@google.com21b519d2012-10-02 17:42:15 +00001417
1418 for (int i = 0; i < 1000; ++i) {
1419 rand_op(canvas, rand);
1420 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001421 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
jvanverth@google.comc490f802013-03-04 13:56:38 +00001422
1423 rand = rand2;
reed@google.com21b519d2012-10-02 17:42:15 +00001424 }
1425
1426 {
robertphillips9f1c2412014-06-09 06:25:34 -07001427 SkCanvas* canvas = recorder.beginRecording(100, 100);
reed@google.com21b519d2012-10-02 17:42:15 +00001428 SkRect rect = SkRect::MakeWH(50, 50);
skia.committer@gmail.com52c24372012-10-03 02:01:13 +00001429
reed@google.com21b519d2012-10-02 17:42:15 +00001430 for (int i = 0; i < 100; ++i) {
1431 canvas->save();
1432 }
1433 while (canvas->getSaveCount() > 1) {
1434 canvas->clipRect(rect);
1435 canvas->restore();
1436 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001437 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
reed@google.com21b519d2012-10-02 17:42:15 +00001438 }
1439}
1440
scroggo@google.com4b90b112012-12-04 15:08:56 +00001441#ifndef SK_DEBUG
1442// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
1443// should never do this.
1444static void test_bad_bitmap() {
1445 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
1446 // fail.
1447 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001448 bm.setInfo(SkImageInfo::MakeN32Premul(100, 100));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001449 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001450 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001451 recordingCanvas->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001452 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.com4b90b112012-12-04 15:08:56 +00001453
1454 SkCanvas canvas;
robertphillips9b14f262014-06-04 05:40:44 -07001455 canvas.drawPicture(picture);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001456}
1457#endif
1458
reed@google.com672588b2014-01-08 15:42:01 +00001459static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001460 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001461}
1462
1463static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001464 SkPictureRecorder recorder;
mtklein87c41382014-09-08 07:31:18 -07001465 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bitmap.width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -07001466 SkIntToScalar(bitmap.height()));
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001467 canvas->drawBitmap(bitmap, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001468 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1469
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001470 SkDynamicMemoryWStream wStream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001471 picture->serialize(&wStream, &encode_bitmap_to_data);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001472 return wStream.copyToData();
1473}
1474
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001475struct ErrorContext {
1476 int fErrors;
1477 skiatest::Reporter* fReporter;
1478};
1479
1480static void assert_one_parse_error_cb(SkError error, void* context) {
1481 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
1482 errorContext->fErrors++;
1483 // This test only expects one error, and that is a kParseError. If there are others,
1484 // there is some unknown problem.
1485 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors,
1486 "This threw more errors than expected.");
1487 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error,
1488 SkGetLastErrorString());
1489}
1490
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001491static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
1492 // Create a bitmap that will be encoded.
1493 SkBitmap original;
1494 make_bm(&original, 100, 100, SK_ColorBLUE, true);
1495 SkDynamicMemoryWStream wStream;
1496 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
1497 return;
1498 }
1499 SkAutoDataUnref data(wStream.copyToData());
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001500
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001501 SkBitmap bm;
halcanary@google.com3d50ea12014-01-02 13:15:13 +00001502 bool installSuccess = SkInstallDiscardablePixelRef(
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +00001503 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm);
reed@google.combf790232013-12-13 19:45:58 +00001504 REPORTER_ASSERT(reporter, installSuccess);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001505
1506 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
1507 // Flattening original will follow the old path of performing an encode, while flattening bm
1508 // will use the already encoded data.
1509 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
1510 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
1511 REPORTER_ASSERT(reporter, picture1->equals(picture2));
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001512 // Now test that a parse error was generated when trying to create a new SkPicture without
1513 // providing a function to decode the bitmap.
1514 ErrorContext context;
1515 context.fErrors = 0;
1516 context.fReporter = reporter;
1517 SkSetErrorCallback(assert_one_parse_error_cb, &context);
1518 SkMemoryStream pictureStream(picture1);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001519 SkClearLastError();
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001520 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL));
1521 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001522 SkClearLastError();
1523 SkSetErrorCallback(NULL, NULL);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001524}
1525
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001526static void test_clip_bound_opt(skiatest::Reporter* reporter) {
1527 // Test for crbug.com/229011
1528 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
1529 SkIntToScalar(2), SkIntToScalar(2));
1530 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
1531 SkIntToScalar(1), SkIntToScalar(1));
1532 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
1533 SkIntToScalar(1), SkIntToScalar(1));
1534
1535 SkPath invPath;
1536 invPath.addOval(rect1);
1537 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
1538 SkPath path;
1539 path.addOval(rect2);
1540 SkPath path2;
1541 path2.addOval(rect3);
1542 SkIRect clipBounds;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001543 SkPictureRecorder recorder;
reedd9544982014-09-09 18:46:22 -07001544
1545 // Testing conservative-raster-clip that is enabled by PictureRecord
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001546 {
robertphillips9f1c2412014-06-09 06:25:34 -07001547 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001548 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1549 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1550 REPORTER_ASSERT(reporter, true == nonEmpty);
1551 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1552 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1553 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1554 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1555 }
1556 {
robertphillips9f1c2412014-06-09 06:25:34 -07001557 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001558 canvas->clipPath(path, SkRegion::kIntersect_Op);
1559 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1560 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1561 REPORTER_ASSERT(reporter, true == nonEmpty);
1562 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1563 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1564 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1565 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1566 }
1567 {
robertphillips9f1c2412014-06-09 06:25:34 -07001568 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001569 canvas->clipPath(path, SkRegion::kIntersect_Op);
1570 canvas->clipPath(invPath, SkRegion::kUnion_Op);
1571 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1572 REPORTER_ASSERT(reporter, true == nonEmpty);
1573 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1574 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1575 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1576 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1577 }
1578 {
robertphillips9f1c2412014-06-09 06:25:34 -07001579 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001580 canvas->clipPath(path, SkRegion::kDifference_Op);
1581 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1582 REPORTER_ASSERT(reporter, true == nonEmpty);
1583 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1584 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1585 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1586 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1587 }
1588 {
robertphillips9f1c2412014-06-09 06:25:34 -07001589 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001590 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
1591 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1592 // True clip is actually empty in this case, but the best
1593 // determination we can make using only bounds as input is that the
1594 // clip is included in the bounds of 'path'.
1595 REPORTER_ASSERT(reporter, true == nonEmpty);
1596 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1597 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1598 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1599 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1600 }
1601 {
robertphillips9f1c2412014-06-09 06:25:34 -07001602 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001603 canvas->clipPath(path, SkRegion::kIntersect_Op);
1604 canvas->clipPath(path2, SkRegion::kXOR_Op);
1605 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1606 REPORTER_ASSERT(reporter, true == nonEmpty);
1607 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
1608 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
1609 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1610 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1611 }
1612}
1613
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001614/**
1615 * A canvas that records the number of clip commands.
1616 */
1617class ClipCountingCanvas : public SkCanvas {
1618public:
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001619 ClipCountingCanvas(int width, int height)
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001620 : INHERITED(width, height)
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001621 , fClipCount(0){
1622 }
1623
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001624 virtual void onClipRect(const SkRect& r,
1625 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001626 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001627 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001628 this->INHERITED::onClipRect(r, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001629 }
1630
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001631 virtual void onClipRRect(const SkRRect& rrect,
1632 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001633 ClipEdgeStyle edgeStyle)SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001634 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001635 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001636 }
1637
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001638 virtual void onClipPath(const SkPath& path,
1639 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001640 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001641 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001642 this->INHERITED::onClipPath(path, op, edgeStyle);
1643 }
1644
1645 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE {
1646 fClipCount += 1;
1647 this->INHERITED::onClipRegion(deviceRgn, op);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001648 }
1649
1650 unsigned getClipCount() const { return fClipCount; }
1651
1652private:
1653 unsigned fClipCount;
1654
1655 typedef SkCanvas INHERITED;
1656};
1657
1658static void test_clip_expansion(skiatest::Reporter* reporter) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001659 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001660 SkCanvas* canvas = recorder.beginRecording(10, 10);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001661
1662 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
1663 // The following expanding clip should not be skipped.
1664 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
1665 // Draw something so the optimizer doesn't just fold the world.
1666 SkPaint p;
1667 p.setColor(SK_ColorBLUE);
1668 canvas->drawPaint(p);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001669 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001670
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001671 ClipCountingCanvas testCanvas(10, 10);
robertphillipsc5ba71d2014-09-04 08:42:50 -07001672 picture->playback(&testCanvas);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001673
1674 // Both clips should be present on playback.
1675 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
1676}
1677
tomhudson@google.com381010e2013-10-24 11:12:47 +00001678static void test_hierarchical(skiatest::Reporter* reporter) {
1679 SkBitmap bm;
1680 make_bm(&bm, 10, 10, SK_ColorRED, true);
1681
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001682 SkPictureRecorder recorder;
tomhudson@google.com381010e2013-10-24 11:12:47 +00001683
robertphillips9f1c2412014-06-09 06:25:34 -07001684 recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001685 SkAutoTUnref<SkPicture> childPlain(recorder.endRecording());
1686 REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0
tomhudson@google.com381010e2013-10-24 11:12:47 +00001687
robertphillips9f1c2412014-06-09 06:25:34 -07001688 recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001689 SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording());
1690 REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1
tomhudson@google.com381010e2013-10-24 11:12:47 +00001691
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001692 {
robertphillips9f1c2412014-06-09 06:25:34 -07001693 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips9b14f262014-06-04 05:40:44 -07001694 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001695 SkAutoTUnref<SkPicture> parentPP(recorder.endRecording());
1696 REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0
1697 }
1698 {
robertphillips9f1c2412014-06-09 06:25:34 -07001699 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips9b14f262014-06-04 05:40:44 -07001700 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001701 SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording());
1702 REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1
1703 }
1704 {
robertphillips9f1c2412014-06-09 06:25:34 -07001705 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001706 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001707 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001708 SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording());
1709 REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1
1710 }
1711 {
robertphillips9f1c2412014-06-09 06:25:34 -07001712 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001713 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001714 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001715 SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording());
1716 REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2
1717 }
tomhudson@google.com381010e2013-10-24 11:12:47 +00001718}
1719
robertphillips@google.comd5500882014-04-02 23:51:13 +00001720static void test_gen_id(skiatest::Reporter* reporter) {
1721
Robert Phillipscfaeec42014-07-13 12:00:50 -04001722 SkPictureRecorder recorder;
1723 recorder.beginRecording(0, 0);
1724 SkAutoTUnref<SkPicture> empty(recorder.endRecording());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001725
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001726 // Empty pictures should still have a valid ID
Robert Phillipscfaeec42014-07-13 12:00:50 -04001727 REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001728
robertphillips9f1c2412014-06-09 06:25:34 -07001729 SkCanvas* canvas = recorder.beginRecording(1, 1);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001730 canvas->drawARGB(255, 255, 255, 255);
1731 SkAutoTUnref<SkPicture> hasData(recorder.endRecording());
1732 // picture should have a non-zero id after recording
1733 REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001734
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001735 // both pictures should have different ids
Robert Phillipscfaeec42014-07-13 12:00:50 -04001736 REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001737}
1738
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00001739DEF_TEST(Picture, reporter) {
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001740#ifdef SK_DEBUG
robertphillipsdb539902014-07-01 08:47:04 -07001741 test_deleting_empty_picture();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001742 test_serializing_empty_picture();
scroggo@google.com4b90b112012-12-04 15:08:56 +00001743#else
1744 test_bad_bitmap();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001745#endif
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001746 test_unbalanced_save_restores(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001747 test_peephole();
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001748#if SK_SUPPORT_GPU
mtklein8e126562014-10-01 09:29:35 -07001749 test_gpu_veto(reporter);
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001750#endif
mtklein8e126562014-10-01 09:29:35 -07001751 test_has_text(reporter);
1752 test_analysis(reporter);
reed@google.comfe7b1ed2012-11-29 21:00:39 +00001753 test_gatherpixelrefs(reporter);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +00001754 test_gatherpixelrefsandrects(reporter);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001755 test_bitmap_with_encoded_data(reporter);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001756 test_clip_bound_opt(reporter);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001757 test_clip_expansion(reporter);
tomhudson@google.com381010e2013-10-24 11:12:47 +00001758 test_hierarchical(reporter);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001759 test_gen_id(reporter);
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001760}
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001761
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001762#if SK_SUPPORT_GPU
1763DEF_GPUTEST(GPUPicture, reporter, factory) {
1764 test_gpu_picture_optimization(reporter, factory);
1765}
1766#endif
1767
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001768static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) {
1769 const SkPaint paint;
1770 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f };
1771 const SkIRect irect = { 2, 2, 3, 3 };
1772
1773 // Don't care what these record, as long as they're legal.
1774 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint);
1775 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag);
1776 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint);
1777 canvas->drawBitmapNine(bitmap, irect, rect, &paint);
1778 canvas->drawSprite(bitmap, 1, 1);
1779}
1780
1781static void test_draw_bitmaps(SkCanvas* canvas) {
1782 SkBitmap empty;
1783 draw_bitmaps(empty, canvas);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001784 empty.setInfo(SkImageInfo::MakeN32Premul(10, 10));
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001785 draw_bitmaps(empty, canvas);
1786}
1787
1788DEF_TEST(Picture_EmptyBitmap, r) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001789 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001790 test_draw_bitmaps(recorder.beginRecording(10, 10));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001791 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001792}
1793
1794DEF_TEST(Canvas_EmptyBitmap, r) {
1795 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001796 dst.allocN32Pixels(10, 10);
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001797 SkCanvas canvas(dst);
1798
1799 test_draw_bitmaps(&canvas);
1800}
dneto3f22e8c2014-07-30 15:42:22 -07001801
1802DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) {
1803 // This test is from crbug.com/344987.
1804 // The commands are:
1805 // saveLayer with paint that modifies alpha
1806 // drawBitmapRectToRect
1807 // drawBitmapRectToRect
1808 // restore
1809 // The bug was that this structure was modified so that:
1810 // - The saveLayer and restore were eliminated
1811 // - The alpha was only applied to the first drawBitmapRectToRect
1812
1813 // This test draws blue and red squares inside a 50% transparent
1814 // layer. Both colours should show up muted.
1815 // When the bug is present, the red square (the second bitmap)
1816 // shows upwith full opacity.
1817
1818 SkBitmap blueBM;
1819 make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true);
1820 SkBitmap redBM;
1821 make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true);
1822 SkPaint semiTransparent;
1823 semiTransparent.setAlpha(0x80);
1824
1825 SkPictureRecorder recorder;
1826 SkCanvas* canvas = recorder.beginRecording(100, 100);
1827 canvas->drawARGB(0, 0, 0, 0);
1828
1829 canvas->saveLayer(0, &semiTransparent);
1830 canvas->drawBitmap(blueBM, 25, 25);
1831 canvas->drawBitmap(redBM, 50, 50);
1832 canvas->restore();
1833
1834 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1835
1836 // Now replay the picture back on another canvas
1837 // and check a couple of its pixels.
1838 SkBitmap replayBM;
1839 make_bm(&replayBM, 100, 100, SK_ColorBLACK, false);
1840 SkCanvas replayCanvas(replayBM);
robertphillipsc5ba71d2014-09-04 08:42:50 -07001841 picture->playback(&replayCanvas);
dneto3f22e8c2014-07-30 15:42:22 -07001842 replayCanvas.flush();
1843
1844 // With the bug present, at (55, 55) we would get a fully opaque red
1845 // intead of a dark red.
1846 REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080);
1847 REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000);
1848}
mtklein3e8232b2014-08-18 13:39:11 -07001849
1850struct CountingBBH : public SkBBoxHierarchy {
1851 mutable int searchCalls;
1852
1853 CountingBBH() : searchCalls(0) {}
1854
mtklein6bd41962014-10-02 07:41:56 -07001855 virtual void search(const SkRect& query, SkTDArray<unsigned>* results) const SK_OVERRIDE {
mtklein3e8232b2014-08-18 13:39:11 -07001856 this->searchCalls++;
1857 }
1858
mtklein4477c3c2014-10-27 10:27:10 -07001859 virtual void insert(SkAutoTMalloc<SkRect>*, int) SK_OVERRIDE {}
mtklein3e8232b2014-08-18 13:39:11 -07001860};
1861
1862class SpoonFedBBHFactory : public SkBBHFactory {
1863public:
1864 explicit SpoonFedBBHFactory(SkBBoxHierarchy* bbh) : fBBH(bbh) {}
1865 virtual SkBBoxHierarchy* operator()(int width, int height) const {
1866 return SkRef(fBBH);
1867 }
1868private:
1869 SkBBoxHierarchy* fBBH;
1870};
1871
1872// When the canvas clip covers the full picture, we don't need to call the BBH.
1873DEF_TEST(Picture_SkipBBH, r) {
1874 CountingBBH bbh;
1875 SpoonFedBBHFactory factory(&bbh);
1876
1877 SkPictureRecorder recorder;
1878 recorder.beginRecording(320, 240, &factory);
1879 SkAutoTUnref<const SkPicture> picture(recorder.endRecording());
1880
1881 SkCanvas big(640, 480), small(300, 200);
1882
robertphillipsc5ba71d2014-09-04 08:42:50 -07001883 picture->playback(&big);
mtklein3e8232b2014-08-18 13:39:11 -07001884 REPORTER_ASSERT(r, bbh.searchCalls == 0);
1885
robertphillipsc5ba71d2014-09-04 08:42:50 -07001886 picture->playback(&small);
mtklein3e8232b2014-08-18 13:39:11 -07001887 REPORTER_ASSERT(r, bbh.searchCalls == 1);
1888}
mtkleind72094d2014-08-27 12:12:23 -07001889
1890DEF_TEST(Picture_BitmapLeak, r) {
1891 SkBitmap mut, immut;
1892 mut.allocN32Pixels(300, 200);
1893 immut.allocN32Pixels(300, 200);
1894 immut.setImmutable();
1895 SkASSERT(!mut.isImmutable());
1896 SkASSERT(immut.isImmutable());
1897
1898 // No one can hold a ref on our pixels yet.
1899 REPORTER_ASSERT(r, mut.pixelRef()->unique());
1900 REPORTER_ASSERT(r, immut.pixelRef()->unique());
1901
1902 SkPictureRecorder rec;
1903 SkCanvas* canvas = rec.beginRecording(1920, 1200);
1904 canvas->drawBitmap(mut, 0, 0);
1905 canvas->drawBitmap(immut, 800, 600);
1906 SkAutoTDelete<const SkPicture> pic(rec.endRecording());
1907
1908 // The picture shares the immutable pixels but copies the mutable ones.
1909 REPORTER_ASSERT(r, mut.pixelRef()->unique());
1910 REPORTER_ASSERT(r, !immut.pixelRef()->unique());
1911
1912 // When the picture goes away, it's just our bitmaps holding the refs.
1913 pic.reset(NULL);
1914 REPORTER_ASSERT(r, mut.pixelRef()->unique());
1915 REPORTER_ASSERT(r, immut.pixelRef()->unique());
1916}