blob: b77beca33413009fd8b0fc599893ce524fcf510a [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"
commit-bot@chromium.org70512af2014-03-18 17:45:32 +000019#include "SkQuadTreePicture.h"
reed@google.com72aa79c2013-01-24 18:27:42 +000020#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000021#include "SkRandom.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000022#include "SkShader.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000023#include "SkStream.h"
commit-bot@chromium.org70512af2014-03-18 17:45:32 +000024#include "SkTileGrid.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000025#include "Test.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000026
robertphillips@google.comed9866c2014-01-09 19:20:45 +000027static const int gColorScale = 30;
28static const int gColorOffset = 60;
reed@google.comfe7b1ed2012-11-29 21:00:39 +000029
30static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000031 bm->allocN32Pixels(w, h);
reed@google.comfe7b1ed2012-11-29 21:00:39 +000032 bm->eraseColor(color);
33 if (immutable) {
34 bm->setImmutable();
35 }
36}
37
robertphillips@google.comfe5824a2014-01-09 19:45:29 +000038static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000039 SkASSERT(w % 2 == 0);
40 SkASSERT(h % 2 == 0);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000041 bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType,
42 kPremul_SkAlphaType));
robertphillips@google.comed9866c2014-01-09 19:20:45 +000043 SkAutoLockPixels lock(*bm);
44 for (int y = 0; y < h; y += 2) {
45 uint8_t* s = bm->getAddr8(0, y);
46 for (int x = 0; x < w; x += 2) {
47 *s++ = 0xFF;
48 *s++ = 0x00;
49 }
50 s = bm->getAddr8(0, y + 1);
51 for (int x = 0; x < w; x += 2) {
52 *s++ = 0x00;
53 *s++ = 0xFF;
54 }
55 }
56 if (immutable) {
57 bm->setImmutable();
58 }
59}
reed@google.comfe7b1ed2012-11-29 21:00:39 +000060
robertphillips@google.comed9866c2014-01-09 19:20:45 +000061static void init_paint(SkPaint* paint, const SkBitmap &bm) {
62 SkShader* shader = SkShader::CreateBitmapShader(bm,
63 SkShader::kClamp_TileMode,
64 SkShader::kClamp_TileMode);
65 paint->setShader(shader)->unref();
66}
67
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +000068typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000069 const SkBitmap&, const SkPoint&,
70 SkTDArray<SkPixelRef*>* usedPixRefs);
robertphillips@google.comed9866c2014-01-09 19:20:45 +000071
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000072static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000073 const SkBitmap& altBM, const SkPoint& pos,
74 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000075 SkPaint paint;
76 init_paint(&paint, bm);
77
78 canvas->drawPaint(paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000079 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +000080}
81
skia.committer@gmail.com856673a2014-01-10 07:08:11 +000082static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000083 const SkBitmap& altBM, const SkPoint& pos,
84 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +000085 SkPaint paint;
86 init_paint(&paint, bm);
87
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000088 // draw a rect
robertphillips@google.comed9866c2014-01-09 19:20:45 +000089 SkPoint points[5] = {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +000090 { pos.fX, pos.fY },
91 { pos.fX + bm.width() - 1, pos.fY },
92 { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 },
93 { pos.fX, pos.fY + bm.height() - 1 },
94 { pos.fX, pos.fY },
robertphillips@google.comed9866c2014-01-09 19:20:45 +000095 };
96
97 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000098 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +000099}
100
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000101static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000102 const SkBitmap& altBM, const SkPoint& pos,
103 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000104 SkPaint paint;
105 init_paint(&paint, bm);
106
107 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
108 r.offset(pos.fX, pos.fY);
109
110 canvas->drawRect(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000111 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000112}
113
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000114static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000115 const SkBitmap& altBM, const SkPoint& pos,
116 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000117 SkPaint paint;
118 init_paint(&paint, bm);
119
120 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
121 r.offset(pos.fX, pos.fY);
122
123 canvas->drawOval(r, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000124 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000125}
126
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000127static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000128 const SkBitmap& altBM, const SkPoint& pos,
129 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000130 SkPaint paint;
131 init_paint(&paint, bm);
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000132
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000133 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
134 r.offset(pos.fX, pos.fY);
135
136 SkRRect rr;
137 rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4);
138 canvas->drawRRect(rr, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000139 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000140}
141
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000142static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000143 const SkBitmap& altBM, const SkPoint& pos,
144 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000145 SkPaint paint;
146 init_paint(&paint, bm);
147
148 SkPath path;
149 path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height()));
150 path.lineTo(SkIntToScalar(bm.width()), 0);
151 path.close();
152 path.offset(pos.fX, pos.fY);
153
154 canvas->drawPath(path, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000155 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000156}
157
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000158static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000159 const SkBitmap& altBM, const SkPoint& pos,
160 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000161 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000162 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000163}
164
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000165static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000166 const SkBitmap& altBM, const SkPoint& pos,
167 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000168 SkPaint paint;
169 init_paint(&paint, bm);
170
171 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
172 canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000173 *usedPixRefs->append() = bm.pixelRef();
174 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000175}
176
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000177static void drawsprite_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 const SkMatrix& ctm = canvas->getTotalMatrix();
181
182 SkPoint p(pos);
183 ctm.mapPoints(&p, 1);
184
185 canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000186 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000187}
188
189#if 0
190// Although specifiable, this case doesn't seem to make sense (i.e., the
191// bitmap in the shader is never used).
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000192static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000193 const SkBitmap& altBM, const SkPoint& pos,
194 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000195 SkPaint paint;
196 init_paint(&paint, bm);
197
198 const SkMatrix& ctm = canvas->getTotalMatrix();
199
200 SkPoint p(pos);
201 ctm.mapPoints(&p, 1);
202
203 canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000204 *usedPixRefs->append() = bm.pixelRef();
205 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000206}
207#endif
208
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000209static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000210 const SkBitmap& altBM, const SkPoint& pos,
211 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000212 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
213
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000214 r.offset(pos.fX, pos.fY);
215 canvas->drawBitmapRectToRect(bm, NULL, r, NULL);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000216 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000217}
218
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000219static void drawbitmaprect_withshader_proc(SkCanvas* canvas,
220 const SkBitmap& bm,
221 const SkBitmap& altBM,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000222 const SkPoint& pos,
223 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000224 SkPaint paint;
225 init_paint(&paint, bm);
226
227 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000228 r.offset(pos.fX, pos.fY);
229
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000230 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap
231 canvas->drawBitmapRectToRect(altBM, NULL, r, &paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000232 *usedPixRefs->append() = bm.pixelRef();
233 *usedPixRefs->append() = altBM.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000234}
235
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000236static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000237 const SkBitmap& altBM, const SkPoint& pos,
238 SkTDArray<SkPixelRef*>* usedPixRefs) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000239 SkPaint paint;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000240 init_paint(&paint, bm);
241 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
242
243 canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000244 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000245}
246
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000247static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000248 const SkBitmap& altBM, const SkPoint& pos,
249 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000250 SkPaint paint;
251 init_paint(&paint, bm);
252 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
253
254 SkPoint point = { pos.fX, pos.fY + bm.height() };
255 canvas->drawPosText("O", 1, &point, 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 drawtextonpath_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
264 init_paint(&paint, bm);
265 paint.setTextSize(SkIntToScalar(1.5*bm.width()));
266
267 SkPath path;
268 path.lineTo(SkIntToScalar(bm.width()), 0);
269 path.offset(pos.fX, pos.fY+bm.height());
270
271 canvas->drawTextOnPath("O", 1, path, NULL, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000272 *usedPixRefs->append() = bm.pixelRef();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000273}
274
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000275static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000276 const SkBitmap& altBM, const SkPoint& pos,
277 SkTDArray<SkPixelRef*>* usedPixRefs) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000278 SkPaint paint;
279 init_paint(&paint, bm);
280
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000281 SkPoint verts[4] = {
282 { pos.fX, pos.fY },
283 { pos.fX + bm.width(), pos.fY },
284 { pos.fX + bm.width(), pos.fY + bm.height() },
285 { pos.fX, pos.fY + bm.height() }
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000286 };
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000287 SkPoint texs[4] = { { 0, 0 },
288 { SkIntToScalar(bm.width()), 0 },
289 { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) },
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000290 { 0, SkIntToScalar(bm.height()) } };
291 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 };
292
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000293 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL,
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000294 indices, 6, paint);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000295 *usedPixRefs->append() = bm.pixelRef();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000296}
297
298// Return a picture with the bitmaps drawn at the specified positions.
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000299static SkPicture* record_bitmaps(const SkBitmap bm[],
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000300 const SkPoint pos[],
301 SkTDArray<SkPixelRef*> analytic[],
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000302 int count,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000303 DrawBitmapProc proc) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000304 SkPicture* pic = new SkPicture;
305 SkCanvas* canvas = pic->beginRecording(1000, 1000);
306 for (int i = 0; i < count; ++i) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000307 analytic[i].rewind();
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000308 canvas->save();
309 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY,
310 SkIntToScalar(bm[i].width()),
311 SkIntToScalar(bm[i].height()));
312 canvas->clipRect(clipRect, SkRegion::kIntersect_Op);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000313 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000314 canvas->restore();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000315 }
316 pic->endRecording();
317 return pic;
318}
319
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000320static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000321 rect->fLeft = rand.nextRangeScalar(-W, 2*W);
322 rect->fTop = rand.nextRangeScalar(-H, 2*H);
323 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W);
324 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H);
325
326 // we integralize rect to make our tests more predictable, since Gather is
327 // a little sloppy.
328 SkIRect ir;
329 rect->round(&ir);
330 rect->set(ir);
331}
332
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000333static void draw(SkPicture* pic, int width, int height, SkBitmap* result) {
334 make_bm(result, width, height, SK_ColorBLACK, false);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000335
336 SkCanvas canvas(*result);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000337 canvas.drawPicture(*pic);
338}
339
340template <typename T> int find_index(const T* array, T elem, int count) {
341 for (int i = 0; i < count; ++i) {
342 if (array[i] == elem) {
343 return i;
344 }
345 }
346 return -1;
347}
348
349// Return true if 'ref' is found in array[]
350static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) {
351 return find_index<const SkPixelRef*>(array, ref, count) >= 0;
352}
353
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000354// Look at each pixel that is inside 'subset', and if its color appears in
355// colors[], find the corresponding value in refs[] and append that ref into
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000356// array, skipping duplicates of the same value.
357// Note that gathering pixelRefs from rendered colors suffers from the problem
358// that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color)
359// isn't easy to reconstruct.
360static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[],
361 int count, SkTDArray<SkPixelRef*>* array,
362 const SkRect& subset) {
363 SkIRect ir;
364 subset.roundOut(&ir);
365
366 if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) {
367 return;
368 }
369
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000370 // Since we only want to return unique values in array, when we scan we just
371 // set a bit for each index'd color found. In practice we only have a few
372 // distinct colors, so we just use an int's bits as our array. Hence the
373 // assert that count <= number-of-bits-in-our-int.
374 SkASSERT((unsigned)count <= 32);
375 uint32_t bitarray = 0;
376
377 SkAutoLockPixels alp(bm);
378
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000379 for (int y = ir.fTop; y < ir.fBottom; ++y) {
380 for (int x = ir.fLeft; x < ir.fRight; ++x) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000381 SkPMColor pmc = *bm.getAddr32(x, y);
382 // the only good case where the color is not found would be if
383 // the color is transparent, meaning no bitmap was drawn in that
384 // pixel.
385 if (pmc) {
bsalomon@google.comc3d753e2013-01-08 17:24:44 +0000386 uint32_t index = SkGetPackedR32(pmc);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000387 SkASSERT(SkGetPackedG32(pmc) == index);
388 SkASSERT(SkGetPackedB32(pmc) == index);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000389 if (0 == index) {
390 continue; // background color
391 }
392 SkASSERT(0 == (index - gColorOffset) % gColorScale);
393 index = (index - gColorOffset) / gColorScale;
bsalomon@google.com5f429b02013-01-08 18:42:20 +0000394 SkASSERT(static_cast<int>(index) < count);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000395 bitarray |= 1 << index;
396 }
397 }
398 }
399
400 for (int i = 0; i < count; ++i) {
401 if (bitarray & (1 << i)) {
402 *array->append() = refs[i];
403 }
404 }
405}
406
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000407static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h,
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000408 const SkTDArray<SkPixelRef*> analytic[],
409 int count,
410 SkTDArray<SkPixelRef*>* result,
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000411 const SkRect& subset) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000412 for (int i = 0; i < count; ++i) {
413 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h);
414
415 if (SkRect::Intersects(subset, rect)) {
416 result->append(analytic[i].count(), analytic[i].begin());
417 }
418 }
419}
420
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000421
422static const struct {
423 const DrawBitmapProc proc;
424 const char* const desc;
425} gProcs[] = {
426 {drawpaint_proc, "drawpaint"},
427 {drawpoints_proc, "drawpoints"},
428 {drawrect_proc, "drawrect"},
429 {drawoval_proc, "drawoval"},
430 {drawrrect_proc, "drawrrect"},
431 {drawpath_proc, "drawpath"},
432 {drawbitmap_proc, "drawbitmap"},
433 {drawbitmap_withshader_proc, "drawbitmap_withshader"},
434 {drawsprite_proc, "drawsprite"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000435#if 0
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000436 {drawsprite_withshader_proc, "drawsprite_withshader"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000437#endif
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000438 {drawbitmaprect_proc, "drawbitmaprect"},
439 {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"},
440 {drawtext_proc, "drawtext"},
441 {drawpostext_proc, "drawpostext"},
442 {drawtextonpath_proc, "drawtextonpath"},
443 {drawverts_proc, "drawverts"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000444};
445
446static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000447 // Our convention is that the color components contain an encoding of
448 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000449 // reserved for the background
450 for (int i = 0; i < num; ++i) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000451 make_bm(&bm[i], w, h,
452 SkColorSetARGB(0xFF,
453 gColorScale*i+gColorOffset,
454 gColorScale*i+gColorOffset,
455 gColorScale*i+gColorOffset),
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000456 true);
457 refs[i] = bm[i].pixelRef();
458 }
459
460 // The A8 alternate bitmaps are all BW checkerboards
461 for (int i = 0; i < num; ++i) {
462 make_checkerboard(&bm[num+i], w, h, true);
463 refs[num+i] = bm[num+i].pixelRef();
464 }
465}
466
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000467static void test_gatherpixelrefs(skiatest::Reporter* reporter) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000468 const int IW = 32;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000469 const int IH = IW;
470 const SkScalar W = SkIntToScalar(IW);
471 const SkScalar H = W;
472
473 static const int N = 4;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000474 SkBitmap bm[2*N];
475 SkPixelRef* refs[2*N];
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000476 SkTDArray<SkPixelRef*> analytic[N];
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000477
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000478 const SkPoint pos[N] = {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000479 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
480 };
481
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000482 create_textures(bm, refs, N, IW, IH);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000483
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000484 SkRandom rand;
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000485 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000486 SkAutoTUnref<SkPicture> pic(
487 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000488
tomhudson@google.com381010e2013-10-24 11:12:47 +0000489 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000490 // quick check for a small piece of each quadrant, which should just
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000491 // contain 1 or 2 bitmaps.
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000492 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
493 SkRect r;
494 r.set(2, 2, W - 2, H - 2);
495 r.offset(pos[i].fX, pos[i].fY);
496 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r));
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000497 if (!data) {
498 ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned "
499 "NULL for %s.", gProcs[k].desc);
500 continue;
501 }
502 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data();
503 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*));
504 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
505 if (1 == count) {
506 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
507 } else if (2 == count) {
508 REPORTER_ASSERT(reporter,
509 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
510 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
commit-bot@chromium.orgfe433c12013-07-09 16:04:32 +0000511 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000512 }
513
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000514 SkBitmap image;
515 draw(pic, 2*IW, 2*IH, &image);
516
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000517 // Test a bunch of random (mostly) rects, and compare the gather results
518 // with a deduced list of refs by looking at the colors drawn.
519 for (int j = 0; j < 100; ++j) {
520 SkRect r;
521 rand_rect(&r, rand, 2*W, 2*H);
522
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000523 SkTDArray<SkPixelRef*> fromImage;
524 gather_from_image(image, refs, N, &fromImage, r);
525
526 SkTDArray<SkPixelRef*> fromAnalytic;
527 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000528
529 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
530 size_t dataSize = data ? data->size() : 0;
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000531 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000532 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize);
533 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL;
534 SkAutoDataUnref adu(data);
535
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000536 // Everything that we saw drawn should appear in the analytic list
537 // but the analytic list may contain some pixelRefs that were not
538 // seen in the image (e.g., A8 textures used as masks)
539 for (int i = 0; i < fromImage.count(); ++i) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000540 if (-1 == fromAnalytic.find(fromImage[i])) {
541 ERRORF(reporter, "PixelRef missing %d %s",
542 i, gProcs[k].desc);
543 }
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000544 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000545
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000546 /*
547 * GatherPixelRefs is conservative, so it can return more bitmaps
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000548 * than are strictly required. Thus our check here is only that
549 * Gather didn't miss any that we actually needed. Even that isn't
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000550 * a strict requirement on Gather, which is meant to be quick and
551 * only mostly-correct, but at the moment this test should work.
552 */
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000553 for (int i = 0; i < fromAnalytic.count(); ++i) {
554 bool found = find(gatherRefs, fromAnalytic[i], gatherCount);
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000555 if (!found) {
556 ERRORF(reporter, "PixelRef missing %d %s",
557 i, gProcs[k].desc);
558 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000559#if 0
560 // enable this block of code to debug failures, as it will rerun
561 // the case that failed.
562 if (!found) {
563 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
564 size_t dataSize = data ? data->size() : 0;
565 }
566#endif
567 }
568 }
569 }
570}
571
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000572static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) {
573 const int IW = 32;
574 const int IH = IW;
575 const SkScalar W = SkIntToScalar(IW);
576 const SkScalar H = W;
577
578 static const int N = 4;
579 SkBitmap bm[2*N];
580 SkPixelRef* refs[2*N];
581 SkTDArray<SkPixelRef*> analytic[N];
582
583 const SkPoint pos[N] = {
584 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
585 };
586
587 create_textures(bm, refs, N, IW, IH);
588
589 SkRandom rand;
590 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000591 SkAutoTUnref<SkPicture> pic(
592 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000593
594 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
595
596 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont(
597 new SkPictureUtils::SkPixelRefsAndRectsList);
598
599 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont);
600
601 // quick check for a small piece of each quadrant, which should just
602 // contain 1 or 2 bitmaps.
603 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
604 SkRect r;
605 r.set(2, 2, W - 2, H - 2);
606 r.offset(pos[i].fX, pos[i].fY);
607
608 SkTDArray<SkPixelRef*> gatheredRefs;
609 prCont->query(r, &gatheredRefs);
610
611 int count = gatheredRefs.count();
612 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
613 if (1 == count) {
614 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
615 } else if (2 == count) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000616 REPORTER_ASSERT(reporter,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000617 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
618 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
619 }
620 }
621
622 SkBitmap image;
623 draw(pic, 2*IW, 2*IH, &image);
624
625 // Test a bunch of random (mostly) rects, and compare the gather results
626 // with the analytic results and the pixel refs seen in a rendering.
627 for (int j = 0; j < 100; ++j) {
628 SkRect r;
629 rand_rect(&r, rand, 2*W, 2*H);
630
631 SkTDArray<SkPixelRef*> fromImage;
632 gather_from_image(image, refs, N, &fromImage, r);
633
634 SkTDArray<SkPixelRef*> fromAnalytic;
635 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
636
637 SkTDArray<SkPixelRef*> gatheredRefs;
638 prCont->query(r, &gatheredRefs);
639
640 // Everything that we saw drawn should appear in the analytic list
641 // but the analytic list may contain some pixelRefs that were not
642 // seen in the image (e.g., A8 textures used as masks)
643 for (int i = 0; i < fromImage.count(); ++i) {
644 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]));
645 }
646
647 // Everything in the analytic list should appear in the gathered
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000648 // list.
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000649 for (int i = 0; i < fromAnalytic.count(); ++i) {
650 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i]));
651 }
652 }
653 }
654}
655
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000656#ifdef SK_DEBUG
657// Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only
658// run in debug mode.
659static void test_deleting_empty_playback() {
660 SkPicture picture;
661 // Creates an SkPictureRecord
662 picture.beginRecording(0, 0);
663 // Turns that into an SkPicturePlayback
664 picture.endRecording();
665 // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord
666 picture.beginRecording(0, 0);
667}
668
669// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
670static void test_serializing_empty_picture() {
671 SkPicture picture;
672 picture.beginRecording(0, 0);
673 picture.endRecording();
674 SkDynamicMemoryWStream stream;
675 picture.serialize(&stream);
676}
677#endif
678
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000679static void rand_op(SkCanvas* canvas, SkRandom& rand) {
reed@google.com21b519d2012-10-02 17:42:15 +0000680 SkPaint paint;
681 SkRect rect = SkRect::MakeWH(50, 50);
682
683 SkScalar unit = rand.nextUScalar1();
684 if (unit <= 0.3) {
685// SkDebugf("save\n");
686 canvas->save();
687 } else if (unit <= 0.6) {
688// SkDebugf("restore\n");
689 canvas->restore();
690 } else if (unit <= 0.9) {
691// SkDebugf("clip\n");
692 canvas->clipRect(rect);
693 } else {
694// SkDebugf("draw\n");
695 canvas->drawPaint(paint);
696 }
697}
698
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000699static void set_canvas_to_save_count_4(SkCanvas* canvas) {
700 canvas->restoreToCount(1);
701 canvas->save();
702 canvas->save();
703 canvas->save();
704}
705
706static void test_unbalanced_save_restores(skiatest::Reporter* reporter) {
707 SkCanvas testCanvas(100, 100);
708 set_canvas_to_save_count_4(&testCanvas);
709
710 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
711
712 SkPaint paint;
713 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000);
714
715 SkPicture extra_save_picture;
716 extra_save_picture.beginRecording(100, 100);
717 extra_save_picture.getRecordingCanvas()->save();
718 extra_save_picture.getRecordingCanvas()->translate(10, 10);
719 extra_save_picture.getRecordingCanvas()->drawRect(rect, paint);
720 extra_save_picture.getRecordingCanvas()->save();
721 extra_save_picture.getRecordingCanvas()->translate(10, 10);
722 extra_save_picture.getRecordingCanvas()->drawRect(rect, paint);
723
724 testCanvas.drawPicture(extra_save_picture);
725 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
726
727 set_canvas_to_save_count_4(&testCanvas);
728
729 SkPicture extra_restore_picture;
730 extra_restore_picture.beginRecording(100, 100);
731 extra_restore_picture.getRecordingCanvas()->save();
732 extra_restore_picture.getRecordingCanvas()->translate(10, 10);
733 extra_restore_picture.getRecordingCanvas()->drawRect(rect, paint);
734 extra_restore_picture.getRecordingCanvas()->save();
735 extra_restore_picture.getRecordingCanvas()->translate(10, 10);
736 extra_restore_picture.getRecordingCanvas()->drawRect(rect, paint);
737 extra_restore_picture.getRecordingCanvas()->restore();
738 extra_restore_picture.getRecordingCanvas()->restore();
739 extra_restore_picture.getRecordingCanvas()->restore();
740 extra_restore_picture.getRecordingCanvas()->restore();
741
742 testCanvas.drawPicture(extra_save_picture);
743 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
744
745 SkPicture no_save_picture;
746 extra_restore_picture.beginRecording(100, 100);
747 extra_restore_picture.getRecordingCanvas()->translate(10, 10);
748 extra_restore_picture.getRecordingCanvas()->drawRect(rect, paint);
749
750 testCanvas.drawPicture(extra_save_picture);
751 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
752 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity());
753}
754
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000755static void test_peephole() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000756 SkRandom rand;
reed@google.com21b519d2012-10-02 17:42:15 +0000757
758 for (int j = 0; j < 100; j++) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000759 SkRandom rand2(rand); // remember the seed
reed@google.com21b519d2012-10-02 17:42:15 +0000760
761 SkPicture picture;
762 SkCanvas* canvas = picture.beginRecording(100, 100);
763
764 for (int i = 0; i < 1000; ++i) {
765 rand_op(canvas, rand);
766 }
767 picture.endRecording();
jvanverth@google.comc490f802013-03-04 13:56:38 +0000768
769 rand = rand2;
reed@google.com21b519d2012-10-02 17:42:15 +0000770 }
771
772 {
773 SkPicture picture;
774 SkCanvas* canvas = picture.beginRecording(100, 100);
775 SkRect rect = SkRect::MakeWH(50, 50);
skia.committer@gmail.com52c24372012-10-03 02:01:13 +0000776
reed@google.com21b519d2012-10-02 17:42:15 +0000777 for (int i = 0; i < 100; ++i) {
778 canvas->save();
779 }
780 while (canvas->getSaveCount() > 1) {
781 canvas->clipRect(rect);
782 canvas->restore();
783 }
784 picture.endRecording();
785 }
786}
787
scroggo@google.com4b90b112012-12-04 15:08:56 +0000788#ifndef SK_DEBUG
789// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
790// should never do this.
791static void test_bad_bitmap() {
792 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
793 // fail.
794 SkBitmap bm;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000795 bm.setConfig(SkImageInfo::MakeN32Premul(100, 100));
scroggo@google.com4b90b112012-12-04 15:08:56 +0000796 SkPicture picture;
797 SkCanvas* recordingCanvas = picture.beginRecording(100, 100);
798 recordingCanvas->drawBitmap(bm, 0, 0);
799 picture.endRecording();
800
801 SkCanvas canvas;
802 canvas.drawPicture(picture);
803}
804#endif
805
reed@google.com672588b2014-01-08 15:42:01 +0000806static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000807 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000808}
809
810static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
811 SkPicture picture;
812 SkCanvas* canvas = picture.beginRecording(bitmap.width(), bitmap.height());
813 canvas->drawBitmap(bitmap, 0, 0);
814 SkDynamicMemoryWStream wStream;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000815 picture.serialize(&wStream, &encode_bitmap_to_data);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000816 return wStream.copyToData();
817}
818
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000819struct ErrorContext {
820 int fErrors;
821 skiatest::Reporter* fReporter;
822};
823
824static void assert_one_parse_error_cb(SkError error, void* context) {
825 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
826 errorContext->fErrors++;
827 // This test only expects one error, and that is a kParseError. If there are others,
828 // there is some unknown problem.
829 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors,
830 "This threw more errors than expected.");
831 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error,
832 SkGetLastErrorString());
833}
834
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000835static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
836 // Create a bitmap that will be encoded.
837 SkBitmap original;
838 make_bm(&original, 100, 100, SK_ColorBLUE, true);
839 SkDynamicMemoryWStream wStream;
840 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
841 return;
842 }
843 SkAutoDataUnref data(wStream.copyToData());
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000844
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000845 SkBitmap bm;
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000846 bool installSuccess = SkInstallDiscardablePixelRef(
847 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm, NULL);
reed@google.combf790232013-12-13 19:45:58 +0000848 REPORTER_ASSERT(reporter, installSuccess);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000849
850 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
851 // Flattening original will follow the old path of performing an encode, while flattening bm
852 // will use the already encoded data.
853 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
854 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
855 REPORTER_ASSERT(reporter, picture1->equals(picture2));
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000856 // Now test that a parse error was generated when trying to create a new SkPicture without
857 // providing a function to decode the bitmap.
858 ErrorContext context;
859 context.fErrors = 0;
860 context.fReporter = reporter;
861 SkSetErrorCallback(assert_one_parse_error_cb, &context);
862 SkMemoryStream pictureStream(picture1);
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000863 SkClearLastError();
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000864 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL));
865 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000866 SkClearLastError();
867 SkSetErrorCallback(NULL, NULL);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000868}
869
junov@chromium.org94f20dc2013-01-28 21:04:44 +0000870static void test_clone_empty(skiatest::Reporter* reporter) {
871 // This is a regression test for crbug.com/172062
872 // Before the fix, we used to crash accessing a null pointer when we
873 // had a picture with no paints. This test passes by not crashing.
874 {
875 SkPicture picture;
876 picture.beginRecording(1, 1);
877 picture.endRecording();
878 SkPicture* destPicture = picture.clone();
879 REPORTER_ASSERT(reporter, NULL != destPicture);
880 destPicture->unref();
881 }
882 {
883 // Test without call to endRecording
884 SkPicture picture;
885 picture.beginRecording(1, 1);
886 SkPicture* destPicture = picture.clone();
887 REPORTER_ASSERT(reporter, NULL != destPicture);
888 destPicture->unref();
889 }
890}
891
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000892static void test_draw_empty(skiatest::Reporter* reporter) {
893 SkBitmap result;
894 make_bm(&result, 2, 2, SK_ColorBLACK, false);
895
896 SkCanvas canvas(result);
897
898 {
899 // stock SkPicture
900 SkPicture picture;
901 picture.beginRecording(1, 1);
902 picture.endRecording();
903
904 canvas.drawPicture(picture);
905 }
906
907 {
908 // tile grid
909 SkTileGridPicture::TileGridInfo gridInfo;
910 gridInfo.fMargin.setEmpty();
911 gridInfo.fOffset.setZero();
912 gridInfo.fTileInterval.set(1, 1);
913
914 SkTileGridPicture picture(1, 1, gridInfo);
915 picture.beginRecording(1, 1, SkPicture::kOptimizeForClippedPlayback_RecordingFlag);
916 picture.endRecording();
917
918 canvas.drawPicture(picture);
919 }
920
921 {
922 // RTree
923 SkPicture picture;
924 picture.beginRecording(1, 1, SkPicture::kOptimizeForClippedPlayback_RecordingFlag);
925 picture.endRecording();
926
927 canvas.drawPicture(picture);
928 }
929
930 {
931 // quad tree
932 SkQuadTreePicture picture(SkIRect::MakeWH(1, 1));
933 picture.beginRecording(1, 1, SkPicture::kOptimizeForClippedPlayback_RecordingFlag);
934 picture.endRecording();
935
936 canvas.drawPicture(picture);
937 }
938}
939
junov@chromium.orgd575eed2013-05-08 15:39:13 +0000940static void test_clip_bound_opt(skiatest::Reporter* reporter) {
941 // Test for crbug.com/229011
942 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
943 SkIntToScalar(2), SkIntToScalar(2));
944 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
945 SkIntToScalar(1), SkIntToScalar(1));
946 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
947 SkIntToScalar(1), SkIntToScalar(1));
948
949 SkPath invPath;
950 invPath.addOval(rect1);
951 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
952 SkPath path;
953 path.addOval(rect2);
954 SkPath path2;
955 path2.addOval(rect3);
956 SkIRect clipBounds;
957 // Minimalist test set for 100% code coverage of
958 // SkPictureRecord::updateClipConservativelyUsingBounds
959 {
960 SkPicture picture;
961 SkCanvas* canvas = picture.beginRecording(10, 10,
962 SkPicture::kUsePathBoundsForClip_RecordingFlag);
963 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
964 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
965 REPORTER_ASSERT(reporter, true == nonEmpty);
966 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
967 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
968 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
969 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
970 }
971 {
972 SkPicture picture;
973 SkCanvas* canvas = picture.beginRecording(10, 10,
974 SkPicture::kUsePathBoundsForClip_RecordingFlag);
975 canvas->clipPath(path, SkRegion::kIntersect_Op);
976 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
977 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
978 REPORTER_ASSERT(reporter, true == nonEmpty);
979 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
980 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
981 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
982 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
983 }
984 {
985 SkPicture picture;
986 SkCanvas* canvas = picture.beginRecording(10, 10,
987 SkPicture::kUsePathBoundsForClip_RecordingFlag);
988 canvas->clipPath(path, SkRegion::kIntersect_Op);
989 canvas->clipPath(invPath, SkRegion::kUnion_Op);
990 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
991 REPORTER_ASSERT(reporter, true == nonEmpty);
992 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
993 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
994 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
995 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
996 }
997 {
998 SkPicture picture;
999 SkCanvas* canvas = picture.beginRecording(10, 10,
1000 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1001 canvas->clipPath(path, SkRegion::kDifference_Op);
1002 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1003 REPORTER_ASSERT(reporter, true == nonEmpty);
1004 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1005 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1006 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1007 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1008 }
1009 {
1010 SkPicture picture;
1011 SkCanvas* canvas = picture.beginRecording(10, 10,
1012 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1013 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
1014 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1015 // True clip is actually empty in this case, but the best
1016 // determination we can make using only bounds as input is that the
1017 // clip is included in the bounds of 'path'.
1018 REPORTER_ASSERT(reporter, true == nonEmpty);
1019 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1020 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1021 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1022 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1023 }
1024 {
1025 SkPicture picture;
1026 SkCanvas* canvas = picture.beginRecording(10, 10,
1027 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1028 canvas->clipPath(path, SkRegion::kIntersect_Op);
1029 canvas->clipPath(path2, SkRegion::kXOR_Op);
1030 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1031 REPORTER_ASSERT(reporter, true == nonEmpty);
1032 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
1033 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
1034 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1035 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1036 }
1037}
1038
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001039/**
1040 * A canvas that records the number of clip commands.
1041 */
1042class ClipCountingCanvas : public SkCanvas {
1043public:
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001044 explicit ClipCountingCanvas(int width, int height)
1045 : INHERITED(width, height)
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001046 , fClipCount(0){
1047 }
1048
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001049 virtual void onClipRect(const SkRect& r,
1050 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001051 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001052 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001053 this->INHERITED::onClipRect(r, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001054 }
1055
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001056 virtual void onClipRRect(const SkRRect& rrect,
1057 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001058 ClipEdgeStyle edgeStyle)SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001059 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001060 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001061 }
1062
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001063 virtual void onClipPath(const SkPath& path,
1064 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001065 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001066 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001067 this->INHERITED::onClipPath(path, op, edgeStyle);
1068 }
1069
1070 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE {
1071 fClipCount += 1;
1072 this->INHERITED::onClipRegion(deviceRgn, op);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001073 }
1074
1075 unsigned getClipCount() const { return fClipCount; }
1076
1077private:
1078 unsigned fClipCount;
1079
1080 typedef SkCanvas INHERITED;
1081};
1082
1083static void test_clip_expansion(skiatest::Reporter* reporter) {
1084 SkPicture picture;
1085 SkCanvas* canvas = picture.beginRecording(10, 10, 0);
1086
1087 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
1088 // The following expanding clip should not be skipped.
1089 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
1090 // Draw something so the optimizer doesn't just fold the world.
1091 SkPaint p;
1092 p.setColor(SK_ColorBLUE);
1093 canvas->drawPaint(p);
1094
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001095 ClipCountingCanvas testCanvas(10, 10);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001096 picture.draw(&testCanvas);
1097
1098 // Both clips should be present on playback.
1099 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
1100}
1101
tomhudson@google.com381010e2013-10-24 11:12:47 +00001102static void test_hierarchical(skiatest::Reporter* reporter) {
1103 SkBitmap bm;
1104 make_bm(&bm, 10, 10, SK_ColorRED, true);
1105
1106 SkCanvas* canvas;
1107
1108 SkPicture childPlain;
1109 childPlain.beginRecording(10, 10);
1110 childPlain.endRecording();
1111 REPORTER_ASSERT(reporter, !childPlain.willPlayBackBitmaps()); // 0
1112
1113 SkPicture childWithBitmap;
1114 childWithBitmap.beginRecording(10, 10)->drawBitmap(bm, 0, 0);
1115 childWithBitmap.endRecording();
1116 REPORTER_ASSERT(reporter, childWithBitmap.willPlayBackBitmaps()); // 1
1117
1118 SkPicture parentPP;
1119 canvas = parentPP.beginRecording(10, 10);
1120 canvas->drawPicture(childPlain);
1121 parentPP.endRecording();
1122 REPORTER_ASSERT(reporter, !parentPP.willPlayBackBitmaps()); // 0
1123
1124 SkPicture parentPWB;
1125 canvas = parentPWB.beginRecording(10, 10);
1126 canvas->drawPicture(childWithBitmap);
1127 parentPWB.endRecording();
1128 REPORTER_ASSERT(reporter, parentPWB.willPlayBackBitmaps()); // 1
1129
1130 SkPicture parentWBP;
1131 canvas = parentWBP.beginRecording(10, 10);
1132 canvas->drawBitmap(bm, 0, 0);
1133 canvas->drawPicture(childPlain);
1134 parentWBP.endRecording();
1135 REPORTER_ASSERT(reporter, parentWBP.willPlayBackBitmaps()); // 1
1136
1137 SkPicture parentWBWB;
1138 canvas = parentWBWB.beginRecording(10, 10);
1139 canvas->drawBitmap(bm, 0, 0);
1140 canvas->drawPicture(childWithBitmap);
1141 parentWBWB.endRecording();
1142 REPORTER_ASSERT(reporter, parentWBWB.willPlayBackBitmaps()); // 2
1143}
1144
robertphillips@google.comd5500882014-04-02 23:51:13 +00001145static void test_gen_id(skiatest::Reporter* reporter) {
1146
1147 SkPicture hasData, empty, midRecord;
1148
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001149 uint32_t beforeID = hasData.uniqueID();
1150 REPORTER_ASSERT(reporter, SK_InvalidGenID != beforeID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001151
1152 // all 3 pictures should have different ids
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001153 REPORTER_ASSERT(reporter, beforeID != empty.uniqueID());
1154 REPORTER_ASSERT(reporter, beforeID != midRecord.uniqueID());
1155 REPORTER_ASSERT(reporter, empty.uniqueID() != midRecord.uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001156
1157 hasData.beginRecording(1, 1);
1158 // gen ID should be invalid mid-record
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001159 REPORTER_ASSERT(reporter, SK_InvalidGenID == hasData.uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001160 hasData.endRecording();
1161 // picture should get a new (non-zero) id after recording
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001162 REPORTER_ASSERT(reporter, hasData.uniqueID() != beforeID);
1163 REPORTER_ASSERT(reporter, hasData.uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001164
1165 midRecord.beginRecording(1, 1);
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001166 REPORTER_ASSERT(reporter, SK_InvalidGenID == midRecord.uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001167
1168 // test out copy constructor
1169 SkPicture copyWithData(hasData);
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001170 REPORTER_ASSERT(reporter, hasData.uniqueID() == copyWithData.uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001171
1172 SkPicture emptyCopy(empty);
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001173 REPORTER_ASSERT(reporter, empty.uniqueID() != emptyCopy.uniqueID());
skia.committer@gmail.coma9157722014-04-03 03:04:26 +00001174
robertphillips@google.comd5500882014-04-02 23:51:13 +00001175 SkPicture copyMidRecord(midRecord);
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001176 REPORTER_ASSERT(reporter, midRecord.uniqueID() != copyMidRecord.uniqueID());
1177 REPORTER_ASSERT(reporter, copyMidRecord.uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001178
1179 // test out swap
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001180 beforeID = copyMidRecord.uniqueID();
robertphillips@google.comd5500882014-04-02 23:51:13 +00001181 copyWithData.swap(copyMidRecord);
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001182 REPORTER_ASSERT(reporter, copyWithData.uniqueID() == beforeID);
1183 REPORTER_ASSERT(reporter, copyMidRecord.uniqueID() == hasData.uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001184
1185 // test out clone
1186 SkAutoTUnref<SkPicture> cloneWithData(hasData.clone());
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001187 REPORTER_ASSERT(reporter, hasData.uniqueID() == cloneWithData->uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001188
1189 SkAutoTUnref<SkPicture> emptyClone(empty.clone());
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001190 REPORTER_ASSERT(reporter, empty.uniqueID() != emptyClone->uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001191
1192 SkAutoTUnref<SkPicture> cloneMidRecord(midRecord.clone());
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001193 REPORTER_ASSERT(reporter, midRecord.uniqueID() != cloneMidRecord->uniqueID());
1194 REPORTER_ASSERT(reporter, cloneMidRecord->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001195}
1196
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00001197DEF_TEST(Picture, reporter) {
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001198#ifdef SK_DEBUG
1199 test_deleting_empty_playback();
1200 test_serializing_empty_picture();
scroggo@google.com4b90b112012-12-04 15:08:56 +00001201#else
1202 test_bad_bitmap();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001203#endif
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001204 test_unbalanced_save_restores(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001205 test_peephole();
reed@google.comfe7b1ed2012-11-29 21:00:39 +00001206 test_gatherpixelrefs(reporter);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +00001207 test_gatherpixelrefsandrects(reporter);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001208 test_bitmap_with_encoded_data(reporter);
junov@chromium.org94f20dc2013-01-28 21:04:44 +00001209 test_clone_empty(reporter);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001210 test_draw_empty(reporter);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001211 test_clip_bound_opt(reporter);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001212 test_clip_expansion(reporter);
tomhudson@google.com381010e2013-10-24 11:12:47 +00001213 test_hierarchical(reporter);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001214 test_gen_id(reporter);
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001215}
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001216
1217static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) {
1218 const SkPaint paint;
1219 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f };
1220 const SkIRect irect = { 2, 2, 3, 3 };
1221
1222 // Don't care what these record, as long as they're legal.
1223 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint);
1224 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag);
1225 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint);
1226 canvas->drawBitmapNine(bitmap, irect, rect, &paint);
1227 canvas->drawSprite(bitmap, 1, 1);
1228}
1229
1230static void test_draw_bitmaps(SkCanvas* canvas) {
1231 SkBitmap empty;
1232 draw_bitmaps(empty, canvas);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001233 empty.setConfig(SkImageInfo::MakeN32Premul(10, 10));
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001234 draw_bitmaps(empty, canvas);
1235}
1236
1237DEF_TEST(Picture_EmptyBitmap, r) {
1238 SkPicture picture;
1239 test_draw_bitmaps(picture.beginRecording(10, 10));
1240 picture.endRecording();
1241}
1242
1243DEF_TEST(Canvas_EmptyBitmap, r) {
1244 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001245 dst.allocN32Pixels(10, 10);
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001246 SkCanvas canvas(dst);
1247
1248 test_draw_bitmaps(&canvas);
1249}