Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(946)

Side by Side Diff: third_party/WebKit/Source/platform/image-decoders/ImageFrame.cpp

Issue 2155973002: Save a bitmap copy when advancing to dependent GIF and WebP animation frames (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: finalizePixelsAndGetImage. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2008, 2009 Google, Inc. 3 * Copyright (C) 2008, 2009 Google, Inc.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 12 * documentation and/or other materials provided with the distribution.
13 * 13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */ 25 */
26 26
27 #include "platform/image-decoders/ImageFrame.h" 27 #include "platform/image-decoders/ImageFrame.h"
28 28
29 #include "platform/RuntimeEnabledFeatures.h" 29 #include "platform/RuntimeEnabledFeatures.h"
30 #include "platform/graphics/skia/SkiaUtils.h"
30 #include "platform/image-decoders/ImageDecoder.h" 31 #include "platform/image-decoders/ImageDecoder.h"
31 32
32 namespace blink { 33 namespace blink {
33 34
34 ImageFrame::ImageFrame() 35 ImageFrame::ImageFrame()
35 : m_allocator(0) 36 : m_allocator(0)
36 , m_hasAlpha(true) 37 , m_hasAlpha(true)
37 , m_status(FrameEmpty) 38 , m_status(FrameEmpty)
38 , m_duration(0) 39 , m_duration(0)
39 , m_disposalMethod(DisposeNotSpecified) 40 , m_disposalMethod(DisposeNotSpecified)
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 bool ImageFrame::copyBitmapData(const ImageFrame& other) 90 bool ImageFrame::copyBitmapData(const ImageFrame& other)
90 { 91 {
91 if (this == &other) 92 if (this == &other)
92 return true; 93 return true;
93 94
94 m_hasAlpha = other.m_hasAlpha; 95 m_hasAlpha = other.m_hasAlpha;
95 m_bitmap.reset(); 96 m_bitmap.reset();
96 return other.m_bitmap.copyTo(&m_bitmap, other.m_bitmap.colorType()); 97 return other.m_bitmap.copyTo(&m_bitmap, other.m_bitmap.colorType());
97 } 98 }
98 99
100 bool ImageFrame::takeBitmapDataIfWritable(ImageFrame* other)
101 {
102 DCHECK(other);
103 if (other->m_bitmap.isImmutable() || this == other)
Peter Kasting 2016/08/30 06:59:40 Tiny efficiency nit: Reverse order of checks
aleksandar.stojiljkovic 2016/09/21 20:56:57 It isn't obvious when/if this could happen (this =
Peter Kasting 2016/09/21 21:54:49 I suppose you could make this (and maybe that func
aleksandar.stojiljkovic 2016/09/22 09:41:19 Done.
104 return false;
105 m_hasAlpha = other->m_hasAlpha;
106 m_bitmap.reset();
107 m_bitmap.swap(other->m_bitmap);
108 other->m_status = FrameEmpty;
109 return true;
110 }
111
99 bool ImageFrame::setSizeAndColorProfile(int newWidth, int newHeight, const ICCPr ofile& newIccProfile) 112 bool ImageFrame::setSizeAndColorProfile(int newWidth, int newHeight, const ICCPr ofile& newIccProfile)
100 { 113 {
101 // setSizeAndColorProfile() should only be called once, it leaks memory othe rwise. 114 // setSizeAndColorProfile() should only be called once, it leaks memory othe rwise.
102 ASSERT(!width() && !height()); 115 ASSERT(!width() && !height());
103 116
104 sk_sp<SkColorSpace> colorSpace; 117 sk_sp<SkColorSpace> colorSpace;
105 if (RuntimeEnabledFeatures::colorCorrectRenderingEnabled() && !newIccProfile .isEmpty()) 118 if (RuntimeEnabledFeatures::colorCorrectRenderingEnabled() && !newIccProfile .isEmpty())
106 colorSpace = SkColorSpace::NewICC(newIccProfile.data(), newIccProfile.si ze()); 119 colorSpace = SkColorSpace::NewICC(newIccProfile.data(), newIccProfile.si ze());
107 120
108 m_bitmap.setInfo(SkImageInfo::MakeN32(newWidth, newHeight, 121 m_bitmap.setInfo(SkImageInfo::MakeN32(newWidth, newHeight,
109 m_premultiplyAlpha ? kPremul_SkAlphaType : kUnpremul_SkAlphaType, colorS pace)); 122 m_premultiplyAlpha ? kPremul_SkAlphaType : kUnpremul_SkAlphaType, colorS pace));
110 if (!m_bitmap.tryAllocPixels(m_allocator, 0)) 123 if (!m_bitmap.tryAllocPixels(m_allocator, 0))
111 return false; 124 return false;
112 125
113 zeroFillPixelData(); 126 zeroFillPixelData();
114 return true; 127 return true;
115 } 128 }
116 129
117 bool ImageFrame::hasAlpha() const 130 bool ImageFrame::hasAlpha() const
118 { 131 {
119 return m_hasAlpha; 132 return m_hasAlpha;
120 } 133 }
121 134
135 const SkBitmap& ImageFrame::bitmap() const
136 {
137 // We don't want to set the status to immutable here as we do in
138 // finalizePixelsAndGetImage() because mutable bitmap is reusable as the
139 // next animation frame by calling takeBitmapDataIfWritable.
Peter Kasting 2016/08/30 06:59:40 Nit: The header comments should already justify an
aleksandar.stojiljkovic 2016/09/21 20:56:57 Done.
140 return m_bitmap;
141 }
142
143 PassRefPtr<SkImage> ImageFrame::finalizePixelsAndGetImage()
144 {
145 DCHECK(m_status != FrameComplete || (!m_bitmap.isNull() && !m_bitmap.empty() ));
146
147 // We don't set the status to immutable until there is a request to get
148 // SkImage and decoding is done. We set the bitmap to immutable here to
149 // avoid copying in SkImage::MakeFromBitmap.
150 if (!m_bitmap.isImmutable() && m_status == FrameComplete)
151 m_bitmap.setImmutable();
152 return fromSkSp(SkImage::MakeFromBitmap(m_bitmap));
153 }
154
122 void ImageFrame::setHasAlpha(bool alpha) 155 void ImageFrame::setHasAlpha(bool alpha)
123 { 156 {
124 m_hasAlpha = alpha; 157 m_hasAlpha = alpha;
125 158
126 m_bitmap.setAlphaType(computeAlphaType()); 159 m_bitmap.setAlphaType(computeAlphaType());
127 } 160 }
128 161
129 void ImageFrame::setStatus(Status status) 162 void ImageFrame::setStatus(Status status)
130 { 163 {
131 m_status = status; 164 m_status = status;
132 if (m_status == FrameComplete) { 165 if (m_status == FrameComplete) {
133 m_bitmap.setAlphaType(computeAlphaType()); 166 m_bitmap.setAlphaType(computeAlphaType());
134 // Send pending pixels changed notifications now, because we can't do th is after 167 // Send pending pixels changed notifications now, because we can't do th is after
135 // the bitmap has been marked immutable. 168 // the bitmap has been marked immutable.
136 notifyBitmapIfPixelsChanged(); 169 notifyBitmapIfPixelsChanged();
137 m_bitmap.setImmutable(); // Tell the bitmap it's done.
138 } 170 }
139 } 171 }
140 172
141 void ImageFrame::zeroFillFrameRect(const IntRect& rect) 173 void ImageFrame::zeroFillFrameRect(const IntRect& rect)
142 { 174 {
143 if (rect.isEmpty()) 175 if (rect.isEmpty())
144 return; 176 return;
145 177
146 m_bitmap.eraseArea(rect, SkColorSetARGB(0, 0, 0, 0)); 178 m_bitmap.eraseArea(rect, SkColorSetARGB(0, 0, 0, 0));
147 setHasAlpha(true); 179 setHasAlpha(true);
148 } 180 }
149 181
150 SkAlphaType ImageFrame::computeAlphaType() const 182 SkAlphaType ImageFrame::computeAlphaType() const
151 { 183 {
152 // If the frame is not fully loaded, there will be transparent pixels, 184 // If the frame is not fully loaded, there will be transparent pixels,
153 // so we can't tell skia we're opaque, even for image types that logically 185 // so we can't tell skia we're opaque, even for image types that logically
154 // always are (e.g. jpeg). 186 // always are (e.g. jpeg).
155 if (!m_hasAlpha && m_status == FrameComplete) 187 if (!m_hasAlpha && m_status == FrameComplete)
156 return kOpaque_SkAlphaType; 188 return kOpaque_SkAlphaType;
157 189
158 return m_premultiplyAlpha ? kPremul_SkAlphaType : kUnpremul_SkAlphaType; 190 return m_premultiplyAlpha ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
159 } 191 }
160 192
161 } // namespace blink 193 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698