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/FrameView.h" 35 #include "core/frame/LocalFrame.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 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 InterpolationQuality ImageQualityController::chooseInterpolationQuality(GraphicsContext* context, RenderObject* object, Image* image, const void* layer, const LayoutSize& layoutSize) 64 { 65 if (InterpolationDefault == InterpolationLow) 66 return InterpolationLow; 67 68 if (shouldPaintAtLowQuality(context, object, image, layer, layoutSize)) 69 return InterpolationLow; 70 71 // For images that are potentially animated we paint them at medium quality. 72 if (image && image->maybeAnimated()) 73 return InterpolationMedium; 74 75 return InterpolationDefault; 76 } 77 78 ImageQualityController::~ImageQualityController() 79 { 80 // This will catch users of ImageQualityController that forget to call cleanUp. 81 ASSERT(!gImageQualityController || gImageQualityController->isEmpty()); 82 } 83 84 ImageQualityController::ImageQualityController() 85 : m_timer(this, &ImageQualityController::highQualityRepaintTimerFired) 86 , m_animatedResizeIsActive(false) 87 , m_liveResizeOptimizationIsActive(false) 88 { 89 } 90 91 void ImageQualityController::removeLayer(RenderObject* object, LayerSizeMap* innerMap, const void* layer) 92 { 93 if (innerMap) { 94 innerMap->remove(layer); 95 if (innerMap->isEmpty()) 96 objectDestroyed(object); 97 } 98 } 99 100 void ImageQualityController::set(RenderObject* object, LayerSizeMap* innerMap, const void* layer, const LayoutSize& size) 101 { 102 if (innerMap) 103 innerMap->set(layer, size); 104 else { 105 LayerSizeMap newInnerMap; 106 newInnerMap.set(layer, size); 107 m_objectLayerSizeMap.set(object, newInnerMap); 108 } 109 } 110 111 void ImageQualityController::objectDestroyed(RenderObject* object) 112 { 113 m_objectLayerSizeMap.remove(object); 114 if (m_objectLayerSizeMap.isEmpty()) { 115 m_animatedResizeIsActive = false; 116 m_timer.stop(); 117 } 118 } 119 120 void ImageQualityController::highQualityRepaintTimerFired(Timer<ImageQualityController>*) 121 { 122 if (!m_animatedResizeIsActive && !m_liveResizeOptimizationIsActive) 123 return; 124 m_animatedResizeIsActive = false; 125 126 for (ObjectLayerSizeMap::iterator it = m_objectLayerSizeMap.begin(); it != m_objectLayerSizeMap.end(); ++it) { 127 if (LocalFrame* frame = it->key->document().frame()) { 128 // If this renderer's containing FrameView is in live resize, punt the timer and hold back for now. 129 if (frame->view() && frame->view()->inLiveResize()) { 130 restartTimer(); 131 return; 132 } 133 } 134 it->key->paintInvalidationForWholeRenderer(); 135 } 136 137 m_liveResizeOptimizationIsActive = false; 138 } 139 140 void ImageQualityController::restartTimer() 141 { 142 m_timer.startOneShot(cLowQualityTimeThreshold, FROM_HERE); 143 } 144 145 bool ImageQualityController::shouldPaintAtLowQuality(GraphicsContext* context, RenderObject* object, Image* image, const void *layer, const LayoutSize& layoutSize) 146 { 147 // If the image is not a bitmap image, then none of this is relevant and we just paint at high 148 // quality. 149 if (!image || !image->isBitmapImage() || context->paintingDisabled()) 150 return false; 151 152 if (object->style()->imageRendering() == ImageRenderingOptimizeContrast) 153 return true; 154 155 // Look ourselves up in the hashtables. 156 ObjectLayerSizeMap::iterator i = m_objectLayerSizeMap.find(object); 157 LayerSizeMap* innerMap = i != m_objectLayerSizeMap.end() ? &i->value : 0; 158 LayoutSize oldSize; 159 bool isFirstResize = true; 160 if (innerMap) { 161 LayerSizeMap::iterator j = innerMap->find(layer); 162 if (j != innerMap->end()) { 163 isFirstResize = false; 164 oldSize = j->value; 165 } 166 } 167 168 const AffineTransform& currentTransform = context->getCTM(); 169 bool contextIsScaled = !currentTransform.isIdentityOrTranslationOrFlipped(); 170 171 // Make sure to use the unzoomed image size, since if a full page zoom is in effect, the image 172 // is actually being scaled. 173 LayoutSize scaledImageSize = currentTransform.mapSize(image->size()); 174 LayoutSize scaledLayoutSize = currentTransform.mapSize(roundedIntSize(layoutSize)); 175 176 // If the containing FrameView is being resized, paint at low quality until resizing is finished. 177 if (LocalFrame* frame = object->document().frame()) { 178 bool frameViewIsCurrentlyInLiveResize = frame->view() && frame->view()->inLiveResize(); 179 if (frameViewIsCurrentlyInLiveResize) { 180 set(object, innerMap, layer, scaledLayoutSize); 181 restartTimer(); 182 m_liveResizeOptimizationIsActive = true; 183 return true; 184 } 185 if (m_liveResizeOptimizationIsActive) { 186 // Live resize has ended, paint in HQ and remove this object from the list. 187 removeLayer(object, innerMap, layer); 188 return false; 189 } 190 } 191 192 // See crbug.com/382491. This test is insufficient to ensure that there is no scale 193 // applied in the compositor, but it is probably adequate here. In the worst case we 194 // draw at high quality when we need not. 195 if (!contextIsScaled && scaledLayoutSize == scaledImageSize) { 196 // There is no scale in effect. If we had a scale in effect before, we can just remove this object from the list. 197 removeLayer(object, innerMap, layer); 198 return false; 199 } 200 201 // If an animated resize is active, paint in low quality and kick the timer ahead. 202 if (m_animatedResizeIsActive) { 203 set(object, innerMap, layer, scaledLayoutSize); 204 restartTimer(); 205 return true; 206 } 207 // If this is the first time resizing this image, or its size is the 208 // same as the last resize, draw at high res, but record the paint 209 // size and set the timer. 210 if (isFirstResize || oldSize == scaledLayoutSize) { 211 restartTimer(); 212 set(object, innerMap, layer, scaledLayoutSize); 213 return false; 214 } 215 // If the timer is no longer active, draw at high quality and don't 216 // set the timer. 217 if (!m_timer.isActive()) { 218 removeLayer(object, innerMap, layer); 219 return false; 220 } 221 // This object has been resized to two different sizes while the timer 222 // is active, so draw at low quality, set the flag for animated resizes and 223 // the object to the list for high quality redraw. 224 set(object, innerMap, layer, scaledLayoutSize); 225 m_animatedResizeIsActive = true; 226 restartTimer(); 227 return true; 228 } 229 230 } // namespace WebCore 231