blob: 5e152341ccee0812beaf9d0f6f56fd5f8ee7d581 [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"
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +000011#include "SkDashPathEffect.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000012#include "SkData.h"
reed@google.combf790232013-12-13 19:45:58 +000013#include "SkDecodingImageGenerator.h"
scroggo@google.com49ce11b2013-04-25 18:29:32 +000014#include "SkError.h"
halcanary@google.com3d50ea12014-01-02 13:15:13 +000015#include "SkImageEncoder.h"
16#include "SkImageGenerator.h"
reed@google.com21b519d2012-10-02 17:42:15 +000017#include "SkPaint.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000018#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000019#include "SkPictureRecorder.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000020#include "SkPictureUtils.h"
reed@google.com72aa79c2013-01-24 18:27:42 +000021#include "SkRRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000022#include "SkRandom.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"
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) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000304 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000305 SkCanvas* canvas = recorder.beginRecording(1000, 1000, NULL, 0);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000306 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 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000316 return recorder.endRecording();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000317}
318
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000319static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000320 rect->fLeft = rand.nextRangeScalar(-W, 2*W);
321 rect->fTop = rand.nextRangeScalar(-H, 2*H);
322 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W);
323 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H);
324
325 // we integralize rect to make our tests more predictable, since Gather is
326 // a little sloppy.
327 SkIRect ir;
328 rect->round(&ir);
329 rect->set(ir);
330}
331
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000332static void draw(SkPicture* pic, int width, int height, SkBitmap* result) {
333 make_bm(result, width, height, SK_ColorBLACK, false);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000334
335 SkCanvas canvas(*result);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000336 canvas.drawPicture(*pic);
337}
338
339template <typename T> int find_index(const T* array, T elem, int count) {
340 for (int i = 0; i < count; ++i) {
341 if (array[i] == elem) {
342 return i;
343 }
344 }
345 return -1;
346}
347
348// Return true if 'ref' is found in array[]
349static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) {
350 return find_index<const SkPixelRef*>(array, ref, count) >= 0;
351}
352
skia.committer@gmail.com856673a2014-01-10 07:08:11 +0000353// Look at each pixel that is inside 'subset', and if its color appears in
354// colors[], find the corresponding value in refs[] and append that ref into
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000355// array, skipping duplicates of the same value.
356// Note that gathering pixelRefs from rendered colors suffers from the problem
357// that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color)
358// isn't easy to reconstruct.
359static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[],
360 int count, SkTDArray<SkPixelRef*>* array,
361 const SkRect& subset) {
362 SkIRect ir;
363 subset.roundOut(&ir);
364
365 if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) {
366 return;
367 }
368
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000369 // Since we only want to return unique values in array, when we scan we just
370 // set a bit for each index'd color found. In practice we only have a few
371 // distinct colors, so we just use an int's bits as our array. Hence the
372 // assert that count <= number-of-bits-in-our-int.
373 SkASSERT((unsigned)count <= 32);
374 uint32_t bitarray = 0;
375
376 SkAutoLockPixels alp(bm);
377
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000378 for (int y = ir.fTop; y < ir.fBottom; ++y) {
379 for (int x = ir.fLeft; x < ir.fRight; ++x) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000380 SkPMColor pmc = *bm.getAddr32(x, y);
381 // the only good case where the color is not found would be if
382 // the color is transparent, meaning no bitmap was drawn in that
383 // pixel.
384 if (pmc) {
bsalomon@google.comc3d753e2013-01-08 17:24:44 +0000385 uint32_t index = SkGetPackedR32(pmc);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000386 SkASSERT(SkGetPackedG32(pmc) == index);
387 SkASSERT(SkGetPackedB32(pmc) == index);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000388 if (0 == index) {
389 continue; // background color
390 }
391 SkASSERT(0 == (index - gColorOffset) % gColorScale);
392 index = (index - gColorOffset) / gColorScale;
bsalomon@google.com5f429b02013-01-08 18:42:20 +0000393 SkASSERT(static_cast<int>(index) < count);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000394 bitarray |= 1 << index;
395 }
396 }
397 }
398
399 for (int i = 0; i < count; ++i) {
400 if (bitarray & (1 << i)) {
401 *array->append() = refs[i];
402 }
403 }
404}
405
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000406static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h,
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000407 const SkTDArray<SkPixelRef*> analytic[],
408 int count,
409 SkTDArray<SkPixelRef*>* result,
robertphillips@google.com0e4ce142014-01-13 13:46:45 +0000410 const SkRect& subset) {
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000411 for (int i = 0; i < count; ++i) {
412 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h);
413
414 if (SkRect::Intersects(subset, rect)) {
415 result->append(analytic[i].count(), analytic[i].begin());
416 }
417 }
418}
419
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000420
421static const struct {
422 const DrawBitmapProc proc;
423 const char* const desc;
424} gProcs[] = {
425 {drawpaint_proc, "drawpaint"},
426 {drawpoints_proc, "drawpoints"},
427 {drawrect_proc, "drawrect"},
428 {drawoval_proc, "drawoval"},
429 {drawrrect_proc, "drawrrect"},
430 {drawpath_proc, "drawpath"},
431 {drawbitmap_proc, "drawbitmap"},
432 {drawbitmap_withshader_proc, "drawbitmap_withshader"},
433 {drawsprite_proc, "drawsprite"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000434#if 0
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000435 {drawsprite_withshader_proc, "drawsprite_withshader"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000436#endif
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000437 {drawbitmaprect_proc, "drawbitmaprect"},
438 {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"},
439 {drawtext_proc, "drawtext"},
440 {drawpostext_proc, "drawpostext"},
441 {drawtextonpath_proc, "drawtextonpath"},
442 {drawverts_proc, "drawverts"},
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000443};
444
445static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000446 // Our convention is that the color components contain an encoding of
447 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000448 // reserved for the background
449 for (int i = 0; i < num; ++i) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000450 make_bm(&bm[i], w, h,
451 SkColorSetARGB(0xFF,
452 gColorScale*i+gColorOffset,
453 gColorScale*i+gColorOffset,
454 gColorScale*i+gColorOffset),
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000455 true);
456 refs[i] = bm[i].pixelRef();
457 }
458
459 // The A8 alternate bitmaps are all BW checkerboards
460 for (int i = 0; i < num; ++i) {
461 make_checkerboard(&bm[num+i], w, h, true);
462 refs[num+i] = bm[num+i].pixelRef();
463 }
464}
465
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000466static void test_gatherpixelrefs(skiatest::Reporter* reporter) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000467 const int IW = 32;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000468 const int IH = IW;
469 const SkScalar W = SkIntToScalar(IW);
470 const SkScalar H = W;
471
472 static const int N = 4;
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000473 SkBitmap bm[2*N];
474 SkPixelRef* refs[2*N];
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000475 SkTDArray<SkPixelRef*> analytic[N];
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000476
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000477 const SkPoint pos[N] = {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000478 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
479 };
480
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000481 create_textures(bm, refs, N, IW, IH);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000482
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000483 SkRandom rand;
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000484 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000485 SkAutoTUnref<SkPicture> pic(
486 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000487
tomhudson@google.com381010e2013-10-24 11:12:47 +0000488 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000489 // quick check for a small piece of each quadrant, which should just
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000490 // contain 1 or 2 bitmaps.
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000491 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
492 SkRect r;
493 r.set(2, 2, W - 2, H - 2);
494 r.offset(pos[i].fX, pos[i].fY);
495 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r));
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000496 if (!data) {
497 ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned "
498 "NULL for %s.", gProcs[k].desc);
499 continue;
500 }
501 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data();
502 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*));
503 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
504 if (1 == count) {
505 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
506 } else if (2 == count) {
507 REPORTER_ASSERT(reporter,
508 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
509 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
commit-bot@chromium.orgfe433c12013-07-09 16:04:32 +0000510 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000511 }
512
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000513 SkBitmap image;
514 draw(pic, 2*IW, 2*IH, &image);
515
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000516 // Test a bunch of random (mostly) rects, and compare the gather results
517 // with a deduced list of refs by looking at the colors drawn.
518 for (int j = 0; j < 100; ++j) {
519 SkRect r;
520 rand_rect(&r, rand, 2*W, 2*H);
521
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000522 SkTDArray<SkPixelRef*> fromImage;
523 gather_from_image(image, refs, N, &fromImage, r);
524
525 SkTDArray<SkPixelRef*> fromAnalytic;
526 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000527
528 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
529 size_t dataSize = data ? data->size() : 0;
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000530 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000531 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize);
532 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL;
533 SkAutoDataUnref adu(data);
534
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000535 // Everything that we saw drawn should appear in the analytic list
536 // but the analytic list may contain some pixelRefs that were not
537 // seen in the image (e.g., A8 textures used as masks)
538 for (int i = 0; i < fromImage.count(); ++i) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000539 if (-1 == fromAnalytic.find(fromImage[i])) {
540 ERRORF(reporter, "PixelRef missing %d %s",
541 i, gProcs[k].desc);
542 }
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000543 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000544
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000545 /*
546 * GatherPixelRefs is conservative, so it can return more bitmaps
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000547 * than are strictly required. Thus our check here is only that
548 * Gather didn't miss any that we actually needed. Even that isn't
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000549 * a strict requirement on Gather, which is meant to be quick and
550 * only mostly-correct, but at the moment this test should work.
551 */
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000552 for (int i = 0; i < fromAnalytic.count(); ++i) {
553 bool found = find(gatherRefs, fromAnalytic[i], gatherCount);
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000554 if (!found) {
555 ERRORF(reporter, "PixelRef missing %d %s",
556 i, gProcs[k].desc);
557 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000558#if 0
559 // enable this block of code to debug failures, as it will rerun
560 // the case that failed.
561 if (!found) {
562 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
563 size_t dataSize = data ? data->size() : 0;
564 }
565#endif
566 }
567 }
568 }
569}
570
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000571static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) {
572 const int IW = 32;
573 const int IH = IW;
574 const SkScalar W = SkIntToScalar(IW);
575 const SkScalar H = W;
576
577 static const int N = 4;
578 SkBitmap bm[2*N];
579 SkPixelRef* refs[2*N];
580 SkTDArray<SkPixelRef*> analytic[N];
581
582 const SkPoint pos[N] = {
583 { 0, 0 }, { W, 0 }, { 0, H }, { W, H }
584 };
585
586 create_textures(bm, refs, N, IW, IH);
587
588 SkRandom rand;
589 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) {
commit-bot@chromium.org5bee0542014-03-05 20:59:45 +0000590 SkAutoTUnref<SkPicture> pic(
591 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc));
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000592
593 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0);
594
595 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont(
596 new SkPictureUtils::SkPixelRefsAndRectsList);
597
598 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont);
599
600 // quick check for a small piece of each quadrant, which should just
601 // contain 1 or 2 bitmaps.
602 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
603 SkRect r;
604 r.set(2, 2, W - 2, H - 2);
605 r.offset(pos[i].fX, pos[i].fY);
606
607 SkTDArray<SkPixelRef*> gatheredRefs;
608 prCont->query(r, &gatheredRefs);
609
610 int count = gatheredRefs.count();
611 REPORTER_ASSERT(reporter, 1 == count || 2 == count);
612 if (1 == count) {
613 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]);
614 } else if (2 == count) {
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000615 REPORTER_ASSERT(reporter,
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000616 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) ||
617 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N]));
618 }
619 }
620
621 SkBitmap image;
622 draw(pic, 2*IW, 2*IH, &image);
623
624 // Test a bunch of random (mostly) rects, and compare the gather results
625 // with the analytic results and the pixel refs seen in a rendering.
626 for (int j = 0; j < 100; ++j) {
627 SkRect r;
628 rand_rect(&r, rand, 2*W, 2*H);
629
630 SkTDArray<SkPixelRef*> fromImage;
631 gather_from_image(image, refs, N, &fromImage, r);
632
633 SkTDArray<SkPixelRef*> fromAnalytic;
634 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r);
635
636 SkTDArray<SkPixelRef*> gatheredRefs;
637 prCont->query(r, &gatheredRefs);
638
639 // Everything that we saw drawn should appear in the analytic list
640 // but the analytic list may contain some pixelRefs that were not
641 // seen in the image (e.g., A8 textures used as masks)
642 for (int i = 0; i < fromImage.count(); ++i) {
643 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]));
644 }
645
646 // Everything in the analytic list should appear in the gathered
skia.committer@gmail.com2e9a7152014-01-14 07:01:42 +0000647 // list.
robertphillips@google.com56bf6e42014-01-13 13:33:26 +0000648 for (int i = 0; i < fromAnalytic.count(); ++i) {
649 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i]));
650 }
651 }
652 }
653}
654
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000655#ifdef SK_DEBUG
656// Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only
657// run in debug mode.
658static void test_deleting_empty_playback() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000659 SkPictureRecorder recorder;
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000660 // Creates an SkPictureRecord
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000661 recorder.beginRecording(0, 0, NULL, 0);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000662 // Turns that into an SkPicturePlayback
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000663 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000664 // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000665 recorder.beginRecording(0, 0, NULL, 0);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000666}
667
668// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
669static void test_serializing_empty_picture() {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000670 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000671 recorder.beginRecording(0, 0, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000672 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000673 SkDynamicMemoryWStream stream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000674 picture->serialize(&stream);
scroggo@google.comd614c6a2012-09-14 17:26:37 +0000675}
676#endif
677
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000678static void rand_op(SkCanvas* canvas, SkRandom& rand) {
reed@google.com21b519d2012-10-02 17:42:15 +0000679 SkPaint paint;
680 SkRect rect = SkRect::MakeWH(50, 50);
681
682 SkScalar unit = rand.nextUScalar1();
683 if (unit <= 0.3) {
684// SkDebugf("save\n");
685 canvas->save();
686 } else if (unit <= 0.6) {
687// SkDebugf("restore\n");
688 canvas->restore();
689 } else if (unit <= 0.9) {
690// SkDebugf("clip\n");
691 canvas->clipRect(rect);
692 } else {
693// SkDebugf("draw\n");
694 canvas->drawPaint(paint);
695 }
696}
697
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +0000698static void test_gpu_veto(skiatest::Reporter* reporter) {
699
700 SkPictureRecorder recorder;
701
702 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
703 {
704 SkPath path;
705 path.moveTo(0, 0);
706 path.lineTo(50, 50);
707
708 SkScalar intervals[] = { 1.0f, 1.0f };
709 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0));
710
711 SkPaint paint;
712 paint.setStyle(SkPaint::kStroke_Style);
713 paint.setPathEffect(dash);
714
715 canvas->drawPath(path, paint);
716 }
717 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
718 // path effects currently render an SkPicture undesireable for GPU rendering
719 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
720
721 canvas = recorder.beginRecording(100, 100, NULL, 0);
722 {
723 SkPath path;
724
725 path.moveTo(0, 0);
726 path.lineTo(0, 50);
727 path.lineTo(25, 25);
728 path.lineTo(50, 50);
729 path.lineTo(50, 0);
730 path.close();
731 REPORTER_ASSERT(reporter, !path.isConvex());
732
733 SkPaint paint;
734 paint.setAntiAlias(true);
735 for (int i = 0; i < 50; ++i) {
736 canvas->drawPath(path, paint);
737 }
738 }
739 picture.reset(recorder.endRecording());
740 // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering
741 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL));
742
743 canvas = recorder.beginRecording(100, 100, NULL, 0);
744 {
745 SkPath path;
746
747 path.moveTo(0, 0);
748 path.lineTo(0, 50);
749 path.lineTo(25, 25);
750 path.lineTo(50, 50);
751 path.lineTo(50, 0);
752 path.close();
753 REPORTER_ASSERT(reporter, !path.isConvex());
754
755 SkPaint paint;
756 paint.setAntiAlias(true);
757 paint.setStyle(SkPaint::kStroke_Style);
758 paint.setStrokeWidth(0);
759 for (int i = 0; i < 50; ++i) {
760 canvas->drawPath(path, paint);
761 }
762 }
763 picture.reset(recorder.endRecording());
764 // hairline stroked AA concave paths are fine for GPU rendering
765 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL));
766}
767
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000768static void set_canvas_to_save_count_4(SkCanvas* canvas) {
769 canvas->restoreToCount(1);
770 canvas->save();
771 canvas->save();
772 canvas->save();
773}
774
775static void test_unbalanced_save_restores(skiatest::Reporter* reporter) {
776 SkCanvas testCanvas(100, 100);
777 set_canvas_to_save_count_4(&testCanvas);
778
779 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
780
781 SkPaint paint;
782 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000);
783
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000784 SkPictureRecorder recorder;
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000785
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000786 {
787 // Create picture with 2 unbalanced saves
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000788 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000789 canvas->save();
790 canvas->translate(10, 10);
791 canvas->drawRect(rect, paint);
792 canvas->save();
793 canvas->translate(10, 10);
794 canvas->drawRect(rect, paint);
795 SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording());
796
797 testCanvas.drawPicture(*extraSavePicture);
798 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
799 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000800
801 set_canvas_to_save_count_4(&testCanvas);
802
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000803 {
804 // Create picture with 2 unbalanced restores
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000805 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000806 canvas->save();
807 canvas->translate(10, 10);
808 canvas->drawRect(rect, paint);
809 canvas->save();
810 canvas->translate(10, 10);
811 canvas->drawRect(rect, paint);
812 canvas->restore();
813 canvas->restore();
814 canvas->restore();
815 canvas->restore();
816 SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording());
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000817
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000818 testCanvas.drawPicture(*extraRestorePicture);
819 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
820 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000821
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000822 set_canvas_to_save_count_4(&testCanvas);
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000823
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000824 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000825 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000826 canvas->translate(10, 10);
827 canvas->drawRect(rect, paint);
828 SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording());
829
830 testCanvas.drawPicture(*noSavePicture);
831 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
832 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity());
833 }
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000834}
835
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000836static void test_peephole() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000837 SkRandom rand;
reed@google.com21b519d2012-10-02 17:42:15 +0000838
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000839 SkPictureRecorder recorder;
840
reed@google.com21b519d2012-10-02 17:42:15 +0000841 for (int j = 0; j < 100; j++) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000842 SkRandom rand2(rand); // remember the seed
reed@google.com21b519d2012-10-02 17:42:15 +0000843
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000844 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
reed@google.com21b519d2012-10-02 17:42:15 +0000845
846 for (int i = 0; i < 1000; ++i) {
847 rand_op(canvas, rand);
848 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000849 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
jvanverth@google.comc490f802013-03-04 13:56:38 +0000850
851 rand = rand2;
reed@google.com21b519d2012-10-02 17:42:15 +0000852 }
853
854 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000855 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
reed@google.com21b519d2012-10-02 17:42:15 +0000856 SkRect rect = SkRect::MakeWH(50, 50);
skia.committer@gmail.com52c24372012-10-03 02:01:13 +0000857
reed@google.com21b519d2012-10-02 17:42:15 +0000858 for (int i = 0; i < 100; ++i) {
859 canvas->save();
860 }
861 while (canvas->getSaveCount() > 1) {
862 canvas->clipRect(rect);
863 canvas->restore();
864 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000865 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
reed@google.com21b519d2012-10-02 17:42:15 +0000866 }
867}
868
scroggo@google.com4b90b112012-12-04 15:08:56 +0000869#ifndef SK_DEBUG
870// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
871// should never do this.
872static void test_bad_bitmap() {
873 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
874 // fail.
875 SkBitmap bm;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000876 bm.setConfig(SkImageInfo::MakeN32Premul(100, 100));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000877 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000878 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100, NULL, 0);
scroggo@google.com4b90b112012-12-04 15:08:56 +0000879 recordingCanvas->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000880 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
scroggo@google.com4b90b112012-12-04 15:08:56 +0000881
882 SkCanvas canvas;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000883 canvas.drawPicture(*picture);
scroggo@google.com4b90b112012-12-04 15:08:56 +0000884}
885#endif
886
reed@google.com672588b2014-01-08 15:42:01 +0000887static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000888 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000889}
890
891static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000892 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000893 SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height(), NULL, 0);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000894 canvas->drawBitmap(bitmap, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000895 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
896
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000897 SkDynamicMemoryWStream wStream;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000898 picture->serialize(&wStream, &encode_bitmap_to_data);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000899 return wStream.copyToData();
900}
901
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000902struct ErrorContext {
903 int fErrors;
904 skiatest::Reporter* fReporter;
905};
906
907static void assert_one_parse_error_cb(SkError error, void* context) {
908 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
909 errorContext->fErrors++;
910 // This test only expects one error, and that is a kParseError. If there are others,
911 // there is some unknown problem.
912 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors,
913 "This threw more errors than expected.");
914 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error,
915 SkGetLastErrorString());
916}
917
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000918static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
919 // Create a bitmap that will be encoded.
920 SkBitmap original;
921 make_bm(&original, 100, 100, SK_ColorBLUE, true);
922 SkDynamicMemoryWStream wStream;
923 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
924 return;
925 }
926 SkAutoDataUnref data(wStream.copyToData());
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000927
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000928 SkBitmap bm;
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000929 bool installSuccess = SkInstallDiscardablePixelRef(
930 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm, NULL);
reed@google.combf790232013-12-13 19:45:58 +0000931 REPORTER_ASSERT(reporter, installSuccess);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000932
933 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
934 // Flattening original will follow the old path of performing an encode, while flattening bm
935 // will use the already encoded data.
936 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
937 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
938 REPORTER_ASSERT(reporter, picture1->equals(picture2));
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000939 // Now test that a parse error was generated when trying to create a new SkPicture without
940 // providing a function to decode the bitmap.
941 ErrorContext context;
942 context.fErrors = 0;
943 context.fReporter = reporter;
944 SkSetErrorCallback(assert_one_parse_error_cb, &context);
945 SkMemoryStream pictureStream(picture1);
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000946 SkClearLastError();
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000947 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL));
948 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
scroggo@google.com49ce11b2013-04-25 18:29:32 +0000949 SkClearLastError();
950 SkSetErrorCallback(NULL, NULL);
scroggo@google.com7c9d5392012-12-10 15:40:55 +0000951}
952
junov@chromium.org94f20dc2013-01-28 21:04:44 +0000953static void test_clone_empty(skiatest::Reporter* reporter) {
954 // This is a regression test for crbug.com/172062
955 // Before the fix, we used to crash accessing a null pointer when we
956 // had a picture with no paints. This test passes by not crashing.
957 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000958 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000959 recorder.beginRecording(1, 1, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000960 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
961 SkAutoTUnref<SkPicture> destPicture(picture->clone());
junov@chromium.org94f20dc2013-01-28 21:04:44 +0000962 REPORTER_ASSERT(reporter, NULL != destPicture);
junov@chromium.org94f20dc2013-01-28 21:04:44 +0000963 }
964}
965
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000966static void test_draw_empty(skiatest::Reporter* reporter) {
967 SkBitmap result;
968 make_bm(&result, 2, 2, SK_ColorBLACK, false);
969
970 SkCanvas canvas(result);
971
972 {
973 // stock SkPicture
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000974 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000975 recorder.beginRecording(1, 1, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000976 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000977
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000978 canvas.drawPicture(*picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000979 }
980
981 {
982 // tile grid
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000983 SkTileGridFactory::TileGridInfo gridInfo;
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000984 gridInfo.fMargin.setEmpty();
985 gridInfo.fOffset.setZero();
986 gridInfo.fTileInterval.set(1, 1);
987
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000988 SkTileGridFactory factory(gridInfo);
989 SkPictureRecorder recorder;
990 recorder.beginRecording(1, 1, &factory, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000991 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000992
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000993 canvas.drawPicture(*picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +0000994 }
995
996 {
997 // RTree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000998 SkRTreeFactory factory;
999 SkPictureRecorder recorder;
1000 recorder.beginRecording(1, 1, &factory, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001001 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001002
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001003 canvas.drawPicture(*picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001004 }
1005
1006 {
1007 // quad tree
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001008 SkQuadTreeFactory factory;
1009 SkPictureRecorder recorder;
1010 recorder.beginRecording(1, 1, &factory, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001011 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001012
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001013 canvas.drawPicture(*picture);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001014 }
1015}
1016
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001017static void test_clip_bound_opt(skiatest::Reporter* reporter) {
1018 // Test for crbug.com/229011
1019 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
1020 SkIntToScalar(2), SkIntToScalar(2));
1021 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
1022 SkIntToScalar(1), SkIntToScalar(1));
1023 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
1024 SkIntToScalar(1), SkIntToScalar(1));
1025
1026 SkPath invPath;
1027 invPath.addOval(rect1);
1028 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
1029 SkPath path;
1030 path.addOval(rect2);
1031 SkPath path2;
1032 path2.addOval(rect3);
1033 SkIRect clipBounds;
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001034 SkPictureRecorder recorder;
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001035 // Minimalist test set for 100% code coverage of
1036 // SkPictureRecord::updateClipConservativelyUsingBounds
1037 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001038 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001039 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1040 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1041 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1042 REPORTER_ASSERT(reporter, true == nonEmpty);
1043 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1044 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1045 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1046 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1047 }
1048 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001049 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001050 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1051 canvas->clipPath(path, SkRegion::kIntersect_Op);
1052 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
1053 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1054 REPORTER_ASSERT(reporter, true == nonEmpty);
1055 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1056 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1057 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1058 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1059 }
1060 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001061 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001062 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1063 canvas->clipPath(path, SkRegion::kIntersect_Op);
1064 canvas->clipPath(invPath, SkRegion::kUnion_Op);
1065 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1066 REPORTER_ASSERT(reporter, true == nonEmpty);
1067 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1068 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1069 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1070 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1071 }
1072 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001073 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001074 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1075 canvas->clipPath(path, SkRegion::kDifference_Op);
1076 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1077 REPORTER_ASSERT(reporter, true == nonEmpty);
1078 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1079 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1080 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1081 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1082 }
1083 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001084 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001085 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1086 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
1087 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1088 // True clip is actually empty in this case, but the best
1089 // determination we can make using only bounds as input is that the
1090 // clip is included in the bounds of 'path'.
1091 REPORTER_ASSERT(reporter, true == nonEmpty);
1092 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1093 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1094 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1095 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1096 }
1097 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001098 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL,
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001099 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1100 canvas->clipPath(path, SkRegion::kIntersect_Op);
1101 canvas->clipPath(path2, SkRegion::kXOR_Op);
1102 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1103 REPORTER_ASSERT(reporter, true == nonEmpty);
1104 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
1105 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
1106 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1107 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1108 }
1109}
1110
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001111/**
1112 * A canvas that records the number of clip commands.
1113 */
1114class ClipCountingCanvas : public SkCanvas {
1115public:
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001116 explicit ClipCountingCanvas(int width, int height)
1117 : INHERITED(width, height)
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001118 , fClipCount(0){
1119 }
1120
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001121 virtual void onClipRect(const SkRect& r,
1122 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001123 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001124 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001125 this->INHERITED::onClipRect(r, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001126 }
1127
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001128 virtual void onClipRRect(const SkRRect& rrect,
1129 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001130 ClipEdgeStyle edgeStyle)SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001131 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001132 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001133 }
1134
skia.committer@gmail.com370a8992014-03-01 03:02:09 +00001135 virtual void onClipPath(const SkPath& path,
1136 SkRegion::Op op,
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001137 ClipEdgeStyle edgeStyle) SK_OVERRIDE {
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001138 fClipCount += 1;
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001139 this->INHERITED::onClipPath(path, op, edgeStyle);
1140 }
1141
1142 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE {
1143 fClipCount += 1;
1144 this->INHERITED::onClipRegion(deviceRgn, op);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001145 }
1146
1147 unsigned getClipCount() const { return fClipCount; }
1148
1149private:
1150 unsigned fClipCount;
1151
1152 typedef SkCanvas INHERITED;
1153};
1154
1155static void test_clip_expansion(skiatest::Reporter* reporter) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001156 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001157 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001158
1159 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
1160 // The following expanding clip should not be skipped.
1161 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
1162 // Draw something so the optimizer doesn't just fold the world.
1163 SkPaint p;
1164 p.setColor(SK_ColorBLUE);
1165 canvas->drawPaint(p);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001166 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001167
commit-bot@chromium.orge2543102014-01-31 19:42:58 +00001168 ClipCountingCanvas testCanvas(10, 10);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001169 picture->draw(&testCanvas);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001170
1171 // Both clips should be present on playback.
1172 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
1173}
1174
tomhudson@google.com381010e2013-10-24 11:12:47 +00001175static void test_hierarchical(skiatest::Reporter* reporter) {
1176 SkBitmap bm;
1177 make_bm(&bm, 10, 10, SK_ColorRED, true);
1178
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001179 SkPictureRecorder recorder;
tomhudson@google.com381010e2013-10-24 11:12:47 +00001180
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001181 recorder.beginRecording(10, 10, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001182 SkAutoTUnref<SkPicture> childPlain(recorder.endRecording());
1183 REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0
tomhudson@google.com381010e2013-10-24 11:12:47 +00001184
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001185 recorder.beginRecording(10, 10, NULL, 0)->drawBitmap(bm, 0, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001186 SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording());
1187 REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1
tomhudson@google.com381010e2013-10-24 11:12:47 +00001188
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001189 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001190 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001191 canvas->drawPicture(*childPlain);
1192 SkAutoTUnref<SkPicture> parentPP(recorder.endRecording());
1193 REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0
1194 }
1195 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001196 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001197 canvas->drawPicture(*childWithBitmap);
1198 SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording());
1199 REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1
1200 }
1201 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001202 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001203 canvas->drawBitmap(bm, 0, 0);
1204 canvas->drawPicture(*childPlain);
1205 SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording());
1206 REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1
1207 }
1208 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001209 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001210 canvas->drawBitmap(bm, 0, 0);
1211 canvas->drawPicture(*childWithBitmap);
1212 SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording());
1213 REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2
1214 }
tomhudson@google.com381010e2013-10-24 11:12:47 +00001215}
1216
robertphillips@google.comd5500882014-04-02 23:51:13 +00001217static void test_gen_id(skiatest::Reporter* reporter) {
1218
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001219 SkPicture empty;
robertphillips@google.comd5500882014-04-02 23:51:13 +00001220
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001221 // Empty pictures should still have a valid ID
1222 REPORTER_ASSERT(reporter, empty.uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001223
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001224 SkPictureRecorder recorder;
robertphillips@google.comd5500882014-04-02 23:51:13 +00001225
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001226 SkCanvas* canvas = recorder.beginRecording(1, 1, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001227 canvas->drawARGB(255, 255, 255, 255);
1228 SkAutoTUnref<SkPicture> hasData(recorder.endRecording());
1229 // picture should have a non-zero id after recording
1230 REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001231
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001232 // both pictures should have different ids
1233 REPORTER_ASSERT(reporter, hasData->uniqueID() != empty.uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001234
1235 // test out copy constructor
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001236 SkPicture copyWithData(*hasData);
1237 REPORTER_ASSERT(reporter, hasData->uniqueID() == copyWithData.uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001238
1239 SkPicture emptyCopy(empty);
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +00001240 REPORTER_ASSERT(reporter, empty.uniqueID() != emptyCopy.uniqueID());
skia.committer@gmail.coma9157722014-04-03 03:04:26 +00001241
robertphillips@google.comd5500882014-04-02 23:51:13 +00001242 // test out swap
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001243 {
1244 SkPicture swapWithData;
1245 uint32_t beforeID1 = swapWithData.uniqueID();
1246 uint32_t beforeID2 = copyWithData.uniqueID();
1247 swapWithData.swap(copyWithData);
1248 REPORTER_ASSERT(reporter, copyWithData.uniqueID() == beforeID1);
1249 REPORTER_ASSERT(reporter, swapWithData.uniqueID() == beforeID2);
1250 }
robertphillips@google.comd5500882014-04-02 23:51:13 +00001251
1252 // test out clone
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001253 {
1254 SkAutoTUnref<SkPicture> cloneWithData(hasData->clone());
1255 REPORTER_ASSERT(reporter, hasData->uniqueID() == cloneWithData->uniqueID());
robertphillips@google.comd5500882014-04-02 23:51:13 +00001256
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001257 SkAutoTUnref<SkPicture> emptyClone(empty.clone());
1258 REPORTER_ASSERT(reporter, empty.uniqueID() != emptyClone->uniqueID());
1259 }
robertphillips@google.comd5500882014-04-02 23:51:13 +00001260}
1261
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00001262DEF_TEST(Picture, reporter) {
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001263#ifdef SK_DEBUG
1264 test_deleting_empty_playback();
1265 test_serializing_empty_picture();
scroggo@google.com4b90b112012-12-04 15:08:56 +00001266#else
1267 test_bad_bitmap();
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001268#endif
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +00001269 test_unbalanced_save_restores(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001270 test_peephole();
commit-bot@chromium.orge2cb12a2014-04-24 21:53:13 +00001271 test_gpu_veto(reporter);
reed@google.comfe7b1ed2012-11-29 21:00:39 +00001272 test_gatherpixelrefs(reporter);
robertphillips@google.com56bf6e42014-01-13 13:33:26 +00001273 test_gatherpixelrefsandrects(reporter);
scroggo@google.com7c9d5392012-12-10 15:40:55 +00001274 test_bitmap_with_encoded_data(reporter);
junov@chromium.org94f20dc2013-01-28 21:04:44 +00001275 test_clone_empty(reporter);
commit-bot@chromium.org70512af2014-03-18 17:45:32 +00001276 test_draw_empty(reporter);
junov@chromium.orgd575eed2013-05-08 15:39:13 +00001277 test_clip_bound_opt(reporter);
fmalita@google.comd0f1a4f2013-08-27 15:50:19 +00001278 test_clip_expansion(reporter);
tomhudson@google.com381010e2013-10-24 11:12:47 +00001279 test_hierarchical(reporter);
robertphillips@google.comd5500882014-04-02 23:51:13 +00001280 test_gen_id(reporter);
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001281}
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001282
1283static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) {
1284 const SkPaint paint;
1285 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f };
1286 const SkIRect irect = { 2, 2, 3, 3 };
1287
1288 // Don't care what these record, as long as they're legal.
1289 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint);
1290 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag);
1291 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint);
1292 canvas->drawBitmapNine(bitmap, irect, rect, &paint);
1293 canvas->drawSprite(bitmap, 1, 1);
1294}
1295
1296static void test_draw_bitmaps(SkCanvas* canvas) {
1297 SkBitmap empty;
1298 draw_bitmaps(empty, canvas);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001299 empty.setConfig(SkImageInfo::MakeN32Premul(10, 10));
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001300 draw_bitmaps(empty, canvas);
1301}
1302
1303DEF_TEST(Picture_EmptyBitmap, r) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001304 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001305 test_draw_bitmaps(recorder.beginRecording(10, 10, NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001306 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001307}
1308
1309DEF_TEST(Canvas_EmptyBitmap, r) {
1310 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +00001311 dst.allocN32Pixels(10, 10);
commit-bot@chromium.org50b393a2014-02-10 18:29:10 +00001312 SkCanvas canvas(dst);
1313
1314 test_draw_bitmaps(&canvas);
1315}