| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2008, Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 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 | |
| 13 * distribution. | |
| 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. | |
| 17 * | |
| 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. | |
| 29 */ | |
| 30 | |
| 31 #ifndef NativeImageSkia_h | |
| 32 #define NativeImageSkia_h | |
| 33 | |
| 34 #include "SkBitmap.h" | |
| 35 #include "SkRect.h" | |
| 36 #include "SkSize.h" | |
| 37 #include "SkXfermode.h" | |
| 38 #include "platform/geometry/IntSize.h" | |
| 39 #include "platform/graphics/GraphicsTypes.h" | |
| 40 #include "wtf/Forward.h" | |
| 41 #include "wtf/PassRefPtr.h" | |
| 42 #include "wtf/RefCounted.h" | |
| 43 | |
| 44 class SkMatrix; | |
| 45 class SkPaint; | |
| 46 | |
| 47 namespace WebCore { | |
| 48 | |
| 49 class FloatPoint; | |
| 50 class FloatRect; | |
| 51 class FloatSize; | |
| 52 class GraphicsContext; | |
| 53 | |
| 54 // Used by computeResamplingMode to tell how bitmaps should be resampled. | |
| 55 enum ResamplingMode { | |
| 56 // Nearest neighbor resampling. Used when we detect that the page is | |
| 57 // trying to make a pattern by stretching a small bitmap very large. | |
| 58 NoResampling, | |
| 59 | |
| 60 // Default skia resampling. Used for large growing of images where high | |
| 61 // quality resampling doesn't get us very much except a slowdown. | |
| 62 LinearResampling, | |
| 63 | |
| 64 // High quality resampling. | |
| 65 AwesomeResampling, | |
| 66 }; | |
| 67 | |
| 68 // This object is used as the "native image" in our port. When WebKit uses | |
| 69 // PassNativeImagePtr / NativeImagePtr, it is a smart pointer to this type. | |
| 70 // It has an SkBitmap, and also stores a cached resized image. | |
| 71 class NativeImageSkia : public RefCounted<NativeImageSkia> { | |
| 72 public: | |
| 73 static PassRefPtr<NativeImageSkia> create() | |
| 74 { | |
| 75 return adoptRef(new NativeImageSkia()); | |
| 76 } | |
| 77 | |
| 78 // This factory method does a shallow copy of the passed-in SkBitmap | |
| 79 // (ie., it references the same pixel data and bumps the refcount). Use | |
| 80 // only when you want sharing semantics. | |
| 81 static PassRefPtr<NativeImageSkia> create(const SkBitmap& bitmap, float reso
lutionScale = 1) | |
| 82 { | |
| 83 return adoptRef(new NativeImageSkia(bitmap, resolutionScale)); | |
| 84 } | |
| 85 | |
| 86 // This method does a shallow copy of the internal SkBitmap (ie., it | |
| 87 // references the same pixel data and bumps the refcount). Use only when | |
| 88 // you want sharing semantics. | |
| 89 PassRefPtr<NativeImageSkia> clone() const | |
| 90 { | |
| 91 return adoptRef(new NativeImageSkia(m_image, m_resolutionScale, m_resize
dImage, m_cachedImageInfo, m_resizeRequests)); | |
| 92 } | |
| 93 | |
| 94 ~NativeImageSkia(); | |
| 95 | |
| 96 // Returns the number of bytes of image data. This includes the cached | |
| 97 // resized version if there is one. | |
| 98 int decodedSize() const; | |
| 99 | |
| 100 // Sets the immutable flag on the bitmap, indicating that the image data | |
| 101 // will not be modified any further. This is called by the image decoder | |
| 102 // when all data is complete, used by us to know whether we can cache | |
| 103 // resized images, and used by Skia for various optimizations. | |
| 104 void setDataComplete() { m_image.setImmutable(); } | |
| 105 | |
| 106 // Returns true if the entire image has been decoded. | |
| 107 bool isDataComplete() const { return m_image.isImmutable(); } | |
| 108 | |
| 109 // Get reference to the internal SkBitmap representing this image. | |
| 110 const SkBitmap& bitmap() const { return m_image; } | |
| 111 SkBitmap& bitmap() { return m_image; } | |
| 112 | |
| 113 float resolutionScale() const { return m_resolutionScale; } | |
| 114 | |
| 115 // We can keep a resized version of the bitmap cached on this object. | |
| 116 // This function will return true if there is a cached version of the given | |
| 117 // scale and subset. | |
| 118 bool hasResizedBitmap(const SkISize& scaledImageSize, const SkIRect& scaledI
mageSubset) const; | |
| 119 | |
| 120 // This will return an existing resized image subset, or generate a new one | |
| 121 // of the specified size and subset and possibly cache it. | |
| 122 // | |
| 123 // scaledImageSize | |
| 124 // Dimensions of the scaled full image. | |
| 125 // | |
| 126 // scaledImageSubset | |
| 127 // Rectangle of the subset in the scaled image. | |
| 128 SkBitmap resizedBitmap(const SkISize& scaledImageSize, const SkIRect& scaled
ImageSubset) const; | |
| 129 | |
| 130 void draw(GraphicsContext*, const SkRect& srcRect, const SkRect& destRect, P
assRefPtr<SkXfermode>) const; | |
| 131 void drawPattern( | |
| 132 GraphicsContext*, | |
| 133 const FloatRect& srcRect, | |
| 134 const FloatSize& scale, | |
| 135 const FloatPoint& phase, | |
| 136 CompositeOperator, | |
| 137 const FloatRect& destRect, | |
| 138 blink::WebBlendMode, | |
| 139 const IntSize& repeatSpacing) const; | |
| 140 | |
| 141 private: | |
| 142 NativeImageSkia(); | |
| 143 | |
| 144 NativeImageSkia(const SkBitmap&, float resolutionScale); | |
| 145 | |
| 146 // ImageResourceInfo is used to uniquely identify cached or requested image | |
| 147 // resizes. | |
| 148 // Image resize is identified by the scaled image size and scaled image subs
et. | |
| 149 struct ImageResourceInfo { | |
| 150 SkISize scaledImageSize; | |
| 151 SkIRect scaledImageSubset; | |
| 152 | |
| 153 ImageResourceInfo(); | |
| 154 | |
| 155 bool isEqual(const SkISize& otherScaledImageSize, const SkIRect& otherSc
aledImageSubset) const; | |
| 156 void set(const SkISize& otherScaledImageSize, const SkIRect& otherScaled
ImageSubset); | |
| 157 SkIRect rectInSubset(const SkIRect& otherScaledImageRect); | |
| 158 }; | |
| 159 | |
| 160 NativeImageSkia(const SkBitmap& image, float resolutionScale, const SkBitmap
& resizedImage, const ImageResourceInfo&, int resizeRequests); | |
| 161 | |
| 162 // Returns true if the given resize operation should either resize the whole | |
| 163 // image and cache it, or resize just the part it needs and throw the result | |
| 164 // away. | |
| 165 // | |
| 166 // Calling this function may increment a request count that can change the | |
| 167 // result of subsequent calls. | |
| 168 // | |
| 169 // On the one hand, if only a small subset is desired, then we will waste a | |
| 170 // lot of time resampling the entire thing, so we only want to do exactly | |
| 171 // what's required. On the other hand, resampling the entire bitmap is | |
| 172 // better if we're going to be using it more than once (like a bitmap | |
| 173 // scrolling on and off the screen. Since we only cache when doing the | |
| 174 // entire thing, it's best to just do it up front. | |
| 175 bool shouldCacheResampling(const SkISize& scaledImageSize, const SkIRect& sc
aledImageSubset) const; | |
| 176 | |
| 177 ResamplingMode computeResamplingMode(const SkMatrix&, float srcWidth, float
srcHeight, float destWidth, float destHeight) const; | |
| 178 SkBitmap extractScaledImageFragment(const SkRect& srcRect, float scaleX, flo
at scaleY, SkRect* scaledSrcRect) const; | |
| 179 void drawResampledBitmap(GraphicsContext*, SkPaint&, const SkRect& srcRect,
const SkRect& destRect) const; | |
| 180 | |
| 181 // The original image. | |
| 182 SkBitmap m_image; | |
| 183 float m_resolutionScale; | |
| 184 | |
| 185 // The cached bitmap fragment. This is a subset of the scaled version of | |
| 186 // |m_image|. empty() returns true if there is no cached image. | |
| 187 mutable SkBitmap m_resizedImage; | |
| 188 | |
| 189 // References how many times that the image size has been requested for | |
| 190 // the last size. | |
| 191 // | |
| 192 // Every time we get a call to shouldCacheResampling, if it matches the | |
| 193 // m_cachedImageInfo, we'll increment the counter, and if not, we'll reset | |
| 194 // the counter and save the dimensions. | |
| 195 // | |
| 196 // This allows us to see if many requests have been made for the same | |
| 197 // resized image, we know that we should probably cache it, even if all of | |
| 198 // those requests individually are small and would not otherwise be cached. | |
| 199 // | |
| 200 // We also track scaling information and destination subset for the scaled | |
| 201 // image. See comments for ImageResourceInfo. | |
| 202 mutable ImageResourceInfo m_cachedImageInfo; | |
| 203 mutable int m_resizeRequests; | |
| 204 }; | |
| 205 | |
| 206 } | |
| 207 #endif // NativeImageSkia_h | |
| OLD | NEW |