1 /*
2 * Copyright (C) 2013 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 #include "core/rendering/ImageQualityController.h"
33
34 #include "core/frame/Frame.h"
35 #include "core/frame/FrameView.h"
36 #include "platform/graphics/GraphicsContext.h"
37
38 namespace WebCore {
39
40 static const double cLowQualityTimeThreshold = 0.500; // 500 ms
41
42 static ImageQualityController* gImageQualityController = 0;
43
imageQualityController()44 ImageQualityController* ImageQualityController::imageQualityController()
45 {
46 if (!gImageQualityController)
47 gImageQualityController = new ImageQualityController;
48
49 return gImageQualityController;
50 }
51
remove(RenderObject * renderer)52 void ImageQualityController::remove(RenderObject* renderer)
53 {
54 if (gImageQualityController) {
55 gImageQualityController->objectDestroyed(renderer);
56 if (gImageQualityController->isEmpty()) {
57 delete gImageQualityController;
58 gImageQualityController = 0;
59 }
60 }
61 }
62
~ImageQualityController()63 ImageQualityController::~ImageQualityController()
64 {
65 // This will catch users of ImageQualityController that forget to call cleanUp.
66 ASSERT(!gImageQualityController || gImageQualityController->isEmpty());
67 }
68
ImageQualityController()69 ImageQualityController::ImageQualityController()
70 : m_timer(this, &ImageQualityController::highQualityRepaintTimerFired)
71 , m_animatedResizeIsActive(false)
72 , m_liveResizeOptimizationIsActive(false)
73 {
74 }
75
removeLayer(RenderObject * object,LayerSizeMap * innerMap,const void * layer)76 void ImageQualityController::removeLayer(RenderObject* object, LayerSizeMap* innerMap, const void* layer)
77 {
78 if (innerMap) {
79 innerMap->remove(layer);
80 if (innerMap->isEmpty())
81 objectDestroyed(object);
82 }
83 }
84
set(RenderObject * object,LayerSizeMap * innerMap,const void * layer,const LayoutSize & size)85 void ImageQualityController::set(RenderObject* object, LayerSizeMap* innerMap, const void* layer, const LayoutSize& size)
86 {
87 if (innerMap)
88 innerMap->set(layer, size);
89 else {
90 LayerSizeMap newInnerMap;
91 newInnerMap.set(layer, size);
92 m_objectLayerSizeMap.set(object, newInnerMap);
93 }
94 }
95
objectDestroyed(RenderObject * object)96 void ImageQualityController::objectDestroyed(RenderObject* object)
97 {
98 m_objectLayerSizeMap.remove(object);
99 if (m_objectLayerSizeMap.isEmpty()) {
100 m_animatedResizeIsActive = false;
101 m_timer.stop();
102 }
103 }
104
highQualityRepaintTimerFired(Timer<ImageQualityController> *)105 void ImageQualityController::highQualityRepaintTimerFired(Timer<ImageQualityController>*)
106 {
107 if (!m_animatedResizeIsActive && !m_liveResizeOptimizationIsActive)
108 return;
109 m_animatedResizeIsActive = false;
110
111 for (ObjectLayerSizeMap::iterator it = m_objectLayerSizeMap.begin(); it != m_objectLayerSizeMap.end(); ++it) {
112 if (Frame* frame = it->key->document().frame()) {
113 // If this renderer's containing FrameView is in live resize, punt the timer and hold back for now.
114 if (frame->view() && frame->view()->inLiveResize()) {
115 restartTimer();
116 return;
117 }
118 }
119 it->key->repaint();
120 }
121
122 m_liveResizeOptimizationIsActive = false;
123 }
124
restartTimer()125 void ImageQualityController::restartTimer()
126 {
127 m_timer.startOneShot(cLowQualityTimeThreshold);
128 }
129
shouldPaintAtLowQuality(GraphicsContext * context,RenderObject * object,Image * image,const void * layer,const LayoutSize & layoutSize)130 bool ImageQualityController::shouldPaintAtLowQuality(GraphicsContext* context, RenderObject* object, Image* image, const void *layer, const LayoutSize& layoutSize)
131 {
132 // If the image is not a bitmap image, then none of this is relevant and we just paint at high
133 // quality.
134 if (!image || !image->isBitmapImage() || context->paintingDisabled())
135 return false;
136
137 if (object->style()->imageRendering() == ImageRenderingOptimizeContrast)
138 return true;
139
140 // Look ourselves up in the hashtables.
141 ObjectLayerSizeMap::iterator i = m_objectLayerSizeMap.find(object);
142 LayerSizeMap* innerMap = i != m_objectLayerSizeMap.end() ? &i->value : 0;
143 LayoutSize oldSize;
144 bool isFirstResize = true;
145 if (innerMap) {
146 LayerSizeMap::iterator j = innerMap->find(layer);
147 if (j != innerMap->end()) {
148 isFirstResize = false;
149 oldSize = j->value;
150 }
151 }
152
153 const AffineTransform& currentTransform = context->getCTM();
154 bool contextIsScaled = !currentTransform.isIdentityOrTranslationOrFlipped();
155
156 // Make sure to use the unzoomed image size, since if a full page zoom is in effect, the image
157 // is actually being scaled.
158 LayoutSize scaledImageSize = currentTransform.mapSize(image->size());
159 LayoutSize scaledLayoutSize = currentTransform.mapSize(roundedIntSize(layoutSize));
160
161 // If the containing FrameView is being resized, paint at low quality until resizing is finished.
162 if (Frame* frame = object->document().frame()) {
163 bool frameViewIsCurrentlyInLiveResize = frame->view() && frame->view()->inLiveResize();
164 if (frameViewIsCurrentlyInLiveResize) {
165 set(object, innerMap, layer, scaledLayoutSize);
166 restartTimer();
167 m_liveResizeOptimizationIsActive = true;
168 return true;
169 }
170 if (m_liveResizeOptimizationIsActive) {
171 // Live resize has ended, paint in HQ and remove this object from the list.
172 removeLayer(object, innerMap, layer);
173 return false;
174 }
175 }
176
177 if (!contextIsScaled && scaledLayoutSize == scaledImageSize) {
178 // There is no scale in effect. If we had a scale in effect before, we can just remove this object from the list.
179 removeLayer(object, innerMap, layer);
180 return false;
181 }
182
183 // If an animated resize is active, paint in low quality and kick the timer ahead.
184 if (m_animatedResizeIsActive) {
185 set(object, innerMap, layer, scaledLayoutSize);
186 restartTimer();
187 return true;
188 }
189 // If this is the first time resizing this image, or its size is the
190 // same as the last resize, draw at high res, but record the paint
191 // size and set the timer.
192 if (isFirstResize || oldSize == scaledLayoutSize) {
193 restartTimer();
194 set(object, innerMap, layer, scaledLayoutSize);
195 return false;
196 }
197 // If the timer is no longer active, draw at high quality and don't
198 // set the timer.
199 if (!m_timer.isActive()) {
200 removeLayer(object, innerMap, layer);
201 return false;
202 }
203 // This object has been resized to two different sizes while the timer
204 // is active, so draw at low quality, set the flag for animated resizes and
205 // the object to the list for high quality redraw.
206 set(object, innerMap, layer, scaledLayoutSize);
207 m_animatedResizeIsActive = true;
208 restartTimer();
209 return true;
210 }
211
212 } // namespace WebCore
213