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 static void rasterizeMain(SkCanvas* canvas, SkPicture* picture)
181 canvas->drawPicture(*picture);
184 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread)
186 m_lazyDecoder->setData(m_data.get(), true);
187 RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewNativeImage();
188 EXPECT_EQ(1, image->bitmap().width());
189 EXPECT_EQ(1, image->bitmap().height());
190 EXPECT_FALSE(image->bitmap().isNull());
191 EXPECT_TRUE(image->bitmap().isImmutable());
193 SkCanvas* tempCanvas = m_picture.beginRecording(100, 100);
194 tempCanvas->drawBitmap(image->bitmap(), 0, 0);
195 m_picture.endRecording();
196 EXPECT_EQ(0, m_frameBufferRequestCount);
198 // Create a thread to rasterize SkPicture.
199 OwnPtr<blink::WebThread> thread = adoptPtr(blink::Platform::current()->createThread("RasterThread"));
200 thread->postTask(new Task(WTF::bind(&rasterizeMain, m_canvas.get(), &m_picture)));
202 EXPECT_EQ(0, m_frameBufferRequestCount);
204 SkBitmap canvasBitmap;
205 canvasBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
206 ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0));
207 SkAutoLockPixels autoLock(canvasBitmap);
208 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
211 TEST_F(DeferredImageDecoderTest, singleFrameImageLoading)
213 m_status = ImageFrame::FramePartial;
214 m_lazyDecoder->setData(m_data.get(), false);
215 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0));
216 ImageFrame* frame = m_lazyDecoder->frameBufferAtIndex(0);
217 unsigned firstId = frame->getSkBitmap().getGenerationID();
218 EXPECT_EQ(ImageFrame::FramePartial, frame->status());
219 EXPECT_TRUE(m_actualDecoder);
221 m_status = ImageFrame::FrameComplete;
222 m_data->append(" ", 1);
223 m_lazyDecoder->setData(m_data.get(), true);
224 EXPECT_FALSE(m_actualDecoder);
225 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0));
226 frame = m_lazyDecoder->frameBufferAtIndex(0);
227 unsigned secondId = frame->getSkBitmap().getGenerationID();
228 EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
229 EXPECT_FALSE(m_frameBufferRequestCount);
230 EXPECT_NE(firstId, secondId);
232 EXPECT_EQ(secondId, m_lazyDecoder->frameBufferAtIndex(0)->getSkBitmap().getGenerationID());
235 TEST_F(DeferredImageDecoderTest, multiFrameImageLoading)
237 m_repetitionCount = 10;
239 m_frameDuration = 10;
240 m_status = ImageFrame::FramePartial;
241 m_lazyDecoder->setData(m_data.get(), false);
242 EXPECT_EQ(ImageFrame::FramePartial, m_lazyDecoder->frameBufferAtIndex(0)->status());
243 unsigned firstId = m_lazyDecoder->frameBufferAtIndex(0)->getSkBitmap().getGenerationID();
244 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0));
245 EXPECT_EQ(10.0f, m_lazyDecoder->frameBufferAtIndex(0)->duration());
246 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0));
249 m_frameDuration = 20;
250 m_status = ImageFrame::FrameComplete;
251 m_data->append(" ", 1);
252 m_lazyDecoder->setData(m_data.get(), false);
253 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(0)->status());
254 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(1)->status());
255 unsigned secondId = m_lazyDecoder->frameBufferAtIndex(0)->getSkBitmap().getGenerationID();
256 EXPECT_NE(firstId, secondId);
257 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0));
258 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1));
259 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1));
260 EXPECT_EQ(10.0f, m_lazyDecoder->frameBufferAtIndex(0)->duration());
261 EXPECT_EQ(20.0f, m_lazyDecoder->frameBufferAtIndex(1)->duration());
262 EXPECT_TRUE(m_actualDecoder);
265 m_frameDuration = 30;
266 m_status = ImageFrame::FrameComplete;
267 m_lazyDecoder->setData(m_data.get(), true);
268 EXPECT_FALSE(m_actualDecoder);
269 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(0)->status());
270 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(1)->status());
271 EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(2)->status());
272 EXPECT_EQ(secondId, m_lazyDecoder->frameBufferAtIndex(0)->getSkBitmap().getGenerationID());
273 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0));
274 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1));
275 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(2));
276 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0));
277 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1));
278 EXPECT_EQ(30.0f, m_lazyDecoder->frameDurationAtIndex(2));
279 EXPECT_EQ(10.0f, m_lazyDecoder->frameBufferAtIndex(0)->duration());
280 EXPECT_EQ(20.0f, m_lazyDecoder->frameBufferAtIndex(1)->duration());
281 EXPECT_EQ(30.0f, m_lazyDecoder->frameBufferAtIndex(2)->duration());
282 EXPECT_EQ(10, m_lazyDecoder->repetitionCount());
285 TEST_F(DeferredImageDecoderTest, decodedSize)
287 m_decodedSize = IntSize(22, 33);
288 m_lazyDecoder->setData(m_data.get(), true);
289 RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewNativeImage();
290 EXPECT_EQ(m_decodedSize.width(), image->bitmap().width());
291 EXPECT_EQ(m_decodedSize.height(), image->bitmap().height());
292 EXPECT_FALSE(image->bitmap().isNull());
293 EXPECT_TRUE(image->bitmap().isImmutable());
295 useMockImageDecoderFactory();
297 // The following code should not fail any assert.
298 SkCanvas* tempCanvas = m_picture.beginRecording(100, 100);
299 tempCanvas->drawBitmap(image->bitmap(), 0, 0);
300 m_picture.endRecording();
301 EXPECT_EQ(0, m_frameBufferRequestCount);
302 m_canvas->drawPicture(m_picture);
303 EXPECT_EQ(1, m_frameBufferRequestCount);
306 } // namespace WebCore