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

Side by Side Diff: tests/ImageTest.cpp

Issue 2206633004: Move off SK_SUPPORT_LEGACY_DATA_FACTORIES. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Gotta catch 'em all. Created 4 years, 4 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
OLDNEW
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 <functional> 8 #include <functional>
9 #include <initializer_list> 9 #include <initializer_list>
10 #include <vector> 10 #include <vector>
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 SkPaint paint; 66 SkPaint paint;
67 paint.setColor(SK_ColorBLACK); 67 paint.setColor(SK_ColorBLACK);
68 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint); 68 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
69 } 69 }
70 static sk_sp<SkImage> create_image() { 70 static sk_sp<SkImage> create_image() {
71 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); 71 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
72 auto surface(SkSurface::MakeRaster(info)); 72 auto surface(SkSurface::MakeRaster(info));
73 draw_image_test_pattern(surface->getCanvas()); 73 draw_image_test_pattern(surface->getCanvas());
74 return surface->makeImageSnapshot(); 74 return surface->makeImageSnapshot();
75 } 75 }
76 static SkData* create_image_data(SkImageInfo* info) { 76 static sk_sp<SkData> create_image_data(SkImageInfo* info) {
77 *info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); 77 *info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
78 const size_t rowBytes = info->minRowBytes(); 78 const size_t rowBytes = info->minRowBytes();
79 SkAutoTUnref<SkData> data(SkData::NewUninitialized(rowBytes * info->height() )); 79 sk_sp<SkData> data(SkData::MakeUninitialized(rowBytes * info->height()));
80 { 80 {
81 SkBitmap bm; 81 SkBitmap bm;
82 bm.installPixels(*info, data->writable_data(), rowBytes); 82 bm.installPixels(*info, data->writable_data(), rowBytes);
83 SkCanvas canvas(bm); 83 SkCanvas canvas(bm);
84 draw_image_test_pattern(&canvas); 84 draw_image_test_pattern(&canvas);
85 } 85 }
86 return data.release(); 86 return data;
87 } 87 }
88 static sk_sp<SkImage> create_data_image() { 88 static sk_sp<SkImage> create_data_image() {
89 SkImageInfo info; 89 SkImageInfo info;
90 sk_sp<SkData> data(create_image_data(&info)); 90 sk_sp<SkData> data(create_image_data(&info));
91 return SkImage::MakeRasterData(info, data, info.minRowBytes()); 91 return SkImage::MakeRasterData(info, std::move(data), info.minRowBytes());
92 } 92 }
93 #if SK_SUPPORT_GPU // not gpu-specific but currently only used in GPU tests 93 #if SK_SUPPORT_GPU // not gpu-specific but currently only used in GPU tests
94 static sk_sp<SkImage> create_image_565() { 94 static sk_sp<SkImage> create_image_565() {
95 const SkImageInfo info = SkImageInfo::Make(20, 20, kRGB_565_SkColorType, kOp aque_SkAlphaType); 95 const SkImageInfo info = SkImageInfo::Make(20, 20, kRGB_565_SkColorType, kOp aque_SkAlphaType);
96 auto surface(SkSurface::MakeRaster(info)); 96 auto surface(SkSurface::MakeRaster(info));
97 draw_image_test_pattern(surface->getCanvas()); 97 draw_image_test_pattern(surface->getCanvas());
98 return surface->makeImageSnapshot(); 98 return surface->makeImageSnapshot();
99 } 99 }
100 static sk_sp<SkImage> create_image_large() { 100 static sk_sp<SkImage> create_image_large() {
101 const SkImageInfo info = SkImageInfo::MakeN32(32000, 32, kOpaque_SkAlphaType ); 101 const SkImageInfo info = SkImageInfo::MakeN32(32000, 32, kOpaque_SkAlphaType );
(...skipping 25 matching lines...) Expand all
127 SkPictureRecorder recorder; 127 SkPictureRecorder recorder;
128 SkCanvas* canvas = recorder.beginRecording(10, 10); 128 SkCanvas* canvas = recorder.beginRecording(10, 10);
129 canvas->clear(SK_ColorCYAN); 129 canvas->clear(SK_ColorCYAN);
130 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize ::Make(10, 10), 130 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize ::Make(10, 10),
131 nullptr, nullptr); 131 nullptr, nullptr);
132 }; 132 };
133 #endif 133 #endif
134 // Want to ensure that our Release is called when the owning image is destroyed 134 // Want to ensure that our Release is called when the owning image is destroyed
135 struct RasterDataHolder { 135 struct RasterDataHolder {
136 RasterDataHolder() : fReleaseCount(0) {} 136 RasterDataHolder() : fReleaseCount(0) {}
137 SkAutoTUnref<SkData> fData; 137 sk_sp<SkData> fData;
138 int fReleaseCount; 138 int fReleaseCount;
139 static void Release(const void* pixels, void* context) { 139 static void Release(const void* pixels, void* context) {
140 RasterDataHolder* self = static_cast<RasterDataHolder*>(context); 140 RasterDataHolder* self = static_cast<RasterDataHolder*>(context);
141 self->fReleaseCount++; 141 self->fReleaseCount++;
142 self->fData.reset(); 142 self->fData.reset();
143 } 143 }
144 }; 144 };
145 static sk_sp<SkImage> create_rasterproc_image(RasterDataHolder* dataHolder) { 145 static sk_sp<SkImage> create_rasterproc_image(RasterDataHolder* dataHolder) {
146 SkASSERT(dataHolder); 146 SkASSERT(dataHolder);
147 SkImageInfo info; 147 SkImageInfo info;
148 SkAutoTUnref<SkData> data(create_image_data(&info)); 148 dataHolder->fData = create_image_data(&info);
149 dataHolder->fData.reset(SkRef(data.get())); 149 return SkImage::MakeFromRaster(SkPixmap(info, dataHolder->fData->data(), inf o.minRowBytes()),
150 return SkImage::MakeFromRaster(SkPixmap(info, data->data(), info.minRowBytes ()),
151 RasterDataHolder::Release, dataHolder); 150 RasterDataHolder::Release, dataHolder);
152 } 151 }
153 static sk_sp<SkImage> create_codec_image() { 152 static sk_sp<SkImage> create_codec_image() {
154 SkImageInfo info; 153 SkImageInfo info;
155 SkAutoTUnref<SkData> data(create_image_data(&info)); 154 sk_sp<SkData> data(create_image_data(&info));
156 SkBitmap bitmap; 155 SkBitmap bitmap;
157 bitmap.installPixels(info, data->writable_data(), info.minRowBytes()); 156 bitmap.installPixels(info, data->writable_data(), info.minRowBytes());
158 sk_sp<SkData> src( 157 sk_sp<SkData> src(SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Ty pe, 100));
159 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100)); 158 return SkImage::MakeFromEncoded(std::move(src));
160 return SkImage::MakeFromEncoded(src);
161 } 159 }
162 #if SK_SUPPORT_GPU 160 #if SK_SUPPORT_GPU
163 static sk_sp<SkImage> create_gpu_image(GrContext* context) { 161 static sk_sp<SkImage> create_gpu_image(GrContext* context) {
164 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); 162 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
165 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info)); 163 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
166 draw_image_test_pattern(surface->getCanvas()); 164 draw_image_test_pattern(surface->getCanvas());
167 return surface->makeImageSnapshot(); 165 return surface->makeImageSnapshot();
168 } 166 }
169 #endif 167 #endif
170 168
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 REPORTER_ASSERT(reporter, rec.fExpectSameAsImmutable == sameImmutable); 222 REPORTER_ASSERT(reporter, rec.fExpectSameAsImmutable == sameImmutable);
225 } 223 }
226 } 224 }
227 225
228 namespace { 226 namespace {
229 227
230 const char* kSerializedData = "serialized"; 228 const char* kSerializedData = "serialized";
231 229
232 class MockSerializer : public SkPixelSerializer { 230 class MockSerializer : public SkPixelSerializer {
233 public: 231 public:
234 MockSerializer(SkData* (*func)()) : fFunc(func), fDidEncode(false) { } 232 MockSerializer(sk_sp<SkData> (*func)()) : fFunc(func), fDidEncode(false) { }
235 233
236 bool didEncode() const { return fDidEncode; } 234 bool didEncode() const { return fDidEncode; }
237 235
238 protected: 236 protected:
239 bool onUseEncodedData(const void*, size_t) override { 237 bool onUseEncodedData(const void*, size_t) override {
240 return false; 238 return false;
241 } 239 }
242 240
243 SkData* onEncode(const SkPixmap&) override { 241 SkData* onEncode(const SkPixmap&) override {
244 fDidEncode = true; 242 fDidEncode = true;
245 return fFunc(); 243 return fFunc().release();
246 } 244 }
247 245
248 private: 246 private:
249 SkData* (*fFunc)(); 247 sk_sp<SkData> (*fFunc)();
250 bool fDidEncode; 248 bool fDidEncode;
251 249
252 typedef SkPixelSerializer INHERITED; 250 typedef SkPixelSerializer INHERITED;
253 }; 251 };
254 252
255 } // anonymous namespace 253 } // anonymous namespace
256 254
257 // Test that SkImage encoding observes custom pixel serializers. 255 // Test that SkImage encoding observes custom pixel serializers.
258 DEF_TEST(Image_Encode_Serializer, reporter) { 256 DEF_TEST(Image_Encode_Serializer, reporter) {
259 MockSerializer serializer([]() -> SkData* { return SkData::NewWithCString(kS erializedData); }); 257 MockSerializer serializer([]() -> sk_sp<SkData> {
258 return SkData::MakeWithCString(kSerializedData);
259 });
260 sk_sp<SkImage> image(create_image()); 260 sk_sp<SkImage> image(create_image());
261 SkAutoTUnref<SkData> encoded(image->encode(&serializer)); 261 sk_sp<SkData> encoded(image->encode(&serializer));
262 SkAutoTUnref<SkData> reference(SkData::NewWithCString(kSerializedData)); 262 sk_sp<SkData> reference(SkData::MakeWithCString(kSerializedData));
263 263
264 REPORTER_ASSERT(reporter, serializer.didEncode()); 264 REPORTER_ASSERT(reporter, serializer.didEncode());
265 REPORTER_ASSERT(reporter, encoded); 265 REPORTER_ASSERT(reporter, encoded);
266 REPORTER_ASSERT(reporter, encoded->size() > 0); 266 REPORTER_ASSERT(reporter, encoded->size() > 0);
267 REPORTER_ASSERT(reporter, encoded->equals(reference)); 267 REPORTER_ASSERT(reporter, encoded->equals(reference.get()));
268 } 268 }
269 269
270 // Test that image encoding failures do not break picture serialization/deserial ization. 270 // Test that image encoding failures do not break picture serialization/deserial ization.
271 DEF_TEST(Image_Serialize_Encoding_Failure, reporter) { 271 DEF_TEST(Image_Serialize_Encoding_Failure, reporter) {
272 auto surface(SkSurface::MakeRasterN32Premul(100, 100)); 272 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
273 surface->getCanvas()->clear(SK_ColorGREEN); 273 surface->getCanvas()->clear(SK_ColorGREEN);
274 sk_sp<SkImage> image(surface->makeImageSnapshot()); 274 sk_sp<SkImage> image(surface->makeImageSnapshot());
275 REPORTER_ASSERT(reporter, image); 275 REPORTER_ASSERT(reporter, image);
276 276
277 SkPictureRecorder recorder; 277 SkPictureRecorder recorder;
278 SkCanvas* canvas = recorder.beginRecording(100, 100); 278 SkCanvas* canvas = recorder.beginRecording(100, 100);
279 canvas->drawImage(image, 0, 0); 279 canvas->drawImage(image, 0, 0);
280 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); 280 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
281 REPORTER_ASSERT(reporter, picture); 281 REPORTER_ASSERT(reporter, picture);
282 REPORTER_ASSERT(reporter, picture->approximateOpCount() > 0); 282 REPORTER_ASSERT(reporter, picture->approximateOpCount() > 0);
283 283
284 MockSerializer emptySerializer([]() -> SkData* { return SkData::NewEmpty(); }); 284 MockSerializer emptySerializer([]() -> sk_sp<SkData> { return SkData::MakeEm pty(); });
285 MockSerializer nullSerializer([]() -> SkData* { return nullptr; }); 285 MockSerializer nullSerializer([]() -> sk_sp<SkData> { return nullptr; });
286 MockSerializer* serializers[] = { &emptySerializer, &nullSerializer }; 286 MockSerializer* serializers[] = { &emptySerializer, &nullSerializer };
287 287
288 for (size_t i = 0; i < SK_ARRAY_COUNT(serializers); ++i) { 288 for (size_t i = 0; i < SK_ARRAY_COUNT(serializers); ++i) {
289 SkDynamicMemoryWStream wstream; 289 SkDynamicMemoryWStream wstream;
290 REPORTER_ASSERT(reporter, !serializers[i]->didEncode()); 290 REPORTER_ASSERT(reporter, !serializers[i]->didEncode());
291 picture->serialize(&wstream, serializers[i]); 291 picture->serialize(&wstream, serializers[i]);
292 REPORTER_ASSERT(reporter, serializers[i]->didEncode()); 292 REPORTER_ASSERT(reporter, serializers[i]->didEncode());
293 293
294 SkAutoTDelete<SkStream> rstream(wstream.detachAsStream()); 294 SkAutoTDelete<SkStream> rstream(wstream.detachAsStream());
295 sk_sp<SkPicture> deserialized(SkPicture::MakeFromStream(rstream)); 295 sk_sp<SkPicture> deserialized(SkPicture::MakeFromStream(rstream));
(...skipping 720 matching lines...) Expand 10 before | Expand all | Expand 10 after
1016 SkBitmap bm1; 1016 SkBitmap bm1;
1017 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kUnpremul_SkAlphaType)); 1017 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kUnpremul_SkAlphaType));
1018 bm0.readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0); 1018 bm0.readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
1019 1019
1020 SkBitmap bm2; 1020 SkBitmap bm2;
1021 bm2.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType)); 1021 bm2.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1022 bm1.readPixels(bm2.info(), bm2.getPixels(), bm2.rowBytes(), 0, 0); 1022 bm1.readPixels(bm2.info(), bm2.getPixels(), bm2.rowBytes(), 0, 0);
1023 1023
1024 REPORTER_ASSERT(reporter, equal(bm0, bm2)); 1024 REPORTER_ASSERT(reporter, equal(bm0, bm2));
1025 } 1025 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698