2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "platform/graphics/DeferredImageDecoder.h"
29 #include "SkBitmapDevice.h"
31 #include "SkPicture.h"
32 #include "platform/SharedBuffer.h"
33 #include "platform/Task.h"
34 #include "platform/graphics/ImageDecodingStore.h"
35 #include "platform/graphics/skia/NativeImageSkia.h"
36 #include "platform/graphics/test/MockImageDecoder.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>
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,
62 static SkCanvas* createRasterCanvas(int width, int height)
64 SkAutoTUnref<SkBaseDevice> device(new SkBitmapDevice(SkBitmap::kARGB_8888_Config, width, height));
65 return new SkCanvas(device);
75 class DeferredImageDecoderTest : public ::testing::Test, public MockImageDecoderClient {
77 virtual void SetUp() OVERRIDE
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;
89 m_repetitionCount = cAnimationNone;
90 m_status = ImageFrame::FrameComplete;
92 m_decodedSize = m_actualDecoder->size();
95 virtual void TearDown() OVERRIDE
97 ImageDecodingStore::shutdown();
100 virtual void decoderBeingDestroyed() OVERRIDE
105 virtual void frameBufferRequested() OVERRIDE
107 ++m_frameBufferRequestCount;
110 virtual size_t frameCount() OVERRIDE
115 virtual int repetitionCount() const OVERRIDE
117 return m_repetitionCount;
120 virtual ImageFrame::Status status() OVERRIDE
125 virtual float frameDuration() const OVERRIDE
127 return m_frameDuration;
130 virtual IntSize decodedSize() const OVERRIDE
132 return m_decodedSize;
136 void useMockImageDecoderFactory()
138 m_lazyDecoder->frameGenerator()->setImageDecoderFactory(MockImageDecoderFactory::create(this, m_decodedSize));
141 // Don't own this but saves the pointer to query states.
142 MockImageDecoder* m_actualDecoder;
143 OwnPtr<DeferredImageDecoder> m_lazyDecoder;
145 SkAutoTUnref<SkCanvas> m_canvas;
146 int m_frameBufferRequestCount;
147 RefPtr<SharedBuffer> m_data;
149 int m_repetitionCount;
150 ImageFrame::Status m_status;
151 float m_frameDuration;
152 IntSize m_decodedSize;
155 TEST_F(DeferredImageDecoderTest, drawIntoSkPicture)
157 m_lazyDecoder->setData(m_data.get(), true);
158 RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewNativeImage();
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());
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);
169 m_canvas->drawPicture(m_picture);
170 EXPECT_EQ(0, m_frameBufferRequestCount);
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));
179 TEST_F(DeferredImageDecoderTest, drawIntoSkPictureProgressive)
181 RefPtr<SharedBuffer> partialData = SharedBuffer::create(m_data->data(), m_data->size() - 10);
183 // Received only half the file.
184 m_lazyDecoder->setData(partialData.get(), false);
185 RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewNativeImage();
186 SkCanvas* tempCanvas = m_picture.beginRecording(100, 100);
187 tempCanvas->drawBitmap(image->bitmap(), 0, 0);
188 m_picture.endRecording();
189 m_canvas->drawPicture(m_picture);
191 // Fully received the file and draw the SkPicture again.
192 m_lazyDecoder->setData(m_data.get(), true);
193 image = m_lazyDecoder->frameBufferAtIndex(0)->asNewNativeImage();
194 tempCanvas = m_picture.beginRecording(100, 100);
195 tempCanvas->drawBitmap(image->bitmap(), 0, 0);
196 m_picture.endRecording();
197 m_canvas->drawPicture(m_picture);
199 SkBitmap canvasBitmap;
200 canvasBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
201 ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0));
202 SkAutoLockPixels autoLock(canvasBitmap);
203 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
206 static void rasterizeMain(SkCanvas* canvas, SkPicture* picture)
208 canvas->drawPicture(*picture);
211 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread)
213 m_lazyDecoder->setData(m_data.get(), true);
214 RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewNativeImage();
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());
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);
225 // Create a thread to rasterize SkPicture.
226 OwnPtr<blink::WebThread> thread = adoptPtr(blink::Platform::current()->createThread("RasterThread"));
227 thread->postTask(new Task(WTF::bind(&rasterizeMain, m_canvas.get(), &m_picture)));
229 EXPECT_EQ(0, m_frameBufferRequestCount);
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));
238 TEST_F(DeferredImageDecoderTest, singleFrameImageLoading)
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 unsigned firstId = frame->getSkBitmap().getGenerationID();
245 EXPECT_EQ(ImageFrame::FramePartial, frame->status());
246 EXPECT_TRUE(m_actualDecoder);
248 m_status = ImageFrame::FrameComplete;
249 m_data->append(" ", 1);
250 m_lazyDecoder->setData(m_data.get(), true);
251 EXPECT_FALSE(m_actualDecoder);
252 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0));
253 frame = m_lazyDecoder->frameBufferAtIndex(0);
254 unsigned secondId = frame->getSkBitmap().getGenerationID();
255 EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
256 EXPECT_FALSE(m_frameBufferRequestCount);
257 EXPECT_NE(firstId, secondId);
259 EXPECT_EQ(secondId, m_lazyDecoder->frameBufferAtIndex(0)->getSkBitmap().getGenerationID());
262 TEST_F(DeferredImageDecoderTest, multiFrameImageLoading)
264 m_repetitionCount = 10;
266 m_frameDuration = 10;
267 m_status = ImageFrame::FramePartial;
268 m_lazyDecoder->setData(m_data.get(), false);
269 EXPECT_EQ(ImageFrame::FramePartial, m_lazyDecoder->frameBufferAtIndex(0)->status());
270 unsigned firstId = m_lazyDecoder->frameBufferAtIndex(0)->getSkBitmap().getGenerationID();
271 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0));
272 EXPECT_EQ(10.0f, m_lazyDecoder->frameBufferAtIndex(0)->duration());
273 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0));
276 m_frameDuration = 20;
277 m_status = ImageFrame::FrameComplete;
278 m_data->append(" ", 1);
279 m_lazyDecoder->setData(m_data.get(), false);
280 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(0)->status());
281 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(1)->status());
282 unsigned secondId = m_lazyDecoder->frameBufferAtIndex(0)->getSkBitmap().getGenerationID();
283 EXPECT_NE(firstId, secondId);
284 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0));
285 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1));
286 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1));
287 EXPECT_EQ(10.0f, m_lazyDecoder->frameBufferAtIndex(0)->duration());
288 EXPECT_EQ(20.0f, m_lazyDecoder->frameBufferAtIndex(1)->duration());
289 EXPECT_TRUE(m_actualDecoder);
292 m_frameDuration = 30;
293 m_status = ImageFrame::FrameComplete;
294 m_lazyDecoder->setData(m_data.get(), true);
295 EXPECT_FALSE(m_actualDecoder);
296 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(0)->status());
297 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(1)->status());
298 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(2)->status());
299 EXPECT_EQ(secondId, m_lazyDecoder->frameBufferAtIndex(0)->getSkBitmap().getGenerationID());
300 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0));
301 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1));
302 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(2));
303 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0));
304 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1));
305 EXPECT_EQ(30.0f, m_lazyDecoder->frameDurationAtIndex(2));
306 EXPECT_EQ(10.0f, m_lazyDecoder->frameBufferAtIndex(0)->duration());
307 EXPECT_EQ(20.0f, m_lazyDecoder->frameBufferAtIndex(1)->duration());
308 EXPECT_EQ(30.0f, m_lazyDecoder->frameBufferAtIndex(2)->duration());
309 EXPECT_EQ(10, m_lazyDecoder->repetitionCount());
312 TEST_F(DeferredImageDecoderTest, decodedSize)
314 m_decodedSize = IntSize(22, 33);
315 m_lazyDecoder->setData(m_data.get(), true);
316 RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewNativeImage();
317 EXPECT_EQ(m_decodedSize.width(), image->bitmap().width());
318 EXPECT_EQ(m_decodedSize.height(), image->bitmap().height());
319 EXPECT_FALSE(image->bitmap().isNull());
320 EXPECT_TRUE(image->bitmap().isImmutable());
322 useMockImageDecoderFactory();
324 // The following code should not fail any assert.
325 SkCanvas* tempCanvas = m_picture.beginRecording(100, 100);
326 tempCanvas->drawBitmap(image->bitmap(), 0, 0);
327 m_picture.endRecording();
328 EXPECT_EQ(0, m_frameBufferRequestCount);
329 m_canvas->drawPicture(m_picture);
330 EXPECT_EQ(1, m_frameBufferRequestCount);
333 } // namespace WebCore