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