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/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"
44
45namespace blink {
46
47class FloatPoint;
48class FloatRect;
49class FloatSize;
50class GraphicsContext;
51
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.
55class PLATFORM_EXPORT NativeImageSkia : public RefCounted<NativeImageSkia> {
56public:
57    static PassRefPtr<NativeImageSkia> create()
58    {
59        return adoptRef(new NativeImageSkia());
60    }
61
62    // 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
64    // only when you want sharing semantics.
65    static PassRefPtr<NativeImageSkia> create(const SkBitmap& bitmap)
66    {
67        return adoptRef(new NativeImageSkia(bitmap));
68    }
69
70    ~NativeImageSkia();
71
72    // Returns true if the entire image has been decoded.
73    bool isDataComplete() const { return bitmap().isImmutable(); }
74
75    // Get reference to the internal SkBitmap representing this image.
76    const SkBitmap& bitmap() const { return m_bitmap; }
77
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& scaledImageSubset) 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& scaledImageSubset) const;
92
93    void draw(
94        GraphicsContext*,
95        const SkRect& srcRect,
96        const SkRect& destRect,
97        CompositeOperator,
98        WebBlendMode) const;
99
100    void drawPattern(
101        GraphicsContext*,
102        const FloatRect& srcRect,
103        const FloatSize& scale,
104        const FloatPoint& phase,
105        CompositeOperator,
106        const FloatRect& destRect,
107        WebBlendMode,
108        const IntSize& repeatSpacing) const;
109
110private:
111    NativeImageSkia();
112
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 subset.
118    struct ImageResourceInfo {
119        SkISize scaledImageSize;
120        SkIRect scaledImageSubset;
121
122        ImageResourceInfo();
123
124        bool isEqual(const SkISize& otherScaledImageSize, const SkIRect& otherScaledImageSubset) const;
125        void set(const SkISize& otherScaledImageSize, const SkIRect& otherScaledImageSubset);
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& scaledImageSubset) const;
143
144    SkBitmap extractScaledImageFragment(const SkRect& srcRect, float scaleX, float scaleY, SkRect* scaledSrcRect) const;
145
146    // The original image.
147    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};
169
170} // namespace blink
171
172#endif  // NativeImageSkia_h
173