Home | History | Annotate | Download | only in skia
      1 /*
      2  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
      3  * Copyright (C) 2008, 2009 Google, Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      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 COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include "config.h"
     28 #include "ImageDecoder.h"
     29 #if PLATFORM(ANDROID)
     30 #include "SkBitmapRef.h"
     31 #endif
     32 
     33 #include "NotImplemented.h"
     34 
     35 namespace WebCore {
     36 
     37 ImageFrame::ImageFrame()
     38     : m_status(FrameEmpty)
     39     , m_duration(0)
     40     , m_disposalMethod(DisposeNotSpecified)
     41     , m_premultiplyAlpha(true)
     42 {
     43 }
     44 
     45 ImageFrame& ImageFrame::operator=(const ImageFrame& other)
     46 {
     47     if (this == &other)
     48         return *this;
     49 
     50     m_bitmap = other.m_bitmap;
     51     // Keep the pixels locked since we will be writing directly into the
     52     // bitmap throughout this object's lifetime.
     53     m_bitmap.lockPixels();
     54     setOriginalFrameRect(other.originalFrameRect());
     55     setStatus(other.status());
     56     setDuration(other.duration());
     57     setDisposalMethod(other.disposalMethod());
     58     setPremultiplyAlpha(other.premultiplyAlpha());
     59     return *this;
     60 }
     61 
     62 void ImageFrame::clearPixelData()
     63 {
     64     m_bitmap.reset();
     65     m_status = FrameEmpty;
     66     // NOTE: Do not reset other members here; clearFrameBufferCache()
     67     // calls this to free the bitmap data, but other functions like
     68     // initFrameBuffer() and frameComplete() may still need to read
     69     // other metadata out of this frame later.
     70 }
     71 
     72 void ImageFrame::zeroFillPixelData()
     73 {
     74     m_bitmap.eraseARGB(0, 0, 0, 0);
     75 }
     76 
     77 bool ImageFrame::copyBitmapData(const ImageFrame& other)
     78 {
     79     if (this == &other)
     80         return true;
     81 
     82     m_bitmap.reset();
     83     const NativeImageSkia& otherBitmap = other.m_bitmap;
     84     return otherBitmap.copyTo(&m_bitmap, otherBitmap.config());
     85 }
     86 
     87 bool ImageFrame::setSize(int newWidth, int newHeight)
     88 {
     89     // This function should only be called once, it will leak memory
     90     // otherwise.
     91     ASSERT(width() == 0 && height() == 0);
     92     m_bitmap.setConfig(SkBitmap::kARGB_8888_Config, newWidth, newHeight);
     93     if (!m_bitmap.allocPixels())
     94         return false;
     95 
     96     zeroFillPixelData();
     97 
     98     return true;
     99 }
    100 
    101 NativeImagePtr ImageFrame::asNewNativeImage() const
    102 {
    103 #if PLATFORM(ANDROID)
    104     return new SkBitmapRef(m_bitmap);
    105 #else
    106     return new NativeImageSkia(m_bitmap);
    107 #endif
    108 }
    109 
    110 bool ImageFrame::hasAlpha() const
    111 {
    112     return !m_bitmap.isOpaque();
    113 }
    114 
    115 void ImageFrame::setHasAlpha(bool alpha)
    116 {
    117     m_bitmap.setIsOpaque(!alpha);
    118 }
    119 
    120 void ImageFrame::setColorProfile(const ColorProfile& colorProfile)
    121 {
    122     notImplemented();
    123 }
    124 
    125 void ImageFrame::setStatus(FrameStatus status)
    126 {
    127     m_status = status;
    128     if (m_status == FrameComplete)
    129         m_bitmap.setDataComplete();  // Tell the bitmap it's done.
    130 }
    131 
    132 int ImageFrame::width() const
    133 {
    134     return m_bitmap.width();
    135 }
    136 
    137 int ImageFrame::height() const
    138 {
    139     return m_bitmap.height();
    140 }
    141 
    142 } // namespace WebCore
    143