blob: 47804c7850ec758b83ca871771abc7d5f5c02bf3 [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"
reed@google.com21b519d2012-10-02 17:42:15 +00009#include "SkCanvas.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000010#include "SkColorPriv.h"
11#include "SkData.h"
reed@google.combf790232013-12-13 19:45:58 +000012#include "SkDecodingImageGenerator.h"
scroggo@google.com49ce11b2013-04-25 18:29:32 +000013#include "SkError.h"
halcanary@google.com3d50ea12014-01-02 13:15:13 +000014#include "SkImageEncoder.h"
15#include "SkImageGenerator.h"
reed@google.com21b519d2012-10-02 17:42:15 +000016#include "SkPaint.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000017#include "SkPicture.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000018#include "SkPictureUtils.h"
reed@google.com72aa79c2013-01-24 18:27:42 +000019#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000020#include "SkRandom.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000021#include "SkShader.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000022#include "SkStream.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000023#include "Test.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000024
robertphillips@google.comed9866c2014-01-09 19:20:45 +000025static const int gColorScale = 30;
26static const int gColorOffset = 60;
reed@google.comfe7b1ed2012-11-29 21:00:39 +000027
28static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000029 bm->allocN32Pixels(w, h);
reed@google.comfe7b1ed2012-11-29 21:00:39 +000030 bm->eraseColor(color);
31 if (immutable) {
32 bm->setImmutable();
33 }
34}
35
robertphillips@google.comfe5824a2014-01-09 19:45:29 +000036static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000037 SkASSERT(w % 2 == 0);
38 SkASSERT(h % 2 == 0);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000039 bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType,
40 kPremul_SkAlphaType));
robertphillips@google.comed9866c2014-01-09 19:20:45 +000041 SkAutoLockPixels lock(*bm);
42 for (int y = 0; y < h; y += 2) {
43 uint8_t* s = bm->getAddr8(0, y);
44 for (int x = 0; x < w; x += 2) {
45 *s++ = 0xFF;
46 *s++ = 0x00;
47 }
48 s = bm->getAddr8(0, y + 1);
49 for (int x = 0; x < w; x += 2) {
50 *s++ = 0x00;
51 *s++ = 0xFF;
52 }
53 }
54 if (immutable) {
55 bm->setImmutable();
56 }
57}
reed@google.comfe7b1ed2012-11-29 21:00:39 +000058
robertphillips@google.comed9866c2014-01-09 19:20:45 +000059static void init_paint(SkPaint* paint, const SkBitmap &bm) {
60 SkShader* shader = SkShader::CreateBitmapShader(bm,
61 SkShader::kClamp_TileMode,
62 SkShader::kClamp_TileMode);
63 paint->setShader(shader)->unref();
64}
65
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +000066typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000067 const SkBitmap&, const SkPoint&,
68 SkTDArray<SkPixelRef*>* usedPixRefs);
robertphillips@google.comed9866c2014-01-09 19:20:45 +000069
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000070static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000071 const SkBitmap& altBM, const SkPoint& pos,
72 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000073 SkPaint paint;
74 init_paint(&paint, bm);
75
76 canvas->drawPaint(paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000077 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +000078}
79
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000080static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000081 const SkBitmap& altBM, const SkPoint& pos,
82 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000083 SkPaint paint;
84 init_paint(&paint, bm);
85
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000086 // draw a rect
robertphillips@google.comed9866c2014-01-09 19:20:45 +000087 SkPoint points[5] = {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +000088 { pos.fX, pos.fY },
89 { pos.fX + bm.width() - 1, pos.fY },
90 { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 },
91 { pos.fX, pos.fY + bm.height() - 1 },
92 { pos.fX, pos.fY },
robertphillips@google.comed9866c2014-01-09 19:20:45 +000093 };
94
95 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000096 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +000097}
98
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000099static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000100 const SkBitmap& altBM, const SkPoint& pos,
101 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000102 SkPaint paint;
103 init_paint(&paint, bm);
104
105 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
106 r.offset(pos.fX, pos.fY);
107
108 canvas->drawRect(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000109 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000110}
111
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000112static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000113 const SkBitmap& altBM, const SkPoint& pos,
114 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000115 SkPaint paint;
116 init_paint(&paint, bm);
117
118 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
119 r.offset(pos.fX, pos.fY);
120
121 canvas->drawOval(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000122 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000123}
124
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000125static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000126 const SkBitmap& altBM, const SkPoint& pos,
127 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000128 SkPaint paint;
129 init_paint(&paint, bm);
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000130
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000131 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
132 r.offset(pos.fX, pos.fY);
133
134 SkRRect rr;
135 rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4);
136 canvas->drawRRect(rr, 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 drawpath_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);
145
146 SkPath path;
147 path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height()));
148 path.lineTo(SkIntToScalar(bm.width()), 0);
149 path.close();
150 path.offset(pos.fX, pos.fY);
151
152 canvas->drawPath(path, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000153 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000154}
155
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000156static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000157 const SkBitmap& altBM, const SkPoint& pos,
158 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000159 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000160 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000161}
162
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000163static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000164 const SkBitmap& altBM, const SkPoint& pos,
165 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000166 SkPaint paint;
167 init_paint(&paint, bm);
168
169 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
170 canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000171 *usedPixRefs->append() = bm.pixelRef();
172 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000173}
174
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000175static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000176 const SkBitmap& altBM, const SkPoint& pos,
177 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000178 const SkMatrix& ctm = canvas->getTotalMatrix();
179
180 SkPoint p(pos);
181 ctm.mapPoints(&p, 1);
182
183 canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000184 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000185}
186
187#if 0
188// Although specifiable, this case doesn't seem to make sense (i.e., the
189// bitmap in the shader is never used).
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000190static void drawsprite_withshader_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 SkPaint paint;
194 init_paint(&paint, bm);
195
196 const SkMatrix& ctm = canvas->getTotalMatrix();
197
198 SkPoint p(pos);
199 ctm.mapPoints(&p, 1);
200
201 canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000202 *usedPixRefs->append() = bm.pixelRef();
203 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000204}
205#endif
206
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000207static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000208 const SkBitmap& altBM, const SkPoint& pos,
209 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000210 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
211
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000212 r.offset(pos.fX, pos.fY);
213 canvas->drawBitmapRectToRect(bm, NULL, r, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000214 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000215}
216
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000217static void drawbitmaprect_withshader_proc(SkCanvas* canvas,
218 const SkBitmap& bm,
219 const SkBitmap& altBM,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000220 const SkPoint& pos,
221 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000222 SkPaint paint;
223 init_paint(&paint, bm);
224
225 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000226 r.offset(pos.fX, pos.fY);
227
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000228 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
229 canvas->drawBitmapRectToRect(altBM, NULL, r, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000230 *usedPixRefs->append() = bm.pixelRef();
231 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000232}
233
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000234static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000235 const SkBitmap& altBM, const SkPoint& pos,
236 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000237 SkPaint paint;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000238 init_paint(&paint, bm);
239 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
240
241 canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000242 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000243}
244
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000245static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000246 const SkBitmap& altBM, const SkPoint& pos,
247 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000248 SkPaint paint;
249 init_paint(&paint, bm);
250 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
251
252 SkPoint point = { pos.fX, pos.fY + bm.height() };
253 canvas->drawPosText("O", 1, &point, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000254 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000255}
256
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000257static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000258 const SkBitmap& altBM, const SkPoint& pos,
259 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000260 SkPaint paint;
261
262 init_paint(&paint, bm);
263 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
264
265 SkPath path;
266 path.lineTo(SkIntToScalar(bm.width()), 0);
267 path.offset(pos.fX, pos.fY+bm.height());
268
269 canvas->drawTextOnPath("O", 1, path, NULL, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000270 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000271}
272
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000273static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000274 const SkBitmap& altBM, const SkPoint& pos,
275 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000276 SkPaint paint;
277 init_paint(&paint, bm);
278
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000279 SkPoint verts[4] = {
280 { pos.fX, pos.fY },
281 { pos.fX + bm.width(), pos.fY },
282 { pos.fX + bm.width(), pos.fY + bm.height() },
283 { pos.fX, pos.fY + bm.height() }
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000284 };
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000285 SkPoint texs[4] = { { 0, 0 },
286 { SkIntToScalar(bm.width()), 0 },
287 { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000288 { 0, SkIntToScalar(bm.height()) } };
289 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 };
290
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000291 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL,
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000292 indices, 6, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000293 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000294}
295
296// Return a picture with the bitmaps drawn at the specified positions.
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000297static SkPicture* record_bitmaps(const SkBitmap bm[],
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000298 const SkPoint pos[],
299 SkTDArray<SkPixelRef*> analytic[],
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000300 int count,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000301 DrawBitmapProc proc) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000302 SkPicture* pic = new SkPicture;
303 SkCanvas* canvas = pic->beginRecording(1000, 1000);
304 for (int i = 0; i < count; ++i) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000305 analytic[i].rewind();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000306 canvas->save();
307 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY,
308 SkIntToScalar(bm[i].width()),
309 SkIntToScalar(bm[i].height()));
310 canvas->clipRect(clipRect, SkRegion::kIntersect_Op);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000311 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000312 canvas->restore();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000313 }
314 pic->endRecording();
315 return pic;
316}
317
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000318static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000319 rect->fLeft = rand.nextRangeScalar(-W, 2*W);
320 rect->fTop = rand.nextRangeScalar(-H, 2*H);
321 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W);
322 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H);
323
324 // we integralize rect to make our tests more predictable, since Gather is
325 // a little sloppy.
326 SkIRect ir;
327 rect->round(&ir);
328 rect->set(ir);
329}
330
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000331static void draw(SkPicture* pic, int width, int height, SkBitmap* result) {
332 make_bm(result, width, height, SK_ColorBLACK, false);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000333
334 SkCanvas canvas(*result);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000335 canvas.drawPicture(*pic);
336}
337
338template <typename T> int find_index(const T* array, T elem, int count) {
339 for (int i = 0; i < count; ++i) {
340 if (array[i] == elem) {
341 return i;
342 }
343 }
344 return -1;
345}
346
347// Return true if 'ref' is found in array[]
348static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) {
349 return find_index<const SkPixelRef*>(array, ref, count) >= 0;
350}
351
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000352// Look at each pixel that is inside 'subset', and if its color appears in
353// colors[], find the corresponding value in refs[] and append that ref into
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000354// array, skipping duplicates of the same value.
355// Note that gathering pixelRefs from rendered colors suffers from the problem
356// that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color)
357// isn't easy to reconstruct.
358static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[],
359 int count, SkTDArray<SkPixelRef*>* array,
360 const SkRect& subset) {
361 SkIRect ir;
362 subset.roundOut(&ir);
363
364 if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) {
365 return;
366 }
367
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000368 // Since we only want to return unique values in array, when we scan we just
369 // set a bit for each index'd color found. In practice we only have a few
370 // distinct colors, so we just use an int's bits as our array. Hence the
371 // assert that count <= number-of-bits-in-our-int.
372 SkASSERT((unsigned)count <= 32);
373 uint32_t bitarray = 0;
374
375 SkAutoLockPixels alp(bm);
376
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000377 for (int y = ir.fTop; y < ir.fBottom; ++y) {
378 for (int x = ir.fLeft; x < ir.fRight; ++x) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000379 SkPMColor pmc = *bm.getAddr32(x, y);
380 // the only good case where the color is not found would be if
381 // the color is transparent, meaning no bitmap was drawn in that
382 // pixel.
383 if (pmc) {
bsalomon@google.comc3d753e2013-01-08 17:24:44 +0000384 uint32_t index = SkGetPackedR32(pmc);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000385 SkASSERT(SkGetPackedG32(pmc) == index);
386 SkASSERT(SkGetPackedB32(pmc) == index);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000387 if (0 == index) {
388 continue; // background color
389 }
390 SkASSERT(0 == (index - gColorOffset) % gColorScale);
391 index = (index - gColorOffset) / gColorScale;
bsalomon@google.com5f429b02013-01-08 18:42:20 +0000392 SkASSERT(static_cast<int>(index) < count);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000393 bitarray |= 1 << index;
394 }
395 }
396 }
397
398 for (int i = 0; i < count; ++i) {
399 if (bitarray & (1 << i)) {
400 *array->append() = refs[i];
401 }
402 }
403}
404
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000405static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h,
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000406 const SkTDArray<SkPixelRef*> analytic[],
407 int count,
408 SkTDArray<SkPixelRef*>* result,
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000409 const SkRect& subset) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000410 for (int i = 0; i < count; ++i) {
411 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h);
412
413 if (SkRect::Intersects(subset, rect)) {
414 result->append(analytic[i].count(), analytic[i].begin());
415 }
416 }
417}
418
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000419
420static const struct {
421 const DrawBitmapProc proc;
422 const char* const desc;
423} gProcs[] = {
424 {drawpaint_proc, "drawpaint"},
425 {drawpoints_proc, "drawpoints"},
426 {drawrect_proc, "drawrect"},
427 {drawoval_proc, "drawoval"},
428 {drawrrect_proc, "drawrrect"},
429 {drawpath_proc, "drawpath"},
430 {drawbitmap_proc, "drawbitmap"},
431 {drawbitmap_withshader_proc, "drawbitmap_withshader"},
432 {drawsprite_proc, "drawsprite"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000433#if 0
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000434 {drawsprite_withshader_proc, "drawsprite_withshader"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000435#endif
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000436 {drawbitmaprect_proc, "drawbitmaprect"},
437 {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"},
438 {drawtext_proc, "drawtext"},
439 {drawpostext_proc, "drawpostext"},
440 {drawtextonpath_proc, "drawtextonpath"},
441 {drawverts_proc, "drawverts"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000442};
443
444static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000445 // Our convention is that the color components contain an encoding of
446 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000447 // reserved for the background
448 for (int i = 0; i < num; ++i) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000449 make_bm(&bm[i], w, h,
450 SkColorSetARGB(0xFF,
451 gColorScale*i+gColorOffset,
452 gColorScale*i+gColorOffset,
453 gColorScale*i+gColorOffset),
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000454 true);
455 refs[i] = bm[i].pixelRef();
456 }
457
458 // The A8 alternate bitmaps are all BW checkerboards
459 for (int i = 0; i < num; ++i) {
460 make_checkerboard(&bm[num+i], w, h, true);
461 refs[num+i] = bm[num+i].pixelRef();
462 }
463}
464
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000465static void test_gatherpixelrefs(skiatest::Reporter* reporter) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000466 const int IW = 32;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000467 const int IH = IW;
468 const SkScalar W = SkIntToScalar(IW);
469 const SkScalar H = W;
470
471 static const int N = 4;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000472 SkBitmap bm[2*N];
473 SkPixelRef* refs[2*N];
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000474 SkTDArray<SkPixelRef*> analytic[N];
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000475
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000476 const SkPoint pos[N] = {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000477 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
478 };
479
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000480 create_textures(bm, refs, N, IW, IH);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000481
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000482 SkRandom rand;
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000483 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000484 SkAutoTUnref<SkPicture> pic(
485 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000486
tomhudson@google.com381010e2013-10-24 11:12:47 +0000487 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000488 // quick check for a small piece of each quadrant, which should just
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000489 // contain 1 or 2 bitmaps.
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000490 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
491 SkRect r;
492 r.set(2, 2, W - 2, H - 2);
493 r.offset(pos[i].fX, pos[i].fY);
494 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r));
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000495 if (!data) {
496 ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned "
497 "NULL for %s.", gProcs[k].desc);
498 continue;
499 }
500 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data();
501 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*));
502 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
503 if (1 == count) {
504 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
505 } else if (2 == count) {
506 REPORTER_ASSERT(reporter,
507 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
508 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
commit-bot@chromium.orgfe433c12013-07-09 16:04:32 +0000509 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000510 }
511
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000512 SkBitmap image;
513 draw(pic, 2*IW, 2*IH, &image);
514
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000515 // Test a bunch of random (mostly) rects, and compare the gather results
516 // with a deduced list of refs by looking at the colors drawn.
517 for (int j = 0; j < 100; ++j) {
518 SkRect r;
519 rand_rect(&r, rand, 2*W, 2*H);
520
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000521 SkTDArray<SkPixelRef*> fromImage;
522 gather_from_image(image, refs, N, &fromImage, r);
523
524 SkTDArray<SkPixelRef*> fromAnalytic;
525 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000526
527 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
528 size_t dataSize = data ? data->size() : 0;
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000529 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000530 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize);
531 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL;
532 SkAutoDataUnref adu(data);
533
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000534 // Everything that we saw drawn should appear in the analytic list
535 // but the analytic list may contain some pixelRefs that were not
536 // seen in the image (e.g., A8 textures used as masks)
537 for (int i = 0; i < fromImage.count(); ++i) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000538 if (-1 == fromAnalytic.find(fromImage[i])) {
539 ERRORF(reporter, "PixelRef missing %d %s",
540 i, gProcs[k].desc);
541 }
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000542 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000543
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000544 /*
545 * GatherPixelRefs is conservative, so it can return more bitmaps
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000546 * than are strictly required. Thus our check here is only that
547 * Gather didn't miss any that we actually needed. Even that isn't
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000548 * a strict requirement on Gather, which is meant to be quick and
549 * only mostly-correct, but at the moment this test should work.
550 */
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000551 for (int i = 0; i < fromAnalytic.count(); ++i) {
552 bool found = find(gatherRefs, fromAnalytic[i], gatherCount);
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000553 if (!found) {
554 ERRORF(reporter, "PixelRef missing %d %s",
555 i, gProcs[k].desc);
556 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000557#if 0
558 // enable this block of code to debug failures, as it will rerun
559 // the case that failed.
560 if (!found) {
561 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
562 size_t dataSize = data ? data->size() : 0;
563 }
564#endif
565 }
566 }
567 }
568}
569
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000570static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) {
571 const int IW = 32;
572 const int IH = IW;
573 const SkScalar W = SkIntToScalar(IW);
574 const SkScalar H = W;
575
576 static const int N = 4;
577 SkBitmap bm[2*N];
578 SkPixelRef* refs[2*N];
579 SkTDArray<SkPixelRef*> analytic[N];
580
581 const SkPoint pos[N] = {
582 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
583 };
584
585 create_textures(bm, refs, N, IW, IH);
586
587 SkRandom rand;
588 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000589 SkAutoTUnref<SkPicture> pic(
590 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000591
592 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
593
594 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont(
595 new SkPictureUtils::SkPixelRefsAndRectsList);
596
597 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont);
598
599 // quick check for a small piece of each quadrant, which should just
600 // contain 1 or 2 bitmaps.
601 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
602 SkRect r;
603 r.set(2, 2, W - 2, H - 2);
604 r.offset(pos[i].fX, pos[i].fY);
605
606 SkTDArray<SkPixelRef*> gatheredRefs;
607 prCont->query(r, &gatheredRefs);
608
609 int count = gatheredRefs.count();
610 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
611 if (1 == count) {
612 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
613 } else if (2 == count) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000614 REPORTER_ASSERT(reporter,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000615 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
616 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
617 }
618 }
619
620 SkBitmap image;
621 draw(pic, 2*IW, 2*IH, &image);
622
623 // Test a bunch of random (mostly) rects, and compare the gather results
624 // with the analytic results and the pixel refs seen in a rendering.
625 for (int j = 0; j < 100; ++j) {
626 SkRect r;
627 rand_rect(&r, rand, 2*W, 2*H);
628
629 SkTDArray<SkPixelRef*> fromImage;
630 gather_from_image(image, refs, N, &fromImage, r);
631
632 SkTDArray<SkPixelRef*> fromAnalytic;
633 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
634
635 SkTDArray<SkPixelRef*> gatheredRefs;
636 prCont->query(r, &gatheredRefs);
637
638 // Everything that we saw drawn should appear in the analytic list
639 // but the analytic list may contain some pixelRefs that were not
640 // seen in the image (e.g., A8 textures used as masks)
641 for (int i = 0; i < fromImage.count(); ++i) {
642 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]));
643 }
644
645 // Everything in the analytic list should appear in the gathered
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000646 // list.
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000647 for (int i = 0; i < fromAnalytic.count(); ++i) {
648 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i]));
649 }
650 }
651 }
652}
653
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000654#ifdef SK_DEBUG
655// Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only
656// run in debug mode.
657static void test_deleting_empty_playback() {
658 SkPicture picture;
659 // Creates an SkPictureRecord
660 picture.beginRecording(0, 0);
661 // Turns that into an SkPicturePlayback
662 picture.endRecording();
663 // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord
664 picture.beginRecording(0, 0);
665}
666
667// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
668static void test_serializing_empty_picture() {
669 SkPicture picture;
670 picture.beginRecording(0, 0);
671 picture.endRecording();
672 SkDynamicMemoryWStream stream;
673 picture.serialize(&stream);
674}
675#endif
676
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000677static void rand_op(SkCanvas* canvas, SkRandom& rand) {
reed@google.com21b519d2012-10-02 17:42:15 +0000678 SkPaint paint;
679 SkRect rect = SkRect::MakeWH(50, 50);
680
681 SkScalar unit = rand.nextUScalar1();
682 if (unit <= 0.3) {
683// SkDebugf("save\n");
684 canvas->save();
685 } else if (unit <= 0.6) {
686// SkDebugf("restore\n");
687 canvas->restore();
688 } else if (unit <= 0.9) {
689// SkDebugf("clip\n");
690 canvas->clipRect(rect);
691 } else {
692// SkDebugf("draw\n");
693 canvas->drawPaint(paint);
694 }
695}
696
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000697static void set_canvas_to_save_count_4(SkCanvas* canvas) {
698 canvas->restoreToCount(1);
699 canvas->save();
700 canvas->save();
701 canvas->save();
702}
703
704static void test_unbalanced_save_restores(skiatest::Reporter* reporter) {
705 SkCanvas testCanvas(100, 100);
706 set_canvas_to_save_count_4(&testCanvas);
707
708 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
709
710 SkPaint paint;
711 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000);
712
713 SkPicture extra_save_picture;
714 extra_save_picture.beginRecording(100, 100);
715 extra_save_picture.getRecordingCanvas()->save();
716 extra_save_picture.getRecordingCanvas()->translate(10, 10);
717 extra_save_picture.getRecordingCanvas()->drawRect(rect, paint);
718 extra_save_picture.getRecordingCanvas()->save();
719 extra_save_picture.getRecordingCanvas()->translate(10, 10);
720 extra_save_picture.getRecordingCanvas()->drawRect(rect, paint);
721
722 testCanvas.drawPicture(extra_save_picture);
723 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
724
725 set_canvas_to_save_count_4(&testCanvas);
726
727 SkPicture extra_restore_picture;
728 extra_restore_picture.beginRecording(100, 100);
729 extra_restore_picture.getRecordingCanvas()->save();
730 extra_restore_picture.getRecordingCanvas()->translate(10, 10);
731 extra_restore_picture.getRecordingCanvas()->drawRect(rect, paint);
732 extra_restore_picture.getRecordingCanvas()->save();
733 extra_restore_picture.getRecordingCanvas()->translate(10, 10);
734 extra_restore_picture.getRecordingCanvas()->drawRect(rect, paint);
735 extra_restore_picture.getRecordingCanvas()->restore();
736 extra_restore_picture.getRecordingCanvas()->restore();
737 extra_restore_picture.getRecordingCanvas()->restore();
738 extra_restore_picture.getRecordingCanvas()->restore();
739
740 testCanvas.drawPicture(extra_save_picture);
741 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
742
743 SkPicture no_save_picture;
744 extra_restore_picture.beginRecording(100, 100);
745 extra_restore_picture.getRecordingCanvas()->translate(10, 10);
746 extra_restore_picture.getRecordingCanvas()->drawRect(rect, paint);
747
748 testCanvas.drawPicture(extra_save_picture);
749 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
750 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity());
751}
752
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000753static void test_peephole() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000754 SkRandom rand;
reed@google.com21b519d2012-10-02 17:42:15 +0000755
756 for (int j = 0; j < 100; j++) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000757 SkRandom rand2(rand); // remember the seed
reed@google.com21b519d2012-10-02 17:42:15 +0000758
759 SkPicture picture;
760 SkCanvas* canvas = picture.beginRecording(100, 100);
761
762 for (int i = 0; i < 1000; ++i) {
763 rand_op(canvas, rand);
764 }
765 picture.endRecording();
jvanverth@google.comc490f802013-03-04 13:56:38 +0000766
767 rand = rand2;
reed@google.com21b519d2012-10-02 17:42:15 +0000768 }
769
770 {
771 SkPicture picture;
772 SkCanvas* canvas = picture.beginRecording(100, 100);
773 SkRect rect = SkRect::MakeWH(50, 50);
skia.committer@gmail.com52c24372012-10-03 02:01:13 +0000774
reed@google.com21b519d2012-10-02 17:42:15 +0000775 for (int i = 0; i < 100; ++i) {
776 canvas->save();
777 }
778 while (canvas->getSaveCount() > 1) {
779 canvas->clipRect(rect);
780 canvas->restore();
781 }
782 picture.endRecording();
783 }
784}
785
scroggo@google.com4b90b112012-12-04 15:08:56 +0000786#ifndef SK_DEBUG
787// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
788// should never do this.
789static void test_bad_bitmap() {
790 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
791 // fail.
792 SkBitmap bm;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000793 bm.setConfig(SkImageInfo::MakeN32Premul(100, 100));
scroggo@google.com4b90b112012-12-04 15:08:56 +0000794 SkPicture picture;
795 SkCanvas* recordingCanvas = picture.beginRecording(100, 100);
796 recordingCanvas->drawBitmap(bm, 0, 0);
797 picture.endRecording();
798
799 SkCanvas canvas;
800 canvas.drawPicture(picture);
801}
802#endif
803
reed@google.com672588b2014-01-08 15:42:01 +0000804static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000805 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000806}
807
808static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
809 SkPicture picture;
810 SkCanvas* canvas = picture.beginRecording(bitmap.width(), bitmap.height());
811 canvas->drawBitmap(bitmap, 0, 0);
812 SkDynamicMemoryWStream wStream;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000813 picture.serialize(&wStream, &encode_bitmap_to_data);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000814 return wStream.copyToData();
815}
816
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000817struct ErrorContext {
818 int fErrors;
819 skiatest::Reporter* fReporter;
820};
821
822static void assert_one_parse_error_cb(SkError error, void* context) {
823 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
824 errorContext->fErrors++;
825 // This test only expects one error, and that is a kParseError. If there are others,
826 // there is some unknown problem.
827 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors,
828 "This threw more errors than expected.");
829 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error,
830 SkGetLastErrorString());
831}
832
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000833static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
834 // Create a bitmap that will be encoded.
835 SkBitmap original;
836 make_bm(&original, 100, 100, SK_ColorBLUE, true);
837 SkDynamicMemoryWStream wStream;
838 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
839 return;
840 }
841 SkAutoDataUnref data(wStream.copyToData());
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000842
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000843 SkBitmap bm;
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000844 bool installSuccess = SkInstallDiscardablePixelRef(
845 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm, NULL);
reed@google.combf790232013-12-13 19:45:58 +0000846 REPORTER_ASSERT(reporter, installSuccess);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000847
848 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
849 // Flattening original will follow the old path of performing an encode, while flattening bm
850 // will use the already encoded data.
851 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
852 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
853 REPORTER_ASSERT(reporter, picture1->equals(picture2));
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000854 // Now test that a parse error was generated when trying to create a new SkPicture without
855 // providing a function to decode the bitmap.
856 ErrorContext context;
857 context.fErrors = 0;
858 context.fReporter = reporter;
859 SkSetErrorCallback(assert_one_parse_error_cb, &context);
860 SkMemoryStream pictureStream(picture1);
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000861 SkClearLastError();
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000862 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL));
863 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000864 SkClearLastError();
865 SkSetErrorCallback(NULL, NULL);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000866}
867
junov@chromium.org94f20dc2013-01-28 21:04:44 +0000868static void test_clone_empty(skiatest::Reporter* reporter) {
869 // This is a regression test for crbug.com/172062
870 // Before the fix, we used to crash accessing a null pointer when we
871 // had a picture with no paints. This test passes by not crashing.
872 {
873 SkPicture picture;
874 picture.beginRecording(1, 1);
875 picture.endRecording();
876 SkPicture* destPicture = picture.clone();
877 REPORTER_ASSERT(reporter, NULL != destPicture);
878 destPicture->unref();
879 }
880 {
881 // Test without call to endRecording
882 SkPicture picture;
883 picture.beginRecording(1, 1);
884 SkPicture* destPicture = picture.clone();
885 REPORTER_ASSERT(reporter, NULL != destPicture);
886 destPicture->unref();
887 }
888}
889
junov@chromium.orgd575eed2013-05-08 15:39:13 +0000890static void test_clip_bound_opt(skiatest::Reporter* reporter) {
891 // Test for crbug.com/229011
892 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
893 SkIntToScalar(2), SkIntToScalar(2));
894 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
895 SkIntToScalar(1), SkIntToScalar(1));
896 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
897 SkIntToScalar(1), SkIntToScalar(1));
898
899 SkPath invPath;
900 invPath.addOval(rect1);
901 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
902 SkPath path;
903 path.addOval(rect2);
904 SkPath path2;
905 path2.addOval(rect3);
906 SkIRect clipBounds;
907 // Minimalist test set for 100% code coverage of
908 // SkPictureRecord::updateClipConservativelyUsingBounds
909 {
910 SkPicture picture;
911 SkCanvas* canvas = picture.beginRecording(10, 10,
912 SkPicture::kUsePathBoundsForClip_RecordingFlag);
913 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
914 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
915 REPORTER_ASSERT(reporter, true == nonEmpty);
916 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
917 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
918 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
919 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
920 }
921 {
922 SkPicture picture;
923 SkCanvas* canvas = picture.beginRecording(10, 10,
924 SkPicture::kUsePathBoundsForClip_RecordingFlag);
925 canvas->clipPath(path, SkRegion::kIntersect_Op);
926 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
927 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
928 REPORTER_ASSERT(reporter, true == nonEmpty);
929 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
930 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
931 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
932 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
933 }
934 {
935 SkPicture picture;
936 SkCanvas* canvas = picture.beginRecording(10, 10,
937 SkPicture::kUsePathBoundsForClip_RecordingFlag);
938 canvas->clipPath(path, SkRegion::kIntersect_Op);
939 canvas->clipPath(invPath, SkRegion::kUnion_Op);
940 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
941 REPORTER_ASSERT(reporter, true == nonEmpty);
942 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
943 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
944 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
945 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
946 }
947 {
948 SkPicture picture;
949 SkCanvas* canvas = picture.beginRecording(10, 10,
950 SkPicture::kUsePathBoundsForClip_RecordingFlag);
951 canvas->clipPath(path, SkRegion::kDifference_Op);
952 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
953 REPORTER_ASSERT(reporter, true == nonEmpty);
954 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
955 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
956 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
957 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
958 }
959 {
960 SkPicture picture;
961 SkCanvas* canvas = picture.beginRecording(10, 10,
962 SkPicture::kUsePathBoundsForClip_RecordingFlag);
963 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
964 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
965 // True clip is actually empty in this case, but the best
966 // determination we can make using only bounds as input is that the
967 // clip is included in the bounds of 'path'.
968 REPORTER_ASSERT(reporter, true == nonEmpty);
969 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
970 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
971 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
972 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
973 }
974 {
975 SkPicture picture;
976 SkCanvas* canvas = picture.beginRecording(10, 10,
977 SkPicture::kUsePathBoundsForClip_RecordingFlag);
978 canvas->clipPath(path, SkRegion::kIntersect_Op);
979 canvas->clipPath(path2, SkRegion::kXOR_Op);
980 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
981 REPORTER_ASSERT(reporter, true == nonEmpty);
982 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
983 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
984 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
985 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
986 }
987}
988
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +0000989/**
990 * A canvas that records the number of clip commands.
991 */
992class ClipCountingCanvas : public SkCanvas {
993public:
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000994 explicit ClipCountingCanvas(int width, int height)
995 : INHERITED(width, height)
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +0000996 , fClipCount(0){
997 }
998
skia.committer@gmail.com370a8992014-03-01 03:02:09 +0000999 virtual void onClipRect(const SkRect& r,
1000 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001001 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001002 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001003 this->INHERITED::onClipRect(r, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001004 }
1005
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001006 virtual void onClipRRect(const SkRRect& rrect,
1007 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001008 ClipEdgeStyle edgeStyle)SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001009 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001010 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001011 }
1012
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001013 virtual void onClipPath(const SkPath& path,
1014 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001015 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001016 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001017 this->INHERITED::onClipPath(path, op, edgeStyle);
1018 }
1019
1020 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE {
1021 fClipCount += 1;
1022 this->INHERITED::onClipRegion(deviceRgn, op);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001023 }
1024
1025 unsigned getClipCount() const { return fClipCount; }
1026
1027private:
1028 unsigned fClipCount;
1029
1030 typedef SkCanvas INHERITED;
1031};
1032
1033static void test_clip_expansion(skiatest::Reporter* reporter) {
1034 SkPicture picture;
1035 SkCanvas* canvas = picture.beginRecording(10, 10, 0);
1036
1037 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
1038 // The following expanding clip should not be skipped.
1039 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
1040 // Draw something so the optimizer doesn't just fold the world.
1041 SkPaint p;
1042 p.setColor(SK_ColorBLUE);
1043 canvas->drawPaint(p);
1044
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001045 ClipCountingCanvas testCanvas(10, 10);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001046 picture.draw(&testCanvas);
1047
1048 // Both clips should be present on playback.
1049 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
1050}
1051
tomhudson@google.com381010e2013-10-24 11:12:47 +00001052static void test_hierarchical(skiatest::Reporter* reporter) {
1053 SkBitmap bm;
1054 make_bm(&bm, 10, 10, SK_ColorRED, true);
1055
1056 SkCanvas* canvas;
1057
1058 SkPicture childPlain;
1059 childPlain.beginRecording(10, 10);
1060 childPlain.endRecording();
1061 REPORTER_ASSERT(reporter, !childPlain.willPlayBackBitmaps()); // 0
1062
1063 SkPicture childWithBitmap;
1064 childWithBitmap.beginRecording(10, 10)->drawBitmap(bm, 0, 0);
1065 childWithBitmap.endRecording();
1066 REPORTER_ASSERT(reporter, childWithBitmap.willPlayBackBitmaps()); // 1
1067
1068 SkPicture parentPP;
1069 canvas = parentPP.beginRecording(10, 10);
1070 canvas->drawPicture(childPlain);
1071 parentPP.endRecording();
1072 REPORTER_ASSERT(reporter, !parentPP.willPlayBackBitmaps()); // 0
1073
1074 SkPicture parentPWB;
1075 canvas = parentPWB.beginRecording(10, 10);
1076 canvas->drawPicture(childWithBitmap);
1077 parentPWB.endRecording();
1078 REPORTER_ASSERT(reporter, parentPWB.willPlayBackBitmaps()); // 1
1079
1080 SkPicture parentWBP;
1081 canvas = parentWBP.beginRecording(10, 10);
1082 canvas->drawBitmap(bm, 0, 0);
1083 canvas->drawPicture(childPlain);
1084 parentWBP.endRecording();
1085 REPORTER_ASSERT(reporter, parentWBP.willPlayBackBitmaps()); // 1
1086
1087 SkPicture parentWBWB;
1088 canvas = parentWBWB.beginRecording(10, 10);
1089 canvas->drawBitmap(bm, 0, 0);
1090 canvas->drawPicture(childWithBitmap);
1091 parentWBWB.endRecording();
1092 REPORTER_ASSERT(reporter, parentWBWB.willPlayBackBitmaps()); // 2
1093}
1094
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00001095DEF_TEST(Picture, reporter) {
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001096#ifdef SK_DEBUG
1097 test_deleting_empty_playback();
1098 test_serializing_empty_picture();
scroggo@google.com4b90b112012-12-04 15:08:56 +00001099#else
1100 test_bad_bitmap();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001101#endif
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001102 test_unbalanced_save_restores(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001103 test_peephole();
reed@google.comfe7b1ed2012-11-29 21:00:39 +00001104 test_gatherpixelrefs(reporter);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +00001105 test_gatherpixelrefsandrects(reporter);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001106 test_bitmap_with_encoded_data(reporter);
junov@chromium.org94f20dc2013-01-28 21:04:44 +00001107 test_clone_empty(reporter);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001108 test_clip_bound_opt(reporter);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001109 test_clip_expansion(reporter);
tomhudson@google.com381010e2013-10-24 11:12:47 +00001110 test_hierarchical(reporter);
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001111}
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001112
1113static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) {
1114 const SkPaint paint;
1115 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f };
1116 const SkIRect irect = { 2, 2, 3, 3 };
1117
1118 // Don't care what these record, as long as they're legal.
1119 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint);
1120 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag);
1121 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint);
1122 canvas->drawBitmapNine(bitmap, irect, rect, &paint);
1123 canvas->drawSprite(bitmap, 1, 1);
1124}
1125
1126static void test_draw_bitmaps(SkCanvas* canvas) {
1127 SkBitmap empty;
1128 draw_bitmaps(empty, canvas);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001129 empty.setConfig(SkImageInfo::MakeN32Premul(10, 10));
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001130 draw_bitmaps(empty, canvas);
1131}
1132
1133DEF_TEST(Picture_EmptyBitmap, r) {
1134 SkPicture picture;
1135 test_draw_bitmaps(picture.beginRecording(10, 10));
1136 picture.endRecording();
1137}
1138
1139DEF_TEST(Canvas_EmptyBitmap, r) {
1140 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001141 dst.allocN32Pixels(10, 10);
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001142 SkCanvas canvas(dst);
1143
1144 test_draw_bitmaps(&canvas);
1145}