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

Side by Side Diff: tests/SerializationTest.cpp

Issue 1811703002: return pictures as sk_sp (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rely on RVO in picturerecorder Created 4 years, 9 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/RecordingXfermodeTest.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 "SkAnnotationKeys.h" 8 #include "SkAnnotationKeys.h"
9 #include "Resources.h" 9 #include "Resources.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 paint.setTypeface(typeface); 331 paint.setTypeface(typeface);
332 332
333 // Paint some text. 333 // Paint some text.
334 SkPictureRecorder recorder; 334 SkPictureRecorder recorder;
335 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize); 335 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
336 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()) , 336 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(canvasRect.width()) ,
337 SkIntToScalar(canvasRect.height() ), 337 SkIntToScalar(canvasRect.height() ),
338 nullptr, 0); 338 nullptr, 0);
339 canvas->drawColor(SK_ColorWHITE); 339 canvas->drawColor(SK_ColorWHITE);
340 canvas->drawText(text, 2, 24, 32, paint); 340 canvas->drawText(text, 2, 24, 32, paint);
341 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 341 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
342 342
343 // Serlialize picture and create its clone from stream. 343 // Serlialize picture and create its clone from stream.
344 SkDynamicMemoryWStream stream; 344 SkDynamicMemoryWStream stream;
345 picture->serialize(&stream); 345 picture->serialize(&stream);
346 SkAutoTDelete<SkStream> inputStream(stream.detachAsStream()); 346 SkAutoTDelete<SkStream> inputStream(stream.detachAsStream());
347 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStrea m.get())); 347 sk_sp<SkPicture> loadedPicture(SkPicture::MakeFromStream(inputStream.get())) ;
348 348
349 // Draw both original and clone picture and compare bitmaps -- they should b e identical. 349 // Draw both original and clone picture and compare bitmaps -- they should b e identical.
350 SkBitmap origBitmap = draw_picture(*picture); 350 SkBitmap origBitmap = draw_picture(*picture);
351 SkBitmap destBitmap = draw_picture(*loadedPicture); 351 SkBitmap destBitmap = draw_picture(*loadedPicture);
352 compare_bitmaps(reporter, origBitmap, destBitmap); 352 compare_bitmaps(reporter, origBitmap, destBitmap);
353 } 353 }
354 354
355 static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) { 355 static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
356 { 356 {
357 // Load typeface from file to test CreateFromFile with index. 357 // Load typeface from file to test CreateFromFile with index.
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 // even when the device fails to initialize, due to its size 522 // even when the device fails to initialize, due to its size
523 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter); 523 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter);
524 } 524 }
525 525
526 // Test simple SkPicture serialization 526 // Test simple SkPicture serialization
527 { 527 {
528 SkPictureRecorder recorder; 528 SkPictureRecorder recorder;
529 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize), 529 draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
530 SkIntToScalar(kBitmapSize), 530 SkIntToScalar(kBitmapSize),
531 nullptr, 0)); 531 nullptr, 0));
532 SkAutoTUnref<SkPicture> pict(recorder.endRecording()); 532 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
533 533
534 // Serialize picture 534 // Serialize picture
535 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); 535 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag);
536 pict->flatten(writer); 536 pict->flatten(writer);
537 size_t size = writer.bytesWritten(); 537 size_t size = writer.bytesWritten();
538 SkAutoTMalloc<unsigned char> data(size); 538 SkAutoTMalloc<unsigned char> data(size);
539 writer.writeToMemory(static_cast<void*>(data.get())); 539 writer.writeToMemory(static_cast<void*>(data.get()));
540 540
541 // Deserialize picture 541 // Deserialize picture
542 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); 542 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
543 SkAutoTUnref<SkPicture> readPict( 543 sk_sp<SkPicture> readPict(SkPicture::MakeFromBuffer(reader));
544 SkPicture::CreateFromBuffer(reader));
545 REPORTER_ASSERT(reporter, readPict.get()); 544 REPORTER_ASSERT(reporter, readPict.get());
546 } 545 }
547 546
548 TestPictureTypefaceSerialization(reporter); 547 TestPictureTypefaceSerialization(reporter);
549 } 548 }
550 549
551 //////////////////////////////////////////////////////////////////////////////// /////////////////// 550 //////////////////////////////////////////////////////////////////////////////// ///////////////////
552 #include "SkAnnotation.h" 551 #include "SkAnnotation.h"
553 552
554 static SkPicture* copy_picture_via_serialization(SkPicture* src) { 553 static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) {
555 SkDynamicMemoryWStream wstream; 554 SkDynamicMemoryWStream wstream;
556 src->serialize(&wstream); 555 src->serialize(&wstream);
557 SkAutoTDelete<SkStreamAsset> rstream(wstream.detachAsStream()); 556 SkAutoTDelete<SkStreamAsset> rstream(wstream.detachAsStream());
558 return SkPicture::CreateFromStream(rstream); 557 return SkPicture::MakeFromStream(rstream);
559 } 558 }
560 559
561 struct AnnotationRec { 560 struct AnnotationRec {
562 const SkRect fRect; 561 const SkRect fRect;
563 const char* fKey; 562 const char* fKey;
564 SkData* fValue; 563 SkData* fValue;
565 }; 564 };
566 565
567 class TestAnnotationCanvas : public SkCanvas { 566 class TestAnnotationCanvas : public SkCanvas {
568 skiatest::Reporter* fReporter; 567 skiatest::Reporter* fReporter;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6); 614 const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6);
616 SkAutoTUnref<SkData> d2(SkData::NewWithCString(str2)); 615 SkAutoTUnref<SkData> d2(SkData::NewWithCString(str2));
617 SkAnnotateLinkToDestination(recordingCanvas, r2, d2); 616 SkAnnotateLinkToDestination(recordingCanvas, r2, d2);
618 617
619 const AnnotationRec recs[] = { 618 const AnnotationRec recs[] = {
620 { r0, SkAnnotationKeys::URL_Key(), d0 }, 619 { r0, SkAnnotationKeys::URL_Key(), d0 },
621 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), d1 }, 620 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), d1 },
622 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), d2 }, 621 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), d2 },
623 }; 622 };
624 623
625 SkAutoTUnref<SkPicture> pict0(recorder.endRecording()); 624 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture());
626 SkAutoTUnref<SkPicture> pict1(copy_picture_via_serialization(pict0)); 625 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get()));
627 626
628 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs)); 627 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs));
629 canvas.drawPicture(pict1); 628 canvas.drawPicture(pict1);
630 } 629 }
631 630
OLDNEW
« no previous file with comments | « tests/RecordingXfermodeTest.cpp ('k') | tests/SkResourceCacheTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698