• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "IntSize.h"
36 
37 // This object is used as the "native image" in our port. When WebKit uses
38 // "NativeImagePtr", it is a pointer to this type. It is an SkBitmap, but also
39 // stores a cached resized image.
40 class NativeImageSkia : public SkBitmap {
41 public:
42     NativeImageSkia();
43 
44     // Returns the number of bytes of image data. This includes the cached
45     // resized version if there is one.
46     int decodedSize() const;
47 
48     // Sets the data complete flag. This is called by the image decoder when
49     // all data is complete, and used by us to know whether we can cache
50     // resized images.
setDataComplete()51     void setDataComplete() { m_isDataComplete = true; }
52 
53     // Returns true if the entire image has been decoded.
isDataComplete()54     bool isDataComplete() const { return m_isDataComplete; }
55 
56     // We can keep a resized version of the bitmap cached on this object.
57     // This function will return true if there is a cached version of the
58     // given image subset with the given dimensions.
59     bool hasResizedBitmap(int width, int height) const;
60 
61     // This will return an existing resized image, or generate a new one of
62     // the specified size and store it in the cache. Subsetted images can not
63     // be cached unless the subset is the entire bitmap.
64     SkBitmap resizedBitmap(int width, int height) const;
65 
66     // Returns true if the given resize operation should either resize the whole
67     // image and cache it, or resize just the part it needs and throw the result
68     // away.
69     //
70     // On the one hand, if only a small subset is desired, then we will waste a
71     // lot of time resampling the entire thing, so we only want to do exactly
72     // what's required. On the other hand, resampling the entire bitmap is
73     // better if we're going to be using it more than once (like a bitmap
74     // scrolling on and off the screen. Since we only cache when doing the
75     // entire thing, it's best to just do it up front.
76     bool shouldCacheResampling(int destWidth,
77                                int destHeight,
78                                int destSubsetWidth,
79                                int destSubsetHeight) const;
80 
81 private:
82     // Set to true when the data is complete. Before the entire image has
83     // loaded, we do not want to cache a resize.
84     bool m_isDataComplete;
85 
86     // The cached bitmap. This will be empty() if there is no cached image.
87     mutable SkBitmap m_resizedImage;
88 
89     // References how many times that the image size has been requested for
90     // the last size.
91     //
92     // Every time we get a request, if it matches the m_lastRequestSize, we'll
93     // increment the counter, and if not, we'll reset the counter and save the
94     // size.
95     //
96     // This allows us to see if many requests have been made for the same
97     // resized image, we know that we should probably cache it, even if all of
98     // those requests individually are small and would not otherwise be cached.
99     mutable WebCore::IntSize m_lastRequestSize;
100     mutable int m_resizeRequests;
101 };
102 
103 #endif  // NativeImageSkia_h
104 
105