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
40 // This object is used as the "native image" in our port. When WebKit uses
41 // "NativeImagePtr", it is a pointer to this type. It has an SkBitmap, and also
42 // stores a cached resized image.
43 class NativeImageSkia {
48 // This constructor does a shallow copy of the passed-in SkBitmap (ie., it
49 // references the same pixel data and bumps the refcount). Use only when
50 // you want sharing semantics.
51 explicit NativeImageSkia(const SkBitmap&);
53 // Returns the number of bytes of image data. This includes the cached
54 // resized version if there is one.
55 int decodedSize() const;
57 // Sets the data complete flag. This is called by the image decoder when
58 // all data is complete, and used by us to know whether we can cache
60 void setDataComplete() { m_isDataComplete = true; }
62 // Returns true if the entire image has been decoded.
63 bool isDataComplete() const { return m_isDataComplete; }
65 // Get reference to the internal SkBitmap representing this image.
66 const SkBitmap& bitmap() const { return m_image; }
67 SkBitmap& bitmap() { return m_image; }
69 // We can keep a resized version of the bitmap cached on this object.
70 // This function will return true if there is a cached version of the
71 // given image subset with the given dimensions and subsets.
72 bool hasResizedBitmap(const SkIRect& srcSubset, int width, int height) const;
74 // This will return an existing resized image subset, or generate a new one
75 // of the specified size and subsets and possibly cache it.
76 // srcSubset is the subset of the image to resize in image space.
77 SkBitmap resizedBitmap(const SkIRect& srcSubset, int destWidth, int destHeight) const
79 SkIRect destVisibleSubset = {0, 0, destWidth, destHeight};
80 return resizedBitmap(srcSubset, destWidth, destHeight, destVisibleSubset);
83 // Same as above, but returns a subset of the destination image (ie: the
84 // visible subset). destVisibleSubset is the subset of the resized
85 // (destWidth x destHeight) image.
87 // - crop image by srcSubset -> imageSubset.
88 // - resize imageSubset to destWidth x destHeight -> destImage.
89 // - return destImage cropped by destVisibleSubset.
90 SkBitmap resizedBitmap(const SkIRect& srcSubset, int destWidth, int destHeight, const SkIRect& destVisibleSubset) const;
93 // CachedImageInfo is used to uniquely identify cached or requested image
95 struct CachedImageInfo {
101 bool isEqual(const SkIRect& otherSrcSubset, int width, int height) const;
102 void set(const SkIRect& otherSrcSubset, int width, int height);
105 // Returns true if the given resize operation should either resize the whole
106 // image and cache it, or resize just the part it needs and throw the result
109 // Calling this function may increment a request count that can change the
110 // result of subsequent calls.
112 // On the one hand, if only a small subset is desired, then we will waste a
113 // lot of time resampling the entire thing, so we only want to do exactly
114 // what's required. On the other hand, resampling the entire bitmap is
115 // better if we're going to be using it more than once (like a bitmap
116 // scrolling on and off the screen. Since we only cache when doing the
117 // entire thing, it's best to just do it up front.
118 bool shouldCacheResampling(const SkIRect& srcSubset,
121 const SkIRect& destSubset) const;
123 // The original image.
126 // The cached bitmap. This will be empty() if there is no cached image.
127 mutable SkBitmap m_resizedImage;
129 // References how many times that the image size has been requested for
132 // Every time we get a call to shouldCacheResampling, if it matches the
133 // m_cachedImageInfo, we'll increment the counter, and if not, we'll reset
134 // the counter and save the dimensions.
136 // This allows us to see if many requests have been made for the same
137 // resized image, we know that we should probably cache it, even if all of
138 // those requests individually are small and would not otherwise be cached.
140 // We also track the source and destination subsets for caching partial
142 mutable CachedImageInfo m_cachedImageInfo;
143 mutable int m_resizeRequests;
145 // Set to true when the data is complete. Before the entire image has
146 // loaded, we do not want to cache a resize.
147 bool m_isDataComplete;
151 #endif // NativeImageSkia_h