OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 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 |
| 8 #include "Resources.h" |
| 9 #include "SkCanvas.h" |
| 10 #include "SkPipe.h" |
| 11 #include "SkPaint.h" |
| 12 #include "SkStream.h" |
| 13 #include "SkSurface.h" |
| 14 #include "Test.h" |
| 15 |
| 16 #include "SkNullCanvas.h" |
| 17 #include "SkAutoPixmapStorage.h" |
| 18 |
| 19 static void drain(SkPipeDeserializer* deserial, SkDynamicMemoryWStream* stream)
{ |
| 20 std::unique_ptr<SkCanvas> canvas(SkCreateNullCanvas()); |
| 21 sk_sp<SkData> data = stream->detachAsData(); |
| 22 deserial->playback(data->data(), data->size(), canvas.get()); |
| 23 } |
| 24 |
| 25 static sk_sp<SkImage> drain_as_image(SkPipeDeserializer* deserial, SkDynamicMemo
ryWStream* stream) { |
| 26 sk_sp<SkData> data = stream->detachAsData(); |
| 27 return deserial->readImage(data->data(), data->size()); |
| 28 } |
| 29 |
| 30 static bool deep_equal(SkImage* a, SkImage* b) { |
| 31 if (a->width() != b->width() || a->height() != b->height()) { |
| 32 return false; |
| 33 } |
| 34 |
| 35 const SkImageInfo info = SkImageInfo::MakeN32Premul(a->width(), a->height())
; |
| 36 SkAutoPixmapStorage pmapA, pmapB; |
| 37 pmapA.alloc(info); |
| 38 pmapB.alloc(info); |
| 39 |
| 40 if (!a->readPixels(pmapA, 0, 0) || !b->readPixels(pmapB, 0, 0)) { |
| 41 return false; |
| 42 } |
| 43 |
| 44 for (int y = 0; y < info.height(); ++y) { |
| 45 if (memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y), info.width() * sizeof
(SkPMColor))) { |
| 46 return false; |
| 47 } |
| 48 } |
| 49 return true; |
| 50 } |
| 51 |
| 52 DEF_TEST(Pipe_image, reporter) { |
| 53 sk_sp<SkImage> img = GetResourceAsImage("mandrill_128.png"); |
| 54 SkASSERT(img.get()); |
| 55 |
| 56 SkPipeSerializer serializer; |
| 57 SkPipeDeserializer deserializer; |
| 58 |
| 59 SkDynamicMemoryWStream stream; |
| 60 SkCanvas* wc = serializer.beginWrite(SkRect::MakeWH(100, 100), &stream); |
| 61 wc->drawImage(img, 0, 0, nullptr); |
| 62 serializer.endWrite(); |
| 63 size_t offset0 = stream.bytesWritten(); |
| 64 REPORTER_ASSERT(reporter, offset0 > 100); // the raw image must be sorta b
ig |
| 65 drain(&deserializer, &stream); |
| 66 |
| 67 // try drawing the same image again -- it should be much smaller |
| 68 wc = serializer.beginWrite(SkRect::MakeWH(100, 100), &stream); |
| 69 wc->drawImage(img, 0, 0, nullptr); |
| 70 size_t offset1 = stream.bytesWritten(); |
| 71 serializer.endWrite(); |
| 72 REPORTER_ASSERT(reporter, offset1 <= 32); |
| 73 drain(&deserializer, &stream); |
| 74 |
| 75 // try serializing the same image directly, again it should be small |
| 76 serializer.write(img.get(), &stream); |
| 77 size_t offset2 = stream.bytesWritten(); |
| 78 REPORTER_ASSERT(reporter, offset2 <= 32); |
| 79 auto img1 = drain_as_image(&deserializer, &stream); |
| 80 REPORTER_ASSERT(reporter, deep_equal(img.get(), img1.get())); |
| 81 |
| 82 // try serializing the same image directly (again), check that it is the sam
e! |
| 83 serializer.write(img.get(), &stream); |
| 84 size_t offset3 = stream.bytesWritten(); |
| 85 REPORTER_ASSERT(reporter, offset3 <= 32); |
| 86 auto img2 = drain_as_image(&deserializer, &stream); |
| 87 REPORTER_ASSERT(reporter, img1.get() == img2.get()); |
| 88 } |
OLD | NEW |