Home | History | Annotate | Download | only in chromium
      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 "core/platform/graphics/chromium/DeferredImageDecoder.h"
     29 
     30 #include "SkCanvas.h"
     31 #include "SkDevice.h"
     32 #include "SkPicture.h"
     33 #include "core/platform/SharedBuffer.h"
     34 #include "core/platform/graphics/chromium/ImageDecodingStore.h"
     35 #include "core/platform/graphics/chromium/test/MockImageDecoder.h"
     36 #include "core/platform/graphics/skia/NativeImageSkia.h"
     37 #include "wtf/PassRefPtr.h"
     38 #include "wtf/RefPtr.h"
     39 #include <gtest/gtest.h>
     40 
     41 using namespace WebCore;
     42 
     43 namespace {
     44 
     45 // Raw data for a PNG file with 1x1 white pixels.
     46 const unsigned char whitePNG[] = {
     47     0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00,
     48     0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01,
     49     0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90,
     50     0x77, 0x53, 0xde, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47,
     51     0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, 0x00, 0x09,
     52     0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0b, 0x13, 0x00, 0x00,
     53     0x0b, 0x13, 0x01, 0x00, 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x00,
     54     0x0c, 0x49, 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xff,
     55     0xff, 0x3f, 0x00, 0x05, 0xfe, 0x02, 0xfe, 0xdc, 0xcc, 0x59,
     56     0xe7, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
     57     0x42, 0x60, 0x82,
     58 };
     59 
     60 static SkCanvas* createRasterCanvas(int width, int height)
     61 {
     62     SkAutoTUnref<SkDevice> device(new SkDevice(SkBitmap::kARGB_8888_Config, width, height));
     63     return new SkCanvas(device);
     64 }
     65 
     66 struct Rasterizer {
     67     SkCanvas* canvas;
     68     SkPicture* picture;
     69 };
     70 
     71 class DeferredImageDecoderTest : public ::testing::Test, public MockImageDecoderClient {
     72 public:
     73     virtual void SetUp()
     74     {
     75         ImageDecodingStore::initializeOnce();
     76         DeferredImageDecoder::setEnabled(true);
     77         m_data = SharedBuffer::create(whitePNG, sizeof(whitePNG));
     78         OwnPtr<MockImageDecoder> decoder = MockImageDecoder::create(this);
     79         m_actualDecoder = decoder.get();
     80         m_actualDecoder->setSize(1, 1);
     81         m_lazyDecoder = DeferredImageDecoder::createForTesting(decoder.release());
     82         m_canvas.reset(createRasterCanvas(100, 100));
     83         m_frameBufferRequestCount = 0;
     84         m_frameCount = 1;
     85         m_repetitionCount = cAnimationNone;
     86         m_frameStatus = ImageFrame::FrameComplete;
     87         m_frameDuration = 0;
     88     }
     89 
     90     virtual void TearDown()
     91     {
     92         ImageDecodingStore::shutdown();
     93     }
     94 
     95     virtual void decoderBeingDestroyed()
     96     {
     97         m_actualDecoder = 0;
     98     }
     99 
    100     virtual void frameBufferRequested()
    101     {
    102         ++m_frameBufferRequestCount;
    103     }
    104 
    105     virtual size_t frameCount()
    106     {
    107         return m_frameCount;
    108     }
    109 
    110     virtual int repetitionCount() const
    111     {
    112         return m_repetitionCount;
    113     }
    114 
    115     virtual ImageFrame::FrameStatus frameStatus()
    116     {
    117         return m_frameStatus;
    118     }
    119 
    120     virtual float frameDuration() const
    121     {
    122         return m_frameDuration;
    123     }
    124 
    125 protected:
    126     // Don't own this but saves the pointer to query states.
    127     MockImageDecoder* m_actualDecoder;
    128     OwnPtr<DeferredImageDecoder> m_lazyDecoder;
    129     SkPicture m_picture;
    130     SkAutoTUnref<SkCanvas> m_canvas;
    131     int m_frameBufferRequestCount;
    132     RefPtr<SharedBuffer> m_data;
    133     size_t m_frameCount;
    134     int m_repetitionCount;
    135     ImageFrame::FrameStatus m_frameStatus;
    136     float m_frameDuration;
    137 };
    138 
    139 TEST_F(DeferredImageDecoderTest, drawIntoSkPicture)
    140 {
    141     m_lazyDecoder->setData(m_data.get(), true);
    142     RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewNativeImage();
    143     EXPECT_EQ(1, image->bitmap().width());
    144     EXPECT_EQ(1, image->bitmap().height());
    145     EXPECT_FALSE(image->bitmap().isNull());
    146     EXPECT_TRUE(image->bitmap().isImmutable());
    147 
    148     SkCanvas* tempCanvas = m_picture.beginRecording(100, 100);
    149     tempCanvas->drawBitmap(image->bitmap(), 0, 0);
    150     m_picture.endRecording();
    151     EXPECT_EQ(0, m_frameBufferRequestCount);
    152 
    153     m_canvas->drawPicture(m_picture);
    154     EXPECT_EQ(0, m_frameBufferRequestCount);
    155 
    156     SkBitmap canvasBitmap;
    157     canvasBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
    158     ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0));
    159     SkAutoLockPixels autoLock(canvasBitmap);
    160     EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
    161 }
    162 
    163 TEST_F(DeferredImageDecoderTest, DISABLED_drawScaledIntoSkPicture)
    164 {
    165     m_lazyDecoder->setData(m_data.get(), true);
    166     RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewNativeImage();
    167     SkBitmap scaledBitmap = image->resizedBitmap(SkISize::Make(50, 51), SkIRect::MakeWH(50, 51));
    168     EXPECT_FALSE(scaledBitmap.isNull());
    169     EXPECT_TRUE(scaledBitmap.isImmutable());
    170     EXPECT_EQ(50, scaledBitmap.width());
    171     EXPECT_EQ(51, scaledBitmap.height());
    172     EXPECT_EQ(0, m_frameBufferRequestCount);
    173 
    174     SkCanvas* tempCanvas = m_picture.beginRecording(100, 100);
    175     tempCanvas->drawBitmap(scaledBitmap, 0, 0);
    176     m_picture.endRecording();
    177     EXPECT_EQ(0, m_frameBufferRequestCount);
    178 
    179     m_canvas->drawPicture(m_picture);
    180     EXPECT_EQ(0, m_frameBufferRequestCount);
    181 
    182     SkBitmap canvasBitmap;
    183     canvasBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
    184     ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0));
    185     SkAutoLockPixels autoLock(canvasBitmap);
    186     EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
    187     EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(49, 50));
    188 }
    189 
    190 static void rasterizeMain(void* arg)
    191 {
    192     Rasterizer* rasterizer = static_cast<Rasterizer*>(arg);
    193     rasterizer->canvas->drawPicture(*rasterizer->picture);
    194 }
    195 
    196 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread)
    197 {
    198     m_lazyDecoder->setData(m_data.get(), true);
    199     RefPtr<NativeImageSkia> image = m_lazyDecoder->frameBufferAtIndex(0)->asNewNativeImage();
    200     EXPECT_EQ(1, image->bitmap().width());
    201     EXPECT_EQ(1, image->bitmap().height());
    202     EXPECT_FALSE(image->bitmap().isNull());
    203     EXPECT_TRUE(image->bitmap().isImmutable());
    204 
    205     SkCanvas* tempCanvas = m_picture.beginRecording(100, 100);
    206     tempCanvas->drawBitmap(image->bitmap(), 0, 0);
    207     m_picture.endRecording();
    208     EXPECT_EQ(0, m_frameBufferRequestCount);
    209 
    210     // Create a thread to rasterize SkPicture.
    211     Rasterizer rasterizer;
    212     rasterizer.canvas = m_canvas;
    213     rasterizer.picture = &m_picture;
    214     ThreadIdentifier threadID = createThread(&rasterizeMain, &rasterizer, "RasterThread");
    215     waitForThreadCompletion(threadID);
    216     EXPECT_EQ(0, m_frameBufferRequestCount);
    217 
    218     SkBitmap canvasBitmap;
    219     canvasBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
    220     ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0));
    221     SkAutoLockPixels autoLock(canvasBitmap);
    222     EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
    223 }
    224 
    225 TEST_F(DeferredImageDecoderTest, singleFrameImageLoading)
    226 {
    227     m_frameStatus = ImageFrame::FramePartial;
    228     m_lazyDecoder->setData(m_data.get(), false);
    229     EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0));
    230     ImageFrame* frame = m_lazyDecoder->frameBufferAtIndex(0);
    231     EXPECT_EQ(ImageFrame::FramePartial, frame->status());
    232     EXPECT_TRUE(m_actualDecoder);
    233 
    234     m_frameStatus = ImageFrame::FrameComplete;
    235     m_lazyDecoder->setData(m_data.get(), true);
    236     EXPECT_FALSE(m_actualDecoder);
    237     EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0));
    238     frame = m_lazyDecoder->frameBufferAtIndex(0);
    239     EXPECT_EQ(ImageFrame::FrameComplete, frame->status());
    240     EXPECT_FALSE(m_frameBufferRequestCount);
    241 }
    242 
    243 TEST_F(DeferredImageDecoderTest, multiFrameImageLoading)
    244 {
    245     m_repetitionCount = 10;
    246     m_frameCount = 1;
    247     m_frameDuration = 10;
    248     m_frameStatus = ImageFrame::FramePartial;
    249     m_lazyDecoder->setData(m_data.get(), false);
    250     EXPECT_EQ(ImageFrame::FramePartial, m_lazyDecoder->frameBufferAtIndex(0)->status());
    251     EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0));
    252     EXPECT_EQ(10.0f, m_lazyDecoder->frameBufferAtIndex(0)->duration());
    253     EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0));
    254 
    255     m_frameCount = 2;
    256     m_frameDuration = 20;
    257     m_frameStatus = ImageFrame::FrameComplete;
    258     m_lazyDecoder->setData(m_data.get(), false);
    259     EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(0)->status());
    260     EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(1)->status());
    261     EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0));
    262     EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1));
    263     EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1));
    264     EXPECT_EQ(10.0f, m_lazyDecoder->frameBufferAtIndex(0)->duration());
    265     EXPECT_EQ(20.0f, m_lazyDecoder->frameBufferAtIndex(1)->duration());
    266     EXPECT_TRUE(m_actualDecoder);
    267 
    268     m_frameCount = 3;
    269     m_frameDuration = 30;
    270     m_frameStatus = ImageFrame::FrameComplete;
    271     m_lazyDecoder->setData(m_data.get(), true);
    272     EXPECT_FALSE(m_actualDecoder);
    273     EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(0)->status());
    274     EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(1)->status());
    275     EXPECT_EQ(ImageFrame::FrameComplete, m_lazyDecoder->frameBufferAtIndex(2)->status());
    276     EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0));
    277     EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1));
    278     EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(2));
    279     EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0));
    280     EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1));
    281     EXPECT_EQ(30.0f, m_lazyDecoder->frameDurationAtIndex(2));
    282     EXPECT_EQ(10.0f, m_lazyDecoder->frameBufferAtIndex(0)->duration());
    283     EXPECT_EQ(20.0f, m_lazyDecoder->frameBufferAtIndex(1)->duration());
    284     EXPECT_EQ(30.0f, m_lazyDecoder->frameBufferAtIndex(2)->duration());
    285     EXPECT_EQ(10, m_lazyDecoder->repetitionCount());
    286 }
    287 
    288 } // namespace
    289