blob: 6a93a9dd1bd1ab0415b74119aad0f3b1f3c16cdc [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
tomhudson3a0f2792014-08-20 05:29:41 -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
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000618static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) {
619 const int IW = 32;
620 const int IH = IW;
621 const SkScalar W = SkIntToScalar(IW);
622 const SkScalar H = W;
623
624 static const int N = 4;
625 SkBitmap bm[2*N];
626 SkPixelRef* refs[2*N];
627 SkTDArray<SkPixelRef*> analytic[N];
628
629 const SkPoint pos[N] = {
630 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
631 };
632
633 create_textures(bm, refs, N, IW, IH);
634
635 SkRandom rand;
636 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000637 SkAutoTUnref<SkPicture> pic(
638 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000639
640 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
641
642 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont(
643 new SkPictureUtils::SkPixelRefsAndRectsList);
644
645 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont);
646
647 // quick check for a small piece of each quadrant, which should just
648 // contain 1 or 2 bitmaps.
649 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
650 SkRect r;
651 r.set(2, 2, W - 2, H - 2);
652 r.offset(pos[i].fX, pos[i].fY);
653
654 SkTDArray<SkPixelRef*> gatheredRefs;
655 prCont->query(r, &gatheredRefs);
656
657 int count = gatheredRefs.count();
658 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
659 if (1 == count) {
660 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
661 } else if (2 == count) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000662 REPORTER_ASSERT(reporter,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000663 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
664 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
665 }
666 }
667
668 SkBitmap image;
669 draw(pic, 2*IW, 2*IH, &image);
670
671 // Test a bunch of random (mostly) rects, and compare the gather results
672 // with the analytic results and the pixel refs seen in a rendering.
673 for (int j = 0; j < 100; ++j) {
674 SkRect r;
675 rand_rect(&r, rand, 2*W, 2*H);
676
677 SkTDArray<SkPixelRef*> fromImage;
678 gather_from_image(image, refs, N, &fromImage, r);
679
680 SkTDArray<SkPixelRef*> fromAnalytic;
681 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
682
683 SkTDArray<SkPixelRef*> gatheredRefs;
684 prCont->query(r, &gatheredRefs);
685
686 // Everything that we saw drawn should appear in the analytic list
687 // but the analytic list may contain some pixelRefs that were not
688 // seen in the image (e.g., A8 textures used as masks)
689 for (int i = 0; i < fromImage.count(); ++i) {
690 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]));
691 }
692
693 // Everything in the analytic list should appear in the gathered
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000694 // list.
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000695 for (int i = 0; i < fromAnalytic.count(); ++i) {
696 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i]));
697 }
698 }
699 }
700}
701
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000702#ifdef SK_DEBUG
mtklein3e8232b2014-08-18 13:39:11 -0700703// Ensure that deleting an empty SkPicture does not assert. Asserts only fire
robertphillipsdb539902014-07-01 08:47:04 -0700704// in debug mode, so only run in debug mode.
705static void test_deleting_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000706 SkPictureRecorder recorder;
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000707 // Creates an SkPictureRecord
robertphillips9f1c2412014-06-09 06:25:34 -0700708 recorder.beginRecording(0, 0);
robertphillipsdb539902014-07-01 08:47:04 -0700709 // Turns that into an SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000710 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
robertphillipsdb539902014-07-01 08:47:04 -0700711 // Ceates a new SkPictureRecord
robertphillips9f1c2412014-06-09 06:25:34 -0700712 recorder.beginRecording(0, 0);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000713}
714
715// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
716static void test_serializing_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000717 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -0700718 recorder.beginRecording(0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000719 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000720 SkDynamicMemoryWStream stream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000721 picture->serialize(&stream);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000722}
723#endif
724
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000725static void rand_op(SkCanvas* canvas, SkRandom& rand) {
reed@google.com21b519d2012-10-02 17:42:15 +0000726 SkPaint paint;
727 SkRect rect = SkRect::MakeWH(50, 50);
728
729 SkScalar unit = rand.nextUScalar1();
730 if (unit <= 0.3) {
731// SkDebugf("save\n");
732 canvas->save();
733 } else if (unit <= 0.6) {
734// SkDebugf("restore\n");
735 canvas->restore();
736 } else if (unit <= 0.9) {
737// SkDebugf("clip\n");
738 canvas->clipRect(rect);
739 } else {
740// SkDebugf("draw\n");
741 canvas->drawPaint(paint);
742 }
743}
744
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000745#if SK_SUPPORT_GPU
tomhudson3a0f2792014-08-20 05:29:41 -0700746
747static void test_gpu_veto(skiatest::Reporter* reporter,
748 bool useNewPath) {
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000749
750 SkPictureRecorder recorder;
751
tomhudson3a0f2792014-08-20 05:29:41 -0700752 SkCanvas* canvas = GENERATE_CANVAS(recorder, useNewPath);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000753 {
754 SkPath path;
755 path.moveTo(0, 0);
756 path.lineTo(50, 50);
757
758 SkScalar intervals[] = { 1.0f, 1.0f };
759 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0));
760
761 SkPaint paint;
762 paint.setStyle(SkPaint::kStroke_Style);
763 paint.setPathEffect(dash);
764
765 canvas->drawPath(path, paint);
766 }
767 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
768 // path effects currently render an SkPicture undesireable for GPU rendering
commit-bot@chromium.orga1ff26a2014-05-30 21:52:52 +0000769
770 const char *reason = NULL;
771 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL, &reason));
772 REPORTER_ASSERT(reporter, NULL != reason);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000773
tomhudson3a0f2792014-08-20 05:29:41 -0700774 canvas = GENERATE_CANVAS(recorder, useNewPath);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000775 {
776 SkPath path;
777
778 path.moveTo(0, 0);
779 path.lineTo(0, 50);
780 path.lineTo(25, 25);
781 path.lineTo(50, 50);
782 path.lineTo(50, 0);
783 path.close();
784 REPORTER_ASSERT(reporter, !path.isConvex());
785
786 SkPaint paint;
787 paint.setAntiAlias(true);
788 for (int i = 0; i < 50; ++i) {
789 canvas->drawPath(path, paint);
790 }
791 }
792 picture.reset(recorder.endRecording());
793 // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering
794 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
795
tomhudson3a0f2792014-08-20 05:29:41 -0700796 canvas = GENERATE_CANVAS(recorder, useNewPath);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000797 {
798 SkPath path;
799
800 path.moveTo(0, 0);
801 path.lineTo(0, 50);
802 path.lineTo(25, 25);
803 path.lineTo(50, 50);
804 path.lineTo(50, 0);
805 path.close();
806 REPORTER_ASSERT(reporter, !path.isConvex());
807
808 SkPaint paint;
809 paint.setAntiAlias(true);
810 paint.setStyle(SkPaint::kStroke_Style);
811 paint.setStrokeWidth(0);
812 for (int i = 0; i < 50; ++i) {
813 canvas->drawPath(path, paint);
814 }
815 }
816 picture.reset(recorder.endRecording());
817 // hairline stroked AA concave paths are fine for GPU rendering
818 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL));
tomhudson3a0f2792014-08-20 05:29:41 -0700819
820 canvas = GENERATE_CANVAS(recorder, useNewPath);
821 {
822 SkPaint paint;
823 SkScalar intervals [] = { 10, 20 };
824 SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25);
825 paint.setPathEffect(pe)->unref();
826
827 SkPoint points [2] = { { 0, 0 }, { 100, 0 } };
828 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, points, paint);
829 }
830 picture.reset(recorder.endRecording());
831 // fast-path dashed effects are fine for GPU rendering ...
832 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL));
833
834 canvas = GENERATE_CANVAS(recorder, useNewPath);
835 {
836 SkPaint paint;
837 SkScalar intervals [] = { 10, 20 };
838 SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25);
839 paint.setPathEffect(pe)->unref();
840
841 canvas->drawRect(SkRect::MakeWH(10, 10), paint);
842 }
843 picture.reset(recorder.endRecording());
844 // ... but only when applied to drawPoint() calls
845 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000846}
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000847
tomhudson3a0f2792014-08-20 05:29:41 -0700848#undef GENERATE_CANVAS
849
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000850static void test_gpu_picture_optimization(skiatest::Reporter* reporter,
851 GrContextFactory* factory) {
bsalomone904c092014-07-17 10:50:59 -0700852 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
853 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000854
bsalomone904c092014-07-17 10:50:59 -0700855 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
856 continue;
857 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000858
bsalomone904c092014-07-17 10:50:59 -0700859 GrContext* context = factory->get(glCtxType);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000860
bsalomone904c092014-07-17 10:50:59 -0700861 if (NULL == context) {
862 continue;
863 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000864
bsalomone904c092014-07-17 10:50:59 -0700865 static const int kWidth = 100;
866 static const int kHeight = 100;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000867
bsalomone904c092014-07-17 10:50:59 -0700868 SkAutoTUnref<SkPicture> pict;
869
870 // create a picture with the structure:
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000871 // 1)
bsalomone904c092014-07-17 10:50:59 -0700872 // SaveLayer
873 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000874 // 2)
bsalomone904c092014-07-17 10:50:59 -0700875 // SaveLayer
876 // Translate
877 // SaveLayer w/ bound
878 // Restore
879 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000880 // 3)
bsalomone904c092014-07-17 10:50:59 -0700881 // SaveLayer w/ copyable paint
882 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000883 // 4)
bsalomone904c092014-07-17 10:50:59 -0700884 // SaveLayer w/ non-copyable paint
885 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000886 {
bsalomone904c092014-07-17 10:50:59 -0700887 SkPictureRecorder recorder;
888
889 SkCanvas* c = recorder.beginRecording(kWidth, kHeight);
890 // 1)
891 c->saveLayer(NULL, NULL);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000892 c->restore();
bsalomone904c092014-07-17 10:50:59 -0700893
894 // 2)
895 c->saveLayer(NULL, NULL);
896 c->translate(kWidth/2, kHeight/2);
897 SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2);
898 c->saveLayer(&r, NULL);
899 c->restore();
900 c->restore();
901
902 // 3)
903 {
904 SkPaint p;
905 p.setColor(SK_ColorRED);
906 c->saveLayer(NULL, &p);
907 c->restore();
908 }
909 // 4)
910 // TODO: this case will need to be removed once the paint's are immutable
911 {
912 SkPaint p;
913 SkAutoTUnref<SkColorFilter> cf(SkLumaColorFilter::Create());
914 p.setImageFilter(SkColorFilterImageFilter::Create(cf.get()))->unref();
915 c->saveLayer(NULL, &p);
916 c->restore();
917 }
918
919 pict.reset(recorder.endRecording());
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000920 }
921
bsalomone904c092014-07-17 10:50:59 -0700922 // Now test out the SaveLayer extraction
923 {
924 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000925
bsalomone904c092014-07-17 10:50:59 -0700926 SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info));
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000927
bsalomone904c092014-07-17 10:50:59 -0700928 SkCanvas* canvas = surface->getCanvas();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000929
bsalomone904c092014-07-17 10:50:59 -0700930 canvas->EXPERIMENTAL_optimize(pict);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000931
robertphillips6617d502014-08-18 09:39:34 -0700932 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000933
bsalomone904c092014-07-17 10:50:59 -0700934 const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key);
935 REPORTER_ASSERT(reporter, NULL != data);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000936
robertphillips6617d502014-08-18 09:39:34 -0700937 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data);
bsalomone904c092014-07-17 10:50:59 -0700938 REPORTER_ASSERT(reporter, 5 == gpuData->numSaveLayers());
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000939
robertphillips6617d502014-08-18 09:39:34 -0700940 const GrAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0);
bsalomone904c092014-07-17 10:50:59 -0700941 // The parent/child layer appear in reverse order
robertphillips6617d502014-08-18 09:39:34 -0700942 const GrAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2);
943 const GrAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1);
944 const GrAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3);
945// const GrAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(4);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000946
bsalomone904c092014-07-17 10:50:59 -0700947 REPORTER_ASSERT(reporter, info0.fValid);
948 REPORTER_ASSERT(reporter, kWidth == info0.fSize.fWidth && kHeight == info0.fSize.fHeight);
949 REPORTER_ASSERT(reporter, info0.fCTM.isIdentity());
950 REPORTER_ASSERT(reporter, 0 == info0.fOffset.fX && 0 == info0.fOffset.fY);
951 REPORTER_ASSERT(reporter, NULL != info0.fPaint);
952 REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000953
bsalomone904c092014-07-17 10:50:59 -0700954 REPORTER_ASSERT(reporter, info1.fValid);
955 REPORTER_ASSERT(reporter, kWidth == info1.fSize.fWidth && kHeight == info1.fSize.fHeight);
956 REPORTER_ASSERT(reporter, info1.fCTM.isIdentity());
957 REPORTER_ASSERT(reporter, 0 == info1.fOffset.fX && 0 == info1.fOffset.fY);
958 REPORTER_ASSERT(reporter, NULL != info1.fPaint);
959 REPORTER_ASSERT(reporter, !info1.fIsNested && info1.fHasNestedLayers); // has a nested SL
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000960
bsalomone904c092014-07-17 10:50:59 -0700961 REPORTER_ASSERT(reporter, info2.fValid);
962 REPORTER_ASSERT(reporter, kWidth/2 == info2.fSize.fWidth &&
963 kHeight/2 == info2.fSize.fHeight); // bound reduces size
964 REPORTER_ASSERT(reporter, info2.fCTM.isIdentity()); // translated
965 REPORTER_ASSERT(reporter, kWidth/2 == info2.fOffset.fX &&
966 kHeight/2 == info2.fOffset.fY);
967 REPORTER_ASSERT(reporter, NULL != info1.fPaint);
968 REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000969
bsalomone904c092014-07-17 10:50:59 -0700970 REPORTER_ASSERT(reporter, info3.fValid);
971 REPORTER_ASSERT(reporter, kWidth == info3.fSize.fWidth && kHeight == info3.fSize.fHeight);
972 REPORTER_ASSERT(reporter, info3.fCTM.isIdentity());
973 REPORTER_ASSERT(reporter, 0 == info3.fOffset.fX && 0 == info3.fOffset.fY);
974 REPORTER_ASSERT(reporter, NULL != info3.fPaint);
975 REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000976
bsalomone904c092014-07-17 10:50:59 -0700977 #if 0 // needs more though for GrGatherCanvas
978 REPORTER_ASSERT(reporter, !info4.fValid); // paint is/was uncopyable
979 REPORTER_ASSERT(reporter, kWidth == info4.fSize.fWidth && kHeight == info4.fSize.fHeight);
980 REPORTER_ASSERT(reporter, 0 == info4.fOffset.fX && 0 == info4.fOffset.fY);
981 REPORTER_ASSERT(reporter, info4.fCTM.isIdentity());
982 REPORTER_ASSERT(reporter, NULL == info4.fPaint); // paint is/was uncopyable
983 REPORTER_ASSERT(reporter, !info4.fIsNested && !info4.fHasNestedLayers);
984 #endif
985 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000986 }
987}
988
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000989#endif
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000990
mtkleinc551d9f2014-08-20 08:09:46 -0700991static void test_has_text(skiatest::Reporter* reporter, bool useNewPath) {
ajuma750ae262014-08-18 12:59:55 -0700992 SkPictureRecorder recorder;
mtkleinc551d9f2014-08-20 08:09:46 -0700993#define BEGIN_RECORDING useNewPath ? recorder.EXPERIMENTAL_beginRecording(100, 100) \
994 : recorder. beginRecording(100, 100)
ajuma750ae262014-08-18 12:59:55 -0700995
mtkleinc551d9f2014-08-20 08:09:46 -0700996 SkCanvas* canvas = BEGIN_RECORDING;
ajuma750ae262014-08-18 12:59:55 -0700997 {
mtkleinc551d9f2014-08-20 08:09:46 -0700998 canvas->drawRect(SkRect::MakeWH(20, 20), SkPaint());
ajuma750ae262014-08-18 12:59:55 -0700999 }
1000 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1001 REPORTER_ASSERT(reporter, !picture->hasText());
1002
mtkleinc551d9f2014-08-20 08:09:46 -07001003 SkPoint point = SkPoint::Make(10, 10);
1004 canvas = BEGIN_RECORDING;
ajuma750ae262014-08-18 12:59:55 -07001005 {
mtkleinc551d9f2014-08-20 08:09:46 -07001006 canvas->drawText("Q", 1, point.fX, point.fY, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001007 }
1008 picture.reset(recorder.endRecording());
1009 REPORTER_ASSERT(reporter, picture->hasText());
1010
mtkleinc551d9f2014-08-20 08:09:46 -07001011 canvas = BEGIN_RECORDING;
ajuma750ae262014-08-18 12:59:55 -07001012 {
mtkleinc551d9f2014-08-20 08:09:46 -07001013 canvas->drawPosText("Q", 1, &point, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001014 }
1015 picture.reset(recorder.endRecording());
1016 REPORTER_ASSERT(reporter, picture->hasText());
1017
mtkleinc551d9f2014-08-20 08:09:46 -07001018 canvas = BEGIN_RECORDING;
ajuma750ae262014-08-18 12:59:55 -07001019 {
mtkleinc551d9f2014-08-20 08:09:46 -07001020 canvas->drawPosTextH("Q", 1, &point.fX, point.fY, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001021 }
1022 picture.reset(recorder.endRecording());
1023 REPORTER_ASSERT(reporter, picture->hasText());
1024
mtkleinc551d9f2014-08-20 08:09:46 -07001025 canvas = BEGIN_RECORDING;
ajuma750ae262014-08-18 12:59:55 -07001026 {
1027 SkPath path;
1028 path.moveTo(0, 0);
1029 path.lineTo(50, 50);
1030
mtkleinc551d9f2014-08-20 08:09:46 -07001031 canvas->drawTextOnPathHV("Q", 1, path, point.fX, point.fY, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001032 }
1033 picture.reset(recorder.endRecording());
1034 REPORTER_ASSERT(reporter, picture->hasText());
1035
mtkleinc551d9f2014-08-20 08:09:46 -07001036 canvas = BEGIN_RECORDING;
ajuma750ae262014-08-18 12:59:55 -07001037 {
1038 SkPath path;
1039 path.moveTo(0, 0);
1040 path.lineTo(50, 50);
1041
mtkleinc551d9f2014-08-20 08:09:46 -07001042 canvas->drawTextOnPath("Q", 1, path, NULL, SkPaint());
ajuma750ae262014-08-18 12:59:55 -07001043 }
1044 picture.reset(recorder.endRecording());
1045 REPORTER_ASSERT(reporter, picture->hasText());
mtkleinc551d9f2014-08-20 08:09:46 -07001046#undef BEGIN_RECORDING
ajuma750ae262014-08-18 12:59:55 -07001047}
1048
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001049static void set_canvas_to_save_count_4(SkCanvas* canvas) {
1050 canvas->restoreToCount(1);
1051 canvas->save();
1052 canvas->save();
1053 canvas->save();
1054}
1055
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001056/**
1057 * A canvas that records the number of saves, saveLayers and restores.
1058 */
1059class SaveCountingCanvas : public SkCanvas {
1060public:
1061 SaveCountingCanvas(int width, int height)
1062 : INHERITED(width, height)
1063 , fSaveCount(0)
1064 , fSaveLayerCount(0)
1065 , fRestoreCount(0){
1066 }
1067
1068 virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint,
1069 SaveFlags flags) SK_OVERRIDE {
1070 ++fSaveLayerCount;
1071 return this->INHERITED::willSaveLayer(bounds, paint, flags);
1072 }
1073
Florin Malita5f6102d2014-06-30 10:13:28 -04001074 virtual void willSave() SK_OVERRIDE {
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001075 ++fSaveCount;
Florin Malita5f6102d2014-06-30 10:13:28 -04001076 this->INHERITED::willSave();
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001077 }
1078
1079 virtual void willRestore() SK_OVERRIDE {
1080 ++fRestoreCount;
1081 this->INHERITED::willRestore();
1082 }
1083
1084 unsigned int getSaveCount() const { return fSaveCount; }
1085 unsigned int getSaveLayerCount() const { return fSaveLayerCount; }
1086 unsigned int getRestoreCount() const { return fRestoreCount; }
1087
1088private:
1089 unsigned int fSaveCount;
1090 unsigned int fSaveLayerCount;
1091 unsigned int fRestoreCount;
1092
1093 typedef SkCanvas INHERITED;
1094};
1095
skia.committer@gmail.com8e7d37d2014-05-28 03:06:06 +00001096void check_save_state(skiatest::Reporter* reporter, SkPicture* picture,
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001097 unsigned int numSaves, unsigned int numSaveLayers,
1098 unsigned int numRestores) {
1099 SaveCountingCanvas canvas(picture->width(), picture->height());
1100
1101 picture->draw(&canvas);
1102
1103 REPORTER_ASSERT(reporter, numSaves == canvas.getSaveCount());
1104 REPORTER_ASSERT(reporter, numSaveLayers == canvas.getSaveLayerCount());
1105 REPORTER_ASSERT(reporter, numRestores == canvas.getRestoreCount());
1106}
1107
1108// This class exists so SkPicture can friend it and give it access to
1109// the 'partialReplay' method.
1110class SkPictureRecorderReplayTester {
1111public:
1112 static SkPicture* Copy(SkPictureRecorder* recorder) {
1113 SkPictureRecorder recorder2;
1114
robertphillips9f1c2412014-06-09 06:25:34 -07001115 SkCanvas* canvas = recorder2.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001116
1117 recorder->partialReplay(canvas);
1118
1119 return recorder2.endRecording();
1120 }
1121};
1122
robertphillips9058d602014-06-10 11:45:46 -07001123static void create_imbalance(SkCanvas* canvas) {
1124 SkRect clipRect = SkRect::MakeWH(2, 2);
1125 SkRect drawRect = SkRect::MakeWH(10, 10);
1126 canvas->save();
1127 canvas->clipRect(clipRect, SkRegion::kReplace_Op);
1128 canvas->translate(1.0f, 1.0f);
1129 SkPaint p;
1130 p.setColor(SK_ColorGREEN);
1131 canvas->drawRect(drawRect, p);
1132 // no restore
1133}
1134
1135// This tests that replaying a potentially unbalanced picture into a canvas
1136// doesn't affect the canvas' save count or matrix/clip state.
1137static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) {
1138 SkBitmap bm;
1139 bm.allocN32Pixels(4, 3);
1140 SkCanvas canvas(bm);
1141
1142 int beforeSaveCount = canvas.getSaveCount();
1143
1144 SkMatrix beforeMatrix = canvas.getTotalMatrix();
1145
1146 SkRect beforeClip;
1147
1148 canvas.getClipBounds(&beforeClip);
1149
1150 canvas.drawPicture(picture);
1151
1152 REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount());
1153 REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix());
1154
1155 SkRect afterClip;
1156
1157 canvas.getClipBounds(&afterClip);
1158
1159 REPORTER_ASSERT(reporter, afterClip == beforeClip);
1160}
1161
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001162// Test out SkPictureRecorder::partialReplay
1163DEF_TEST(PictureRecorder_replay, reporter) {
1164 // check save/saveLayer state
1165 {
1166 SkPictureRecorder recorder;
1167
robertphillips9f1c2412014-06-09 06:25:34 -07001168 SkCanvas* canvas = recorder.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001169
1170 canvas->saveLayer(NULL, NULL);
1171
1172 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1173
1174 // The extra save and restore comes from the Copy process.
1175 check_save_state(reporter, copy, 2, 1, 3);
1176
1177 canvas->saveLayer(NULL, NULL);
1178
1179 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1180
1181 check_save_state(reporter, final, 1, 2, 3);
1182
1183 // The copy shouldn't pick up any operations added after it was made
1184 check_save_state(reporter, copy, 2, 1, 3);
1185 }
1186
1187 // (partially) check leakage of draw ops
1188 {
1189 SkPictureRecorder recorder;
1190
robertphillips9f1c2412014-06-09 06:25:34 -07001191 SkCanvas* canvas = recorder.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001192
1193 SkRect r = SkRect::MakeWH(5, 5);
1194 SkPaint p;
1195
1196 canvas->drawRect(r, p);
1197
1198 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1199
1200 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1201
1202 SkBitmap bm;
1203 make_bm(&bm, 10, 10, SK_ColorRED, true);
1204
1205 r.offset(5.0f, 5.0f);
1206 canvas->drawBitmapRectToRect(bm, NULL, r);
1207
1208 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1209 REPORTER_ASSERT(reporter, final->willPlayBackBitmaps());
1210
1211 REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID());
1212
1213 // The snapshot shouldn't pick up any operations added after it was made
1214 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1215 }
robertphillips9058d602014-06-10 11:45:46 -07001216
1217 // Recreate the Android partialReplay test case
1218 {
1219 SkPictureRecorder recorder;
1220
1221 SkCanvas* canvas = recorder.beginRecording(4, 3, NULL, 0);
1222 create_imbalance(canvas);
1223
1224 int expectedSaveCount = canvas->getSaveCount();
1225
1226 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1227 check_balance(reporter, copy);
1228
1229 REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount());
1230
1231 // End the recording of source to test the picture finalization
1232 // process isn't complicated by the partialReplay step
1233 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1234 }
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001235}
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001236
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001237static void test_unbalanced_save_restores(skiatest::Reporter* reporter) {
1238 SkCanvas testCanvas(100, 100);
1239 set_canvas_to_save_count_4(&testCanvas);
1240
1241 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1242
1243 SkPaint paint;
1244 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000);
1245
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001246 SkPictureRecorder recorder;
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001247
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001248 {
1249 // Create picture with 2 unbalanced saves
robertphillips9f1c2412014-06-09 06:25:34 -07001250 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001251 canvas->save();
1252 canvas->translate(10, 10);
1253 canvas->drawRect(rect, paint);
1254 canvas->save();
1255 canvas->translate(10, 10);
1256 canvas->drawRect(rect, paint);
1257 SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording());
1258
robertphillips9b14f262014-06-04 05:40:44 -07001259 testCanvas.drawPicture(extraSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001260 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1261 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001262
1263 set_canvas_to_save_count_4(&testCanvas);
1264
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001265 {
1266 // Create picture with 2 unbalanced restores
robertphillips9f1c2412014-06-09 06:25:34 -07001267 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001268 canvas->save();
1269 canvas->translate(10, 10);
1270 canvas->drawRect(rect, paint);
1271 canvas->save();
1272 canvas->translate(10, 10);
1273 canvas->drawRect(rect, paint);
1274 canvas->restore();
1275 canvas->restore();
1276 canvas->restore();
1277 canvas->restore();
1278 SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording());
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001279
robertphillips9b14f262014-06-04 05:40:44 -07001280 testCanvas.drawPicture(extraRestorePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001281 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1282 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001283
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001284 set_canvas_to_save_count_4(&testCanvas);
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001285
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001286 {
robertphillips9f1c2412014-06-09 06:25:34 -07001287 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001288 canvas->translate(10, 10);
1289 canvas->drawRect(rect, paint);
1290 SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording());
1291
robertphillips9b14f262014-06-04 05:40:44 -07001292 testCanvas.drawPicture(noSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001293 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1294 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity());
1295 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001296}
1297
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001298static void test_peephole() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001299 SkRandom rand;
reed@google.com21b519d2012-10-02 17:42:15 +00001300
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001301 SkPictureRecorder recorder;
1302
reed@google.com21b519d2012-10-02 17:42:15 +00001303 for (int j = 0; j < 100; j++) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001304 SkRandom rand2(rand); // remember the seed
reed@google.com21b519d2012-10-02 17:42:15 +00001305
robertphillips9f1c2412014-06-09 06:25:34 -07001306 SkCanvas* canvas = recorder.beginRecording(100, 100);
reed@google.com21b519d2012-10-02 17:42:15 +00001307
1308 for (int i = 0; i < 1000; ++i) {
1309 rand_op(canvas, rand);
1310 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001311 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
jvanverth@google.comc490f802013-03-04 13:56:38 +00001312
1313 rand = rand2;
reed@google.com21b519d2012-10-02 17:42:15 +00001314 }
1315
1316 {
robertphillips9f1c2412014-06-09 06:25:34 -07001317 SkCanvas* canvas = recorder.beginRecording(100, 100);
reed@google.com21b519d2012-10-02 17:42:15 +00001318 SkRect rect = SkRect::MakeWH(50, 50);
skia.committer@gmail.com52c24372012-10-03 02:01:13 +00001319
reed@google.com21b519d2012-10-02 17:42:15 +00001320 for (int i = 0; i < 100; ++i) {
1321 canvas->save();
1322 }
1323 while (canvas->getSaveCount() > 1) {
1324 canvas->clipRect(rect);
1325 canvas->restore();
1326 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001327 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
reed@google.com21b519d2012-10-02 17:42:15 +00001328 }
1329}
1330
scroggo@google.com4b90b112012-12-04 15:08:56 +00001331#ifndef SK_DEBUG
1332// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
1333// should never do this.
1334static void test_bad_bitmap() {
1335 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
1336 // fail.
1337 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001338 bm.setInfo(SkImageInfo::MakeN32Premul(100, 100));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001339 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001340 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001341 recordingCanvas->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001342 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.com4b90b112012-12-04 15:08:56 +00001343
1344 SkCanvas canvas;
robertphillips9b14f262014-06-04 05:40:44 -07001345 canvas.drawPicture(picture);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001346}
1347#endif
1348
reed@google.com672588b2014-01-08 15:42:01 +00001349static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001350 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001351}
1352
1353static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001354 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001355 SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height());
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001356 canvas->drawBitmap(bitmap, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001357 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1358
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001359 SkDynamicMemoryWStream wStream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001360 picture->serialize(&wStream, &encode_bitmap_to_data);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001361 return wStream.copyToData();
1362}
1363
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001364struct ErrorContext {
1365 int fErrors;
1366 skiatest::Reporter* fReporter;
1367};
1368
1369static void assert_one_parse_error_cb(SkError error, void* context) {
1370 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
1371 errorContext->fErrors++;
1372 // This test only expects one error, and that is a kParseError. If there are others,
1373 // there is some unknown problem.
1374 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors,
1375 "This threw more errors than expected.");
1376 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error,
1377 SkGetLastErrorString());
1378}
1379
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001380static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
1381 // Create a bitmap that will be encoded.
1382 SkBitmap original;
1383 make_bm(&original, 100, 100, SK_ColorBLUE, true);
1384 SkDynamicMemoryWStream wStream;
1385 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
1386 return;
1387 }
1388 SkAutoDataUnref data(wStream.copyToData());
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001389
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001390 SkBitmap bm;
halcanary@google.com3d50ea12014-01-02 13:15:13 +00001391 bool installSuccess = SkInstallDiscardablePixelRef(
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +00001392 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm);
reed@google.combf790232013-12-13 19:45:58 +00001393 REPORTER_ASSERT(reporter, installSuccess);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001394
1395 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
1396 // Flattening original will follow the old path of performing an encode, while flattening bm
1397 // will use the already encoded data.
1398 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
1399 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
1400 REPORTER_ASSERT(reporter, picture1->equals(picture2));
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001401 // Now test that a parse error was generated when trying to create a new SkPicture without
1402 // providing a function to decode the bitmap.
1403 ErrorContext context;
1404 context.fErrors = 0;
1405 context.fReporter = reporter;
1406 SkSetErrorCallback(assert_one_parse_error_cb, &context);
1407 SkMemoryStream pictureStream(picture1);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001408 SkClearLastError();
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001409 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL));
1410 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001411 SkClearLastError();
1412 SkSetErrorCallback(NULL, NULL);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001413}
1414
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001415static void test_draw_empty(skiatest::Reporter* reporter) {
1416 SkBitmap result;
1417 make_bm(&result, 2, 2, SK_ColorBLACK, false);
1418
1419 SkCanvas canvas(result);
1420
1421 {
1422 // stock SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001423 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001424 recorder.beginRecording(1, 1);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001425 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001426
robertphillips9b14f262014-06-04 05:40:44 -07001427 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001428 }
1429
1430 {
1431 // tile grid
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001432 SkTileGridFactory::TileGridInfo gridInfo;
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001433 gridInfo.fMargin.setEmpty();
1434 gridInfo.fOffset.setZero();
1435 gridInfo.fTileInterval.set(1, 1);
1436
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001437 SkTileGridFactory factory(gridInfo);
1438 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001439 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001440 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001441
robertphillips9b14f262014-06-04 05:40:44 -07001442 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001443 }
1444
1445 {
1446 // RTree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001447 SkRTreeFactory factory;
1448 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001449 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001450 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001451
robertphillips9b14f262014-06-04 05:40:44 -07001452 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001453 }
1454
1455 {
1456 // quad tree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001457 SkQuadTreeFactory factory;
1458 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001459 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001460 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001461
robertphillips9b14f262014-06-04 05:40:44 -07001462 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001463 }
1464}
1465
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001466static void test_clip_bound_opt(skiatest::Reporter* reporter) {
1467 // Test for crbug.com/229011
1468 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
1469 SkIntToScalar(2), SkIntToScalar(2));
1470 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
1471 SkIntToScalar(1), SkIntToScalar(1));
1472 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
1473 SkIntToScalar(1), SkIntToScalar(1));
1474
1475 SkPath invPath;
1476 invPath.addOval(rect1);
1477 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
1478 SkPath path;
1479 path.addOval(rect2);
1480 SkPath path2;
1481 path2.addOval(rect3);
1482 SkIRect clipBounds;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001483 SkPictureRecorder recorder;
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001484 // Minimalist test set for 100% code coverage of
1485 // SkPictureRecord::updateClipConservativelyUsingBounds
1486 {
robertphillips9f1c2412014-06-09 06:25:34 -07001487 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001488 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1489 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1490 REPORTER_ASSERT(reporter, true == nonEmpty);
1491 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1492 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1493 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1494 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1495 }
1496 {
robertphillips9f1c2412014-06-09 06:25:34 -07001497 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001498 canvas->clipPath(path, SkRegion::kIntersect_Op);
1499 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1500 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1501 REPORTER_ASSERT(reporter, true == nonEmpty);
1502 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1503 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1504 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1505 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1506 }
1507 {
robertphillips9f1c2412014-06-09 06:25:34 -07001508 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001509 canvas->clipPath(path, SkRegion::kIntersect_Op);
1510 canvas->clipPath(invPath, SkRegion::kUnion_Op);
1511 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1512 REPORTER_ASSERT(reporter, true == nonEmpty);
1513 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1514 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1515 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1516 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1517 }
1518 {
robertphillips9f1c2412014-06-09 06:25:34 -07001519 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001520 canvas->clipPath(path, SkRegion::kDifference_Op);
1521 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1522 REPORTER_ASSERT(reporter, true == nonEmpty);
1523 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1524 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1525 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1526 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1527 }
1528 {
robertphillips9f1c2412014-06-09 06:25:34 -07001529 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001530 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
1531 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1532 // True clip is actually empty in this case, but the best
1533 // determination we can make using only bounds as input is that the
1534 // clip is included in the bounds of 'path'.
1535 REPORTER_ASSERT(reporter, true == nonEmpty);
1536 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1537 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1538 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1539 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1540 }
1541 {
robertphillips9f1c2412014-06-09 06:25:34 -07001542 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001543 canvas->clipPath(path, SkRegion::kIntersect_Op);
1544 canvas->clipPath(path2, SkRegion::kXOR_Op);
1545 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1546 REPORTER_ASSERT(reporter, true == nonEmpty);
1547 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
1548 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
1549 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1550 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1551 }
1552}
1553
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001554/**
1555 * A canvas that records the number of clip commands.
1556 */
1557class ClipCountingCanvas : public SkCanvas {
1558public:
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001559 ClipCountingCanvas(int width, int height)
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001560 : INHERITED(width, height)
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001561 , fClipCount(0){
1562 }
1563
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001564 virtual void onClipRect(const SkRect& r,
1565 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001566 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001567 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001568 this->INHERITED::onClipRect(r, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001569 }
1570
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001571 virtual void onClipRRect(const SkRRect& rrect,
1572 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001573 ClipEdgeStyle edgeStyle)SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001574 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001575 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001576 }
1577
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001578 virtual void onClipPath(const SkPath& path,
1579 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001580 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001581 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001582 this->INHERITED::onClipPath(path, op, edgeStyle);
1583 }
1584
1585 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE {
1586 fClipCount += 1;
1587 this->INHERITED::onClipRegion(deviceRgn, op);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001588 }
1589
1590 unsigned getClipCount() const { return fClipCount; }
1591
1592private:
1593 unsigned fClipCount;
1594
1595 typedef SkCanvas INHERITED;
1596};
1597
1598static void test_clip_expansion(skiatest::Reporter* reporter) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001599 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001600 SkCanvas* canvas = recorder.beginRecording(10, 10);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001601
1602 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
1603 // The following expanding clip should not be skipped.
1604 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
1605 // Draw something so the optimizer doesn't just fold the world.
1606 SkPaint p;
1607 p.setColor(SK_ColorBLUE);
1608 canvas->drawPaint(p);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001609 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001610
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001611 ClipCountingCanvas testCanvas(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001612 picture->draw(&testCanvas);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001613
1614 // Both clips should be present on playback.
1615 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
1616}
1617
tomhudson@google.com381010e2013-10-24 11:12:47 +00001618static void test_hierarchical(skiatest::Reporter* reporter) {
1619 SkBitmap bm;
1620 make_bm(&bm, 10, 10, SK_ColorRED, true);
1621
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001622 SkPictureRecorder recorder;
tomhudson@google.com381010e2013-10-24 11:12:47 +00001623
robertphillips9f1c2412014-06-09 06:25:34 -07001624 recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001625 SkAutoTUnref<SkPicture> childPlain(recorder.endRecording());
1626 REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0
tomhudson@google.com381010e2013-10-24 11:12:47 +00001627
robertphillips9f1c2412014-06-09 06:25:34 -07001628 recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001629 SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording());
1630 REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1
tomhudson@google.com381010e2013-10-24 11:12:47 +00001631
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001632 {
robertphillips9f1c2412014-06-09 06:25:34 -07001633 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips9b14f262014-06-04 05:40:44 -07001634 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001635 SkAutoTUnref<SkPicture> parentPP(recorder.endRecording());
1636 REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0
1637 }
1638 {
robertphillips9f1c2412014-06-09 06:25:34 -07001639 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips9b14f262014-06-04 05:40:44 -07001640 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001641 SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording());
1642 REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1
1643 }
1644 {
robertphillips9f1c2412014-06-09 06:25:34 -07001645 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001646 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001647 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001648 SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording());
1649 REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1
1650 }
1651 {
robertphillips9f1c2412014-06-09 06:25:34 -07001652 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001653 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001654 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001655 SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording());
1656 REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2
1657 }
tomhudson@google.com381010e2013-10-24 11:12:47 +00001658}
1659
robertphillips@google.comd5500882014-04-02 23:51:13 +00001660static void test_gen_id(skiatest::Reporter* reporter) {
1661
Robert Phillipscfaeec42014-07-13 12:00:50 -04001662 SkPictureRecorder recorder;
1663 recorder.beginRecording(0, 0);
1664 SkAutoTUnref<SkPicture> empty(recorder.endRecording());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001665
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001666 // Empty pictures should still have a valid ID
Robert Phillipscfaeec42014-07-13 12:00:50 -04001667 REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001668
robertphillips9f1c2412014-06-09 06:25:34 -07001669 SkCanvas* canvas = recorder.beginRecording(1, 1);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001670 canvas->drawARGB(255, 255, 255, 255);
1671 SkAutoTUnref<SkPicture> hasData(recorder.endRecording());
1672 // picture should have a non-zero id after recording
1673 REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001674
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001675 // both pictures should have different ids
Robert Phillipscfaeec42014-07-13 12:00:50 -04001676 REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001677}
1678
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00001679DEF_TEST(Picture, reporter) {
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001680#ifdef SK_DEBUG
robertphillipsdb539902014-07-01 08:47:04 -07001681 test_deleting_empty_picture();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001682 test_serializing_empty_picture();
scroggo@google.com4b90b112012-12-04 15:08:56 +00001683#else
1684 test_bad_bitmap();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001685#endif
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001686 test_unbalanced_save_restores(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001687 test_peephole();
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001688#if SK_SUPPORT_GPU
tomhudson3a0f2792014-08-20 05:29:41 -07001689 test_gpu_veto(reporter, false);
1690 test_gpu_veto(reporter, true);
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001691#endif
mtkleinc551d9f2014-08-20 08:09:46 -07001692 test_has_text(reporter, false);
1693 test_has_text(reporter, true);
tomhudson3a0f2792014-08-20 05:29:41 -07001694 test_analysis(reporter, false);
1695 test_analysis(reporter, true);
reed@google.comfe7b1ed2012-11-29 21:00:39 +00001696 test_gatherpixelrefs(reporter);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +00001697 test_gatherpixelrefsandrects(reporter);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001698 test_bitmap_with_encoded_data(reporter);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001699 test_draw_empty(reporter);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001700 test_clip_bound_opt(reporter);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001701 test_clip_expansion(reporter);
tomhudson@google.com381010e2013-10-24 11:12:47 +00001702 test_hierarchical(reporter);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001703 test_gen_id(reporter);
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001704}
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001705
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001706#if SK_SUPPORT_GPU
1707DEF_GPUTEST(GPUPicture, reporter, factory) {
1708 test_gpu_picture_optimization(reporter, factory);
1709}
1710#endif
1711
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001712static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) {
1713 const SkPaint paint;
1714 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f };
1715 const SkIRect irect = { 2, 2, 3, 3 };
1716
1717 // Don't care what these record, as long as they're legal.
1718 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint);
1719 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag);
1720 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint);
1721 canvas->drawBitmapNine(bitmap, irect, rect, &paint);
1722 canvas->drawSprite(bitmap, 1, 1);
1723}
1724
1725static void test_draw_bitmaps(SkCanvas* canvas) {
1726 SkBitmap empty;
1727 draw_bitmaps(empty, canvas);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001728 empty.setInfo(SkImageInfo::MakeN32Premul(10, 10));
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001729 draw_bitmaps(empty, canvas);
1730}
1731
1732DEF_TEST(Picture_EmptyBitmap, r) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001733 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001734 test_draw_bitmaps(recorder.beginRecording(10, 10));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001735 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001736}
1737
1738DEF_TEST(Canvas_EmptyBitmap, r) {
1739 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001740 dst.allocN32Pixels(10, 10);
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001741 SkCanvas canvas(dst);
1742
1743 test_draw_bitmaps(&canvas);
1744}
dneto3f22e8c2014-07-30 15:42:22 -07001745
1746DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) {
1747 // This test is from crbug.com/344987.
1748 // The commands are:
1749 // saveLayer with paint that modifies alpha
1750 // drawBitmapRectToRect
1751 // drawBitmapRectToRect
1752 // restore
1753 // The bug was that this structure was modified so that:
1754 // - The saveLayer and restore were eliminated
1755 // - The alpha was only applied to the first drawBitmapRectToRect
1756
1757 // This test draws blue and red squares inside a 50% transparent
1758 // layer. Both colours should show up muted.
1759 // When the bug is present, the red square (the second bitmap)
1760 // shows upwith full opacity.
1761
1762 SkBitmap blueBM;
1763 make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true);
1764 SkBitmap redBM;
1765 make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true);
1766 SkPaint semiTransparent;
1767 semiTransparent.setAlpha(0x80);
1768
1769 SkPictureRecorder recorder;
1770 SkCanvas* canvas = recorder.beginRecording(100, 100);
1771 canvas->drawARGB(0, 0, 0, 0);
1772
1773 canvas->saveLayer(0, &semiTransparent);
1774 canvas->drawBitmap(blueBM, 25, 25);
1775 canvas->drawBitmap(redBM, 50, 50);
1776 canvas->restore();
1777
1778 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1779
1780 // Now replay the picture back on another canvas
1781 // and check a couple of its pixels.
1782 SkBitmap replayBM;
1783 make_bm(&replayBM, 100, 100, SK_ColorBLACK, false);
1784 SkCanvas replayCanvas(replayBM);
1785 picture->draw(&replayCanvas);
1786 replayCanvas.flush();
1787
1788 // With the bug present, at (55, 55) we would get a fully opaque red
1789 // intead of a dark red.
1790 REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080);
1791 REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000);
1792}
mtklein3e8232b2014-08-18 13:39:11 -07001793
1794struct CountingBBH : public SkBBoxHierarchy {
1795 mutable int searchCalls;
1796
1797 CountingBBH() : searchCalls(0) {}
1798
1799 virtual void search(const SkIRect& query, SkTDArray<void*>* results) const {
1800 this->searchCalls++;
1801 }
1802
1803 // All other methods unimplemented.
1804 virtual void insert(void* data, const SkIRect& bounds, bool defer) {}
1805 virtual void flushDeferredInserts() {}
1806 virtual void clear() {}
1807 virtual int getCount() const { return 0; }
1808 virtual int getDepth() const { return 0; }
1809 virtual void rewindInserts() {}
1810};
1811
1812class SpoonFedBBHFactory : public SkBBHFactory {
1813public:
1814 explicit SpoonFedBBHFactory(SkBBoxHierarchy* bbh) : fBBH(bbh) {}
1815 virtual SkBBoxHierarchy* operator()(int width, int height) const {
1816 return SkRef(fBBH);
1817 }
1818private:
1819 SkBBoxHierarchy* fBBH;
1820};
1821
1822// When the canvas clip covers the full picture, we don't need to call the BBH.
1823DEF_TEST(Picture_SkipBBH, r) {
1824 CountingBBH bbh;
1825 SpoonFedBBHFactory factory(&bbh);
1826
1827 SkPictureRecorder recorder;
1828 recorder.beginRecording(320, 240, &factory);
1829 SkAutoTUnref<const SkPicture> picture(recorder.endRecording());
1830
1831 SkCanvas big(640, 480), small(300, 200);
1832
1833 picture->draw(&big);
1834 REPORTER_ASSERT(r, bbh.searchCalls == 0);
1835
1836 picture->draw(&small);
1837 REPORTER_ASSERT(r, bbh.searchCalls == 1);
1838}