OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2012 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 |
| 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. |
| 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN
Y |
| 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N |
| 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 */ |
| 24 |
| 25 #include "config.h" |
| 26 |
| 27 #if USE(ACCELERATED_COMPOSITING) |
| 28 |
| 29 #include "ScrollbarLayerChromium.h" |
| 30 |
| 31 #include "BitmapCanvasLayerTextureUpdater.h" |
| 32 #include "GraphicsContext.h" |
| 33 #include "LayerPainterChromium.h" |
| 34 #include "PlatformContextSkia.h" |
| 35 #include "Scrollbar.h" |
| 36 #include "ScrollbarThemeComposite.h" |
| 37 #include "cc/CCLayerTreeHost.h" |
| 38 #include "cc/CCScrollbarLayerImpl.h" |
| 39 #include "cc/CCTextureUpdater.h" |
| 40 |
| 41 namespace WebCore { |
| 42 |
| 43 PassOwnPtr<CCLayerImpl> ScrollbarLayerChromium::createCCLayerImpl() |
| 44 { |
| 45 return CCScrollbarLayerImpl::create(id()); |
| 46 } |
| 47 |
| 48 PassRefPtr<ScrollbarLayerChromium> ScrollbarLayerChromium::create(Scrollbar* scr
ollbar, int scrollLayerId) |
| 49 { |
| 50 return adoptRef(new ScrollbarLayerChromium(scrollbar, scrollLayerId)); |
| 51 } |
| 52 |
| 53 ScrollbarLayerChromium::ScrollbarLayerChromium(Scrollbar* scrollbar, int scrollL
ayerId) |
| 54 : m_scrollbar(scrollbar) |
| 55 , m_scrollLayerId(scrollLayerId) |
| 56 , m_textureFormat(GL_INVALID_ENUM) |
| 57 , m_scrollbarOverlayStyle(scrollbar->scrollbarOverlayStyle()) |
| 58 , m_isScrollableAreaActive(scrollbar->isScrollableAreaActive()) |
| 59 , m_isScrollViewScrollbar(scrollbar->isScrollViewScrollbar()) |
| 60 , m_orientation(scrollbar->orientation()) |
| 61 , m_controlSize(scrollbar->controlSize()) |
| 62 { |
| 63 } |
| 64 |
| 65 ScrollbarThemeComposite* ScrollbarLayerChromium::theme() const |
| 66 { |
| 67 // All Chromium scrollbars are ScrollbarThemeComposite-derived. |
| 68 return static_cast<ScrollbarThemeComposite*>(m_scrollbar->theme()); |
| 69 } |
| 70 |
| 71 void ScrollbarLayerChromium::pushPropertiesTo(CCLayerImpl* layer) |
| 72 { |
| 73 LayerChromium::pushPropertiesTo(layer); |
| 74 |
| 75 CCScrollbarLayerImpl* scrollbarLayer = static_cast<CCScrollbarLayerImpl*>(la
yer); |
| 76 |
| 77 scrollbarLayer->setScrollbarOverlayStyle(m_scrollbarOverlayStyle); |
| 78 |
| 79 if (m_backTrack && m_backTrack->texture()->isReserved()) |
| 80 scrollbarLayer->setBackTrackTextureId(m_backTrack->texture()->textureId(
)); |
| 81 else |
| 82 scrollbarLayer->setBackTrackTextureId(0); |
| 83 |
| 84 if (m_foreTrack && m_foreTrack->texture()->isReserved()) |
| 85 scrollbarLayer->setForeTrackTextureId(m_foreTrack->texture()->textureId(
)); |
| 86 else |
| 87 scrollbarLayer->setForeTrackTextureId(0); |
| 88 |
| 89 if (m_thumb && m_thumb->texture()->isReserved()) |
| 90 scrollbarLayer->setThumbTextureId(m_thumb->texture()->textureId()); |
| 91 else |
| 92 scrollbarLayer->setThumbTextureId(0); |
| 93 |
| 94 Vector<IntRect> tickmarks; |
| 95 m_scrollbar->getTickmarks(tickmarks); |
| 96 scrollbarLayer->setTickmarks(tickmarks); |
| 97 |
| 98 scrollbarLayer->setIsScrollableAreaActive(m_isScrollableAreaActive); |
| 99 scrollbarLayer->setIsScrollViewScrollbar(m_isScrollViewScrollbar); |
| 100 |
| 101 scrollbarLayer->setOrientation(m_orientation); |
| 102 |
| 103 scrollbarLayer->setControlSize(m_controlSize); |
| 104 |
| 105 scrollbarLayer->setPressedPart(m_scrollbar->pressedPart()); |
| 106 scrollbarLayer->setHoveredPart(m_scrollbar->hoveredPart()); |
| 107 |
| 108 scrollbarLayer->setEnabled(m_scrollbar->enabled()); |
| 109 } |
| 110 |
| 111 class ScrollbarBackgroundPainter : public LayerPainterChromium { |
| 112 WTF_MAKE_NONCOPYABLE(ScrollbarBackgroundPainter); |
| 113 public: |
| 114 static PassOwnPtr<ScrollbarBackgroundPainter> create(ScrollbarThemeClient* s
crollbar, ScrollbarThemeComposite* theme, ScrollbarPart trackPart) |
| 115 { |
| 116 return adoptPtr(new ScrollbarBackgroundPainter(scrollbar, theme, trackPa
rt)); |
| 117 } |
| 118 |
| 119 virtual void paint(SkCanvas* canvas, const IntRect& contentRect, IntRect&) O
VERRIDE |
| 120 { |
| 121 PlatformContextSkia platformContext(canvas); |
| 122 platformContext.setDrawingToImageBuffer(true); |
| 123 GraphicsContext context(&platformContext); |
| 124 |
| 125 // The following is a simplification of ScrollbarThemeComposite::paint. |
| 126 m_theme->paintScrollbarBackground(&context, m_scrollbar); |
| 127 |
| 128 if (m_theme->hasButtons(m_scrollbar)) { |
| 129 IntRect backButtonStartPaintRect = m_theme->backButtonRect(m_scrollb
ar, BackButtonStartPart, true); |
| 130 m_theme->paintButton(&context, m_scrollbar, backButtonStartPaintRect
, BackButtonStartPart); |
| 131 |
| 132 IntRect backButtonEndPaintRect = m_theme->backButtonRect(m_scrollbar
, BackButtonEndPart, true); |
| 133 m_theme->paintButton(&context, m_scrollbar, backButtonEndPaintRect,
BackButtonEndPart); |
| 134 |
| 135 IntRect forwardButtonStartPaintRect = m_theme->forwardButtonRect(m_s
crollbar, ForwardButtonStartPart, true); |
| 136 m_theme->paintButton(&context, m_scrollbar, forwardButtonStartPaintR
ect, ForwardButtonStartPart); |
| 137 |
| 138 IntRect forwardButtonEndPaintRect = m_theme->forwardButtonRect(m_scr
ollbar, ForwardButtonEndPart, true); |
| 139 m_theme->paintButton(&context, m_scrollbar, forwardButtonEndPaintRec
t, ForwardButtonEndPart); |
| 140 } |
| 141 |
| 142 IntRect trackPaintRect = m_theme->trackRect(m_scrollbar, true); |
| 143 m_theme->paintTrackBackground(&context, m_scrollbar, trackPaintRect); |
| 144 |
| 145 bool thumbPresent = m_theme->hasThumb(m_scrollbar); |
| 146 if (thumbPresent) |
| 147 m_theme->paintTrackPiece(&context, m_scrollbar, trackPaintRect, m_tr
ackPart); |
| 148 |
| 149 m_theme->paintTickmarks(&context, m_scrollbar, trackPaintRect); |
| 150 } |
| 151 private: |
| 152 ScrollbarBackgroundPainter(ScrollbarThemeClient* scrollbar, ScrollbarThemeCo
mposite* theme, ScrollbarPart trackPart) |
| 153 : m_scrollbar(scrollbar) |
| 154 , m_theme(theme) |
| 155 , m_trackPart(trackPart) |
| 156 { |
| 157 } |
| 158 |
| 159 ScrollbarThemeClient* m_scrollbar; |
| 160 ScrollbarThemeComposite* m_theme; |
| 161 ScrollbarPart m_trackPart; |
| 162 }; |
| 163 |
| 164 class ScrollbarThumbPainter : public LayerPainterChromium { |
| 165 WTF_MAKE_NONCOPYABLE(ScrollbarThumbPainter); |
| 166 public: |
| 167 static PassOwnPtr<ScrollbarThumbPainter> create(ScrollbarThemeClient* scroll
bar, ScrollbarThemeComposite* theme) |
| 168 { |
| 169 return adoptPtr(new ScrollbarThumbPainter(scrollbar, theme)); |
| 170 } |
| 171 |
| 172 virtual void paint(SkCanvas* canvas, const IntRect& contentRect, IntRect& op
aque) OVERRIDE |
| 173 { |
| 174 PlatformContextSkia platformContext(canvas); |
| 175 platformContext.setDrawingToImageBuffer(true); |
| 176 GraphicsContext context(&platformContext); |
| 177 |
| 178 // Consider the thumb to be at the origin when painting. |
| 179 IntRect thumbRect = IntRect(IntPoint(), m_theme->thumbRect(m_scrollbar).
size()); |
| 180 m_theme->paintThumb(&context, m_scrollbar, thumbRect); |
| 181 } |
| 182 |
| 183 private: |
| 184 ScrollbarThumbPainter(ScrollbarThemeClient* scrollbar, ScrollbarThemeComposi
te* theme) |
| 185 : m_scrollbar(scrollbar) |
| 186 , m_theme(theme) |
| 187 { |
| 188 } |
| 189 |
| 190 ScrollbarThemeClient* m_scrollbar; |
| 191 ScrollbarThemeComposite* m_theme; |
| 192 }; |
| 193 |
| 194 void ScrollbarLayerChromium::setLayerTreeHost(CCLayerTreeHost* host) |
| 195 { |
| 196 if (!host || host != layerTreeHost()) { |
| 197 m_backTrackUpdater.clear(); |
| 198 m_backTrack.clear(); |
| 199 m_thumbUpdater.clear(); |
| 200 m_thumb.clear(); |
| 201 } |
| 202 |
| 203 LayerChromium::setLayerTreeHost(host); |
| 204 } |
| 205 |
| 206 void ScrollbarLayerChromium::createTextureUpdaterIfNeeded() |
| 207 { |
| 208 bool useMapSubImage = layerTreeHost()->layerRendererCapabilities().usingMapS
ub; |
| 209 m_textureFormat = layerTreeHost()->layerRendererCapabilities().bestTextureFo
rmat; |
| 210 |
| 211 if (!m_backTrackUpdater) |
| 212 m_backTrackUpdater = BitmapCanvasLayerTextureUpdater::create(ScrollbarBa
ckgroundPainter::create(m_scrollbar.get(), theme(), BackTrackPart), useMapSubIma
ge); |
| 213 if (!m_backTrack) |
| 214 m_backTrack = m_backTrackUpdater->createTexture(layerTreeHost()->content
sTextureManager()); |
| 215 |
| 216 // Only create two-part track if we think the two parts could be different i
n appearance. |
| 217 if (m_scrollbar->isCustomScrollbar()) { |
| 218 if (!m_foreTrackUpdater) |
| 219 m_foreTrackUpdater = BitmapCanvasLayerTextureUpdater::create(Scrollb
arBackgroundPainter::create(m_scrollbar.get(), theme(), ForwardTrackPart), useMa
pSubImage); |
| 220 if (!m_foreTrack) |
| 221 m_foreTrack = m_foreTrackUpdater->createTexture(layerTreeHost()->con
tentsTextureManager()); |
| 222 } |
| 223 |
| 224 |
| 225 if (!m_thumbUpdater) |
| 226 m_thumbUpdater = BitmapCanvasLayerTextureUpdater::create(ScrollbarThumbP
ainter::create(m_scrollbar.get(), theme()), useMapSubImage); |
| 227 if (!m_thumb) |
| 228 m_thumb = m_thumbUpdater->createTexture(layerTreeHost()->contentsTexture
Manager()); |
| 229 } |
| 230 |
| 231 void ScrollbarLayerChromium::updatePart(LayerTextureUpdater* painter, LayerTextu
reUpdater::Texture* texture, const IntRect& rect, CCTextureUpdater& updater) |
| 232 { |
| 233 bool textureValid = texture->texture()->isValid(rect.size(), m_textureFormat
); |
| 234 // Skip painting and uploading if there are no invalidations. |
| 235 if (textureValid && m_updateRect.isEmpty()) { |
| 236 texture->texture()->reserve(rect.size(), m_textureFormat); |
| 237 return; |
| 238 } |
| 239 |
| 240 // ScrollbarLayerChromium doesn't support partial uploads, so any |
| 241 // invalidation is treated a full layer invalidation. |
| 242 if (layerTreeHost()->bufferedUpdates() && textureValid) |
| 243 layerTreeHost()->deleteTextureAfterCommit(texture->texture()->steal()); |
| 244 |
| 245 if (!texture->texture()->reserve(rect.size(), m_textureFormat)) |
| 246 return; |
| 247 |
| 248 // Paint and upload the entire part. |
| 249 IntRect paintedOpaqueRect; |
| 250 painter->prepareToUpdate(rect, rect.size(), 1, paintedOpaqueRect); |
| 251 texture->prepareRect(rect); |
| 252 |
| 253 IntRect destRect(IntPoint(), rect.size()); |
| 254 updater.appendUpdate(texture, rect, destRect); |
| 255 } |
| 256 |
| 257 void ScrollbarLayerChromium::update(CCTextureUpdater& updater, const CCOcclusion
Tracker*) |
| 258 { |
| 259 if (contentBounds().isEmpty()) |
| 260 return; |
| 261 |
| 262 createTextureUpdaterIfNeeded(); |
| 263 |
| 264 IntPoint scrollbarOrigin(m_scrollbar->x(), m_scrollbar->y()); |
| 265 IntRect contentRect(scrollbarOrigin, contentBounds()); |
| 266 updatePart(m_backTrackUpdater.get(), m_backTrack.get(), contentRect, updater
); |
| 267 if (m_foreTrack && m_foreTrackUpdater) |
| 268 updatePart(m_foreTrackUpdater.get(), m_foreTrack.get(), contentRect, upd
ater); |
| 269 |
| 270 // Consider the thumb to be at the origin when painting. |
| 271 IntRect thumbRect = IntRect(IntPoint(), theme()->thumbRect(m_scrollbar.get()
).size()); |
| 272 if (!thumbRect.isEmpty()) |
| 273 updatePart(m_thumbUpdater.get(), m_thumb.get(), thumbRect, updater); |
| 274 } |
| 275 |
| 276 } |
| 277 #endif // USE(ACCELERATED_COMPOSITING) |
OLD | NEW |