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 namespace WebCore { 34 35 RGBA32Buffer::RGBA32Buffer() 36 : m_status(FrameEmpty) 37 , m_duration(0) 38 , m_disposalMethod(DisposeNotSpecified) 39 { 40 } 41 42 void RGBA32Buffer::clear() 43 { 44 m_bitmap.reset(); 45 m_status = FrameEmpty; 46 // NOTE: Do not reset other members here; clearFrameBufferCache() 47 // calls this to free the bitmap data, but other functions like 48 // initFrameBuffer() and frameComplete() may still need to read 49 // other metadata out of this frame later. 50 } 51 52 void RGBA32Buffer::zeroFill() 53 { 54 m_bitmap.eraseARGB(0, 0, 0, 0); 55 } 56 57 void RGBA32Buffer::copyBitmapData(const RGBA32Buffer& other) 58 { 59 if (this == &other) 60 return; 61 62 m_bitmap.reset(); 63 const NativeImageSkia& otherBitmap = other.m_bitmap; 64 otherBitmap.copyTo(&m_bitmap, otherBitmap.config()); 65 } 66 67 bool RGBA32Buffer::setSize(int newWidth, int newHeight) 68 { 69 // This function should only be called once, it will leak memory 70 // otherwise. 71 ASSERT(width() == 0 && height() == 0); 72 m_bitmap.setConfig(SkBitmap::kARGB_8888_Config, newWidth, newHeight); 73 if (!m_bitmap.allocPixels()) 74 return false; 75 76 // Zero the image. 77 zeroFill(); 78 79 return true; 80 } 81 82 NativeImagePtr RGBA32Buffer::asNewNativeImage() const 83 { 84 #if PLATFORM(ANDROID) 85 return new SkBitmapRef(m_bitmap); 86 #else 87 return new NativeImageSkia(m_bitmap); 88 #endif 89 } 90 91 bool RGBA32Buffer::hasAlpha() const 92 { 93 return !m_bitmap.isOpaque(); 94 } 95 96 void RGBA32Buffer::setHasAlpha(bool alpha) 97 { 98 m_bitmap.setIsOpaque(!alpha); 99 } 100 101 void RGBA32Buffer::setStatus(FrameStatus status) 102 { 103 m_status = status; 104 if (m_status == FrameComplete) 105 m_bitmap.setDataComplete(); // Tell the bitmap it's done. 106 } 107 108 RGBA32Buffer& RGBA32Buffer::operator=(const RGBA32Buffer& other) 109 { 110 if (this == &other) 111 return *this; 112 113 m_bitmap = other.m_bitmap; 114 // Keep the pixels locked since we will be writing directly into the 115 // bitmap throughout this object's lifetime. 116 m_bitmap.lockPixels(); 117 setRect(other.rect()); 118 setStatus(other.status()); 119 setDuration(other.duration()); 120 setDisposalMethod(other.disposalMethod()); 121 return *this; 122 } 123 124 int RGBA32Buffer::width() const 125 { 126 return m_bitmap.width(); 127 } 128 129 int RGBA32Buffer::height() const 130 { 131 return m_bitmap.height(); 132 } 133 134 } // namespace WebCore 135