OLD | NEW |
| (Empty) |
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 "sky/engine/config.h" | |
32 #include "sky/engine/core/rendering/ImageQualityController.h" | |
33 | |
34 #include "sky/engine/core/frame/FrameView.h" | |
35 #include "sky/engine/core/frame/LocalFrame.h" | |
36 #include "sky/engine/platform/graphics/GraphicsContext.h" | |
37 | |
38 namespace blink { | |
39 | |
40 static const double cLowQualityTimeThreshold = 0.500; // 500 ms | |
41 | |
42 static ImageQualityController* gImageQualityController = 0; | |
43 | |
44 ImageQualityController* ImageQualityController::imageQualityController() | |
45 { | |
46 if (!gImageQualityController) | |
47 gImageQualityController = new ImageQualityController; | |
48 | |
49 return gImageQualityController; | |
50 } | |
51 | |
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 | |
63 bool ImageQualityController::has(RenderObject* renderer) | |
64 { | |
65 return gImageQualityController && gImageQualityController->m_objectLayerSize
Map.contains(renderer); | |
66 } | |
67 | |
68 InterpolationQuality ImageQualityController::chooseInterpolationQuality(Graphics
Context* context, RenderObject* object, Image* image, const void* layer, const L
ayoutSize& layoutSize) | |
69 { | |
70 if (object->style()->imageRendering() == ImageRenderingPixelated | |
71 && image | |
72 && (layoutSize.width() > image->width() || layoutSize.height() > image->
height() || layoutSize == image->size())) { | |
73 return InterpolationNone; | |
74 } | |
75 | |
76 if (InterpolationDefault == InterpolationLow) | |
77 return InterpolationLow; | |
78 | |
79 if (shouldPaintAtLowQuality(context, object, image, layer, layoutSize)) | |
80 return InterpolationLow; | |
81 | |
82 // For images that are potentially animated we paint them at medium quality. | |
83 if (image && image->maybeAnimated()) | |
84 return InterpolationMedium; | |
85 | |
86 return InterpolationDefault; | |
87 } | |
88 | |
89 ImageQualityController::~ImageQualityController() | |
90 { | |
91 // This will catch users of ImageQualityController that forget to call clean
Up. | |
92 ASSERT(!gImageQualityController || gImageQualityController->isEmpty()); | |
93 } | |
94 | |
95 // FIXME(sky): m_liveResizeOptimizationIsActive is never set to true. | |
96 ImageQualityController::ImageQualityController() | |
97 : m_timer(this, &ImageQualityController::highQualityRepaintTimerFired) | |
98 , m_animatedResizeIsActive(false) | |
99 , m_liveResizeOptimizationIsActive(false) | |
100 { | |
101 } | |
102 | |
103 void ImageQualityController::removeLayer(RenderObject* object, LayerSizeMap* inn
erMap, const void* layer) | |
104 { | |
105 if (innerMap) { | |
106 innerMap->remove(layer); | |
107 if (innerMap->isEmpty()) | |
108 objectDestroyed(object); | |
109 } | |
110 } | |
111 | |
112 void ImageQualityController::set(RenderObject* object, LayerSizeMap* innerMap, c
onst void* layer, const LayoutSize& size) | |
113 { | |
114 if (innerMap) | |
115 innerMap->set(layer, size); | |
116 else { | |
117 LayerSizeMap newInnerMap; | |
118 newInnerMap.set(layer, size); | |
119 m_objectLayerSizeMap.set(object, newInnerMap); | |
120 } | |
121 } | |
122 | |
123 void ImageQualityController::objectDestroyed(RenderObject* object) | |
124 { | |
125 m_objectLayerSizeMap.remove(object); | |
126 if (m_objectLayerSizeMap.isEmpty()) { | |
127 m_animatedResizeIsActive = false; | |
128 m_timer.stop(); | |
129 } | |
130 } | |
131 | |
132 void ImageQualityController::highQualityRepaintTimerFired(Timer<ImageQualityCont
roller>*) | |
133 { | |
134 if (!m_animatedResizeIsActive && !m_liveResizeOptimizationIsActive) | |
135 return; | |
136 m_animatedResizeIsActive = false; | |
137 | |
138 for (ObjectLayerSizeMap::iterator it = m_objectLayerSizeMap.begin(); it != m
_objectLayerSizeMap.end(); ++it) { | |
139 it->key->scheduleVisualUpdate(); | |
140 } | |
141 | |
142 m_liveResizeOptimizationIsActive = false; | |
143 } | |
144 | |
145 void ImageQualityController::restartTimer() | |
146 { | |
147 m_timer.startOneShot(cLowQualityTimeThreshold, FROM_HERE); | |
148 } | |
149 | |
150 bool ImageQualityController::shouldPaintAtLowQuality(GraphicsContext* context, R
enderObject* object, Image* image, const void *layer, const LayoutSize& layoutSi
ze) | |
151 { | |
152 // If the image is not a bitmap image, then none of this is relevant and we
just paint at high | |
153 // quality. | |
154 if (!image || !image->isBitmapImage()) | |
155 return false; | |
156 | |
157 if (object->style()->imageRendering() == ImageRenderingOptimizeContrast) | |
158 return true; | |
159 | |
160 // Look ourselves up in the hashtables. | |
161 ObjectLayerSizeMap::iterator i = m_objectLayerSizeMap.find(object); | |
162 LayerSizeMap* innerMap = i != m_objectLayerSizeMap.end() ? &i->value : 0; | |
163 LayoutSize oldSize; | |
164 bool isFirstResize = true; | |
165 if (innerMap) { | |
166 LayerSizeMap::iterator j = innerMap->find(layer); | |
167 if (j != innerMap->end()) { | |
168 isFirstResize = false; | |
169 oldSize = j->value; | |
170 } | |
171 } | |
172 | |
173 const AffineTransform& currentTransform = context->getCTM(); | |
174 bool contextIsScaled = !currentTransform.isIdentityOrTranslationOrFlipped(); | |
175 | |
176 LayoutSize scaledImageSize = currentTransform.mapSize(image->size()); | |
177 LayoutSize scaledLayoutSize = currentTransform.mapSize(roundedIntSize(layout
Size)); | |
178 | |
179 // If the containing FrameView is being resized, paint at low quality until
resizing is finished. | |
180 if (m_liveResizeOptimizationIsActive) { | |
181 // Live resize has ended, paint in HQ and remove this object from the li
st. | |
182 removeLayer(object, innerMap, layer); | |
183 return false; | |
184 } | |
185 | |
186 // See crbug.com/382491. This test is insufficient to ensure that there is n
o scale | |
187 // applied in the compositor, but it is probably adequate here. In the worst
case we | |
188 // draw at high quality when we need not. | |
189 if (!contextIsScaled && scaledLayoutSize == scaledImageSize) { | |
190 // There is no scale in effect. If we had a scale in effect before, we c
an just remove this object from the list. | |
191 removeLayer(object, innerMap, layer); | |
192 return false; | |
193 } | |
194 | |
195 // If an animated resize is active, paint in low quality and kick the timer
ahead. | |
196 if (m_animatedResizeIsActive) { | |
197 set(object, innerMap, layer, scaledLayoutSize); | |
198 restartTimer(); | |
199 return true; | |
200 } | |
201 // If this is the first time resizing this image, or its size is the | |
202 // same as the last resize, draw at high res, but record the paint | |
203 // size and set the timer. | |
204 if (isFirstResize || oldSize == scaledLayoutSize) { | |
205 restartTimer(); | |
206 set(object, innerMap, layer, scaledLayoutSize); | |
207 return false; | |
208 } | |
209 // If the timer is no longer active, draw at high quality and don't | |
210 // set the timer. | |
211 if (!m_timer.isActive()) { | |
212 removeLayer(object, innerMap, layer); | |
213 return false; | |
214 } | |
215 // This object has been resized to two different sizes while the timer | |
216 // is active, so draw at low quality, set the flag for animated resizes and | |
217 // the object to the list for high quality redraw. | |
218 set(object, innerMap, layer, scaledLayoutSize); | |
219 m_animatedResizeIsActive = true; | |
220 restartTimer(); | |
221 return true; | |
222 } | |
223 | |
224 } // namespace blink | |
OLD | NEW |