| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2008, Google Inc. All rights reserved. | 2 * Copyright (c) 2008, Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * 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 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #ifndef NativeImageSkia_h | 31 #ifndef NativeImageSkia_h |
| 32 #define NativeImageSkia_h | 32 #define NativeImageSkia_h |
| 33 | 33 |
| 34 #include "SkBitmap.h" | |
| 35 #include "SkRect.h" | |
| 36 #include "SkSize.h" | |
| 37 #include "SkXfermode.h" | |
| 38 #include "platform/PlatformExport.h" | 34 #include "platform/PlatformExport.h" |
| 39 #include "platform/geometry/IntSize.h" | |
| 40 #include "platform/graphics/GraphicsTypes.h" | 35 #include "platform/graphics/GraphicsTypes.h" |
| 41 #include "wtf/Forward.h" | 36 #include "public/platform/WebBlendMode.h" |
| 37 #include "third_party/skia/include/core/SkBitmap.h" |
| 42 #include "wtf/PassRefPtr.h" | 38 #include "wtf/PassRefPtr.h" |
| 43 #include "wtf/RefCounted.h" | 39 #include "wtf/RefCounted.h" |
| 44 | 40 |
| 41 struct SkRect; |
| 42 |
| 45 namespace blink { | 43 namespace blink { |
| 46 | 44 |
| 47 class FloatPoint; | 45 class FloatPoint; |
| 48 class FloatRect; | 46 class FloatRect; |
| 49 class FloatSize; | 47 class FloatSize; |
| 48 class IntSize; |
| 50 class GraphicsContext; | 49 class GraphicsContext; |
| 51 | 50 |
| 52 // This object is used as the "native image" in our port. When WebKit uses | |
| 53 // PassNativeImagePtr / NativeImagePtr, it is a smart pointer to this type. | |
| 54 // It has an SkBitmap, and also stores a cached resized image. | |
| 55 class PLATFORM_EXPORT NativeImageSkia : public RefCounted<NativeImageSkia> { | 51 class PLATFORM_EXPORT NativeImageSkia : public RefCounted<NativeImageSkia> { |
| 56 public: | 52 public: |
| 57 static PassRefPtr<NativeImageSkia> create() | 53 static PassRefPtr<NativeImageSkia> create() |
| 58 { | 54 { |
| 59 return adoptRef(new NativeImageSkia()); | 55 return adoptRef(new NativeImageSkia()); |
| 60 } | 56 } |
| 61 | 57 |
| 62 // This factory method does a shallow copy of the passed-in SkBitmap | 58 // This factory method does a shallow copy of the passed-in SkBitmap |
| 63 // (ie., it references the same pixel data and bumps the refcount). Use | 59 // (ie., it references the same pixel data and bumps the refcount). Use |
| 64 // only when you want sharing semantics. | 60 // only when you want sharing semantics. |
| 65 static PassRefPtr<NativeImageSkia> create(const SkBitmap& bitmap) | 61 static PassRefPtr<NativeImageSkia> create(const SkBitmap& bitmap) |
| 66 { | 62 { |
| 67 return adoptRef(new NativeImageSkia(bitmap)); | 63 return adoptRef(new NativeImageSkia(bitmap)); |
| 68 } | 64 } |
| 69 | 65 |
| 70 ~NativeImageSkia(); | |
| 71 | |
| 72 // Returns true if the entire image has been decoded. | 66 // Returns true if the entire image has been decoded. |
| 73 bool isDataComplete() const { return bitmap().isImmutable(); } | 67 bool isDataComplete() const { return bitmap().isImmutable(); } |
| 74 | 68 |
| 75 // Get reference to the internal SkBitmap representing this image. | 69 // Get reference to the internal SkBitmap representing this image. |
| 76 const SkBitmap& bitmap() const { return m_bitmap; } | 70 const SkBitmap& bitmap() const { return m_bitmap; } |
| 77 | 71 |
| 78 // We can keep a resized version of the bitmap cached on this object. | |
| 79 // This function will return true if there is a cached version of the given | |
| 80 // scale and subset. | |
| 81 bool hasResizedBitmap(const SkISize& scaledImageSize, const SkIRect& scaledI
mageSubset) const; | |
| 82 | |
| 83 // This will return an existing resized image subset, or generate a new one | |
| 84 // of the specified size and subset and possibly cache it. | |
| 85 // | |
| 86 // scaledImageSize | |
| 87 // Dimensions of the scaled full image. | |
| 88 // | |
| 89 // scaledImageSubset | |
| 90 // Rectangle of the subset in the scaled image. | |
| 91 SkBitmap resizedBitmap(const SkISize& scaledImageSize, const SkIRect& scaled
ImageSubset) const; | |
| 92 | |
| 93 void draw( | 72 void draw( |
| 94 GraphicsContext*, | 73 GraphicsContext*, |
| 95 const SkRect& srcRect, | 74 const SkRect& srcRect, |
| 96 const SkRect& destRect, | 75 const SkRect& destRect, |
| 97 CompositeOperator, | 76 CompositeOperator, |
| 98 WebBlendMode) const; | 77 WebBlendMode) const; |
| 99 | 78 |
| 100 void drawPattern( | 79 void drawPattern( |
| 101 GraphicsContext*, | 80 GraphicsContext*, |
| 102 const FloatRect& srcRect, | 81 const FloatRect& srcRect, |
| 103 const FloatSize& scale, | 82 const FloatSize& scale, |
| 104 const FloatPoint& phase, | 83 const FloatPoint& phase, |
| 105 CompositeOperator, | 84 CompositeOperator, |
| 106 const FloatRect& destRect, | 85 const FloatRect& destRect, |
| 107 WebBlendMode, | 86 WebBlendMode, |
| 108 const IntSize& repeatSpacing) const; | 87 const IntSize& repeatSpacing) const; |
| 109 | 88 |
| 110 private: | 89 private: |
| 111 NativeImageSkia(); | 90 NativeImageSkia() { } |
| 91 explicit NativeImageSkia(const SkBitmap& bitmap) : m_bitmap(bitmap) { } |
| 112 | 92 |
| 113 NativeImageSkia(const SkBitmap&); | |
| 114 | |
| 115 // ImageResourceInfo is used to uniquely identify cached or requested image | |
| 116 // resizes. | |
| 117 // Image resize is identified by the scaled image size and scaled image subs
et. | |
| 118 struct ImageResourceInfo { | |
| 119 SkISize scaledImageSize; | |
| 120 SkIRect scaledImageSubset; | |
| 121 | |
| 122 ImageResourceInfo(); | |
| 123 | |
| 124 bool isEqual(const SkISize& otherScaledImageSize, const SkIRect& otherSc
aledImageSubset) const; | |
| 125 void set(const SkISize& otherScaledImageSize, const SkIRect& otherScaled
ImageSubset); | |
| 126 SkIRect rectInSubset(const SkIRect& otherScaledImageRect); | |
| 127 }; | |
| 128 | |
| 129 // Returns true if the given resize operation should either resize the whole | |
| 130 // image and cache it, or resize just the part it needs and throw the result | |
| 131 // away. | |
| 132 // | |
| 133 // Calling this function may increment a request count that can change the | |
| 134 // result of subsequent calls. | |
| 135 // | |
| 136 // On the one hand, if only a small subset is desired, then we will waste a | |
| 137 // lot of time resampling the entire thing, so we only want to do exactly | |
| 138 // what's required. On the other hand, resampling the entire bitmap is | |
| 139 // better if we're going to be using it more than once (like a bitmap | |
| 140 // scrolling on and off the screen. Since we only cache when doing the | |
| 141 // entire thing, it's best to just do it up front. | |
| 142 bool shouldCacheResampling(const SkISize& scaledImageSize, const SkIRect& sc
aledImageSubset) const; | |
| 143 | |
| 144 SkBitmap extractScaledImageFragment(const SkRect& srcRect, float scaleX, flo
at scaleY, SkRect* scaledSrcRect) const; | |
| 145 | |
| 146 // The original image. | |
| 147 SkBitmap m_bitmap; | 93 SkBitmap m_bitmap; |
| 148 | |
| 149 // The cached bitmap fragment. This is a subset of the scaled version of | |
| 150 // |m_bitmap|. empty() returns true if there is no cached image. | |
| 151 mutable SkBitmap m_resizedImage; | |
| 152 | |
| 153 // References how many times that the image size has been requested for | |
| 154 // the last size. | |
| 155 // | |
| 156 // Every time we get a call to shouldCacheResampling, if it matches the | |
| 157 // m_cachedImageInfo, we'll increment the counter, and if not, we'll reset | |
| 158 // the counter and save the dimensions. | |
| 159 // | |
| 160 // This allows us to see if many requests have been made for the same | |
| 161 // resized image, we know that we should probably cache it, even if all of | |
| 162 // those requests individually are small and would not otherwise be cached. | |
| 163 // | |
| 164 // We also track scaling information and destination subset for the scaled | |
| 165 // image. See comments for ImageResourceInfo. | |
| 166 mutable ImageResourceInfo m_cachedImageInfo; | |
| 167 mutable int m_resizeRequests; | |
| 168 }; | 94 }; |
| 169 | 95 |
| 170 } // namespace blink | 96 } // namespace blink |
| 171 | 97 |
| 172 #endif // NativeImageSkia_h | 98 #endif // NativeImageSkia_h |
| OLD | NEW |