2 * Copyright (c) 2008, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef NativeImageSkia_h
32 #define NativeImageSkia_h
37 #include "SkXfermode.h"
38 #include "platform/PlatformExport.h"
39 #include "platform/geometry/IntSize.h"
40 #include "platform/graphics/GraphicsTypes.h"
41 #include "wtf/Forward.h"
42 #include "wtf/PassRefPtr.h"
43 #include "wtf/RefCounted.h"
53 class GraphicsContext;
55 // Used by computeResamplingMode to tell how bitmaps should be resampled.
57 // Nearest neighbor resampling. Used when we detect that the page is
58 // trying to make a pattern by stretching a small bitmap very large.
61 // Default skia resampling. Used for large growing of images where high
62 // quality resampling doesn't get us very much except a slowdown.
65 // High quality resampling.
69 // This object is used as the "native image" in our port. When WebKit uses
70 // PassNativeImagePtr / NativeImagePtr, it is a smart pointer to this type.
71 // It has an SkBitmap, and also stores a cached resized image.
72 class PLATFORM_EXPORT NativeImageSkia : public RefCounted<NativeImageSkia> {
74 static PassRefPtr<NativeImageSkia> create()
76 return adoptRef(new NativeImageSkia());
79 // This factory method does a shallow copy of the passed-in SkBitmap
80 // (ie., it references the same pixel data and bumps the refcount). Use
81 // only when you want sharing semantics.
82 static PassRefPtr<NativeImageSkia> create(const SkBitmap& bitmap)
84 return adoptRef(new NativeImageSkia(bitmap));
87 // This method does a shallow copy of the internal SkBitmap (ie., it
88 // references the same pixel data and bumps the refcount). Use only when
89 // you want sharing semantics.
90 PassRefPtr<NativeImageSkia> clone() const
92 return adoptRef(new NativeImageSkia(m_image, m_resizedImage, m_cachedImageInfo, m_resizeRequests));
97 // Returns the number of bytes of image data. This includes the cached
98 // resized version if there is one.
99 int decodedSize() const;
101 // Sets the immutable flag on the bitmap, indicating that the image data
102 // will not be modified any further. This is called by the image decoder
103 // when all data is complete, used by us to know whether we can cache
104 // resized images, and used by Skia for various optimizations.
105 void setDataComplete() { m_image.setImmutable(); }
107 // Returns true if the entire image has been decoded.
108 bool isDataComplete() const { return m_image.isImmutable(); }
110 // Get reference to the internal SkBitmap representing this image.
111 const SkBitmap& bitmap() const { return m_image; }
112 SkBitmap& bitmap() { return m_image; }
114 // We can keep a resized version of the bitmap cached on this object.
115 // This function will return true if there is a cached version of the given
117 bool hasResizedBitmap(const SkISize& scaledImageSize, const SkIRect& scaledImageSubset) const;
119 // This will return an existing resized image subset, or generate a new one
120 // of the specified size and subset and possibly cache it.
123 // Dimensions of the scaled full image.
126 // Rectangle of the subset in the scaled image.
127 SkBitmap resizedBitmap(const SkISize& scaledImageSize, const SkIRect& scaledImageSubset) const;
129 void draw(GraphicsContext*, const SkRect& srcRect, const SkRect& destRect, PassRefPtr<SkXfermode>) const;
132 const FloatRect& srcRect,
133 const FloatSize& scale,
134 const FloatPoint& phase,
136 const FloatRect& destRect,
138 const IntSize& repeatSpacing) const;
143 NativeImageSkia(const SkBitmap&);
145 // ImageResourceInfo is used to uniquely identify cached or requested image
147 // Image resize is identified by the scaled image size and scaled image subset.
148 struct ImageResourceInfo {
149 SkISize scaledImageSize;
150 SkIRect scaledImageSubset;
154 bool isEqual(const SkISize& otherScaledImageSize, const SkIRect& otherScaledImageSubset) const;
155 void set(const SkISize& otherScaledImageSize, const SkIRect& otherScaledImageSubset);
156 SkIRect rectInSubset(const SkIRect& otherScaledImageRect);
159 NativeImageSkia(const SkBitmap& image, const SkBitmap& resizedImage, const ImageResourceInfo&, int resizeRequests);
161 // Returns true if the given resize operation should either resize the whole
162 // image and cache it, or resize just the part it needs and throw the result
165 // Calling this function may increment a request count that can change the
166 // result of subsequent calls.
168 // On the one hand, if only a small subset is desired, then we will waste a
169 // lot of time resampling the entire thing, so we only want to do exactly
170 // what's required. On the other hand, resampling the entire bitmap is
171 // better if we're going to be using it more than once (like a bitmap
172 // scrolling on and off the screen. Since we only cache when doing the
173 // entire thing, it's best to just do it up front.
174 bool shouldCacheResampling(const SkISize& scaledImageSize, const SkIRect& scaledImageSubset) const;
176 ResamplingMode computeResamplingMode(const SkMatrix&, float srcWidth, float srcHeight, float destWidth, float destHeight) const;
177 SkBitmap extractScaledImageFragment(const SkRect& srcRect, float scaleX, float scaleY, SkRect* scaledSrcRect) const;
178 void drawResampledBitmap(GraphicsContext*, SkPaint&, const SkRect& srcRect, const SkRect& destRect) const;
180 // The original image.
183 // The cached bitmap fragment. This is a subset of the scaled version of
184 // |m_image|. empty() returns true if there is no cached image.
185 mutable SkBitmap m_resizedImage;
187 // References how many times that the image size has been requested for
190 // Every time we get a call to shouldCacheResampling, if it matches the
191 // m_cachedImageInfo, we'll increment the counter, and if not, we'll reset
192 // the counter and save the dimensions.
194 // This allows us to see if many requests have been made for the same
195 // resized image, we know that we should probably cache it, even if all of
196 // those requests individually are small and would not otherwise be cached.
198 // We also track scaling information and destination subset for the scaled
199 // image. See comments for ImageResourceInfo.
200 mutable ImageResourceInfo m_cachedImageInfo;
201 mutable int m_resizeRequests;
205 #endif // NativeImageSkia_h