WinCairo build fails to link.
[WebKit-https.git] / Source / WebKit / chromium / tests / DeferredImageDecoderTest.cpp
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
28 #include "DeferredImageDecoder.h"
29
30 #include "ImageDecodingStore.h"
31 #include "MockImageDecoder.h"
32 #include "NativeImageSkia.h"
33 #include "SharedBuffer.h"
34 #include "SkCanvas.h"
35 #include "SkDevice.h"
36 #include "SkPicture.h"
37 #include <gtest/gtest.h>
38 #include <wtf/PassRefPtr.h>
39 #include <wtf/RefPtr.h>
40 #include <wtf/Threading.h>
41
42 using namespace WebCore;
43
44 namespace {
45
46 // Raw data for a PNG file with 1x1 white pixels.
47 const unsigned char whitePNG[] = {
48     0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00,
49     0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01,
50     0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90,
51     0x77, 0x53, 0xde, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47,
52     0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, 0x00, 0x09,
53     0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0b, 0x13, 0x00, 0x00,
54     0x0b, 0x13, 0x01, 0x00, 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x00,
55     0x0c, 0x49, 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xff,
56     0xff, 0x3f, 0x00, 0x05, 0xfe, 0x02, 0xfe, 0xdc, 0xcc, 0x59,
57     0xe7, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
58     0x42, 0x60, 0x82,
59 };
60
61 static SkCanvas* createRasterCanvas(int width, int height)
62 {
63     SkAutoTUnref<SkDevice> device(new SkDevice(SkBitmap::kARGB_8888_Config, width, height));
64     return new SkCanvas(device);
65 }
66
67 struct Rasterizer {
68     SkCanvas* canvas;
69     SkPicture* picture;
70 };
71
72 class DeferredImageDecoderTest : public ::testing::Test, public MockImageDecoderClient {
73 public:
74     virtual void SetUp()
75     {
76         ImageDecodingStore::initializeOnce();
77         DeferredImageDecoder::setEnabled(true);
78         m_data = SharedBuffer::create(whitePNG, sizeof(whitePNG));
79         OwnPtr<MockImageDecoder> decoder = MockImageDecoder::create(this);
80         m_actualDecoder = decoder.get();
81         m_actualDecoder->setSize(1, 1);
82         m_lazyDecoder = DeferredImageDecoder::createForTesting(decoder.release());
83         m_lazyDecoder->setData(m_data.get(), true);
84         m_canvas.reset(createRasterCanvas(100, 100));
85         m_frameBufferRequestCount = 0;
86     }
87
88     virtual void TearDown()
89     {
90         ImageDecodingStore::shutdown();
91     }
92
93     virtual void decoderBeingDestroyed()
94     {
95         m_actualDecoder = 0;
96     }
97
98     virtual void frameBufferRequested()
99     {
100         ++m_frameBufferRequestCount;
101     }
102
103     virtual ImageFrame::FrameStatus frameStatus()
104     {
105         return ImageFrame::FrameComplete;
106     }
107
108 protected:
109     // Don't own this but saves the pointer to query states.
110     MockImageDecoder* m_actualDecoder;
111     OwnPtr<DeferredImageDecoder> m_lazyDecoder;
112     SkPicture m_picture;
113     SkAutoTUnref<SkCanvas> m_canvas;
114     int m_frameBufferRequestCount;
115     RefPtr<SharedBuffer> m_data;
116 };
117
118 TEST_F(DeferredImageDecoderTest, drawIntoSkPicture)
119 {
120     RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewNativeImage();
121     EXPECT_EQ(1, image->bitmap().width());
122     EXPECT_EQ(1, image->bitmap().height());
123     EXPECT_FALSE(image->bitmap().isNull());
124     EXPECT_TRUE(image->bitmap().isImmutable());
125
126     SkCanvas* tempCanvas = m_picture.beginRecording(100, 100);
127     tempCanvas->drawBitmap(image->bitmap(), 0, 0);
128     m_picture.endRecording();
129     EXPECT_EQ(0, m_frameBufferRequestCount);
130
131     m_canvas->drawPicture(m_picture);
132     EXPECT_EQ(0, m_frameBufferRequestCount);
133
134     SkBitmap canvasBitmap;
135     canvasBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
136     ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0));
137     SkAutoLockPixels autoLock(canvasBitmap);
138     EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
139 }
140
141 TEST_F(DeferredImageDecoderTest, drawScaledIntoSkPicture)
142 {
143     RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewNativeImage();
144     SkBitmap scaledBitmap = image->resizedBitmap(SkISize::Make(50, 51), SkIRect::MakeWH(50, 51));
145     EXPECT_FALSE(scaledBitmap.isNull());
146     EXPECT_TRUE(scaledBitmap.isImmutable());
147     EXPECT_EQ(50, scaledBitmap.width());
148     EXPECT_EQ(51, scaledBitmap.height());
149     EXPECT_EQ(0, m_frameBufferRequestCount);
150
151     SkCanvas* tempCanvas = m_picture.beginRecording(100, 100);
152     tempCanvas->drawBitmap(scaledBitmap, 0, 0);
153     m_picture.endRecording();
154     EXPECT_EQ(0, m_frameBufferRequestCount);
155
156     m_canvas->drawPicture(m_picture);
157     EXPECT_EQ(0, m_frameBufferRequestCount);
158
159     SkBitmap canvasBitmap;
160     canvasBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
161     ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0));
162     SkAutoLockPixels autoLock(canvasBitmap);
163     EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
164     EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(49, 50));
165 }
166
167 static void rasterizeMain(void* arg)
168 {
169     Rasterizer* rasterizer = static_cast<Rasterizer*>(arg);
170     rasterizer->canvas->drawPicture(*rasterizer->picture);
171 }
172
173 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread)
174 {
175     WTF::initializeThreading();
176
177     RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewNativeImage();
178     EXPECT_EQ(1, image->bitmap().width());
179     EXPECT_EQ(1, image->bitmap().height());
180     EXPECT_FALSE(image->bitmap().isNull());
181     EXPECT_TRUE(image->bitmap().isImmutable());
182
183     SkCanvas* tempCanvas = m_picture.beginRecording(100, 100);
184     tempCanvas->drawBitmap(image->bitmap(), 0, 0);
185     m_picture.endRecording();
186     EXPECT_EQ(0, m_frameBufferRequestCount);
187
188     // Create a thread to rasterize SkPicture.
189     Rasterizer rasterizer;
190     rasterizer.canvas = m_canvas;
191     rasterizer.picture = &m_picture;
192     ThreadIdentifier threadID = createThread(&rasterizeMain, &rasterizer, "RasterThread");
193     waitForThreadCompletion(threadID);
194     EXPECT_EQ(0, m_frameBufferRequestCount);
195
196     SkBitmap canvasBitmap;
197     canvasBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
198     ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0));
199     SkAutoLockPixels autoLock(canvasBitmap);
200     EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
201 }
202
203 } // namespace