blob: f65c8408991c1dbfa9a4ef752c572da1239fe024 [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
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00008#include "SkBitmapDevice.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"
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000016#if SK_SUPPORT_GPU
17#include "SkGpuDevice.h"
18#endif
halcanary@google.com3d50ea12014-01-02 13:15:13 +000019#include "SkImageEncoder.h"
20#include "SkImageGenerator.h"
reed@google.com21b519d2012-10-02 17:42:15 +000021#include "SkPaint.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000022#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000023#include "SkPictureRecorder.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000024#include "SkPictureUtils.h"
reed@google.com72aa79c2013-01-24 18:27:42 +000025#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000026#include "SkRandom.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000027#include "SkShader.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000028#include "SkStream.h"
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000029
30#if SK_SUPPORT_GPU
31#include "SkSurface.h"
32#include "GrContextFactory.h"
33#include "GrPictureUtils.h"
34#endif
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000035#include "Test.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000036
reed@google.com47b679b2014-05-14 18:58:16 +000037#include "SkLumaColorFilter.h"
38#include "SkColorFilterImageFilter.h"
39
robertphillips@google.comed9866c2014-01-09 19:20:45 +000040static const int gColorScale = 30;
41static const int gColorOffset = 60;
reed@google.comfe7b1ed2012-11-29 21:00:39 +000042
43static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000044 bm->allocN32Pixels(w, h);
reed@google.comfe7b1ed2012-11-29 21:00:39 +000045 bm->eraseColor(color);
46 if (immutable) {
47 bm->setImmutable();
48 }
49}
50
robertphillips@google.comfe5824a2014-01-09 19:45:29 +000051static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000052 SkASSERT(w % 2 == 0);
53 SkASSERT(h % 2 == 0);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000054 bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType,
55 kPremul_SkAlphaType));
robertphillips@google.comed9866c2014-01-09 19:20:45 +000056 SkAutoLockPixels lock(*bm);
57 for (int y = 0; y < h; y += 2) {
58 uint8_t* s = bm->getAddr8(0, y);
59 for (int x = 0; x < w; x += 2) {
60 *s++ = 0xFF;
61 *s++ = 0x00;
62 }
63 s = bm->getAddr8(0, y + 1);
64 for (int x = 0; x < w; x += 2) {
65 *s++ = 0x00;
66 *s++ = 0xFF;
67 }
68 }
69 if (immutable) {
70 bm->setImmutable();
71 }
72}
reed@google.comfe7b1ed2012-11-29 21:00:39 +000073
robertphillips@google.comed9866c2014-01-09 19:20:45 +000074static void init_paint(SkPaint* paint, const SkBitmap &bm) {
75 SkShader* shader = SkShader::CreateBitmapShader(bm,
76 SkShader::kClamp_TileMode,
77 SkShader::kClamp_TileMode);
78 paint->setShader(shader)->unref();
79}
80
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +000081typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000082 const SkBitmap&, const SkPoint&,
83 SkTDArray<SkPixelRef*>* usedPixRefs);
robertphillips@google.comed9866c2014-01-09 19:20:45 +000084
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000085static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000086 const SkBitmap& altBM, const SkPoint& pos,
87 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000088 SkPaint paint;
89 init_paint(&paint, bm);
90
91 canvas->drawPaint(paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000092 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +000093}
94
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000095static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000096 const SkBitmap& altBM, const SkPoint& pos,
97 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000098 SkPaint paint;
99 init_paint(&paint, bm);
100
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000101 // draw a rect
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000102 SkPoint points[5] = {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000103 { pos.fX, pos.fY },
104 { pos.fX + bm.width() - 1, pos.fY },
105 { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 },
106 { pos.fX, pos.fY + bm.height() - 1 },
107 { pos.fX, pos.fY },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000108 };
109
110 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000111 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000112}
113
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000114static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000115 const SkBitmap& altBM, const SkPoint& pos,
116 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000117 SkPaint paint;
118 init_paint(&paint, bm);
119
120 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
121 r.offset(pos.fX, pos.fY);
122
123 canvas->drawRect(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000124 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000125}
126
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000127static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000128 const SkBitmap& altBM, const SkPoint& pos,
129 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000130 SkPaint paint;
131 init_paint(&paint, bm);
132
133 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
134 r.offset(pos.fX, pos.fY);
135
136 canvas->drawOval(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000137 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000138}
139
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000140static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000141 const SkBitmap& altBM, const SkPoint& pos,
142 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000143 SkPaint paint;
144 init_paint(&paint, bm);
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000145
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000146 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
147 r.offset(pos.fX, pos.fY);
148
149 SkRRect rr;
150 rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4);
151 canvas->drawRRect(rr, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000152 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000153}
154
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000155static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000156 const SkBitmap& altBM, const SkPoint& pos,
157 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000158 SkPaint paint;
159 init_paint(&paint, bm);
160
161 SkPath path;
162 path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height()));
163 path.lineTo(SkIntToScalar(bm.width()), 0);
164 path.close();
165 path.offset(pos.fX, pos.fY);
166
167 canvas->drawPath(path, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000168 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000169}
170
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000171static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000172 const SkBitmap& altBM, const SkPoint& pos,
173 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000174 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000175 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000176}
177
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000178static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000179 const SkBitmap& altBM, const SkPoint& pos,
180 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000181 SkPaint paint;
182 init_paint(&paint, bm);
183
184 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
185 canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000186 *usedPixRefs->append() = bm.pixelRef();
187 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000188}
189
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000190static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000191 const SkBitmap& altBM, const SkPoint& pos,
192 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000193 const SkMatrix& ctm = canvas->getTotalMatrix();
194
195 SkPoint p(pos);
196 ctm.mapPoints(&p, 1);
197
198 canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000199 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000200}
201
202#if 0
203// Although specifiable, this case doesn't seem to make sense (i.e., the
204// bitmap in the shader is never used).
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000205static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000206 const SkBitmap& altBM, const SkPoint& pos,
207 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000208 SkPaint paint;
209 init_paint(&paint, bm);
210
211 const SkMatrix& ctm = canvas->getTotalMatrix();
212
213 SkPoint p(pos);
214 ctm.mapPoints(&p, 1);
215
216 canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000217 *usedPixRefs->append() = bm.pixelRef();
218 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000219}
220#endif
221
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000222static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000223 const SkBitmap& altBM, const SkPoint& pos,
224 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000225 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
226
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000227 r.offset(pos.fX, pos.fY);
228 canvas->drawBitmapRectToRect(bm, NULL, r, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000229 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000230}
231
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000232static void drawbitmaprect_withshader_proc(SkCanvas* canvas,
233 const SkBitmap& bm,
234 const SkBitmap& altBM,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000235 const SkPoint& pos,
236 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000237 SkPaint paint;
238 init_paint(&paint, bm);
239
240 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000241 r.offset(pos.fX, pos.fY);
242
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000243 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
244 canvas->drawBitmapRectToRect(altBM, NULL, r, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000245 *usedPixRefs->append() = bm.pixelRef();
246 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000247}
248
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000249static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000250 const SkBitmap& altBM, const SkPoint& pos,
251 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000252 SkPaint paint;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000253 init_paint(&paint, bm);
254 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
255
256 canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000257 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000258}
259
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000260static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000261 const SkBitmap& altBM, const SkPoint& pos,
262 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000263 SkPaint paint;
264 init_paint(&paint, bm);
265 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
266
267 SkPoint point = { pos.fX, pos.fY + bm.height() };
268 canvas->drawPosText("O", 1, &point, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000269 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000270}
271
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000272static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000273 const SkBitmap& altBM, const SkPoint& pos,
274 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000275 SkPaint paint;
276
277 init_paint(&paint, bm);
278 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
279
280 SkPath path;
281 path.lineTo(SkIntToScalar(bm.width()), 0);
282 path.offset(pos.fX, pos.fY+bm.height());
283
284 canvas->drawTextOnPath("O", 1, path, NULL, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000285 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000286}
287
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000288static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000289 const SkBitmap& altBM, const SkPoint& pos,
290 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000291 SkPaint paint;
292 init_paint(&paint, bm);
293
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000294 SkPoint verts[4] = {
295 { pos.fX, pos.fY },
296 { pos.fX + bm.width(), pos.fY },
297 { pos.fX + bm.width(), pos.fY + bm.height() },
298 { pos.fX, pos.fY + bm.height() }
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000299 };
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000300 SkPoint texs[4] = { { 0, 0 },
301 { SkIntToScalar(bm.width()), 0 },
302 { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000303 { 0, SkIntToScalar(bm.height()) } };
304 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 };
305
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000306 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL,
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000307 indices, 6, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000308 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000309}
310
311// Return a picture with the bitmaps drawn at the specified positions.
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000312static SkPicture* record_bitmaps(const SkBitmap bm[],
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000313 const SkPoint pos[],
314 SkTDArray<SkPixelRef*> analytic[],
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000315 int count,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000316 DrawBitmapProc proc) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000317 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000318 SkCanvas* canvas = recorder.beginRecording(1000, 1000, NULL, 0);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000319 for (int i = 0; i < count; ++i) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000320 analytic[i].rewind();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000321 canvas->save();
322 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY,
323 SkIntToScalar(bm[i].width()),
324 SkIntToScalar(bm[i].height()));
325 canvas->clipRect(clipRect, SkRegion::kIntersect_Op);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000326 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000327 canvas->restore();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000328 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000329 return recorder.endRecording();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000330}
331
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000332static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000333 rect->fLeft = rand.nextRangeScalar(-W, 2*W);
334 rect->fTop = rand.nextRangeScalar(-H, 2*H);
335 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W);
336 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H);
337
338 // we integralize rect to make our tests more predictable, since Gather is
339 // a little sloppy.
340 SkIRect ir;
341 rect->round(&ir);
342 rect->set(ir);
343}
344
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000345static void draw(SkPicture* pic, int width, int height, SkBitmap* result) {
346 make_bm(result, width, height, SK_ColorBLACK, false);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000347
348 SkCanvas canvas(*result);
robertphillips9b14f262014-06-04 05:40:44 -0700349 canvas.drawPicture(pic);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000350}
351
352template <typename T> int find_index(const T* array, T elem, int count) {
353 for (int i = 0; i < count; ++i) {
354 if (array[i] == elem) {
355 return i;
356 }
357 }
358 return -1;
359}
360
361// Return true if 'ref' is found in array[]
362static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) {
363 return find_index<const SkPixelRef*>(array, ref, count) >= 0;
364}
365
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000366// Look at each pixel that is inside 'subset', and if its color appears in
367// colors[], find the corresponding value in refs[] and append that ref into
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000368// array, skipping duplicates of the same value.
369// Note that gathering pixelRefs from rendered colors suffers from the problem
370// that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color)
371// isn't easy to reconstruct.
372static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[],
373 int count, SkTDArray<SkPixelRef*>* array,
374 const SkRect& subset) {
375 SkIRect ir;
376 subset.roundOut(&ir);
377
378 if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) {
379 return;
380 }
381
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000382 // Since we only want to return unique values in array, when we scan we just
383 // set a bit for each index'd color found. In practice we only have a few
384 // distinct colors, so we just use an int's bits as our array. Hence the
385 // assert that count <= number-of-bits-in-our-int.
386 SkASSERT((unsigned)count <= 32);
387 uint32_t bitarray = 0;
388
389 SkAutoLockPixels alp(bm);
390
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000391 for (int y = ir.fTop; y < ir.fBottom; ++y) {
392 for (int x = ir.fLeft; x < ir.fRight; ++x) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000393 SkPMColor pmc = *bm.getAddr32(x, y);
394 // the only good case where the color is not found would be if
395 // the color is transparent, meaning no bitmap was drawn in that
396 // pixel.
397 if (pmc) {
bsalomon@google.comc3d753e2013-01-08 17:24:44 +0000398 uint32_t index = SkGetPackedR32(pmc);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000399 SkASSERT(SkGetPackedG32(pmc) == index);
400 SkASSERT(SkGetPackedB32(pmc) == index);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000401 if (0 == index) {
402 continue; // background color
403 }
404 SkASSERT(0 == (index - gColorOffset) % gColorScale);
405 index = (index - gColorOffset) / gColorScale;
bsalomon@google.com5f429b02013-01-08 18:42:20 +0000406 SkASSERT(static_cast<int>(index) < count);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000407 bitarray |= 1 << index;
408 }
409 }
410 }
411
412 for (int i = 0; i < count; ++i) {
413 if (bitarray & (1 << i)) {
414 *array->append() = refs[i];
415 }
416 }
417}
418
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000419static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h,
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000420 const SkTDArray<SkPixelRef*> analytic[],
421 int count,
422 SkTDArray<SkPixelRef*>* result,
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000423 const SkRect& subset) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000424 for (int i = 0; i < count; ++i) {
425 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h);
426
427 if (SkRect::Intersects(subset, rect)) {
428 result->append(analytic[i].count(), analytic[i].begin());
429 }
430 }
431}
432
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000433
434static const struct {
435 const DrawBitmapProc proc;
436 const char* const desc;
437} gProcs[] = {
438 {drawpaint_proc, "drawpaint"},
439 {drawpoints_proc, "drawpoints"},
440 {drawrect_proc, "drawrect"},
441 {drawoval_proc, "drawoval"},
442 {drawrrect_proc, "drawrrect"},
443 {drawpath_proc, "drawpath"},
444 {drawbitmap_proc, "drawbitmap"},
445 {drawbitmap_withshader_proc, "drawbitmap_withshader"},
446 {drawsprite_proc, "drawsprite"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000447#if 0
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000448 {drawsprite_withshader_proc, "drawsprite_withshader"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000449#endif
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000450 {drawbitmaprect_proc, "drawbitmaprect"},
451 {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"},
452 {drawtext_proc, "drawtext"},
453 {drawpostext_proc, "drawpostext"},
454 {drawtextonpath_proc, "drawtextonpath"},
455 {drawverts_proc, "drawverts"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000456};
457
458static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000459 // Our convention is that the color components contain an encoding of
460 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000461 // reserved for the background
462 for (int i = 0; i < num; ++i) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000463 make_bm(&bm[i], w, h,
464 SkColorSetARGB(0xFF,
465 gColorScale*i+gColorOffset,
466 gColorScale*i+gColorOffset,
467 gColorScale*i+gColorOffset),
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000468 true);
469 refs[i] = bm[i].pixelRef();
470 }
471
472 // The A8 alternate bitmaps are all BW checkerboards
473 for (int i = 0; i < num; ++i) {
474 make_checkerboard(&bm[num+i], w, h, true);
475 refs[num+i] = bm[num+i].pixelRef();
476 }
477}
478
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000479static void test_gatherpixelrefs(skiatest::Reporter* reporter) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000480 const int IW = 32;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000481 const int IH = IW;
482 const SkScalar W = SkIntToScalar(IW);
483 const SkScalar H = W;
484
485 static const int N = 4;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000486 SkBitmap bm[2*N];
487 SkPixelRef* refs[2*N];
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000488 SkTDArray<SkPixelRef*> analytic[N];
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000489
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000490 const SkPoint pos[N] = {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000491 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
492 };
493
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000494 create_textures(bm, refs, N, IW, IH);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000495
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000496 SkRandom rand;
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000497 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000498 SkAutoTUnref<SkPicture> pic(
499 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000500
tomhudson@google.com381010e2013-10-24 11:12:47 +0000501 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000502 // quick check for a small piece of each quadrant, which should just
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000503 // contain 1 or 2 bitmaps.
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000504 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
505 SkRect r;
506 r.set(2, 2, W - 2, H - 2);
507 r.offset(pos[i].fX, pos[i].fY);
508 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r));
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000509 if (!data) {
510 ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned "
511 "NULL for %s.", gProcs[k].desc);
512 continue;
513 }
514 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data();
515 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*));
516 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
517 if (1 == count) {
518 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
519 } else if (2 == count) {
520 REPORTER_ASSERT(reporter,
521 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
522 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
commit-bot@chromium.orgfe433c12013-07-09 16:04:32 +0000523 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000524 }
525
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000526 SkBitmap image;
527 draw(pic, 2*IW, 2*IH, &image);
528
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000529 // Test a bunch of random (mostly) rects, and compare the gather results
530 // with a deduced list of refs by looking at the colors drawn.
531 for (int j = 0; j < 100; ++j) {
532 SkRect r;
533 rand_rect(&r, rand, 2*W, 2*H);
534
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000535 SkTDArray<SkPixelRef*> fromImage;
536 gather_from_image(image, refs, N, &fromImage, r);
537
538 SkTDArray<SkPixelRef*> fromAnalytic;
539 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000540
541 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
542 size_t dataSize = data ? data->size() : 0;
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000543 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000544 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize);
545 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL;
546 SkAutoDataUnref adu(data);
547
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000548 // Everything that we saw drawn should appear in the analytic list
549 // but the analytic list may contain some pixelRefs that were not
550 // seen in the image (e.g., A8 textures used as masks)
551 for (int i = 0; i < fromImage.count(); ++i) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000552 if (-1 == fromAnalytic.find(fromImage[i])) {
553 ERRORF(reporter, "PixelRef missing %d %s",
554 i, gProcs[k].desc);
555 }
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000556 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000557
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000558 /*
559 * GatherPixelRefs is conservative, so it can return more bitmaps
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000560 * than are strictly required. Thus our check here is only that
561 * Gather didn't miss any that we actually needed. Even that isn't
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000562 * a strict requirement on Gather, which is meant to be quick and
563 * only mostly-correct, but at the moment this test should work.
564 */
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000565 for (int i = 0; i < fromAnalytic.count(); ++i) {
566 bool found = find(gatherRefs, fromAnalytic[i], gatherCount);
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000567 if (!found) {
568 ERRORF(reporter, "PixelRef missing %d %s",
569 i, gProcs[k].desc);
570 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000571#if 0
572 // enable this block of code to debug failures, as it will rerun
573 // the case that failed.
574 if (!found) {
575 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
576 size_t dataSize = data ? data->size() : 0;
577 }
578#endif
579 }
580 }
581 }
582}
583
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000584static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) {
585 const int IW = 32;
586 const int IH = IW;
587 const SkScalar W = SkIntToScalar(IW);
588 const SkScalar H = W;
589
590 static const int N = 4;
591 SkBitmap bm[2*N];
592 SkPixelRef* refs[2*N];
593 SkTDArray<SkPixelRef*> analytic[N];
594
595 const SkPoint pos[N] = {
596 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
597 };
598
599 create_textures(bm, refs, N, IW, IH);
600
601 SkRandom rand;
602 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000603 SkAutoTUnref<SkPicture> pic(
604 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000605
606 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
607
608 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont(
609 new SkPictureUtils::SkPixelRefsAndRectsList);
610
611 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont);
612
613 // quick check for a small piece of each quadrant, which should just
614 // contain 1 or 2 bitmaps.
615 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
616 SkRect r;
617 r.set(2, 2, W - 2, H - 2);
618 r.offset(pos[i].fX, pos[i].fY);
619
620 SkTDArray<SkPixelRef*> gatheredRefs;
621 prCont->query(r, &gatheredRefs);
622
623 int count = gatheredRefs.count();
624 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
625 if (1 == count) {
626 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
627 } else if (2 == count) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000628 REPORTER_ASSERT(reporter,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000629 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
630 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
631 }
632 }
633
634 SkBitmap image;
635 draw(pic, 2*IW, 2*IH, &image);
636
637 // Test a bunch of random (mostly) rects, and compare the gather results
638 // with the analytic results and the pixel refs seen in a rendering.
639 for (int j = 0; j < 100; ++j) {
640 SkRect r;
641 rand_rect(&r, rand, 2*W, 2*H);
642
643 SkTDArray<SkPixelRef*> fromImage;
644 gather_from_image(image, refs, N, &fromImage, r);
645
646 SkTDArray<SkPixelRef*> fromAnalytic;
647 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
648
649 SkTDArray<SkPixelRef*> gatheredRefs;
650 prCont->query(r, &gatheredRefs);
651
652 // Everything that we saw drawn should appear in the analytic list
653 // but the analytic list may contain some pixelRefs that were not
654 // seen in the image (e.g., A8 textures used as masks)
655 for (int i = 0; i < fromImage.count(); ++i) {
656 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]));
657 }
658
659 // Everything in the analytic list should appear in the gathered
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000660 // list.
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000661 for (int i = 0; i < fromAnalytic.count(); ++i) {
662 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i]));
663 }
664 }
665 }
666}
667
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000668#ifdef SK_DEBUG
669// Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only
670// run in debug mode.
671static void test_deleting_empty_playback() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000672 SkPictureRecorder recorder;
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000673 // Creates an SkPictureRecord
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000674 recorder.beginRecording(0, 0, NULL, 0);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000675 // Turns that into an SkPicturePlayback
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000676 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000677 // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000678 recorder.beginRecording(0, 0, NULL, 0);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000679}
680
681// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
682static void test_serializing_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000683 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000684 recorder.beginRecording(0, 0, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000685 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000686 SkDynamicMemoryWStream stream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000687 picture->serialize(&stream);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000688}
689#endif
690
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000691static void rand_op(SkCanvas* canvas, SkRandom& rand) {
reed@google.com21b519d2012-10-02 17:42:15 +0000692 SkPaint paint;
693 SkRect rect = SkRect::MakeWH(50, 50);
694
695 SkScalar unit = rand.nextUScalar1();
696 if (unit <= 0.3) {
697// SkDebugf("save\n");
698 canvas->save();
699 } else if (unit <= 0.6) {
700// SkDebugf("restore\n");
701 canvas->restore();
702 } else if (unit <= 0.9) {
703// SkDebugf("clip\n");
704 canvas->clipRect(rect);
705 } else {
706// SkDebugf("draw\n");
707 canvas->drawPaint(paint);
708 }
709}
710
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000711#if SK_SUPPORT_GPU
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000712static void test_gpu_veto(skiatest::Reporter* reporter) {
713
714 SkPictureRecorder recorder;
715
716 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
717 {
718 SkPath path;
719 path.moveTo(0, 0);
720 path.lineTo(50, 50);
721
722 SkScalar intervals[] = { 1.0f, 1.0f };
723 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0));
724
725 SkPaint paint;
726 paint.setStyle(SkPaint::kStroke_Style);
727 paint.setPathEffect(dash);
728
729 canvas->drawPath(path, paint);
730 }
731 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
732 // path effects currently render an SkPicture undesireable for GPU rendering
commit-bot@chromium.orga1ff26a2014-05-30 21:52:52 +0000733
734 const char *reason = NULL;
735 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL, &reason));
736 REPORTER_ASSERT(reporter, NULL != reason);
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000737
738 canvas = recorder.beginRecording(100, 100, NULL, 0);
739 {
740 SkPath path;
741
742 path.moveTo(0, 0);
743 path.lineTo(0, 50);
744 path.lineTo(25, 25);
745 path.lineTo(50, 50);
746 path.lineTo(50, 0);
747 path.close();
748 REPORTER_ASSERT(reporter, !path.isConvex());
749
750 SkPaint paint;
751 paint.setAntiAlias(true);
752 for (int i = 0; i < 50; ++i) {
753 canvas->drawPath(path, paint);
754 }
755 }
756 picture.reset(recorder.endRecording());
757 // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering
758 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
759
760 canvas = recorder.beginRecording(100, 100, NULL, 0);
761 {
762 SkPath path;
763
764 path.moveTo(0, 0);
765 path.lineTo(0, 50);
766 path.lineTo(25, 25);
767 path.lineTo(50, 50);
768 path.lineTo(50, 0);
769 path.close();
770 REPORTER_ASSERT(reporter, !path.isConvex());
771
772 SkPaint paint;
773 paint.setAntiAlias(true);
774 paint.setStyle(SkPaint::kStroke_Style);
775 paint.setStrokeWidth(0);
776 for (int i = 0; i < 50; ++i) {
777 canvas->drawPath(path, paint);
778 }
779 }
780 picture.reset(recorder.endRecording());
781 // hairline stroked AA concave paths are fine for GPU rendering
782 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL));
783}
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000784
785static void test_gpu_picture_optimization(skiatest::Reporter* reporter,
786 GrContextFactory* factory) {
787
788 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
789
790 static const int kWidth = 100;
791 static const int kHeight = 100;
792
793 SkAutoTUnref<SkPicture> pict;
794
795 // create a picture with the structure:
796 // 1)
797 // SaveLayer
798 // Restore
799 // 2)
800 // SaveLayer
skia.committer@gmail.coma5b068c2014-05-07 03:04:15 +0000801 // Translate
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000802 // SaveLayer w/ bound
803 // Restore
804 // Restore
805 // 3)
806 // SaveLayer w/ copyable paint
807 // Restore
808 // 4)
809 // SaveLayer w/ non-copyable paint
810 // Restore
811 {
812 SkPictureRecorder recorder;
813
814 SkCanvas* c = recorder.beginRecording(kWidth, kHeight, NULL, 0);
815 // 1)
816 c->saveLayer(NULL, NULL);
817 c->restore();
818
819 // 2)
820 c->saveLayer(NULL, NULL);
821 c->translate(kWidth/2, kHeight/2);
822 SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2);
823 c->saveLayer(&r, NULL);
824 c->restore();
825 c->restore();
826
827 // 3)
828 {
829 SkPaint p;
830 p.setColor(SK_ColorRED);
831 c->saveLayer(NULL, &p);
832 c->restore();
833 }
834 // 4)
835 // TODO: this case will need to be removed once the paint's are immutable
836 {
837 SkPaint p;
reed@google.com47b679b2014-05-14 18:58:16 +0000838 SkAutoTUnref<SkColorFilter> cf(SkLumaColorFilter::Create());
839 p.setImageFilter(SkColorFilterImageFilter::Create(cf.get()))->unref();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000840 c->saveLayer(NULL, &p);
841 c->restore();
842 }
843
844 pict.reset(recorder.endRecording());
845 }
846
847 // Now test out the SaveLayer extraction
848 {
849 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
850
851 SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info));
852
skia.committer@gmail.coma5b068c2014-05-07 03:04:15 +0000853 SkCanvas* canvas = surface->getCanvas();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000854
855 canvas->EXPERIMENTAL_optimize(pict);
856
857 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
858
859 const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key);
860 REPORTER_ASSERT(reporter, NULL != data);
861
862 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
863 REPORTER_ASSERT(reporter, 5 == gpuData->numSaveLayers());
864
865 const GPUAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0);
866 // The parent/child layer appear in reverse order
867 const GPUAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2);
868 const GPUAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1);
869 const GPUAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3);
reed@google.com47b679b2014-05-14 18:58:16 +0000870// const GPUAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(4);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000871
872 REPORTER_ASSERT(reporter, info0.fValid);
873 REPORTER_ASSERT(reporter, kWidth == info0.fSize.fWidth && kHeight == info0.fSize.fHeight);
874 REPORTER_ASSERT(reporter, info0.fCTM.isIdentity());
875 REPORTER_ASSERT(reporter, 0 == info0.fOffset.fX && 0 == info0.fOffset.fY);
876 REPORTER_ASSERT(reporter, NULL != info0.fPaint);
877 REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers);
878
879 REPORTER_ASSERT(reporter, info1.fValid);
880 REPORTER_ASSERT(reporter, kWidth == info1.fSize.fWidth && kHeight == info1.fSize.fHeight);
881 REPORTER_ASSERT(reporter, info1.fCTM.isIdentity());
882 REPORTER_ASSERT(reporter, 0 == info1.fOffset.fX && 0 == info1.fOffset.fY);
883 REPORTER_ASSERT(reporter, NULL != info1.fPaint);
884 REPORTER_ASSERT(reporter, !info1.fIsNested && info1.fHasNestedLayers); // has a nested SL
885
886 REPORTER_ASSERT(reporter, info2.fValid);
skia.committer@gmail.coma5b068c2014-05-07 03:04:15 +0000887 REPORTER_ASSERT(reporter, kWidth/2 == info2.fSize.fWidth &&
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000888 kHeight/2 == info2.fSize.fHeight); // bound reduces size
889 REPORTER_ASSERT(reporter, info2.fCTM.isIdentity()); // translated
skia.committer@gmail.com8a1e6882014-05-09 03:05:30 +0000890 REPORTER_ASSERT(reporter, kWidth/2 == info2.fOffset.fX &&
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +0000891 kHeight/2 == info2.fOffset.fY);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000892 REPORTER_ASSERT(reporter, NULL != info1.fPaint);
893 REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested
894
895 REPORTER_ASSERT(reporter, info3.fValid);
896 REPORTER_ASSERT(reporter, kWidth == info3.fSize.fWidth && kHeight == info3.fSize.fHeight);
897 REPORTER_ASSERT(reporter, info3.fCTM.isIdentity());
898 REPORTER_ASSERT(reporter, 0 == info3.fOffset.fX && 0 == info3.fOffset.fY);
899 REPORTER_ASSERT(reporter, NULL != info3.fPaint);
900 REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers);
901
reed@google.com47b679b2014-05-14 18:58:16 +0000902#if 0 // needs more though for GrGatherCanvas
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000903 REPORTER_ASSERT(reporter, !info4.fValid); // paint is/was uncopyable
skia.committer@gmail.coma5b068c2014-05-07 03:04:15 +0000904 REPORTER_ASSERT(reporter, kWidth == info4.fSize.fWidth && kHeight == info4.fSize.fHeight);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000905 REPORTER_ASSERT(reporter, 0 == info4.fOffset.fX && 0 == info4.fOffset.fY);
906 REPORTER_ASSERT(reporter, info4.fCTM.isIdentity());
907 REPORTER_ASSERT(reporter, NULL == info4.fPaint); // paint is/was uncopyable
908 REPORTER_ASSERT(reporter, !info4.fIsNested && !info4.fHasNestedLayers);
reed@google.com47b679b2014-05-14 18:58:16 +0000909#endif
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000910 }
911}
912
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000913#endif
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000914
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000915static void set_canvas_to_save_count_4(SkCanvas* canvas) {
916 canvas->restoreToCount(1);
917 canvas->save();
918 canvas->save();
919 canvas->save();
920}
921
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +0000922#ifdef SK_BUILD_FOR_ANDROID
923/**
924 * A canvas that records the number of saves, saveLayers and restores.
925 */
926class SaveCountingCanvas : public SkCanvas {
927public:
928 SaveCountingCanvas(int width, int height)
929 : INHERITED(width, height)
930 , fSaveCount(0)
931 , fSaveLayerCount(0)
932 , fRestoreCount(0){
933 }
934
935 virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint,
936 SaveFlags flags) SK_OVERRIDE {
937 ++fSaveLayerCount;
938 return this->INHERITED::willSaveLayer(bounds, paint, flags);
939 }
940
941 virtual void willSave(SaveFlags flags) SK_OVERRIDE {
942 ++fSaveCount;
943 this->INHERITED::willSave(flags);
944 }
945
946 virtual void willRestore() SK_OVERRIDE {
947 ++fRestoreCount;
948 this->INHERITED::willRestore();
949 }
950
951 unsigned int getSaveCount() const { return fSaveCount; }
952 unsigned int getSaveLayerCount() const { return fSaveLayerCount; }
953 unsigned int getRestoreCount() const { return fRestoreCount; }
954
955private:
956 unsigned int fSaveCount;
957 unsigned int fSaveLayerCount;
958 unsigned int fRestoreCount;
959
960 typedef SkCanvas INHERITED;
961};
962
skia.committer@gmail.com8e7d37d2014-05-28 03:06:06 +0000963void check_save_state(skiatest::Reporter* reporter, SkPicture* picture,
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +0000964 unsigned int numSaves, unsigned int numSaveLayers,
965 unsigned int numRestores) {
966 SaveCountingCanvas canvas(picture->width(), picture->height());
967
968 picture->draw(&canvas);
969
970 REPORTER_ASSERT(reporter, numSaves == canvas.getSaveCount());
971 REPORTER_ASSERT(reporter, numSaveLayers == canvas.getSaveLayerCount());
972 REPORTER_ASSERT(reporter, numRestores == canvas.getRestoreCount());
973}
974
975// This class exists so SkPicture can friend it and give it access to
976// the 'partialReplay' method.
977class SkPictureRecorderReplayTester {
978public:
979 static SkPicture* Copy(SkPictureRecorder* recorder) {
980 SkPictureRecorder recorder2;
981
982 SkCanvas* canvas = recorder2.beginRecording(10, 10, NULL, 0);
983
984 recorder->partialReplay(canvas);
985
986 return recorder2.endRecording();
987 }
988};
989
990// Test out SkPictureRecorder::partialReplay
991DEF_TEST(PictureRecorder_replay, reporter) {
992 // check save/saveLayer state
993 {
994 SkPictureRecorder recorder;
995
996 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
997
998 canvas->saveLayer(NULL, NULL);
999
1000 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1001
1002 // The extra save and restore comes from the Copy process.
1003 check_save_state(reporter, copy, 2, 1, 3);
1004
1005 canvas->saveLayer(NULL, NULL);
1006
1007 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1008
1009 check_save_state(reporter, final, 1, 2, 3);
1010
1011 // The copy shouldn't pick up any operations added after it was made
1012 check_save_state(reporter, copy, 2, 1, 3);
1013 }
1014
1015 // (partially) check leakage of draw ops
1016 {
1017 SkPictureRecorder recorder;
1018
1019 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
1020
1021 SkRect r = SkRect::MakeWH(5, 5);
1022 SkPaint p;
1023
1024 canvas->drawRect(r, p);
1025
1026 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder));
1027
1028 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1029
1030 SkBitmap bm;
1031 make_bm(&bm, 10, 10, SK_ColorRED, true);
1032
1033 r.offset(5.0f, 5.0f);
1034 canvas->drawBitmapRectToRect(bm, NULL, r);
1035
1036 SkAutoTUnref<SkPicture> final(recorder.endRecording());
1037 REPORTER_ASSERT(reporter, final->willPlayBackBitmaps());
1038
1039 REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID());
1040
1041 // The snapshot shouldn't pick up any operations added after it was made
1042 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps());
1043 }
1044}
1045#endif
1046
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001047static void test_unbalanced_save_restores(skiatest::Reporter* reporter) {
1048 SkCanvas testCanvas(100, 100);
1049 set_canvas_to_save_count_4(&testCanvas);
1050
1051 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1052
1053 SkPaint paint;
1054 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000);
1055
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001056 SkPictureRecorder recorder;
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001057
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001058 {
1059 // Create picture with 2 unbalanced saves
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001060 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001061 canvas->save();
1062 canvas->translate(10, 10);
1063 canvas->drawRect(rect, paint);
1064 canvas->save();
1065 canvas->translate(10, 10);
1066 canvas->drawRect(rect, paint);
1067 SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording());
1068
robertphillips9b14f262014-06-04 05:40:44 -07001069 testCanvas.drawPicture(extraSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001070 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1071 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001072
1073 set_canvas_to_save_count_4(&testCanvas);
1074
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001075 {
1076 // Create picture with 2 unbalanced restores
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001077 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001078 canvas->save();
1079 canvas->translate(10, 10);
1080 canvas->drawRect(rect, paint);
1081 canvas->save();
1082 canvas->translate(10, 10);
1083 canvas->drawRect(rect, paint);
1084 canvas->restore();
1085 canvas->restore();
1086 canvas->restore();
1087 canvas->restore();
1088 SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording());
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001089
robertphillips9b14f262014-06-04 05:40:44 -07001090 testCanvas.drawPicture(extraRestorePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001091 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1092 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001093
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001094 set_canvas_to_save_count_4(&testCanvas);
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001095
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001096 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001097 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001098 canvas->translate(10, 10);
1099 canvas->drawRect(rect, paint);
1100 SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording());
1101
robertphillips9b14f262014-06-04 05:40:44 -07001102 testCanvas.drawPicture(noSavePicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001103 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
1104 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity());
1105 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001106}
1107
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001108static void test_peephole() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001109 SkRandom rand;
reed@google.com21b519d2012-10-02 17:42:15 +00001110
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001111 SkPictureRecorder recorder;
1112
reed@google.com21b519d2012-10-02 17:42:15 +00001113 for (int j = 0; j < 100; j++) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001114 SkRandom rand2(rand); // remember the seed
reed@google.com21b519d2012-10-02 17:42:15 +00001115
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001116 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
reed@google.com21b519d2012-10-02 17:42:15 +00001117
1118 for (int i = 0; i < 1000; ++i) {
1119 rand_op(canvas, rand);
1120 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001121 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
jvanverth@google.comc490f802013-03-04 13:56:38 +00001122
1123 rand = rand2;
reed@google.com21b519d2012-10-02 17:42:15 +00001124 }
1125
1126 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001127 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
reed@google.com21b519d2012-10-02 17:42:15 +00001128 SkRect rect = SkRect::MakeWH(50, 50);
skia.committer@gmail.com52c24372012-10-03 02:01:13 +00001129
reed@google.com21b519d2012-10-02 17:42:15 +00001130 for (int i = 0; i < 100; ++i) {
1131 canvas->save();
1132 }
1133 while (canvas->getSaveCount() > 1) {
1134 canvas->clipRect(rect);
1135 canvas->restore();
1136 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001137 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
reed@google.com21b519d2012-10-02 17:42:15 +00001138 }
1139}
1140
scroggo@google.com4b90b112012-12-04 15:08:56 +00001141#ifndef SK_DEBUG
1142// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
1143// should never do this.
1144static void test_bad_bitmap() {
1145 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
1146 // fail.
1147 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001148 bm.setInfo(SkImageInfo::MakeN32Premul(100, 100));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001149 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001150 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100, NULL, 0);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001151 recordingCanvas->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001152 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.com4b90b112012-12-04 15:08:56 +00001153
1154 SkCanvas canvas;
robertphillips9b14f262014-06-04 05:40:44 -07001155 canvas.drawPicture(picture);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001156}
1157#endif
1158
reed@google.com672588b2014-01-08 15:42:01 +00001159static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001160 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001161}
1162
1163static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001164 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001165 SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height(), NULL, 0);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001166 canvas->drawBitmap(bitmap, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001167 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1168
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001169 SkDynamicMemoryWStream wStream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001170 picture->serialize(&wStream, &encode_bitmap_to_data);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001171 return wStream.copyToData();
1172}
1173
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001174struct ErrorContext {
1175 int fErrors;
1176 skiatest::Reporter* fReporter;
1177};
1178
1179static void assert_one_parse_error_cb(SkError error, void* context) {
1180 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
1181 errorContext->fErrors++;
1182 // This test only expects one error, and that is a kParseError. If there are others,
1183 // there is some unknown problem.
1184 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors,
1185 "This threw more errors than expected.");
1186 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error,
1187 SkGetLastErrorString());
1188}
1189
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001190static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
1191 // Create a bitmap that will be encoded.
1192 SkBitmap original;
1193 make_bm(&original, 100, 100, SK_ColorBLUE, true);
1194 SkDynamicMemoryWStream wStream;
1195 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
1196 return;
1197 }
1198 SkAutoDataUnref data(wStream.copyToData());
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001199
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001200 SkBitmap bm;
halcanary@google.com3d50ea12014-01-02 13:15:13 +00001201 bool installSuccess = SkInstallDiscardablePixelRef(
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +00001202 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm);
reed@google.combf790232013-12-13 19:45:58 +00001203 REPORTER_ASSERT(reporter, installSuccess);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001204
1205 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
1206 // Flattening original will follow the old path of performing an encode, while flattening bm
1207 // will use the already encoded data.
1208 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
1209 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
1210 REPORTER_ASSERT(reporter, picture1->equals(picture2));
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001211 // Now test that a parse error was generated when trying to create a new SkPicture without
1212 // providing a function to decode the bitmap.
1213 ErrorContext context;
1214 context.fErrors = 0;
1215 context.fReporter = reporter;
1216 SkSetErrorCallback(assert_one_parse_error_cb, &context);
1217 SkMemoryStream pictureStream(picture1);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001218 SkClearLastError();
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001219 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL));
1220 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001221 SkClearLastError();
1222 SkSetErrorCallback(NULL, NULL);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001223}
1224
junov@chromium.org94f20dc2013-01-28 21:04:44 +00001225static void test_clone_empty(skiatest::Reporter* reporter) {
1226 // This is a regression test for crbug.com/172062
1227 // Before the fix, we used to crash accessing a null pointer when we
1228 // had a picture with no paints. This test passes by not crashing.
1229 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001230 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001231 recorder.beginRecording(1, 1, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001232 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1233 SkAutoTUnref<SkPicture> destPicture(picture->clone());
junov@chromium.org94f20dc2013-01-28 21:04:44 +00001234 REPORTER_ASSERT(reporter, NULL != destPicture);
junov@chromium.org94f20dc2013-01-28 21:04:44 +00001235 }
1236}
1237
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001238static void test_draw_empty(skiatest::Reporter* reporter) {
1239 SkBitmap result;
1240 make_bm(&result, 2, 2, SK_ColorBLACK, false);
1241
1242 SkCanvas canvas(result);
1243
1244 {
1245 // stock SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001246 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001247 recorder.beginRecording(1, 1, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001248 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001249
robertphillips9b14f262014-06-04 05:40:44 -07001250 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001251 }
1252
1253 {
1254 // tile grid
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001255 SkTileGridFactory::TileGridInfo gridInfo;
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001256 gridInfo.fMargin.setEmpty();
1257 gridInfo.fOffset.setZero();
1258 gridInfo.fTileInterval.set(1, 1);
1259
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001260 SkTileGridFactory factory(gridInfo);
1261 SkPictureRecorder recorder;
1262 recorder.beginRecording(1, 1, &factory, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001263 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001264
robertphillips9b14f262014-06-04 05:40:44 -07001265 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001266 }
1267
1268 {
1269 // RTree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001270 SkRTreeFactory factory;
1271 SkPictureRecorder recorder;
1272 recorder.beginRecording(1, 1, &factory, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001273 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001274
robertphillips9b14f262014-06-04 05:40:44 -07001275 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001276 }
1277
1278 {
1279 // quad tree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001280 SkQuadTreeFactory factory;
1281 SkPictureRecorder recorder;
1282 recorder.beginRecording(1, 1, &factory, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001283 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001284
robertphillips9b14f262014-06-04 05:40:44 -07001285 canvas.drawPicture(picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001286 }
1287}
1288
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001289static void test_clip_bound_opt(skiatest::Reporter* reporter) {
1290 // Test for crbug.com/229011
1291 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
1292 SkIntToScalar(2), SkIntToScalar(2));
1293 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
1294 SkIntToScalar(1), SkIntToScalar(1));
1295 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
1296 SkIntToScalar(1), SkIntToScalar(1));
1297
1298 SkPath invPath;
1299 invPath.addOval(rect1);
1300 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
1301 SkPath path;
1302 path.addOval(rect2);
1303 SkPath path2;
1304 path2.addOval(rect3);
1305 SkIRect clipBounds;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001306 SkPictureRecorder recorder;
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001307 // Minimalist test set for 100% code coverage of
1308 // SkPictureRecord::updateClipConservativelyUsingBounds
1309 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001310 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001311 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1312 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1313 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1314 REPORTER_ASSERT(reporter, true == nonEmpty);
1315 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1316 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1317 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1318 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1319 }
1320 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001321 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001322 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1323 canvas->clipPath(path, SkRegion::kIntersect_Op);
1324 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1325 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1326 REPORTER_ASSERT(reporter, true == nonEmpty);
1327 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1328 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1329 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1330 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1331 }
1332 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001333 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001334 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1335 canvas->clipPath(path, SkRegion::kIntersect_Op);
1336 canvas->clipPath(invPath, SkRegion::kUnion_Op);
1337 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1338 REPORTER_ASSERT(reporter, true == nonEmpty);
1339 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1340 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1341 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1342 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1343 }
1344 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001345 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001346 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1347 canvas->clipPath(path, SkRegion::kDifference_Op);
1348 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1349 REPORTER_ASSERT(reporter, true == nonEmpty);
1350 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1351 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1352 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1353 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1354 }
1355 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001356 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001357 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1358 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
1359 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1360 // True clip is actually empty in this case, but the best
1361 // determination we can make using only bounds as input is that the
1362 // clip is included in the bounds of 'path'.
1363 REPORTER_ASSERT(reporter, true == nonEmpty);
1364 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1365 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1366 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1367 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1368 }
1369 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001370 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001371 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1372 canvas->clipPath(path, SkRegion::kIntersect_Op);
1373 canvas->clipPath(path2, SkRegion::kXOR_Op);
1374 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1375 REPORTER_ASSERT(reporter, true == nonEmpty);
1376 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
1377 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
1378 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1379 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1380 }
1381}
1382
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001383/**
1384 * A canvas that records the number of clip commands.
1385 */
1386class ClipCountingCanvas : public SkCanvas {
1387public:
commit-bot@chromium.org6d3eaea2014-05-27 23:41:45 +00001388 ClipCountingCanvas(int width, int height)
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001389 : INHERITED(width, height)
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001390 , fClipCount(0){
1391 }
1392
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001393 virtual void onClipRect(const SkRect& r,
1394 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001395 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001396 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001397 this->INHERITED::onClipRect(r, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001398 }
1399
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001400 virtual void onClipRRect(const SkRRect& rrect,
1401 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001402 ClipEdgeStyle edgeStyle)SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001403 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001404 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001405 }
1406
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001407 virtual void onClipPath(const SkPath& path,
1408 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001409 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001410 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001411 this->INHERITED::onClipPath(path, op, edgeStyle);
1412 }
1413
1414 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE {
1415 fClipCount += 1;
1416 this->INHERITED::onClipRegion(deviceRgn, op);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001417 }
1418
1419 unsigned getClipCount() const { return fClipCount; }
1420
1421private:
1422 unsigned fClipCount;
1423
1424 typedef SkCanvas INHERITED;
1425};
1426
1427static void test_clip_expansion(skiatest::Reporter* reporter) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001428 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001429 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001430
1431 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
1432 // The following expanding clip should not be skipped.
1433 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
1434 // Draw something so the optimizer doesn't just fold the world.
1435 SkPaint p;
1436 p.setColor(SK_ColorBLUE);
1437 canvas->drawPaint(p);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001438 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001439
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001440 ClipCountingCanvas testCanvas(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001441 picture->draw(&testCanvas);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001442
1443 // Both clips should be present on playback.
1444 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
1445}
1446
tomhudson@google.com381010e2013-10-24 11:12:47 +00001447static void test_hierarchical(skiatest::Reporter* reporter) {
1448 SkBitmap bm;
1449 make_bm(&bm, 10, 10, SK_ColorRED, true);
1450
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001451 SkPictureRecorder recorder;
tomhudson@google.com381010e2013-10-24 11:12:47 +00001452
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001453 recorder.beginRecording(10, 10, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001454 SkAutoTUnref<SkPicture> childPlain(recorder.endRecording());
1455 REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0
tomhudson@google.com381010e2013-10-24 11:12:47 +00001456
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001457 recorder.beginRecording(10, 10, NULL, 0)->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001458 SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording());
1459 REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1
tomhudson@google.com381010e2013-10-24 11:12:47 +00001460
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001461 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001462 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001463 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001464 SkAutoTUnref<SkPicture> parentPP(recorder.endRecording());
1465 REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0
1466 }
1467 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001468 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001469 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001470 SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording());
1471 REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1
1472 }
1473 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001474 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001475 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001476 canvas->drawPicture(childPlain);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001477 SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording());
1478 REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1
1479 }
1480 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001481 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001482 canvas->drawBitmap(bm, 0, 0);
robertphillips9b14f262014-06-04 05:40:44 -07001483 canvas->drawPicture(childWithBitmap);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001484 SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording());
1485 REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2
1486 }
tomhudson@google.com381010e2013-10-24 11:12:47 +00001487}
1488
robertphillips@google.comd5500882014-04-02 23:51:13 +00001489static void test_gen_id(skiatest::Reporter* reporter) {
1490
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001491 SkPicture empty;
robertphillips@google.comd5500882014-04-02 23:51:13 +00001492
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001493 // Empty pictures should still have a valid ID
1494 REPORTER_ASSERT(reporter, empty.uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001495
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001496 SkPictureRecorder recorder;
robertphillips@google.comd5500882014-04-02 23:51:13 +00001497
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001498 SkCanvas* canvas = recorder.beginRecording(1, 1, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001499 canvas->drawARGB(255, 255, 255, 255);
1500 SkAutoTUnref<SkPicture> hasData(recorder.endRecording());
1501 // picture should have a non-zero id after recording
1502 REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001503
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001504 // both pictures should have different ids
1505 REPORTER_ASSERT(reporter, hasData->uniqueID() != empty.uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001506
1507 // test out copy constructor
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001508 SkPicture copyWithData(*hasData);
1509 REPORTER_ASSERT(reporter, hasData->uniqueID() == copyWithData.uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001510
1511 SkPicture emptyCopy(empty);
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001512 REPORTER_ASSERT(reporter, empty.uniqueID() != emptyCopy.uniqueID());
skia.committer@gmail.coma9157722014-04-03 03:04:26 +00001513
robertphillips@google.comd5500882014-04-02 23:51:13 +00001514 // test out swap
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001515 {
1516 SkPicture swapWithData;
1517 uint32_t beforeID1 = swapWithData.uniqueID();
1518 uint32_t beforeID2 = copyWithData.uniqueID();
1519 swapWithData.swap(copyWithData);
1520 REPORTER_ASSERT(reporter, copyWithData.uniqueID() == beforeID1);
1521 REPORTER_ASSERT(reporter, swapWithData.uniqueID() == beforeID2);
1522 }
robertphillips@google.comd5500882014-04-02 23:51:13 +00001523
1524 // test out clone
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001525 {
1526 SkAutoTUnref<SkPicture> cloneWithData(hasData->clone());
1527 REPORTER_ASSERT(reporter, hasData->uniqueID() == cloneWithData->uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001528
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001529 SkAutoTUnref<SkPicture> emptyClone(empty.clone());
1530 REPORTER_ASSERT(reporter, empty.uniqueID() != emptyClone->uniqueID());
1531 }
robertphillips@google.comd5500882014-04-02 23:51:13 +00001532}
1533
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00001534DEF_TEST(Picture, reporter) {
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001535#ifdef SK_DEBUG
1536 test_deleting_empty_playback();
1537 test_serializing_empty_picture();
scroggo@google.com4b90b112012-12-04 15:08:56 +00001538#else
1539 test_bad_bitmap();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001540#endif
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001541 test_unbalanced_save_restores(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001542 test_peephole();
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001543#if SK_SUPPORT_GPU
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +00001544 test_gpu_veto(reporter);
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001545#endif
reed@google.comfe7b1ed2012-11-29 21:00:39 +00001546 test_gatherpixelrefs(reporter);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +00001547 test_gatherpixelrefsandrects(reporter);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001548 test_bitmap_with_encoded_data(reporter);
junov@chromium.org94f20dc2013-01-28 21:04:44 +00001549 test_clone_empty(reporter);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001550 test_draw_empty(reporter);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001551 test_clip_bound_opt(reporter);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001552 test_clip_expansion(reporter);
tomhudson@google.com381010e2013-10-24 11:12:47 +00001553 test_hierarchical(reporter);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001554 test_gen_id(reporter);
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001555}
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001556
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001557#if SK_SUPPORT_GPU
1558DEF_GPUTEST(GPUPicture, reporter, factory) {
1559 test_gpu_picture_optimization(reporter, factory);
1560}
1561#endif
1562
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001563static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) {
1564 const SkPaint paint;
1565 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f };
1566 const SkIRect irect = { 2, 2, 3, 3 };
1567
1568 // Don't care what these record, as long as they're legal.
1569 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint);
1570 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag);
1571 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint);
1572 canvas->drawBitmapNine(bitmap, irect, rect, &paint);
1573 canvas->drawSprite(bitmap, 1, 1);
1574}
1575
1576static void test_draw_bitmaps(SkCanvas* canvas) {
1577 SkBitmap empty;
1578 draw_bitmaps(empty, canvas);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001579 empty.setInfo(SkImageInfo::MakeN32Premul(10, 10));
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001580 draw_bitmaps(empty, canvas);
1581}
1582
1583DEF_TEST(Picture_EmptyBitmap, r) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001584 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001585 test_draw_bitmaps(recorder.beginRecording(10, 10, NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001586 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001587}
1588
1589DEF_TEST(Canvas_EmptyBitmap, r) {
1590 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001591 dst.allocN32Pixels(10, 10);
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001592 SkCanvas canvas(dst);
1593
1594 test_draw_bitmaps(&canvas);
1595}