2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/layout/ImageQualityController.h"
34 #include "core/frame/FrameView.h"
35 #include "core/frame/LocalFrame.h"
36 #include "platform/graphics/GraphicsContext.h"
40 static const double cLowQualityTimeThreshold
= 0.500; // 500 ms
42 static ImageQualityController
* gImageQualityController
= nullptr;
44 ImageQualityController
* ImageQualityController::imageQualityController()
46 if (!gImageQualityController
)
47 gImageQualityController
= new ImageQualityController
;
49 return gImageQualityController
;
52 void ImageQualityController::remove(LayoutObject
* layoutObject
)
54 if (gImageQualityController
) {
55 gImageQualityController
->objectDestroyed(layoutObject
);
56 if (gImageQualityController
->isEmpty()) {
57 delete gImageQualityController
;
58 gImageQualityController
= nullptr;
63 bool ImageQualityController::has(LayoutObject
* layoutObject
)
65 return gImageQualityController
&& gImageQualityController
->m_objectLayerSizeMap
.contains(layoutObject
);
68 InterpolationQuality
ImageQualityController::chooseInterpolationQuality(GraphicsContext
* context
, LayoutObject
* object
, Image
* image
, const void* layer
, const LayoutSize
& layoutSize
)
70 if (object
->style()->imageRendering() == ImageRenderingPixelated
)
71 return InterpolationNone
;
73 if (InterpolationDefault
== InterpolationLow
)
74 return InterpolationLow
;
76 if (shouldPaintAtLowQuality(context
, object
, image
, layer
, layoutSize
))
77 return InterpolationLow
;
79 // For images that are potentially animated we paint them at medium quality.
80 if (image
&& image
->maybeAnimated())
81 return InterpolationMedium
;
83 return InterpolationDefault
;
86 ImageQualityController::~ImageQualityController()
88 // This will catch users of ImageQualityController that forget to call cleanUp.
89 ASSERT(!gImageQualityController
|| gImageQualityController
->isEmpty());
92 ImageQualityController::ImageQualityController()
93 : m_timer(adoptPtr(new Timer
<ImageQualityController
>(this, &ImageQualityController::highQualityRepaintTimerFired
)))
94 , m_animatedResizeIsActive(false)
95 , m_liveResizeOptimizationIsActive(false)
99 void ImageQualityController::setTimer(Timer
<ImageQualityController
>* newTimer
)
101 m_timer
= adoptPtr(newTimer
);
104 void ImageQualityController::removeLayer(LayoutObject
* object
, LayerSizeMap
* innerMap
, const void* layer
)
107 innerMap
->remove(layer
);
108 if (innerMap
->isEmpty())
109 objectDestroyed(object
);
113 void ImageQualityController::set(LayoutObject
* object
, LayerSizeMap
* innerMap
, const void* layer
, const LayoutSize
& size
)
116 innerMap
->set(layer
, size
);
118 LayerSizeMap newInnerMap
;
119 newInnerMap
.set(layer
, size
);
120 m_objectLayerSizeMap
.set(object
, newInnerMap
);
124 void ImageQualityController::objectDestroyed(LayoutObject
* object
)
126 m_objectLayerSizeMap
.remove(object
);
127 if (m_objectLayerSizeMap
.isEmpty()) {
128 m_animatedResizeIsActive
= false;
133 void ImageQualityController::highQualityRepaintTimerFired(Timer
<ImageQualityController
>*)
135 if (!m_animatedResizeIsActive
&& !m_liveResizeOptimizationIsActive
)
137 m_animatedResizeIsActive
= false;
139 for (auto* layoutObject
: m_objectLayerSizeMap
.keys()) {
140 if (LocalFrame
* frame
= layoutObject
->document().frame()) {
141 // If this layoutObject's containing FrameView is in live resize, punt the timer and hold back for now.
142 if (frame
->view() && frame
->view()->inLiveResize()) {
147 layoutObject
->setShouldDoFullPaintInvalidation();
150 m_liveResizeOptimizationIsActive
= false;
153 void ImageQualityController::restartTimer()
155 m_timer
->startOneShot(cLowQualityTimeThreshold
, FROM_HERE
);
158 bool ImageQualityController::shouldPaintAtLowQuality(GraphicsContext
* context
, LayoutObject
* object
, Image
* image
, const void *layer
, const LayoutSize
& layoutSize
)
160 // If the image is not a bitmap image, then none of this is relevant and we just paint at high
162 if (!image
|| !image
->isBitmapImage())
168 if (object
->style()->imageRendering() == ImageRenderingOptimizeContrast
)
171 // Look ourselves up in the hashtables.
172 ObjectLayerSizeMap::iterator i
= m_objectLayerSizeMap
.find(object
);
173 LayerSizeMap
* innerMap
= i
!= m_objectLayerSizeMap
.end() ? &i
->value
: 0;
175 bool isFirstResize
= true;
177 LayerSizeMap::iterator j
= innerMap
->find(layer
);
178 if (j
!= innerMap
->end()) {
179 isFirstResize
= false;
184 // If the containing FrameView is being resized, paint at low quality until resizing is finished.
185 if (LocalFrame
* frame
= object
->document().frame()) {
186 bool frameViewIsCurrentlyInLiveResize
= frame
->view() && frame
->view()->inLiveResize();
187 if (frameViewIsCurrentlyInLiveResize
) {
188 set(object
, innerMap
, layer
, layoutSize
);
190 m_liveResizeOptimizationIsActive
= true;
193 if (m_liveResizeOptimizationIsActive
) {
194 // Live resize has ended, paint in HQ and remove this object from the list.
195 removeLayer(object
, innerMap
, layer
);
200 if (layoutSize
== image
->size()) {
201 // There is no scale in effect. If we had a scale in effect before, we can just remove this object from the list.
202 removeLayer(object
, innerMap
, layer
);
206 // If an animated resize is active, paint in low quality and kick the timer ahead.
207 if (m_animatedResizeIsActive
) {
208 set(object
, innerMap
, layer
, layoutSize
);
209 if (oldSize
!= layoutSize
)
213 // If this is the first time resizing this image, or its size is the
214 // same as the last resize, draw at high res, but record the paint
215 // size and set the timer.
216 if (isFirstResize
|| oldSize
== layoutSize
) {
218 set(object
, innerMap
, layer
, layoutSize
);
221 // If the timer is no longer active, draw at high quality and don't
223 if (!m_timer
->isActive()) {
224 removeLayer(object
, innerMap
, layer
);
228 // This object has been resized to two different sizes while the timer
229 // is active, so draw at low quality, set the flag for animated resizes and
230 // the object to the list for high quality redraw.
231 set(object
, innerMap
, layer
, layoutSize
);
232 m_animatedResizeIsActive
= true;