1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Test.h"
#include "SkCanvas.h"
#include "SkColorPriv.h"
#include "SkData.h"
#include "SkPaint.h"
#include "SkPicture.h"
#include "SkRandom.h"
#include "SkRRect.h"
#include "SkShader.h"
#include "SkStream.h"
#include "SkPictureUtils.h"
static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) {
bm->setConfig(SkBitmap::kARGB_8888_Config, w, h);
bm->allocPixels();
bm->eraseColor(color);
if (immutable) {
bm->setImmutable();
}
}
typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&, const SkPoint&);
static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm,
const SkPoint& pos) {
canvas->drawBitmap(bm, pos.fX, pos.fY, NULL);
}
static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm,
const SkPoint& pos) {
SkRect r = {
0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height())
};
r.offset(pos.fX, pos.fY);
canvas->drawBitmapRectToRect(bm, NULL, r, NULL);
}
static void drawshader_proc(SkCanvas* canvas, const SkBitmap& bm,
const SkPoint& pos) {
SkRect r = {
0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height())
};
r.offset(pos.fX, pos.fY);
SkShader* s = SkShader::CreateBitmapShader(bm,
SkShader::kClamp_TileMode,
SkShader::kClamp_TileMode);
SkPaint paint;
paint.setShader(s)->unref();
canvas->drawRect(r, paint);
canvas->drawOval(r, paint);
SkRRect rr;
rr.setRectXY(r, 10, 10);
canvas->drawRRect(rr, paint);
}
// Return a picture with the bitmaps drawn at the specified positions.
static SkPicture* record_bitmaps(const SkBitmap bm[], const SkPoint pos[],
int count, DrawBitmapProc proc) {
SkPicture* pic = new SkPicture;
SkCanvas* canvas = pic->beginRecording(1000, 1000);
for (int i = 0; i < count; ++i) {
proc(canvas, bm[i], pos[i]);
}
pic->endRecording();
return pic;
}
static void rand_rect(SkRect* rect, SkMWCRandom& rand, SkScalar W, SkScalar H) {
rect->fLeft = rand.nextRangeScalar(-W, 2*W);
rect->fTop = rand.nextRangeScalar(-H, 2*H);
rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W);
rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H);
// we integralize rect to make our tests more predictable, since Gather is
// a little sloppy.
SkIRect ir;
rect->round(&ir);
rect->set(ir);
}
// Allocate result to be large enough to hold subset, and then draw the picture
// into it, offsetting by subset's top/left corner.
static void draw(SkPicture* pic, const SkRect& subset, SkBitmap* result) {
SkIRect ir;
subset.roundOut(&ir);
int w = ir.width();
int h = ir.height();
make_bm(result, w, h, 0, false);
SkCanvas canvas(*result);
canvas.translate(-SkIntToScalar(ir.left()), -SkIntToScalar(ir.top()));
canvas.drawPicture(*pic);
}
template <typename T> int find_index(const T* array, T elem, int count) {
for (int i = 0; i < count; ++i) {
if (array[i] == elem) {
return i;
}
}
return -1;
}
// Return true if 'ref' is found in array[]
static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) {
return find_index<const SkPixelRef*>(array, ref, count) >= 0;
}
// Look at each pixel in bm, and if its color appears in colors[], find the
// corresponding value in refs[] and append that ref into array, skipping
// duplicates of the same value.
static void gather_from_colors(const SkBitmap& bm, SkPixelRef* const refs[],
int count, SkTDArray<SkPixelRef*>* array) {
// Since we only want to return unique values in array, when we scan we just
// set a bit for each index'd color found. In practice we only have a few
// distinct colors, so we just use an int's bits as our array. Hence the
// assert that count <= number-of-bits-in-our-int.
SkASSERT((unsigned)count <= 32);
uint32_t bitarray = 0;
SkAutoLockPixels alp(bm);
for (int y = 0; y < bm.height(); ++y) {
for (int x = 0; x < bm.width(); ++x) {
SkPMColor pmc = *bm.getAddr32(x, y);
// the only good case where the color is not found would be if
// the color is transparent, meaning no bitmap was drawn in that
// pixel.
if (pmc) {
uint32_t index = SkGetPackedR32(pmc);
SkASSERT(SkGetPackedG32(pmc) == index);
SkASSERT(SkGetPackedB32(pmc) == index);
SkASSERT(static_cast<int>(index) < count);
bitarray |= 1 << index;
}
}
}
for (int i = 0; i < count; ++i) {
if (bitarray & (1 << i)) {
*array->append() = refs[i];
}
}
}
static void test_gatherpixelrefs(skiatest::Reporter* reporter) {
const int IW = 8;
const int IH = IW;
const SkScalar W = SkIntToScalar(IW);
const SkScalar H = W;
static const int N = 4;
SkBitmap bm[N];
SkPixelRef* refs[N];
const SkPoint pos[] = {
{ 0, 0 }, { W, 0 }, { 0, H }, { W, H }
};
// Our convention is that the color components contain the index of their
// corresponding bitmap/pixelref
for (int i = 0; i < N; ++i) {
make_bm(&bm[i], IW, IH, SkColorSetARGB(0xFF, i, i, i), true);
refs[i] = bm[i].pixelRef();
}
static const DrawBitmapProc procs[] = {
drawbitmap_proc, drawbitmaprect_proc, drawshader_proc
};
SkMWCRandom rand;
for (size_t k = 0; k < SK_ARRAY_COUNT(procs); ++k) {
SkAutoTUnref<SkPicture> pic(record_bitmaps(bm, pos, N, procs[k]));
// quick check for a small piece of each quadrant, which should just
// contain 1 bitmap.
for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
SkRect r;
r.set(2, 2, W - 2, H - 2);
r.offset(pos[i].fX, pos[i].fY);
SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r));
REPORTER_ASSERT(reporter, data);
int count = data->size() / sizeof(SkPixelRef*);
REPORTER_ASSERT(reporter, 1 == count);
REPORTER_ASSERT(reporter, *(SkPixelRef**)data->data() == refs[i]);
}
// Test a bunch of random (mostly) rects, and compare the gather results
// with a deduced list of refs by looking at the colors drawn.
for (int j = 0; j < 100; ++j) {
SkRect r;
rand_rect(&r, rand, 2*W, 2*H);
SkBitmap result;
draw(pic, r, &result);
SkTDArray<SkPixelRef*> array;
SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
size_t dataSize = data ? data->size() : 0;
int gatherCount = dataSize / sizeof(SkPixelRef*);
SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize);
SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL;
SkAutoDataUnref adu(data);
gather_from_colors(result, refs, N, &array);
/*
* GatherPixelRefs is conservative, so it can return more bitmaps
* that we actually can see (usually because of conservative bounds
* inflation for antialiasing). Thus our check here is only that
* Gather didn't miss any that we actually saw. Even that isn't
* a strict requirement on Gather, which is meant to be quick and
* only mostly-correct, but at the moment this test should work.
*/
for (int i = 0; i < array.count(); ++i) {
bool found = find(gatherRefs, array[i], gatherCount);
REPORTER_ASSERT(reporter, found);
#if 0
// enable this block of code to debug failures, as it will rerun
// the case that failed.
if (!found) {
SkData* data = SkPictureUtils::GatherPixelRefs(pic, r);
size_t dataSize = data ? data->size() : 0;
}
#endif
}
}
}
}
#ifdef SK_DEBUG
// Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only
// run in debug mode.
static void test_deleting_empty_playback() {
SkPicture picture;
// Creates an SkPictureRecord
picture.beginRecording(0, 0);
// Turns that into an SkPicturePlayback
picture.endRecording();
// Deletes the old SkPicturePlayback, and creates a new SkPictureRecord
picture.beginRecording(0, 0);
}
// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
static void test_serializing_empty_picture() {
SkPicture picture;
picture.beginRecording(0, 0);
picture.endRecording();
SkDynamicMemoryWStream stream;
picture.serialize(&stream);
}
#endif
static void rand_op(SkCanvas* canvas, SkMWCRandom& rand) {
SkPaint paint;
SkRect rect = SkRect::MakeWH(50, 50);
SkScalar unit = rand.nextUScalar1();
if (unit <= 0.3) {
// SkDebugf("save\n");
canvas->save();
} else if (unit <= 0.6) {
// SkDebugf("restore\n");
canvas->restore();
} else if (unit <= 0.9) {
// SkDebugf("clip\n");
canvas->clipRect(rect);
} else {
// SkDebugf("draw\n");
canvas->drawPaint(paint);
}
}
static void test_peephole() {
SkMWCRandom rand;
for (int j = 0; j < 100; j++) {
SkMWCRandom rand2(rand); // remember the seed
SkPicture picture;
SkCanvas* canvas = picture.beginRecording(100, 100);
for (int i = 0; i < 1000; ++i) {
rand_op(canvas, rand);
}
picture.endRecording();
rand = rand2;
}
{
SkPicture picture;
SkCanvas* canvas = picture.beginRecording(100, 100);
SkRect rect = SkRect::MakeWH(50, 50);
for (int i = 0; i < 100; ++i) {
canvas->save();
}
while (canvas->getSaveCount() > 1) {
canvas->clipRect(rect);
canvas->restore();
}
picture.endRecording();
}
}
#ifndef SK_DEBUG
// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
// should never do this.
static void test_bad_bitmap() {
// This bitmap has a width and height but no pixels. As a result, attempting to record it will
// fail.
SkBitmap bm;
bm.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
SkPicture picture;
SkCanvas* recordingCanvas = picture.beginRecording(100, 100);
recordingCanvas->drawBitmap(bm, 0, 0);
picture.endRecording();
SkCanvas canvas;
canvas.drawPicture(picture);
}
#endif
#include "SkData.h"
#include "SkImageRef_GlobalPool.h"
// Class to test SkPixelRef::onRefEncodedData, since there are currently no implementations in skia.
class SkDataImageRef : public SkImageRef_GlobalPool {
public:
SkDataImageRef(SkMemoryStream* stream)
: SkImageRef_GlobalPool(stream, SkBitmap::kNo_Config) {
SkASSERT(stream != NULL);
fData = stream->copyToData();
this->setImmutable();
}
~SkDataImageRef() {
fData->unref();
}
virtual SkData* onRefEncodedData() SK_OVERRIDE {
fData->ref();
return fData;
}
private:
SkData* fData;
};
#include "SkImageEncoder.h"
static bool PNGEncodeBitmapToStream(SkWStream* wStream, const SkBitmap& bm) {
return SkImageEncoder::EncodeStream(wStream, bm, SkImageEncoder::kPNG_Type, 100);
}
static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
SkPicture picture;
SkCanvas* canvas = picture.beginRecording(bitmap.width(), bitmap.height());
canvas->drawBitmap(bitmap, 0, 0);
SkDynamicMemoryWStream wStream;
picture.serialize(&wStream, &PNGEncodeBitmapToStream);
return wStream.copyToData();
}
static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) {
// Create a bitmap that will be encoded.
SkBitmap original;
make_bm(&original, 100, 100, SK_ColorBLUE, true);
SkDynamicMemoryWStream wStream;
if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) {
return;
}
SkAutoDataUnref data(wStream.copyToData());
SkMemoryStream memStream;
memStream.setData(data);
// Use the encoded bitmap as the data for an image ref.
SkBitmap bm;
SkAutoTUnref<SkDataImageRef> imageRef(SkNEW_ARGS(SkDataImageRef, (&memStream)));
imageRef->getInfo(&bm);
bm.setPixelRef(imageRef);
// Write both bitmaps to pictures, and ensure that the resulting data streams are the same.
// Flattening original will follow the old path of performing an encode, while flattening bm
// will use the already encoded data.
SkAutoDataUnref picture1(serialized_picture_from_bitmap(original));
SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm));
REPORTER_ASSERT(reporter, picture1->equals(picture2));
}
static void test_clone_empty(skiatest::Reporter* reporter) {
// This is a regression test for crbug.com/172062
// Before the fix, we used to crash accessing a null pointer when we
// had a picture with no paints. This test passes by not crashing.
{
SkPicture picture;
picture.beginRecording(1, 1);
picture.endRecording();
SkPicture* destPicture = picture.clone();
REPORTER_ASSERT(reporter, NULL != destPicture);
destPicture->unref();
}
{
// Test without call to endRecording
SkPicture picture;
picture.beginRecording(1, 1);
SkPicture* destPicture = picture.clone();
REPORTER_ASSERT(reporter, NULL != destPicture);
destPicture->unref();
}
}
static void TestPicture(skiatest::Reporter* reporter) {
#ifdef SK_DEBUG
test_deleting_empty_playback();
test_serializing_empty_picture();
#else
test_bad_bitmap();
#endif
test_peephole();
test_gatherpixelrefs(reporter);
test_bitmap_with_encoded_data(reporter);
test_clone_empty(reporter);
}
#include "TestClassDef.h"
DEFINE_TESTCLASS("Pictures", PictureTestClass, TestPicture)
|