blob: 071e6f4129f9045c3b80cefa4abb684d21466915 [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#if SK_SUPPORT_GPU
10#include "SkBlurImageFilter.h"
11#endif
reed@google.com21b519d2012-10-02 17:42:15 +000012#include "SkCanvas.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000013#include "SkColorPriv.h"
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +000014#include "SkDashPathEffect.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000015#include "SkData.h"
reed@google.combf790232013-12-13 19:45:58 +000016#include "SkDecodingImageGenerator.h"
scroggo@google.com49ce11b2013-04-25 18:29:32 +000017#include "SkError.h"
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000018#if SK_SUPPORT_GPU
19#include "SkGpuDevice.h"
20#endif
halcanary@google.com3d50ea12014-01-02 13:15:13 +000021#include "SkImageEncoder.h"
22#include "SkImageGenerator.h"
reed@google.com21b519d2012-10-02 17:42:15 +000023#include "SkPaint.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000024#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000025#include "SkPictureRecorder.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000026#include "SkPictureUtils.h"
reed@google.com72aa79c2013-01-24 18:27:42 +000027#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000028#include "SkRandom.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000029#include "SkShader.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000030#include "SkStream.h"
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000031
32#if SK_SUPPORT_GPU
33#include "SkSurface.h"
34#include "GrContextFactory.h"
35#include "GrPictureUtils.h"
36#endif
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000037#include "Test.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000038
robertphillips@google.comed9866c2014-01-09 19:20:45 +000039static const int gColorScale = 30;
40static const int gColorOffset = 60;
reed@google.comfe7b1ed2012-11-29 21:00:39 +000041
42static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000043 bm->allocN32Pixels(w, h);
reed@google.comfe7b1ed2012-11-29 21:00:39 +000044 bm->eraseColor(color);
45 if (immutable) {
46 bm->setImmutable();
47 }
48}
49
robertphillips@google.comfe5824a2014-01-09 19:45:29 +000050static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000051 SkASSERT(w % 2 == 0);
52 SkASSERT(h % 2 == 0);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000053 bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType,
54 kPremul_SkAlphaType));
robertphillips@google.comed9866c2014-01-09 19:20:45 +000055 SkAutoLockPixels lock(*bm);
56 for (int y = 0; y < h; y += 2) {
57 uint8_t* s = bm->getAddr8(0, y);
58 for (int x = 0; x < w; x += 2) {
59 *s++ = 0xFF;
60 *s++ = 0x00;
61 }
62 s = bm->getAddr8(0, y + 1);
63 for (int x = 0; x < w; x += 2) {
64 *s++ = 0x00;
65 *s++ = 0xFF;
66 }
67 }
68 if (immutable) {
69 bm->setImmutable();
70 }
71}
reed@google.comfe7b1ed2012-11-29 21:00:39 +000072
robertphillips@google.comed9866c2014-01-09 19:20:45 +000073static void init_paint(SkPaint* paint, const SkBitmap &bm) {
74 SkShader* shader = SkShader::CreateBitmapShader(bm,
75 SkShader::kClamp_TileMode,
76 SkShader::kClamp_TileMode);
77 paint->setShader(shader)->unref();
78}
79
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +000080typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000081 const SkBitmap&, const SkPoint&,
82 SkTDArray<SkPixelRef*>* usedPixRefs);
robertphillips@google.comed9866c2014-01-09 19:20:45 +000083
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000084static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000085 const SkBitmap& altBM, const SkPoint& pos,
86 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000087 SkPaint paint;
88 init_paint(&paint, bm);
89
90 canvas->drawPaint(paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000091 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +000092}
93
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000094static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000095 const SkBitmap& altBM, const SkPoint& pos,
96 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000097 SkPaint paint;
98 init_paint(&paint, bm);
99
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000100 // draw a rect
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000101 SkPoint points[5] = {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000102 { pos.fX, pos.fY },
103 { pos.fX + bm.width() - 1, pos.fY },
104 { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 },
105 { pos.fX, pos.fY + bm.height() - 1 },
106 { pos.fX, pos.fY },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000107 };
108
109 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000110 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000111}
112
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000113static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000114 const SkBitmap& altBM, const SkPoint& pos,
115 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000116 SkPaint paint;
117 init_paint(&paint, bm);
118
119 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
120 r.offset(pos.fX, pos.fY);
121
122 canvas->drawRect(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000123 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000124}
125
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000126static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000127 const SkBitmap& altBM, const SkPoint& pos,
128 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000129 SkPaint paint;
130 init_paint(&paint, bm);
131
132 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
133 r.offset(pos.fX, pos.fY);
134
135 canvas->drawOval(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000136 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000137}
138
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000139static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000140 const SkBitmap& altBM, const SkPoint& pos,
141 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000142 SkPaint paint;
143 init_paint(&paint, bm);
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000144
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000145 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
146 r.offset(pos.fX, pos.fY);
147
148 SkRRect rr;
149 rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4);
150 canvas->drawRRect(rr, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000151 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000152}
153
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000154static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000155 const SkBitmap& altBM, const SkPoint& pos,
156 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000157 SkPaint paint;
158 init_paint(&paint, bm);
159
160 SkPath path;
161 path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height()));
162 path.lineTo(SkIntToScalar(bm.width()), 0);
163 path.close();
164 path.offset(pos.fX, pos.fY);
165
166 canvas->drawPath(path, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000167 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000168}
169
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000170static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000171 const SkBitmap& altBM, const SkPoint& pos,
172 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000173 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000174 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000175}
176
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000177static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000178 const SkBitmap& altBM, const SkPoint& pos,
179 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000180 SkPaint paint;
181 init_paint(&paint, bm);
182
183 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
184 canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000185 *usedPixRefs->append() = bm.pixelRef();
186 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000187}
188
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000189static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000190 const SkBitmap& altBM, const SkPoint& pos,
191 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000192 const SkMatrix& ctm = canvas->getTotalMatrix();
193
194 SkPoint p(pos);
195 ctm.mapPoints(&p, 1);
196
197 canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000198 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000199}
200
201#if 0
202// Although specifiable, this case doesn't seem to make sense (i.e., the
203// bitmap in the shader is never used).
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000204static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000205 const SkBitmap& altBM, const SkPoint& pos,
206 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000207 SkPaint paint;
208 init_paint(&paint, bm);
209
210 const SkMatrix& ctm = canvas->getTotalMatrix();
211
212 SkPoint p(pos);
213 ctm.mapPoints(&p, 1);
214
215 canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000216 *usedPixRefs->append() = bm.pixelRef();
217 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000218}
219#endif
220
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000221static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000222 const SkBitmap& altBM, const SkPoint& pos,
223 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000224 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
225
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000226 r.offset(pos.fX, pos.fY);
227 canvas->drawBitmapRectToRect(bm, NULL, r, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000228 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000229}
230
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000231static void drawbitmaprect_withshader_proc(SkCanvas* canvas,
232 const SkBitmap& bm,
233 const SkBitmap& altBM,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000234 const SkPoint& pos,
235 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000236 SkPaint paint;
237 init_paint(&paint, bm);
238
239 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000240 r.offset(pos.fX, pos.fY);
241
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000242 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
243 canvas->drawBitmapRectToRect(altBM, NULL, r, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000244 *usedPixRefs->append() = bm.pixelRef();
245 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000246}
247
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000248static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000249 const SkBitmap& altBM, const SkPoint& pos,
250 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000251 SkPaint paint;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000252 init_paint(&paint, bm);
253 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
254
255 canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000256 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000257}
258
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000259static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000260 const SkBitmap& altBM, const SkPoint& pos,
261 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000262 SkPaint paint;
263 init_paint(&paint, bm);
264 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
265
266 SkPoint point = { pos.fX, pos.fY + bm.height() };
267 canvas->drawPosText("O", 1, &point, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000268 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000269}
270
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000271static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000272 const SkBitmap& altBM, const SkPoint& pos,
273 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000274 SkPaint paint;
275
276 init_paint(&paint, bm);
277 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
278
279 SkPath path;
280 path.lineTo(SkIntToScalar(bm.width()), 0);
281 path.offset(pos.fX, pos.fY+bm.height());
282
283 canvas->drawTextOnPath("O", 1, path, NULL, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000284 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000285}
286
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000287static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000288 const SkBitmap& altBM, const SkPoint& pos,
289 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000290 SkPaint paint;
291 init_paint(&paint, bm);
292
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000293 SkPoint verts[4] = {
294 { pos.fX, pos.fY },
295 { pos.fX + bm.width(), pos.fY },
296 { pos.fX + bm.width(), pos.fY + bm.height() },
297 { pos.fX, pos.fY + bm.height() }
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000298 };
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000299 SkPoint texs[4] = { { 0, 0 },
300 { SkIntToScalar(bm.width()), 0 },
301 { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000302 { 0, SkIntToScalar(bm.height()) } };
303 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 };
304
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000305 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL,
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000306 indices, 6, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000307 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000308}
309
310// Return a picture with the bitmaps drawn at the specified positions.
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000311static SkPicture* record_bitmaps(const SkBitmap bm[],
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000312 const SkPoint pos[],
313 SkTDArray<SkPixelRef*> analytic[],
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000314 int count,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000315 DrawBitmapProc proc) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000316 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000317 SkCanvas* canvas = recorder.beginRecording(1000, 1000, NULL, 0);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000318 for (int i = 0; i < count; ++i) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000319 analytic[i].rewind();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000320 canvas->save();
321 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY,
322 SkIntToScalar(bm[i].width()),
323 SkIntToScalar(bm[i].height()));
324 canvas->clipRect(clipRect, SkRegion::kIntersect_Op);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000325 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000326 canvas->restore();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000327 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000328 return recorder.endRecording();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000329}
330
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000331static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000332 rect->fLeft = rand.nextRangeScalar(-W, 2*W);
333 rect->fTop = rand.nextRangeScalar(-H, 2*H);
334 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W);
335 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H);
336
337 // we integralize rect to make our tests more predictable, since Gather is
338 // a little sloppy.
339 SkIRect ir;
340 rect->round(&ir);
341 rect->set(ir);
342}
343
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000344static void draw(SkPicture* pic, int width, int height, SkBitmap* result) {
345 make_bm(result, width, height, SK_ColorBLACK, false);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000346
347 SkCanvas canvas(*result);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000348 canvas.drawPicture(*pic);
349}
350
351template <typename T> int find_index(const T* array, T elem, int count) {
352 for (int i = 0; i < count; ++i) {
353 if (array[i] == elem) {
354 return i;
355 }
356 }
357 return -1;
358}
359
360// Return true if 'ref' is found in array[]
361static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) {
362 return find_index<const SkPixelRef*>(array, ref, count) >= 0;
363}
364
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000365// Look at each pixel that is inside 'subset', and if its color appears in
366// colors[], find the corresponding value in refs[] and append that ref into
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000367// array, skipping duplicates of the same value.
368// Note that gathering pixelRefs from rendered colors suffers from the problem
369// that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color)
370// isn't easy to reconstruct.
371static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[],
372 int count, SkTDArray<SkPixelRef*>* array,
373 const SkRect& subset) {
374 SkIRect ir;
375 subset.roundOut(&ir);
376
377 if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) {
378 return;
379 }
380
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000381 // Since we only want to return unique values in array, when we scan we just
382 // set a bit for each index'd color found. In practice we only have a few
383 // distinct colors, so we just use an int's bits as our array. Hence the
384 // assert that count <= number-of-bits-in-our-int.
385 SkASSERT((unsigned)count <= 32);
386 uint32_t bitarray = 0;
387
388 SkAutoLockPixels alp(bm);
389
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000390 for (int y = ir.fTop; y < ir.fBottom; ++y) {
391 for (int x = ir.fLeft; x < ir.fRight; ++x) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000392 SkPMColor pmc = *bm.getAddr32(x, y);
393 // the only good case where the color is not found would be if
394 // the color is transparent, meaning no bitmap was drawn in that
395 // pixel.
396 if (pmc) {
bsalomon@google.comc3d753e2013-01-08 17:24:44 +0000397 uint32_t index = SkGetPackedR32(pmc);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000398 SkASSERT(SkGetPackedG32(pmc) == index);
399 SkASSERT(SkGetPackedB32(pmc) == index);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000400 if (0 == index) {
401 continue; // background color
402 }
403 SkASSERT(0 == (index - gColorOffset) % gColorScale);
404 index = (index - gColorOffset) / gColorScale;
bsalomon@google.com5f429b02013-01-08 18:42:20 +0000405 SkASSERT(static_cast<int>(index) < count);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000406 bitarray |= 1 << index;
407 }
408 }
409 }
410
411 for (int i = 0; i < count; ++i) {
412 if (bitarray & (1 << i)) {
413 *array->append() = refs[i];
414 }
415 }
416}
417
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000418static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h,
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000419 const SkTDArray<SkPixelRef*> analytic[],
420 int count,
421 SkTDArray<SkPixelRef*>* result,
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000422 const SkRect& subset) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000423 for (int i = 0; i < count; ++i) {
424 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h);
425
426 if (SkRect::Intersects(subset, rect)) {
427 result->append(analytic[i].count(), analytic[i].begin());
428 }
429 }
430}
431
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000432
433static const struct {
434 const DrawBitmapProc proc;
435 const char* const desc;
436} gProcs[] = {
437 {drawpaint_proc, "drawpaint"},
438 {drawpoints_proc, "drawpoints"},
439 {drawrect_proc, "drawrect"},
440 {drawoval_proc, "drawoval"},
441 {drawrrect_proc, "drawrrect"},
442 {drawpath_proc, "drawpath"},
443 {drawbitmap_proc, "drawbitmap"},
444 {drawbitmap_withshader_proc, "drawbitmap_withshader"},
445 {drawsprite_proc, "drawsprite"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000446#if 0
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000447 {drawsprite_withshader_proc, "drawsprite_withshader"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000448#endif
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000449 {drawbitmaprect_proc, "drawbitmaprect"},
450 {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"},
451 {drawtext_proc, "drawtext"},
452 {drawpostext_proc, "drawpostext"},
453 {drawtextonpath_proc, "drawtextonpath"},
454 {drawverts_proc, "drawverts"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000455};
456
457static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000458 // Our convention is that the color components contain an encoding of
459 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000460 // reserved for the background
461 for (int i = 0; i < num; ++i) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000462 make_bm(&bm[i], w, h,
463 SkColorSetARGB(0xFF,
464 gColorScale*i+gColorOffset,
465 gColorScale*i+gColorOffset,
466 gColorScale*i+gColorOffset),
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000467 true);
468 refs[i] = bm[i].pixelRef();
469 }
470
471 // The A8 alternate bitmaps are all BW checkerboards
472 for (int i = 0; i < num; ++i) {
473 make_checkerboard(&bm[num+i], w, h, true);
474 refs[num+i] = bm[num+i].pixelRef();
475 }
476}
477
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000478static void test_gatherpixelrefs(skiatest::Reporter* reporter) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000479 const int IW = 32;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000480 const int IH = IW;
481 const SkScalar W = SkIntToScalar(IW);
482 const SkScalar H = W;
483
484 static const int N = 4;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000485 SkBitmap bm[2*N];
486 SkPixelRef* refs[2*N];
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000487 SkTDArray<SkPixelRef*> analytic[N];
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000488
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000489 const SkPoint pos[N] = {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000490 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
491 };
492
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000493 create_textures(bm, refs, N, IW, IH);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000494
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000495 SkRandom rand;
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000496 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000497 SkAutoTUnref<SkPicture> pic(
498 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000499
tomhudson@google.com381010e2013-10-24 11:12:47 +0000500 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000501 // quick check for a small piece of each quadrant, which should just
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000502 // contain 1 or 2 bitmaps.
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000503 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
504 SkRect r;
505 r.set(2, 2, W - 2, H - 2);
506 r.offset(pos[i].fX, pos[i].fY);
507 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r));
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000508 if (!data) {
509 ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned "
510 "NULL for %s.", gProcs[k].desc);
511 continue;
512 }
513 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data();
514 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*));
515 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
516 if (1 == count) {
517 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
518 } else if (2 == count) {
519 REPORTER_ASSERT(reporter,
520 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
521 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
commit-bot@chromium.orgfe433c12013-07-09 16:04:32 +0000522 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000523 }
524
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000525 SkBitmap image;
526 draw(pic, 2*IW, 2*IH, &image);
527
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000528 // Test a bunch of random (mostly) rects, and compare the gather results
529 // with a deduced list of refs by looking at the colors drawn.
530 for (int j = 0; j < 100; ++j) {
531 SkRect r;
532 rand_rect(&r, rand, 2*W, 2*H);
533
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000534 SkTDArray<SkPixelRef*> fromImage;
535 gather_from_image(image, refs, N, &fromImage, r);
536
537 SkTDArray<SkPixelRef*> fromAnalytic;
538 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000539
540 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
541 size_t dataSize = data ? data->size() : 0;
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000542 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000543 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize);
544 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL;
545 SkAutoDataUnref adu(data);
546
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000547 // Everything that we saw drawn should appear in the analytic list
548 // but the analytic list may contain some pixelRefs that were not
549 // seen in the image (e.g., A8 textures used as masks)
550 for (int i = 0; i < fromImage.count(); ++i) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000551 if (-1 == fromAnalytic.find(fromImage[i])) {
552 ERRORF(reporter, "PixelRef missing %d %s",
553 i, gProcs[k].desc);
554 }
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000555 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000556
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000557 /*
558 * GatherPixelRefs is conservative, so it can return more bitmaps
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000559 * than are strictly required. Thus our check here is only that
560 * Gather didn't miss any that we actually needed. Even that isn't
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000561 * a strict requirement on Gather, which is meant to be quick and
562 * only mostly-correct, but at the moment this test should work.
563 */
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000564 for (int i = 0; i < fromAnalytic.count(); ++i) {
565 bool found = find(gatherRefs, fromAnalytic[i], gatherCount);
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000566 if (!found) {
567 ERRORF(reporter, "PixelRef missing %d %s",
568 i, gProcs[k].desc);
569 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000570#if 0
571 // enable this block of code to debug failures, as it will rerun
572 // the case that failed.
573 if (!found) {
574 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
575 size_t dataSize = data ? data->size() : 0;
576 }
577#endif
578 }
579 }
580 }
581}
582
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000583static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) {
584 const int IW = 32;
585 const int IH = IW;
586 const SkScalar W = SkIntToScalar(IW);
587 const SkScalar H = W;
588
589 static const int N = 4;
590 SkBitmap bm[2*N];
591 SkPixelRef* refs[2*N];
592 SkTDArray<SkPixelRef*> analytic[N];
593
594 const SkPoint pos[N] = {
595 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
596 };
597
598 create_textures(bm, refs, N, IW, IH);
599
600 SkRandom rand;
601 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000602 SkAutoTUnref<SkPicture> pic(
603 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000604
605 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
606
607 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont(
608 new SkPictureUtils::SkPixelRefsAndRectsList);
609
610 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont);
611
612 // quick check for a small piece of each quadrant, which should just
613 // contain 1 or 2 bitmaps.
614 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
615 SkRect r;
616 r.set(2, 2, W - 2, H - 2);
617 r.offset(pos[i].fX, pos[i].fY);
618
619 SkTDArray<SkPixelRef*> gatheredRefs;
620 prCont->query(r, &gatheredRefs);
621
622 int count = gatheredRefs.count();
623 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
624 if (1 == count) {
625 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
626 } else if (2 == count) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000627 REPORTER_ASSERT(reporter,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000628 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
629 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
630 }
631 }
632
633 SkBitmap image;
634 draw(pic, 2*IW, 2*IH, &image);
635
636 // Test a bunch of random (mostly) rects, and compare the gather results
637 // with the analytic results and the pixel refs seen in a rendering.
638 for (int j = 0; j < 100; ++j) {
639 SkRect r;
640 rand_rect(&r, rand, 2*W, 2*H);
641
642 SkTDArray<SkPixelRef*> fromImage;
643 gather_from_image(image, refs, N, &fromImage, r);
644
645 SkTDArray<SkPixelRef*> fromAnalytic;
646 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
647
648 SkTDArray<SkPixelRef*> gatheredRefs;
649 prCont->query(r, &gatheredRefs);
650
651 // Everything that we saw drawn should appear in the analytic list
652 // but the analytic list may contain some pixelRefs that were not
653 // seen in the image (e.g., A8 textures used as masks)
654 for (int i = 0; i < fromImage.count(); ++i) {
655 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]));
656 }
657
658 // Everything in the analytic list should appear in the gathered
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000659 // list.
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000660 for (int i = 0; i < fromAnalytic.count(); ++i) {
661 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i]));
662 }
663 }
664 }
665}
666
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000667#ifdef SK_DEBUG
668// Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only
669// run in debug mode.
670static void test_deleting_empty_playback() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000671 SkPictureRecorder recorder;
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000672 // Creates an SkPictureRecord
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000673 recorder.beginRecording(0, 0, NULL, 0);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000674 // Turns that into an SkPicturePlayback
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000675 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000676 // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000677 recorder.beginRecording(0, 0, NULL, 0);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000678}
679
680// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
681static void test_serializing_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000682 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000683 recorder.beginRecording(0, 0, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000684 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000685 SkDynamicMemoryWStream stream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000686 picture->serialize(&stream);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000687}
688#endif
689
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000690static void rand_op(SkCanvas* canvas, SkRandom& rand) {
reed@google.com21b519d2012-10-02 17:42:15 +0000691 SkPaint paint;
692 SkRect rect = SkRect::MakeWH(50, 50);
693
694 SkScalar unit = rand.nextUScalar1();
695 if (unit <= 0.3) {
696// SkDebugf("save\n");
697 canvas->save();
698 } else if (unit <= 0.6) {
699// SkDebugf("restore\n");
700 canvas->restore();
701 } else if (unit <= 0.9) {
702// SkDebugf("clip\n");
703 canvas->clipRect(rect);
704 } else {
705// SkDebugf("draw\n");
706 canvas->drawPaint(paint);
707 }
708}
709
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000710#if SK_SUPPORT_GPU
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000711static void test_gpu_veto(skiatest::Reporter* reporter) {
712
713 SkPictureRecorder recorder;
714
715 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
716 {
717 SkPath path;
718 path.moveTo(0, 0);
719 path.lineTo(50, 50);
720
721 SkScalar intervals[] = { 1.0f, 1.0f };
722 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0));
723
724 SkPaint paint;
725 paint.setStyle(SkPaint::kStroke_Style);
726 paint.setPathEffect(dash);
727
728 canvas->drawPath(path, paint);
729 }
730 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
731 // path effects currently render an SkPicture undesireable for GPU rendering
732 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
733
734 canvas = recorder.beginRecording(100, 100, NULL, 0);
735 {
736 SkPath path;
737
738 path.moveTo(0, 0);
739 path.lineTo(0, 50);
740 path.lineTo(25, 25);
741 path.lineTo(50, 50);
742 path.lineTo(50, 0);
743 path.close();
744 REPORTER_ASSERT(reporter, !path.isConvex());
745
746 SkPaint paint;
747 paint.setAntiAlias(true);
748 for (int i = 0; i < 50; ++i) {
749 canvas->drawPath(path, paint);
750 }
751 }
752 picture.reset(recorder.endRecording());
753 // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering
754 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
755
756 canvas = recorder.beginRecording(100, 100, NULL, 0);
757 {
758 SkPath path;
759
760 path.moveTo(0, 0);
761 path.lineTo(0, 50);
762 path.lineTo(25, 25);
763 path.lineTo(50, 50);
764 path.lineTo(50, 0);
765 path.close();
766 REPORTER_ASSERT(reporter, !path.isConvex());
767
768 SkPaint paint;
769 paint.setAntiAlias(true);
770 paint.setStyle(SkPaint::kStroke_Style);
771 paint.setStrokeWidth(0);
772 for (int i = 0; i < 50; ++i) {
773 canvas->drawPath(path, paint);
774 }
775 }
776 picture.reset(recorder.endRecording());
777 // hairline stroked AA concave paths are fine for GPU rendering
778 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL));
779}
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000780
781static void test_gpu_picture_optimization(skiatest::Reporter* reporter,
782 GrContextFactory* factory) {
783
784 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
785
786 static const int kWidth = 100;
787 static const int kHeight = 100;
788
789 SkAutoTUnref<SkPicture> pict;
790
791 // create a picture with the structure:
792 // 1)
793 // SaveLayer
794 // Restore
795 // 2)
796 // SaveLayer
skia.committer@gmail.coma5b068c2014-05-07 03:04:15 +0000797 // Translate
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000798 // SaveLayer w/ bound
799 // Restore
800 // Restore
801 // 3)
802 // SaveLayer w/ copyable paint
803 // Restore
804 // 4)
805 // SaveLayer w/ non-copyable paint
806 // Restore
807 {
808 SkPictureRecorder recorder;
809
810 SkCanvas* c = recorder.beginRecording(kWidth, kHeight, NULL, 0);
811 // 1)
812 c->saveLayer(NULL, NULL);
813 c->restore();
814
815 // 2)
816 c->saveLayer(NULL, NULL);
817 c->translate(kWidth/2, kHeight/2);
818 SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2);
819 c->saveLayer(&r, NULL);
820 c->restore();
821 c->restore();
822
823 // 3)
824 {
825 SkPaint p;
826 p.setColor(SK_ColorRED);
827 c->saveLayer(NULL, &p);
828 c->restore();
829 }
830 // 4)
831 // TODO: this case will need to be removed once the paint's are immutable
832 {
833 SkPaint p;
834 SkBitmap bmp;
835 bmp.allocN32Pixels(10, 10);
836 bmp.eraseColor(SK_ColorGREEN);
837 bmp.setAlphaType(kOpaque_SkAlphaType);
838 SkShader* shader = SkShader::CreateBitmapShader(bmp,
839 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
840 p.setShader(shader)->unref();
841
842 c->saveLayer(NULL, &p);
843 c->restore();
844 }
845
846 pict.reset(recorder.endRecording());
847 }
848
849 // Now test out the SaveLayer extraction
850 {
851 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
852
853 SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info));
854
skia.committer@gmail.coma5b068c2014-05-07 03:04:15 +0000855 SkCanvas* canvas = surface->getCanvas();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000856
857 canvas->EXPERIMENTAL_optimize(pict);
858
859 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
860
861 const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key);
862 REPORTER_ASSERT(reporter, NULL != data);
863
864 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
865 REPORTER_ASSERT(reporter, 5 == gpuData->numSaveLayers());
866
867 const GPUAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0);
868 // The parent/child layer appear in reverse order
869 const GPUAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2);
870 const GPUAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1);
871 const GPUAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3);
872 const GPUAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(4);
873
874 REPORTER_ASSERT(reporter, info0.fValid);
875 REPORTER_ASSERT(reporter, kWidth == info0.fSize.fWidth && kHeight == info0.fSize.fHeight);
876 REPORTER_ASSERT(reporter, info0.fCTM.isIdentity());
877 REPORTER_ASSERT(reporter, 0 == info0.fOffset.fX && 0 == info0.fOffset.fY);
878 REPORTER_ASSERT(reporter, NULL != info0.fPaint);
879 REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers);
880
881 REPORTER_ASSERT(reporter, info1.fValid);
882 REPORTER_ASSERT(reporter, kWidth == info1.fSize.fWidth && kHeight == info1.fSize.fHeight);
883 REPORTER_ASSERT(reporter, info1.fCTM.isIdentity());
884 REPORTER_ASSERT(reporter, 0 == info1.fOffset.fX && 0 == info1.fOffset.fY);
885 REPORTER_ASSERT(reporter, NULL != info1.fPaint);
886 REPORTER_ASSERT(reporter, !info1.fIsNested && info1.fHasNestedLayers); // has a nested SL
887
888 REPORTER_ASSERT(reporter, info2.fValid);
skia.committer@gmail.coma5b068c2014-05-07 03:04:15 +0000889 REPORTER_ASSERT(reporter, kWidth/2 == info2.fSize.fWidth &&
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000890 kHeight/2 == info2.fSize.fHeight); // bound reduces size
891 REPORTER_ASSERT(reporter, info2.fCTM.isIdentity()); // translated
892 REPORTER_ASSERT(reporter, 0 == info2.fOffset.fX && 0 == info2.fOffset.fY);
893 REPORTER_ASSERT(reporter, NULL != info1.fPaint);
894 REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested
895
896 REPORTER_ASSERT(reporter, info3.fValid);
897 REPORTER_ASSERT(reporter, kWidth == info3.fSize.fWidth && kHeight == info3.fSize.fHeight);
898 REPORTER_ASSERT(reporter, info3.fCTM.isIdentity());
899 REPORTER_ASSERT(reporter, 0 == info3.fOffset.fX && 0 == info3.fOffset.fY);
900 REPORTER_ASSERT(reporter, NULL != info3.fPaint);
901 REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers);
902
903 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);
909 }
910}
911
robertphillips@google.comb950c6f2014-04-25 00:02:12 +0000912#endif
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000913
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000914static void set_canvas_to_save_count_4(SkCanvas* canvas) {
915 canvas->restoreToCount(1);
916 canvas->save();
917 canvas->save();
918 canvas->save();
919}
920
921static void test_unbalanced_save_restores(skiatest::Reporter* reporter) {
922 SkCanvas testCanvas(100, 100);
923 set_canvas_to_save_count_4(&testCanvas);
924
925 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
926
927 SkPaint paint;
928 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000);
929
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000930 SkPictureRecorder recorder;
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000931
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000932 {
933 // Create picture with 2 unbalanced saves
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000934 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000935 canvas->save();
936 canvas->translate(10, 10);
937 canvas->drawRect(rect, paint);
938 canvas->save();
939 canvas->translate(10, 10);
940 canvas->drawRect(rect, paint);
941 SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording());
942
943 testCanvas.drawPicture(*extraSavePicture);
944 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
945 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000946
947 set_canvas_to_save_count_4(&testCanvas);
948
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000949 {
950 // Create picture with 2 unbalanced restores
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000951 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000952 canvas->save();
953 canvas->translate(10, 10);
954 canvas->drawRect(rect, paint);
955 canvas->save();
956 canvas->translate(10, 10);
957 canvas->drawRect(rect, paint);
958 canvas->restore();
959 canvas->restore();
960 canvas->restore();
961 canvas->restore();
962 SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording());
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000963
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000964 testCanvas.drawPicture(*extraRestorePicture);
965 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
966 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000967
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000968 set_canvas_to_save_count_4(&testCanvas);
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000969
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000970 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000971 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000972 canvas->translate(10, 10);
973 canvas->drawRect(rect, paint);
974 SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording());
975
976 testCanvas.drawPicture(*noSavePicture);
977 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
978 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity());
979 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000980}
981
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000982static void test_peephole() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000983 SkRandom rand;
reed@google.com21b519d2012-10-02 17:42:15 +0000984
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000985 SkPictureRecorder recorder;
986
reed@google.com21b519d2012-10-02 17:42:15 +0000987 for (int j = 0; j < 100; j++) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000988 SkRandom rand2(rand); // remember the seed
reed@google.com21b519d2012-10-02 17:42:15 +0000989
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000990 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
reed@google.com21b519d2012-10-02 17:42:15 +0000991
992 for (int i = 0; i < 1000; ++i) {
993 rand_op(canvas, rand);
994 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000995 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
jvanverth@google.comc490f802013-03-04 13:56:38 +0000996
997 rand = rand2;
reed@google.com21b519d2012-10-02 17:42:15 +0000998 }
999
1000 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001001 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
reed@google.com21b519d2012-10-02 17:42:15 +00001002 SkRect rect = SkRect::MakeWH(50, 50);
skia.committer@gmail.com52c24372012-10-03 02:01:13 +00001003
reed@google.com21b519d2012-10-02 17:42:15 +00001004 for (int i = 0; i < 100; ++i) {
1005 canvas->save();
1006 }
1007 while (canvas->getSaveCount() > 1) {
1008 canvas->clipRect(rect);
1009 canvas->restore();
1010 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001011 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
reed@google.com21b519d2012-10-02 17:42:15 +00001012 }
1013}
1014
scroggo@google.com4b90b112012-12-04 15:08:56 +00001015#ifndef SK_DEBUG
1016// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
1017// should never do this.
1018static void test_bad_bitmap() {
1019 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
1020 // fail.
1021 SkBitmap bm;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001022 bm.setConfig(SkImageInfo::MakeN32Premul(100, 100));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001023 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001024 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100, NULL, 0);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001025 recordingCanvas->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001026 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.com4b90b112012-12-04 15:08:56 +00001027
1028 SkCanvas canvas;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001029 canvas.drawPicture(*picture);
scroggo@google.com4b90b112012-12-04 15:08:56 +00001030}
1031#endif
1032
reed@google.com672588b2014-01-08 15:42:01 +00001033static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +00001034 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001035}
1036
1037static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001038 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001039 SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height(), NULL, 0);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001040 canvas->drawBitmap(bitmap, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001041 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1042
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001043 SkDynamicMemoryWStream wStream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001044 picture->serialize(&wStream, &encode_bitmap_to_data);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001045 return wStream.copyToData();
1046}
1047
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001048struct ErrorContext {
1049 int fErrors;
1050 skiatest::Reporter* fReporter;
1051};
1052
1053static void assert_one_parse_error_cb(SkError error, void* context) {
1054 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
1055 errorContext->fErrors++;
1056 // This test only expects one error, and that is a kParseError. If there are others,
1057 // there is some unknown problem.
1058 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors,
1059 "This threw more errors than expected.");
1060 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error,
1061 SkGetLastErrorString());
1062}
1063
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001064static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
1065 // Create a bitmap that will be encoded.
1066 SkBitmap original;
1067 make_bm(&original, 100, 100, SK_ColorBLUE, true);
1068 SkDynamicMemoryWStream wStream;
1069 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
1070 return;
1071 }
1072 SkAutoDataUnref data(wStream.copyToData());
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001073
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001074 SkBitmap bm;
halcanary@google.com3d50ea12014-01-02 13:15:13 +00001075 bool installSuccess = SkInstallDiscardablePixelRef(
1076 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm, NULL);
reed@google.combf790232013-12-13 19:45:58 +00001077 REPORTER_ASSERT(reporter, installSuccess);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001078
1079 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
1080 // Flattening original will follow the old path of performing an encode, while flattening bm
1081 // will use the already encoded data.
1082 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
1083 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
1084 REPORTER_ASSERT(reporter, picture1->equals(picture2));
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001085 // Now test that a parse error was generated when trying to create a new SkPicture without
1086 // providing a function to decode the bitmap.
1087 ErrorContext context;
1088 context.fErrors = 0;
1089 context.fReporter = reporter;
1090 SkSetErrorCallback(assert_one_parse_error_cb, &context);
1091 SkMemoryStream pictureStream(picture1);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001092 SkClearLastError();
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001093 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL));
1094 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
scroggo@google.com49ce11b2013-04-25 18:29:32 +00001095 SkClearLastError();
1096 SkSetErrorCallback(NULL, NULL);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001097}
1098
junov@chromium.org94f20dc2013-01-28 21:04:44 +00001099static void test_clone_empty(skiatest::Reporter* reporter) {
1100 // This is a regression test for crbug.com/172062
1101 // Before the fix, we used to crash accessing a null pointer when we
1102 // had a picture with no paints. This test passes by not crashing.
1103 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001104 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001105 recorder.beginRecording(1, 1, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001106 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1107 SkAutoTUnref<SkPicture> destPicture(picture->clone());
junov@chromium.org94f20dc2013-01-28 21:04:44 +00001108 REPORTER_ASSERT(reporter, NULL != destPicture);
junov@chromium.org94f20dc2013-01-28 21:04:44 +00001109 }
1110}
1111
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001112static void test_draw_empty(skiatest::Reporter* reporter) {
1113 SkBitmap result;
1114 make_bm(&result, 2, 2, SK_ColorBLACK, false);
1115
1116 SkCanvas canvas(result);
1117
1118 {
1119 // stock SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001120 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001121 recorder.beginRecording(1, 1, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001122 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001123
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001124 canvas.drawPicture(*picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001125 }
1126
1127 {
1128 // tile grid
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001129 SkTileGridFactory::TileGridInfo gridInfo;
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001130 gridInfo.fMargin.setEmpty();
1131 gridInfo.fOffset.setZero();
1132 gridInfo.fTileInterval.set(1, 1);
1133
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001134 SkTileGridFactory factory(gridInfo);
1135 SkPictureRecorder recorder;
1136 recorder.beginRecording(1, 1, &factory, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001137 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001138
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001139 canvas.drawPicture(*picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001140 }
1141
1142 {
1143 // RTree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001144 SkRTreeFactory factory;
1145 SkPictureRecorder recorder;
1146 recorder.beginRecording(1, 1, &factory, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001147 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001148
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001149 canvas.drawPicture(*picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001150 }
1151
1152 {
1153 // quad tree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001154 SkQuadTreeFactory factory;
1155 SkPictureRecorder recorder;
1156 recorder.beginRecording(1, 1, &factory, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001157 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001158
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001159 canvas.drawPicture(*picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001160 }
1161}
1162
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001163static void test_clip_bound_opt(skiatest::Reporter* reporter) {
1164 // Test for crbug.com/229011
1165 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
1166 SkIntToScalar(2), SkIntToScalar(2));
1167 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
1168 SkIntToScalar(1), SkIntToScalar(1));
1169 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
1170 SkIntToScalar(1), SkIntToScalar(1));
1171
1172 SkPath invPath;
1173 invPath.addOval(rect1);
1174 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
1175 SkPath path;
1176 path.addOval(rect2);
1177 SkPath path2;
1178 path2.addOval(rect3);
1179 SkIRect clipBounds;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001180 SkPictureRecorder recorder;
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001181 // Minimalist test set for 100% code coverage of
1182 // SkPictureRecord::updateClipConservativelyUsingBounds
1183 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001184 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001185 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1186 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1187 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1188 REPORTER_ASSERT(reporter, true == nonEmpty);
1189 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1190 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1191 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1192 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1193 }
1194 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001195 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001196 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1197 canvas->clipPath(path, SkRegion::kIntersect_Op);
1198 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1199 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1200 REPORTER_ASSERT(reporter, true == nonEmpty);
1201 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1202 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1203 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1204 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1205 }
1206 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001207 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001208 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1209 canvas->clipPath(path, SkRegion::kIntersect_Op);
1210 canvas->clipPath(invPath, SkRegion::kUnion_Op);
1211 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1212 REPORTER_ASSERT(reporter, true == nonEmpty);
1213 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1214 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1215 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1216 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1217 }
1218 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001219 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001220 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1221 canvas->clipPath(path, SkRegion::kDifference_Op);
1222 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1223 REPORTER_ASSERT(reporter, true == nonEmpty);
1224 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1225 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1226 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1227 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1228 }
1229 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001230 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001231 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1232 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
1233 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1234 // True clip is actually empty in this case, but the best
1235 // determination we can make using only bounds as input is that the
1236 // clip is included in the bounds of 'path'.
1237 REPORTER_ASSERT(reporter, true == nonEmpty);
1238 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1239 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1240 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1241 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1242 }
1243 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001244 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001245 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1246 canvas->clipPath(path, SkRegion::kIntersect_Op);
1247 canvas->clipPath(path2, SkRegion::kXOR_Op);
1248 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1249 REPORTER_ASSERT(reporter, true == nonEmpty);
1250 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
1251 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
1252 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1253 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1254 }
1255}
1256
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001257/**
1258 * A canvas that records the number of clip commands.
1259 */
1260class ClipCountingCanvas : public SkCanvas {
1261public:
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001262 explicit ClipCountingCanvas(int width, int height)
1263 : INHERITED(width, height)
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001264 , fClipCount(0){
1265 }
1266
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001267 virtual void onClipRect(const SkRect& r,
1268 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001269 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001270 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001271 this->INHERITED::onClipRect(r, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001272 }
1273
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001274 virtual void onClipRRect(const SkRRect& rrect,
1275 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001276 ClipEdgeStyle edgeStyle)SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001277 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001278 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001279 }
1280
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001281 virtual void onClipPath(const SkPath& path,
1282 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001283 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001284 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001285 this->INHERITED::onClipPath(path, op, edgeStyle);
1286 }
1287
1288 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE {
1289 fClipCount += 1;
1290 this->INHERITED::onClipRegion(deviceRgn, op);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001291 }
1292
1293 unsigned getClipCount() const { return fClipCount; }
1294
1295private:
1296 unsigned fClipCount;
1297
1298 typedef SkCanvas INHERITED;
1299};
1300
1301static void test_clip_expansion(skiatest::Reporter* reporter) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001302 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001303 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001304
1305 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
1306 // The following expanding clip should not be skipped.
1307 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
1308 // Draw something so the optimizer doesn't just fold the world.
1309 SkPaint p;
1310 p.setColor(SK_ColorBLUE);
1311 canvas->drawPaint(p);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001312 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001313
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001314 ClipCountingCanvas testCanvas(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001315 picture->draw(&testCanvas);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001316
1317 // Both clips should be present on playback.
1318 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
1319}
1320
tomhudson@google.com381010e2013-10-24 11:12:47 +00001321static void test_hierarchical(skiatest::Reporter* reporter) {
1322 SkBitmap bm;
1323 make_bm(&bm, 10, 10, SK_ColorRED, true);
1324
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001325 SkPictureRecorder recorder;
tomhudson@google.com381010e2013-10-24 11:12:47 +00001326
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001327 recorder.beginRecording(10, 10, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001328 SkAutoTUnref<SkPicture> childPlain(recorder.endRecording());
1329 REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0
tomhudson@google.com381010e2013-10-24 11:12:47 +00001330
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001331 recorder.beginRecording(10, 10, NULL, 0)->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001332 SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording());
1333 REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1
tomhudson@google.com381010e2013-10-24 11:12:47 +00001334
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001335 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001336 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001337 canvas->drawPicture(*childPlain);
1338 SkAutoTUnref<SkPicture> parentPP(recorder.endRecording());
1339 REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0
1340 }
1341 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001342 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001343 canvas->drawPicture(*childWithBitmap);
1344 SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording());
1345 REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1
1346 }
1347 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001348 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001349 canvas->drawBitmap(bm, 0, 0);
1350 canvas->drawPicture(*childPlain);
1351 SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording());
1352 REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1
1353 }
1354 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001355 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001356 canvas->drawBitmap(bm, 0, 0);
1357 canvas->drawPicture(*childWithBitmap);
1358 SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording());
1359 REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2
1360 }
tomhudson@google.com381010e2013-10-24 11:12:47 +00001361}
1362
robertphillips@google.comd5500882014-04-02 23:51:13 +00001363static void test_gen_id(skiatest::Reporter* reporter) {
1364
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001365 SkPicture empty;
robertphillips@google.comd5500882014-04-02 23:51:13 +00001366
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001367 // Empty pictures should still have a valid ID
1368 REPORTER_ASSERT(reporter, empty.uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001369
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001370 SkPictureRecorder recorder;
robertphillips@google.comd5500882014-04-02 23:51:13 +00001371
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001372 SkCanvas* canvas = recorder.beginRecording(1, 1, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001373 canvas->drawARGB(255, 255, 255, 255);
1374 SkAutoTUnref<SkPicture> hasData(recorder.endRecording());
1375 // picture should have a non-zero id after recording
1376 REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001377
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001378 // both pictures should have different ids
1379 REPORTER_ASSERT(reporter, hasData->uniqueID() != empty.uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001380
1381 // test out copy constructor
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001382 SkPicture copyWithData(*hasData);
1383 REPORTER_ASSERT(reporter, hasData->uniqueID() == copyWithData.uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001384
1385 SkPicture emptyCopy(empty);
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001386 REPORTER_ASSERT(reporter, empty.uniqueID() != emptyCopy.uniqueID());
skia.committer@gmail.coma9157722014-04-03 03:04:26 +00001387
robertphillips@google.comd5500882014-04-02 23:51:13 +00001388 // test out swap
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001389 {
1390 SkPicture swapWithData;
1391 uint32_t beforeID1 = swapWithData.uniqueID();
1392 uint32_t beforeID2 = copyWithData.uniqueID();
1393 swapWithData.swap(copyWithData);
1394 REPORTER_ASSERT(reporter, copyWithData.uniqueID() == beforeID1);
1395 REPORTER_ASSERT(reporter, swapWithData.uniqueID() == beforeID2);
1396 }
robertphillips@google.comd5500882014-04-02 23:51:13 +00001397
1398 // test out clone
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001399 {
1400 SkAutoTUnref<SkPicture> cloneWithData(hasData->clone());
1401 REPORTER_ASSERT(reporter, hasData->uniqueID() == cloneWithData->uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001402
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001403 SkAutoTUnref<SkPicture> emptyClone(empty.clone());
1404 REPORTER_ASSERT(reporter, empty.uniqueID() != emptyClone->uniqueID());
1405 }
robertphillips@google.comd5500882014-04-02 23:51:13 +00001406}
1407
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00001408DEF_TEST(Picture, reporter) {
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001409#ifdef SK_DEBUG
1410 test_deleting_empty_playback();
1411 test_serializing_empty_picture();
scroggo@google.com4b90b112012-12-04 15:08:56 +00001412#else
1413 test_bad_bitmap();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001414#endif
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001415 test_unbalanced_save_restores(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001416 test_peephole();
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001417#if SK_SUPPORT_GPU
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +00001418 test_gpu_veto(reporter);
robertphillips@google.comb950c6f2014-04-25 00:02:12 +00001419#endif
reed@google.comfe7b1ed2012-11-29 21:00:39 +00001420 test_gatherpixelrefs(reporter);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +00001421 test_gatherpixelrefsandrects(reporter);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001422 test_bitmap_with_encoded_data(reporter);
junov@chromium.org94f20dc2013-01-28 21:04:44 +00001423 test_clone_empty(reporter);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001424 test_draw_empty(reporter);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001425 test_clip_bound_opt(reporter);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001426 test_clip_expansion(reporter);
tomhudson@google.com381010e2013-10-24 11:12:47 +00001427 test_hierarchical(reporter);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001428 test_gen_id(reporter);
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001429}
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001430
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001431#if SK_SUPPORT_GPU
1432DEF_GPUTEST(GPUPicture, reporter, factory) {
1433 test_gpu_picture_optimization(reporter, factory);
1434}
1435#endif
1436
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001437static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) {
1438 const SkPaint paint;
1439 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f };
1440 const SkIRect irect = { 2, 2, 3, 3 };
1441
1442 // Don't care what these record, as long as they're legal.
1443 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint);
1444 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag);
1445 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint);
1446 canvas->drawBitmapNine(bitmap, irect, rect, &paint);
1447 canvas->drawSprite(bitmap, 1, 1);
1448}
1449
1450static void test_draw_bitmaps(SkCanvas* canvas) {
1451 SkBitmap empty;
1452 draw_bitmaps(empty, canvas);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001453 empty.setConfig(SkImageInfo::MakeN32Premul(10, 10));
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001454 draw_bitmaps(empty, canvas);
1455}
1456
1457DEF_TEST(Picture_EmptyBitmap, r) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001458 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001459 test_draw_bitmaps(recorder.beginRecording(10, 10, NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001460 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001461}
1462
1463DEF_TEST(Canvas_EmptyBitmap, r) {
1464 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001465 dst.allocN32Pixels(10, 10);
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001466 SkCanvas canvas(dst);
1467
1468 test_draw_bitmaps(&canvas);
1469}