scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 7 | #include "Test.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
| 11 | #include "SkData.h" |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 12 | #include "SkError.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 13 | #include "SkPaint.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 14 | #include "SkPicture.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 15 | #include "SkPictureUtils.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 16 | #include "SkRandom.h" |
reed@google.com | 72aa79c | 2013-01-24 18:27:42 +0000 | [diff] [blame] | 17 | #include "SkRRect.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 18 | #include "SkShader.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 19 | #include "SkStream.h" |
| 20 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 21 | |
| 22 | static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) { |
| 23 | bm->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 24 | bm->allocPixels(); |
| 25 | bm->eraseColor(color); |
| 26 | if (immutable) { |
| 27 | bm->setImmutable(); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&, const SkPoint&); |
| 32 | |
| 33 | static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm, |
| 34 | const SkPoint& pos) { |
| 35 | canvas->drawBitmap(bm, pos.fX, pos.fY, NULL); |
| 36 | } |
| 37 | |
| 38 | static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm, |
| 39 | const SkPoint& pos) { |
| 40 | SkRect r = { |
| 41 | 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) |
| 42 | }; |
| 43 | r.offset(pos.fX, pos.fY); |
| 44 | canvas->drawBitmapRectToRect(bm, NULL, r, NULL); |
| 45 | } |
| 46 | |
| 47 | static void drawshader_proc(SkCanvas* canvas, const SkBitmap& bm, |
| 48 | const SkPoint& pos) { |
| 49 | SkRect r = { |
| 50 | 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) |
| 51 | }; |
| 52 | r.offset(pos.fX, pos.fY); |
| 53 | |
| 54 | SkShader* s = SkShader::CreateBitmapShader(bm, |
| 55 | SkShader::kClamp_TileMode, |
| 56 | SkShader::kClamp_TileMode); |
| 57 | SkPaint paint; |
| 58 | paint.setShader(s)->unref(); |
| 59 | canvas->drawRect(r, paint); |
reed@google.com | 72aa79c | 2013-01-24 18:27:42 +0000 | [diff] [blame] | 60 | canvas->drawOval(r, paint); |
| 61 | SkRRect rr; |
| 62 | rr.setRectXY(r, 10, 10); |
| 63 | canvas->drawRRect(rr, paint); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Return a picture with the bitmaps drawn at the specified positions. |
| 67 | static SkPicture* record_bitmaps(const SkBitmap bm[], const SkPoint pos[], |
| 68 | int count, DrawBitmapProc proc) { |
| 69 | SkPicture* pic = new SkPicture; |
| 70 | SkCanvas* canvas = pic->beginRecording(1000, 1000); |
| 71 | for (int i = 0; i < count; ++i) { |
| 72 | proc(canvas, bm[i], pos[i]); |
| 73 | } |
| 74 | pic->endRecording(); |
| 75 | return pic; |
| 76 | } |
| 77 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 78 | static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 79 | rect->fLeft = rand.nextRangeScalar(-W, 2*W); |
| 80 | rect->fTop = rand.nextRangeScalar(-H, 2*H); |
| 81 | rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W); |
| 82 | rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H); |
| 83 | |
| 84 | // we integralize rect to make our tests more predictable, since Gather is |
| 85 | // a little sloppy. |
| 86 | SkIRect ir; |
| 87 | rect->round(&ir); |
| 88 | rect->set(ir); |
| 89 | } |
| 90 | |
| 91 | // Allocate result to be large enough to hold subset, and then draw the picture |
| 92 | // into it, offsetting by subset's top/left corner. |
| 93 | static void draw(SkPicture* pic, const SkRect& subset, SkBitmap* result) { |
| 94 | SkIRect ir; |
| 95 | subset.roundOut(&ir); |
| 96 | int w = ir.width(); |
| 97 | int h = ir.height(); |
| 98 | make_bm(result, w, h, 0, false); |
| 99 | |
| 100 | SkCanvas canvas(*result); |
| 101 | canvas.translate(-SkIntToScalar(ir.left()), -SkIntToScalar(ir.top())); |
| 102 | canvas.drawPicture(*pic); |
| 103 | } |
| 104 | |
| 105 | template <typename T> int find_index(const T* array, T elem, int count) { |
| 106 | for (int i = 0; i < count; ++i) { |
| 107 | if (array[i] == elem) { |
| 108 | return i; |
| 109 | } |
| 110 | } |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | // Return true if 'ref' is found in array[] |
| 115 | static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) { |
| 116 | return find_index<const SkPixelRef*>(array, ref, count) >= 0; |
| 117 | } |
| 118 | |
| 119 | // Look at each pixel in bm, and if its color appears in colors[], find the |
| 120 | // corresponding value in refs[] and append that ref into array, skipping |
| 121 | // duplicates of the same value. |
| 122 | static void gather_from_colors(const SkBitmap& bm, SkPixelRef* const refs[], |
| 123 | int count, SkTDArray<SkPixelRef*>* array) { |
| 124 | // Since we only want to return unique values in array, when we scan we just |
| 125 | // set a bit for each index'd color found. In practice we only have a few |
| 126 | // distinct colors, so we just use an int's bits as our array. Hence the |
| 127 | // assert that count <= number-of-bits-in-our-int. |
| 128 | SkASSERT((unsigned)count <= 32); |
| 129 | uint32_t bitarray = 0; |
| 130 | |
| 131 | SkAutoLockPixels alp(bm); |
| 132 | |
| 133 | for (int y = 0; y < bm.height(); ++y) { |
| 134 | for (int x = 0; x < bm.width(); ++x) { |
| 135 | SkPMColor pmc = *bm.getAddr32(x, y); |
| 136 | // the only good case where the color is not found would be if |
| 137 | // the color is transparent, meaning no bitmap was drawn in that |
| 138 | // pixel. |
| 139 | if (pmc) { |
bsalomon@google.com | c3d753e | 2013-01-08 17:24:44 +0000 | [diff] [blame] | 140 | uint32_t index = SkGetPackedR32(pmc); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 141 | SkASSERT(SkGetPackedG32(pmc) == index); |
| 142 | SkASSERT(SkGetPackedB32(pmc) == index); |
bsalomon@google.com | 5f429b0 | 2013-01-08 18:42:20 +0000 | [diff] [blame] | 143 | SkASSERT(static_cast<int>(index) < count); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 144 | bitarray |= 1 << index; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | for (int i = 0; i < count; ++i) { |
| 150 | if (bitarray & (1 << i)) { |
| 151 | *array->append() = refs[i]; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static void test_gatherpixelrefs(skiatest::Reporter* reporter) { |
| 157 | const int IW = 8; |
| 158 | const int IH = IW; |
| 159 | const SkScalar W = SkIntToScalar(IW); |
| 160 | const SkScalar H = W; |
| 161 | |
| 162 | static const int N = 4; |
| 163 | SkBitmap bm[N]; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 164 | SkPixelRef* refs[N]; |
| 165 | |
| 166 | const SkPoint pos[] = { |
| 167 | { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
| 168 | }; |
| 169 | |
| 170 | // Our convention is that the color components contain the index of their |
| 171 | // corresponding bitmap/pixelref |
| 172 | for (int i = 0; i < N; ++i) { |
| 173 | make_bm(&bm[i], IW, IH, SkColorSetARGB(0xFF, i, i, i), true); |
| 174 | refs[i] = bm[i].pixelRef(); |
| 175 | } |
| 176 | |
| 177 | static const DrawBitmapProc procs[] = { |
| 178 | drawbitmap_proc, drawbitmaprect_proc, drawshader_proc |
| 179 | }; |
| 180 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 181 | SkRandom rand; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 182 | for (size_t k = 0; k < SK_ARRAY_COUNT(procs); ++k) { |
| 183 | SkAutoTUnref<SkPicture> pic(record_bitmaps(bm, pos, N, procs[k])); |
| 184 | |
| 185 | // quick check for a small piece of each quadrant, which should just |
| 186 | // contain 1 bitmap. |
| 187 | for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
| 188 | SkRect r; |
| 189 | r.set(2, 2, W - 2, H - 2); |
| 190 | r.offset(pos[i].fX, pos[i].fY); |
| 191 | SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r)); |
| 192 | REPORTER_ASSERT(reporter, data); |
commit-bot@chromium.org | fe433c1 | 2013-07-09 16:04:32 +0000 | [diff] [blame] | 193 | if (data) { |
| 194 | int count = data->size() / sizeof(SkPixelRef*); |
| 195 | REPORTER_ASSERT(reporter, 1 == count); |
| 196 | REPORTER_ASSERT(reporter, *(SkPixelRef**)data->data() == refs[i]); |
| 197 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // Test a bunch of random (mostly) rects, and compare the gather results |
| 201 | // with a deduced list of refs by looking at the colors drawn. |
| 202 | for (int j = 0; j < 100; ++j) { |
| 203 | SkRect r; |
| 204 | rand_rect(&r, rand, 2*W, 2*H); |
| 205 | |
| 206 | SkBitmap result; |
| 207 | draw(pic, r, &result); |
| 208 | SkTDArray<SkPixelRef*> array; |
| 209 | |
| 210 | SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
| 211 | size_t dataSize = data ? data->size() : 0; |
| 212 | int gatherCount = dataSize / sizeof(SkPixelRef*); |
| 213 | SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize); |
| 214 | SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL; |
| 215 | SkAutoDataUnref adu(data); |
| 216 | |
| 217 | gather_from_colors(result, refs, N, &array); |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 218 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 219 | /* |
| 220 | * GatherPixelRefs is conservative, so it can return more bitmaps |
| 221 | * that we actually can see (usually because of conservative bounds |
| 222 | * inflation for antialiasing). Thus our check here is only that |
| 223 | * Gather didn't miss any that we actually saw. Even that isn't |
| 224 | * a strict requirement on Gather, which is meant to be quick and |
| 225 | * only mostly-correct, but at the moment this test should work. |
| 226 | */ |
| 227 | for (int i = 0; i < array.count(); ++i) { |
| 228 | bool found = find(gatherRefs, array[i], gatherCount); |
| 229 | REPORTER_ASSERT(reporter, found); |
| 230 | #if 0 |
| 231 | // enable this block of code to debug failures, as it will rerun |
| 232 | // the case that failed. |
| 233 | if (!found) { |
| 234 | SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
| 235 | size_t dataSize = data ? data->size() : 0; |
| 236 | } |
| 237 | #endif |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 243 | #ifdef SK_DEBUG |
| 244 | // Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only |
| 245 | // run in debug mode. |
| 246 | static void test_deleting_empty_playback() { |
| 247 | SkPicture picture; |
| 248 | // Creates an SkPictureRecord |
| 249 | picture.beginRecording(0, 0); |
| 250 | // Turns that into an SkPicturePlayback |
| 251 | picture.endRecording(); |
| 252 | // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord |
| 253 | picture.beginRecording(0, 0); |
| 254 | } |
| 255 | |
| 256 | // Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. |
| 257 | static void test_serializing_empty_picture() { |
| 258 | SkPicture picture; |
| 259 | picture.beginRecording(0, 0); |
| 260 | picture.endRecording(); |
| 261 | SkDynamicMemoryWStream stream; |
| 262 | picture.serialize(&stream); |
| 263 | } |
| 264 | #endif |
| 265 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 266 | static void rand_op(SkCanvas* canvas, SkRandom& rand) { |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 267 | SkPaint paint; |
| 268 | SkRect rect = SkRect::MakeWH(50, 50); |
| 269 | |
| 270 | SkScalar unit = rand.nextUScalar1(); |
| 271 | if (unit <= 0.3) { |
| 272 | // SkDebugf("save\n"); |
| 273 | canvas->save(); |
| 274 | } else if (unit <= 0.6) { |
| 275 | // SkDebugf("restore\n"); |
| 276 | canvas->restore(); |
| 277 | } else if (unit <= 0.9) { |
| 278 | // SkDebugf("clip\n"); |
| 279 | canvas->clipRect(rect); |
| 280 | } else { |
| 281 | // SkDebugf("draw\n"); |
| 282 | canvas->drawPaint(paint); |
| 283 | } |
| 284 | } |
| 285 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 286 | static void test_peephole() { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 287 | SkRandom rand; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 288 | |
| 289 | for (int j = 0; j < 100; j++) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 290 | SkRandom rand2(rand); // remember the seed |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 291 | |
| 292 | SkPicture picture; |
| 293 | SkCanvas* canvas = picture.beginRecording(100, 100); |
| 294 | |
| 295 | for (int i = 0; i < 1000; ++i) { |
| 296 | rand_op(canvas, rand); |
| 297 | } |
| 298 | picture.endRecording(); |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 299 | |
| 300 | rand = rand2; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | { |
| 304 | SkPicture picture; |
| 305 | SkCanvas* canvas = picture.beginRecording(100, 100); |
| 306 | SkRect rect = SkRect::MakeWH(50, 50); |
skia.committer@gmail.com | 52c2437 | 2012-10-03 02:01:13 +0000 | [diff] [blame] | 307 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 308 | for (int i = 0; i < 100; ++i) { |
| 309 | canvas->save(); |
| 310 | } |
| 311 | while (canvas->getSaveCount() > 1) { |
| 312 | canvas->clipRect(rect); |
| 313 | canvas->restore(); |
| 314 | } |
| 315 | picture.endRecording(); |
| 316 | } |
| 317 | } |
| 318 | |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 319 | #ifndef SK_DEBUG |
| 320 | // Only test this is in release mode. We deliberately crash in debug mode, since a valid caller |
| 321 | // should never do this. |
| 322 | static void test_bad_bitmap() { |
| 323 | // This bitmap has a width and height but no pixels. As a result, attempting to record it will |
| 324 | // fail. |
| 325 | SkBitmap bm; |
| 326 | bm.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 327 | SkPicture picture; |
| 328 | SkCanvas* recordingCanvas = picture.beginRecording(100, 100); |
| 329 | recordingCanvas->drawBitmap(bm, 0, 0); |
| 330 | picture.endRecording(); |
| 331 | |
| 332 | SkCanvas canvas; |
| 333 | canvas.drawPicture(picture); |
| 334 | } |
| 335 | #endif |
| 336 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 337 | #include "SkData.h" |
| 338 | #include "SkImageRef_GlobalPool.h" |
| 339 | // Class to test SkPixelRef::onRefEncodedData, since there are currently no implementations in skia. |
| 340 | class SkDataImageRef : public SkImageRef_GlobalPool { |
| 341 | |
| 342 | public: |
| 343 | SkDataImageRef(SkMemoryStream* stream) |
| 344 | : SkImageRef_GlobalPool(stream, SkBitmap::kNo_Config) { |
| 345 | SkASSERT(stream != NULL); |
| 346 | fData = stream->copyToData(); |
| 347 | this->setImmutable(); |
| 348 | } |
| 349 | |
| 350 | ~SkDataImageRef() { |
| 351 | fData->unref(); |
| 352 | } |
| 353 | |
| 354 | virtual SkData* onRefEncodedData() SK_OVERRIDE { |
| 355 | fData->ref(); |
| 356 | return fData; |
| 357 | } |
| 358 | |
| 359 | private: |
| 360 | SkData* fData; |
| 361 | }; |
| 362 | |
| 363 | #include "SkImageEncoder.h" |
| 364 | |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 365 | static SkData* encode_bitmap_to_data(size_t* offset, const SkBitmap& bm) { |
| 366 | *offset = 0; |
| 367 | return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { |
| 371 | SkPicture picture; |
| 372 | SkCanvas* canvas = picture.beginRecording(bitmap.width(), bitmap.height()); |
| 373 | canvas->drawBitmap(bitmap, 0, 0); |
| 374 | SkDynamicMemoryWStream wStream; |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 375 | picture.serialize(&wStream, &encode_bitmap_to_data); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 376 | return wStream.copyToData(); |
| 377 | } |
| 378 | |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 379 | struct ErrorContext { |
| 380 | int fErrors; |
| 381 | skiatest::Reporter* fReporter; |
| 382 | }; |
| 383 | |
| 384 | static void assert_one_parse_error_cb(SkError error, void* context) { |
| 385 | ErrorContext* errorContext = static_cast<ErrorContext*>(context); |
| 386 | errorContext->fErrors++; |
| 387 | // This test only expects one error, and that is a kParseError. If there are others, |
| 388 | // there is some unknown problem. |
| 389 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors, |
| 390 | "This threw more errors than expected."); |
| 391 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error, |
| 392 | SkGetLastErrorString()); |
| 393 | } |
| 394 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 395 | static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { |
| 396 | // Create a bitmap that will be encoded. |
| 397 | SkBitmap original; |
| 398 | make_bm(&original, 100, 100, SK_ColorBLUE, true); |
| 399 | SkDynamicMemoryWStream wStream; |
| 400 | if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { |
| 401 | return; |
| 402 | } |
| 403 | SkAutoDataUnref data(wStream.copyToData()); |
| 404 | SkMemoryStream memStream; |
| 405 | memStream.setData(data); |
| 406 | |
| 407 | // Use the encoded bitmap as the data for an image ref. |
| 408 | SkBitmap bm; |
| 409 | SkAutoTUnref<SkDataImageRef> imageRef(SkNEW_ARGS(SkDataImageRef, (&memStream))); |
| 410 | imageRef->getInfo(&bm); |
| 411 | bm.setPixelRef(imageRef); |
| 412 | |
| 413 | // Write both bitmaps to pictures, and ensure that the resulting data streams are the same. |
| 414 | // Flattening original will follow the old path of performing an encode, while flattening bm |
| 415 | // will use the already encoded data. |
| 416 | SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); |
| 417 | SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); |
| 418 | REPORTER_ASSERT(reporter, picture1->equals(picture2)); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 419 | // Now test that a parse error was generated when trying to create a new SkPicture without |
| 420 | // providing a function to decode the bitmap. |
| 421 | ErrorContext context; |
| 422 | context.fErrors = 0; |
| 423 | context.fReporter = reporter; |
| 424 | SkSetErrorCallback(assert_one_parse_error_cb, &context); |
| 425 | SkMemoryStream pictureStream(picture1); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 426 | SkClearLastError(); |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 427 | SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL)); |
| 428 | REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 429 | SkClearLastError(); |
| 430 | SkSetErrorCallback(NULL, NULL); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 431 | } |
| 432 | |
junov@chromium.org | 94f20dc | 2013-01-28 21:04:44 +0000 | [diff] [blame] | 433 | static void test_clone_empty(skiatest::Reporter* reporter) { |
| 434 | // This is a regression test for crbug.com/172062 |
| 435 | // Before the fix, we used to crash accessing a null pointer when we |
| 436 | // had a picture with no paints. This test passes by not crashing. |
| 437 | { |
| 438 | SkPicture picture; |
| 439 | picture.beginRecording(1, 1); |
| 440 | picture.endRecording(); |
| 441 | SkPicture* destPicture = picture.clone(); |
| 442 | REPORTER_ASSERT(reporter, NULL != destPicture); |
| 443 | destPicture->unref(); |
| 444 | } |
| 445 | { |
| 446 | // Test without call to endRecording |
| 447 | SkPicture picture; |
| 448 | picture.beginRecording(1, 1); |
| 449 | SkPicture* destPicture = picture.clone(); |
| 450 | REPORTER_ASSERT(reporter, NULL != destPicture); |
| 451 | destPicture->unref(); |
| 452 | } |
| 453 | } |
| 454 | |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 455 | static void test_clip_bound_opt(skiatest::Reporter* reporter) { |
| 456 | // Test for crbug.com/229011 |
| 457 | SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), |
| 458 | SkIntToScalar(2), SkIntToScalar(2)); |
| 459 | SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), |
| 460 | SkIntToScalar(1), SkIntToScalar(1)); |
| 461 | SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), |
| 462 | SkIntToScalar(1), SkIntToScalar(1)); |
| 463 | |
| 464 | SkPath invPath; |
| 465 | invPath.addOval(rect1); |
| 466 | invPath.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 467 | SkPath path; |
| 468 | path.addOval(rect2); |
| 469 | SkPath path2; |
| 470 | path2.addOval(rect3); |
| 471 | SkIRect clipBounds; |
| 472 | // Minimalist test set for 100% code coverage of |
| 473 | // SkPictureRecord::updateClipConservativelyUsingBounds |
| 474 | { |
| 475 | SkPicture picture; |
| 476 | SkCanvas* canvas = picture.beginRecording(10, 10, |
| 477 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 478 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 479 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 480 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 481 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 482 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 483 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 484 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 485 | } |
| 486 | { |
| 487 | SkPicture picture; |
| 488 | SkCanvas* canvas = picture.beginRecording(10, 10, |
| 489 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 490 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 491 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 492 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 493 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 494 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 495 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 496 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 497 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 498 | } |
| 499 | { |
| 500 | SkPicture picture; |
| 501 | SkCanvas* canvas = picture.beginRecording(10, 10, |
| 502 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 503 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 504 | canvas->clipPath(invPath, SkRegion::kUnion_Op); |
| 505 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 506 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 507 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 508 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 509 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 510 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 511 | } |
| 512 | { |
| 513 | SkPicture picture; |
| 514 | SkCanvas* canvas = picture.beginRecording(10, 10, |
| 515 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 516 | canvas->clipPath(path, SkRegion::kDifference_Op); |
| 517 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 518 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 519 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 520 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 521 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 522 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 523 | } |
| 524 | { |
| 525 | SkPicture picture; |
| 526 | SkCanvas* canvas = picture.beginRecording(10, 10, |
| 527 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 528 | canvas->clipPath(path, SkRegion::kReverseDifference_Op); |
| 529 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 530 | // True clip is actually empty in this case, but the best |
| 531 | // determination we can make using only bounds as input is that the |
| 532 | // clip is included in the bounds of 'path'. |
| 533 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 534 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 535 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 536 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 537 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 538 | } |
| 539 | { |
| 540 | SkPicture picture; |
| 541 | SkCanvas* canvas = picture.beginRecording(10, 10, |
| 542 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 543 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 544 | canvas->clipPath(path2, SkRegion::kXOR_Op); |
| 545 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 546 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 547 | REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft); |
| 548 | REPORTER_ASSERT(reporter, 6 == clipBounds.fTop); |
| 549 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 550 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 551 | } |
| 552 | } |
| 553 | |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 554 | /** |
| 555 | * A canvas that records the number of clip commands. |
| 556 | */ |
| 557 | class ClipCountingCanvas : public SkCanvas { |
| 558 | public: |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 559 | explicit ClipCountingCanvas(SkBaseDevice* device) |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 560 | : SkCanvas(device) |
| 561 | , fClipCount(0){ |
| 562 | } |
| 563 | |
| 564 | virtual bool clipRect(const SkRect& r, SkRegion::Op op, bool doAA) |
| 565 | SK_OVERRIDE { |
| 566 | fClipCount += 1; |
| 567 | return this->INHERITED::clipRect(r, op, doAA); |
| 568 | } |
| 569 | |
| 570 | virtual bool clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) |
| 571 | SK_OVERRIDE { |
| 572 | fClipCount += 1; |
| 573 | return this->INHERITED::clipRRect(rrect, op, doAA); |
| 574 | } |
| 575 | |
| 576 | virtual bool clipPath(const SkPath& path, SkRegion::Op op, bool doAA) |
| 577 | SK_OVERRIDE { |
| 578 | fClipCount += 1; |
| 579 | return this->INHERITED::clipPath(path, op, doAA); |
| 580 | } |
| 581 | |
| 582 | unsigned getClipCount() const { return fClipCount; } |
| 583 | |
| 584 | private: |
| 585 | unsigned fClipCount; |
| 586 | |
| 587 | typedef SkCanvas INHERITED; |
| 588 | }; |
| 589 | |
| 590 | static void test_clip_expansion(skiatest::Reporter* reporter) { |
| 591 | SkPicture picture; |
| 592 | SkCanvas* canvas = picture.beginRecording(10, 10, 0); |
| 593 | |
| 594 | canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op); |
| 595 | // The following expanding clip should not be skipped. |
| 596 | canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op); |
| 597 | // Draw something so the optimizer doesn't just fold the world. |
| 598 | SkPaint p; |
| 599 | p.setColor(SK_ColorBLUE); |
| 600 | canvas->drawPaint(p); |
| 601 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 602 | SkBitmapDevice testDevice(SkBitmap::kNo_Config, 10, 10); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 603 | ClipCountingCanvas testCanvas(&testDevice); |
| 604 | picture.draw(&testCanvas); |
| 605 | |
| 606 | // Both clips should be present on playback. |
| 607 | REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2); |
| 608 | } |
| 609 | |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 610 | static void TestPicture(skiatest::Reporter* reporter) { |
| 611 | #ifdef SK_DEBUG |
| 612 | test_deleting_empty_playback(); |
| 613 | test_serializing_empty_picture(); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 614 | #else |
| 615 | test_bad_bitmap(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 616 | #endif |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 617 | test_peephole(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 618 | test_gatherpixelrefs(reporter); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 619 | test_bitmap_with_encoded_data(reporter); |
junov@chromium.org | 94f20dc | 2013-01-28 21:04:44 +0000 | [diff] [blame] | 620 | test_clone_empty(reporter); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 621 | test_clip_bound_opt(reporter); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 622 | test_clip_expansion(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | #include "TestClassDef.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 626 | DEFINE_TESTCLASS("Pictures", PictureTestClass, TestPicture) |