blob: 2fe3885e7e32c7c2b920769780d9a5f21e2e9f52 [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
scroggo@google.comd614c6a2012-09-14 17:26:37 +00008#include "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000010#include "SkBitmapDevice.h"
reed@google.com21b519d2012-10-02 17:42:15 +000011#include "SkCanvas.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000012#include "SkColorPriv.h"
13#include "SkData.h"
reed@google.combf790232013-12-13 19:45:58 +000014#include "SkDecodingImageGenerator.h"
scroggo@google.com49ce11b2013-04-25 18:29:32 +000015#include "SkError.h"
halcanary@google.com3d50ea12014-01-02 13:15:13 +000016#include "SkImageEncoder.h"
17#include "SkImageGenerator.h"
reed@google.com21b519d2012-10-02 17:42:15 +000018#include "SkPaint.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000019#include "SkPicture.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000020#include "SkPictureUtils.h"
reed@google.com21b519d2012-10-02 17:42:15 +000021#include "SkRandom.h"
reed@google.com72aa79c2013-01-24 18:27:42 +000022#include "SkRRect.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000023#include "SkShader.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000024#include "SkStream.h"
25
robertphillips@google.comed9866c2014-01-09 19:20:45 +000026static const int gColorScale = 30;
27static const int gColorOffset = 60;
reed@google.comfe7b1ed2012-11-29 21:00:39 +000028
29static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) {
30 bm->setConfig(SkBitmap::kARGB_8888_Config, w, h);
31 bm->allocPixels();
32 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);
41 bm->setConfig(SkBitmap::kA8_Config, w, h);
42 bm->allocPixels();
43 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
421static const DrawBitmapProc gProcs[] = {
422 drawpaint_proc,
423 drawpoints_proc,
424 drawrect_proc,
425 drawoval_proc,
426 drawrrect_proc,
427 drawpath_proc,
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000428 drawbitmap_proc,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000429 drawbitmap_withshader_proc,
430 drawsprite_proc,
431#if 0
432 drawsprite_withshader_proc,
433#endif
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000434 drawbitmaprect_proc,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000435 drawbitmaprect_withshader_proc,
436 drawtext_proc,
437 drawpostext_proc,
438 drawtextonpath_proc,
439 drawverts_proc,
440};
441
442static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000443 // Our convention is that the color components contain an encoding of
444 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000445 // reserved for the background
446 for (int i = 0; i < num; ++i) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000447 make_bm(&bm[i], w, h,
448 SkColorSetARGB(0xFF,
449 gColorScale*i+gColorOffset,
450 gColorScale*i+gColorOffset,
451 gColorScale*i+gColorOffset),
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000452 true);
453 refs[i] = bm[i].pixelRef();
454 }
455
456 // The A8 alternate bitmaps are all BW checkerboards
457 for (int i = 0; i < num; ++i) {
458 make_checkerboard(&bm[num+i], w, h, true);
459 refs[num+i] = bm[num+i].pixelRef();
460 }
461}
462
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000463static void test_gatherpixelrefs(skiatest::Reporter* reporter) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000464 const int IW = 32;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000465 const int IH = IW;
466 const SkScalar W = SkIntToScalar(IW);
467 const SkScalar H = W;
468
469 static const int N = 4;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000470 SkBitmap bm[2*N];
471 SkPixelRef* refs[2*N];
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000472 SkTDArray<SkPixelRef*> analytic[N];
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000473
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000474 const SkPoint pos[N] = {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000475 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
476 };
477
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000478 create_textures(bm, refs, N, IW, IH);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000479
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000480 SkRandom rand;
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000481 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
482 SkAutoTUnref<SkPicture> pic(record_bitmaps(bm, pos, analytic, N, gProcs[k]));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000483
tomhudson@google.com381010e2013-10-24 11:12:47 +0000484 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000485 // quick check for a small piece of each quadrant, which should just
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000486 // contain 1 or 2 bitmaps.
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000487 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
488 SkRect r;
489 r.set(2, 2, W - 2, H - 2);
490 r.offset(pos[i].fX, pos[i].fY);
491 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r));
492 REPORTER_ASSERT(reporter, data);
commit-bot@chromium.orgfe433c12013-07-09 16:04:32 +0000493 if (data) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000494 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data();
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000495 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*));
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000496 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
497 if (1 == count) {
498 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
499 } else if (2 == count) {
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000500 REPORTER_ASSERT(reporter,
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000501 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
502 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
503 }
commit-bot@chromium.orgfe433c12013-07-09 16:04:32 +0000504 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000505 }
506
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000507 SkBitmap image;
508 draw(pic, 2*IW, 2*IH, &image);
509
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000510 // Test a bunch of random (mostly) rects, and compare the gather results
511 // with a deduced list of refs by looking at the colors drawn.
512 for (int j = 0; j < 100; ++j) {
513 SkRect r;
514 rand_rect(&r, rand, 2*W, 2*H);
515
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000516 SkTDArray<SkPixelRef*> fromImage;
517 gather_from_image(image, refs, N, &fromImage, r);
518
519 SkTDArray<SkPixelRef*> fromAnalytic;
520 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000521
522 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
523 size_t dataSize = data ? data->size() : 0;
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000524 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000525 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize);
526 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL;
527 SkAutoDataUnref adu(data);
528
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000529 // Everything that we saw drawn should appear in the analytic list
530 // but the analytic list may contain some pixelRefs that were not
531 // seen in the image (e.g., A8 textures used as masks)
532 for (int i = 0; i < fromImage.count(); ++i) {
533 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]));
534 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000535
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000536 /*
537 * GatherPixelRefs is conservative, so it can return more bitmaps
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000538 * than are strictly required. Thus our check here is only that
539 * Gather didn't miss any that we actually needed. Even that isn't
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000540 * a strict requirement on Gather, which is meant to be quick and
541 * only mostly-correct, but at the moment this test should work.
542 */
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000543 for (int i = 0; i < fromAnalytic.count(); ++i) {
544 bool found = find(gatherRefs, fromAnalytic[i], gatherCount);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000545 REPORTER_ASSERT(reporter, found);
546#if 0
547 // enable this block of code to debug failures, as it will rerun
548 // the case that failed.
549 if (!found) {
550 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
551 size_t dataSize = data ? data->size() : 0;
552 }
553#endif
554 }
555 }
556 }
557}
558
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000559static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) {
560 const int IW = 32;
561 const int IH = IW;
562 const SkScalar W = SkIntToScalar(IW);
563 const SkScalar H = W;
564
565 static const int N = 4;
566 SkBitmap bm[2*N];
567 SkPixelRef* refs[2*N];
568 SkTDArray<SkPixelRef*> analytic[N];
569
570 const SkPoint pos[N] = {
571 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
572 };
573
574 create_textures(bm, refs, N, IW, IH);
575
576 SkRandom rand;
577 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
578 SkAutoTUnref<SkPicture> pic(record_bitmaps(bm, pos, analytic, N, gProcs[k]));
579
580 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
581
582 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont(
583 new SkPictureUtils::SkPixelRefsAndRectsList);
584
585 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont);
586
587 // quick check for a small piece of each quadrant, which should just
588 // contain 1 or 2 bitmaps.
589 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
590 SkRect r;
591 r.set(2, 2, W - 2, H - 2);
592 r.offset(pos[i].fX, pos[i].fY);
593
594 SkTDArray<SkPixelRef*> gatheredRefs;
595 prCont->query(r, &gatheredRefs);
596
597 int count = gatheredRefs.count();
598 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
599 if (1 == count) {
600 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
601 } else if (2 == count) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000602 REPORTER_ASSERT(reporter,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000603 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
604 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
605 }
606 }
607
608 SkBitmap image;
609 draw(pic, 2*IW, 2*IH, &image);
610
611 // Test a bunch of random (mostly) rects, and compare the gather results
612 // with the analytic results and the pixel refs seen in a rendering.
613 for (int j = 0; j < 100; ++j) {
614 SkRect r;
615 rand_rect(&r, rand, 2*W, 2*H);
616
617 SkTDArray<SkPixelRef*> fromImage;
618 gather_from_image(image, refs, N, &fromImage, r);
619
620 SkTDArray<SkPixelRef*> fromAnalytic;
621 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
622
623 SkTDArray<SkPixelRef*> gatheredRefs;
624 prCont->query(r, &gatheredRefs);
625
626 // Everything that we saw drawn should appear in the analytic list
627 // but the analytic list may contain some pixelRefs that were not
628 // seen in the image (e.g., A8 textures used as masks)
629 for (int i = 0; i < fromImage.count(); ++i) {
630 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]));
631 }
632
633 // Everything in the analytic list should appear in the gathered
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000634 // list.
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000635 for (int i = 0; i < fromAnalytic.count(); ++i) {
636 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i]));
637 }
638 }
639 }
640}
641
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000642#ifdef SK_DEBUG
643// Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only
644// run in debug mode.
645static void test_deleting_empty_playback() {
646 SkPicture picture;
647 // Creates an SkPictureRecord
648 picture.beginRecording(0, 0);
649 // Turns that into an SkPicturePlayback
650 picture.endRecording();
651 // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord
652 picture.beginRecording(0, 0);
653}
654
655// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
656static void test_serializing_empty_picture() {
657 SkPicture picture;
658 picture.beginRecording(0, 0);
659 picture.endRecording();
660 SkDynamicMemoryWStream stream;
661 picture.serialize(&stream);
662}
663#endif
664
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000665static void rand_op(SkCanvas* canvas, SkRandom& rand) {
reed@google.com21b519d2012-10-02 17:42:15 +0000666 SkPaint paint;
667 SkRect rect = SkRect::MakeWH(50, 50);
668
669 SkScalar unit = rand.nextUScalar1();
670 if (unit <= 0.3) {
671// SkDebugf("save\n");
672 canvas->save();
673 } else if (unit <= 0.6) {
674// SkDebugf("restore\n");
675 canvas->restore();
676 } else if (unit <= 0.9) {
677// SkDebugf("clip\n");
678 canvas->clipRect(rect);
679 } else {
680// SkDebugf("draw\n");
681 canvas->drawPaint(paint);
682 }
683}
684
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000685static void test_peephole() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000686 SkRandom rand;
reed@google.com21b519d2012-10-02 17:42:15 +0000687
688 for (int j = 0; j < 100; j++) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000689 SkRandom rand2(rand); // remember the seed
reed@google.com21b519d2012-10-02 17:42:15 +0000690
691 SkPicture picture;
692 SkCanvas* canvas = picture.beginRecording(100, 100);
693
694 for (int i = 0; i < 1000; ++i) {
695 rand_op(canvas, rand);
696 }
697 picture.endRecording();
jvanverth@google.comc490f802013-03-04 13:56:38 +0000698
699 rand = rand2;
reed@google.com21b519d2012-10-02 17:42:15 +0000700 }
701
702 {
703 SkPicture picture;
704 SkCanvas* canvas = picture.beginRecording(100, 100);
705 SkRect rect = SkRect::MakeWH(50, 50);
skia.committer@gmail.com52c24372012-10-03 02:01:13 +0000706
reed@google.com21b519d2012-10-02 17:42:15 +0000707 for (int i = 0; i < 100; ++i) {
708 canvas->save();
709 }
710 while (canvas->getSaveCount() > 1) {
711 canvas->clipRect(rect);
712 canvas->restore();
713 }
714 picture.endRecording();
715 }
716}
717
scroggo@google.com4b90b112012-12-04 15:08:56 +0000718#ifndef SK_DEBUG
719// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
720// should never do this.
721static void test_bad_bitmap() {
722 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
723 // fail.
724 SkBitmap bm;
725 bm.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
726 SkPicture picture;
727 SkCanvas* recordingCanvas = picture.beginRecording(100, 100);
728 recordingCanvas->drawBitmap(bm, 0, 0);
729 picture.endRecording();
730
731 SkCanvas canvas;
732 canvas.drawPicture(picture);
733}
734#endif
735
reed@google.com672588b2014-01-08 15:42:01 +0000736static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000737 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000738}
739
740static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
741 SkPicture picture;
742 SkCanvas* canvas = picture.beginRecording(bitmap.width(), bitmap.height());
743 canvas->drawBitmap(bitmap, 0, 0);
744 SkDynamicMemoryWStream wStream;
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000745 picture.serialize(&wStream, &encode_bitmap_to_data);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000746 return wStream.copyToData();
747}
748
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000749struct ErrorContext {
750 int fErrors;
751 skiatest::Reporter* fReporter;
752};
753
754static void assert_one_parse_error_cb(SkError error, void* context) {
755 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
756 errorContext->fErrors++;
757 // This test only expects one error, and that is a kParseError. If there are others,
758 // there is some unknown problem.
759 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors,
760 "This threw more errors than expected.");
761 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error,
762 SkGetLastErrorString());
763}
764
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000765static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
766 // Create a bitmap that will be encoded.
767 SkBitmap original;
768 make_bm(&original, 100, 100, SK_ColorBLUE, true);
769 SkDynamicMemoryWStream wStream;
770 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
771 return;
772 }
773 SkAutoDataUnref data(wStream.copyToData());
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000774
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000775 SkBitmap bm;
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000776 bool installSuccess = SkInstallDiscardablePixelRef(
777 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm, NULL);
reed@google.combf790232013-12-13 19:45:58 +0000778 REPORTER_ASSERT(reporter, installSuccess);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000779
780 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
781 // Flattening original will follow the old path of performing an encode, while flattening bm
782 // will use the already encoded data.
783 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
784 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
785 REPORTER_ASSERT(reporter, picture1->equals(picture2));
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000786 // Now test that a parse error was generated when trying to create a new SkPicture without
787 // providing a function to decode the bitmap.
788 ErrorContext context;
789 context.fErrors = 0;
790 context.fReporter = reporter;
791 SkSetErrorCallback(assert_one_parse_error_cb, &context);
792 SkMemoryStream pictureStream(picture1);
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000793 SkClearLastError();
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000794 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL));
795 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000796 SkClearLastError();
797 SkSetErrorCallback(NULL, NULL);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000798}
799
junov@chromium.org94f20dc2013-01-28 21:04:44 +0000800static void test_clone_empty(skiatest::Reporter* reporter) {
801 // This is a regression test for crbug.com/172062
802 // Before the fix, we used to crash accessing a null pointer when we
803 // had a picture with no paints. This test passes by not crashing.
804 {
805 SkPicture picture;
806 picture.beginRecording(1, 1);
807 picture.endRecording();
808 SkPicture* destPicture = picture.clone();
809 REPORTER_ASSERT(reporter, NULL != destPicture);
810 destPicture->unref();
811 }
812 {
813 // Test without call to endRecording
814 SkPicture picture;
815 picture.beginRecording(1, 1);
816 SkPicture* destPicture = picture.clone();
817 REPORTER_ASSERT(reporter, NULL != destPicture);
818 destPicture->unref();
819 }
820}
821
junov@chromium.orgd575eed2013-05-08 15:39:13 +0000822static void test_clip_bound_opt(skiatest::Reporter* reporter) {
823 // Test for crbug.com/229011
824 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
825 SkIntToScalar(2), SkIntToScalar(2));
826 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
827 SkIntToScalar(1), SkIntToScalar(1));
828 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
829 SkIntToScalar(1), SkIntToScalar(1));
830
831 SkPath invPath;
832 invPath.addOval(rect1);
833 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
834 SkPath path;
835 path.addOval(rect2);
836 SkPath path2;
837 path2.addOval(rect3);
838 SkIRect clipBounds;
839 // Minimalist test set for 100% code coverage of
840 // SkPictureRecord::updateClipConservativelyUsingBounds
841 {
842 SkPicture picture;
843 SkCanvas* canvas = picture.beginRecording(10, 10,
844 SkPicture::kUsePathBoundsForClip_RecordingFlag);
845 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
846 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
847 REPORTER_ASSERT(reporter, true == nonEmpty);
848 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
849 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
850 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
851 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
852 }
853 {
854 SkPicture picture;
855 SkCanvas* canvas = picture.beginRecording(10, 10,
856 SkPicture::kUsePathBoundsForClip_RecordingFlag);
857 canvas->clipPath(path, SkRegion::kIntersect_Op);
858 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
859 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
860 REPORTER_ASSERT(reporter, true == nonEmpty);
861 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
862 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
863 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
864 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
865 }
866 {
867 SkPicture picture;
868 SkCanvas* canvas = picture.beginRecording(10, 10,
869 SkPicture::kUsePathBoundsForClip_RecordingFlag);
870 canvas->clipPath(path, SkRegion::kIntersect_Op);
871 canvas->clipPath(invPath, SkRegion::kUnion_Op);
872 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
873 REPORTER_ASSERT(reporter, true == nonEmpty);
874 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
875 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
876 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
877 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
878 }
879 {
880 SkPicture picture;
881 SkCanvas* canvas = picture.beginRecording(10, 10,
882 SkPicture::kUsePathBoundsForClip_RecordingFlag);
883 canvas->clipPath(path, SkRegion::kDifference_Op);
884 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
885 REPORTER_ASSERT(reporter, true == nonEmpty);
886 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
887 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
888 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
889 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
890 }
891 {
892 SkPicture picture;
893 SkCanvas* canvas = picture.beginRecording(10, 10,
894 SkPicture::kUsePathBoundsForClip_RecordingFlag);
895 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
896 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
897 // True clip is actually empty in this case, but the best
898 // determination we can make using only bounds as input is that the
899 // clip is included in the bounds of 'path'.
900 REPORTER_ASSERT(reporter, true == nonEmpty);
901 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
902 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
903 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
904 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
905 }
906 {
907 SkPicture picture;
908 SkCanvas* canvas = picture.beginRecording(10, 10,
909 SkPicture::kUsePathBoundsForClip_RecordingFlag);
910 canvas->clipPath(path, SkRegion::kIntersect_Op);
911 canvas->clipPath(path2, SkRegion::kXOR_Op);
912 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
913 REPORTER_ASSERT(reporter, true == nonEmpty);
914 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
915 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
916 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
917 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
918 }
919}
920
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +0000921/**
922 * A canvas that records the number of clip commands.
923 */
924class ClipCountingCanvas : public SkCanvas {
925public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000926 explicit ClipCountingCanvas(SkBaseDevice* device)
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +0000927 : SkCanvas(device)
928 , fClipCount(0){
929 }
930
931 virtual bool clipRect(const SkRect& r, SkRegion::Op op, bool doAA)
932 SK_OVERRIDE {
933 fClipCount += 1;
934 return this->INHERITED::clipRect(r, op, doAA);
935 }
936
937 virtual bool clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA)
938 SK_OVERRIDE {
939 fClipCount += 1;
940 return this->INHERITED::clipRRect(rrect, op, doAA);
941 }
942
943 virtual bool clipPath(const SkPath& path, SkRegion::Op op, bool doAA)
944 SK_OVERRIDE {
945 fClipCount += 1;
946 return this->INHERITED::clipPath(path, op, doAA);
947 }
948
949 unsigned getClipCount() const { return fClipCount; }
950
951private:
952 unsigned fClipCount;
953
954 typedef SkCanvas INHERITED;
955};
956
957static void test_clip_expansion(skiatest::Reporter* reporter) {
958 SkPicture picture;
959 SkCanvas* canvas = picture.beginRecording(10, 10, 0);
960
961 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
962 // The following expanding clip should not be skipped.
963 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
964 // Draw something so the optimizer doesn't just fold the world.
965 SkPaint p;
966 p.setColor(SK_ColorBLUE);
967 canvas->drawPaint(p);
968
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000969 SkBitmapDevice testDevice(SkBitmap::kNo_Config, 10, 10);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +0000970 ClipCountingCanvas testCanvas(&testDevice);
971 picture.draw(&testCanvas);
972
973 // Both clips should be present on playback.
974 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
975}
976
tomhudson@google.com381010e2013-10-24 11:12:47 +0000977static void test_hierarchical(skiatest::Reporter* reporter) {
978 SkBitmap bm;
979 make_bm(&bm, 10, 10, SK_ColorRED, true);
980
981 SkCanvas* canvas;
982
983 SkPicture childPlain;
984 childPlain.beginRecording(10, 10);
985 childPlain.endRecording();
986 REPORTER_ASSERT(reporter, !childPlain.willPlayBackBitmaps()); // 0
987
988 SkPicture childWithBitmap;
989 childWithBitmap.beginRecording(10, 10)->drawBitmap(bm, 0, 0);
990 childWithBitmap.endRecording();
991 REPORTER_ASSERT(reporter, childWithBitmap.willPlayBackBitmaps()); // 1
992
993 SkPicture parentPP;
994 canvas = parentPP.beginRecording(10, 10);
995 canvas->drawPicture(childPlain);
996 parentPP.endRecording();
997 REPORTER_ASSERT(reporter, !parentPP.willPlayBackBitmaps()); // 0
998
999 SkPicture parentPWB;
1000 canvas = parentPWB.beginRecording(10, 10);
1001 canvas->drawPicture(childWithBitmap);
1002 parentPWB.endRecording();
1003 REPORTER_ASSERT(reporter, parentPWB.willPlayBackBitmaps()); // 1
1004
1005 SkPicture parentWBP;
1006 canvas = parentWBP.beginRecording(10, 10);
1007 canvas->drawBitmap(bm, 0, 0);
1008 canvas->drawPicture(childPlain);
1009 parentWBP.endRecording();
1010 REPORTER_ASSERT(reporter, parentWBP.willPlayBackBitmaps()); // 1
1011
1012 SkPicture parentWBWB;
1013 canvas = parentWBWB.beginRecording(10, 10);
1014 canvas->drawBitmap(bm, 0, 0);
1015 canvas->drawPicture(childWithBitmap);
1016 parentWBWB.endRecording();
1017 REPORTER_ASSERT(reporter, parentWBWB.willPlayBackBitmaps()); // 2
1018}
1019
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00001020DEF_TEST(Picture, reporter) {
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001021#ifdef SK_DEBUG
1022 test_deleting_empty_playback();
1023 test_serializing_empty_picture();
scroggo@google.com4b90b112012-12-04 15:08:56 +00001024#else
1025 test_bad_bitmap();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001026#endif
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001027 test_peephole();
reed@google.comfe7b1ed2012-11-29 21:00:39 +00001028 test_gatherpixelrefs(reporter);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +00001029 test_gatherpixelrefsandrects(reporter);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001030 test_bitmap_with_encoded_data(reporter);
junov@chromium.org94f20dc2013-01-28 21:04:44 +00001031 test_clone_empty(reporter);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001032 test_clip_bound_opt(reporter);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001033 test_clip_expansion(reporter);
tomhudson@google.com381010e2013-10-24 11:12:47 +00001034 test_hierarchical(reporter);
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001035}