• 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 #include "config.h"
32 
33 #if !PLATFORM(ANDROID)
34 #include "skia/ext/image_operations.h"
35 #endif
36 
37 #include "NativeImageSkia.h"
38 #include "SkiaUtils.h"
39 
NativeImageSkia()40 NativeImageSkia::NativeImageSkia()
41     : m_isDataComplete(false),
42       m_lastRequestSize(0, 0),
43       m_resizeRequests(0)
44 {
45 }
46 
decodedSize() const47 int NativeImageSkia::decodedSize() const
48 {
49     return getSize() + m_resizedImage.getSize();
50 }
51 
hasResizedBitmap(int w,int h) const52 bool NativeImageSkia::hasResizedBitmap(int w, int h) const
53 {
54     if (m_lastRequestSize.width() == w && m_lastRequestSize.height() == h)
55         m_resizeRequests++;
56     else {
57         m_lastRequestSize = WebCore::IntSize(w, h);
58         m_resizeRequests = 0;
59     }
60 
61     return m_resizedImage.width() == w && m_resizedImage.height() == h;
62 }
63 
64 // FIXME: don't cache when image is in-progress.
65 
resizedBitmap(int w,int h) const66 SkBitmap NativeImageSkia::resizedBitmap(int w, int h) const
67 {
68 #if !PLATFORM(ANDROID)
69     if (m_resizedImage.width() != w || m_resizedImage.height() != h)
70         m_resizedImage = skia::ImageOperations::Resize(*this, skia::ImageOperations::RESIZE_LANCZOS3, w, h);
71 #endif
72 
73     return m_resizedImage;
74 }
75 
shouldCacheResampling(int destWidth,int destHeight,int destSubsetWidth,int destSubsetHeight) const76 bool NativeImageSkia::shouldCacheResampling(int destWidth,
77                                             int destHeight,
78                                             int destSubsetWidth,
79                                             int destSubsetHeight) const
80 {
81     // We can not cache incomplete frames. This might be a good optimization in
82     // the future, were we know how much of the frame has been decoded, so when
83     // we incrementally draw more of the image, we only have to resample the
84     // parts that are changed.
85     if (!m_isDataComplete)
86         return false;
87 
88     // If the destination bitmap is small, we'll always allow caching, since
89     // there is not very much penalty for computing it and it may come in handy.
90     static const int kSmallBitmapSize = 4096;
91     if (destWidth * destHeight <= kSmallBitmapSize)
92         return true;
93 
94     // If "too many" requests have been made for this bitmap, we assume that
95     // many more will be made as well, and we'll go ahead and cache it.
96     static const int kManyRequestThreshold = 4;
97     if (m_lastRequestSize.width() == destWidth &&
98         m_lastRequestSize.height() == destHeight) {
99         if (m_resizeRequests >= kManyRequestThreshold)
100             return true;
101     } else {
102         // When a different size is being requested, count this as a query
103         // (hasResizedBitmap) and reset the counter.
104         m_lastRequestSize = WebCore::IntSize(destWidth, destHeight);
105         m_resizeRequests = 0;
106     }
107 
108     // Otherwise, use the heuristic that if more than 1/4 of the image is
109     // requested, it's worth caching.
110     int destSize = destWidth * destHeight;
111     int destSubsetSize = destSubsetWidth * destSubsetHeight;
112     return destSize / 4 < destSubsetSize;
113 }
114