| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkCanvas.h" | 8 #include "SkCanvas.h" |
| 9 #include "SkData.h" | 9 #include "SkData.h" |
| 10 #include "SkDevice.h" | 10 #include "SkDevice.h" |
| 11 #include "SkImageEncoder.h" | 11 #include "SkImageEncoder.h" |
| 12 #include "SkImage_Base.h" | 12 #include "SkImage_Base.h" |
| 13 #include "SkPicture.h" |
| 14 #include "SkPictureRecorder.h" |
| 13 #include "SkPixelSerializer.h" | 15 #include "SkPixelSerializer.h" |
| 14 #include "SkRRect.h" | 16 #include "SkRRect.h" |
| 17 #include "SkStream.h" |
| 15 #include "SkSurface.h" | 18 #include "SkSurface.h" |
| 16 #include "SkUtils.h" | 19 #include "SkUtils.h" |
| 17 #include "Test.h" | 20 #include "Test.h" |
| 18 | 21 |
| 19 #if SK_SUPPORT_GPU | 22 #if SK_SUPPORT_GPU |
| 20 #include "GrContextFactory.h" | 23 #include "GrContextFactory.h" |
| 21 #include "GrTest.h" | 24 #include "GrTest.h" |
| 22 #include "gl/GrGLInterface.h" | 25 #include "gl/GrGLInterface.h" |
| 23 #include "gl/GrGLUtil.h" | 26 #include "gl/GrGLUtil.h" |
| 24 #else | 27 #else |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 } | 106 } |
| 104 test_encode(reporter, ctx); | 107 test_encode(reporter, ctx); |
| 105 } | 108 } |
| 106 #endif | 109 #endif |
| 107 | 110 |
| 108 namespace { | 111 namespace { |
| 109 | 112 |
| 110 const char* kSerializedData = "serialized"; | 113 const char* kSerializedData = "serialized"; |
| 111 | 114 |
| 112 class MockSerializer : public SkPixelSerializer { | 115 class MockSerializer : public SkPixelSerializer { |
| 116 public: |
| 117 MockSerializer(SkData* (*func)()) : fFunc(func), fDidEncode(false) { } |
| 118 |
| 119 bool didEncode() const { return fDidEncode; } |
| 120 |
| 113 protected: | 121 protected: |
| 114 bool onUseEncodedData(const void*, size_t) override { | 122 bool onUseEncodedData(const void*, size_t) override { |
| 115 return false; | 123 return false; |
| 116 } | 124 } |
| 117 | 125 |
| 118 SkData* onEncodePixels(const SkImageInfo&, const void*, size_t) override { | 126 SkData* onEncodePixels(const SkImageInfo&, const void*, size_t) override { |
| 119 return SkData::NewWithCString(kSerializedData); | 127 fDidEncode = true; |
| 128 return fFunc(); |
| 120 } | 129 } |
| 130 |
| 131 private: |
| 132 SkData* (*fFunc)(); |
| 133 bool fDidEncode; |
| 134 |
| 135 typedef SkPixelSerializer INHERITED; |
| 121 }; | 136 }; |
| 122 | 137 |
| 123 } // anonymous namespace | 138 } // anonymous namespace |
| 124 | 139 |
| 125 // Test that SkImage encoding observes custom pixel serializers. | 140 // Test that SkImage encoding observes custom pixel serializers. |
| 126 DEF_TEST(Image_Encode_Serializer, reporter) { | 141 DEF_TEST(Image_Encode_Serializer, reporter) { |
| 127 MockSerializer serializer; | 142 MockSerializer serializer([]() -> SkData* { return SkData::NewWithCString(kS
erializedData); }); |
| 128 const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10); | 143 const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10); |
| 129 SkAutoTUnref<SkImage> image(make_image(nullptr, 20, 20, ir)); | 144 SkAutoTUnref<SkImage> image(make_image(nullptr, 20, 20, ir)); |
| 130 SkAutoTUnref<SkData> encoded(image->encode(&serializer)); | 145 SkAutoTUnref<SkData> encoded(image->encode(&serializer)); |
| 131 SkAutoTUnref<SkData> reference(SkData::NewWithCString(kSerializedData)); | 146 SkAutoTUnref<SkData> reference(SkData::NewWithCString(kSerializedData)); |
| 132 | 147 |
| 148 REPORTER_ASSERT(reporter, serializer.didEncode()); |
| 133 REPORTER_ASSERT(reporter, encoded); | 149 REPORTER_ASSERT(reporter, encoded); |
| 134 REPORTER_ASSERT(reporter, encoded->size() > 0); | 150 REPORTER_ASSERT(reporter, encoded->size() > 0); |
| 135 REPORTER_ASSERT(reporter, encoded->equals(reference)); | 151 REPORTER_ASSERT(reporter, encoded->equals(reference)); |
| 136 } | 152 } |
| 137 | 153 |
| 154 // Test that image encoding failures do not break picture serialization/deserial
ization. |
| 155 DEF_TEST(Image_Serialize_Encoding_Failure, reporter) { |
| 156 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(100, 100)); |
| 157 surface->getCanvas()->clear(SK_ColorGREEN); |
| 158 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
| 159 REPORTER_ASSERT(reporter, image); |
| 160 |
| 161 SkPictureRecorder recorder; |
| 162 SkCanvas* canvas = recorder.beginRecording(100, 100); |
| 163 canvas->drawImage(image, 0, 0); |
| 164 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 165 REPORTER_ASSERT(reporter, picture); |
| 166 REPORTER_ASSERT(reporter, picture->approximateOpCount() > 0); |
| 167 |
| 168 MockSerializer emptySerializer([]() -> SkData* { return SkData::NewEmpty();
}); |
| 169 MockSerializer nullSerializer([]() -> SkData* { return nullptr; }); |
| 170 MockSerializer* serializers[] = { &emptySerializer, &nullSerializer }; |
| 171 |
| 172 for (size_t i = 0; i < SK_ARRAY_COUNT(serializers); ++i) { |
| 173 SkDynamicMemoryWStream wstream; |
| 174 REPORTER_ASSERT(reporter, !serializers[i]->didEncode()); |
| 175 picture->serialize(&wstream, serializers[i]); |
| 176 REPORTER_ASSERT(reporter, serializers[i]->didEncode()); |
| 177 |
| 178 SkAutoTDelete<SkStream> rstream(wstream.detachAsStream()); |
| 179 SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rstream
)); |
| 180 REPORTER_ASSERT(reporter, deserialized); |
| 181 REPORTER_ASSERT(reporter, deserialized->approximateOpCount() > 0); |
| 182 } |
| 183 } |
| 184 |
| 138 DEF_TEST(Image_NewRasterCopy, reporter) { | 185 DEF_TEST(Image_NewRasterCopy, reporter) { |
| 139 const SkPMColor red = SkPackARGB32(0xFF, 0xFF, 0, 0); | 186 const SkPMColor red = SkPackARGB32(0xFF, 0xFF, 0, 0); |
| 140 const SkPMColor green = SkPackARGB32(0xFF, 0, 0xFF, 0); | 187 const SkPMColor green = SkPackARGB32(0xFF, 0, 0xFF, 0); |
| 141 const SkPMColor blue = SkPackARGB32(0xFF, 0, 0, 0xFF); | 188 const SkPMColor blue = SkPackARGB32(0xFF, 0, 0, 0xFF); |
| 142 SkPMColor colors[] = { red, green, blue, 0 }; | 189 SkPMColor colors[] = { red, green, blue, 0 }; |
| 143 SkAutoTUnref<SkColorTable> ctable(new SkColorTable(colors, SK_ARRAY_COUNT(co
lors))); | 190 SkAutoTUnref<SkColorTable> ctable(new SkColorTable(colors, SK_ARRAY_COUNT(co
lors))); |
| 144 // The colortable made a copy, so we can trash the original colors | 191 // The colortable made a copy, so we can trash the original colors |
| 145 memset(colors, 0xFF, sizeof(colors)); | 192 memset(colors, 0xFF, sizeof(colors)); |
| 146 | 193 |
| 147 const SkImageInfo srcInfo = SkImageInfo::Make(2, 2, kIndex_8_SkColorType, kP
remul_SkAlphaType); | 194 const SkImageInfo srcInfo = SkImageInfo::Make(2, 2, kIndex_8_SkColorType, kP
remul_SkAlphaType); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 } | 386 } |
| 340 } | 387 } |
| 341 | 388 |
| 342 image.reset(nullptr); | 389 image.reset(nullptr); |
| 343 { | 390 { |
| 344 SkBitmap cachedBitmap; | 391 SkBitmap cachedBitmap; |
| 345 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(uniqueID, &cachedBitmap))
; | 392 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(uniqueID, &cachedBitmap))
; |
| 346 } | 393 } |
| 347 } | 394 } |
| 348 #endif | 395 #endif |
| OLD | NEW |