• 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 "SharedGraphicsContext3D.h"
39 #include "SkiaUtils.h"
40 
41 namespace WebCore {
42 
NativeImageSkia()43 NativeImageSkia::NativeImageSkia()
44     : m_isDataComplete(false),
45       m_lastRequestSize(0, 0),
46       m_resizeRequests(0)
47 {
48 }
49 
NativeImageSkia(const SkBitmap & other)50 NativeImageSkia::NativeImageSkia(const SkBitmap& other)
51     : SkBitmap(other),
52       m_isDataComplete(false),
53       m_lastRequestSize(0, 0),
54       m_resizeRequests(0)
55 {
56 }
57 
58 
~NativeImageSkia()59 NativeImageSkia::~NativeImageSkia()
60 {
61 #if PLATFORM(ANDROID)
62     // SharedGraphicsContext3D::removeTexturesFor() takes a NativeImagePtr. On
63     // Chromium, this is NativeImageSkia, which inherits from SkBitmap. On
64     // Android, NativeImagePtr is a SkBitmapRef, which is a wrapper around
65     // SkBitmap. Failing to call removeTexturesFor() probably causes a leak.
66     // TODO: Fix this. See http://b/3047425
67 #else
68     SharedGraphicsContext3D::removeTexturesFor(this);
69 #endif
70 }
71 
decodedSize() const72 int NativeImageSkia::decodedSize() const
73 {
74     return getSize() + m_resizedImage.getSize();
75 }
76 
hasResizedBitmap(int w,int h) const77 bool NativeImageSkia::hasResizedBitmap(int w, int h) const
78 {
79     if (m_lastRequestSize.width() == w && m_lastRequestSize.height() == h)
80         m_resizeRequests++;
81     else {
82         m_lastRequestSize = IntSize(w, h);
83         m_resizeRequests = 0;
84     }
85 
86     return m_resizedImage.width() == w && m_resizedImage.height() == h;
87 }
88 
89 // FIXME: don't cache when image is in-progress.
90 
resizedBitmap(int w,int h) const91 SkBitmap NativeImageSkia::resizedBitmap(int w, int h) const
92 {
93 #if !PLATFORM(ANDROID)
94     if (m_resizedImage.width() != w || m_resizedImage.height() != h)
95         m_resizedImage = skia::ImageOperations::Resize(*this, skia::ImageOperations::RESIZE_LANCZOS3, w, h);
96 #endif
97 
98     return m_resizedImage;
99 }
100 
shouldCacheResampling(int destWidth,int destHeight,int destSubsetWidth,int destSubsetHeight) const101 bool NativeImageSkia::shouldCacheResampling(int destWidth,
102                                             int destHeight,
103                                             int destSubsetWidth,
104                                             int destSubsetHeight) const
105 {
106     // We can not cache incomplete frames. This might be a good optimization in
107     // the future, were we know how much of the frame has been decoded, so when
108     // we incrementally draw more of the image, we only have to resample the
109     // parts that are changed.
110     if (!m_isDataComplete)
111         return false;
112 
113     // If the destination bitmap is small, we'll always allow caching, since
114     // there is not very much penalty for computing it and it may come in handy.
115     static const int kSmallBitmapSize = 4096;
116     if (destWidth * destHeight <= kSmallBitmapSize)
117         return true;
118 
119     // If "too many" requests have been made for this bitmap, we assume that
120     // many more will be made as well, and we'll go ahead and cache it.
121     static const int kManyRequestThreshold = 4;
122     if (m_lastRequestSize.width() == destWidth &&
123         m_lastRequestSize.height() == destHeight) {
124         if (m_resizeRequests >= kManyRequestThreshold)
125             return true;
126     } else {
127         // When a different size is being requested, count this as a query
128         // (hasResizedBitmap) and reset the counter.
129         m_lastRequestSize = IntSize(destWidth, destHeight);
130         m_resizeRequests = 0;
131     }
132 
133     // Otherwise, use the heuristic that if more than 1/4 of the image is
134     // requested, it's worth caching.
135     int destSize = destWidth * destHeight;
136     int destSubsetSize = destSubsetWidth * destSubsetHeight;
137     return destSize / 4 < destSubsetSize;
138 }
139 
140 } // namespace WebCore
141