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 "platform/image-decoders/ImageDecoder.h" 29 30 namespace WebCore { 31 32 ImageFrame::ImageFrame() 33 : m_bitmap(NativeImageSkia::create()) 34 , m_allocator(0) 35 , m_hasAlpha(false) 36 , m_status(FrameEmpty) 37 , m_duration(0) 38 , m_disposalMethod(DisposeNotSpecified) 39 , m_alphaBlendSource(BlendAtopPreviousFrame) 40 , m_premultiplyAlpha(true) 41 , m_pixelsChanged(false) 42 , m_requiredPreviousFrameIndex(kNotFound) 43 #if !ASSERT_DISABLED 44 , m_requiredPreviousFrameIndexValid(false) 45 #endif 46 { 47 } 48 49 ImageFrame& ImageFrame::operator=(const ImageFrame& other) 50 { 51 if (this == &other) 52 return *this; 53 54 m_bitmap = other.m_bitmap->clone(); 55 // Keep the pixels locked since we will be writing directly into the 56 // bitmap throughout this object's lifetime. 57 m_bitmap->bitmap().lockPixels(); 58 // Be sure to assign this before calling setStatus(), since setStatus() may 59 // call notifyBitmapIfPixelsChanged(). 60 m_pixelsChanged = other.m_pixelsChanged; 61 setMemoryAllocator(other.allocator()); 62 setOriginalFrameRect(other.originalFrameRect()); 63 setStatus(other.status()); 64 setDuration(other.duration()); 65 setDisposalMethod(other.disposalMethod()); 66 setAlphaBlendSource(other.alphaBlendSource()); 67 setPremultiplyAlpha(other.premultiplyAlpha()); 68 // Be sure that this is called after we've called setStatus(), since we 69 // look at our status to know what to do with the alpha value. 70 setHasAlpha(other.hasAlpha()); 71 // Copy raw fields to avoid ASSERT failure in requiredPreviousFrameIndex(). 72 m_requiredPreviousFrameIndex = other.m_requiredPreviousFrameIndex; 73 #if !ASSERT_DISABLED 74 m_requiredPreviousFrameIndexValid = other.m_requiredPreviousFrameIndexValid; 75 #endif 76 return *this; 77 } 78 79 void ImageFrame::clearPixelData() 80 { 81 m_bitmap->bitmap().reset(); 82 m_status = FrameEmpty; 83 // NOTE: Do not reset other members here; clearFrameBufferCache() 84 // calls this to free the bitmap data, but other functions like 85 // initFrameBuffer() and frameComplete() may still need to read 86 // other metadata out of this frame later. 87 } 88 89 void ImageFrame::zeroFillPixelData() 90 { 91 m_bitmap->bitmap().eraseARGB(0, 0, 0, 0); 92 m_hasAlpha = true; 93 } 94 95 bool ImageFrame::copyBitmapData(const ImageFrame& other) 96 { 97 if (this == &other) 98 return true; 99 100 m_hasAlpha = other.m_hasAlpha; 101 m_bitmap->bitmap().reset(); 102 const NativeImageSkia* otherBitmap = other.m_bitmap.get(); 103 return otherBitmap->bitmap().copyTo(&m_bitmap->bitmap(), otherBitmap->bitmap().config()); 104 } 105 106 bool ImageFrame::setSize(int newWidth, int newHeight) 107 { 108 // setSize() should only be called once, it leaks memory otherwise. 109 ASSERT(!width() && !height()); 110 111 m_bitmap->bitmap().setConfig(SkBitmap::kARGB_8888_Config, newWidth, newHeight); 112 if (!m_bitmap->bitmap().allocPixels(m_allocator, 0)) 113 return false; 114 115 zeroFillPixelData(); 116 return true; 117 } 118 119 PassRefPtr<NativeImageSkia> ImageFrame::asNewNativeImage() const 120 { 121 return m_bitmap->clone(); 122 } 123 124 bool ImageFrame::hasAlpha() const 125 { 126 return m_hasAlpha; 127 } 128 129 void ImageFrame::setHasAlpha(bool alpha) 130 { 131 m_hasAlpha = alpha; 132 133 // If the frame is not fully loaded, there will be transparent pixels, 134 // so we can't tell skia we're opaque, even for image types that logically 135 // always are (e.g. jpeg). 136 if (m_status != FrameComplete) 137 alpha = true; 138 m_bitmap->bitmap().setAlphaType(alpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType); 139 } 140 141 void ImageFrame::setStatus(Status status) 142 { 143 m_status = status; 144 if (m_status == FrameComplete) { 145 m_bitmap->bitmap().setAlphaType(m_hasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType); 146 // Send pending pixels changed notifications now, because we can't do this after 147 // the bitmap was set immutable by setDataComplete(). 148 notifyBitmapIfPixelsChanged(); 149 m_bitmap->setDataComplete(); // Tell the bitmap it's done. 150 } 151 } 152 153 void ImageFrame::zeroFillFrameRect(const IntRect& rect) 154 { 155 if (rect.isEmpty()) 156 return; 157 158 m_bitmap->bitmap().eraseArea(rect, SkColorSetARGB(0, 0, 0, 0)); 159 setHasAlpha(true); 160 } 161 162 } // namespace WebCore 163