OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 24 matching lines...) Expand all Loading... |
35 #include "public/platform/WebThread.h" | 35 #include "public/platform/WebThread.h" |
36 #include "public/platform/WebTraceLocation.h" | 36 #include "public/platform/WebTraceLocation.h" |
37 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
38 #include "third_party/skia/include/core/SkCanvas.h" | 38 #include "third_party/skia/include/core/SkCanvas.h" |
39 #include "third_party/skia/include/core/SkImage.h" | 39 #include "third_party/skia/include/core/SkImage.h" |
40 #include "third_party/skia/include/core/SkPicture.h" | 40 #include "third_party/skia/include/core/SkPicture.h" |
41 #include "third_party/skia/include/core/SkPictureRecorder.h" | 41 #include "third_party/skia/include/core/SkPictureRecorder.h" |
42 #include "third_party/skia/include/core/SkPixmap.h" | 42 #include "third_party/skia/include/core/SkPixmap.h" |
43 #include "third_party/skia/include/core/SkSurface.h" | 43 #include "third_party/skia/include/core/SkSurface.h" |
44 #include "wtf/PassRefPtr.h" | 44 #include "wtf/PassRefPtr.h" |
| 45 #include "wtf/PtrUtil.h" |
45 #include "wtf/RefPtr.h" | 46 #include "wtf/RefPtr.h" |
| 47 #include <memory> |
46 | 48 |
47 namespace blink { | 49 namespace blink { |
48 | 50 |
49 namespace { | 51 namespace { |
50 | 52 |
51 // Raw data for a PNG file with 1x1 white pixels. | 53 // Raw data for a PNG file with 1x1 white pixels. |
52 const unsigned char whitePNG[] = { | 54 const unsigned char whitePNG[] = { |
53 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, | 55 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, |
54 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, | 56 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, |
55 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, | 57 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 94 |
93 } // namespace | 95 } // namespace |
94 | 96 |
95 class DeferredImageDecoderTest : public ::testing::Test, public MockImageDecoder
Client { | 97 class DeferredImageDecoderTest : public ::testing::Test, public MockImageDecoder
Client { |
96 public: | 98 public: |
97 void SetUp() override | 99 void SetUp() override |
98 { | 100 { |
99 ImageDecodingStore::instance().setCacheLimitInBytes(1024 * 1024); | 101 ImageDecodingStore::instance().setCacheLimitInBytes(1024 * 1024); |
100 m_data = SharedBuffer::create(whitePNG, sizeof(whitePNG)); | 102 m_data = SharedBuffer::create(whitePNG, sizeof(whitePNG)); |
101 m_frameCount = 1; | 103 m_frameCount = 1; |
102 OwnPtr<MockImageDecoder> decoder = MockImageDecoder::create(this); | 104 std::unique_ptr<MockImageDecoder> decoder = MockImageDecoder::create(thi
s); |
103 m_actualDecoder = decoder.get(); | 105 m_actualDecoder = decoder.get(); |
104 m_actualDecoder->setSize(1, 1); | 106 m_actualDecoder->setSize(1, 1); |
105 m_lazyDecoder = DeferredImageDecoder::createForTesting(std::move(decoder
)); | 107 m_lazyDecoder = DeferredImageDecoder::createForTesting(std::move(decoder
)); |
106 m_surface = SkSurface::MakeRasterN32Premul(100, 100); | 108 m_surface = SkSurface::MakeRasterN32Premul(100, 100); |
107 ASSERT_TRUE(m_surface.get()); | 109 ASSERT_TRUE(m_surface.get()); |
108 m_decodeRequestCount = 0; | 110 m_decodeRequestCount = 0; |
109 m_repetitionCount = cAnimationNone; | 111 m_repetitionCount = cAnimationNone; |
110 m_status = ImageFrame::FrameComplete; | 112 m_status = ImageFrame::FrameComplete; |
111 m_frameDuration = 0; | 113 m_frameDuration = 0; |
112 m_decodedSize = m_actualDecoder->size(); | 114 m_decodedSize = m_actualDecoder->size(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 } | 155 } |
154 | 156 |
155 protected: | 157 protected: |
156 void useMockImageDecoderFactory() | 158 void useMockImageDecoderFactory() |
157 { | 159 { |
158 m_lazyDecoder->frameGenerator()->setImageDecoderFactory(MockImageDecoder
Factory::create(this, m_decodedSize)); | 160 m_lazyDecoder->frameGenerator()->setImageDecoderFactory(MockImageDecoder
Factory::create(this, m_decodedSize)); |
159 } | 161 } |
160 | 162 |
161 // Don't own this but saves the pointer to query states. | 163 // Don't own this but saves the pointer to query states. |
162 MockImageDecoder* m_actualDecoder; | 164 MockImageDecoder* m_actualDecoder; |
163 OwnPtr<DeferredImageDecoder> m_lazyDecoder; | 165 std::unique_ptr<DeferredImageDecoder> m_lazyDecoder; |
164 sk_sp<SkSurface> m_surface; | 166 sk_sp<SkSurface> m_surface; |
165 int m_decodeRequestCount; | 167 int m_decodeRequestCount; |
166 RefPtr<SharedBuffer> m_data; | 168 RefPtr<SharedBuffer> m_data; |
167 size_t m_frameCount; | 169 size_t m_frameCount; |
168 int m_repetitionCount; | 170 int m_repetitionCount; |
169 ImageFrame::Status m_status; | 171 ImageFrame::Status m_status; |
170 float m_frameDuration; | 172 float m_frameDuration; |
171 IntSize m_decodedSize; | 173 IntSize m_decodedSize; |
172 }; | 174 }; |
173 | 175 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 EXPECT_EQ(1, image->width()); | 238 EXPECT_EQ(1, image->width()); |
237 EXPECT_EQ(1, image->height()); | 239 EXPECT_EQ(1, image->height()); |
238 | 240 |
239 SkPictureRecorder recorder; | 241 SkPictureRecorder recorder; |
240 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); | 242 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); |
241 tempCanvas->drawImage(image.get(), 0, 0); | 243 tempCanvas->drawImage(image.get(), 0, 0); |
242 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); | 244 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); |
243 EXPECT_EQ(0, m_decodeRequestCount); | 245 EXPECT_EQ(0, m_decodeRequestCount); |
244 | 246 |
245 // Create a thread to rasterize SkPicture. | 247 // Create a thread to rasterize SkPicture. |
246 OwnPtr<WebThread> thread = adoptPtr(Platform::current()->createThread("Raste
rThread")); | 248 std::unique_ptr<WebThread> thread = wrapUnique(Platform::current()->createTh
read("RasterThread")); |
247 thread->getWebTaskRunner()->postTask(BLINK_FROM_HERE, threadSafeBind(&raster
izeMain, AllowCrossThreadAccess(m_surface->getCanvas()), AllowCrossThreadAccess(
picture.get()))); | 249 thread->getWebTaskRunner()->postTask(BLINK_FROM_HERE, threadSafeBind(&raster
izeMain, AllowCrossThreadAccess(m_surface->getCanvas()), AllowCrossThreadAccess(
picture.get()))); |
248 thread.reset(); | 250 thread.reset(); |
249 EXPECT_EQ(0, m_decodeRequestCount); | 251 EXPECT_EQ(0, m_decodeRequestCount); |
250 | 252 |
251 SkBitmap canvasBitmap; | 253 SkBitmap canvasBitmap; |
252 canvasBitmap.allocN32Pixels(100, 100); | 254 canvasBitmap.allocN32Pixels(100, 100); |
253 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); | 255 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); |
254 SkAutoLockPixels autoLock(canvasBitmap); | 256 SkAutoLockPixels autoLock(canvasBitmap); |
255 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); | 257 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); |
256 } | 258 } |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 m_frameCount = 2; | 353 m_frameCount = 2; |
352 m_lazyDecoder->setData(*m_data, false); | 354 m_lazyDecoder->setData(*m_data, false); |
353 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); | 355 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); |
354 m_frameCount = 0; | 356 m_frameCount = 0; |
355 m_lazyDecoder->setData(*m_data, true); | 357 m_lazyDecoder->setData(*m_data, true); |
356 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); | 358 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); |
357 } | 359 } |
358 | 360 |
359 TEST_F(DeferredImageDecoderTest, frameOpacity) | 361 TEST_F(DeferredImageDecoderTest, frameOpacity) |
360 { | 362 { |
361 OwnPtr<ImageDecoder> actualDecoder = ImageDecoder::create(*m_data, | 363 std::unique_ptr<ImageDecoder> actualDecoder = ImageDecoder::create(*m_data, |
362 ImageDecoder::AlphaPremultiplied, ImageDecoder::GammaAndColorProfileAppl
ied); | 364 ImageDecoder::AlphaPremultiplied, ImageDecoder::GammaAndColorProfileAppl
ied); |
363 OwnPtr<DeferredImageDecoder> decoder = DeferredImageDecoder::createForTestin
g(std::move(actualDecoder)); | 365 std::unique_ptr<DeferredImageDecoder> decoder = DeferredImageDecoder::create
ForTesting(std::move(actualDecoder)); |
364 decoder->setData(*m_data, true); | 366 decoder->setData(*m_data, true); |
365 | 367 |
366 SkImageInfo pixInfo = SkImageInfo::MakeN32Premul(1, 1); | 368 SkImageInfo pixInfo = SkImageInfo::MakeN32Premul(1, 1); |
367 | 369 |
368 size_t rowBytes = pixInfo.minRowBytes(); | 370 size_t rowBytes = pixInfo.minRowBytes(); |
369 size_t size = pixInfo.getSafeSize(rowBytes); | 371 size_t size = pixInfo.getSafeSize(rowBytes); |
370 | 372 |
371 SkAutoMalloc storage(size); | 373 SkAutoMalloc storage(size); |
372 SkPixmap pixmap(pixInfo, storage.get(), rowBytes); | 374 SkPixmap pixmap(pixInfo, storage.get(), rowBytes); |
373 | 375 |
(...skipping 27 matching lines...) Expand all Loading... |
401 m_lazyDecoder->createFrameAtIndex(0); | 403 m_lazyDecoder->createFrameAtIndex(0); |
402 m_lazyDecoder->createFrameAtIndex(1); | 404 m_lazyDecoder->createFrameAtIndex(1); |
403 m_lazyDecoder->setData(*m_data, true); | 405 m_lazyDecoder->setData(*m_data, true); |
404 // Clears only the first frame (0 bytes). If DeferredImageDecoder doesn't | 406 // Clears only the first frame (0 bytes). If DeferredImageDecoder doesn't |
405 // check with the actual decoder it reports 4 bytes instead. | 407 // check with the actual decoder it reports 4 bytes instead. |
406 size_t frameBytesCleared = m_lazyDecoder->clearCacheExceptFrame(1); | 408 size_t frameBytesCleared = m_lazyDecoder->clearCacheExceptFrame(1); |
407 EXPECT_EQ(static_cast<size_t>(0), frameBytesCleared); | 409 EXPECT_EQ(static_cast<size_t>(0), frameBytesCleared); |
408 } | 410 } |
409 | 411 |
410 } // namespace blink | 412 } // namespace blink |
OLD | NEW |