blob: 61cc5021fc7b69a89d2ec546e1ab9f75d30c222c [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
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000581static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) {
582 const int IW = 32;
583 const int IH = IW;
584 const SkScalar W = SkIntToScalar(IW);
585 const SkScalar H = W;
586
587 static const int N = 4;
588 SkBitmap bm[2*N];
589 SkPixelRef* refs[2*N];
590 SkTDArray<SkPixelRef*> analytic[N];
591
592 const SkPoint pos[N] = {
593 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
594 };
595
596 create_textures(bm, refs, N, IW, IH);
597
598 SkRandom rand;
599 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000600 SkAutoTUnref<SkPicture> pic(
601 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000602
603 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
604
605 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont(
606 new SkPictureUtils::SkPixelRefsAndRectsList);
607
608 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont);
609
610 // quick check for a small piece of each quadrant, which should just
611 // contain 1 or 2 bitmaps.
612 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
613 SkRect r;
614 r.set(2, 2, W - 2, H - 2);
615 r.offset(pos[i].fX, pos[i].fY);
616
617 SkTDArray<SkPixelRef*> gatheredRefs;
618 prCont->query(r, &gatheredRefs);
619
620 int count = gatheredRefs.count();
621 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
622 if (1 == count) {
623 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
624 } else if (2 == count) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000625 REPORTER_ASSERT(reporter,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000626 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
627 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
628 }
629 }
630
631 SkBitmap image;
632 draw(pic, 2*IW, 2*IH, &image);
633
634 // Test a bunch of random (mostly) rects, and compare the gather results
635 // with the analytic results and the pixel refs seen in a rendering.
636 for (int j = 0; j < 100; ++j) {
637 SkRect r;
638 rand_rect(&r, rand, 2*W, 2*H);
639
640 SkTDArray<SkPixelRef*> fromImage;
641 gather_from_image(image, refs, N, &fromImage, r);
642
643 SkTDArray<SkPixelRef*> fromAnalytic;
644 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
645
646 SkTDArray<SkPixelRef*> gatheredRefs;
647 prCont->query(r, &gatheredRefs);
648
649 // Everything that we saw drawn should appear in the analytic list
650 // but the analytic list may contain some pixelRefs that were not
651 // seen in the image (e.g., A8 textures used as masks)
652 for (int i = 0; i < fromImage.count(); ++i) {
653 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]));
654 }
655
656 // Everything in the analytic list should appear in the gathered
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000657 // list.
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000658 for (int i = 0; i < fromAnalytic.count(); ++i) {
659 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i]));
660 }
661 }
662 }
663}
664
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000665#ifdef SK_DEBUG
mtklein3e8232b2014-08-18 13:39:11 -0700666// Ensure that deleting an empty SkPicture does not assert. Asserts only fire
robertphillipsdb539902014-07-01 08:47:04 -0700667// in debug mode, so only run in debug mode.
668static void test_deleting_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000669 SkPictureRecorder recorder;
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000670 // Creates an SkPictureRecord
robertphillips9f1c2412014-06-09 06:25:34 -0700671 recorder.beginRecording(0, 0);
robertphillipsdb539902014-07-01 08:47:04 -0700672 // Turns that into an SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000673 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
robertphillipsdb539902014-07-01 08:47:04 -0700674 // Ceates a new SkPictureRecord
robertphillips9f1c2412014-06-09 06:25:34 -0700675 recorder.beginRecording(0, 0);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000676}
677
678// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
679static void test_serializing_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000680 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -0700681 recorder.beginRecording(0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000682 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000683 SkDynamicMemoryWStream stream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000684 picture->serialize(&stream);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000685}
686#endif
687
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000688static void rand_op(SkCanvas* canvas, SkRandom& rand) {
reed@google.com21b519d2012-10-02 17:42:15 +0000689 SkPaint paint;
690 SkRect rect = SkRect::MakeWH(50, 50);
691
692 SkScalar unit = rand.nextUScalar1();
693 if (unit <= 0.3) {
694// SkDebugf("save\n");
695 canvas->save();
696 } else if (unit <= 0.6) {
697// SkDebugf("restore\n");
698 canvas->restore();
699 } else if (unit <= 0.9) {
700// SkDebugf("clip\n");
701 canvas->clipRect(rect);
702 } else {
703// SkDebugf("draw\n");
704 canvas->drawPaint(paint);
705 }
706}
707
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000708#if SK_SUPPORT_GPU
Mike Klein27dc17c2014-08-18 18:35:16 -0400709static void test_gpu_veto(skiatest::Reporter* reporter) {
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000710
711 SkPictureRecorder recorder;
712
Mike Klein27dc17c2014-08-18 18:35:16 -0400713 SkCanvas* canvas = recorder.beginRecording(100, 100);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000714 {
715 SkPath path;
716 path.moveTo(0, 0);
717 path.lineTo(50, 50);
718
719 SkScalar intervals[] = { 1.0f, 1.0f };
720 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0));
721
722 SkPaint paint;
723 paint.setStyle(SkPaint::kStroke_Style);
724 paint.setPathEffect(dash);
725
726 canvas->drawPath(path, paint);
727 }
728 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
729 // path effects currently render an SkPicture undesireable for GPU rendering
commit-bot@chromium.orga1ff26a2014-05-30 21:52:52 +0000730
731 const char *reason = NULL;
732 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL, &reason));
733 REPORTER_ASSERT(reporter, NULL != reason);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000734
Mike Klein27dc17c2014-08-18 18:35:16 -0400735 canvas = recorder.beginRecording(100, 100);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000736 {
737 SkPath path;
738
739 path.moveTo(0, 0);
740 path.lineTo(0, 50);
741 path.lineTo(25, 25);
742 path.lineTo(50, 50);
743 path.lineTo(50, 0);
744 path.close();
745 REPORTER_ASSERT(reporter, !path.isConvex());
746
747 SkPaint paint;
748 paint.setAntiAlias(true);
749 for (int i = 0; i < 50; ++i) {
750 canvas->drawPath(path, paint);
751 }
752 }
753 picture.reset(recorder.endRecording());
754 // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering
755 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
756
Mike Klein27dc17c2014-08-18 18:35:16 -0400757 canvas = recorder.beginRecording(100, 100);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000758 {
759 SkPath path;
760
761 path.moveTo(0, 0);
762 path.lineTo(0, 50);
763 path.lineTo(25, 25);
764 path.lineTo(50, 50);
765 path.lineTo(50, 0);
766 path.close();
767 REPORTER_ASSERT(reporter, !path.isConvex());
768
769 SkPaint paint;
770 paint.setAntiAlias(true);
771 paint.setStyle(SkPaint::kStroke_Style);
772 paint.setStrokeWidth(0);
773 for (int i = 0; i < 50; ++i) {
774 canvas->drawPath(path, paint);
775 }
776 }
777 picture.reset(recorder.endRecording());
778 // hairline stroked AA concave paths are fine for GPU rendering
779 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL));
780}
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000781
782static void test_gpu_picture_optimization(skiatest::Reporter* reporter,
783 GrContextFactory* factory) {
bsalomone904c092014-07-17 10:50:59 -0700784 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
785 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000786
bsalomone904c092014-07-17 10:50:59 -0700787 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
788 continue;
789 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000790
bsalomone904c092014-07-17 10:50:59 -0700791 GrContext* context = factory->get(glCtxType);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000792
bsalomone904c092014-07-17 10:50:59 -0700793 if (NULL == context) {
794 continue;
795 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000796
bsalomone904c092014-07-17 10:50:59 -0700797 static const int kWidth = 100;
798 static const int kHeight = 100;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000799
bsalomone904c092014-07-17 10:50:59 -0700800 SkAutoTUnref<SkPicture> pict;
801
802 // create a picture with the structure:
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000803 // 1)
bsalomone904c092014-07-17 10:50:59 -0700804 // SaveLayer
805 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000806 // 2)
bsalomone904c092014-07-17 10:50:59 -0700807 // SaveLayer
808 // Translate
809 // SaveLayer w/ bound
810 // Restore
811 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000812 // 3)
bsalomone904c092014-07-17 10:50:59 -0700813 // SaveLayer w/ copyable paint
814 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000815 // 4)
bsalomone904c092014-07-17 10:50:59 -0700816 // SaveLayer w/ non-copyable paint
817 // Restore
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000818 {
bsalomone904c092014-07-17 10:50:59 -0700819 SkPictureRecorder recorder;
820
821 SkCanvas* c = recorder.beginRecording(kWidth, kHeight);
822 // 1)
823 c->saveLayer(NULL, NULL);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000824 c->restore();
bsalomone904c092014-07-17 10:50:59 -0700825
826 // 2)
827 c->saveLayer(NULL, NULL);
828 c->translate(kWidth/2, kHeight/2);
829 SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2);
830 c->saveLayer(&r, NULL);
831 c->restore();
832 c->restore();
833
834 // 3)
835 {
836 SkPaint p;
837 p.setColor(SK_ColorRED);
838 c->saveLayer(NULL, &p);
839 c->restore();
840 }
841 // 4)
842 // TODO: this case will need to be removed once the paint's are immutable
843 {
844 SkPaint p;
845 SkAutoTUnref<SkColorFilter> cf(SkLumaColorFilter::Create());
846 p.setImageFilter(SkColorFilterImageFilter::Create(cf.get()))->unref();
847 c->saveLayer(NULL, &p);
848 c->restore();
849 }
850
851 pict.reset(recorder.endRecording());
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000852 }
853
bsalomone904c092014-07-17 10:50:59 -0700854 // Now test out the SaveLayer extraction
855 {
856 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000857
bsalomone904c092014-07-17 10:50:59 -0700858 SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info));
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000859
bsalomone904c092014-07-17 10:50:59 -0700860 SkCanvas* canvas = surface->getCanvas();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000861
bsalomone904c092014-07-17 10:50:59 -0700862 canvas->EXPERIMENTAL_optimize(pict);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000863
robertphillips6617d502014-08-18 09:39:34 -0700864 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000865
bsalomone904c092014-07-17 10:50:59 -0700866 const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key);
867 REPORTER_ASSERT(reporter, NULL != data);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000868
robertphillips6617d502014-08-18 09:39:34 -0700869 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data);
bsalomone904c092014-07-17 10:50:59 -0700870 REPORTER_ASSERT(reporter, 5 == gpuData->numSaveLayers());
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000871
robertphillips6617d502014-08-18 09:39:34 -0700872 const GrAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0);
bsalomone904c092014-07-17 10:50:59 -0700873 // The parent/child layer appear in reverse order
robertphillips6617d502014-08-18 09:39:34 -0700874 const GrAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2);
875 const GrAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1);
876 const GrAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3);
877// const GrAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(4);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000878
bsalomone904c092014-07-17 10:50:59 -0700879 REPORTER_ASSERT(reporter, info0.fValid);
880 REPORTER_ASSERT(reporter, kWidth == info0.fSize.fWidth && kHeight == info0.fSize.fHeight);
881 REPORTER_ASSERT(reporter, info0.fCTM.isIdentity());
882 REPORTER_ASSERT(reporter, 0 == info0.fOffset.fX && 0 == info0.fOffset.fY);
883 REPORTER_ASSERT(reporter, NULL != info0.fPaint);
884 REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000885
bsalomone904c092014-07-17 10:50:59 -0700886 REPORTER_ASSERT(reporter, info1.fValid);
887 REPORTER_ASSERT(reporter, kWidth == info1.fSize.fWidth && kHeight == info1.fSize.fHeight);
888 REPORTER_ASSERT(reporter, info1.fCTM.isIdentity());
889 REPORTER_ASSERT(reporter, 0 == info1.fOffset.fX && 0 == info1.fOffset.fY);
890 REPORTER_ASSERT(reporter, NULL != info1.fPaint);
891 REPORTER_ASSERT(reporter, !info1.fIsNested && info1.fHasNestedLayers); // has a nested SL
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000892
bsalomone904c092014-07-17 10:50:59 -0700893 REPORTER_ASSERT(reporter, info2.fValid);
894 REPORTER_ASSERT(reporter, kWidth/2 == info2.fSize.fWidth &&
895 kHeight/2 == info2.fSize.fHeight); // bound reduces size
896 REPORTER_ASSERT(reporter, info2.fCTM.isIdentity()); // translated
897 REPORTER_ASSERT(reporter, kWidth/2 == info2.fOffset.fX &&
898 kHeight/2 == info2.fOffset.fY);
899 REPORTER_ASSERT(reporter, NULL != info1.fPaint);
900 REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000901
bsalomone904c092014-07-17 10:50:59 -0700902 REPORTER_ASSERT(reporter, info3.fValid);
903 REPORTER_ASSERT(reporter, kWidth == info3.fSize.fWidth && kHeight == info3.fSize.fHeight);
904 REPORTER_ASSERT(reporter, info3.fCTM.isIdentity());
905 REPORTER_ASSERT(reporter, 0 == info3.fOffset.fX && 0 == info3.fOffset.fY);
906 REPORTER_ASSERT(reporter, NULL != info3.fPaint);
907 REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000908
bsalomone904c092014-07-17 10:50:59 -0700909 #if 0 // needs more though for GrGatherCanvas
910 REPORTER_ASSERT(reporter, !info4.fValid); // paint is/was uncopyable
911 REPORTER_ASSERT(reporter, kWidth == info4.fSize.fWidth && kHeight == info4.fSize.fHeight);
912 REPORTER_ASSERT(reporter, 0 == info4.fOffset.fX && 0 == info4.fOffset.fY);
913 REPORTER_ASSERT(reporter, info4.fCTM.isIdentity());
914 REPORTER_ASSERT(reporter, NULL == info4.fPaint); // paint is/was uncopyable
915 REPORTER_ASSERT(reporter, !info4.fIsNested && !info4.fHasNestedLayers);
916 #endif
917 }
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000918 }
919}
920
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000921#endif
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000922
ajuma750ae262014-08-18 12:59:55 -0700923static void test_has_text(skiatest::Reporter* reporter) {
924 SkPictureRecorder recorder;
925 SkPaint paint;
926 paint.setColor(SK_ColorBLUE);
927 SkPoint point = SkPoint::Make(10, 10);
928
929 SkCanvas* canvas = recorder.beginRecording(100, 100);
930 {
931 canvas->drawRect(SkRect::MakeWH(20, 20), paint);
932 }
933 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
934 REPORTER_ASSERT(reporter, !picture->hasText());
935
936 canvas = recorder.beginRecording(100, 100);
937 {
938 canvas->drawText("Q", 1, point.fX, point.fY, paint);
939 }
940 picture.reset(recorder.endRecording());
941 REPORTER_ASSERT(reporter, picture->hasText());
942
943 canvas = recorder.beginRecording(100, 100);
944 {
945 canvas->drawPosText("Q", 1, &point, paint);
946 }
947 picture.reset(recorder.endRecording());
948 REPORTER_ASSERT(reporter, picture->hasText());
949
950 canvas = recorder.beginRecording(100, 100);
951 {
952 canvas->drawPosTextH("Q", 1, &point.fX, point.fY, paint);
953 }
954 picture.reset(recorder.endRecording());
955 REPORTER_ASSERT(reporter, picture->hasText());
956
957 canvas = recorder.beginRecording(100, 100);
958 {
959 SkPath path;
960 path.moveTo(0, 0);
961 path.lineTo(50, 50);
962
963 canvas->drawTextOnPathHV("Q", 1, path, point.fX, point.fY, paint);
964 }
965 picture.reset(recorder.endRecording());
966 REPORTER_ASSERT(reporter, picture->hasText());
967
968 canvas = recorder.beginRecording(100, 100);
969 {
970 SkPath path;
971 path.moveTo(0, 0);
972 path.lineTo(50, 50);
973
974 canvas->drawTextOnPath("Q", 1, path, NULL, paint);
975 }
976 picture.reset(recorder.endRecording());
977 REPORTER_ASSERT(reporter, picture->hasText());
978}
979
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000980static void set_canvas_to_save_count_4(SkCanvas* canvas) {
981 canvas->restoreToCount(1);
982 canvas->save();
983 canvas->save();
984 canvas->save();
985}
986
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +0000987/**
988 * A canvas that records the number of saves, saveLayers and restores.
989 */
990class SaveCountingCanvas : public SkCanvas {
991public:
992 SaveCountingCanvas(int width, int height)
993 : INHERITED(width, height)
994 , fSaveCount(0)
995 , fSaveLayerCount(0)
996 , fRestoreCount(0){
997 }
998
999 virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint,
1000 SaveFlags flags) SK_OVERRIDE {
1001 ++fSaveLayerCount;
1002 return this->INHERITED::willSaveLayer(bounds, paint, flags);
1003 }
1004
Florin Malita5f6102d2014-06-30 10:13:28 -04001005 virtual void willSave() SK_OVERRIDE {
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001006 ++fSaveCount;
Florin Malita5f6102d2014-06-30 10:13:28 -04001007 this->INHERITED::willSave();
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001008 }
1009
1010 virtual void willRestore() SK_OVERRIDE {
1011 ++fRestoreCount;
1012 this->INHERITED::willRestore();
1013 }
1014
1015 unsigned int getSaveCount() const { return fSaveCount; }
1016 unsigned int getSaveLayerCount() const { return fSaveLayerCount; }
1017 unsigned int getRestoreCount() const { return fRestoreCount; }
1018
1019private:
1020 unsigned int fSaveCount;
1021 unsigned int fSaveLayerCount;
1022 unsigned int fRestoreCount;
1023
1024 typedef SkCanvas INHERITED;
1025};
1026
skia.committer@gmail.com8e7d37d2014-05-28 03:06:06 +00001027void check_save_state(skiatest::Reporter* reporter, SkPicture* picture,
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001028 unsigned int numSaves, unsigned int numSaveLayers,
1029 unsigned int numRestores) {
1030 SaveCountingCanvas canvas(picture->width(), picture->height());
1031
1032 picture->draw(&canvas);
1033
1034 REPORTER_ASSERT(reporter, numSaves == canvas.getSaveCount());
1035 REPORTER_ASSERT(reporter, numSaveLayers == canvas.getSaveLayerCount());
1036 REPORTER_ASSERT(reporter, numRestores == canvas.getRestoreCount());
1037}
1038
1039// This class exists so SkPicture can friend it and give it access to
1040// the 'partialReplay' method.
1041class SkPictureRecorderReplayTester {
1042public:
1043 static SkPicture* Copy(SkPictureRecorder* recorder) {
1044 SkPictureRecorder recorder2;
1045
robertphillips9f1c2412014-06-09 06:25:34 -07001046 SkCanvas* canvas = recorder2.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001047
1048 recorder->partialReplay(canvas);
1049
1050 return recorder2.endRecording();
1051 }
1052};
1053
robertphillips9058d602014-06-10 11:45:46 -07001054static void create_imbalance(SkCanvas* canvas) {
1055 SkRect clipRect = SkRect::MakeWH(2, 2);
1056 SkRect drawRect = SkRect::MakeWH(10, 10);
1057 canvas->save();
1058 canvas->clipRect(clipRect, SkRegion::kReplace_Op);
1059 canvas->translate(1.0f, 1.0f);
1060 SkPaint p;
1061 p.setColor(SK_ColorGREEN);
1062 canvas->drawRect(drawRect, p);
1063 // no restore
1064}
1065
1066// This tests that replaying a potentially unbalanced picture into a canvas
1067// doesn't affect the canvas' save count or matrix/clip state.
1068static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) {
1069 SkBitmap bm;
1070 bm.allocN32Pixels(4, 3);
1071 SkCanvas canvas(bm);
1072
1073 int beforeSaveCount = canvas.getSaveCount();
1074
1075 SkMatrix beforeMatrix = canvas.getTotalMatrix();
1076
1077 SkRect beforeClip;
1078
1079 canvas.getClipBounds(&beforeClip);
1080
1081 canvas.drawPicture(picture);
1082
1083 REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount());
1084 REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix());
1085
1086 SkRect afterClip;
1087
1088 canvas.getClipBounds(&afterClip);
1089
1090 REPORTER_ASSERT(reporter, afterClip == beforeClip);
1091}
1092
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001093// Test out SkPictureRecorder::partialReplay
1094DEF_TEST(PictureRecorder_replay, reporter) {
1095 // check save/saveLayer state
1096 {
1097 SkPictureRecorder recorder;
1098
robertphillips9f1c2412014-06-09 06:25:34 -07001099 SkCanvas* canvas = recorder.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001100
1101 canvas->saveLayer(NULL, NULL);
1102
1103 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1104
1105 // The extra save and restore comes from the Copy process.
1106 check_save_state(reporter, copy, 2, 1, 3);
1107
1108 canvas->saveLayer(NULL, NULL);
1109
1110 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1111
1112 check_save_state(reporter, final, 1, 2, 3);
1113
1114 // The copy shouldn't pick up any operations added after it was made
1115 check_save_state(reporter, copy, 2, 1, 3);
1116 }
1117
1118 // (partially) check leakage of draw ops
1119 {
1120 SkPictureRecorder recorder;
1121
robertphillips9f1c2412014-06-09 06:25:34 -07001122 SkCanvas* canvas = recorder.beginRecording(10, 10);
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001123
1124 SkRect r = SkRect::MakeWH(5, 5);
1125 SkPaint p;
1126
1127 canvas->drawRect(r, p);
1128
1129 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1130
1131 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1132
1133 SkBitmap bm;
1134 make_bm(&bm, 10, 10, SK_ColorRED, true);
1135
1136 r.offset(5.0f, 5.0f);
1137 canvas->drawBitmapRectToRect(bm, NULL, r);
1138
1139 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1140 REPORTER_ASSERT(reporter, final->willPlayBackBitmaps());
1141
1142 REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID());
1143
1144 // The snapshot shouldn't pick up any operations added after it was made
1145 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1146 }
robertphillips9058d602014-06-10 11:45:46 -07001147
1148 // Recreate the Android partialReplay test case
1149 {
1150 SkPictureRecorder recorder;
1151
1152 SkCanvas* canvas = recorder.beginRecording(4, 3, NULL, 0);
1153 create_imbalance(canvas);
1154
1155 int expectedSaveCount = canvas->getSaveCount();
1156
1157 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1158 check_balance(reporter, copy);
1159
1160 REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount());
1161
1162 // End the recording of source to test the picture finalization
1163 // process isn't complicated by the partialReplay step
1164 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1165 }
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001166}
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001167
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001168static void test_unbalanced_save_restores(skiatest::Reporter* reporter) {
1169 SkCanvas testCanvas(100, 100);
1170 set_canvas_to_save_count_4(&testCanvas);
1171
1172 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1173
1174 SkPaint paint;
1175 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000);
1176
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001177 SkPictureRecorder recorder;
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001178
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001179 {
1180 // Create picture with 2 unbalanced saves
robertphillips9f1c2412014-06-09 06:25:34 -07001181 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001182 canvas->save();
1183 canvas->translate(10, 10);
1184 canvas->drawRect(rect, paint);
1185 canvas->save();
1186 canvas->translate(10, 10);
1187 canvas->drawRect(rect, paint);
1188 SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording());
1189
robertphillips9b14f262014-06-04 05:40:44 -07001190 testCanvas.drawPicture(extraSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001191 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1192 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001193
1194 set_canvas_to_save_count_4(&testCanvas);
1195
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001196 {
1197 // Create picture with 2 unbalanced restores
robertphillips9f1c2412014-06-09 06:25:34 -07001198 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001199 canvas->save();
1200 canvas->translate(10, 10);
1201 canvas->drawRect(rect, paint);
1202 canvas->save();
1203 canvas->translate(10, 10);
1204 canvas->drawRect(rect, paint);
1205 canvas->restore();
1206 canvas->restore();
1207 canvas->restore();
1208 canvas->restore();
1209 SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording());
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001210
robertphillips9b14f262014-06-04 05:40:44 -07001211 testCanvas.drawPicture(extraRestorePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001212 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1213 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001214
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001215 set_canvas_to_save_count_4(&testCanvas);
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001216
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001217 {
robertphillips9f1c2412014-06-09 06:25:34 -07001218 SkCanvas* canvas = recorder.beginRecording(100, 100);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001219 canvas->translate(10, 10);
1220 canvas->drawRect(rect, paint);
1221 SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording());
1222
robertphillips9b14f262014-06-04 05:40:44 -07001223 testCanvas.drawPicture(noSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001224 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1225 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity());
1226 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001227}
1228
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001229static void test_peephole() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001230 SkRandom rand;
reed@google.com21b519d2012-10-02 17:42:15 +00001231
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001232 SkPictureRecorder recorder;
1233
reed@google.com21b519d2012-10-02 17:42:15 +00001234 for (int j = 0; j < 100; j++) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001235 SkRandom rand2(rand); // remember the seed
reed@google.com21b519d2012-10-02 17:42:15 +00001236
robertphillips9f1c2412014-06-09 06:25:34 -07001237 SkCanvas* canvas = recorder.beginRecording(100, 100);
reed@google.com21b519d2012-10-02 17:42:15 +00001238
1239 for (int i = 0; i < 1000; ++i) {
1240 rand_op(canvas, rand);
1241 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001242 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
jvanverth@google.comc490f802013-03-04 13:56:38 +00001243
1244 rand = rand2;
reed@google.com21b519d2012-10-02 17:42:15 +00001245 }
1246
1247 {
robertphillips9f1c2412014-06-09 06:25:34 -07001248 SkCanvas* canvas = recorder.beginRecording(100, 100);
reed@google.com21b519d2012-10-02 17:42:15 +00001249 SkRect rect = SkRect::MakeWH(50, 50);
skia.committer@gmail.com52c24372012-10-03 02:01:13 +00001250
reed@google.com21b519d2012-10-02 17:42:15 +00001251 for (int i = 0; i < 100; ++i) {
1252 canvas->save();
1253 }
1254 while (canvas->getSaveCount() > 1) {
1255 canvas->clipRect(rect);
1256 canvas->restore();
1257 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001258 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
reed@google.com21b519d2012-10-02 17:42:15 +00001259 }
1260}
1261
scroggo@google.com4b90b112012-12-04 15:08:56 +00001262#ifndef SK_DEBUG
1263// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
1264// should never do this.
1265static void test_bad_bitmap() {
1266 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
1267 // fail.
1268 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001269 bm.setInfo(SkImageInfo::MakeN32Premul(100, 100));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001270 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001271 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001272 recordingCanvas->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001273 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.com4b90b112012-12-04 15:08:56 +00001274
1275 SkCanvas canvas;
robertphillips9b14f262014-06-04 05:40:44 -07001276 canvas.drawPicture(picture);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001277}
1278#endif
1279
reed@google.com672588b2014-01-08 15:42:01 +00001280static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001281 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001282}
1283
1284static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001285 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001286 SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height());
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001287 canvas->drawBitmap(bitmap, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001288 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1289
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001290 SkDynamicMemoryWStream wStream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001291 picture->serialize(&wStream, &encode_bitmap_to_data);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001292 return wStream.copyToData();
1293}
1294
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001295struct ErrorContext {
1296 int fErrors;
1297 skiatest::Reporter* fReporter;
1298};
1299
1300static void assert_one_parse_error_cb(SkError error, void* context) {
1301 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
1302 errorContext->fErrors++;
1303 // This test only expects one error, and that is a kParseError. If there are others,
1304 // there is some unknown problem.
1305 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors,
1306 "This threw more errors than expected.");
1307 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error,
1308 SkGetLastErrorString());
1309}
1310
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001311static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
1312 // Create a bitmap that will be encoded.
1313 SkBitmap original;
1314 make_bm(&original, 100, 100, SK_ColorBLUE, true);
1315 SkDynamicMemoryWStream wStream;
1316 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
1317 return;
1318 }
1319 SkAutoDataUnref data(wStream.copyToData());
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001320
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001321 SkBitmap bm;
halcanary@google.com3d50ea12014-01-02 13:15:13 +00001322 bool installSuccess = SkInstallDiscardablePixelRef(
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +00001323 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm);
reed@google.combf790232013-12-13 19:45:58 +00001324 REPORTER_ASSERT(reporter, installSuccess);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001325
1326 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
1327 // Flattening original will follow the old path of performing an encode, while flattening bm
1328 // will use the already encoded data.
1329 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
1330 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
1331 REPORTER_ASSERT(reporter, picture1->equals(picture2));
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001332 // Now test that a parse error was generated when trying to create a new SkPicture without
1333 // providing a function to decode the bitmap.
1334 ErrorContext context;
1335 context.fErrors = 0;
1336 context.fReporter = reporter;
1337 SkSetErrorCallback(assert_one_parse_error_cb, &context);
1338 SkMemoryStream pictureStream(picture1);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001339 SkClearLastError();
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001340 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL));
1341 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001342 SkClearLastError();
1343 SkSetErrorCallback(NULL, NULL);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001344}
1345
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001346static void test_draw_empty(skiatest::Reporter* reporter) {
1347 SkBitmap result;
1348 make_bm(&result, 2, 2, SK_ColorBLACK, false);
1349
1350 SkCanvas canvas(result);
1351
1352 {
1353 // stock SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001354 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001355 recorder.beginRecording(1, 1);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001356 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001357
robertphillips9b14f262014-06-04 05:40:44 -07001358 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001359 }
1360
1361 {
1362 // tile grid
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001363 SkTileGridFactory::TileGridInfo gridInfo;
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001364 gridInfo.fMargin.setEmpty();
1365 gridInfo.fOffset.setZero();
1366 gridInfo.fTileInterval.set(1, 1);
1367
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001368 SkTileGridFactory factory(gridInfo);
1369 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001370 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001371 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001372
robertphillips9b14f262014-06-04 05:40:44 -07001373 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001374 }
1375
1376 {
1377 // RTree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001378 SkRTreeFactory factory;
1379 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001380 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001381 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001382
robertphillips9b14f262014-06-04 05:40:44 -07001383 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001384 }
1385
1386 {
1387 // quad tree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001388 SkQuadTreeFactory factory;
1389 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001390 recorder.beginRecording(1, 1, &factory);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001391 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001392
robertphillips9b14f262014-06-04 05:40:44 -07001393 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001394 }
1395}
1396
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001397static void test_clip_bound_opt(skiatest::Reporter* reporter) {
1398 // Test for crbug.com/229011
1399 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
1400 SkIntToScalar(2), SkIntToScalar(2));
1401 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
1402 SkIntToScalar(1), SkIntToScalar(1));
1403 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
1404 SkIntToScalar(1), SkIntToScalar(1));
1405
1406 SkPath invPath;
1407 invPath.addOval(rect1);
1408 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
1409 SkPath path;
1410 path.addOval(rect2);
1411 SkPath path2;
1412 path2.addOval(rect3);
1413 SkIRect clipBounds;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001414 SkPictureRecorder recorder;
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001415 // Minimalist test set for 100% code coverage of
1416 // SkPictureRecord::updateClipConservativelyUsingBounds
1417 {
robertphillips9f1c2412014-06-09 06:25:34 -07001418 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001419 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1420 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1421 REPORTER_ASSERT(reporter, true == nonEmpty);
1422 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1423 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1424 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1425 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1426 }
1427 {
robertphillips9f1c2412014-06-09 06:25:34 -07001428 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001429 canvas->clipPath(path, SkRegion::kIntersect_Op);
1430 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1431 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1432 REPORTER_ASSERT(reporter, true == nonEmpty);
1433 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1434 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1435 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1436 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1437 }
1438 {
robertphillips9f1c2412014-06-09 06:25:34 -07001439 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001440 canvas->clipPath(path, SkRegion::kIntersect_Op);
1441 canvas->clipPath(invPath, SkRegion::kUnion_Op);
1442 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1443 REPORTER_ASSERT(reporter, true == nonEmpty);
1444 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1445 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1446 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1447 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1448 }
1449 {
robertphillips9f1c2412014-06-09 06:25:34 -07001450 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001451 canvas->clipPath(path, SkRegion::kDifference_Op);
1452 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1453 REPORTER_ASSERT(reporter, true == nonEmpty);
1454 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1455 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1456 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1457 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1458 }
1459 {
robertphillips9f1c2412014-06-09 06:25:34 -07001460 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001461 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
1462 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1463 // True clip is actually empty in this case, but the best
1464 // determination we can make using only bounds as input is that the
1465 // clip is included in the bounds of 'path'.
1466 REPORTER_ASSERT(reporter, true == nonEmpty);
1467 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1468 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1469 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1470 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1471 }
1472 {
robertphillips9f1c2412014-06-09 06:25:34 -07001473 SkCanvas* canvas = recorder.beginRecording(10, 10);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001474 canvas->clipPath(path, SkRegion::kIntersect_Op);
1475 canvas->clipPath(path2, SkRegion::kXOR_Op);
1476 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1477 REPORTER_ASSERT(reporter, true == nonEmpty);
1478 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
1479 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
1480 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1481 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1482 }
1483}
1484
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001485/**
1486 * A canvas that records the number of clip commands.
1487 */
1488class ClipCountingCanvas : public SkCanvas {
1489public:
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001490 ClipCountingCanvas(int width, int height)
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001491 : INHERITED(width, height)
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001492 , fClipCount(0){
1493 }
1494
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001495 virtual void onClipRect(const SkRect& r,
1496 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001497 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001498 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001499 this->INHERITED::onClipRect(r, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001500 }
1501
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001502 virtual void onClipRRect(const SkRRect& rrect,
1503 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001504 ClipEdgeStyle edgeStyle)SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001505 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001506 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001507 }
1508
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001509 virtual void onClipPath(const SkPath& path,
1510 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001511 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001512 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001513 this->INHERITED::onClipPath(path, op, edgeStyle);
1514 }
1515
1516 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE {
1517 fClipCount += 1;
1518 this->INHERITED::onClipRegion(deviceRgn, op);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001519 }
1520
1521 unsigned getClipCount() const { return fClipCount; }
1522
1523private:
1524 unsigned fClipCount;
1525
1526 typedef SkCanvas INHERITED;
1527};
1528
1529static void test_clip_expansion(skiatest::Reporter* reporter) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001530 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001531 SkCanvas* canvas = recorder.beginRecording(10, 10);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001532
1533 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
1534 // The following expanding clip should not be skipped.
1535 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
1536 // Draw something so the optimizer doesn't just fold the world.
1537 SkPaint p;
1538 p.setColor(SK_ColorBLUE);
1539 canvas->drawPaint(p);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001540 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001541
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001542 ClipCountingCanvas testCanvas(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001543 picture->draw(&testCanvas);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001544
1545 // Both clips should be present on playback.
1546 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
1547}
1548
tomhudson@google.com381010e2013-10-24 11:12:47 +00001549static void test_hierarchical(skiatest::Reporter* reporter) {
1550 SkBitmap bm;
1551 make_bm(&bm, 10, 10, SK_ColorRED, true);
1552
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001553 SkPictureRecorder recorder;
tomhudson@google.com381010e2013-10-24 11:12:47 +00001554
robertphillips9f1c2412014-06-09 06:25:34 -07001555 recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001556 SkAutoTUnref<SkPicture> childPlain(recorder.endRecording());
1557 REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0
tomhudson@google.com381010e2013-10-24 11:12:47 +00001558
robertphillips9f1c2412014-06-09 06:25:34 -07001559 recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001560 SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording());
1561 REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1
tomhudson@google.com381010e2013-10-24 11:12:47 +00001562
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001563 {
robertphillips9f1c2412014-06-09 06:25:34 -07001564 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips9b14f262014-06-04 05:40:44 -07001565 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001566 SkAutoTUnref<SkPicture> parentPP(recorder.endRecording());
1567 REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0
1568 }
1569 {
robertphillips9f1c2412014-06-09 06:25:34 -07001570 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips9b14f262014-06-04 05:40:44 -07001571 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001572 SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording());
1573 REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1
1574 }
1575 {
robertphillips9f1c2412014-06-09 06:25:34 -07001576 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001577 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001578 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001579 SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording());
1580 REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1
1581 }
1582 {
robertphillips9f1c2412014-06-09 06:25:34 -07001583 SkCanvas* canvas = recorder.beginRecording(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001584 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001585 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001586 SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording());
1587 REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2
1588 }
tomhudson@google.com381010e2013-10-24 11:12:47 +00001589}
1590
robertphillips@google.comd5500882014-04-02 23:51:13 +00001591static void test_gen_id(skiatest::Reporter* reporter) {
1592
Robert Phillipscfaeec42014-07-13 12:00:50 -04001593 SkPictureRecorder recorder;
1594 recorder.beginRecording(0, 0);
1595 SkAutoTUnref<SkPicture> empty(recorder.endRecording());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001596
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001597 // Empty pictures should still have a valid ID
Robert Phillipscfaeec42014-07-13 12:00:50 -04001598 REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001599
robertphillips9f1c2412014-06-09 06:25:34 -07001600 SkCanvas* canvas = recorder.beginRecording(1, 1);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001601 canvas->drawARGB(255, 255, 255, 255);
1602 SkAutoTUnref<SkPicture> hasData(recorder.endRecording());
1603 // picture should have a non-zero id after recording
1604 REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001605
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001606 // both pictures should have different ids
Robert Phillipscfaeec42014-07-13 12:00:50 -04001607 REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001608}
1609
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00001610DEF_TEST(Picture, reporter) {
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001611#ifdef SK_DEBUG
robertphillipsdb539902014-07-01 08:47:04 -07001612 test_deleting_empty_picture();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001613 test_serializing_empty_picture();
scroggo@google.com4b90b112012-12-04 15:08:56 +00001614#else
1615 test_bad_bitmap();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001616#endif
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001617 test_unbalanced_save_restores(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001618 test_peephole();
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001619#if SK_SUPPORT_GPU
Mike Klein27dc17c2014-08-18 18:35:16 -04001620 test_gpu_veto(reporter);
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001621#endif
ajuma750ae262014-08-18 12:59:55 -07001622 test_has_text(reporter);
reed@google.comfe7b1ed2012-11-29 21:00:39 +00001623 test_gatherpixelrefs(reporter);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +00001624 test_gatherpixelrefsandrects(reporter);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001625 test_bitmap_with_encoded_data(reporter);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001626 test_draw_empty(reporter);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001627 test_clip_bound_opt(reporter);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001628 test_clip_expansion(reporter);
tomhudson@google.com381010e2013-10-24 11:12:47 +00001629 test_hierarchical(reporter);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001630 test_gen_id(reporter);
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001631}
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001632
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001633#if SK_SUPPORT_GPU
1634DEF_GPUTEST(GPUPicture, reporter, factory) {
1635 test_gpu_picture_optimization(reporter, factory);
1636}
1637#endif
1638
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001639static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) {
1640 const SkPaint paint;
1641 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f };
1642 const SkIRect irect = { 2, 2, 3, 3 };
1643
1644 // Don't care what these record, as long as they're legal.
1645 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint);
1646 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag);
1647 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint);
1648 canvas->drawBitmapNine(bitmap, irect, rect, &paint);
1649 canvas->drawSprite(bitmap, 1, 1);
1650}
1651
1652static void test_draw_bitmaps(SkCanvas* canvas) {
1653 SkBitmap empty;
1654 draw_bitmaps(empty, canvas);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001655 empty.setInfo(SkImageInfo::MakeN32Premul(10, 10));
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001656 draw_bitmaps(empty, canvas);
1657}
1658
1659DEF_TEST(Picture_EmptyBitmap, r) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001660 SkPictureRecorder recorder;
robertphillips9f1c2412014-06-09 06:25:34 -07001661 test_draw_bitmaps(recorder.beginRecording(10, 10));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001662 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001663}
1664
1665DEF_TEST(Canvas_EmptyBitmap, r) {
1666 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001667 dst.allocN32Pixels(10, 10);
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001668 SkCanvas canvas(dst);
1669
1670 test_draw_bitmaps(&canvas);
1671}
dneto3f22e8c2014-07-30 15:42:22 -07001672
1673DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) {
1674 // This test is from crbug.com/344987.
1675 // The commands are:
1676 // saveLayer with paint that modifies alpha
1677 // drawBitmapRectToRect
1678 // drawBitmapRectToRect
1679 // restore
1680 // The bug was that this structure was modified so that:
1681 // - The saveLayer and restore were eliminated
1682 // - The alpha was only applied to the first drawBitmapRectToRect
1683
1684 // This test draws blue and red squares inside a 50% transparent
1685 // layer. Both colours should show up muted.
1686 // When the bug is present, the red square (the second bitmap)
1687 // shows upwith full opacity.
1688
1689 SkBitmap blueBM;
1690 make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true);
1691 SkBitmap redBM;
1692 make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true);
1693 SkPaint semiTransparent;
1694 semiTransparent.setAlpha(0x80);
1695
1696 SkPictureRecorder recorder;
1697 SkCanvas* canvas = recorder.beginRecording(100, 100);
1698 canvas->drawARGB(0, 0, 0, 0);
1699
1700 canvas->saveLayer(0, &semiTransparent);
1701 canvas->drawBitmap(blueBM, 25, 25);
1702 canvas->drawBitmap(redBM, 50, 50);
1703 canvas->restore();
1704
1705 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1706
1707 // Now replay the picture back on another canvas
1708 // and check a couple of its pixels.
1709 SkBitmap replayBM;
1710 make_bm(&replayBM, 100, 100, SK_ColorBLACK, false);
1711 SkCanvas replayCanvas(replayBM);
1712 picture->draw(&replayCanvas);
1713 replayCanvas.flush();
1714
1715 // With the bug present, at (55, 55) we would get a fully opaque red
1716 // intead of a dark red.
1717 REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080);
1718 REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000);
1719}
mtklein3e8232b2014-08-18 13:39:11 -07001720
1721struct CountingBBH : public SkBBoxHierarchy {
1722 mutable int searchCalls;
1723
1724 CountingBBH() : searchCalls(0) {}
1725
1726 virtual void search(const SkIRect& query, SkTDArray<void*>* results) const {
1727 this->searchCalls++;
1728 }
1729
1730 // All other methods unimplemented.
1731 virtual void insert(void* data, const SkIRect& bounds, bool defer) {}
1732 virtual void flushDeferredInserts() {}
1733 virtual void clear() {}
1734 virtual int getCount() const { return 0; }
1735 virtual int getDepth() const { return 0; }
1736 virtual void rewindInserts() {}
1737};
1738
1739class SpoonFedBBHFactory : public SkBBHFactory {
1740public:
1741 explicit SpoonFedBBHFactory(SkBBoxHierarchy* bbh) : fBBH(bbh) {}
1742 virtual SkBBoxHierarchy* operator()(int width, int height) const {
1743 return SkRef(fBBH);
1744 }
1745private:
1746 SkBBoxHierarchy* fBBH;
1747};
1748
1749// When the canvas clip covers the full picture, we don't need to call the BBH.
1750DEF_TEST(Picture_SkipBBH, r) {
1751 CountingBBH bbh;
1752 SpoonFedBBHFactory factory(&bbh);
1753
1754 SkPictureRecorder recorder;
1755 recorder.beginRecording(320, 240, &factory);
1756 SkAutoTUnref<const SkPicture> picture(recorder.endRecording());
1757
1758 SkCanvas big(640, 480), small(300, 200);
1759
1760 picture->draw(&big);
1761 REPORTER_ASSERT(r, bbh.searchCalls == 0);
1762
1763 picture->draw(&small);
1764 REPORTER_ASSERT(r, bbh.searchCalls == 1);
1765}