blob: fa4dc101421da570449bbeba487624365bf5a091 [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"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000011#include "SkColorPriv.h"
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +000012#include "SkDashPathEffect.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000013#include "SkData.h"
reed@google.combf790232013-12-13 19:45:58 +000014#include "SkDecodingImageGenerator.h"
scroggo@google.com49ce11b2013-04-25 18:29:32 +000015#include "SkError.h"
halcanary@google.com3d50ea12014-01-02 13:15:13 +000016#include "SkImageEncoder.h"
17#include "SkImageGenerator.h"
reed@google.com21b519d2012-10-02 17:42:15 +000018#include "SkPaint.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000019#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000020#include "SkPictureRecorder.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000021#include "SkPictureUtils.h"
mtkleind72094d2014-08-27 12:12:23 -070022#include "SkPixelRef.h"
reed@google.com72aa79c2013-01-24 18:27:42 +000023#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000024#include "SkRandom.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000025#include "SkShader.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000026#include "SkStream.h"
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000027
28#if SK_SUPPORT_GPU
29#include "SkSurface.h"
30#include "GrContextFactory.h"
31#include "GrPictureUtils.h"
32#endif
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000033#include "Test.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000034
reed@google.com47b679b2014-05-14 18:58:16 +000035#include "SkLumaColorFilter.h"
36#include "SkColorFilterImageFilter.h"
37
robertphillips@google.comed9866c2014-01-09 19:20:45 +000038static const int gColorScale = 30;
39static const int gColorOffset = 60;
reed@google.comfe7b1ed2012-11-29 21:00:39 +000040
41static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000042 bm->allocN32Pixels(w, h);
reed@google.comfe7b1ed2012-11-29 21:00:39 +000043 bm->eraseColor(color);
44 if (immutable) {
45 bm->setImmutable();
46 }
47}
48
robertphillips@google.comfe5824a2014-01-09 19:45:29 +000049static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000050 SkASSERT(w % 2 == 0);
51 SkASSERT(h % 2 == 0);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000052 bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType,
53 kPremul_SkAlphaType));
robertphillips@google.comed9866c2014-01-09 19:20:45 +000054 SkAutoLockPixels lock(*bm);
55 for (int y = 0; y < h; y += 2) {
56 uint8_t* s = bm->getAddr8(0, y);
57 for (int x = 0; x < w; x += 2) {
58 *s++ = 0xFF;
59 *s++ = 0x00;
60 }
61 s = bm->getAddr8(0, y + 1);
62 for (int x = 0; x < w; x += 2) {
63 *s++ = 0x00;
64 *s++ = 0xFF;
65 }
66 }
67 if (immutable) {
68 bm->setImmutable();
69 }
70}
reed@google.comfe7b1ed2012-11-29 21:00:39 +000071
robertphillips@google.comed9866c2014-01-09 19:20:45 +000072static void init_paint(SkPaint* paint, const SkBitmap &bm) {
73 SkShader* shader = SkShader::CreateBitmapShader(bm,
74 SkShader::kClamp_TileMode,
75 SkShader::kClamp_TileMode);
76 paint->setShader(shader)->unref();
77}
78
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +000079typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000080 const SkBitmap&, const SkPoint&,
81 SkTDArray<SkPixelRef*>* usedPixRefs);
robertphillips@google.comed9866c2014-01-09 19:20:45 +000082
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000083static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000084 const SkBitmap& altBM, const SkPoint& pos,
85 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000086 SkPaint paint;
87 init_paint(&paint, bm);
88
89 canvas->drawPaint(paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000090 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +000091}
92
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000093static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000094 const SkBitmap& altBM, const SkPoint& pos,
95 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000096 SkPaint paint;
97 init_paint(&paint, bm);
98
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000099 // draw a rect
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000100 SkPoint points[5] = {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000101 { pos.fX, pos.fY },
102 { pos.fX + bm.width() - 1, pos.fY },
103 { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 },
104 { pos.fX, pos.fY + bm.height() - 1 },
105 { pos.fX, pos.fY },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000106 };
107
108 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000109 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000110}
111
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000112static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000113 const SkBitmap& altBM, const SkPoint& pos,
114 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000115 SkPaint paint;
116 init_paint(&paint, bm);
117
118 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
119 r.offset(pos.fX, pos.fY);
120
121 canvas->drawRect(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000122 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000123}
124
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000125static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000126 const SkBitmap& altBM, const SkPoint& pos,
127 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000128 SkPaint paint;
129 init_paint(&paint, bm);
130
131 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
132 r.offset(pos.fX, pos.fY);
133
134 canvas->drawOval(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000135 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000136}
137
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000138static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000139 const SkBitmap& altBM, const SkPoint& pos,
140 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000141 SkPaint paint;
142 init_paint(&paint, bm);
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000143
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000144 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
145 r.offset(pos.fX, pos.fY);
146
147 SkRRect rr;
148 rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4);
149 canvas->drawRRect(rr, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000150 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000151}
152
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000153static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000154 const SkBitmap& altBM, const SkPoint& pos,
155 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000156 SkPaint paint;
157 init_paint(&paint, bm);
158
159 SkPath path;
160 path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height()));
161 path.lineTo(SkIntToScalar(bm.width()), 0);
162 path.close();
163 path.offset(pos.fX, pos.fY);
164
165 canvas->drawPath(path, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000166 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000167}
168
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000169static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000170 const SkBitmap& altBM, const SkPoint& pos,
171 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000172 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000173 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000174}
175
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000176static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000177 const SkBitmap& altBM, const SkPoint& pos,
178 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000179 SkPaint paint;
180 init_paint(&paint, bm);
181
182 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
183 canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000184 *usedPixRefs->append() = bm.pixelRef();
185 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000186}
187
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000188static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000189 const SkBitmap& altBM, const SkPoint& pos,
190 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000191 const SkMatrix& ctm = canvas->getTotalMatrix();
192
193 SkPoint p(pos);
194 ctm.mapPoints(&p, 1);
195
196 canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000197 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000198}
199
200#if 0
201// Although specifiable, this case doesn't seem to make sense (i.e., the
202// bitmap in the shader is never used).
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000203static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000204 const SkBitmap& altBM, const SkPoint& pos,
205 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000206 SkPaint paint;
207 init_paint(&paint, bm);
208
209 const SkMatrix& ctm = canvas->getTotalMatrix();
210
211 SkPoint p(pos);
212 ctm.mapPoints(&p, 1);
213
214 canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000215 *usedPixRefs->append() = bm.pixelRef();
216 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000217}
218#endif
219
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000220static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000221 const SkBitmap& altBM, const SkPoint& pos,
222 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000223 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
224
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000225 r.offset(pos.fX, pos.fY);
226 canvas->drawBitmapRectToRect(bm, NULL, r, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000227 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000228}
229
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000230static void drawbitmaprect_withshader_proc(SkCanvas* canvas,
231 const SkBitmap& bm,
232 const SkBitmap& altBM,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000233 const SkPoint& pos,
234 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000235 SkPaint paint;
236 init_paint(&paint, bm);
237
238 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000239 r.offset(pos.fX, pos.fY);
240
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000241 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
242 canvas->drawBitmapRectToRect(altBM, NULL, r, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000243 *usedPixRefs->append() = bm.pixelRef();
244 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000245}
246
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000247static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000248 const SkBitmap& altBM, const SkPoint& pos,
249 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000250 SkPaint paint;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000251 init_paint(&paint, bm);
252 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
253
254 canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000255 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000256}
257
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000258static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000259 const SkBitmap& altBM, const SkPoint& pos,
260 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000261 SkPaint paint;
262 init_paint(&paint, bm);
263 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
264
265 SkPoint point = { pos.fX, pos.fY + bm.height() };
266 canvas->drawPosText("O", 1, &point, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000267 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000268}
269
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000270static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000271 const SkBitmap& altBM, const SkPoint& pos,
272 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000273 SkPaint paint;
274
275 init_paint(&paint, bm);
276 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
277
278 SkPath path;
279 path.lineTo(SkIntToScalar(bm.width()), 0);
280 path.offset(pos.fX, pos.fY+bm.height());
281
282 canvas->drawTextOnPath("O", 1, path, NULL, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000283 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000284}
285
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000286static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000287 const SkBitmap& altBM, const SkPoint& pos,
288 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000289 SkPaint paint;
290 init_paint(&paint, bm);
291
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000292 SkPoint verts[4] = {
293 { pos.fX, pos.fY },
294 { pos.fX + bm.width(), pos.fY },
295 { pos.fX + bm.width(), pos.fY + bm.height() },
296 { pos.fX, pos.fY + bm.height() }
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000297 };
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000298 SkPoint texs[4] = { { 0, 0 },
299 { SkIntToScalar(bm.width()), 0 },
300 { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000301 { 0, SkIntToScalar(bm.height()) } };
302 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 };
303
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000304 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL,
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000305 indices, 6, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000306 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000307}
308
309// Return a picture with the bitmaps drawn at the specified positions.
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000310static SkPicture* record_bitmaps(const SkBitmap bm[],
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000311 const SkPoint pos[],
312 SkTDArray<SkPixelRef*> analytic[],
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000313 int count,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000314 DrawBitmapProc proc) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000315 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -0700316 SkCanvas* canvas = recorder.beginRecording(1000, 1000);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000317 for (int i = 0; i < count; ++i) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000318 analytic[i].rewind();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000319 canvas->save();
320 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY,
321 SkIntToScalar(bm[i].width()),
322 SkIntToScalar(bm[i].height()));
323 canvas->clipRect(clipRect, SkRegion::kIntersect_Op);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000324 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000325 canvas->restore();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000326 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000327 return recorder.endRecording();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000328}
329
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000330static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000331 rect->fLeft = rand.nextRangeScalar(-W, 2*W);
332 rect->fTop = rand.nextRangeScalar(-H, 2*H);
333 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W);
334 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H);
335
336 // we integralize rect to make our tests more predictable, since Gather is
337 // a little sloppy.
338 SkIRect ir;
339 rect->round(&ir);
340 rect->set(ir);
341}
342
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000343static void draw(SkPicture* pic, int width, int height, SkBitmap* result) {
344 make_bm(result, width, height, SK_ColorBLACK, false);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000345
346 SkCanvas canvas(*result);
robertphillips9b14f262014-06-04 05:40:44 -0700347 canvas.drawPicture(pic);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000348}
349
350template <typename T> int find_index(const T* array, T elem, int count) {
351 for (int i = 0; i < count; ++i) {
352 if (array[i] == elem) {
353 return i;
354 }
355 }
356 return -1;
357}
358
359// Return true if 'ref' is found in array[]
360static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) {
361 return find_index<const SkPixelRef*>(array, ref, count) >= 0;
362}
363
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000364// Look at each pixel that is inside 'subset', and if its color appears in
365// colors[], find the corresponding value in refs[] and append that ref into
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000366// array, skipping duplicates of the same value.
367// Note that gathering pixelRefs from rendered colors suffers from the problem
368// that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color)
369// isn't easy to reconstruct.
370static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[],
371 int count, SkTDArray<SkPixelRef*>* array,
372 const SkRect& subset) {
373 SkIRect ir;
374 subset.roundOut(&ir);
375
376 if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) {
377 return;
378 }
379
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000380 // Since we only want to return unique values in array, when we scan we just
381 // set a bit for each index'd color found. In practice we only have a few
382 // distinct colors, so we just use an int's bits as our array. Hence the
383 // assert that count <= number-of-bits-in-our-int.
384 SkASSERT((unsigned)count <= 32);
385 uint32_t bitarray = 0;
386
387 SkAutoLockPixels alp(bm);
388
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000389 for (int y = ir.fTop; y < ir.fBottom; ++y) {
390 for (int x = ir.fLeft; x < ir.fRight; ++x) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000391 SkPMColor pmc = *bm.getAddr32(x, y);
392 // the only good case where the color is not found would be if
393 // the color is transparent, meaning no bitmap was drawn in that
394 // pixel.
395 if (pmc) {
bsalomon@google.comc3d753e2013-01-08 17:24:44 +0000396 uint32_t index = SkGetPackedR32(pmc);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000397 SkASSERT(SkGetPackedG32(pmc) == index);
398 SkASSERT(SkGetPackedB32(pmc) == index);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000399 if (0 == index) {
400 continue; // background color
401 }
402 SkASSERT(0 == (index - gColorOffset) % gColorScale);
403 index = (index - gColorOffset) / gColorScale;
bsalomon@google.com5f429b02013-01-08 18:42:20 +0000404 SkASSERT(static_cast<int>(index) < count);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000405 bitarray |= 1 << index;
406 }
407 }
408 }
409
410 for (int i = 0; i < count; ++i) {
411 if (bitarray & (1 << i)) {
412 *array->append() = refs[i];
413 }
414 }
415}
416
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000417static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h,
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000418 const SkTDArray<SkPixelRef*> analytic[],
419 int count,
420 SkTDArray<SkPixelRef*>* result,
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000421 const SkRect& subset) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000422 for (int i = 0; i < count; ++i) {
423 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h);
424
425 if (SkRect::Intersects(subset, rect)) {
426 result->append(analytic[i].count(), analytic[i].begin());
427 }
428 }
429}
430
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000431
432static const struct {
433 const DrawBitmapProc proc;
434 const char* const desc;
435} gProcs[] = {
436 {drawpaint_proc, "drawpaint"},
437 {drawpoints_proc, "drawpoints"},
438 {drawrect_proc, "drawrect"},
439 {drawoval_proc, "drawoval"},
440 {drawrrect_proc, "drawrrect"},
441 {drawpath_proc, "drawpath"},
442 {drawbitmap_proc, "drawbitmap"},
443 {drawbitmap_withshader_proc, "drawbitmap_withshader"},
444 {drawsprite_proc, "drawsprite"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000445#if 0
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000446 {drawsprite_withshader_proc, "drawsprite_withshader"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000447#endif
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000448 {drawbitmaprect_proc, "drawbitmaprect"},
449 {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"},
450 {drawtext_proc, "drawtext"},
451 {drawpostext_proc, "drawpostext"},
452 {drawtextonpath_proc, "drawtextonpath"},
453 {drawverts_proc, "drawverts"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000454};
455
456static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000457 // Our convention is that the color components contain an encoding of
458 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000459 // reserved for the background
460 for (int i = 0; i < num; ++i) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000461 make_bm(&bm[i], w, h,
462 SkColorSetARGB(0xFF,
463 gColorScale*i+gColorOffset,
464 gColorScale*i+gColorOffset,
465 gColorScale*i+gColorOffset),
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000466 true);
467 refs[i] = bm[i].pixelRef();
468 }
469
470 // The A8 alternate bitmaps are all BW checkerboards
471 for (int i = 0; i < num; ++i) {
472 make_checkerboard(&bm[num+i], w, h, true);
473 refs[num+i] = bm[num+i].pixelRef();
474 }
475}
476
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000477static void test_gatherpixelrefs(skiatest::Reporter* reporter) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000478 const int IW = 32;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000479 const int IH = IW;
480 const SkScalar W = SkIntToScalar(IW);
481 const SkScalar H = W;
482
483 static const int N = 4;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000484 SkBitmap bm[2*N];
485 SkPixelRef* refs[2*N];
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000486 SkTDArray<SkPixelRef*> analytic[N];
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000487
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000488 const SkPoint pos[N] = {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000489 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
490 };
491
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000492 create_textures(bm, refs, N, IW, IH);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000493
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000494 SkRandom rand;
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000495 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000496 SkAutoTUnref<SkPicture> pic(
497 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000498
tomhudson@google.com381010e2013-10-24 11:12:47 +0000499 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000500 // quick check for a small piece of each quadrant, which should just
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000501 // contain 1 or 2 bitmaps.
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000502 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
503 SkRect r;
504 r.set(2, 2, W - 2, H - 2);
505 r.offset(pos[i].fX, pos[i].fY);
506 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r));
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000507 if (!data) {
508 ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned "
509 "NULL for %s.", gProcs[k].desc);
510 continue;
511 }
512 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data();
513 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*));
514 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
515 if (1 == count) {
516 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
517 } else if (2 == count) {
518 REPORTER_ASSERT(reporter,
519 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
520 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
commit-bot@chromium.orgfe433c12013-07-09 16:04:32 +0000521 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000522 }
523
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000524 SkBitmap image;
525 draw(pic, 2*IW, 2*IH, &image);
526
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000527 // Test a bunch of random (mostly) rects, and compare the gather results
528 // with a deduced list of refs by looking at the colors drawn.
529 for (int j = 0; j < 100; ++j) {
530 SkRect r;
531 rand_rect(&r, rand, 2*W, 2*H);
532
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000533 SkTDArray<SkPixelRef*> fromImage;
534 gather_from_image(image, refs, N, &fromImage, r);
535
536 SkTDArray<SkPixelRef*> fromAnalytic;
537 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000538
539 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
540 size_t dataSize = data ? data->size() : 0;
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000541 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000542 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize);
543 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL;
544 SkAutoDataUnref adu(data);
545
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000546 // Everything that we saw drawn should appear in the analytic list
547 // but the analytic list may contain some pixelRefs that were not
548 // seen in the image (e.g., A8 textures used as masks)
549 for (int i = 0; i < fromImage.count(); ++i) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000550 if (-1 == fromAnalytic.find(fromImage[i])) {
551 ERRORF(reporter, "PixelRef missing %d %s",
552 i, gProcs[k].desc);
553 }
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000554 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000555
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000556 /*
557 * GatherPixelRefs is conservative, so it can return more bitmaps
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000558 * than are strictly required. Thus our check here is only that
559 * Gather didn't miss any that we actually needed. Even that isn't
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000560 * a strict requirement on Gather, which is meant to be quick and
561 * only mostly-correct, but at the moment this test should work.
562 */
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000563 for (int i = 0; i < fromAnalytic.count(); ++i) {
564 bool found = find(gatherRefs, fromAnalytic[i], gatherCount);
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000565 if (!found) {
566 ERRORF(reporter, "PixelRef missing %d %s",
567 i, gProcs[k].desc);
568 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000569#if 0
570 // enable this block of code to debug failures, as it will rerun
571 // the case that failed.
572 if (!found) {
573 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
574 size_t dataSize = data ? data->size() : 0;
575 }
576#endif
577 }
578 }
579 }
580}
581
tomhudson3a0f2792014-08-20 05:29:41 -0700582/* Hit a few SkPicture::Analysis cases not handled elsewhere. */
mtklein8e126562014-10-01 09:29:35 -0700583static void test_analysis(skiatest::Reporter* reporter) {
tomhudson3a0f2792014-08-20 05:29:41 -0700584 SkPictureRecorder recorder;
585
mtklein8e126562014-10-01 09:29:35 -0700586 SkCanvas* canvas = recorder.beginRecording(100, 100);
tomhudson3a0f2792014-08-20 05:29:41 -0700587 {
588 canvas->drawRect(SkRect::MakeWH(10, 10), SkPaint ());
589 }
590 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
591 REPORTER_ASSERT(reporter, !picture->willPlayBackBitmaps());
592
mtklein8e126562014-10-01 09:29:35 -0700593 canvas = recorder.beginRecording(100, 100);
tomhudson3a0f2792014-08-20 05:29:41 -0700594 {
595 SkPaint paint;
596 // CreateBitmapShader is too smart for us; an empty (or 1x1) bitmap shader
597 // gets optimized into a non-bitmap form, so we create a 2x2 bitmap here.
598 SkBitmap bitmap;
599 bitmap.allocPixels(SkImageInfo::MakeN32Premul(2, 2));
600 bitmap.eraseColor(SK_ColorBLUE);
601 *(bitmap.getAddr32(0, 0)) = SK_ColorGREEN;
602 SkShader* shader = SkShader::CreateBitmapShader(bitmap, SkShader::kClamp_TileMode,
603 SkShader::kClamp_TileMode);
604 paint.setShader(shader)->unref();
605 REPORTER_ASSERT(reporter,
606 shader->asABitmap(NULL, NULL, NULL) == SkShader::kDefault_BitmapType);
607
608 canvas->drawRect(SkRect::MakeWH(10, 10), paint);
609 }
610 picture.reset(recorder.endRecording());
611 REPORTER_ASSERT(reporter, picture->willPlayBackBitmaps());
612}
613
614
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000615static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) {
616 const int IW = 32;
617 const int IH = IW;
618 const SkScalar W = SkIntToScalar(IW);
619 const SkScalar H = W;
620
621 static const int N = 4;
622 SkBitmap bm[2*N];
623 SkPixelRef* refs[2*N];
624 SkTDArray<SkPixelRef*> analytic[N];
625
626 const SkPoint pos[N] = {
627 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
628 };
629
630 create_textures(bm, refs, N, IW, IH);
631
632 SkRandom rand;
633 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000634 SkAutoTUnref<SkPicture> pic(
635 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000636
637 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
638
639 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont(
640 new SkPictureUtils::SkPixelRefsAndRectsList);
641
642 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont);
643
644 // quick check for a small piece of each quadrant, which should just
645 // contain 1 or 2 bitmaps.
646 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
647 SkRect r;
648 r.set(2, 2, W - 2, H - 2);
649 r.offset(pos[i].fX, pos[i].fY);
650
651 SkTDArray<SkPixelRef*> gatheredRefs;
652 prCont->query(r, &gatheredRefs);
653
654 int count = gatheredRefs.count();
655 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
656 if (1 == count) {
657 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
658 } else if (2 == count) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000659 REPORTER_ASSERT(reporter,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000660 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
661 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
662 }
663 }
664
665 SkBitmap image;
666 draw(pic, 2*IW, 2*IH, &image);
667
668 // Test a bunch of random (mostly) rects, and compare the gather results
669 // with the analytic results and the pixel refs seen in a rendering.
670 for (int j = 0; j < 100; ++j) {
671 SkRect r;
672 rand_rect(&r, rand, 2*W, 2*H);
673
674 SkTDArray<SkPixelRef*> fromImage;
675 gather_from_image(image, refs, N, &fromImage, r);
676
677 SkTDArray<SkPixelRef*> fromAnalytic;
678 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
679
680 SkTDArray<SkPixelRef*> gatheredRefs;
681 prCont->query(r, &gatheredRefs);
682
683 // Everything that we saw drawn should appear in the analytic list
684 // but the analytic list may contain some pixelRefs that were not
685 // seen in the image (e.g., A8 textures used as masks)
686 for (int i = 0; i < fromImage.count(); ++i) {
687 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]));
688 }
689
690 // Everything in the analytic list should appear in the gathered
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000691 // list.
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000692 for (int i = 0; i < fromAnalytic.count(); ++i) {
693 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i]));
694 }
695 }
696 }
697}
698
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000699#ifdef SK_DEBUG
mtklein3e8232b2014-08-18 13:39:11 -0700700// Ensure that deleting an empty SkPicture does not assert. Asserts only fire
robertphillipsdb539902014-07-01 08:47:04 -0700701// in debug mode, so only run in debug mode.
702static void test_deleting_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000703 SkPictureRecorder recorder;
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000704 // Creates an SkPictureRecord
robertphillips9f1c2412014-06-09 06:25:34 -0700705 recorder.beginRecording(0, 0);
robertphillipsdb539902014-07-01 08:47:04 -0700706 // Turns that into an SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000707 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
robertphillipsdb539902014-07-01 08:47:04 -0700708 // Ceates a new SkPictureRecord
robertphillips9f1c2412014-06-09 06:25:34 -0700709 recorder.beginRecording(0, 0);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000710}
711
712// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
713static void test_serializing_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000714 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -0700715 recorder.beginRecording(0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000716 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000717 SkDynamicMemoryWStream stream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000718 picture->serialize(&stream);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000719}
720#endif
721
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000722static void rand_op(SkCanvas* canvas, SkRandom& rand) {
reed@google.com21b519d2012-10-02 17:42:15 +0000723 SkPaint paint;
724 SkRect rect = SkRect::MakeWH(50, 50);
725
726 SkScalar unit = rand.nextUScalar1();
727 if (unit <= 0.3) {
728// SkDebugf("save\n");
729 canvas->save();
730 } else if (unit <= 0.6) {
731// SkDebugf("restore\n");
732 canvas->restore();
733 } else if (unit <= 0.9) {
734// SkDebugf("clip\n");
735 canvas->clipRect(rect);
736 } else {
737// SkDebugf("draw\n");
738 canvas->drawPaint(paint);
739 }
740}
741
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000742#if SK_SUPPORT_GPU
tomhudson3a0f2792014-08-20 05:29:41 -0700743
mtklein8e126562014-10-01 09:29:35 -0700744static void test_gpu_veto(skiatest::Reporter* reporter) {
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000745 SkPictureRecorder recorder;
746
mtklein8e126562014-10-01 09:29:35 -0700747 SkCanvas* canvas = recorder.beginRecording(100, 100);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000748 {
749 SkPath path;
750 path.moveTo(0, 0);
751 path.lineTo(50, 50);
752
753 SkScalar intervals[] = { 1.0f, 1.0f };
754 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0));
755
756 SkPaint paint;
757 paint.setStyle(SkPaint::kStroke_Style);
758 paint.setPathEffect(dash);
759
760 canvas->drawPath(path, paint);
761 }
762 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
763 // path effects currently render an SkPicture undesireable for GPU rendering
commit-bot@chromium.orga1ff26a2014-05-30 21:52:52 +0000764
765 const char *reason = NULL;
766 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL, &reason));
bsalomon49f085d2014-09-05 13:34:00 -0700767 REPORTER_ASSERT(reporter, reason);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000768
mtklein8e126562014-10-01 09:29:35 -0700769 canvas = recorder.beginRecording(100, 100);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000770 {
771 SkPath path;
772
773 path.moveTo(0, 0);
774 path.lineTo(0, 50);
775 path.lineTo(25, 25);
776 path.lineTo(50, 50);
777 path.lineTo(50, 0);
778 path.close();
779 REPORTER_ASSERT(reporter, !path.isConvex());
780
781 SkPaint paint;
782 paint.setAntiAlias(true);
783 for (int i = 0; i < 50; ++i) {
784 canvas->drawPath(path, paint);
785 }
786 }
787 picture.reset(recorder.endRecording());
788 // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering
789 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
790
mtklein8e126562014-10-01 09:29:35 -0700791 canvas = recorder.beginRecording(100, 100);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000792 {
793 SkPath path;
794
795 path.moveTo(0, 0);
796 path.lineTo(0, 50);
797 path.lineTo(25, 25);
798 path.lineTo(50, 50);
799 path.lineTo(50, 0);
800 path.close();
801 REPORTER_ASSERT(reporter, !path.isConvex());
802
803 SkPaint paint;
804 paint.setAntiAlias(true);
805 paint.setStyle(SkPaint::kStroke_Style);
806 paint.setStrokeWidth(0);
807 for (int i = 0; i < 50; ++i) {
808 canvas->drawPath(path, paint);
809 }
810 }
811 picture.reset(recorder.endRecording());
812 // hairline stroked AA concave paths are fine for GPU rendering
813 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL));
tomhudson3a0f2792014-08-20 05:29:41 -0700814
mtklein8e126562014-10-01 09:29:35 -0700815 canvas = recorder.beginRecording(100, 100);
tomhudson3a0f2792014-08-20 05:29:41 -0700816 {
817 SkPaint paint;
818 SkScalar intervals [] = { 10, 20 };
819 SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25);
820 paint.setPathEffect(pe)->unref();
821
822 SkPoint points [2] = { { 0, 0 }, { 100, 0 } };
823 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, points, paint);
824 }
825 picture.reset(recorder.endRecording());
826 // fast-path dashed effects are fine for GPU rendering ...
827 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL));
828
mtklein8e126562014-10-01 09:29:35 -0700829 canvas = recorder.beginRecording(100, 100);
tomhudson3a0f2792014-08-20 05:29:41 -0700830 {
831 SkPaint paint;
832 SkScalar intervals [] = { 10, 20 };
833 SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25);
834 paint.setPathEffect(pe)->unref();
835
836 canvas->drawRect(SkRect::MakeWH(10, 10), paint);
837 }
838 picture.reset(recorder.endRecording());
839 // ... but only when applied to drawPoint() calls
840 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
mtklein53fecfb2014-08-21 09:11:37 -0700841
842 // Nest the previous picture inside a new one.
mtklein8e126562014-10-01 09:29:35 -0700843 canvas = recorder.beginRecording(100, 100);
844 {
845 canvas->drawPicture(picture.get());
mtklein53fecfb2014-08-21 09:11:37 -0700846 }
mtklein8e126562014-10-01 09:29:35 -0700847 picture.reset(recorder.endRecording());
848 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000849}
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000850
851static void test_gpu_picture_optimization(skiatest::Reporter* reporter,
852 GrContextFactory* factory) {
bsalomone904c092014-07-17 10:50:59 -0700853 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
854 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000855
bsalomone904c092014-07-17 10:50:59 -0700856 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
857 continue;
858 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000859
bsalomone904c092014-07-17 10:50:59 -0700860 GrContext* context = factory->get(glCtxType);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000861
bsalomone904c092014-07-17 10:50:59 -0700862 if (NULL == context) {
863 continue;
864 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000865
bsalomone904c092014-07-17 10:50:59 -0700866 static const int kWidth = 100;
867 static const int kHeight = 100;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000868
robertphillipsd6283302014-08-27 11:53:28 -0700869 SkAutoTUnref<SkPicture> pict, child;
870
871 {
872 SkPictureRecorder recorder;
873
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700874 SkCanvas* c = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
robertphillipsd6283302014-08-27 11:53:28 -0700875
876 c->saveLayer(NULL, NULL);
877 c->restore();
878
879 child.reset(recorder.endRecording());
880 }
bsalomone904c092014-07-17 10:50:59 -0700881
882 // create a picture with the structure:
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000883 // 1)
bsalomone904c092014-07-17 10:50:59 -0700884 // SaveLayer
885 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000886 // 2)
bsalomone904c092014-07-17 10:50:59 -0700887 // SaveLayer
888 // Translate
889 // SaveLayer w/ bound
890 // Restore
891 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000892 // 3)
bsalomone904c092014-07-17 10:50:59 -0700893 // SaveLayer w/ copyable paint
894 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000895 // 4)
robertphillipsd6283302014-08-27 11:53:28 -0700896 // SaveLayer
897 // DrawPicture (which has a SaveLayer/Restore pair)
898 // Restore
899 // 5)
900 // SaveLayer
901 // DrawPicture with Matrix & Paint (with SaveLayer/Restore pair)
bsalomone904c092014-07-17 10:50:59 -0700902 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000903 {
bsalomone904c092014-07-17 10:50:59 -0700904 SkPictureRecorder recorder;
905
mtklein87c41382014-09-08 07:31:18 -0700906 SkCanvas* c = recorder.beginRecording(SkIntToScalar(kWidth),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700907 SkIntToScalar(kHeight));
bsalomone904c092014-07-17 10:50:59 -0700908 // 1)
robertphillipsd6283302014-08-27 11:53:28 -0700909 c->saveLayer(NULL, NULL); // layer #0
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000910 c->restore();
bsalomone904c092014-07-17 10:50:59 -0700911
912 // 2)
robertphillipsd6283302014-08-27 11:53:28 -0700913 c->saveLayer(NULL, NULL); // layer #1
914 c->translate(kWidth/2.0f, kHeight/2.0f);
bsalomone904c092014-07-17 10:50:59 -0700915 SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2);
robertphillipsd6283302014-08-27 11:53:28 -0700916 c->saveLayer(&r, NULL); // layer #2
bsalomone904c092014-07-17 10:50:59 -0700917 c->restore();
918 c->restore();
919
920 // 3)
921 {
922 SkPaint p;
923 p.setColor(SK_ColorRED);
robertphillipsd6283302014-08-27 11:53:28 -0700924 c->saveLayer(NULL, &p); // layer #3
bsalomone904c092014-07-17 10:50:59 -0700925 c->restore();
926 }
mtklein93f52a62014-09-08 08:41:37 -0700927
928 SkPaint layerPaint;
929 layerPaint.setColor(SK_ColorRED); // Non-alpha only to avoid SaveLayerDrawRestoreNooper
bsalomone904c092014-07-17 10:50:59 -0700930 // 4)
robertphillipsd6283302014-08-27 11:53:28 -0700931 {
mtklein93f52a62014-09-08 08:41:37 -0700932 c->saveLayer(NULL, &layerPaint); // layer #4
robertphillipsd6283302014-08-27 11:53:28 -0700933 c->drawPicture(child); // layer #5 inside picture
934 c->restore();
935 }
936 // 5
bsalomone904c092014-07-17 10:50:59 -0700937 {
mtklein93f52a62014-09-08 08:41:37 -0700938 SkPaint picturePaint;
robertphillipsd6283302014-08-27 11:53:28 -0700939 SkMatrix trans;
940 trans.setTranslate(10, 10);
941
mtklein93f52a62014-09-08 08:41:37 -0700942 c->saveLayer(NULL, &layerPaint); // layer #6
943 c->drawPicture(child, &trans, &picturePaint); // layer #7 inside picture
bsalomone904c092014-07-17 10:50:59 -0700944 c->restore();
945 }
946
947 pict.reset(recorder.endRecording());
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000948 }
949
bsalomone904c092014-07-17 10:50:59 -0700950 // Now test out the SaveLayer extraction
951 {
952 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000953
bsalomone904c092014-07-17 10:50:59 -0700954 SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info));
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000955
bsalomone904c092014-07-17 10:50:59 -0700956 SkCanvas* canvas = surface->getCanvas();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000957
bsalomone904c092014-07-17 10:50:59 -0700958 canvas->EXPERIMENTAL_optimize(pict);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000959
robertphillips6617d502014-08-18 09:39:34 -0700960 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000961
bsalomone904c092014-07-17 10:50:59 -0700962 const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key);
bsalomon49f085d2014-09-05 13:34:00 -0700963 REPORTER_ASSERT(reporter, data);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000964
robertphillips6617d502014-08-18 09:39:34 -0700965 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data);
robertphillipsd6283302014-08-27 11:53:28 -0700966 REPORTER_ASSERT(reporter, 8 == gpuData->numSaveLayers());
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000967
robertphillips6617d502014-08-18 09:39:34 -0700968 const GrAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0);
robertphillipsd6283302014-08-27 11:53:28 -0700969 // The parent/child layers appear in reverse order
robertphillips6617d502014-08-18 09:39:34 -0700970 const GrAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2);
971 const GrAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1);
robertphillipsd6283302014-08-27 11:53:28 -0700972
robertphillips6617d502014-08-18 09:39:34 -0700973 const GrAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3);
robertphillipsd6283302014-08-27 11:53:28 -0700974
975 // The parent/child layers appear in reverse order
976 const GrAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(5);
977 const GrAccelData::SaveLayerInfo& info5 = gpuData->saveLayerInfo(4);
978
979 // The parent/child layers appear in reverse order
980 const GrAccelData::SaveLayerInfo& info6 = gpuData->saveLayerInfo(7);
981 const GrAccelData::SaveLayerInfo& info7 = gpuData->saveLayerInfo(6);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000982
robertphillips30d2cc62014-09-24 08:52:18 -0700983 REPORTER_ASSERT(reporter, NULL == info0.fPicture);
robertphillips3aac6e02014-10-20 08:52:40 -0700984 REPORTER_ASSERT(reporter, kWidth == info0.fBounds.width() &&
985 kHeight == info0.fBounds.height());
robertphillips9e6835d2014-10-22 05:33:52 -0700986 REPORTER_ASSERT(reporter, info0.fLocalMat.isIdentity());
987 REPORTER_ASSERT(reporter, info0.fPreMat.isIdentity());
robertphillips3aac6e02014-10-20 08:52:40 -0700988 REPORTER_ASSERT(reporter, 0 == info0.fBounds.fLeft && 0 == info0.fBounds.fTop);
robertphillipsd6283302014-08-27 11:53:28 -0700989 REPORTER_ASSERT(reporter, NULL == info0.fPaint);
bsalomone904c092014-07-17 10:50:59 -0700990 REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000991
robertphillips30d2cc62014-09-24 08:52:18 -0700992 REPORTER_ASSERT(reporter, NULL == info1.fPicture);
robertphillips3aac6e02014-10-20 08:52:40 -0700993 REPORTER_ASSERT(reporter, kWidth == info1.fBounds.width() &&
994 kHeight == info1.fBounds.height());
robertphillips9e6835d2014-10-22 05:33:52 -0700995 REPORTER_ASSERT(reporter, info1.fLocalMat.isIdentity());
996 REPORTER_ASSERT(reporter, info1.fPreMat.isIdentity());
robertphillips3aac6e02014-10-20 08:52:40 -0700997 REPORTER_ASSERT(reporter, 0 == info1.fBounds.fLeft && 0 == info1.fBounds.fTop);
robertphillipsd6283302014-08-27 11:53:28 -0700998 REPORTER_ASSERT(reporter, NULL == info1.fPaint);
mtklein87c41382014-09-08 07:31:18 -0700999 REPORTER_ASSERT(reporter, !info1.fIsNested &&
robertphillipsd6283302014-08-27 11:53:28 -07001000 info1.fHasNestedLayers); // has a nested SL
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001001
robertphillips30d2cc62014-09-24 08:52:18 -07001002 REPORTER_ASSERT(reporter, NULL == info2.fPicture);
robertphillips3aac6e02014-10-20 08:52:40 -07001003 REPORTER_ASSERT(reporter, kWidth / 2 == info2.fBounds.width() &&
1004 kHeight / 2 == info2.fBounds.height()); // bound reduces size
robertphillips9e6835d2014-10-22 05:33:52 -07001005 REPORTER_ASSERT(reporter, !info2.fLocalMat.isIdentity());
1006 REPORTER_ASSERT(reporter, info2.fPreMat.isIdentity());
robertphillips3aac6e02014-10-20 08:52:40 -07001007 REPORTER_ASSERT(reporter, kWidth / 2 == info2.fBounds.fLeft && // translated
1008 kHeight / 2 == info2.fBounds.fTop);
robertphillipsd6283302014-08-27 11:53:28 -07001009 REPORTER_ASSERT(reporter, NULL == info1.fPaint);
bsalomone904c092014-07-17 10:50:59 -07001010 REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001011
robertphillips30d2cc62014-09-24 08:52:18 -07001012 REPORTER_ASSERT(reporter, NULL == info3.fPicture);
robertphillips3aac6e02014-10-20 08:52:40 -07001013 REPORTER_ASSERT(reporter, kWidth == info3.fBounds.width() &&
1014 kHeight == info3.fBounds.height());
robertphillips9e6835d2014-10-22 05:33:52 -07001015 REPORTER_ASSERT(reporter, info3.fLocalMat.isIdentity());
1016 REPORTER_ASSERT(reporter, info3.fPreMat.isIdentity());
robertphillips3aac6e02014-10-20 08:52:40 -07001017 REPORTER_ASSERT(reporter, 0 == info3.fBounds.fLeft && 0 == info3.fBounds.fTop);
bsalomon49f085d2014-09-05 13:34:00 -07001018 REPORTER_ASSERT(reporter, info3.fPaint);
bsalomone904c092014-07-17 10:50:59 -07001019 REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001020
robertphillips30d2cc62014-09-24 08:52:18 -07001021 REPORTER_ASSERT(reporter, NULL == info4.fPicture);
robertphillips3aac6e02014-10-20 08:52:40 -07001022 REPORTER_ASSERT(reporter, kWidth == info4.fBounds.width() &&
1023 kHeight == info4.fBounds.height());
1024 REPORTER_ASSERT(reporter, 0 == info4.fBounds.fLeft && 0 == info4.fBounds.fTop);
robertphillips9e6835d2014-10-22 05:33:52 -07001025 REPORTER_ASSERT(reporter, info4.fLocalMat.isIdentity());
1026 REPORTER_ASSERT(reporter, info4.fPreMat.isIdentity());
mtklein93f52a62014-09-08 08:41:37 -07001027 REPORTER_ASSERT(reporter, info4.fPaint);
mtklein87c41382014-09-08 07:31:18 -07001028 REPORTER_ASSERT(reporter, !info4.fIsNested &&
robertphillipsd6283302014-08-27 11:53:28 -07001029 info4.fHasNestedLayers); // has a nested SL
1030
robertphillips30d2cc62014-09-24 08:52:18 -07001031 REPORTER_ASSERT(reporter, child == info5.fPicture); // in a child picture
robertphillips3aac6e02014-10-20 08:52:40 -07001032 REPORTER_ASSERT(reporter, kWidth == info5.fBounds.width() &&
1033 kHeight == info5.fBounds.height());
1034 REPORTER_ASSERT(reporter, 0 == info5.fBounds.fLeft && 0 == info5.fBounds.fTop);
robertphillips9e6835d2014-10-22 05:33:52 -07001035 REPORTER_ASSERT(reporter, info5.fLocalMat.isIdentity());
1036 REPORTER_ASSERT(reporter, info5.fPreMat.isIdentity());
robertphillipsd6283302014-08-27 11:53:28 -07001037 REPORTER_ASSERT(reporter, NULL == info5.fPaint);
1038 REPORTER_ASSERT(reporter, info5.fIsNested && !info5.fHasNestedLayers); // is nested
1039
robertphillips30d2cc62014-09-24 08:52:18 -07001040 REPORTER_ASSERT(reporter, NULL == info6.fPicture);
robertphillips3aac6e02014-10-20 08:52:40 -07001041 REPORTER_ASSERT(reporter, kWidth == info6.fBounds.width() &&
1042 kHeight == info6.fBounds.height());
1043 REPORTER_ASSERT(reporter, 0 == info6.fBounds.fLeft && 0 == info6.fBounds.fTop);
robertphillips9e6835d2014-10-22 05:33:52 -07001044 REPORTER_ASSERT(reporter, info6.fLocalMat.isIdentity());
1045 REPORTER_ASSERT(reporter, info6.fPreMat.isIdentity());
mtklein93f52a62014-09-08 08:41:37 -07001046 REPORTER_ASSERT(reporter, info6.fPaint);
mtklein87c41382014-09-08 07:31:18 -07001047 REPORTER_ASSERT(reporter, !info6.fIsNested &&
robertphillipsd6283302014-08-27 11:53:28 -07001048 info6.fHasNestedLayers); // has a nested SL
1049
robertphillips30d2cc62014-09-24 08:52:18 -07001050 REPORTER_ASSERT(reporter, child == info7.fPicture); // in a child picture
robertphillips3aac6e02014-10-20 08:52:40 -07001051 REPORTER_ASSERT(reporter, kWidth == info7.fBounds.width() &&
1052 kHeight == info7.fBounds.height());
1053 REPORTER_ASSERT(reporter, 0 == info7.fBounds.fLeft && 0 == info7.fBounds.fTop);
robertphillips9e6835d2014-10-22 05:33:52 -07001054 REPORTER_ASSERT(reporter, info7.fLocalMat.isIdentity());
1055 REPORTER_ASSERT(reporter, info7.fPreMat.isIdentity());
robertphillipsd6283302014-08-27 11:53:28 -07001056 REPORTER_ASSERT(reporter, NULL == info7.fPaint);
1057 REPORTER_ASSERT(reporter, info7.fIsNested && !info7.fHasNestedLayers); // is nested
bsalomone904c092014-07-17 10:50:59 -07001058 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001059 }
1060}
1061
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001062#endif
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +00001063
mtklein8e126562014-10-01 09:29:35 -07001064static void test_has_text(skiatest::Reporter* reporter) {
ajuma750ae262014-08-18 12:59:55 -07001065 SkPictureRecorder recorder;
ajuma750ae262014-08-18 12:59:55 -07001066
mtklein8e126562014-10-01 09:29:35 -07001067 SkCanvas* canvas = recorder.beginRecording(100,100);
ajuma750ae262014-08-18 12:59:55 -07001068 {
mtkleinc551d9f2014-08-20 08:09:46 -07001069 canvas->drawRect(SkRect::MakeWH(20, 20), SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001070 }
1071 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1072 REPORTER_ASSERT(reporter, !picture->hasText());
1073
mtkleinc551d9f2014-08-20 08:09:46 -07001074 SkPoint point = SkPoint::Make(10, 10);
mtklein8e126562014-10-01 09:29:35 -07001075 canvas = recorder.beginRecording(100,100);
ajuma750ae262014-08-18 12:59:55 -07001076 {
mtkleinc551d9f2014-08-20 08:09:46 -07001077 canvas->drawText("Q", 1, point.fX, point.fY, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001078 }
1079 picture.reset(recorder.endRecording());
1080 REPORTER_ASSERT(reporter, picture->hasText());
1081
mtklein8e126562014-10-01 09:29:35 -07001082 canvas = recorder.beginRecording(100,100);
ajuma750ae262014-08-18 12:59:55 -07001083 {
mtkleinc551d9f2014-08-20 08:09:46 -07001084 canvas->drawPosText("Q", 1, &point, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001085 }
1086 picture.reset(recorder.endRecording());
1087 REPORTER_ASSERT(reporter, picture->hasText());
1088
mtklein8e126562014-10-01 09:29:35 -07001089 canvas = recorder.beginRecording(100,100);
ajuma750ae262014-08-18 12:59:55 -07001090 {
mtkleinc551d9f2014-08-20 08:09:46 -07001091 canvas->drawPosTextH("Q", 1, &point.fX, point.fY, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001092 }
1093 picture.reset(recorder.endRecording());
1094 REPORTER_ASSERT(reporter, picture->hasText());
1095
mtklein8e126562014-10-01 09:29:35 -07001096 canvas = recorder.beginRecording(100,100);
ajuma750ae262014-08-18 12:59:55 -07001097 {
1098 SkPath path;
1099 path.moveTo(0, 0);
1100 path.lineTo(50, 50);
1101
mtkleinc551d9f2014-08-20 08:09:46 -07001102 canvas->drawTextOnPathHV("Q", 1, path, point.fX, point.fY, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001103 }
1104 picture.reset(recorder.endRecording());
1105 REPORTER_ASSERT(reporter, picture->hasText());
1106
mtklein8e126562014-10-01 09:29:35 -07001107 canvas = recorder.beginRecording(100,100);
ajuma750ae262014-08-18 12:59:55 -07001108 {
1109 SkPath path;
1110 path.moveTo(0, 0);
1111 path.lineTo(50, 50);
1112
mtkleinc551d9f2014-08-20 08:09:46 -07001113 canvas->drawTextOnPath("Q", 1, path, NULL, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001114 }
1115 picture.reset(recorder.endRecording());
1116 REPORTER_ASSERT(reporter, picture->hasText());
mtklein53fecfb2014-08-21 09:11:37 -07001117
1118 // Nest the previous picture inside a new one.
mtklein8e126562014-10-01 09:29:35 -07001119 canvas = recorder.beginRecording(100,100);
1120 {
1121 canvas->drawPicture(picture.get());
mtklein53fecfb2014-08-21 09:11:37 -07001122 }
mtklein8e126562014-10-01 09:29:35 -07001123 picture.reset(recorder.endRecording());
1124 REPORTER_ASSERT(reporter, picture->hasText());
ajuma750ae262014-08-18 12:59:55 -07001125}
1126
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001127static void set_canvas_to_save_count_4(SkCanvas* canvas) {
1128 canvas->restoreToCount(1);
1129 canvas->save();
1130 canvas->save();
1131 canvas->save();
1132}
1133
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001134/**
1135 * A canvas that records the number of saves, saveLayers and restores.
1136 */
1137class SaveCountingCanvas : public SkCanvas {
1138public:
1139 SaveCountingCanvas(int width, int height)
1140 : INHERITED(width, height)
1141 , fSaveCount(0)
1142 , fSaveLayerCount(0)
1143 , fRestoreCount(0){
1144 }
1145
1146 virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint,
1147 SaveFlags flags) SK_OVERRIDE {
1148 ++fSaveLayerCount;
1149 return this->INHERITED::willSaveLayer(bounds, paint, flags);
1150 }
1151
Florin Malita5f6102d2014-06-30 10:13:28 -04001152 virtual void willSave() SK_OVERRIDE {
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001153 ++fSaveCount;
Florin Malita5f6102d2014-06-30 10:13:28 -04001154 this->INHERITED::willSave();
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001155 }
1156
1157 virtual void willRestore() SK_OVERRIDE {
1158 ++fRestoreCount;
1159 this->INHERITED::willRestore();
1160 }
1161
1162 unsigned int getSaveCount() const { return fSaveCount; }
1163 unsigned int getSaveLayerCount() const { return fSaveLayerCount; }
1164 unsigned int getRestoreCount() const { return fRestoreCount; }
1165
1166private:
1167 unsigned int fSaveCount;
1168 unsigned int fSaveLayerCount;
1169 unsigned int fRestoreCount;
1170
1171 typedef SkCanvas INHERITED;
1172};
1173
skia.committer@gmail.com8e7d37d2014-05-28 03:06:06 +00001174void check_save_state(skiatest::Reporter* reporter, SkPicture* picture,
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001175 unsigned int numSaves, unsigned int numSaveLayers,
1176 unsigned int numRestores) {
mtklein87c41382014-09-08 07:31:18 -07001177 SaveCountingCanvas canvas(SkScalarCeilToInt(picture->cullRect().width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -07001178 SkScalarCeilToInt(picture->cullRect().height()));
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001179
robertphillipsc5ba71d2014-09-04 08:42:50 -07001180 picture->playback(&canvas);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001181
mtklein87c41382014-09-08 07:31:18 -07001182 // Optimizations may have removed these,
1183 // so expect to have seen no more than num{Saves,SaveLayers,Restores}.
1184 REPORTER_ASSERT(reporter, numSaves >= canvas.getSaveCount());
1185 REPORTER_ASSERT(reporter, numSaveLayers >= canvas.getSaveLayerCount());
1186 REPORTER_ASSERT(reporter, numRestores >= canvas.getRestoreCount());
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001187}
1188
1189// This class exists so SkPicture can friend it and give it access to
1190// the 'partialReplay' method.
1191class SkPictureRecorderReplayTester {
1192public:
1193 static SkPicture* Copy(SkPictureRecorder* recorder) {
1194 SkPictureRecorder recorder2;
1195
robertphillips9f1c2412014-06-09 06:25:34 -07001196 SkCanvas* canvas = recorder2.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001197
1198 recorder->partialReplay(canvas);
1199
1200 return recorder2.endRecording();
1201 }
1202};
1203
robertphillips9058d602014-06-10 11:45:46 -07001204static void create_imbalance(SkCanvas* canvas) {
1205 SkRect clipRect = SkRect::MakeWH(2, 2);
1206 SkRect drawRect = SkRect::MakeWH(10, 10);
1207 canvas->save();
1208 canvas->clipRect(clipRect, SkRegion::kReplace_Op);
1209 canvas->translate(1.0f, 1.0f);
1210 SkPaint p;
1211 p.setColor(SK_ColorGREEN);
1212 canvas->drawRect(drawRect, p);
1213 // no restore
1214}
1215
1216// This tests that replaying a potentially unbalanced picture into a canvas
1217// doesn't affect the canvas' save count or matrix/clip state.
1218static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) {
1219 SkBitmap bm;
1220 bm.allocN32Pixels(4, 3);
1221 SkCanvas canvas(bm);
1222
1223 int beforeSaveCount = canvas.getSaveCount();
1224
1225 SkMatrix beforeMatrix = canvas.getTotalMatrix();
1226
1227 SkRect beforeClip;
1228
1229 canvas.getClipBounds(&beforeClip);
1230
1231 canvas.drawPicture(picture);
1232
1233 REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount());
1234 REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix());
1235
1236 SkRect afterClip;
1237
1238 canvas.getClipBounds(&afterClip);
1239
1240 REPORTER_ASSERT(reporter, afterClip == beforeClip);
1241}
1242
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001243// Test out SkPictureRecorder::partialReplay
1244DEF_TEST(PictureRecorder_replay, reporter) {
1245 // check save/saveLayer state
1246 {
1247 SkPictureRecorder recorder;
1248
robertphillips9f1c2412014-06-09 06:25:34 -07001249 SkCanvas* canvas = recorder.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001250
1251 canvas->saveLayer(NULL, NULL);
1252
1253 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1254
1255 // The extra save and restore comes from the Copy process.
1256 check_save_state(reporter, copy, 2, 1, 3);
1257
1258 canvas->saveLayer(NULL, NULL);
1259
1260 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1261
1262 check_save_state(reporter, final, 1, 2, 3);
1263
1264 // The copy shouldn't pick up any operations added after it was made
1265 check_save_state(reporter, copy, 2, 1, 3);
1266 }
1267
1268 // (partially) check leakage of draw ops
1269 {
1270 SkPictureRecorder recorder;
1271
robertphillips9f1c2412014-06-09 06:25:34 -07001272 SkCanvas* canvas = recorder.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001273
1274 SkRect r = SkRect::MakeWH(5, 5);
1275 SkPaint p;
1276
1277 canvas->drawRect(r, p);
1278
1279 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1280
1281 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1282
1283 SkBitmap bm;
1284 make_bm(&bm, 10, 10, SK_ColorRED, true);
1285
1286 r.offset(5.0f, 5.0f);
1287 canvas->drawBitmapRectToRect(bm, NULL, r);
1288
1289 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1290 REPORTER_ASSERT(reporter, final->willPlayBackBitmaps());
1291
1292 REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID());
1293
1294 // The snapshot shouldn't pick up any operations added after it was made
1295 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1296 }
robertphillips9058d602014-06-10 11:45:46 -07001297
1298 // Recreate the Android partialReplay test case
1299 {
1300 SkPictureRecorder recorder;
1301
1302 SkCanvas* canvas = recorder.beginRecording(4, 3, NULL, 0);
1303 create_imbalance(canvas);
1304
1305 int expectedSaveCount = canvas->getSaveCount();
1306
1307 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1308 check_balance(reporter, copy);
1309
1310 REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount());
1311
1312 // End the recording of source to test the picture finalization
1313 // process isn't complicated by the partialReplay step
1314 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1315 }
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001316}
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001317
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001318static void test_unbalanced_save_restores(skiatest::Reporter* reporter) {
1319 SkCanvas testCanvas(100, 100);
1320 set_canvas_to_save_count_4(&testCanvas);
1321
1322 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1323
1324 SkPaint paint;
1325 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000);
1326
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001327 SkPictureRecorder recorder;
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001328
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001329 {
1330 // Create picture with 2 unbalanced saves
robertphillips9f1c2412014-06-09 06:25:34 -07001331 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001332 canvas->save();
1333 canvas->translate(10, 10);
1334 canvas->drawRect(rect, paint);
1335 canvas->save();
1336 canvas->translate(10, 10);
1337 canvas->drawRect(rect, paint);
1338 SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording());
1339
robertphillips9b14f262014-06-04 05:40:44 -07001340 testCanvas.drawPicture(extraSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001341 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1342 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001343
1344 set_canvas_to_save_count_4(&testCanvas);
1345
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001346 {
1347 // Create picture with 2 unbalanced restores
robertphillips9f1c2412014-06-09 06:25:34 -07001348 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001349 canvas->save();
1350 canvas->translate(10, 10);
1351 canvas->drawRect(rect, paint);
1352 canvas->save();
1353 canvas->translate(10, 10);
1354 canvas->drawRect(rect, paint);
1355 canvas->restore();
1356 canvas->restore();
1357 canvas->restore();
1358 canvas->restore();
1359 SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording());
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001360
robertphillips9b14f262014-06-04 05:40:44 -07001361 testCanvas.drawPicture(extraRestorePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001362 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1363 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001364
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001365 set_canvas_to_save_count_4(&testCanvas);
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001366
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001367 {
robertphillips9f1c2412014-06-09 06:25:34 -07001368 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001369 canvas->translate(10, 10);
1370 canvas->drawRect(rect, paint);
1371 SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording());
1372
robertphillips9b14f262014-06-04 05:40:44 -07001373 testCanvas.drawPicture(noSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001374 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1375 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity());
1376 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001377}
1378
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001379static void test_peephole() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001380 SkRandom rand;
reed@google.com21b519d2012-10-02 17:42:15 +00001381
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001382 SkPictureRecorder recorder;
1383
reed@google.com21b519d2012-10-02 17:42:15 +00001384 for (int j = 0; j < 100; j++) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001385 SkRandom rand2(rand); // remember the seed
reed@google.com21b519d2012-10-02 17:42:15 +00001386
robertphillips9f1c2412014-06-09 06:25:34 -07001387 SkCanvas* canvas = recorder.beginRecording(100, 100);
reed@google.com21b519d2012-10-02 17:42:15 +00001388
1389 for (int i = 0; i < 1000; ++i) {
1390 rand_op(canvas, rand);
1391 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001392 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
jvanverth@google.comc490f802013-03-04 13:56:38 +00001393
1394 rand = rand2;
reed@google.com21b519d2012-10-02 17:42:15 +00001395 }
1396
1397 {
robertphillips9f1c2412014-06-09 06:25:34 -07001398 SkCanvas* canvas = recorder.beginRecording(100, 100);
reed@google.com21b519d2012-10-02 17:42:15 +00001399 SkRect rect = SkRect::MakeWH(50, 50);
skia.committer@gmail.com52c24372012-10-03 02:01:13 +00001400
reed@google.com21b519d2012-10-02 17:42:15 +00001401 for (int i = 0; i < 100; ++i) {
1402 canvas->save();
1403 }
1404 while (canvas->getSaveCount() > 1) {
1405 canvas->clipRect(rect);
1406 canvas->restore();
1407 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001408 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
reed@google.com21b519d2012-10-02 17:42:15 +00001409 }
1410}
1411
scroggo@google.com4b90b112012-12-04 15:08:56 +00001412#ifndef SK_DEBUG
1413// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
1414// should never do this.
1415static void test_bad_bitmap() {
1416 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
1417 // fail.
1418 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001419 bm.setInfo(SkImageInfo::MakeN32Premul(100, 100));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001420 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001421 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001422 recordingCanvas->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001423 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.com4b90b112012-12-04 15:08:56 +00001424
1425 SkCanvas canvas;
robertphillips9b14f262014-06-04 05:40:44 -07001426 canvas.drawPicture(picture);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001427}
1428#endif
1429
reed@google.com672588b2014-01-08 15:42:01 +00001430static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001431 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001432}
1433
1434static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001435 SkPictureRecorder recorder;
mtklein87c41382014-09-08 07:31:18 -07001436 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bitmap.width()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -07001437 SkIntToScalar(bitmap.height()));
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001438 canvas->drawBitmap(bitmap, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001439 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1440
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001441 SkDynamicMemoryWStream wStream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001442 picture->serialize(&wStream, &encode_bitmap_to_data);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001443 return wStream.copyToData();
1444}
1445
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001446struct ErrorContext {
1447 int fErrors;
1448 skiatest::Reporter* fReporter;
1449};
1450
1451static void assert_one_parse_error_cb(SkError error, void* context) {
1452 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
1453 errorContext->fErrors++;
1454 // This test only expects one error, and that is a kParseError. If there are others,
1455 // there is some unknown problem.
1456 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors,
1457 "This threw more errors than expected.");
1458 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error,
1459 SkGetLastErrorString());
1460}
1461
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001462static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
1463 // Create a bitmap that will be encoded.
1464 SkBitmap original;
1465 make_bm(&original, 100, 100, SK_ColorBLUE, true);
1466 SkDynamicMemoryWStream wStream;
1467 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
1468 return;
1469 }
1470 SkAutoDataUnref data(wStream.copyToData());
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001471
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001472 SkBitmap bm;
halcanary@google.com3d50ea12014-01-02 13:15:13 +00001473 bool installSuccess = SkInstallDiscardablePixelRef(
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +00001474 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm);
reed@google.combf790232013-12-13 19:45:58 +00001475 REPORTER_ASSERT(reporter, installSuccess);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001476
1477 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
1478 // Flattening original will follow the old path of performing an encode, while flattening bm
1479 // will use the already encoded data.
1480 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
1481 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
1482 REPORTER_ASSERT(reporter, picture1->equals(picture2));
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001483 // Now test that a parse error was generated when trying to create a new SkPicture without
1484 // providing a function to decode the bitmap.
1485 ErrorContext context;
1486 context.fErrors = 0;
1487 context.fReporter = reporter;
1488 SkSetErrorCallback(assert_one_parse_error_cb, &context);
1489 SkMemoryStream pictureStream(picture1);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001490 SkClearLastError();
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001491 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL));
1492 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001493 SkClearLastError();
1494 SkSetErrorCallback(NULL, NULL);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001495}
1496
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001497static void test_draw_empty(skiatest::Reporter* reporter) {
1498 SkBitmap result;
1499 make_bm(&result, 2, 2, SK_ColorBLACK, false);
1500
1501 SkCanvas canvas(result);
1502
1503 {
1504 // stock SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001505 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001506 recorder.beginRecording(1, 1);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001507 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001508
robertphillips9b14f262014-06-04 05:40:44 -07001509 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001510 }
1511
1512 {
1513 // tile grid
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001514 SkTileGridFactory::TileGridInfo gridInfo;
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001515 gridInfo.fMargin.setEmpty();
1516 gridInfo.fOffset.setZero();
1517 gridInfo.fTileInterval.set(1, 1);
1518
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001519 SkTileGridFactory factory(gridInfo);
1520 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001521 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001522 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001523
robertphillips9b14f262014-06-04 05:40:44 -07001524 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001525 }
1526
1527 {
1528 // RTree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001529 SkRTreeFactory factory;
1530 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001531 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001532 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001533
robertphillips9b14f262014-06-04 05:40:44 -07001534 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001535 }
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001536}
1537
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001538static void test_clip_bound_opt(skiatest::Reporter* reporter) {
1539 // Test for crbug.com/229011
1540 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
1541 SkIntToScalar(2), SkIntToScalar(2));
1542 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
1543 SkIntToScalar(1), SkIntToScalar(1));
1544 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
1545 SkIntToScalar(1), SkIntToScalar(1));
1546
1547 SkPath invPath;
1548 invPath.addOval(rect1);
1549 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
1550 SkPath path;
1551 path.addOval(rect2);
1552 SkPath path2;
1553 path2.addOval(rect3);
1554 SkIRect clipBounds;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001555 SkPictureRecorder recorder;
reedd9544982014-09-09 18:46:22 -07001556
1557 // Testing conservative-raster-clip that is enabled by PictureRecord
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001558 {
robertphillips9f1c2412014-06-09 06:25:34 -07001559 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001560 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1561 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1562 REPORTER_ASSERT(reporter, true == nonEmpty);
1563 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1564 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1565 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1566 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1567 }
1568 {
robertphillips9f1c2412014-06-09 06:25:34 -07001569 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001570 canvas->clipPath(path, SkRegion::kIntersect_Op);
1571 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1572 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1573 REPORTER_ASSERT(reporter, true == nonEmpty);
1574 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1575 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1576 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1577 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1578 }
1579 {
robertphillips9f1c2412014-06-09 06:25:34 -07001580 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001581 canvas->clipPath(path, SkRegion::kIntersect_Op);
1582 canvas->clipPath(invPath, SkRegion::kUnion_Op);
1583 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1584 REPORTER_ASSERT(reporter, true == nonEmpty);
1585 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1586 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1587 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1588 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1589 }
1590 {
robertphillips9f1c2412014-06-09 06:25:34 -07001591 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001592 canvas->clipPath(path, SkRegion::kDifference_Op);
1593 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1594 REPORTER_ASSERT(reporter, true == nonEmpty);
1595 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1596 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1597 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1598 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1599 }
1600 {
robertphillips9f1c2412014-06-09 06:25:34 -07001601 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001602 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
1603 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1604 // True clip is actually empty in this case, but the best
1605 // determination we can make using only bounds as input is that the
1606 // clip is included in the bounds of 'path'.
1607 REPORTER_ASSERT(reporter, true == nonEmpty);
1608 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1609 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1610 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1611 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1612 }
1613 {
robertphillips9f1c2412014-06-09 06:25:34 -07001614 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001615 canvas->clipPath(path, SkRegion::kIntersect_Op);
1616 canvas->clipPath(path2, SkRegion::kXOR_Op);
1617 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1618 REPORTER_ASSERT(reporter, true == nonEmpty);
1619 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
1620 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
1621 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1622 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1623 }
1624}
1625
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001626/**
1627 * A canvas that records the number of clip commands.
1628 */
1629class ClipCountingCanvas : public SkCanvas {
1630public:
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001631 ClipCountingCanvas(int width, int height)
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001632 : INHERITED(width, height)
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001633 , fClipCount(0){
1634 }
1635
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001636 virtual void onClipRect(const SkRect& r,
1637 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001638 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001639 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001640 this->INHERITED::onClipRect(r, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001641 }
1642
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001643 virtual void onClipRRect(const SkRRect& rrect,
1644 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001645 ClipEdgeStyle edgeStyle)SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001646 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001647 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001648 }
1649
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001650 virtual void onClipPath(const SkPath& path,
1651 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001652 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001653 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001654 this->INHERITED::onClipPath(path, op, edgeStyle);
1655 }
1656
1657 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE {
1658 fClipCount += 1;
1659 this->INHERITED::onClipRegion(deviceRgn, op);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001660 }
1661
1662 unsigned getClipCount() const { return fClipCount; }
1663
1664private:
1665 unsigned fClipCount;
1666
1667 typedef SkCanvas INHERITED;
1668};
1669
1670static void test_clip_expansion(skiatest::Reporter* reporter) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001671 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001672 SkCanvas* canvas = recorder.beginRecording(10, 10);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001673
1674 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
1675 // The following expanding clip should not be skipped.
1676 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
1677 // Draw something so the optimizer doesn't just fold the world.
1678 SkPaint p;
1679 p.setColor(SK_ColorBLUE);
1680 canvas->drawPaint(p);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001681 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001682
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001683 ClipCountingCanvas testCanvas(10, 10);
robertphillipsc5ba71d2014-09-04 08:42:50 -07001684 picture->playback(&testCanvas);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001685
1686 // Both clips should be present on playback.
1687 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
1688}
1689
tomhudson@google.com381010e2013-10-24 11:12:47 +00001690static void test_hierarchical(skiatest::Reporter* reporter) {
1691 SkBitmap bm;
1692 make_bm(&bm, 10, 10, SK_ColorRED, true);
1693
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001694 SkPictureRecorder recorder;
tomhudson@google.com381010e2013-10-24 11:12:47 +00001695
robertphillips9f1c2412014-06-09 06:25:34 -07001696 recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001697 SkAutoTUnref<SkPicture> childPlain(recorder.endRecording());
1698 REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0
tomhudson@google.com381010e2013-10-24 11:12:47 +00001699
robertphillips9f1c2412014-06-09 06:25:34 -07001700 recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001701 SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording());
1702 REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1
tomhudson@google.com381010e2013-10-24 11:12:47 +00001703
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001704 {
robertphillips9f1c2412014-06-09 06:25:34 -07001705 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips9b14f262014-06-04 05:40:44 -07001706 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001707 SkAutoTUnref<SkPicture> parentPP(recorder.endRecording());
1708 REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0
1709 }
1710 {
robertphillips9f1c2412014-06-09 06:25:34 -07001711 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips9b14f262014-06-04 05:40:44 -07001712 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001713 SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording());
1714 REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1
1715 }
1716 {
robertphillips9f1c2412014-06-09 06:25:34 -07001717 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001718 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001719 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001720 SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording());
1721 REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1
1722 }
1723 {
robertphillips9f1c2412014-06-09 06:25:34 -07001724 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001725 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001726 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001727 SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording());
1728 REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2
1729 }
tomhudson@google.com381010e2013-10-24 11:12:47 +00001730}
1731
robertphillips@google.comd5500882014-04-02 23:51:13 +00001732static void test_gen_id(skiatest::Reporter* reporter) {
1733
Robert Phillipscfaeec42014-07-13 12:00:50 -04001734 SkPictureRecorder recorder;
1735 recorder.beginRecording(0, 0);
1736 SkAutoTUnref<SkPicture> empty(recorder.endRecording());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001737
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001738 // Empty pictures should still have a valid ID
Robert Phillipscfaeec42014-07-13 12:00:50 -04001739 REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001740
robertphillips9f1c2412014-06-09 06:25:34 -07001741 SkCanvas* canvas = recorder.beginRecording(1, 1);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001742 canvas->drawARGB(255, 255, 255, 255);
1743 SkAutoTUnref<SkPicture> hasData(recorder.endRecording());
1744 // picture should have a non-zero id after recording
1745 REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001746
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001747 // both pictures should have different ids
Robert Phillipscfaeec42014-07-13 12:00:50 -04001748 REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001749}
1750
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00001751DEF_TEST(Picture, reporter) {
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001752#ifdef SK_DEBUG
robertphillipsdb539902014-07-01 08:47:04 -07001753 test_deleting_empty_picture();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001754 test_serializing_empty_picture();
scroggo@google.com4b90b112012-12-04 15:08:56 +00001755#else
1756 test_bad_bitmap();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001757#endif
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001758 test_unbalanced_save_restores(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001759 test_peephole();
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001760#if SK_SUPPORT_GPU
mtklein8e126562014-10-01 09:29:35 -07001761 test_gpu_veto(reporter);
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001762#endif
mtklein8e126562014-10-01 09:29:35 -07001763 test_has_text(reporter);
1764 test_analysis(reporter);
reed@google.comfe7b1ed2012-11-29 21:00:39 +00001765 test_gatherpixelrefs(reporter);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +00001766 test_gatherpixelrefsandrects(reporter);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001767 test_bitmap_with_encoded_data(reporter);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001768 test_draw_empty(reporter);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001769 test_clip_bound_opt(reporter);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001770 test_clip_expansion(reporter);
tomhudson@google.com381010e2013-10-24 11:12:47 +00001771 test_hierarchical(reporter);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001772 test_gen_id(reporter);
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001773}
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001774
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001775#if SK_SUPPORT_GPU
1776DEF_GPUTEST(GPUPicture, reporter, factory) {
1777 test_gpu_picture_optimization(reporter, factory);
1778}
1779#endif
1780
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001781static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) {
1782 const SkPaint paint;
1783 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f };
1784 const SkIRect irect = { 2, 2, 3, 3 };
1785
1786 // Don't care what these record, as long as they're legal.
1787 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint);
1788 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag);
1789 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint);
1790 canvas->drawBitmapNine(bitmap, irect, rect, &paint);
1791 canvas->drawSprite(bitmap, 1, 1);
1792}
1793
1794static void test_draw_bitmaps(SkCanvas* canvas) {
1795 SkBitmap empty;
1796 draw_bitmaps(empty, canvas);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001797 empty.setInfo(SkImageInfo::MakeN32Premul(10, 10));
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001798 draw_bitmaps(empty, canvas);
1799}
1800
1801DEF_TEST(Picture_EmptyBitmap, r) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001802 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001803 test_draw_bitmaps(recorder.beginRecording(10, 10));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001804 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001805}
1806
1807DEF_TEST(Canvas_EmptyBitmap, r) {
1808 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001809 dst.allocN32Pixels(10, 10);
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001810 SkCanvas canvas(dst);
1811
1812 test_draw_bitmaps(&canvas);
1813}
dneto3f22e8c2014-07-30 15:42:22 -07001814
1815DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) {
1816 // This test is from crbug.com/344987.
1817 // The commands are:
1818 // saveLayer with paint that modifies alpha
1819 // drawBitmapRectToRect
1820 // drawBitmapRectToRect
1821 // restore
1822 // The bug was that this structure was modified so that:
1823 // - The saveLayer and restore were eliminated
1824 // - The alpha was only applied to the first drawBitmapRectToRect
1825
1826 // This test draws blue and red squares inside a 50% transparent
1827 // layer. Both colours should show up muted.
1828 // When the bug is present, the red square (the second bitmap)
1829 // shows upwith full opacity.
1830
1831 SkBitmap blueBM;
1832 make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true);
1833 SkBitmap redBM;
1834 make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true);
1835 SkPaint semiTransparent;
1836 semiTransparent.setAlpha(0x80);
1837
1838 SkPictureRecorder recorder;
1839 SkCanvas* canvas = recorder.beginRecording(100, 100);
1840 canvas->drawARGB(0, 0, 0, 0);
1841
1842 canvas->saveLayer(0, &semiTransparent);
1843 canvas->drawBitmap(blueBM, 25, 25);
1844 canvas->drawBitmap(redBM, 50, 50);
1845 canvas->restore();
1846
1847 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1848
1849 // Now replay the picture back on another canvas
1850 // and check a couple of its pixels.
1851 SkBitmap replayBM;
1852 make_bm(&replayBM, 100, 100, SK_ColorBLACK, false);
1853 SkCanvas replayCanvas(replayBM);
robertphillipsc5ba71d2014-09-04 08:42:50 -07001854 picture->playback(&replayCanvas);
dneto3f22e8c2014-07-30 15:42:22 -07001855 replayCanvas.flush();
1856
1857 // With the bug present, at (55, 55) we would get a fully opaque red
1858 // intead of a dark red.
1859 REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080);
1860 REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000);
1861}
mtklein3e8232b2014-08-18 13:39:11 -07001862
1863struct CountingBBH : public SkBBoxHierarchy {
1864 mutable int searchCalls;
1865
1866 CountingBBH() : searchCalls(0) {}
1867
mtklein6bd41962014-10-02 07:41:56 -07001868 virtual void search(const SkRect& query, SkTDArray<unsigned>* results) const SK_OVERRIDE {
mtklein3e8232b2014-08-18 13:39:11 -07001869 this->searchCalls++;
1870 }
1871
mtklein4477c3c2014-10-27 10:27:10 -07001872 virtual void insert(SkAutoTMalloc<SkRect>*, int) SK_OVERRIDE {}
mtklein3e8232b2014-08-18 13:39:11 -07001873};
1874
1875class SpoonFedBBHFactory : public SkBBHFactory {
1876public:
1877 explicit SpoonFedBBHFactory(SkBBoxHierarchy* bbh) : fBBH(bbh) {}
1878 virtual SkBBoxHierarchy* operator()(int width, int height) const {
1879 return SkRef(fBBH);
1880 }
1881private:
1882 SkBBoxHierarchy* fBBH;
1883};
1884
1885// When the canvas clip covers the full picture, we don't need to call the BBH.
1886DEF_TEST(Picture_SkipBBH, r) {
1887 CountingBBH bbh;
1888 SpoonFedBBHFactory factory(&bbh);
1889
1890 SkPictureRecorder recorder;
1891 recorder.beginRecording(320, 240, &factory);
1892 SkAutoTUnref<const SkPicture> picture(recorder.endRecording());
1893
1894 SkCanvas big(640, 480), small(300, 200);
1895
robertphillipsc5ba71d2014-09-04 08:42:50 -07001896 picture->playback(&big);
mtklein3e8232b2014-08-18 13:39:11 -07001897 REPORTER_ASSERT(r, bbh.searchCalls == 0);
1898
robertphillipsc5ba71d2014-09-04 08:42:50 -07001899 picture->playback(&small);
mtklein3e8232b2014-08-18 13:39:11 -07001900 REPORTER_ASSERT(r, bbh.searchCalls == 1);
1901}
mtkleind72094d2014-08-27 12:12:23 -07001902
1903DEF_TEST(Picture_BitmapLeak, r) {
1904 SkBitmap mut, immut;
1905 mut.allocN32Pixels(300, 200);
1906 immut.allocN32Pixels(300, 200);
1907 immut.setImmutable();
1908 SkASSERT(!mut.isImmutable());
1909 SkASSERT(immut.isImmutable());
1910
1911 // No one can hold a ref on our pixels yet.
1912 REPORTER_ASSERT(r, mut.pixelRef()->unique());
1913 REPORTER_ASSERT(r, immut.pixelRef()->unique());
1914
1915 SkPictureRecorder rec;
1916 SkCanvas* canvas = rec.beginRecording(1920, 1200);
1917 canvas->drawBitmap(mut, 0, 0);
1918 canvas->drawBitmap(immut, 800, 600);
1919 SkAutoTDelete<const SkPicture> pic(rec.endRecording());
1920
1921 // The picture shares the immutable pixels but copies the mutable ones.
1922 REPORTER_ASSERT(r, mut.pixelRef()->unique());
1923 REPORTER_ASSERT(r, !immut.pixelRef()->unique());
1924
1925 // When the picture goes away, it's just our bitmaps holding the refs.
1926 pic.reset(NULL);
1927 REPORTER_ASSERT(r, mut.pixelRef()->unique());
1928 REPORTER_ASSERT(r, immut.pixelRef()->unique());
1929}