OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions | |
6 * are met: | |
7 * | |
8 * 1. Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright | |
11 * notice, this list of conditions and the following disclaimer in the | |
12 * documentation and/or other materials provided with the distribution. | |
13 * | |
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 */ | |
25 | |
26 #include "config.h" | |
27 #include "core/platform/graphics/DeferredImageDecoder.h" | |
28 | |
29 #include "SkBitmapDevice.h" | |
30 #include "SkCanvas.h" | |
31 #include "SkPicture.h" | |
32 #include "core/platform/graphics/ImageDecodingStore.h" | |
33 #include "core/platform/graphics/test/MockImageDecoder.h" | |
34 #include "core/platform/graphics/skia/NativeImageSkia.h" | |
35 #include "platform/SharedBuffer.h" | |
36 #include "platform/Task.h" | |
37 #include "public/platform/Platform.h" | |
38 #include "public/platform/WebThread.h" | |
39 #include "wtf/PassRefPtr.h" | |
40 #include "wtf/RefPtr.h" | |
41 #include <gtest/gtest.h> | |
42 | |
43 namespace WebCore { | |
44 | |
45 namespace { | |
46 | |
47 // Raw data for a PNG file with 1x1 white pixels. | |
48 const unsigned char whitePNG[] = { | |
49 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, | |
50 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, | |
51 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, | |
52 0x77, 0x53, 0xde, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, | |
53 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, 0x00, 0x09, | |
54 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0b, 0x13, 0x00, 0x00, | |
55 0x0b, 0x13, 0x01, 0x00, 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x00, | |
56 0x0c, 0x49, 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xff, | |
57 0xff, 0x3f, 0x00, 0x05, 0xfe, 0x02, 0xfe, 0xdc, 0xcc, 0x59, | |
58 0xe7, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, | |
59 0x42, 0x60, 0x82, | |
60 }; | |
61 | |
62 static SkCanvas* createRasterCanvas(int width, int height) | |
63 { | |
64 SkAutoTUnref<SkBaseDevice> device(new SkBitmapDevice(SkBitmap::kARGB_8888_Co
nfig, width, height)); | |
65 return new SkCanvas(device); | |
66 } | |
67 | |
68 struct Rasterizer { | |
69 SkCanvas* canvas; | |
70 SkPicture* picture; | |
71 }; | |
72 | |
73 } // namespace | |
74 | |
75 class DeferredImageDecoderTest : public ::testing::Test, public MockImageDecoder
Client { | |
76 public: | |
77 virtual void SetUp() OVERRIDE | |
78 { | |
79 ImageDecodingStore::initializeOnce(); | |
80 DeferredImageDecoder::setEnabled(true); | |
81 m_data = SharedBuffer::create(whitePNG, sizeof(whitePNG)); | |
82 OwnPtr<MockImageDecoder> decoder = MockImageDecoder::create(this); | |
83 m_actualDecoder = decoder.get(); | |
84 m_actualDecoder->setSize(1, 1); | |
85 m_lazyDecoder = DeferredImageDecoder::createForTesting(decoder.release()
); | |
86 m_canvas.reset(createRasterCanvas(100, 100)); | |
87 m_frameBufferRequestCount = 0; | |
88 m_frameCount = 1; | |
89 m_repetitionCount = cAnimationNone; | |
90 m_status = ImageFrame::FrameComplete; | |
91 m_frameDuration = 0; | |
92 m_decodedSize = m_actualDecoder->size(); | |
93 } | |
94 | |
95 virtual void TearDown() OVERRIDE | |
96 { | |
97 ImageDecodingStore::shutdown(); | |
98 } | |
99 | |
100 virtual void decoderBeingDestroyed() OVERRIDE | |
101 { | |
102 m_actualDecoder = 0; | |
103 } | |
104 | |
105 virtual void frameBufferRequested() OVERRIDE | |
106 { | |
107 ++m_frameBufferRequestCount; | |
108 } | |
109 | |
110 virtual size_t frameCount() OVERRIDE | |
111 { | |
112 return m_frameCount; | |
113 } | |
114 | |
115 virtual int repetitionCount() const OVERRIDE | |
116 { | |
117 return m_repetitionCount; | |
118 } | |
119 | |
120 virtual ImageFrame::Status status() OVERRIDE | |
121 { | |
122 return m_status; | |
123 } | |
124 | |
125 virtual float frameDuration() const OVERRIDE | |
126 { | |
127 return m_frameDuration; | |
128 } | |
129 | |
130 virtual IntSize decodedSize() const OVERRIDE | |
131 { | |
132 return m_decodedSize; | |
133 } | |
134 | |
135 protected: | |
136 void useMockImageDecoderFactory() | |
137 { | |
138 m_lazyDecoder->frameGenerator()->setImageDecoderFactory(MockImageDecoder
Factory::create(this, m_decodedSize)); | |
139 } | |
140 | |
141 // Don't own this but saves the pointer to query states. | |
142 MockImageDecoder* m_actualDecoder; | |
143 OwnPtr<DeferredImageDecoder> m_lazyDecoder; | |
144 SkPicture m_picture; | |
145 SkAutoTUnref<SkCanvas> m_canvas; | |
146 int m_frameBufferRequestCount; | |
147 RefPtr<SharedBuffer> m_data; | |
148 size_t m_frameCount; | |
149 int m_repetitionCount; | |
150 ImageFrame::Status m_status; | |
151 float m_frameDuration; | |
152 IntSize m_decodedSize; | |
153 }; | |
154 | |
155 TEST_F(DeferredImageDecoderTest, drawIntoSkPicture) | |
156 { | |
157 m_lazyDecoder->setData(m_data.get(), true); | |
158 RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewN
ativeImage(); | |
159 EXPECT_EQ(1, image->bitmap().width()); | |
160 EXPECT_EQ(1, image->bitmap().height()); | |
161 EXPECT_FALSE(image->bitmap().isNull()); | |
162 EXPECT_TRUE(image->bitmap().isImmutable()); | |
163 | |
164 SkCanvas* tempCanvas = m_picture.beginRecording(100, 100); | |
165 tempCanvas->drawBitmap(image->bitmap(), 0, 0); | |
166 m_picture.endRecording(); | |
167 EXPECT_EQ(0, m_frameBufferRequestCount); | |
168 | |
169 m_canvas->drawPicture(m_picture); | |
170 EXPECT_EQ(0, m_frameBufferRequestCount); | |
171 | |
172 SkBitmap canvasBitmap; | |
173 canvasBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); | |
174 ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0)); | |
175 SkAutoLockPixels autoLock(canvasBitmap); | |
176 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); | |
177 } | |
178 | |
179 TEST_F(DeferredImageDecoderTest, DISABLED_drawScaledIntoSkPicture) | |
180 { | |
181 m_lazyDecoder->setData(m_data.get(), true); | |
182 RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewN
ativeImage(); | |
183 SkBitmap scaledBitmap = image->resizedBitmap(SkISize::Make(50, 51), SkIRect:
:MakeWH(50, 51)); | |
184 EXPECT_FALSE(scaledBitmap.isNull()); | |
185 EXPECT_TRUE(scaledBitmap.isImmutable()); | |
186 EXPECT_EQ(50, scaledBitmap.width()); | |
187 EXPECT_EQ(51, scaledBitmap.height()); | |
188 EXPECT_EQ(0, m_frameBufferRequestCount); | |
189 | |
190 SkCanvas* tempCanvas = m_picture.beginRecording(100, 100); | |
191 tempCanvas->drawBitmap(scaledBitmap, 0, 0); | |
192 m_picture.endRecording(); | |
193 EXPECT_EQ(0, m_frameBufferRequestCount); | |
194 | |
195 m_canvas->drawPicture(m_picture); | |
196 EXPECT_EQ(0, m_frameBufferRequestCount); | |
197 | |
198 SkBitmap canvasBitmap; | |
199 canvasBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); | |
200 ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0)); | |
201 SkAutoLockPixels autoLock(canvasBitmap); | |
202 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); | |
203 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(49, 50))
; | |
204 } | |
205 | |
206 static void rasterizeMain(SkCanvas* canvas, SkPicture* picture) | |
207 { | |
208 canvas->drawPicture(*picture); | |
209 } | |
210 | |
211 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread) | |
212 { | |
213 m_lazyDecoder->setData(m_data.get(), true); | |
214 RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewN
ativeImage(); | |
215 EXPECT_EQ(1, image->bitmap().width()); | |
216 EXPECT_EQ(1, image->bitmap().height()); | |
217 EXPECT_FALSE(image->bitmap().isNull()); | |
218 EXPECT_TRUE(image->bitmap().isImmutable()); | |
219 | |
220 SkCanvas* tempCanvas = m_picture.beginRecording(100, 100); | |
221 tempCanvas->drawBitmap(image->bitmap(), 0, 0); | |
222 m_picture.endRecording(); | |
223 EXPECT_EQ(0, m_frameBufferRequestCount); | |
224 | |
225 // Create a thread to rasterize SkPicture. | |
226 OwnPtr<blink::WebThread> thread = adoptPtr(blink::Platform::current()->creat
eThread("RasterThread")); | |
227 thread->postTask(new Task(WTF::bind(&rasterizeMain, m_canvas.get(), &m_pictu
re))); | |
228 thread.clear(); | |
229 EXPECT_EQ(0, m_frameBufferRequestCount); | |
230 | |
231 SkBitmap canvasBitmap; | |
232 canvasBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); | |
233 ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0)); | |
234 SkAutoLockPixels autoLock(canvasBitmap); | |
235 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); | |
236 } | |
237 | |
238 TEST_F(DeferredImageDecoderTest, singleFrameImageLoading) | |
239 { | |
240 m_status = ImageFrame::FramePartial; | |
241 m_lazyDecoder->setData(m_data.get(), false); | |
242 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); | |
243 ImageFrame* frame = m_lazyDecoder->frameBufferAtIndex(0); | |
244 EXPECT_EQ(ImageFrame::FramePartial, frame->status()); | |
245 EXPECT_TRUE(m_actualDecoder); | |
246 | |
247 m_status = ImageFrame::FrameComplete; | |
248 m_lazyDecoder->setData(m_data.get(), true); | |
249 EXPECT_FALSE(m_actualDecoder); | |
250 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0)); | |
251 frame = m_lazyDecoder->frameBufferAtIndex(0); | |
252 EXPECT_EQ(ImageFrame::FrameComplete, frame->status()); | |
253 EXPECT_FALSE(m_frameBufferRequestCount); | |
254 } | |
255 | |
256 TEST_F(DeferredImageDecoderTest, multiFrameImageLoading) | |
257 { | |
258 m_repetitionCount = 10; | |
259 m_frameCount = 1; | |
260 m_frameDuration = 10; | |
261 m_status = ImageFrame::FramePartial; | |
262 m_lazyDecoder->setData(m_data.get(), false); | |
263 EXPECT_EQ(ImageFrame::FramePartial, m_lazyDecoder->frameBufferAtIndex(0)->st
atus()); | |
264 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); | |
265 EXPECT_EQ(10.0f, m_lazyDecoder->frameBufferAtIndex(0)->duration()); | |
266 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0)); | |
267 | |
268 m_frameCount = 2; | |
269 m_frameDuration = 20; | |
270 m_status = ImageFrame::FrameComplete; | |
271 m_lazyDecoder->setData(m_data.get(), false); | |
272 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(0)->s
tatus()); | |
273 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(1)->s
tatus()); | |
274 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0)); | |
275 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1)); | |
276 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1)); | |
277 EXPECT_EQ(10.0f, m_lazyDecoder->frameBufferAtIndex(0)->duration()); | |
278 EXPECT_EQ(20.0f, m_lazyDecoder->frameBufferAtIndex(1)->duration()); | |
279 EXPECT_TRUE(m_actualDecoder); | |
280 | |
281 m_frameCount = 3; | |
282 m_frameDuration = 30; | |
283 m_status = ImageFrame::FrameComplete; | |
284 m_lazyDecoder->setData(m_data.get(), true); | |
285 EXPECT_FALSE(m_actualDecoder); | |
286 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(0)->s
tatus()); | |
287 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(1)->s
tatus()); | |
288 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(2)->s
tatus()); | |
289 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0)); | |
290 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1)); | |
291 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(2)); | |
292 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0)); | |
293 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1)); | |
294 EXPECT_EQ(30.0f, m_lazyDecoder->frameDurationAtIndex(2)); | |
295 EXPECT_EQ(10.0f, m_lazyDecoder->frameBufferAtIndex(0)->duration()); | |
296 EXPECT_EQ(20.0f, m_lazyDecoder->frameBufferAtIndex(1)->duration()); | |
297 EXPECT_EQ(30.0f, m_lazyDecoder->frameBufferAtIndex(2)->duration()); | |
298 EXPECT_EQ(10, m_lazyDecoder->repetitionCount()); | |
299 } | |
300 | |
301 TEST_F(DeferredImageDecoderTest, decodedSize) | |
302 { | |
303 m_decodedSize = IntSize(22, 33); | |
304 m_lazyDecoder->setData(m_data.get(), true); | |
305 RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewN
ativeImage(); | |
306 EXPECT_EQ(m_decodedSize.width(), image->bitmap().width()); | |
307 EXPECT_EQ(m_decodedSize.height(), image->bitmap().height()); | |
308 EXPECT_FALSE(image->bitmap().isNull()); | |
309 EXPECT_TRUE(image->bitmap().isImmutable()); | |
310 | |
311 useMockImageDecoderFactory(); | |
312 | |
313 // The following code should not fail any assert. | |
314 SkCanvas* tempCanvas = m_picture.beginRecording(100, 100); | |
315 tempCanvas->drawBitmap(image->bitmap(), 0, 0); | |
316 m_picture.endRecording(); | |
317 EXPECT_EQ(0, m_frameBufferRequestCount); | |
318 m_canvas->drawPicture(m_picture); | |
319 EXPECT_EQ(1, m_frameBufferRequestCount); | |
320 } | |
321 | |
322 } // namespace WebCore | |
OLD | NEW |