Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: tests/SerializationTest.cpp

Issue 510423005: make allocPixels throw on failure (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tests/ReadPixelsTest.cpp ('k') | tests/SkResourceCacheTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "Resources.h" 8 #include "Resources.h"
9 #include "SkBitmapSource.h" 9 #include "SkBitmapSource.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 picture->serialize(&stream); 328 picture->serialize(&stream);
329 SkAutoTUnref<SkStream> inputStream(stream.detachAsStream()); 329 SkAutoTUnref<SkStream> inputStream(stream.detachAsStream());
330 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStrea m.get())); 330 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStrea m.get()));
331 331
332 // Draw both original and clone picture and compare bitmaps -- they should b e identical. 332 // Draw both original and clone picture and compare bitmaps -- they should b e identical.
333 SkBitmap origBitmap = draw_picture(*picture); 333 SkBitmap origBitmap = draw_picture(*picture);
334 SkBitmap destBitmap = draw_picture(*loadedPicture); 334 SkBitmap destBitmap = draw_picture(*loadedPicture);
335 compare_bitmaps(reporter, origBitmap, destBitmap); 335 compare_bitmaps(reporter, origBitmap, destBitmap);
336 } 336 }
337 337
338 static bool setup_bitmap_for_canvas(SkBitmap* bitmap) { 338 static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
339 SkImageInfo info = SkImageInfo::Make( 339 bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
340 kBitmapSize, kBitmapSize, kN32_SkColorType, kPremul_SkAlphaType);
341 return bitmap->allocPixels(info);
342 } 340 }
343 341
344 static bool make_checkerboard_bitmap(SkBitmap& bitmap) { 342 static void make_checkerboard_bitmap(SkBitmap& bitmap) {
345 bool success = setup_bitmap_for_canvas(&bitmap); 343 setup_bitmap_for_canvas(&bitmap);
346 344
347 SkCanvas canvas(bitmap); 345 SkCanvas canvas(bitmap);
348 canvas.clear(0x00000000); 346 canvas.clear(0x00000000);
349 SkPaint darkPaint; 347 SkPaint darkPaint;
350 darkPaint.setColor(0xFF804020); 348 darkPaint.setColor(0xFF804020);
351 SkPaint lightPaint; 349 SkPaint lightPaint;
352 lightPaint.setColor(0xFF244484); 350 lightPaint.setColor(0xFF244484);
353 const int i = kBitmapSize / 8; 351 const int i = kBitmapSize / 8;
354 const SkScalar f = SkIntToScalar(i); 352 const SkScalar f = SkIntToScalar(i);
355 for (int y = 0; y < kBitmapSize; y += i) { 353 for (int y = 0; y < kBitmapSize; y += i) {
356 for (int x = 0; x < kBitmapSize; x += i) { 354 for (int x = 0; x < kBitmapSize; x += i) {
357 canvas.save(); 355 canvas.save();
358 canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); 356 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
359 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint); 357 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint);
360 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint); 358 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint);
361 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint); 359 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint);
362 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint); 360 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint);
363 canvas.restore(); 361 canvas.restore();
364 } 362 }
365 } 363 }
366
367 return success;
368 } 364 }
369 365
370 static bool draw_something(SkCanvas* canvas) { 366 static void draw_something(SkCanvas* canvas) {
371 SkPaint paint; 367 SkPaint paint;
372 SkBitmap bitmap; 368 SkBitmap bitmap;
373 bool success = make_checkerboard_bitmap(bitmap); 369 make_checkerboard_bitmap(bitmap);
374 370
375 canvas->save(); 371 canvas->save();
376 canvas->scale(0.5f, 0.5f); 372 canvas->scale(0.5f, 0.5f);
377 canvas->drawBitmap(bitmap, 0, 0, NULL); 373 canvas->drawBitmap(bitmap, 0, 0, NULL);
378 canvas->restore(); 374 canvas->restore();
379 375
380 const char beforeStr[] = "before circle"; 376 const char beforeStr[] = "before circle";
381 const char afterStr[] = "after circle"; 377 const char afterStr[] = "after circle";
382 378
383 paint.setAntiAlias(true); 379 paint.setAntiAlias(true);
384 380
385 paint.setColor(SK_ColorRED); 381 paint.setColor(SK_ColorRED);
386 canvas->drawData(beforeStr, sizeof(beforeStr)); 382 canvas->drawData(beforeStr, sizeof(beforeStr));
387 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2 ), SkIntToScalar(kBitmapSize/3), paint); 383 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2 ), SkIntToScalar(kBitmapSize/3), paint);
388 canvas->drawData(afterStr, sizeof(afterStr)); 384 canvas->drawData(afterStr, sizeof(afterStr));
389 paint.setColor(SK_ColorBLACK); 385 paint.setColor(SK_ColorBLACK);
390 paint.setTextSize(SkIntToScalar(kBitmapSize/3)); 386 paint.setTextSize(SkIntToScalar(kBitmapSize/3));
391 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(k BitmapSize/4), paint); 387 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(k BitmapSize/4), paint);
392
393 return success;
394 } 388 }
395 389
396 DEF_TEST(Serialization, reporter) { 390 DEF_TEST(Serialization, reporter) {
397 // Test matrix serialization 391 // Test matrix serialization
398 { 392 {
399 SkMatrix matrix = SkMatrix::I(); 393 SkMatrix matrix = SkMatrix::I();
400 TestObjectSerialization(&matrix, reporter); 394 TestObjectSerialization(&matrix, reporter);
401 } 395 }
402 396
403 // Test path serialization 397 // Test path serialization
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 invalidBitmap.setInfo(info); 468 invalidBitmap.setInfo(info);
475 469
476 // The deserialization should succeed, and the rendering shouldn't crash , 470 // The deserialization should succeed, and the rendering shouldn't crash ,
477 // even when the device fails to initialize, due to its size 471 // even when the device fails to initialize, due to its size
478 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter); 472 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
479 } 473 }
480 474
481 // Test simple SkPicture serialization 475 // Test simple SkPicture serialization
482 { 476 {
483 SkPictureRecorder recorder; 477 SkPictureRecorder recorder;
484 bool didDraw = draw_something(recorder.beginRecording(SkIntToScalar(kBit mapSize), 478 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
485 SkIntToScalar(kBit mapSize), 479 SkIntToScalar(kBitmapSize),
486 NULL, 0)); 480 NULL, 0));
487 REPORTER_ASSERT(reporter, didDraw);
488 SkAutoTUnref<SkPicture> pict(recorder.endRecording()); 481 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
489 482
490 // Serialize picture 483 // Serialize picture
491 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); 484 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
492 pict->flatten(writer); 485 pict->flatten(writer);
493 size_t size = writer.bytesWritten(); 486 size_t size = writer.bytesWritten();
494 SkAutoTMalloc<unsigned char> data(size); 487 SkAutoTMalloc<unsigned char> data(size);
495 writer.writeToMemory(static_cast<void*>(data.get())); 488 writer.writeToMemory(static_cast<void*>(data.get()));
496 489
497 // Deserialize picture 490 // Deserialize picture
498 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); 491 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
499 SkAutoTUnref<SkPicture> readPict( 492 SkAutoTUnref<SkPicture> readPict(
500 SkPicture::CreateFromBuffer(reader)); 493 SkPicture::CreateFromBuffer(reader));
501 REPORTER_ASSERT(reporter, NULL != readPict.get()); 494 REPORTER_ASSERT(reporter, NULL != readPict.get());
502 } 495 }
503 496
504 TestPictureTypefaceSerialization(reporter); 497 TestPictureTypefaceSerialization(reporter);
505 } 498 }
OLDNEW
« no previous file with comments | « tests/ReadPixelsTest.cpp ('k') | tests/SkResourceCacheTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698