~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/graphics/skia/NativeImageSkia.h

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <wtf/Forward.h>
 
38
 
 
39
namespace WebCore {
 
40
 
 
41
// This object is used as the "native image" in our port. When WebKit uses
 
42
// "NativeImagePtr", it is a pointer to this type. It has an SkBitmap, and also
 
43
// stores a cached resized image.
 
44
class NativeImageSkia {
 
45
public:
 
46
    NativeImageSkia();
 
47
    ~NativeImageSkia();
 
48
 
 
49
    // This constructor does a shallow copy of the passed-in SkBitmap (ie., it
 
50
    // references the same pixel data and bumps the refcount).  Use only when
 
51
    // you want sharing semantics.
 
52
    NativeImageSkia(const SkBitmap&, float resolutionScale);
 
53
 
 
54
    // Returns the number of bytes of image data. This includes the cached
 
55
    // resized version if there is one.
 
56
    int decodedSize() const;
 
57
 
 
58
    // Sets the immutable flag on the bitmap, indicating that the image data
 
59
    // will not be modified any further. This is called by the image decoder
 
60
    // when all data is complete, used by us to know whether we can cache
 
61
    // resized images, and used by Skia for various optimizations.
 
62
    void setDataComplete() { m_image.setImmutable(); }
 
63
 
 
64
    // Returns true if the entire image has been decoded.
 
65
    bool isDataComplete() const { return m_image.isImmutable(); }
 
66
 
 
67
    // Get reference to the internal SkBitmap representing this image.
 
68
    const SkBitmap& bitmap() const { return m_image; }
 
69
    SkBitmap& bitmap() { return m_image; }
 
70
 
 
71
    float resolutionScale() const { return m_resolutionScale; }
 
72
 
 
73
    // We can keep a resized version of the bitmap cached on this object.
 
74
    // This function will return true if there is a cached version of the given
 
75
    // scale and subset.
 
76
    bool hasResizedBitmap(const SkISize& scaledImageSize, const SkIRect& scaledImageSubset) const;
 
77
 
 
78
    // This will return an existing resized image subset, or generate a new one
 
79
    // of the specified size and subset and possibly cache it.
 
80
    //
 
81
    // scaledImageSize
 
82
    // Dimensions of the scaled full image.
 
83
    //
 
84
    // scaledImageSubset
 
85
    // Rectangle of the subset in the scaled image.
 
86
    SkBitmap resizedBitmap(const SkISize& scaledImageSize, const SkIRect& scaledImageSubset) const;
 
87
 
 
88
    void reportMemoryUsage(MemoryObjectInfo*) const;
 
89
 
 
90
private:
 
91
    // CachedImageInfo is used to uniquely identify cached or requested image
 
92
    // resizes.
 
93
    // Image resize is identified by the scaled image size and scaled image subset.
 
94
    struct CachedImageInfo {
 
95
        SkISize scaledImageSize;
 
96
        SkIRect scaledImageSubset;
 
97
 
 
98
        CachedImageInfo();
 
99
 
 
100
        bool isEqual(const SkISize& otherScaledImageSize, const SkIRect& otherScaledImageSubset) const;
 
101
        void set(const SkISize& otherScaledImageSize, const SkIRect& otherScaledImageSubset);
 
102
        SkIRect rectInSubset(const SkIRect& otherScaledImageRect);
 
103
    };
 
104
 
 
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
 
107
    // away.
 
108
    //
 
109
    // Calling this function may increment a request count that can change the
 
110
    // result of subsequent calls.
 
111
    //
 
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 SkISize& scaledImageSize, const SkIRect& scaledImageSubset) const;
 
119
 
 
120
    // The original image.
 
121
    SkBitmap m_image;
 
122
    float m_resolutionScale;
 
123
 
 
124
    // The cached bitmap fragment. This is a subset of the scaled version of
 
125
    // |m_image|. empty() returns true if there is no cached image.
 
126
    mutable SkBitmap m_resizedImage;
 
127
 
 
128
    // References how many times that the image size has been requested for
 
129
    // the last size.
 
130
    //
 
131
    // Every time we get a call to shouldCacheResampling, if it matches the
 
132
    // m_cachedImageInfo, we'll increment the counter, and if not, we'll reset
 
133
    // the counter and save the dimensions.
 
134
    //
 
135
    // This allows us to see if many requests have been made for the same
 
136
    // resized image, we know that we should probably cache it, even if all of
 
137
    // those requests individually are small and would not otherwise be cached.
 
138
    //
 
139
    // We also track scaling information and destination subset for the scaled
 
140
    // image. See comments for CachedImageInfo.
 
141
    mutable CachedImageInfo m_cachedImageInfo;
 
142
    mutable int m_resizeRequests;
 
143
};
 
144
 
 
145
}
 
146
#endif  // NativeImageSkia_h