blob: 6f62d185385a19a8074be24d2d660f1e0689e6d8 [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"
reed@google.com72aa79c2013-01-24 18:27:42 +000022#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000023#include "SkRandom.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000024#include "SkShader.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000025#include "SkStream.h"
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000026
27#if SK_SUPPORT_GPU
28#include "SkSurface.h"
29#include "GrContextFactory.h"
30#include "GrPictureUtils.h"
31#endif
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000032#include "Test.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000033
reed@google.com47b679b2014-05-14 18:58:16 +000034#include "SkLumaColorFilter.h"
35#include "SkColorFilterImageFilter.h"
36
robertphillips@google.comed9866c2014-01-09 19:20:45 +000037static const int gColorScale = 30;
38static const int gColorOffset = 60;
reed@google.comfe7b1ed2012-11-29 21:00:39 +000039
40static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000041 bm->allocN32Pixels(w, h);
reed@google.comfe7b1ed2012-11-29 21:00:39 +000042 bm->eraseColor(color);
43 if (immutable) {
44 bm->setImmutable();
45 }
46}
47
robertphillips@google.comfe5824a2014-01-09 19:45:29 +000048static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000049 SkASSERT(w % 2 == 0);
50 SkASSERT(h % 2 == 0);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000051 bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType,
52 kPremul_SkAlphaType));
robertphillips@google.comed9866c2014-01-09 19:20:45 +000053 SkAutoLockPixels lock(*bm);
54 for (int y = 0; y < h; y += 2) {
55 uint8_t* s = bm->getAddr8(0, y);
56 for (int x = 0; x < w; x += 2) {
57 *s++ = 0xFF;
58 *s++ = 0x00;
59 }
60 s = bm->getAddr8(0, y + 1);
61 for (int x = 0; x < w; x += 2) {
62 *s++ = 0x00;
63 *s++ = 0xFF;
64 }
65 }
66 if (immutable) {
67 bm->setImmutable();
68 }
69}
reed@google.comfe7b1ed2012-11-29 21:00:39 +000070
robertphillips@google.comed9866c2014-01-09 19:20:45 +000071static void init_paint(SkPaint* paint, const SkBitmap &bm) {
72 SkShader* shader = SkShader::CreateBitmapShader(bm,
73 SkShader::kClamp_TileMode,
74 SkShader::kClamp_TileMode);
75 paint->setShader(shader)->unref();
76}
77
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +000078typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000079 const SkBitmap&, const SkPoint&,
80 SkTDArray<SkPixelRef*>* usedPixRefs);
robertphillips@google.comed9866c2014-01-09 19:20:45 +000081
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000082static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000083 const SkBitmap& altBM, const SkPoint& pos,
84 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000085 SkPaint paint;
86 init_paint(&paint, bm);
87
88 canvas->drawPaint(paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000089 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +000090}
91
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000092static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000093 const SkBitmap& altBM, const SkPoint& pos,
94 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000095 SkPaint paint;
96 init_paint(&paint, bm);
97
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000098 // draw a rect
robertphillips@google.comed9866c2014-01-09 19:20:45 +000099 SkPoint points[5] = {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000100 { pos.fX, pos.fY },
101 { pos.fX + bm.width() - 1, pos.fY },
102 { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 },
103 { pos.fX, pos.fY + bm.height() - 1 },
104 { pos.fX, pos.fY },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000105 };
106
107 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000108 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000109}
110
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000111static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000112 const SkBitmap& altBM, const SkPoint& pos,
113 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000114 SkPaint paint;
115 init_paint(&paint, bm);
116
117 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
118 r.offset(pos.fX, pos.fY);
119
120 canvas->drawRect(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000121 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000122}
123
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000124static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000125 const SkBitmap& altBM, const SkPoint& pos,
126 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000127 SkPaint paint;
128 init_paint(&paint, bm);
129
130 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
131 r.offset(pos.fX, pos.fY);
132
133 canvas->drawOval(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000134 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000135}
136
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000137static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000138 const SkBitmap& altBM, const SkPoint& pos,
139 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000140 SkPaint paint;
141 init_paint(&paint, bm);
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000142
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000143 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
144 r.offset(pos.fX, pos.fY);
145
146 SkRRect rr;
147 rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4);
148 canvas->drawRRect(rr, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000149 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000150}
151
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000152static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000153 const SkBitmap& altBM, const SkPoint& pos,
154 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000155 SkPaint paint;
156 init_paint(&paint, bm);
157
158 SkPath path;
159 path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height()));
160 path.lineTo(SkIntToScalar(bm.width()), 0);
161 path.close();
162 path.offset(pos.fX, pos.fY);
163
164 canvas->drawPath(path, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000165 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000166}
167
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000168static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000169 const SkBitmap& altBM, const SkPoint& pos,
170 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000171 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000172 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000173}
174
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000175static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000176 const SkBitmap& altBM, const SkPoint& pos,
177 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000178 SkPaint paint;
179 init_paint(&paint, bm);
180
181 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
182 canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000183 *usedPixRefs->append() = bm.pixelRef();
184 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000185}
186
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000187static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000188 const SkBitmap& altBM, const SkPoint& pos,
189 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000190 const SkMatrix& ctm = canvas->getTotalMatrix();
191
192 SkPoint p(pos);
193 ctm.mapPoints(&p, 1);
194
195 canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000196 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000197}
198
199#if 0
200// Although specifiable, this case doesn't seem to make sense (i.e., the
201// bitmap in the shader is never used).
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000202static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000203 const SkBitmap& altBM, const SkPoint& pos,
204 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000205 SkPaint paint;
206 init_paint(&paint, bm);
207
208 const SkMatrix& ctm = canvas->getTotalMatrix();
209
210 SkPoint p(pos);
211 ctm.mapPoints(&p, 1);
212
213 canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000214 *usedPixRefs->append() = bm.pixelRef();
215 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000216}
217#endif
218
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000219static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000220 const SkBitmap& altBM, const SkPoint& pos,
221 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000222 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
223
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000224 r.offset(pos.fX, pos.fY);
225 canvas->drawBitmapRectToRect(bm, NULL, r, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000226 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000227}
228
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000229static void drawbitmaprect_withshader_proc(SkCanvas* canvas,
230 const SkBitmap& bm,
231 const SkBitmap& altBM,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000232 const SkPoint& pos,
233 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000234 SkPaint paint;
235 init_paint(&paint, bm);
236
237 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000238 r.offset(pos.fX, pos.fY);
239
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000240 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
241 canvas->drawBitmapRectToRect(altBM, NULL, r, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000242 *usedPixRefs->append() = bm.pixelRef();
243 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000244}
245
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000246static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000247 const SkBitmap& altBM, const SkPoint& pos,
248 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000249 SkPaint paint;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000250 init_paint(&paint, bm);
251 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
252
253 canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000254 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000255}
256
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000257static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000258 const SkBitmap& altBM, const SkPoint& pos,
259 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000260 SkPaint paint;
261 init_paint(&paint, bm);
262 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
263
264 SkPoint point = { pos.fX, pos.fY + bm.height() };
265 canvas->drawPosText("O", 1, &point, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000266 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000267}
268
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000269static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000270 const SkBitmap& altBM, const SkPoint& pos,
271 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000272 SkPaint paint;
273
274 init_paint(&paint, bm);
275 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
276
277 SkPath path;
278 path.lineTo(SkIntToScalar(bm.width()), 0);
279 path.offset(pos.fX, pos.fY+bm.height());
280
281 canvas->drawTextOnPath("O", 1, path, NULL, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000282 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000283}
284
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000285static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000286 const SkBitmap& altBM, const SkPoint& pos,
287 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000288 SkPaint paint;
289 init_paint(&paint, bm);
290
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000291 SkPoint verts[4] = {
292 { pos.fX, pos.fY },
293 { pos.fX + bm.width(), pos.fY },
294 { pos.fX + bm.width(), pos.fY + bm.height() },
295 { pos.fX, pos.fY + bm.height() }
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000296 };
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000297 SkPoint texs[4] = { { 0, 0 },
298 { SkIntToScalar(bm.width()), 0 },
299 { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000300 { 0, SkIntToScalar(bm.height()) } };
301 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 };
302
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000303 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL,
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000304 indices, 6, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000305 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000306}
307
308// Return a picture with the bitmaps drawn at the specified positions.
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000309static SkPicture* record_bitmaps(const SkBitmap bm[],
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000310 const SkPoint pos[],
311 SkTDArray<SkPixelRef*> analytic[],
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000312 int count,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000313 DrawBitmapProc proc) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000314 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -0700315 SkCanvas* canvas = recorder.beginRecording(1000, 1000);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000316 for (int i = 0; i < count; ++i) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000317 analytic[i].rewind();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000318 canvas->save();
319 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY,
320 SkIntToScalar(bm[i].width()),
321 SkIntToScalar(bm[i].height()));
322 canvas->clipRect(clipRect, SkRegion::kIntersect_Op);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000323 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000324 canvas->restore();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000325 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000326 return recorder.endRecording();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000327}
328
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000329static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000330 rect->fLeft = rand.nextRangeScalar(-W, 2*W);
331 rect->fTop = rand.nextRangeScalar(-H, 2*H);
332 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W);
333 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H);
334
335 // we integralize rect to make our tests more predictable, since Gather is
336 // a little sloppy.
337 SkIRect ir;
338 rect->round(&ir);
339 rect->set(ir);
340}
341
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000342static void draw(SkPicture* pic, int width, int height, SkBitmap* result) {
343 make_bm(result, width, height, SK_ColorBLACK, false);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000344
345 SkCanvas canvas(*result);
robertphillips9b14f262014-06-04 05:40:44 -0700346 canvas.drawPicture(pic);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000347}
348
349template <typename T> int find_index(const T* array, T elem, int count) {
350 for (int i = 0; i < count; ++i) {
351 if (array[i] == elem) {
352 return i;
353 }
354 }
355 return -1;
356}
357
358// Return true if 'ref' is found in array[]
359static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) {
360 return find_index<const SkPixelRef*>(array, ref, count) >= 0;
361}
362
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000363// Look at each pixel that is inside 'subset', and if its color appears in
364// colors[], find the corresponding value in refs[] and append that ref into
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000365// array, skipping duplicates of the same value.
366// Note that gathering pixelRefs from rendered colors suffers from the problem
367// that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color)
368// isn't easy to reconstruct.
369static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[],
370 int count, SkTDArray<SkPixelRef*>* array,
371 const SkRect& subset) {
372 SkIRect ir;
373 subset.roundOut(&ir);
374
375 if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) {
376 return;
377 }
378
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000379 // Since we only want to return unique values in array, when we scan we just
380 // set a bit for each index'd color found. In practice we only have a few
381 // distinct colors, so we just use an int's bits as our array. Hence the
382 // assert that count <= number-of-bits-in-our-int.
383 SkASSERT((unsigned)count <= 32);
384 uint32_t bitarray = 0;
385
386 SkAutoLockPixels alp(bm);
387
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000388 for (int y = ir.fTop; y < ir.fBottom; ++y) {
389 for (int x = ir.fLeft; x < ir.fRight; ++x) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000390 SkPMColor pmc = *bm.getAddr32(x, y);
391 // the only good case where the color is not found would be if
392 // the color is transparent, meaning no bitmap was drawn in that
393 // pixel.
394 if (pmc) {
bsalomon@google.comc3d753e2013-01-08 17:24:44 +0000395 uint32_t index = SkGetPackedR32(pmc);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000396 SkASSERT(SkGetPackedG32(pmc) == index);
397 SkASSERT(SkGetPackedB32(pmc) == index);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000398 if (0 == index) {
399 continue; // background color
400 }
401 SkASSERT(0 == (index - gColorOffset) % gColorScale);
402 index = (index - gColorOffset) / gColorScale;
bsalomon@google.com5f429b02013-01-08 18:42:20 +0000403 SkASSERT(static_cast<int>(index) < count);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000404 bitarray |= 1 << index;
405 }
406 }
407 }
408
409 for (int i = 0; i < count; ++i) {
410 if (bitarray & (1 << i)) {
411 *array->append() = refs[i];
412 }
413 }
414}
415
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000416static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h,
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000417 const SkTDArray<SkPixelRef*> analytic[],
418 int count,
419 SkTDArray<SkPixelRef*>* result,
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000420 const SkRect& subset) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000421 for (int i = 0; i < count; ++i) {
422 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h);
423
424 if (SkRect::Intersects(subset, rect)) {
425 result->append(analytic[i].count(), analytic[i].begin());
426 }
427 }
428}
429
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000430
431static const struct {
432 const DrawBitmapProc proc;
433 const char* const desc;
434} gProcs[] = {
435 {drawpaint_proc, "drawpaint"},
436 {drawpoints_proc, "drawpoints"},
437 {drawrect_proc, "drawrect"},
438 {drawoval_proc, "drawoval"},
439 {drawrrect_proc, "drawrrect"},
440 {drawpath_proc, "drawpath"},
441 {drawbitmap_proc, "drawbitmap"},
442 {drawbitmap_withshader_proc, "drawbitmap_withshader"},
443 {drawsprite_proc, "drawsprite"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000444#if 0
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000445 {drawsprite_withshader_proc, "drawsprite_withshader"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000446#endif
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000447 {drawbitmaprect_proc, "drawbitmaprect"},
448 {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"},
449 {drawtext_proc, "drawtext"},
450 {drawpostext_proc, "drawpostext"},
451 {drawtextonpath_proc, "drawtextonpath"},
452 {drawverts_proc, "drawverts"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000453};
454
455static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000456 // Our convention is that the color components contain an encoding of
457 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000458 // reserved for the background
459 for (int i = 0; i < num; ++i) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000460 make_bm(&bm[i], w, h,
461 SkColorSetARGB(0xFF,
462 gColorScale*i+gColorOffset,
463 gColorScale*i+gColorOffset,
464 gColorScale*i+gColorOffset),
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000465 true);
466 refs[i] = bm[i].pixelRef();
467 }
468
469 // The A8 alternate bitmaps are all BW checkerboards
470 for (int i = 0; i < num; ++i) {
471 make_checkerboard(&bm[num+i], w, h, true);
472 refs[num+i] = bm[num+i].pixelRef();
473 }
474}
475
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000476static void test_gatherpixelrefs(skiatest::Reporter* reporter) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000477 const int IW = 32;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000478 const int IH = IW;
479 const SkScalar W = SkIntToScalar(IW);
480 const SkScalar H = W;
481
482 static const int N = 4;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000483 SkBitmap bm[2*N];
484 SkPixelRef* refs[2*N];
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000485 SkTDArray<SkPixelRef*> analytic[N];
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000486
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000487 const SkPoint pos[N] = {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000488 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
489 };
490
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000491 create_textures(bm, refs, N, IW, IH);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000492
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000493 SkRandom rand;
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000494 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000495 SkAutoTUnref<SkPicture> pic(
496 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000497
tomhudson@google.com381010e2013-10-24 11:12:47 +0000498 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000499 // quick check for a small piece of each quadrant, which should just
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000500 // contain 1 or 2 bitmaps.
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000501 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
502 SkRect r;
503 r.set(2, 2, W - 2, H - 2);
504 r.offset(pos[i].fX, pos[i].fY);
505 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r));
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000506 if (!data) {
507 ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned "
508 "NULL for %s.", gProcs[k].desc);
509 continue;
510 }
511 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data();
512 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*));
513 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
514 if (1 == count) {
515 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
516 } else if (2 == count) {
517 REPORTER_ASSERT(reporter,
518 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
519 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
commit-bot@chromium.orgfe433c12013-07-09 16:04:32 +0000520 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000521 }
522
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000523 SkBitmap image;
524 draw(pic, 2*IW, 2*IH, &image);
525
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000526 // Test a bunch of random (mostly) rects, and compare the gather results
527 // with a deduced list of refs by looking at the colors drawn.
528 for (int j = 0; j < 100; ++j) {
529 SkRect r;
530 rand_rect(&r, rand, 2*W, 2*H);
531
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000532 SkTDArray<SkPixelRef*> fromImage;
533 gather_from_image(image, refs, N, &fromImage, r);
534
535 SkTDArray<SkPixelRef*> fromAnalytic;
536 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000537
538 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
539 size_t dataSize = data ? data->size() : 0;
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000540 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000541 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize);
542 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL;
543 SkAutoDataUnref adu(data);
544
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000545 // Everything that we saw drawn should appear in the analytic list
546 // but the analytic list may contain some pixelRefs that were not
547 // seen in the image (e.g., A8 textures used as masks)
548 for (int i = 0; i < fromImage.count(); ++i) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000549 if (-1 == fromAnalytic.find(fromImage[i])) {
550 ERRORF(reporter, "PixelRef missing %d %s",
551 i, gProcs[k].desc);
552 }
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000553 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000554
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000555 /*
556 * GatherPixelRefs is conservative, so it can return more bitmaps
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000557 * than are strictly required. Thus our check here is only that
558 * Gather didn't miss any that we actually needed. Even that isn't
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000559 * a strict requirement on Gather, which is meant to be quick and
560 * only mostly-correct, but at the moment this test should work.
561 */
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000562 for (int i = 0; i < fromAnalytic.count(); ++i) {
563 bool found = find(gatherRefs, fromAnalytic[i], gatherCount);
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000564 if (!found) {
565 ERRORF(reporter, "PixelRef missing %d %s",
566 i, gProcs[k].desc);
567 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000568#if 0
569 // enable this block of code to debug failures, as it will rerun
570 // the case that failed.
571 if (!found) {
572 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
573 size_t dataSize = data ? data->size() : 0;
574 }
575#endif
576 }
577 }
578 }
579}
580
tomhudson60c2a792014-08-18 15:07:13 -0700581#define GENERATE_CANVAS(recorder, x) \
582 (x) ? recorder.EXPERIMENTAL_beginRecording(100, 100) \
583 : recorder.beginRecording(100,100);
584
585/* Hit a few SkPicture::Analysis cases not handled elsewhere. */
586static void test_analysis(skiatest::Reporter* reporter, bool useNewPath) {
587 SkPictureRecorder recorder;
588
589 SkCanvas* canvas = GENERATE_CANVAS(recorder, useNewPath);
590 {
591 canvas->drawRect(SkRect::MakeWH(10, 10), SkPaint ());
592 }
593 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
594 REPORTER_ASSERT(reporter, !picture->willPlayBackBitmaps());
595
596 canvas = GENERATE_CANVAS(recorder, useNewPath);
597 {
598 SkPaint paint;
599 // CreateBitmapShader is too smart for us; an empty (or 1x1) bitmap shader
600 // gets optimized into a non-bitmap form, so we create a 2x2 bitmap here.
601 SkBitmap bitmap;
602 bitmap.allocPixels(SkImageInfo::MakeN32Premul(2, 2));
603 bitmap.eraseColor(SK_ColorBLUE);
604 *(bitmap.getAddr32(0, 0)) = SK_ColorGREEN;
605 SkShader* shader = SkShader::CreateBitmapShader(bitmap, SkShader::kClamp_TileMode,
606 SkShader::kClamp_TileMode);
607 paint.setShader(shader)->unref();
608 REPORTER_ASSERT(reporter,
609 shader->asABitmap(NULL, NULL, NULL) == SkShader::kDefault_BitmapType);
610
611 canvas->drawRect(SkRect::MakeWH(10, 10), paint);
612 }
613 picture.reset(recorder.endRecording());
614 REPORTER_ASSERT(reporter, picture->willPlayBackBitmaps());
615}
616
617#undef GENERATE_CANVAS
618
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000619static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) {
620 const int IW = 32;
621 const int IH = IW;
622 const SkScalar W = SkIntToScalar(IW);
623 const SkScalar H = W;
624
625 static const int N = 4;
626 SkBitmap bm[2*N];
627 SkPixelRef* refs[2*N];
628 SkTDArray<SkPixelRef*> analytic[N];
629
630 const SkPoint pos[N] = {
631 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
632 };
633
634 create_textures(bm, refs, N, IW, IH);
635
636 SkRandom rand;
637 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000638 SkAutoTUnref<SkPicture> pic(
639 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000640
641 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
642
643 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont(
644 new SkPictureUtils::SkPixelRefsAndRectsList);
645
646 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont);
647
648 // quick check for a small piece of each quadrant, which should just
649 // contain 1 or 2 bitmaps.
650 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
651 SkRect r;
652 r.set(2, 2, W - 2, H - 2);
653 r.offset(pos[i].fX, pos[i].fY);
654
655 SkTDArray<SkPixelRef*> gatheredRefs;
656 prCont->query(r, &gatheredRefs);
657
658 int count = gatheredRefs.count();
659 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
660 if (1 == count) {
661 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
662 } else if (2 == count) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000663 REPORTER_ASSERT(reporter,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000664 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
665 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
666 }
667 }
668
669 SkBitmap image;
670 draw(pic, 2*IW, 2*IH, &image);
671
672 // Test a bunch of random (mostly) rects, and compare the gather results
673 // with the analytic results and the pixel refs seen in a rendering.
674 for (int j = 0; j < 100; ++j) {
675 SkRect r;
676 rand_rect(&r, rand, 2*W, 2*H);
677
678 SkTDArray<SkPixelRef*> fromImage;
679 gather_from_image(image, refs, N, &fromImage, r);
680
681 SkTDArray<SkPixelRef*> fromAnalytic;
682 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
683
684 SkTDArray<SkPixelRef*> gatheredRefs;
685 prCont->query(r, &gatheredRefs);
686
687 // Everything that we saw drawn should appear in the analytic list
688 // but the analytic list may contain some pixelRefs that were not
689 // seen in the image (e.g., A8 textures used as masks)
690 for (int i = 0; i < fromImage.count(); ++i) {
691 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]));
692 }
693
694 // Everything in the analytic list should appear in the gathered
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000695 // list.
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000696 for (int i = 0; i < fromAnalytic.count(); ++i) {
697 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i]));
698 }
699 }
700 }
701}
702
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000703#ifdef SK_DEBUG
mtklein3e8232b2014-08-18 13:39:11 -0700704// Ensure that deleting an empty SkPicture does not assert. Asserts only fire
robertphillipsdb539902014-07-01 08:47:04 -0700705// in debug mode, so only run in debug mode.
706static void test_deleting_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000707 SkPictureRecorder recorder;
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000708 // Creates an SkPictureRecord
robertphillips9f1c2412014-06-09 06:25:34 -0700709 recorder.beginRecording(0, 0);
robertphillipsdb539902014-07-01 08:47:04 -0700710 // Turns that into an SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000711 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
robertphillipsdb539902014-07-01 08:47:04 -0700712 // Ceates a new SkPictureRecord
robertphillips9f1c2412014-06-09 06:25:34 -0700713 recorder.beginRecording(0, 0);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000714}
715
716// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
717static void test_serializing_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000718 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -0700719 recorder.beginRecording(0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000720 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000721 SkDynamicMemoryWStream stream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000722 picture->serialize(&stream);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000723}
724#endif
725
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000726static void rand_op(SkCanvas* canvas, SkRandom& rand) {
reed@google.com21b519d2012-10-02 17:42:15 +0000727 SkPaint paint;
728 SkRect rect = SkRect::MakeWH(50, 50);
729
730 SkScalar unit = rand.nextUScalar1();
731 if (unit <= 0.3) {
732// SkDebugf("save\n");
733 canvas->save();
734 } else if (unit <= 0.6) {
735// SkDebugf("restore\n");
736 canvas->restore();
737 } else if (unit <= 0.9) {
738// SkDebugf("clip\n");
739 canvas->clipRect(rect);
740 } else {
741// SkDebugf("draw\n");
742 canvas->drawPaint(paint);
743 }
744}
745
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000746#if SK_SUPPORT_GPU
tomhudson60c2a792014-08-18 15:07:13 -0700747#define GENERATE_CANVAS(recorder, x) \
748 (x) ? recorder.EXPERIMENTAL_beginRecording(100, 100) \
749 : recorder.beginRecording(100,100);
750
751static void test_gpu_veto(skiatest::Reporter* reporter,
752 bool useNewPath) {
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000753
754 SkPictureRecorder recorder;
755
tomhudson60c2a792014-08-18 15:07:13 -0700756 SkCanvas* canvas = GENERATE_CANVAS(recorder, useNewPath);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000757 {
758 SkPath path;
759 path.moveTo(0, 0);
760 path.lineTo(50, 50);
761
762 SkScalar intervals[] = { 1.0f, 1.0f };
763 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0));
764
765 SkPaint paint;
766 paint.setStyle(SkPaint::kStroke_Style);
767 paint.setPathEffect(dash);
768
769 canvas->drawPath(path, paint);
770 }
771 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
772 // path effects currently render an SkPicture undesireable for GPU rendering
commit-bot@chromium.orga1ff26a2014-05-30 21:52:52 +0000773
774 const char *reason = NULL;
775 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL, &reason));
776 REPORTER_ASSERT(reporter, NULL != reason);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000777
tomhudson60c2a792014-08-18 15:07:13 -0700778 canvas = GENERATE_CANVAS(recorder, useNewPath);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000779 {
780 SkPath path;
781
782 path.moveTo(0, 0);
783 path.lineTo(0, 50);
784 path.lineTo(25, 25);
785 path.lineTo(50, 50);
786 path.lineTo(50, 0);
787 path.close();
788 REPORTER_ASSERT(reporter, !path.isConvex());
789
790 SkPaint paint;
791 paint.setAntiAlias(true);
792 for (int i = 0; i < 50; ++i) {
793 canvas->drawPath(path, paint);
794 }
795 }
796 picture.reset(recorder.endRecording());
797 // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering
798 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
799
tomhudson60c2a792014-08-18 15:07:13 -0700800 canvas = GENERATE_CANVAS(recorder, useNewPath);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000801 {
802 SkPath path;
803
804 path.moveTo(0, 0);
805 path.lineTo(0, 50);
806 path.lineTo(25, 25);
807 path.lineTo(50, 50);
808 path.lineTo(50, 0);
809 path.close();
810 REPORTER_ASSERT(reporter, !path.isConvex());
811
812 SkPaint paint;
813 paint.setAntiAlias(true);
814 paint.setStyle(SkPaint::kStroke_Style);
815 paint.setStrokeWidth(0);
816 for (int i = 0; i < 50; ++i) {
817 canvas->drawPath(path, paint);
818 }
819 }
820 picture.reset(recorder.endRecording());
821 // hairline stroked AA concave paths are fine for GPU rendering
822 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL));
tomhudson60c2a792014-08-18 15:07:13 -0700823
824 canvas = GENERATE_CANVAS(recorder, useNewPath);
825 {
826 SkPaint paint;
827 SkScalar intervals [] = { 10, 20 };
828 SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25);
829 paint.setPathEffect(pe)->unref();
830
831 SkPoint points [2] = { { 0, 0 }, { 100, 0 } };
832 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, points, paint);
833 }
834 picture.reset(recorder.endRecording());
835 // fast-path dashed effects are fine for GPU rendering ...
836 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL));
837
838 canvas = GENERATE_CANVAS(recorder, useNewPath);
839 {
840 SkPaint paint;
841 SkScalar intervals [] = { 10, 20 };
842 SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25);
843 paint.setPathEffect(pe)->unref();
844
845 canvas->drawRect(SkRect::MakeWH(10, 10), paint);
846 }
847 picture.reset(recorder.endRecording());
848 // ... but only when applied to drawPoint() calls
849 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000850}
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000851
tomhudson60c2a792014-08-18 15:07:13 -0700852#undef GENERATE_CANVAS
853
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000854static void test_gpu_picture_optimization(skiatest::Reporter* reporter,
855 GrContextFactory* factory) {
bsalomone904c092014-07-17 10:50:59 -0700856 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
857 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000858
bsalomone904c092014-07-17 10:50:59 -0700859 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
860 continue;
861 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000862
bsalomone904c092014-07-17 10:50:59 -0700863 GrContext* context = factory->get(glCtxType);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000864
bsalomone904c092014-07-17 10:50:59 -0700865 if (NULL == context) {
866 continue;
867 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000868
bsalomone904c092014-07-17 10:50:59 -0700869 static const int kWidth = 100;
870 static const int kHeight = 100;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000871
bsalomone904c092014-07-17 10:50:59 -0700872 SkAutoTUnref<SkPicture> pict;
873
874 // create a picture with the structure:
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000875 // 1)
bsalomone904c092014-07-17 10:50:59 -0700876 // SaveLayer
877 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000878 // 2)
bsalomone904c092014-07-17 10:50:59 -0700879 // SaveLayer
880 // Translate
881 // SaveLayer w/ bound
882 // Restore
883 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000884 // 3)
bsalomone904c092014-07-17 10:50:59 -0700885 // SaveLayer w/ copyable paint
886 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000887 // 4)
bsalomone904c092014-07-17 10:50:59 -0700888 // SaveLayer w/ non-copyable paint
889 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000890 {
bsalomone904c092014-07-17 10:50:59 -0700891 SkPictureRecorder recorder;
892
893 SkCanvas* c = recorder.beginRecording(kWidth, kHeight);
894 // 1)
895 c->saveLayer(NULL, NULL);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000896 c->restore();
bsalomone904c092014-07-17 10:50:59 -0700897
898 // 2)
899 c->saveLayer(NULL, NULL);
900 c->translate(kWidth/2, kHeight/2);
901 SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2);
902 c->saveLayer(&r, NULL);
903 c->restore();
904 c->restore();
905
906 // 3)
907 {
908 SkPaint p;
909 p.setColor(SK_ColorRED);
910 c->saveLayer(NULL, &p);
911 c->restore();
912 }
913 // 4)
914 // TODO: this case will need to be removed once the paint's are immutable
915 {
916 SkPaint p;
917 SkAutoTUnref<SkColorFilter> cf(SkLumaColorFilter::Create());
918 p.setImageFilter(SkColorFilterImageFilter::Create(cf.get()))->unref();
919 c->saveLayer(NULL, &p);
920 c->restore();
921 }
922
923 pict.reset(recorder.endRecording());
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000924 }
925
bsalomone904c092014-07-17 10:50:59 -0700926 // Now test out the SaveLayer extraction
927 {
928 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000929
bsalomone904c092014-07-17 10:50:59 -0700930 SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info));
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000931
bsalomone904c092014-07-17 10:50:59 -0700932 SkCanvas* canvas = surface->getCanvas();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000933
bsalomone904c092014-07-17 10:50:59 -0700934 canvas->EXPERIMENTAL_optimize(pict);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000935
robertphillips6617d502014-08-18 09:39:34 -0700936 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000937
bsalomone904c092014-07-17 10:50:59 -0700938 const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key);
939 REPORTER_ASSERT(reporter, NULL != data);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000940
robertphillips6617d502014-08-18 09:39:34 -0700941 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data);
bsalomone904c092014-07-17 10:50:59 -0700942 REPORTER_ASSERT(reporter, 5 == gpuData->numSaveLayers());
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000943
robertphillips6617d502014-08-18 09:39:34 -0700944 const GrAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0);
bsalomone904c092014-07-17 10:50:59 -0700945 // The parent/child layer appear in reverse order
robertphillips6617d502014-08-18 09:39:34 -0700946 const GrAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2);
947 const GrAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1);
948 const GrAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3);
949// const GrAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(4);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000950
bsalomone904c092014-07-17 10:50:59 -0700951 REPORTER_ASSERT(reporter, info0.fValid);
952 REPORTER_ASSERT(reporter, kWidth == info0.fSize.fWidth && kHeight == info0.fSize.fHeight);
953 REPORTER_ASSERT(reporter, info0.fCTM.isIdentity());
954 REPORTER_ASSERT(reporter, 0 == info0.fOffset.fX && 0 == info0.fOffset.fY);
955 REPORTER_ASSERT(reporter, NULL != info0.fPaint);
956 REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000957
bsalomone904c092014-07-17 10:50:59 -0700958 REPORTER_ASSERT(reporter, info1.fValid);
959 REPORTER_ASSERT(reporter, kWidth == info1.fSize.fWidth && kHeight == info1.fSize.fHeight);
960 REPORTER_ASSERT(reporter, info1.fCTM.isIdentity());
961 REPORTER_ASSERT(reporter, 0 == info1.fOffset.fX && 0 == info1.fOffset.fY);
962 REPORTER_ASSERT(reporter, NULL != info1.fPaint);
963 REPORTER_ASSERT(reporter, !info1.fIsNested && info1.fHasNestedLayers); // has a nested SL
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000964
bsalomone904c092014-07-17 10:50:59 -0700965 REPORTER_ASSERT(reporter, info2.fValid);
966 REPORTER_ASSERT(reporter, kWidth/2 == info2.fSize.fWidth &&
967 kHeight/2 == info2.fSize.fHeight); // bound reduces size
968 REPORTER_ASSERT(reporter, info2.fCTM.isIdentity()); // translated
969 REPORTER_ASSERT(reporter, kWidth/2 == info2.fOffset.fX &&
970 kHeight/2 == info2.fOffset.fY);
971 REPORTER_ASSERT(reporter, NULL != info1.fPaint);
972 REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000973
bsalomone904c092014-07-17 10:50:59 -0700974 REPORTER_ASSERT(reporter, info3.fValid);
975 REPORTER_ASSERT(reporter, kWidth == info3.fSize.fWidth && kHeight == info3.fSize.fHeight);
976 REPORTER_ASSERT(reporter, info3.fCTM.isIdentity());
977 REPORTER_ASSERT(reporter, 0 == info3.fOffset.fX && 0 == info3.fOffset.fY);
978 REPORTER_ASSERT(reporter, NULL != info3.fPaint);
979 REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000980
bsalomone904c092014-07-17 10:50:59 -0700981 #if 0 // needs more though for GrGatherCanvas
982 REPORTER_ASSERT(reporter, !info4.fValid); // paint is/was uncopyable
983 REPORTER_ASSERT(reporter, kWidth == info4.fSize.fWidth && kHeight == info4.fSize.fHeight);
984 REPORTER_ASSERT(reporter, 0 == info4.fOffset.fX && 0 == info4.fOffset.fY);
985 REPORTER_ASSERT(reporter, info4.fCTM.isIdentity());
986 REPORTER_ASSERT(reporter, NULL == info4.fPaint); // paint is/was uncopyable
987 REPORTER_ASSERT(reporter, !info4.fIsNested && !info4.fHasNestedLayers);
988 #endif
989 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000990 }
991}
992
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000993#endif
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000994
ajuma750ae262014-08-18 12:59:55 -0700995static void test_has_text(skiatest::Reporter* reporter) {
996 SkPictureRecorder recorder;
997 SkPaint paint;
998 paint.setColor(SK_ColorBLUE);
999 SkPoint point = SkPoint::Make(10, 10);
1000
1001 SkCanvas* canvas = recorder.beginRecording(100, 100);
1002 {
1003 canvas->drawRect(SkRect::MakeWH(20, 20), paint);
1004 }
1005 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1006 REPORTER_ASSERT(reporter, !picture->hasText());
1007
1008 canvas = recorder.beginRecording(100, 100);
1009 {
1010 canvas->drawText("Q", 1, point.fX, point.fY, paint);
1011 }
1012 picture.reset(recorder.endRecording());
1013 REPORTER_ASSERT(reporter, picture->hasText());
1014
1015 canvas = recorder.beginRecording(100, 100);
1016 {
1017 canvas->drawPosText("Q", 1, &point, paint);
1018 }
1019 picture.reset(recorder.endRecording());
1020 REPORTER_ASSERT(reporter, picture->hasText());
1021
1022 canvas = recorder.beginRecording(100, 100);
1023 {
1024 canvas->drawPosTextH("Q", 1, &point.fX, point.fY, paint);
1025 }
1026 picture.reset(recorder.endRecording());
1027 REPORTER_ASSERT(reporter, picture->hasText());
1028
1029 canvas = recorder.beginRecording(100, 100);
1030 {
1031 SkPath path;
1032 path.moveTo(0, 0);
1033 path.lineTo(50, 50);
1034
1035 canvas->drawTextOnPathHV("Q", 1, path, point.fX, point.fY, paint);
1036 }
1037 picture.reset(recorder.endRecording());
1038 REPORTER_ASSERT(reporter, picture->hasText());
1039
1040 canvas = recorder.beginRecording(100, 100);
1041 {
1042 SkPath path;
1043 path.moveTo(0, 0);
1044 path.lineTo(50, 50);
1045
1046 canvas->drawTextOnPath("Q", 1, path, NULL, paint);
1047 }
1048 picture.reset(recorder.endRecording());
1049 REPORTER_ASSERT(reporter, picture->hasText());
1050}
1051
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001052static void set_canvas_to_save_count_4(SkCanvas* canvas) {
1053 canvas->restoreToCount(1);
1054 canvas->save();
1055 canvas->save();
1056 canvas->save();
1057}
1058
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001059/**
1060 * A canvas that records the number of saves, saveLayers and restores.
1061 */
1062class SaveCountingCanvas : public SkCanvas {
1063public:
1064 SaveCountingCanvas(int width, int height)
1065 : INHERITED(width, height)
1066 , fSaveCount(0)
1067 , fSaveLayerCount(0)
1068 , fRestoreCount(0){
1069 }
1070
1071 virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint,
1072 SaveFlags flags) SK_OVERRIDE {
1073 ++fSaveLayerCount;
1074 return this->INHERITED::willSaveLayer(bounds, paint, flags);
1075 }
1076
Florin Malita5f6102d2014-06-30 10:13:28 -04001077 virtual void willSave() SK_OVERRIDE {
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001078 ++fSaveCount;
Florin Malita5f6102d2014-06-30 10:13:28 -04001079 this->INHERITED::willSave();
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001080 }
1081
1082 virtual void willRestore() SK_OVERRIDE {
1083 ++fRestoreCount;
1084 this->INHERITED::willRestore();
1085 }
1086
1087 unsigned int getSaveCount() const { return fSaveCount; }
1088 unsigned int getSaveLayerCount() const { return fSaveLayerCount; }
1089 unsigned int getRestoreCount() const { return fRestoreCount; }
1090
1091private:
1092 unsigned int fSaveCount;
1093 unsigned int fSaveLayerCount;
1094 unsigned int fRestoreCount;
1095
1096 typedef SkCanvas INHERITED;
1097};
1098
skia.committer@gmail.com8e7d37d2014-05-28 03:06:06 +00001099void check_save_state(skiatest::Reporter* reporter, SkPicture* picture,
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001100 unsigned int numSaves, unsigned int numSaveLayers,
1101 unsigned int numRestores) {
1102 SaveCountingCanvas canvas(picture->width(), picture->height());
1103
1104 picture->draw(&canvas);
1105
1106 REPORTER_ASSERT(reporter, numSaves == canvas.getSaveCount());
1107 REPORTER_ASSERT(reporter, numSaveLayers == canvas.getSaveLayerCount());
1108 REPORTER_ASSERT(reporter, numRestores == canvas.getRestoreCount());
1109}
1110
1111// This class exists so SkPicture can friend it and give it access to
1112// the 'partialReplay' method.
1113class SkPictureRecorderReplayTester {
1114public:
1115 static SkPicture* Copy(SkPictureRecorder* recorder) {
1116 SkPictureRecorder recorder2;
1117
robertphillips9f1c2412014-06-09 06:25:34 -07001118 SkCanvas* canvas = recorder2.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001119
1120 recorder->partialReplay(canvas);
1121
1122 return recorder2.endRecording();
1123 }
1124};
1125
robertphillips9058d602014-06-10 11:45:46 -07001126static void create_imbalance(SkCanvas* canvas) {
1127 SkRect clipRect = SkRect::MakeWH(2, 2);
1128 SkRect drawRect = SkRect::MakeWH(10, 10);
1129 canvas->save();
1130 canvas->clipRect(clipRect, SkRegion::kReplace_Op);
1131 canvas->translate(1.0f, 1.0f);
1132 SkPaint p;
1133 p.setColor(SK_ColorGREEN);
1134 canvas->drawRect(drawRect, p);
1135 // no restore
1136}
1137
1138// This tests that replaying a potentially unbalanced picture into a canvas
1139// doesn't affect the canvas' save count or matrix/clip state.
1140static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) {
1141 SkBitmap bm;
1142 bm.allocN32Pixels(4, 3);
1143 SkCanvas canvas(bm);
1144
1145 int beforeSaveCount = canvas.getSaveCount();
1146
1147 SkMatrix beforeMatrix = canvas.getTotalMatrix();
1148
1149 SkRect beforeClip;
1150
1151 canvas.getClipBounds(&beforeClip);
1152
1153 canvas.drawPicture(picture);
1154
1155 REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount());
1156 REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix());
1157
1158 SkRect afterClip;
1159
1160 canvas.getClipBounds(&afterClip);
1161
1162 REPORTER_ASSERT(reporter, afterClip == beforeClip);
1163}
1164
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001165// Test out SkPictureRecorder::partialReplay
1166DEF_TEST(PictureRecorder_replay, reporter) {
1167 // check save/saveLayer state
1168 {
1169 SkPictureRecorder recorder;
1170
robertphillips9f1c2412014-06-09 06:25:34 -07001171 SkCanvas* canvas = recorder.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001172
1173 canvas->saveLayer(NULL, NULL);
1174
1175 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1176
1177 // The extra save and restore comes from the Copy process.
1178 check_save_state(reporter, copy, 2, 1, 3);
1179
1180 canvas->saveLayer(NULL, NULL);
1181
1182 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1183
1184 check_save_state(reporter, final, 1, 2, 3);
1185
1186 // The copy shouldn't pick up any operations added after it was made
1187 check_save_state(reporter, copy, 2, 1, 3);
1188 }
1189
1190 // (partially) check leakage of draw ops
1191 {
1192 SkPictureRecorder recorder;
1193
robertphillips9f1c2412014-06-09 06:25:34 -07001194 SkCanvas* canvas = recorder.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001195
1196 SkRect r = SkRect::MakeWH(5, 5);
1197 SkPaint p;
1198
1199 canvas->drawRect(r, p);
1200
1201 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1202
1203 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1204
1205 SkBitmap bm;
1206 make_bm(&bm, 10, 10, SK_ColorRED, true);
1207
1208 r.offset(5.0f, 5.0f);
1209 canvas->drawBitmapRectToRect(bm, NULL, r);
1210
1211 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1212 REPORTER_ASSERT(reporter, final->willPlayBackBitmaps());
1213
1214 REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID());
1215
1216 // The snapshot shouldn't pick up any operations added after it was made
1217 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1218 }
robertphillips9058d602014-06-10 11:45:46 -07001219
1220 // Recreate the Android partialReplay test case
1221 {
1222 SkPictureRecorder recorder;
1223
1224 SkCanvas* canvas = recorder.beginRecording(4, 3, NULL, 0);
1225 create_imbalance(canvas);
1226
1227 int expectedSaveCount = canvas->getSaveCount();
1228
1229 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1230 check_balance(reporter, copy);
1231
1232 REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount());
1233
1234 // End the recording of source to test the picture finalization
1235 // process isn't complicated by the partialReplay step
1236 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1237 }
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001238}
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001239
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001240static void test_unbalanced_save_restores(skiatest::Reporter* reporter) {
1241 SkCanvas testCanvas(100, 100);
1242 set_canvas_to_save_count_4(&testCanvas);
1243
1244 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1245
1246 SkPaint paint;
1247 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000);
1248
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001249 SkPictureRecorder recorder;
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001250
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001251 {
1252 // Create picture with 2 unbalanced saves
robertphillips9f1c2412014-06-09 06:25:34 -07001253 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001254 canvas->save();
1255 canvas->translate(10, 10);
1256 canvas->drawRect(rect, paint);
1257 canvas->save();
1258 canvas->translate(10, 10);
1259 canvas->drawRect(rect, paint);
1260 SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording());
1261
robertphillips9b14f262014-06-04 05:40:44 -07001262 testCanvas.drawPicture(extraSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001263 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1264 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001265
1266 set_canvas_to_save_count_4(&testCanvas);
1267
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001268 {
1269 // Create picture with 2 unbalanced restores
robertphillips9f1c2412014-06-09 06:25:34 -07001270 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001271 canvas->save();
1272 canvas->translate(10, 10);
1273 canvas->drawRect(rect, paint);
1274 canvas->save();
1275 canvas->translate(10, 10);
1276 canvas->drawRect(rect, paint);
1277 canvas->restore();
1278 canvas->restore();
1279 canvas->restore();
1280 canvas->restore();
1281 SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording());
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001282
robertphillips9b14f262014-06-04 05:40:44 -07001283 testCanvas.drawPicture(extraRestorePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001284 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1285 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001286
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001287 set_canvas_to_save_count_4(&testCanvas);
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001288
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001289 {
robertphillips9f1c2412014-06-09 06:25:34 -07001290 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001291 canvas->translate(10, 10);
1292 canvas->drawRect(rect, paint);
1293 SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording());
1294
robertphillips9b14f262014-06-04 05:40:44 -07001295 testCanvas.drawPicture(noSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001296 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1297 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity());
1298 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001299}
1300
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001301static void test_peephole() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001302 SkRandom rand;
reed@google.com21b519d2012-10-02 17:42:15 +00001303
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001304 SkPictureRecorder recorder;
1305
reed@google.com21b519d2012-10-02 17:42:15 +00001306 for (int j = 0; j < 100; j++) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001307 SkRandom rand2(rand); // remember the seed
reed@google.com21b519d2012-10-02 17:42:15 +00001308
robertphillips9f1c2412014-06-09 06:25:34 -07001309 SkCanvas* canvas = recorder.beginRecording(100, 100);
reed@google.com21b519d2012-10-02 17:42:15 +00001310
1311 for (int i = 0; i < 1000; ++i) {
1312 rand_op(canvas, rand);
1313 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001314 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
jvanverth@google.comc490f802013-03-04 13:56:38 +00001315
1316 rand = rand2;
reed@google.com21b519d2012-10-02 17:42:15 +00001317 }
1318
1319 {
robertphillips9f1c2412014-06-09 06:25:34 -07001320 SkCanvas* canvas = recorder.beginRecording(100, 100);
reed@google.com21b519d2012-10-02 17:42:15 +00001321 SkRect rect = SkRect::MakeWH(50, 50);
skia.committer@gmail.com52c24372012-10-03 02:01:13 +00001322
reed@google.com21b519d2012-10-02 17:42:15 +00001323 for (int i = 0; i < 100; ++i) {
1324 canvas->save();
1325 }
1326 while (canvas->getSaveCount() > 1) {
1327 canvas->clipRect(rect);
1328 canvas->restore();
1329 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001330 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
reed@google.com21b519d2012-10-02 17:42:15 +00001331 }
1332}
1333
scroggo@google.com4b90b112012-12-04 15:08:56 +00001334#ifndef SK_DEBUG
1335// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
1336// should never do this.
1337static void test_bad_bitmap() {
1338 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
1339 // fail.
1340 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001341 bm.setInfo(SkImageInfo::MakeN32Premul(100, 100));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001342 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001343 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001344 recordingCanvas->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001345 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.com4b90b112012-12-04 15:08:56 +00001346
1347 SkCanvas canvas;
robertphillips9b14f262014-06-04 05:40:44 -07001348 canvas.drawPicture(picture);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001349}
1350#endif
1351
reed@google.com672588b2014-01-08 15:42:01 +00001352static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001353 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001354}
1355
1356static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001357 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001358 SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height());
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001359 canvas->drawBitmap(bitmap, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001360 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1361
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001362 SkDynamicMemoryWStream wStream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001363 picture->serialize(&wStream, &encode_bitmap_to_data);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001364 return wStream.copyToData();
1365}
1366
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001367struct ErrorContext {
1368 int fErrors;
1369 skiatest::Reporter* fReporter;
1370};
1371
1372static void assert_one_parse_error_cb(SkError error, void* context) {
1373 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
1374 errorContext->fErrors++;
1375 // This test only expects one error, and that is a kParseError. If there are others,
1376 // there is some unknown problem.
1377 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors,
1378 "This threw more errors than expected.");
1379 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error,
1380 SkGetLastErrorString());
1381}
1382
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001383static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
1384 // Create a bitmap that will be encoded.
1385 SkBitmap original;
1386 make_bm(&original, 100, 100, SK_ColorBLUE, true);
1387 SkDynamicMemoryWStream wStream;
1388 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
1389 return;
1390 }
1391 SkAutoDataUnref data(wStream.copyToData());
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001392
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001393 SkBitmap bm;
halcanary@google.com3d50ea12014-01-02 13:15:13 +00001394 bool installSuccess = SkInstallDiscardablePixelRef(
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +00001395 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm);
reed@google.combf790232013-12-13 19:45:58 +00001396 REPORTER_ASSERT(reporter, installSuccess);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001397
1398 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
1399 // Flattening original will follow the old path of performing an encode, while flattening bm
1400 // will use the already encoded data.
1401 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
1402 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
1403 REPORTER_ASSERT(reporter, picture1->equals(picture2));
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001404 // Now test that a parse error was generated when trying to create a new SkPicture without
1405 // providing a function to decode the bitmap.
1406 ErrorContext context;
1407 context.fErrors = 0;
1408 context.fReporter = reporter;
1409 SkSetErrorCallback(assert_one_parse_error_cb, &context);
1410 SkMemoryStream pictureStream(picture1);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001411 SkClearLastError();
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001412 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL));
1413 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001414 SkClearLastError();
1415 SkSetErrorCallback(NULL, NULL);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001416}
1417
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001418static void test_draw_empty(skiatest::Reporter* reporter) {
1419 SkBitmap result;
1420 make_bm(&result, 2, 2, SK_ColorBLACK, false);
1421
1422 SkCanvas canvas(result);
1423
1424 {
1425 // stock SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001426 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001427 recorder.beginRecording(1, 1);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001428 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001429
robertphillips9b14f262014-06-04 05:40:44 -07001430 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001431 }
1432
1433 {
1434 // tile grid
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001435 SkTileGridFactory::TileGridInfo gridInfo;
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001436 gridInfo.fMargin.setEmpty();
1437 gridInfo.fOffset.setZero();
1438 gridInfo.fTileInterval.set(1, 1);
1439
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001440 SkTileGridFactory factory(gridInfo);
1441 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001442 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001443 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001444
robertphillips9b14f262014-06-04 05:40:44 -07001445 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001446 }
1447
1448 {
1449 // RTree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001450 SkRTreeFactory factory;
1451 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001452 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001453 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001454
robertphillips9b14f262014-06-04 05:40:44 -07001455 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001456 }
1457
1458 {
1459 // quad tree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001460 SkQuadTreeFactory factory;
1461 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001462 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001463 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001464
robertphillips9b14f262014-06-04 05:40:44 -07001465 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001466 }
1467}
1468
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001469static void test_clip_bound_opt(skiatest::Reporter* reporter) {
1470 // Test for crbug.com/229011
1471 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
1472 SkIntToScalar(2), SkIntToScalar(2));
1473 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
1474 SkIntToScalar(1), SkIntToScalar(1));
1475 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
1476 SkIntToScalar(1), SkIntToScalar(1));
1477
1478 SkPath invPath;
1479 invPath.addOval(rect1);
1480 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
1481 SkPath path;
1482 path.addOval(rect2);
1483 SkPath path2;
1484 path2.addOval(rect3);
1485 SkIRect clipBounds;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001486 SkPictureRecorder recorder;
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001487 // Minimalist test set for 100% code coverage of
1488 // SkPictureRecord::updateClipConservativelyUsingBounds
1489 {
robertphillips9f1c2412014-06-09 06:25:34 -07001490 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001491 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1492 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1493 REPORTER_ASSERT(reporter, true == nonEmpty);
1494 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1495 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1496 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1497 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1498 }
1499 {
robertphillips9f1c2412014-06-09 06:25:34 -07001500 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001501 canvas->clipPath(path, SkRegion::kIntersect_Op);
1502 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1503 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1504 REPORTER_ASSERT(reporter, true == nonEmpty);
1505 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1506 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1507 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1508 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1509 }
1510 {
robertphillips9f1c2412014-06-09 06:25:34 -07001511 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001512 canvas->clipPath(path, SkRegion::kIntersect_Op);
1513 canvas->clipPath(invPath, SkRegion::kUnion_Op);
1514 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1515 REPORTER_ASSERT(reporter, true == nonEmpty);
1516 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1517 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1518 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1519 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1520 }
1521 {
robertphillips9f1c2412014-06-09 06:25:34 -07001522 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001523 canvas->clipPath(path, SkRegion::kDifference_Op);
1524 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1525 REPORTER_ASSERT(reporter, true == nonEmpty);
1526 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1527 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1528 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1529 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1530 }
1531 {
robertphillips9f1c2412014-06-09 06:25:34 -07001532 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001533 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
1534 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1535 // True clip is actually empty in this case, but the best
1536 // determination we can make using only bounds as input is that the
1537 // clip is included in the bounds of 'path'.
1538 REPORTER_ASSERT(reporter, true == nonEmpty);
1539 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1540 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1541 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1542 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1543 }
1544 {
robertphillips9f1c2412014-06-09 06:25:34 -07001545 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001546 canvas->clipPath(path, SkRegion::kIntersect_Op);
1547 canvas->clipPath(path2, SkRegion::kXOR_Op);
1548 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1549 REPORTER_ASSERT(reporter, true == nonEmpty);
1550 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
1551 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
1552 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1553 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1554 }
1555}
1556
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001557/**
1558 * A canvas that records the number of clip commands.
1559 */
1560class ClipCountingCanvas : public SkCanvas {
1561public:
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001562 ClipCountingCanvas(int width, int height)
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001563 : INHERITED(width, height)
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001564 , fClipCount(0){
1565 }
1566
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001567 virtual void onClipRect(const SkRect& r,
1568 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001569 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001570 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001571 this->INHERITED::onClipRect(r, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001572 }
1573
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001574 virtual void onClipRRect(const SkRRect& rrect,
1575 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001576 ClipEdgeStyle edgeStyle)SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001577 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001578 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001579 }
1580
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001581 virtual void onClipPath(const SkPath& path,
1582 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001583 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001584 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001585 this->INHERITED::onClipPath(path, op, edgeStyle);
1586 }
1587
1588 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE {
1589 fClipCount += 1;
1590 this->INHERITED::onClipRegion(deviceRgn, op);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001591 }
1592
1593 unsigned getClipCount() const { return fClipCount; }
1594
1595private:
1596 unsigned fClipCount;
1597
1598 typedef SkCanvas INHERITED;
1599};
1600
1601static void test_clip_expansion(skiatest::Reporter* reporter) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001602 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001603 SkCanvas* canvas = recorder.beginRecording(10, 10);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001604
1605 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
1606 // The following expanding clip should not be skipped.
1607 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
1608 // Draw something so the optimizer doesn't just fold the world.
1609 SkPaint p;
1610 p.setColor(SK_ColorBLUE);
1611 canvas->drawPaint(p);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001612 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001613
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001614 ClipCountingCanvas testCanvas(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001615 picture->draw(&testCanvas);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001616
1617 // Both clips should be present on playback.
1618 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
1619}
1620
tomhudson@google.com381010e2013-10-24 11:12:47 +00001621static void test_hierarchical(skiatest::Reporter* reporter) {
1622 SkBitmap bm;
1623 make_bm(&bm, 10, 10, SK_ColorRED, true);
1624
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001625 SkPictureRecorder recorder;
tomhudson@google.com381010e2013-10-24 11:12:47 +00001626
robertphillips9f1c2412014-06-09 06:25:34 -07001627 recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001628 SkAutoTUnref<SkPicture> childPlain(recorder.endRecording());
1629 REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0
tomhudson@google.com381010e2013-10-24 11:12:47 +00001630
robertphillips9f1c2412014-06-09 06:25:34 -07001631 recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001632 SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording());
1633 REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1
tomhudson@google.com381010e2013-10-24 11:12:47 +00001634
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001635 {
robertphillips9f1c2412014-06-09 06:25:34 -07001636 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips9b14f262014-06-04 05:40:44 -07001637 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001638 SkAutoTUnref<SkPicture> parentPP(recorder.endRecording());
1639 REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0
1640 }
1641 {
robertphillips9f1c2412014-06-09 06:25:34 -07001642 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips9b14f262014-06-04 05:40:44 -07001643 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001644 SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording());
1645 REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1
1646 }
1647 {
robertphillips9f1c2412014-06-09 06:25:34 -07001648 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001649 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001650 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001651 SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording());
1652 REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1
1653 }
1654 {
robertphillips9f1c2412014-06-09 06:25:34 -07001655 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001656 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001657 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001658 SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording());
1659 REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2
1660 }
tomhudson@google.com381010e2013-10-24 11:12:47 +00001661}
1662
robertphillips@google.comd5500882014-04-02 23:51:13 +00001663static void test_gen_id(skiatest::Reporter* reporter) {
1664
Robert Phillipscfaeec42014-07-13 12:00:50 -04001665 SkPictureRecorder recorder;
1666 recorder.beginRecording(0, 0);
1667 SkAutoTUnref<SkPicture> empty(recorder.endRecording());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001668
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001669 // Empty pictures should still have a valid ID
Robert Phillipscfaeec42014-07-13 12:00:50 -04001670 REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001671
robertphillips9f1c2412014-06-09 06:25:34 -07001672 SkCanvas* canvas = recorder.beginRecording(1, 1);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001673 canvas->drawARGB(255, 255, 255, 255);
1674 SkAutoTUnref<SkPicture> hasData(recorder.endRecording());
1675 // picture should have a non-zero id after recording
1676 REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001677
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001678 // both pictures should have different ids
Robert Phillipscfaeec42014-07-13 12:00:50 -04001679 REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001680}
1681
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00001682DEF_TEST(Picture, reporter) {
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001683#ifdef SK_DEBUG
robertphillipsdb539902014-07-01 08:47:04 -07001684 test_deleting_empty_picture();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001685 test_serializing_empty_picture();
scroggo@google.com4b90b112012-12-04 15:08:56 +00001686#else
1687 test_bad_bitmap();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001688#endif
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001689 test_unbalanced_save_restores(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001690 test_peephole();
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001691#if SK_SUPPORT_GPU
tomhudson60c2a792014-08-18 15:07:13 -07001692 test_gpu_veto(reporter, false);
1693 test_gpu_veto(reporter, true);
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001694#endif
ajuma750ae262014-08-18 12:59:55 -07001695 test_has_text(reporter);
tomhudson60c2a792014-08-18 15:07:13 -07001696 test_analysis(reporter, false);
1697 test_analysis(reporter, true);
reed@google.comfe7b1ed2012-11-29 21:00:39 +00001698 test_gatherpixelrefs(reporter);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +00001699 test_gatherpixelrefsandrects(reporter);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001700 test_bitmap_with_encoded_data(reporter);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001701 test_draw_empty(reporter);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001702 test_clip_bound_opt(reporter);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001703 test_clip_expansion(reporter);
tomhudson@google.com381010e2013-10-24 11:12:47 +00001704 test_hierarchical(reporter);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001705 test_gen_id(reporter);
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001706}
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001707
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001708#if SK_SUPPORT_GPU
1709DEF_GPUTEST(GPUPicture, reporter, factory) {
1710 test_gpu_picture_optimization(reporter, factory);
1711}
1712#endif
1713
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001714static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) {
1715 const SkPaint paint;
1716 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f };
1717 const SkIRect irect = { 2, 2, 3, 3 };
1718
1719 // Don't care what these record, as long as they're legal.
1720 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint);
1721 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag);
1722 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint);
1723 canvas->drawBitmapNine(bitmap, irect, rect, &paint);
1724 canvas->drawSprite(bitmap, 1, 1);
1725}
1726
1727static void test_draw_bitmaps(SkCanvas* canvas) {
1728 SkBitmap empty;
1729 draw_bitmaps(empty, canvas);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001730 empty.setInfo(SkImageInfo::MakeN32Premul(10, 10));
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001731 draw_bitmaps(empty, canvas);
1732}
1733
1734DEF_TEST(Picture_EmptyBitmap, r) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001735 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001736 test_draw_bitmaps(recorder.beginRecording(10, 10));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001737 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001738}
1739
1740DEF_TEST(Canvas_EmptyBitmap, r) {
1741 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001742 dst.allocN32Pixels(10, 10);
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001743 SkCanvas canvas(dst);
1744
1745 test_draw_bitmaps(&canvas);
1746}
dneto3f22e8c2014-07-30 15:42:22 -07001747
1748DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) {
1749 // This test is from crbug.com/344987.
1750 // The commands are:
1751 // saveLayer with paint that modifies alpha
1752 // drawBitmapRectToRect
1753 // drawBitmapRectToRect
1754 // restore
1755 // The bug was that this structure was modified so that:
1756 // - The saveLayer and restore were eliminated
1757 // - The alpha was only applied to the first drawBitmapRectToRect
1758
1759 // This test draws blue and red squares inside a 50% transparent
1760 // layer. Both colours should show up muted.
1761 // When the bug is present, the red square (the second bitmap)
1762 // shows upwith full opacity.
1763
1764 SkBitmap blueBM;
1765 make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true);
1766 SkBitmap redBM;
1767 make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true);
1768 SkPaint semiTransparent;
1769 semiTransparent.setAlpha(0x80);
1770
1771 SkPictureRecorder recorder;
1772 SkCanvas* canvas = recorder.beginRecording(100, 100);
1773 canvas->drawARGB(0, 0, 0, 0);
1774
1775 canvas->saveLayer(0, &semiTransparent);
1776 canvas->drawBitmap(blueBM, 25, 25);
1777 canvas->drawBitmap(redBM, 50, 50);
1778 canvas->restore();
1779
1780 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1781
1782 // Now replay the picture back on another canvas
1783 // and check a couple of its pixels.
1784 SkBitmap replayBM;
1785 make_bm(&replayBM, 100, 100, SK_ColorBLACK, false);
1786 SkCanvas replayCanvas(replayBM);
1787 picture->draw(&replayCanvas);
1788 replayCanvas.flush();
1789
1790 // With the bug present, at (55, 55) we would get a fully opaque red
1791 // intead of a dark red.
1792 REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080);
1793 REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000);
1794}
mtklein3e8232b2014-08-18 13:39:11 -07001795
1796struct CountingBBH : public SkBBoxHierarchy {
1797 mutable int searchCalls;
1798
1799 CountingBBH() : searchCalls(0) {}
1800
1801 virtual void search(const SkIRect& query, SkTDArray<void*>* results) const {
1802 this->searchCalls++;
1803 }
1804
1805 // All other methods unimplemented.
1806 virtual void insert(void* data, const SkIRect& bounds, bool defer) {}
1807 virtual void flushDeferredInserts() {}
1808 virtual void clear() {}
1809 virtual int getCount() const { return 0; }
1810 virtual int getDepth() const { return 0; }
1811 virtual void rewindInserts() {}
1812};
1813
1814class SpoonFedBBHFactory : public SkBBHFactory {
1815public:
1816 explicit SpoonFedBBHFactory(SkBBoxHierarchy* bbh) : fBBH(bbh) {}
1817 virtual SkBBoxHierarchy* operator()(int width, int height) const {
1818 return SkRef(fBBH);
1819 }
1820private:
1821 SkBBoxHierarchy* fBBH;
1822};
1823
1824// When the canvas clip covers the full picture, we don't need to call the BBH.
1825DEF_TEST(Picture_SkipBBH, r) {
1826 CountingBBH bbh;
1827 SpoonFedBBHFactory factory(&bbh);
1828
1829 SkPictureRecorder recorder;
1830 recorder.beginRecording(320, 240, &factory);
1831 SkAutoTUnref<const SkPicture> picture(recorder.endRecording());
1832
1833 SkCanvas big(640, 480), small(300, 200);
1834
1835 picture->draw(&big);
1836 REPORTER_ASSERT(r, bbh.searchCalls == 0);
1837
1838 picture->draw(&small);
1839 REPORTER_ASSERT(r, bbh.searchCalls == 1);
1840}