blob: b7e4bb6ccaebeadfdae4f0927c84858e87bbf6a6 [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
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00008#include "SkBlurImageFilter.h"
reed@google.com21b519d2012-10-02 17:42:15 +00009#include "SkCanvas.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000010#include "SkColorPriv.h"
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +000011#include "SkDashPathEffect.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000012#include "SkData.h"
reed@google.combf790232013-12-13 19:45:58 +000013#include "SkDecodingImageGenerator.h"
scroggo@google.com49ce11b2013-04-25 18:29:32 +000014#include "SkError.h"
halcanary@google.com3d50ea12014-01-02 13:15:13 +000015#include "SkImageEncoder.h"
16#include "SkImageGenerator.h"
reed@google.com21b519d2012-10-02 17:42:15 +000017#include "SkPaint.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000018#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000019#include "SkPictureRecorder.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000020#include "SkPictureUtils.h"
reed@google.com72aa79c2013-01-24 18:27:42 +000021#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000022#include "SkRandom.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000023#include "SkShader.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000024#include "SkStream.h"
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000025
26#if SK_SUPPORT_GPU
27#include "SkSurface.h"
28#include "GrContextFactory.h"
29#include "GrPictureUtils.h"
30#endif
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000031#include "Test.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000032
reed@google.com47b679b2014-05-14 18:58:16 +000033#include "SkLumaColorFilter.h"
34#include "SkColorFilterImageFilter.h"
35
robertphillips@google.comed9866c2014-01-09 19:20:45 +000036static const int gColorScale = 30;
37static const int gColorOffset = 60;
reed@google.comfe7b1ed2012-11-29 21:00:39 +000038
39static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000040 bm->allocN32Pixels(w, h);
reed@google.comfe7b1ed2012-11-29 21:00:39 +000041 bm->eraseColor(color);
42 if (immutable) {
43 bm->setImmutable();
44 }
45}
46
robertphillips@google.comfe5824a2014-01-09 19:45:29 +000047static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000048 SkASSERT(w % 2 == 0);
49 SkASSERT(h % 2 == 0);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000050 bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType,
51 kPremul_SkAlphaType));
robertphillips@google.comed9866c2014-01-09 19:20:45 +000052 SkAutoLockPixels lock(*bm);
53 for (int y = 0; y < h; y += 2) {
54 uint8_t* s = bm->getAddr8(0, y);
55 for (int x = 0; x < w; x += 2) {
56 *s++ = 0xFF;
57 *s++ = 0x00;
58 }
59 s = bm->getAddr8(0, y + 1);
60 for (int x = 0; x < w; x += 2) {
61 *s++ = 0x00;
62 *s++ = 0xFF;
63 }
64 }
65 if (immutable) {
66 bm->setImmutable();
67 }
68}
reed@google.comfe7b1ed2012-11-29 21:00:39 +000069
robertphillips@google.comed9866c2014-01-09 19:20:45 +000070static void init_paint(SkPaint* paint, const SkBitmap &bm) {
71 SkShader* shader = SkShader::CreateBitmapShader(bm,
72 SkShader::kClamp_TileMode,
73 SkShader::kClamp_TileMode);
74 paint->setShader(shader)->unref();
75}
76
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +000077typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000078 const SkBitmap&, const SkPoint&,
79 SkTDArray<SkPixelRef*>* usedPixRefs);
robertphillips@google.comed9866c2014-01-09 19:20:45 +000080
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000081static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000082 const SkBitmap& altBM, const SkPoint& pos,
83 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000084 SkPaint paint;
85 init_paint(&paint, bm);
86
87 canvas->drawPaint(paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000088 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +000089}
90
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000091static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000092 const SkBitmap& altBM, const SkPoint& pos,
93 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000094 SkPaint paint;
95 init_paint(&paint, bm);
96
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000097 // draw a rect
robertphillips@google.comed9866c2014-01-09 19:20:45 +000098 SkPoint points[5] = {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +000099 { pos.fX, pos.fY },
100 { pos.fX + bm.width() - 1, pos.fY },
101 { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 },
102 { pos.fX, pos.fY + bm.height() - 1 },
103 { pos.fX, pos.fY },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000104 };
105
106 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000107 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000108}
109
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000110static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000111 const SkBitmap& altBM, const SkPoint& pos,
112 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000113 SkPaint paint;
114 init_paint(&paint, bm);
115
116 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
117 r.offset(pos.fX, pos.fY);
118
119 canvas->drawRect(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000120 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000121}
122
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000123static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000124 const SkBitmap& altBM, const SkPoint& pos,
125 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000126 SkPaint paint;
127 init_paint(&paint, bm);
128
129 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
130 r.offset(pos.fX, pos.fY);
131
132 canvas->drawOval(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000133 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000134}
135
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000136static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000137 const SkBitmap& altBM, const SkPoint& pos,
138 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000139 SkPaint paint;
140 init_paint(&paint, bm);
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000141
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000142 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
143 r.offset(pos.fX, pos.fY);
144
145 SkRRect rr;
146 rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4);
147 canvas->drawRRect(rr, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000148 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000149}
150
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000151static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000152 const SkBitmap& altBM, const SkPoint& pos,
153 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000154 SkPaint paint;
155 init_paint(&paint, bm);
156
157 SkPath path;
158 path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height()));
159 path.lineTo(SkIntToScalar(bm.width()), 0);
160 path.close();
161 path.offset(pos.fX, pos.fY);
162
163 canvas->drawPath(path, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000164 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000165}
166
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000167static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000168 const SkBitmap& altBM, const SkPoint& pos,
169 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000170 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000171 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000172}
173
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000174static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000175 const SkBitmap& altBM, const SkPoint& pos,
176 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000177 SkPaint paint;
178 init_paint(&paint, bm);
179
180 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
181 canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000182 *usedPixRefs->append() = bm.pixelRef();
183 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000184}
185
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000186static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000187 const SkBitmap& altBM, const SkPoint& pos,
188 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000189 const SkMatrix& ctm = canvas->getTotalMatrix();
190
191 SkPoint p(pos);
192 ctm.mapPoints(&p, 1);
193
194 canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000195 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000196}
197
198#if 0
199// Although specifiable, this case doesn't seem to make sense (i.e., the
200// bitmap in the shader is never used).
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000201static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000202 const SkBitmap& altBM, const SkPoint& pos,
203 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000204 SkPaint paint;
205 init_paint(&paint, bm);
206
207 const SkMatrix& ctm = canvas->getTotalMatrix();
208
209 SkPoint p(pos);
210 ctm.mapPoints(&p, 1);
211
212 canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000213 *usedPixRefs->append() = bm.pixelRef();
214 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000215}
216#endif
217
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000218static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000219 const SkBitmap& altBM, const SkPoint& pos,
220 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000221 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
222
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000223 r.offset(pos.fX, pos.fY);
224 canvas->drawBitmapRectToRect(bm, NULL, r, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000225 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000226}
227
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000228static void drawbitmaprect_withshader_proc(SkCanvas* canvas,
229 const SkBitmap& bm,
230 const SkBitmap& altBM,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000231 const SkPoint& pos,
232 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000233 SkPaint paint;
234 init_paint(&paint, bm);
235
236 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000237 r.offset(pos.fX, pos.fY);
238
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000239 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
240 canvas->drawBitmapRectToRect(altBM, NULL, r, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000241 *usedPixRefs->append() = bm.pixelRef();
242 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000243}
244
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000245static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000246 const SkBitmap& altBM, const SkPoint& pos,
247 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000248 SkPaint paint;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000249 init_paint(&paint, bm);
250 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
251
252 canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000253 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000254}
255
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000256static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000257 const SkBitmap& altBM, const SkPoint& pos,
258 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000259 SkPaint paint;
260 init_paint(&paint, bm);
261 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
262
263 SkPoint point = { pos.fX, pos.fY + bm.height() };
264 canvas->drawPosText("O", 1, &point, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000265 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000266}
267
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000268static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000269 const SkBitmap& altBM, const SkPoint& pos,
270 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000271 SkPaint paint;
272
273 init_paint(&paint, bm);
274 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
275
276 SkPath path;
277 path.lineTo(SkIntToScalar(bm.width()), 0);
278 path.offset(pos.fX, pos.fY+bm.height());
279
280 canvas->drawTextOnPath("O", 1, path, NULL, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000281 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000282}
283
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000284static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000285 const SkBitmap& altBM, const SkPoint& pos,
286 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000287 SkPaint paint;
288 init_paint(&paint, bm);
289
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000290 SkPoint verts[4] = {
291 { pos.fX, pos.fY },
292 { pos.fX + bm.width(), pos.fY },
293 { pos.fX + bm.width(), pos.fY + bm.height() },
294 { pos.fX, pos.fY + bm.height() }
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000295 };
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000296 SkPoint texs[4] = { { 0, 0 },
297 { SkIntToScalar(bm.width()), 0 },
298 { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000299 { 0, SkIntToScalar(bm.height()) } };
300 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 };
301
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000302 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL,
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000303 indices, 6, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000304 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000305}
306
307// Return a picture with the bitmaps drawn at the specified positions.
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000308static SkPicture* record_bitmaps(const SkBitmap bm[],
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000309 const SkPoint pos[],
310 SkTDArray<SkPixelRef*> analytic[],
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000311 int count,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000312 DrawBitmapProc proc) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000313 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -0700314 SkCanvas* canvas = recorder.beginRecording(1000, 1000);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000315 for (int i = 0; i < count; ++i) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000316 analytic[i].rewind();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000317 canvas->save();
318 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY,
319 SkIntToScalar(bm[i].width()),
320 SkIntToScalar(bm[i].height()));
321 canvas->clipRect(clipRect, SkRegion::kIntersect_Op);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000322 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000323 canvas->restore();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000324 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000325 return recorder.endRecording();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000326}
327
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000328static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000329 rect->fLeft = rand.nextRangeScalar(-W, 2*W);
330 rect->fTop = rand.nextRangeScalar(-H, 2*H);
331 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W);
332 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H);
333
334 // we integralize rect to make our tests more predictable, since Gather is
335 // a little sloppy.
336 SkIRect ir;
337 rect->round(&ir);
338 rect->set(ir);
339}
340
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000341static void draw(SkPicture* pic, int width, int height, SkBitmap* result) {
342 make_bm(result, width, height, SK_ColorBLACK, false);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000343
344 SkCanvas canvas(*result);
robertphillips9b14f262014-06-04 05:40:44 -0700345 canvas.drawPicture(pic);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000346}
347
348template <typename T> int find_index(const T* array, T elem, int count) {
349 for (int i = 0; i < count; ++i) {
350 if (array[i] == elem) {
351 return i;
352 }
353 }
354 return -1;
355}
356
357// Return true if 'ref' is found in array[]
358static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) {
359 return find_index<const SkPixelRef*>(array, ref, count) >= 0;
360}
361
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000362// Look at each pixel that is inside 'subset', and if its color appears in
363// colors[], find the corresponding value in refs[] and append that ref into
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000364// array, skipping duplicates of the same value.
365// Note that gathering pixelRefs from rendered colors suffers from the problem
366// that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color)
367// isn't easy to reconstruct.
368static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[],
369 int count, SkTDArray<SkPixelRef*>* array,
370 const SkRect& subset) {
371 SkIRect ir;
372 subset.roundOut(&ir);
373
374 if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) {
375 return;
376 }
377
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000378 // Since we only want to return unique values in array, when we scan we just
379 // set a bit for each index'd color found. In practice we only have a few
380 // distinct colors, so we just use an int's bits as our array. Hence the
381 // assert that count <= number-of-bits-in-our-int.
382 SkASSERT((unsigned)count <= 32);
383 uint32_t bitarray = 0;
384
385 SkAutoLockPixels alp(bm);
386
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000387 for (int y = ir.fTop; y < ir.fBottom; ++y) {
388 for (int x = ir.fLeft; x < ir.fRight; ++x) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000389 SkPMColor pmc = *bm.getAddr32(x, y);
390 // the only good case where the color is not found would be if
391 // the color is transparent, meaning no bitmap was drawn in that
392 // pixel.
393 if (pmc) {
bsalomon@google.comc3d753e2013-01-08 17:24:44 +0000394 uint32_t index = SkGetPackedR32(pmc);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000395 SkASSERT(SkGetPackedG32(pmc) == index);
396 SkASSERT(SkGetPackedB32(pmc) == index);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000397 if (0 == index) {
398 continue; // background color
399 }
400 SkASSERT(0 == (index - gColorOffset) % gColorScale);
401 index = (index - gColorOffset) / gColorScale;
bsalomon@google.com5f429b02013-01-08 18:42:20 +0000402 SkASSERT(static_cast<int>(index) < count);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000403 bitarray |= 1 << index;
404 }
405 }
406 }
407
408 for (int i = 0; i < count; ++i) {
409 if (bitarray & (1 << i)) {
410 *array->append() = refs[i];
411 }
412 }
413}
414
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000415static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h,
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000416 const SkTDArray<SkPixelRef*> analytic[],
417 int count,
418 SkTDArray<SkPixelRef*>* result,
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000419 const SkRect& subset) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000420 for (int i = 0; i < count; ++i) {
421 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h);
422
423 if (SkRect::Intersects(subset, rect)) {
424 result->append(analytic[i].count(), analytic[i].begin());
425 }
426 }
427}
428
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000429
430static const struct {
431 const DrawBitmapProc proc;
432 const char* const desc;
433} gProcs[] = {
434 {drawpaint_proc, "drawpaint"},
435 {drawpoints_proc, "drawpoints"},
436 {drawrect_proc, "drawrect"},
437 {drawoval_proc, "drawoval"},
438 {drawrrect_proc, "drawrrect"},
439 {drawpath_proc, "drawpath"},
440 {drawbitmap_proc, "drawbitmap"},
441 {drawbitmap_withshader_proc, "drawbitmap_withshader"},
442 {drawsprite_proc, "drawsprite"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000443#if 0
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000444 {drawsprite_withshader_proc, "drawsprite_withshader"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000445#endif
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000446 {drawbitmaprect_proc, "drawbitmaprect"},
447 {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"},
448 {drawtext_proc, "drawtext"},
449 {drawpostext_proc, "drawpostext"},
450 {drawtextonpath_proc, "drawtextonpath"},
451 {drawverts_proc, "drawverts"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000452};
453
454static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000455 // Our convention is that the color components contain an encoding of
456 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000457 // reserved for the background
458 for (int i = 0; i < num; ++i) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000459 make_bm(&bm[i], w, h,
460 SkColorSetARGB(0xFF,
461 gColorScale*i+gColorOffset,
462 gColorScale*i+gColorOffset,
463 gColorScale*i+gColorOffset),
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000464 true);
465 refs[i] = bm[i].pixelRef();
466 }
467
468 // The A8 alternate bitmaps are all BW checkerboards
469 for (int i = 0; i < num; ++i) {
470 make_checkerboard(&bm[num+i], w, h, true);
471 refs[num+i] = bm[num+i].pixelRef();
472 }
473}
474
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000475static void test_gatherpixelrefs(skiatest::Reporter* reporter) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000476 const int IW = 32;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000477 const int IH = IW;
478 const SkScalar W = SkIntToScalar(IW);
479 const SkScalar H = W;
480
481 static const int N = 4;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000482 SkBitmap bm[2*N];
483 SkPixelRef* refs[2*N];
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000484 SkTDArray<SkPixelRef*> analytic[N];
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000485
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000486 const SkPoint pos[N] = {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000487 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
488 };
489
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000490 create_textures(bm, refs, N, IW, IH);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000491
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000492 SkRandom rand;
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000493 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000494 SkAutoTUnref<SkPicture> pic(
495 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000496
tomhudson@google.com381010e2013-10-24 11:12:47 +0000497 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000498 // quick check for a small piece of each quadrant, which should just
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000499 // contain 1 or 2 bitmaps.
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000500 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
501 SkRect r;
502 r.set(2, 2, W - 2, H - 2);
503 r.offset(pos[i].fX, pos[i].fY);
504 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r));
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000505 if (!data) {
506 ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned "
507 "NULL for %s.", gProcs[k].desc);
508 continue;
509 }
510 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data();
511 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*));
512 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
513 if (1 == count) {
514 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
515 } else if (2 == count) {
516 REPORTER_ASSERT(reporter,
517 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
518 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
commit-bot@chromium.orgfe433c12013-07-09 16:04:32 +0000519 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000520 }
521
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000522 SkBitmap image;
523 draw(pic, 2*IW, 2*IH, &image);
524
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000525 // Test a bunch of random (mostly) rects, and compare the gather results
526 // with a deduced list of refs by looking at the colors drawn.
527 for (int j = 0; j < 100; ++j) {
528 SkRect r;
529 rand_rect(&r, rand, 2*W, 2*H);
530
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000531 SkTDArray<SkPixelRef*> fromImage;
532 gather_from_image(image, refs, N, &fromImage, r);
533
534 SkTDArray<SkPixelRef*> fromAnalytic;
535 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000536
537 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
538 size_t dataSize = data ? data->size() : 0;
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000539 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000540 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize);
541 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL;
542 SkAutoDataUnref adu(data);
543
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000544 // Everything that we saw drawn should appear in the analytic list
545 // but the analytic list may contain some pixelRefs that were not
546 // seen in the image (e.g., A8 textures used as masks)
547 for (int i = 0; i < fromImage.count(); ++i) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000548 if (-1 == fromAnalytic.find(fromImage[i])) {
549 ERRORF(reporter, "PixelRef missing %d %s",
550 i, gProcs[k].desc);
551 }
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000552 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000553
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000554 /*
555 * GatherPixelRefs is conservative, so it can return more bitmaps
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000556 * than are strictly required. Thus our check here is only that
557 * Gather didn't miss any that we actually needed. Even that isn't
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000558 * a strict requirement on Gather, which is meant to be quick and
559 * only mostly-correct, but at the moment this test should work.
560 */
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000561 for (int i = 0; i < fromAnalytic.count(); ++i) {
562 bool found = find(gatherRefs, fromAnalytic[i], gatherCount);
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000563 if (!found) {
564 ERRORF(reporter, "PixelRef missing %d %s",
565 i, gProcs[k].desc);
566 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000567#if 0
568 // enable this block of code to debug failures, as it will rerun
569 // the case that failed.
570 if (!found) {
571 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
572 size_t dataSize = data ? data->size() : 0;
573 }
574#endif
575 }
576 }
577 }
578}
579
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000580static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) {
581 const int IW = 32;
582 const int IH = IW;
583 const SkScalar W = SkIntToScalar(IW);
584 const SkScalar H = W;
585
586 static const int N = 4;
587 SkBitmap bm[2*N];
588 SkPixelRef* refs[2*N];
589 SkTDArray<SkPixelRef*> analytic[N];
590
591 const SkPoint pos[N] = {
592 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
593 };
594
595 create_textures(bm, refs, N, IW, IH);
596
597 SkRandom rand;
598 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000599 SkAutoTUnref<SkPicture> pic(
600 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000601
602 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
603
604 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont(
605 new SkPictureUtils::SkPixelRefsAndRectsList);
606
607 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont);
608
609 // quick check for a small piece of each quadrant, which should just
610 // contain 1 or 2 bitmaps.
611 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
612 SkRect r;
613 r.set(2, 2, W - 2, H - 2);
614 r.offset(pos[i].fX, pos[i].fY);
615
616 SkTDArray<SkPixelRef*> gatheredRefs;
617 prCont->query(r, &gatheredRefs);
618
619 int count = gatheredRefs.count();
620 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
621 if (1 == count) {
622 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
623 } else if (2 == count) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000624 REPORTER_ASSERT(reporter,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000625 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
626 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
627 }
628 }
629
630 SkBitmap image;
631 draw(pic, 2*IW, 2*IH, &image);
632
633 // Test a bunch of random (mostly) rects, and compare the gather results
634 // with the analytic results and the pixel refs seen in a rendering.
635 for (int j = 0; j < 100; ++j) {
636 SkRect r;
637 rand_rect(&r, rand, 2*W, 2*H);
638
639 SkTDArray<SkPixelRef*> fromImage;
640 gather_from_image(image, refs, N, &fromImage, r);
641
642 SkTDArray<SkPixelRef*> fromAnalytic;
643 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
644
645 SkTDArray<SkPixelRef*> gatheredRefs;
646 prCont->query(r, &gatheredRefs);
647
648 // Everything that we saw drawn should appear in the analytic list
649 // but the analytic list may contain some pixelRefs that were not
650 // seen in the image (e.g., A8 textures used as masks)
651 for (int i = 0; i < fromImage.count(); ++i) {
652 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]));
653 }
654
655 // Everything in the analytic list should appear in the gathered
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000656 // list.
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000657 for (int i = 0; i < fromAnalytic.count(); ++i) {
658 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i]));
659 }
660 }
661 }
662}
663
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000664#ifdef SK_DEBUG
robertphillipsdb539902014-07-01 08:47:04 -0700665// Ensure that deleting an empty SkPicture does not assert. Asserts only fire
666// in debug mode, so only run in debug mode.
667static void test_deleting_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000668 SkPictureRecorder recorder;
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000669 // Creates an SkPictureRecord
robertphillips9f1c2412014-06-09 06:25:34 -0700670 recorder.beginRecording(0, 0);
robertphillipsdb539902014-07-01 08:47:04 -0700671 // Turns that into an SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000672 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
robertphillipsdb539902014-07-01 08:47:04 -0700673 // Ceates a new SkPictureRecord
robertphillips9f1c2412014-06-09 06:25:34 -0700674 recorder.beginRecording(0, 0);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000675}
676
677// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
678static void test_serializing_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000679 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -0700680 recorder.beginRecording(0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000681 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000682 SkDynamicMemoryWStream stream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000683 picture->serialize(&stream);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000684}
685#endif
686
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000687static void rand_op(SkCanvas* canvas, SkRandom& rand) {
reed@google.com21b519d2012-10-02 17:42:15 +0000688 SkPaint paint;
689 SkRect rect = SkRect::MakeWH(50, 50);
690
691 SkScalar unit = rand.nextUScalar1();
692 if (unit <= 0.3) {
693// SkDebugf("save\n");
694 canvas->save();
695 } else if (unit <= 0.6) {
696// SkDebugf("restore\n");
697 canvas->restore();
698 } else if (unit <= 0.9) {
699// SkDebugf("clip\n");
700 canvas->clipRect(rect);
701 } else {
702// SkDebugf("draw\n");
703 canvas->drawPaint(paint);
704 }
705}
706
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000707#if SK_SUPPORT_GPU
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000708static void test_gpu_veto(skiatest::Reporter* reporter) {
709
710 SkPictureRecorder recorder;
711
robertphillips9f1c2412014-06-09 06:25:34 -0700712 SkCanvas* canvas = recorder.beginRecording(100, 100);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000713 {
714 SkPath path;
715 path.moveTo(0, 0);
716 path.lineTo(50, 50);
717
718 SkScalar intervals[] = { 1.0f, 1.0f };
719 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0));
720
721 SkPaint paint;
722 paint.setStyle(SkPaint::kStroke_Style);
723 paint.setPathEffect(dash);
724
725 canvas->drawPath(path, paint);
726 }
727 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
728 // path effects currently render an SkPicture undesireable for GPU rendering
commit-bot@chromium.orga1ff26a2014-05-30 21:52:52 +0000729
730 const char *reason = NULL;
731 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL, &reason));
732 REPORTER_ASSERT(reporter, NULL != reason);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000733
robertphillips9f1c2412014-06-09 06:25:34 -0700734 canvas = recorder.beginRecording(100, 100);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000735 {
736 SkPath path;
737
738 path.moveTo(0, 0);
739 path.lineTo(0, 50);
740 path.lineTo(25, 25);
741 path.lineTo(50, 50);
742 path.lineTo(50, 0);
743 path.close();
744 REPORTER_ASSERT(reporter, !path.isConvex());
745
746 SkPaint paint;
747 paint.setAntiAlias(true);
748 for (int i = 0; i < 50; ++i) {
749 canvas->drawPath(path, paint);
750 }
751 }
752 picture.reset(recorder.endRecording());
753 // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering
754 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
755
robertphillips9f1c2412014-06-09 06:25:34 -0700756 canvas = recorder.beginRecording(100, 100);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000757 {
758 SkPath path;
759
760 path.moveTo(0, 0);
761 path.lineTo(0, 50);
762 path.lineTo(25, 25);
763 path.lineTo(50, 50);
764 path.lineTo(50, 0);
765 path.close();
766 REPORTER_ASSERT(reporter, !path.isConvex());
767
768 SkPaint paint;
769 paint.setAntiAlias(true);
770 paint.setStyle(SkPaint::kStroke_Style);
771 paint.setStrokeWidth(0);
772 for (int i = 0; i < 50; ++i) {
773 canvas->drawPath(path, paint);
774 }
775 }
776 picture.reset(recorder.endRecording());
777 // hairline stroked AA concave paths are fine for GPU rendering
778 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL));
779}
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000780
781static void test_gpu_picture_optimization(skiatest::Reporter* reporter,
782 GrContextFactory* factory) {
bsalomone904c092014-07-17 10:50:59 -0700783 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
784 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000785
bsalomone904c092014-07-17 10:50:59 -0700786 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
787 continue;
788 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000789
bsalomone904c092014-07-17 10:50:59 -0700790 GrContext* context = factory->get(glCtxType);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000791
bsalomone904c092014-07-17 10:50:59 -0700792 if (NULL == context) {
793 continue;
794 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000795
bsalomone904c092014-07-17 10:50:59 -0700796 static const int kWidth = 100;
797 static const int kHeight = 100;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000798
bsalomone904c092014-07-17 10:50:59 -0700799 SkAutoTUnref<SkPicture> pict;
800
801 // create a picture with the structure:
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000802 // 1)
bsalomone904c092014-07-17 10:50:59 -0700803 // SaveLayer
804 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000805 // 2)
bsalomone904c092014-07-17 10:50:59 -0700806 // SaveLayer
807 // Translate
808 // SaveLayer w/ bound
809 // Restore
810 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000811 // 3)
bsalomone904c092014-07-17 10:50:59 -0700812 // SaveLayer w/ copyable paint
813 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000814 // 4)
bsalomone904c092014-07-17 10:50:59 -0700815 // SaveLayer w/ non-copyable paint
816 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000817 {
bsalomone904c092014-07-17 10:50:59 -0700818 SkPictureRecorder recorder;
819
820 SkCanvas* c = recorder.beginRecording(kWidth, kHeight);
821 // 1)
822 c->saveLayer(NULL, NULL);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000823 c->restore();
bsalomone904c092014-07-17 10:50:59 -0700824
825 // 2)
826 c->saveLayer(NULL, NULL);
827 c->translate(kWidth/2, kHeight/2);
828 SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2);
829 c->saveLayer(&r, NULL);
830 c->restore();
831 c->restore();
832
833 // 3)
834 {
835 SkPaint p;
836 p.setColor(SK_ColorRED);
837 c->saveLayer(NULL, &p);
838 c->restore();
839 }
840 // 4)
841 // TODO: this case will need to be removed once the paint's are immutable
842 {
843 SkPaint p;
844 SkAutoTUnref<SkColorFilter> cf(SkLumaColorFilter::Create());
845 p.setImageFilter(SkColorFilterImageFilter::Create(cf.get()))->unref();
846 c->saveLayer(NULL, &p);
847 c->restore();
848 }
849
850 pict.reset(recorder.endRecording());
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000851 }
852
bsalomone904c092014-07-17 10:50:59 -0700853 // Now test out the SaveLayer extraction
854 {
855 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000856
bsalomone904c092014-07-17 10:50:59 -0700857 SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info));
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000858
bsalomone904c092014-07-17 10:50:59 -0700859 SkCanvas* canvas = surface->getCanvas();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000860
bsalomone904c092014-07-17 10:50:59 -0700861 canvas->EXPERIMENTAL_optimize(pict);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000862
robertphillips6617d502014-08-18 09:39:34 -0700863 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000864
bsalomone904c092014-07-17 10:50:59 -0700865 const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key);
866 REPORTER_ASSERT(reporter, NULL != data);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000867
robertphillips6617d502014-08-18 09:39:34 -0700868 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data);
bsalomone904c092014-07-17 10:50:59 -0700869 REPORTER_ASSERT(reporter, 5 == gpuData->numSaveLayers());
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000870
robertphillips6617d502014-08-18 09:39:34 -0700871 const GrAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0);
bsalomone904c092014-07-17 10:50:59 -0700872 // The parent/child layer appear in reverse order
robertphillips6617d502014-08-18 09:39:34 -0700873 const GrAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2);
874 const GrAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1);
875 const GrAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3);
876// const GrAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(4);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000877
bsalomone904c092014-07-17 10:50:59 -0700878 REPORTER_ASSERT(reporter, info0.fValid);
879 REPORTER_ASSERT(reporter, kWidth == info0.fSize.fWidth && kHeight == info0.fSize.fHeight);
880 REPORTER_ASSERT(reporter, info0.fCTM.isIdentity());
881 REPORTER_ASSERT(reporter, 0 == info0.fOffset.fX && 0 == info0.fOffset.fY);
882 REPORTER_ASSERT(reporter, NULL != info0.fPaint);
883 REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000884
bsalomone904c092014-07-17 10:50:59 -0700885 REPORTER_ASSERT(reporter, info1.fValid);
886 REPORTER_ASSERT(reporter, kWidth == info1.fSize.fWidth && kHeight == info1.fSize.fHeight);
887 REPORTER_ASSERT(reporter, info1.fCTM.isIdentity());
888 REPORTER_ASSERT(reporter, 0 == info1.fOffset.fX && 0 == info1.fOffset.fY);
889 REPORTER_ASSERT(reporter, NULL != info1.fPaint);
890 REPORTER_ASSERT(reporter, !info1.fIsNested && info1.fHasNestedLayers); // has a nested SL
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000891
bsalomone904c092014-07-17 10:50:59 -0700892 REPORTER_ASSERT(reporter, info2.fValid);
893 REPORTER_ASSERT(reporter, kWidth/2 == info2.fSize.fWidth &&
894 kHeight/2 == info2.fSize.fHeight); // bound reduces size
895 REPORTER_ASSERT(reporter, info2.fCTM.isIdentity()); // translated
896 REPORTER_ASSERT(reporter, kWidth/2 == info2.fOffset.fX &&
897 kHeight/2 == info2.fOffset.fY);
898 REPORTER_ASSERT(reporter, NULL != info1.fPaint);
899 REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000900
bsalomone904c092014-07-17 10:50:59 -0700901 REPORTER_ASSERT(reporter, info3.fValid);
902 REPORTER_ASSERT(reporter, kWidth == info3.fSize.fWidth && kHeight == info3.fSize.fHeight);
903 REPORTER_ASSERT(reporter, info3.fCTM.isIdentity());
904 REPORTER_ASSERT(reporter, 0 == info3.fOffset.fX && 0 == info3.fOffset.fY);
905 REPORTER_ASSERT(reporter, NULL != info3.fPaint);
906 REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000907
bsalomone904c092014-07-17 10:50:59 -0700908 #if 0 // needs more though for GrGatherCanvas
909 REPORTER_ASSERT(reporter, !info4.fValid); // paint is/was uncopyable
910 REPORTER_ASSERT(reporter, kWidth == info4.fSize.fWidth && kHeight == info4.fSize.fHeight);
911 REPORTER_ASSERT(reporter, 0 == info4.fOffset.fX && 0 == info4.fOffset.fY);
912 REPORTER_ASSERT(reporter, info4.fCTM.isIdentity());
913 REPORTER_ASSERT(reporter, NULL == info4.fPaint); // paint is/was uncopyable
914 REPORTER_ASSERT(reporter, !info4.fIsNested && !info4.fHasNestedLayers);
915 #endif
916 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000917 }
918}
919
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000920#endif
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000921
ajuma750ae262014-08-18 12:59:55 -0700922static void test_has_text(skiatest::Reporter* reporter) {
923 SkPictureRecorder recorder;
924 SkPaint paint;
925 paint.setColor(SK_ColorBLUE);
926 SkPoint point = SkPoint::Make(10, 10);
927
928 SkCanvas* canvas = recorder.beginRecording(100, 100);
929 {
930 canvas->drawRect(SkRect::MakeWH(20, 20), paint);
931 }
932 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
933 REPORTER_ASSERT(reporter, !picture->hasText());
934
935 canvas = recorder.beginRecording(100, 100);
936 {
937 canvas->drawText("Q", 1, point.fX, point.fY, paint);
938 }
939 picture.reset(recorder.endRecording());
940 REPORTER_ASSERT(reporter, picture->hasText());
941
942 canvas = recorder.beginRecording(100, 100);
943 {
944 canvas->drawPosText("Q", 1, &point, paint);
945 }
946 picture.reset(recorder.endRecording());
947 REPORTER_ASSERT(reporter, picture->hasText());
948
949 canvas = recorder.beginRecording(100, 100);
950 {
951 canvas->drawPosTextH("Q", 1, &point.fX, point.fY, paint);
952 }
953 picture.reset(recorder.endRecording());
954 REPORTER_ASSERT(reporter, picture->hasText());
955
956 canvas = recorder.beginRecording(100, 100);
957 {
958 SkPath path;
959 path.moveTo(0, 0);
960 path.lineTo(50, 50);
961
962 canvas->drawTextOnPathHV("Q", 1, path, point.fX, point.fY, paint);
963 }
964 picture.reset(recorder.endRecording());
965 REPORTER_ASSERT(reporter, picture->hasText());
966
967 canvas = recorder.beginRecording(100, 100);
968 {
969 SkPath path;
970 path.moveTo(0, 0);
971 path.lineTo(50, 50);
972
973 canvas->drawTextOnPath("Q", 1, path, NULL, paint);
974 }
975 picture.reset(recorder.endRecording());
976 REPORTER_ASSERT(reporter, picture->hasText());
977}
978
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000979static void set_canvas_to_save_count_4(SkCanvas* canvas) {
980 canvas->restoreToCount(1);
981 canvas->save();
982 canvas->save();
983 canvas->save();
984}
985
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +0000986/**
987 * A canvas that records the number of saves, saveLayers and restores.
988 */
989class SaveCountingCanvas : public SkCanvas {
990public:
991 SaveCountingCanvas(int width, int height)
992 : INHERITED(width, height)
993 , fSaveCount(0)
994 , fSaveLayerCount(0)
995 , fRestoreCount(0){
996 }
997
998 virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint,
999 SaveFlags flags) SK_OVERRIDE {
1000 ++fSaveLayerCount;
1001 return this->INHERITED::willSaveLayer(bounds, paint, flags);
1002 }
1003
Florin Malita5f6102d2014-06-30 10:13:28 -04001004 virtual void willSave() SK_OVERRIDE {
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001005 ++fSaveCount;
Florin Malita5f6102d2014-06-30 10:13:28 -04001006 this->INHERITED::willSave();
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001007 }
1008
1009 virtual void willRestore() SK_OVERRIDE {
1010 ++fRestoreCount;
1011 this->INHERITED::willRestore();
1012 }
1013
1014 unsigned int getSaveCount() const { return fSaveCount; }
1015 unsigned int getSaveLayerCount() const { return fSaveLayerCount; }
1016 unsigned int getRestoreCount() const { return fRestoreCount; }
1017
1018private:
1019 unsigned int fSaveCount;
1020 unsigned int fSaveLayerCount;
1021 unsigned int fRestoreCount;
1022
1023 typedef SkCanvas INHERITED;
1024};
1025
skia.committer@gmail.com8e7d37d2014-05-28 03:06:06 +00001026void check_save_state(skiatest::Reporter* reporter, SkPicture* picture,
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001027 unsigned int numSaves, unsigned int numSaveLayers,
1028 unsigned int numRestores) {
1029 SaveCountingCanvas canvas(picture->width(), picture->height());
1030
1031 picture->draw(&canvas);
1032
1033 REPORTER_ASSERT(reporter, numSaves == canvas.getSaveCount());
1034 REPORTER_ASSERT(reporter, numSaveLayers == canvas.getSaveLayerCount());
1035 REPORTER_ASSERT(reporter, numRestores == canvas.getRestoreCount());
1036}
1037
1038// This class exists so SkPicture can friend it and give it access to
1039// the 'partialReplay' method.
1040class SkPictureRecorderReplayTester {
1041public:
1042 static SkPicture* Copy(SkPictureRecorder* recorder) {
1043 SkPictureRecorder recorder2;
1044
robertphillips9f1c2412014-06-09 06:25:34 -07001045 SkCanvas* canvas = recorder2.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001046
1047 recorder->partialReplay(canvas);
1048
1049 return recorder2.endRecording();
1050 }
1051};
1052
robertphillips9058d602014-06-10 11:45:46 -07001053static void create_imbalance(SkCanvas* canvas) {
1054 SkRect clipRect = SkRect::MakeWH(2, 2);
1055 SkRect drawRect = SkRect::MakeWH(10, 10);
1056 canvas->save();
1057 canvas->clipRect(clipRect, SkRegion::kReplace_Op);
1058 canvas->translate(1.0f, 1.0f);
1059 SkPaint p;
1060 p.setColor(SK_ColorGREEN);
1061 canvas->drawRect(drawRect, p);
1062 // no restore
1063}
1064
1065// This tests that replaying a potentially unbalanced picture into a canvas
1066// doesn't affect the canvas' save count or matrix/clip state.
1067static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) {
1068 SkBitmap bm;
1069 bm.allocN32Pixels(4, 3);
1070 SkCanvas canvas(bm);
1071
1072 int beforeSaveCount = canvas.getSaveCount();
1073
1074 SkMatrix beforeMatrix = canvas.getTotalMatrix();
1075
1076 SkRect beforeClip;
1077
1078 canvas.getClipBounds(&beforeClip);
1079
1080 canvas.drawPicture(picture);
1081
1082 REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount());
1083 REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix());
1084
1085 SkRect afterClip;
1086
1087 canvas.getClipBounds(&afterClip);
1088
1089 REPORTER_ASSERT(reporter, afterClip == beforeClip);
1090}
1091
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001092// Test out SkPictureRecorder::partialReplay
1093DEF_TEST(PictureRecorder_replay, reporter) {
1094 // check save/saveLayer state
1095 {
1096 SkPictureRecorder recorder;
1097
robertphillips9f1c2412014-06-09 06:25:34 -07001098 SkCanvas* canvas = recorder.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001099
1100 canvas->saveLayer(NULL, NULL);
1101
1102 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1103
1104 // The extra save and restore comes from the Copy process.
1105 check_save_state(reporter, copy, 2, 1, 3);
1106
1107 canvas->saveLayer(NULL, NULL);
1108
1109 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1110
1111 check_save_state(reporter, final, 1, 2, 3);
1112
1113 // The copy shouldn't pick up any operations added after it was made
1114 check_save_state(reporter, copy, 2, 1, 3);
1115 }
1116
1117 // (partially) check leakage of draw ops
1118 {
1119 SkPictureRecorder recorder;
1120
robertphillips9f1c2412014-06-09 06:25:34 -07001121 SkCanvas* canvas = recorder.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001122
1123 SkRect r = SkRect::MakeWH(5, 5);
1124 SkPaint p;
1125
1126 canvas->drawRect(r, p);
1127
1128 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1129
1130 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1131
1132 SkBitmap bm;
1133 make_bm(&bm, 10, 10, SK_ColorRED, true);
1134
1135 r.offset(5.0f, 5.0f);
1136 canvas->drawBitmapRectToRect(bm, NULL, r);
1137
1138 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1139 REPORTER_ASSERT(reporter, final->willPlayBackBitmaps());
1140
1141 REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID());
1142
1143 // The snapshot shouldn't pick up any operations added after it was made
1144 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1145 }
robertphillips9058d602014-06-10 11:45:46 -07001146
1147 // Recreate the Android partialReplay test case
1148 {
1149 SkPictureRecorder recorder;
1150
1151 SkCanvas* canvas = recorder.beginRecording(4, 3, NULL, 0);
1152 create_imbalance(canvas);
1153
1154 int expectedSaveCount = canvas->getSaveCount();
1155
1156 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1157 check_balance(reporter, copy);
1158
1159 REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount());
1160
1161 // End the recording of source to test the picture finalization
1162 // process isn't complicated by the partialReplay step
1163 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1164 }
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001165}
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001166
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001167static void test_unbalanced_save_restores(skiatest::Reporter* reporter) {
1168 SkCanvas testCanvas(100, 100);
1169 set_canvas_to_save_count_4(&testCanvas);
1170
1171 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1172
1173 SkPaint paint;
1174 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000);
1175
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001176 SkPictureRecorder recorder;
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001177
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001178 {
1179 // Create picture with 2 unbalanced saves
robertphillips9f1c2412014-06-09 06:25:34 -07001180 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001181 canvas->save();
1182 canvas->translate(10, 10);
1183 canvas->drawRect(rect, paint);
1184 canvas->save();
1185 canvas->translate(10, 10);
1186 canvas->drawRect(rect, paint);
1187 SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording());
1188
robertphillips9b14f262014-06-04 05:40:44 -07001189 testCanvas.drawPicture(extraSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001190 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1191 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001192
1193 set_canvas_to_save_count_4(&testCanvas);
1194
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001195 {
1196 // Create picture with 2 unbalanced restores
robertphillips9f1c2412014-06-09 06:25:34 -07001197 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001198 canvas->save();
1199 canvas->translate(10, 10);
1200 canvas->drawRect(rect, paint);
1201 canvas->save();
1202 canvas->translate(10, 10);
1203 canvas->drawRect(rect, paint);
1204 canvas->restore();
1205 canvas->restore();
1206 canvas->restore();
1207 canvas->restore();
1208 SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording());
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001209
robertphillips9b14f262014-06-04 05:40:44 -07001210 testCanvas.drawPicture(extraRestorePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001211 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1212 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001213
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001214 set_canvas_to_save_count_4(&testCanvas);
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001215
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001216 {
robertphillips9f1c2412014-06-09 06:25:34 -07001217 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001218 canvas->translate(10, 10);
1219 canvas->drawRect(rect, paint);
1220 SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording());
1221
robertphillips9b14f262014-06-04 05:40:44 -07001222 testCanvas.drawPicture(noSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001223 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1224 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity());
1225 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001226}
1227
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001228static void test_peephole() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001229 SkRandom rand;
reed@google.com21b519d2012-10-02 17:42:15 +00001230
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001231 SkPictureRecorder recorder;
1232
reed@google.com21b519d2012-10-02 17:42:15 +00001233 for (int j = 0; j < 100; j++) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001234 SkRandom rand2(rand); // remember the seed
reed@google.com21b519d2012-10-02 17:42:15 +00001235
robertphillips9f1c2412014-06-09 06:25:34 -07001236 SkCanvas* canvas = recorder.beginRecording(100, 100);
reed@google.com21b519d2012-10-02 17:42:15 +00001237
1238 for (int i = 0; i < 1000; ++i) {
1239 rand_op(canvas, rand);
1240 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001241 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
jvanverth@google.comc490f802013-03-04 13:56:38 +00001242
1243 rand = rand2;
reed@google.com21b519d2012-10-02 17:42:15 +00001244 }
1245
1246 {
robertphillips9f1c2412014-06-09 06:25:34 -07001247 SkCanvas* canvas = recorder.beginRecording(100, 100);
reed@google.com21b519d2012-10-02 17:42:15 +00001248 SkRect rect = SkRect::MakeWH(50, 50);
skia.committer@gmail.com52c24372012-10-03 02:01:13 +00001249
reed@google.com21b519d2012-10-02 17:42:15 +00001250 for (int i = 0; i < 100; ++i) {
1251 canvas->save();
1252 }
1253 while (canvas->getSaveCount() > 1) {
1254 canvas->clipRect(rect);
1255 canvas->restore();
1256 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001257 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
reed@google.com21b519d2012-10-02 17:42:15 +00001258 }
1259}
1260
scroggo@google.com4b90b112012-12-04 15:08:56 +00001261#ifndef SK_DEBUG
1262// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
1263// should never do this.
1264static void test_bad_bitmap() {
1265 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
1266 // fail.
1267 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001268 bm.setInfo(SkImageInfo::MakeN32Premul(100, 100));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001269 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001270 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001271 recordingCanvas->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001272 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.com4b90b112012-12-04 15:08:56 +00001273
1274 SkCanvas canvas;
robertphillips9b14f262014-06-04 05:40:44 -07001275 canvas.drawPicture(picture);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001276}
1277#endif
1278
reed@google.com672588b2014-01-08 15:42:01 +00001279static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001280 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001281}
1282
1283static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001284 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001285 SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height());
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001286 canvas->drawBitmap(bitmap, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001287 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1288
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001289 SkDynamicMemoryWStream wStream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001290 picture->serialize(&wStream, &encode_bitmap_to_data);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001291 return wStream.copyToData();
1292}
1293
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001294struct ErrorContext {
1295 int fErrors;
1296 skiatest::Reporter* fReporter;
1297};
1298
1299static void assert_one_parse_error_cb(SkError error, void* context) {
1300 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
1301 errorContext->fErrors++;
1302 // This test only expects one error, and that is a kParseError. If there are others,
1303 // there is some unknown problem.
1304 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors,
1305 "This threw more errors than expected.");
1306 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error,
1307 SkGetLastErrorString());
1308}
1309
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001310static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
1311 // Create a bitmap that will be encoded.
1312 SkBitmap original;
1313 make_bm(&original, 100, 100, SK_ColorBLUE, true);
1314 SkDynamicMemoryWStream wStream;
1315 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
1316 return;
1317 }
1318 SkAutoDataUnref data(wStream.copyToData());
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001319
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001320 SkBitmap bm;
halcanary@google.com3d50ea12014-01-02 13:15:13 +00001321 bool installSuccess = SkInstallDiscardablePixelRef(
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +00001322 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm);
reed@google.combf790232013-12-13 19:45:58 +00001323 REPORTER_ASSERT(reporter, installSuccess);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001324
1325 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
1326 // Flattening original will follow the old path of performing an encode, while flattening bm
1327 // will use the already encoded data.
1328 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
1329 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
1330 REPORTER_ASSERT(reporter, picture1->equals(picture2));
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001331 // Now test that a parse error was generated when trying to create a new SkPicture without
1332 // providing a function to decode the bitmap.
1333 ErrorContext context;
1334 context.fErrors = 0;
1335 context.fReporter = reporter;
1336 SkSetErrorCallback(assert_one_parse_error_cb, &context);
1337 SkMemoryStream pictureStream(picture1);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001338 SkClearLastError();
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001339 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL));
1340 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001341 SkClearLastError();
1342 SkSetErrorCallback(NULL, NULL);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001343}
1344
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001345static void test_draw_empty(skiatest::Reporter* reporter) {
1346 SkBitmap result;
1347 make_bm(&result, 2, 2, SK_ColorBLACK, false);
1348
1349 SkCanvas canvas(result);
1350
1351 {
1352 // stock SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001353 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001354 recorder.beginRecording(1, 1);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001355 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001356
robertphillips9b14f262014-06-04 05:40:44 -07001357 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001358 }
1359
1360 {
1361 // tile grid
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001362 SkTileGridFactory::TileGridInfo gridInfo;
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001363 gridInfo.fMargin.setEmpty();
1364 gridInfo.fOffset.setZero();
1365 gridInfo.fTileInterval.set(1, 1);
1366
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001367 SkTileGridFactory factory(gridInfo);
1368 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001369 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001370 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001371
robertphillips9b14f262014-06-04 05:40:44 -07001372 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001373 }
1374
1375 {
1376 // RTree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001377 SkRTreeFactory factory;
1378 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001379 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001380 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001381
robertphillips9b14f262014-06-04 05:40:44 -07001382 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001383 }
1384
1385 {
1386 // quad tree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001387 SkQuadTreeFactory factory;
1388 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001389 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001390 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001391
robertphillips9b14f262014-06-04 05:40:44 -07001392 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001393 }
1394}
1395
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001396static void test_clip_bound_opt(skiatest::Reporter* reporter) {
1397 // Test for crbug.com/229011
1398 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
1399 SkIntToScalar(2), SkIntToScalar(2));
1400 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
1401 SkIntToScalar(1), SkIntToScalar(1));
1402 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
1403 SkIntToScalar(1), SkIntToScalar(1));
1404
1405 SkPath invPath;
1406 invPath.addOval(rect1);
1407 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
1408 SkPath path;
1409 path.addOval(rect2);
1410 SkPath path2;
1411 path2.addOval(rect3);
1412 SkIRect clipBounds;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001413 SkPictureRecorder recorder;
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001414 // Minimalist test set for 100% code coverage of
1415 // SkPictureRecord::updateClipConservativelyUsingBounds
1416 {
robertphillips9f1c2412014-06-09 06:25:34 -07001417 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001418 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1419 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1420 REPORTER_ASSERT(reporter, true == nonEmpty);
1421 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1422 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1423 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1424 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1425 }
1426 {
robertphillips9f1c2412014-06-09 06:25:34 -07001427 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001428 canvas->clipPath(path, SkRegion::kIntersect_Op);
1429 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1430 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1431 REPORTER_ASSERT(reporter, true == nonEmpty);
1432 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1433 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1434 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1435 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1436 }
1437 {
robertphillips9f1c2412014-06-09 06:25:34 -07001438 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001439 canvas->clipPath(path, SkRegion::kIntersect_Op);
1440 canvas->clipPath(invPath, SkRegion::kUnion_Op);
1441 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1442 REPORTER_ASSERT(reporter, true == nonEmpty);
1443 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1444 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1445 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1446 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1447 }
1448 {
robertphillips9f1c2412014-06-09 06:25:34 -07001449 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001450 canvas->clipPath(path, SkRegion::kDifference_Op);
1451 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1452 REPORTER_ASSERT(reporter, true == nonEmpty);
1453 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1454 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1455 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1456 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1457 }
1458 {
robertphillips9f1c2412014-06-09 06:25:34 -07001459 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001460 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
1461 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1462 // True clip is actually empty in this case, but the best
1463 // determination we can make using only bounds as input is that the
1464 // clip is included in the bounds of 'path'.
1465 REPORTER_ASSERT(reporter, true == nonEmpty);
1466 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1467 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1468 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1469 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1470 }
1471 {
robertphillips9f1c2412014-06-09 06:25:34 -07001472 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001473 canvas->clipPath(path, SkRegion::kIntersect_Op);
1474 canvas->clipPath(path2, SkRegion::kXOR_Op);
1475 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1476 REPORTER_ASSERT(reporter, true == nonEmpty);
1477 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
1478 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
1479 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1480 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1481 }
1482}
1483
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001484/**
1485 * A canvas that records the number of clip commands.
1486 */
1487class ClipCountingCanvas : public SkCanvas {
1488public:
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001489 ClipCountingCanvas(int width, int height)
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001490 : INHERITED(width, height)
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001491 , fClipCount(0){
1492 }
1493
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001494 virtual void onClipRect(const SkRect& r,
1495 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001496 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001497 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001498 this->INHERITED::onClipRect(r, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001499 }
1500
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001501 virtual void onClipRRect(const SkRRect& rrect,
1502 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001503 ClipEdgeStyle edgeStyle)SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001504 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001505 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001506 }
1507
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001508 virtual void onClipPath(const SkPath& path,
1509 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001510 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001511 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001512 this->INHERITED::onClipPath(path, op, edgeStyle);
1513 }
1514
1515 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE {
1516 fClipCount += 1;
1517 this->INHERITED::onClipRegion(deviceRgn, op);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001518 }
1519
1520 unsigned getClipCount() const { return fClipCount; }
1521
1522private:
1523 unsigned fClipCount;
1524
1525 typedef SkCanvas INHERITED;
1526};
1527
1528static void test_clip_expansion(skiatest::Reporter* reporter) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001529 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001530 SkCanvas* canvas = recorder.beginRecording(10, 10);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001531
1532 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
1533 // The following expanding clip should not be skipped.
1534 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
1535 // Draw something so the optimizer doesn't just fold the world.
1536 SkPaint p;
1537 p.setColor(SK_ColorBLUE);
1538 canvas->drawPaint(p);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001539 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001540
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001541 ClipCountingCanvas testCanvas(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001542 picture->draw(&testCanvas);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001543
1544 // Both clips should be present on playback.
1545 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
1546}
1547
tomhudson@google.com381010e2013-10-24 11:12:47 +00001548static void test_hierarchical(skiatest::Reporter* reporter) {
1549 SkBitmap bm;
1550 make_bm(&bm, 10, 10, SK_ColorRED, true);
1551
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001552 SkPictureRecorder recorder;
tomhudson@google.com381010e2013-10-24 11:12:47 +00001553
robertphillips9f1c2412014-06-09 06:25:34 -07001554 recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001555 SkAutoTUnref<SkPicture> childPlain(recorder.endRecording());
1556 REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0
tomhudson@google.com381010e2013-10-24 11:12:47 +00001557
robertphillips9f1c2412014-06-09 06:25:34 -07001558 recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001559 SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording());
1560 REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1
tomhudson@google.com381010e2013-10-24 11:12:47 +00001561
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001562 {
robertphillips9f1c2412014-06-09 06:25:34 -07001563 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips9b14f262014-06-04 05:40:44 -07001564 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001565 SkAutoTUnref<SkPicture> parentPP(recorder.endRecording());
1566 REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0
1567 }
1568 {
robertphillips9f1c2412014-06-09 06:25:34 -07001569 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips9b14f262014-06-04 05:40:44 -07001570 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001571 SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording());
1572 REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1
1573 }
1574 {
robertphillips9f1c2412014-06-09 06:25:34 -07001575 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001576 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001577 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001578 SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording());
1579 REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1
1580 }
1581 {
robertphillips9f1c2412014-06-09 06:25:34 -07001582 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001583 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001584 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001585 SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording());
1586 REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2
1587 }
tomhudson@google.com381010e2013-10-24 11:12:47 +00001588}
1589
robertphillips@google.comd5500882014-04-02 23:51:13 +00001590static void test_gen_id(skiatest::Reporter* reporter) {
1591
Robert Phillipscfaeec42014-07-13 12:00:50 -04001592 SkPictureRecorder recorder;
1593 recorder.beginRecording(0, 0);
1594 SkAutoTUnref<SkPicture> empty(recorder.endRecording());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001595
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001596 // Empty pictures should still have a valid ID
Robert Phillipscfaeec42014-07-13 12:00:50 -04001597 REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001598
robertphillips9f1c2412014-06-09 06:25:34 -07001599 SkCanvas* canvas = recorder.beginRecording(1, 1);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001600 canvas->drawARGB(255, 255, 255, 255);
1601 SkAutoTUnref<SkPicture> hasData(recorder.endRecording());
1602 // picture should have a non-zero id after recording
1603 REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001604
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001605 // both pictures should have different ids
Robert Phillipscfaeec42014-07-13 12:00:50 -04001606 REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001607}
1608
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00001609DEF_TEST(Picture, reporter) {
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001610#ifdef SK_DEBUG
robertphillipsdb539902014-07-01 08:47:04 -07001611 test_deleting_empty_picture();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001612 test_serializing_empty_picture();
scroggo@google.com4b90b112012-12-04 15:08:56 +00001613#else
1614 test_bad_bitmap();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001615#endif
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001616 test_unbalanced_save_restores(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001617 test_peephole();
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001618#if SK_SUPPORT_GPU
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +00001619 test_gpu_veto(reporter);
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001620#endif
ajuma750ae262014-08-18 12:59:55 -07001621 test_has_text(reporter);
reed@google.comfe7b1ed2012-11-29 21:00:39 +00001622 test_gatherpixelrefs(reporter);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +00001623 test_gatherpixelrefsandrects(reporter);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001624 test_bitmap_with_encoded_data(reporter);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001625 test_draw_empty(reporter);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001626 test_clip_bound_opt(reporter);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001627 test_clip_expansion(reporter);
tomhudson@google.com381010e2013-10-24 11:12:47 +00001628 test_hierarchical(reporter);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001629 test_gen_id(reporter);
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001630}
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001631
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001632#if SK_SUPPORT_GPU
1633DEF_GPUTEST(GPUPicture, reporter, factory) {
1634 test_gpu_picture_optimization(reporter, factory);
1635}
1636#endif
1637
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001638static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) {
1639 const SkPaint paint;
1640 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f };
1641 const SkIRect irect = { 2, 2, 3, 3 };
1642
1643 // Don't care what these record, as long as they're legal.
1644 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint);
1645 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag);
1646 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint);
1647 canvas->drawBitmapNine(bitmap, irect, rect, &paint);
1648 canvas->drawSprite(bitmap, 1, 1);
1649}
1650
1651static void test_draw_bitmaps(SkCanvas* canvas) {
1652 SkBitmap empty;
1653 draw_bitmaps(empty, canvas);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001654 empty.setInfo(SkImageInfo::MakeN32Premul(10, 10));
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001655 draw_bitmaps(empty, canvas);
1656}
1657
1658DEF_TEST(Picture_EmptyBitmap, r) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001659 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001660 test_draw_bitmaps(recorder.beginRecording(10, 10));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001661 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001662}
1663
1664DEF_TEST(Canvas_EmptyBitmap, r) {
1665 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001666 dst.allocN32Pixels(10, 10);
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001667 SkCanvas canvas(dst);
1668
1669 test_draw_bitmaps(&canvas);
1670}
dneto3f22e8c2014-07-30 15:42:22 -07001671
1672DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) {
1673 // This test is from crbug.com/344987.
1674 // The commands are:
1675 // saveLayer with paint that modifies alpha
1676 // drawBitmapRectToRect
1677 // drawBitmapRectToRect
1678 // restore
1679 // The bug was that this structure was modified so that:
1680 // - The saveLayer and restore were eliminated
1681 // - The alpha was only applied to the first drawBitmapRectToRect
1682
1683 // This test draws blue and red squares inside a 50% transparent
1684 // layer. Both colours should show up muted.
1685 // When the bug is present, the red square (the second bitmap)
1686 // shows upwith full opacity.
1687
1688 SkBitmap blueBM;
1689 make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true);
1690 SkBitmap redBM;
1691 make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true);
1692 SkPaint semiTransparent;
1693 semiTransparent.setAlpha(0x80);
1694
1695 SkPictureRecorder recorder;
1696 SkCanvas* canvas = recorder.beginRecording(100, 100);
1697 canvas->drawARGB(0, 0, 0, 0);
1698
1699 canvas->saveLayer(0, &semiTransparent);
1700 canvas->drawBitmap(blueBM, 25, 25);
1701 canvas->drawBitmap(redBM, 50, 50);
1702 canvas->restore();
1703
1704 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1705
1706 // Now replay the picture back on another canvas
1707 // and check a couple of its pixels.
1708 SkBitmap replayBM;
1709 make_bm(&replayBM, 100, 100, SK_ColorBLACK, false);
1710 SkCanvas replayCanvas(replayBM);
1711 picture->draw(&replayCanvas);
1712 replayCanvas.flush();
1713
1714 // With the bug present, at (55, 55) we would get a fully opaque red
1715 // intead of a dark red.
1716 REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080);
1717 REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000);
1718}