OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CCLayerTreeHostImpl_h |
| 6 #define CCLayerTreeHostImpl_h |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/time.h" |
| 10 #include "CCAnimationEvents.h" |
| 11 #include "CCInputHandler.h" |
| 12 #include "CCLayerSorter.h" |
| 13 #include "CCRenderPass.h" |
| 14 #include "CCRenderPassSink.h" |
| 15 #include "CCRenderer.h" |
| 16 #include "SkColor.h" |
| 17 #include <public/WebCompositorOutputSurfaceClient.h> |
| 18 #include <wtf/PassOwnPtr.h> |
| 19 #include <wtf/RefPtr.h> |
| 20 |
| 21 namespace cc { |
| 22 |
| 23 class CCCompletionEvent; |
| 24 class CCDebugRectHistory; |
| 25 class CCFrameRateCounter; |
| 26 class CCHeadsUpDisplayLayerImpl; |
| 27 class CCLayerImpl; |
| 28 class CCLayerTreeHostImplTimeSourceAdapter; |
| 29 class CCPageScaleAnimation; |
| 30 class CCRenderPassDrawQuad; |
| 31 class CCResourceProvider; |
| 32 struct RendererCapabilities; |
| 33 struct CCRenderingStats; |
| 34 |
| 35 // CCLayerTreeHost->CCProxy callback interface. |
| 36 class CCLayerTreeHostImplClient { |
| 37 public: |
| 38 virtual void didLoseContextOnImplThread() = 0; |
| 39 virtual void onSwapBuffersCompleteOnImplThread() = 0; |
| 40 virtual void onVSyncParametersChanged(double monotonicTimebase, double inter
valInSeconds) = 0; |
| 41 virtual void onCanDrawStateChanged(bool canDraw) = 0; |
| 42 virtual void setNeedsRedrawOnImplThread() = 0; |
| 43 virtual void setNeedsCommitOnImplThread() = 0; |
| 44 virtual void postAnimationEventsToMainThreadOnImplThread(scoped_ptr<CCAnimat
ionEventsVector>, double wallClockTime) = 0; |
| 45 virtual void releaseContentsTexturesOnImplThread() = 0; |
| 46 }; |
| 47 |
| 48 // CCPinchZoomViewport models the bounds and offset of the viewport that is used
during a pinch-zoom operation. |
| 49 // It tracks the layout-space dimensions of the viewport before any applied scal
e, and then tracks the layout-space |
| 50 // coordinates of the viewport respecting the pinch settings. |
| 51 class CCPinchZoomViewport { |
| 52 public: |
| 53 CCPinchZoomViewport(); |
| 54 |
| 55 float totalPageScaleFactor() const; |
| 56 |
| 57 void setPageScaleFactor(float factor) { m_pageScaleFactor = factor; } |
| 58 float pageScaleFactor() const { return m_pageScaleFactor; } |
| 59 |
| 60 void setPageScaleDelta(float delta); |
| 61 float pageScaleDelta() const { return m_pageScaleDelta; } |
| 62 |
| 63 float minPageScaleFactor() const { return m_minPageScaleFactor; } |
| 64 float maxPageScaleFactor() const { return m_maxPageScaleFactor; } |
| 65 |
| 66 void setSentPageScaleDelta(float delta) { m_sentPageScaleDelta = delta; } |
| 67 float sentPageScaleDelta() const { return m_sentPageScaleDelta; } |
| 68 |
| 69 // Returns true if the passed parameters were different from those previousl
y |
| 70 // cached. |
| 71 bool setPageScaleFactorAndLimits(float pageScaleFactor, |
| 72 float minPageScaleFactor, |
| 73 float maxPageScaleFactor); |
| 74 |
| 75 // Returns the bounds and offset of the scaled and translated viewport to us
e for pinch-zoom. |
| 76 FloatRect bounds() const; |
| 77 const FloatPoint& scrollDelta() const { return m_pinchViewportScrollDelta; } |
| 78 |
| 79 void setLayoutViewportSize(const FloatSize& size) { m_layoutViewportSize = s
ize; } |
| 80 |
| 81 // Apply the scroll offset in layout space to the offset of the pinch-zoom v
iewport. The viewport cannot be |
| 82 // scrolled outside of the layout viewport bounds. Returns the component of
the scroll that is un-applied due to |
| 83 // this constraint. |
| 84 FloatSize applyScroll(FloatSize&); |
| 85 |
| 86 WebKit::WebTransformationMatrix implTransform() const; |
| 87 |
| 88 private: |
| 89 float m_pageScaleFactor; |
| 90 float m_pageScaleDelta; |
| 91 float m_sentPageScaleDelta; |
| 92 float m_maxPageScaleFactor; |
| 93 float m_minPageScaleFactor; |
| 94 |
| 95 FloatPoint m_pinchViewportScrollDelta; |
| 96 FloatSize m_layoutViewportSize; |
| 97 }; |
| 98 |
| 99 // CCLayerTreeHostImpl owns the CCLayerImpl tree as well as associated rendering
state |
| 100 class CCLayerTreeHostImpl : public CCInputHandlerClient, |
| 101 public CCRendererClient, |
| 102 public WebKit::WebCompositorOutputSurfaceClient { |
| 103 typedef std::vector<CCLayerImpl*> CCLayerList; |
| 104 |
| 105 public: |
| 106 static scoped_ptr<CCLayerTreeHostImpl> create(const CCLayerTreeSettings&, CC
LayerTreeHostImplClient*); |
| 107 virtual ~CCLayerTreeHostImpl(); |
| 108 |
| 109 // CCInputHandlerClient implementation |
| 110 virtual CCInputHandlerClient::ScrollStatus scrollBegin(const IntPoint&, CCIn
putHandlerClient::ScrollInputType) OVERRIDE; |
| 111 virtual void scrollBy(const IntPoint&, const IntSize&) OVERRIDE; |
| 112 virtual void scrollEnd() OVERRIDE; |
| 113 virtual void pinchGestureBegin() OVERRIDE; |
| 114 virtual void pinchGestureUpdate(float, const IntPoint&) OVERRIDE; |
| 115 virtual void pinchGestureEnd() OVERRIDE; |
| 116 virtual void startPageScaleAnimation(const IntSize& targetPosition, bool anc
horPoint, float pageScale, double startTime, double duration) OVERRIDE; |
| 117 virtual void scheduleAnimation() OVERRIDE; |
| 118 |
| 119 struct FrameData : public CCRenderPassSink { |
| 120 FrameData(); |
| 121 ~FrameData(); |
| 122 |
| 123 Vector<IntRect> occludingScreenSpaceRects; |
| 124 CCRenderPassList renderPasses; |
| 125 CCRenderPassIdHashMap renderPassesById; |
| 126 CCLayerList* renderSurfaceLayerList; |
| 127 CCLayerList willDrawLayers; |
| 128 |
| 129 // CCRenderPassSink implementation. |
| 130 virtual void appendRenderPass(scoped_ptr<CCRenderPass>) OVERRIDE; |
| 131 }; |
| 132 |
| 133 // Virtual for testing. |
| 134 virtual void beginCommit(); |
| 135 virtual void commitComplete(); |
| 136 virtual void animate(double monotonicTime, double wallClockTime); |
| 137 |
| 138 // Returns false if problems occured preparing the frame, and we should try |
| 139 // to avoid displaying the frame. If prepareToDraw is called, |
| 140 // didDrawAllLayers must also be called, regardless of whether drawLayers is |
| 141 // called between the two. |
| 142 virtual bool prepareToDraw(FrameData&); |
| 143 virtual void drawLayers(const FrameData&); |
| 144 // Must be called if and only if prepareToDraw was called. |
| 145 void didDrawAllLayers(const FrameData&); |
| 146 |
| 147 // CCRendererClient implementation |
| 148 virtual const IntSize& deviceViewportSize() const OVERRIDE; |
| 149 virtual const CCLayerTreeSettings& settings() const OVERRIDE; |
| 150 virtual void didLoseContext() OVERRIDE; |
| 151 virtual void onSwapBuffersComplete() OVERRIDE; |
| 152 virtual void setFullRootLayerDamage() OVERRIDE; |
| 153 virtual void releaseContentsTextures() OVERRIDE; |
| 154 virtual void setMemoryAllocationLimitBytes(size_t) OVERRIDE; |
| 155 |
| 156 // WebCompositorOutputSurfaceClient implementation. |
| 157 virtual void onVSyncParametersChanged(double monotonicTimebase, double inter
valInSeconds) OVERRIDE; |
| 158 |
| 159 // Implementation |
| 160 bool canDraw(); |
| 161 CCGraphicsContext* context() const; |
| 162 |
| 163 std::string layerTreeAsText() const; |
| 164 |
| 165 void finishAllRendering(); |
| 166 int sourceAnimationFrameNumber() const; |
| 167 |
| 168 bool initializeRenderer(scoped_ptr<CCGraphicsContext>); |
| 169 bool isContextLost(); |
| 170 CCRenderer* renderer() { return m_renderer.get(); } |
| 171 const RendererCapabilities& rendererCapabilities() const; |
| 172 |
| 173 bool swapBuffers(); |
| 174 |
| 175 void readback(void* pixels, const IntRect&); |
| 176 |
| 177 void setRootLayer(scoped_ptr<CCLayerImpl>); |
| 178 CCLayerImpl* rootLayer() { return m_rootLayerImpl.get(); } |
| 179 |
| 180 void setHudLayer(CCHeadsUpDisplayLayerImpl* layerImpl) { m_hudLayerImpl = la
yerImpl; } |
| 181 CCHeadsUpDisplayLayerImpl* hudLayer() { return m_hudLayerImpl; } |
| 182 |
| 183 // Release ownership of the current layer tree and replace it with an empty |
| 184 // tree. Returns the root layer of the detached tree. |
| 185 scoped_ptr<CCLayerImpl> detachLayerTree(); |
| 186 |
| 187 CCLayerImpl* rootScrollLayer() const { return m_rootScrollLayerImpl; } |
| 188 |
| 189 bool visible() const { return m_visible; } |
| 190 void setVisible(bool); |
| 191 |
| 192 int sourceFrameNumber() const { return m_sourceFrameNumber; } |
| 193 void setSourceFrameNumber(int frameNumber) { m_sourceFrameNumber = frameNumb
er; } |
| 194 |
| 195 bool contentsTexturesPurged() const { return m_contentsTexturesPurged; } |
| 196 void setContentsTexturesPurged(); |
| 197 void resetContentsTexturesPurged(); |
| 198 size_t memoryAllocationLimitBytes() const { return m_memoryAllocationLimitBy
tes; } |
| 199 |
| 200 void setViewportSize(const IntSize& layoutViewportSize, const IntSize& devic
eViewportSize); |
| 201 const IntSize& layoutViewportSize() const { return m_layoutViewportSize; } |
| 202 |
| 203 float deviceScaleFactor() const { return m_deviceScaleFactor; } |
| 204 void setDeviceScaleFactor(float); |
| 205 |
| 206 float pageScaleFactor() const; |
| 207 void setPageScaleFactorAndLimits(float pageScaleFactor, float minPageScaleFa
ctor, float maxPageScaleFactor); |
| 208 |
| 209 scoped_ptr<CCScrollAndScaleSet> processScrollDeltas(); |
| 210 WebKit::WebTransformationMatrix implTransform() const; |
| 211 |
| 212 void startPageScaleAnimation(const IntSize& tragetPosition, bool useAnchor,
float scale, double durationSec); |
| 213 |
| 214 SkColor backgroundColor() const { return m_backgroundColor; } |
| 215 void setBackgroundColor(SkColor color) { m_backgroundColor = color; } |
| 216 |
| 217 bool hasTransparentBackground() const { return m_hasTransparentBackground; } |
| 218 void setHasTransparentBackground(bool transparent) { m_hasTransparentBackgro
und = transparent; } |
| 219 |
| 220 bool needsAnimateLayers() const { return m_needsAnimateLayers; } |
| 221 void setNeedsAnimateLayers() { m_needsAnimateLayers = true; } |
| 222 |
| 223 void setNeedsRedraw(); |
| 224 |
| 225 void renderingStats(CCRenderingStats*) const; |
| 226 |
| 227 void updateRootScrollLayerImplTransform(); |
| 228 |
| 229 CCFrameRateCounter* fpsCounter() const { return m_fpsCounter.get(); } |
| 230 CCDebugRectHistory* debugRectHistory() const { return m_debugRectHistory.get
(); } |
| 231 CCResourceProvider* resourceProvider() const { return m_resourceProvider.get
(); } |
| 232 |
| 233 class CullRenderPassesWithCachedTextures { |
| 234 public: |
| 235 bool shouldRemoveRenderPass(const CCRenderPassDrawQuad&, const FrameData
&) const; |
| 236 |
| 237 // Iterates from the root first, in order to remove the surfaces closest |
| 238 // to the root with cached textures, and all surfaces that draw into |
| 239 // them. |
| 240 size_t renderPassListBegin(const CCRenderPassList& list) const { return
list.size() - 1; } |
| 241 size_t renderPassListEnd(const CCRenderPassList&) const { return 0 - 1;
} |
| 242 size_t renderPassListNext(size_t it) const { return it - 1; } |
| 243 |
| 244 CullRenderPassesWithCachedTextures(CCRenderer& renderer) : m_renderer(re
nderer) { } |
| 245 private: |
| 246 CCRenderer& m_renderer; |
| 247 }; |
| 248 |
| 249 class CullRenderPassesWithNoQuads { |
| 250 public: |
| 251 bool shouldRemoveRenderPass(const CCRenderPassDrawQuad&, const FrameData
&) const; |
| 252 |
| 253 // Iterates in draw order, so that when a surface is removed, and its |
| 254 // target becomes empty, then its target can be removed also. |
| 255 size_t renderPassListBegin(const CCRenderPassList&) const { return 0; } |
| 256 size_t renderPassListEnd(const CCRenderPassList& list) const { return li
st.size(); } |
| 257 size_t renderPassListNext(size_t it) const { return it + 1; } |
| 258 }; |
| 259 |
| 260 template<typename RenderPassCuller> |
| 261 static void removeRenderPasses(RenderPassCuller, FrameData&); |
| 262 |
| 263 protected: |
| 264 CCLayerTreeHostImpl(const CCLayerTreeSettings&, CCLayerTreeHostImplClient*); |
| 265 |
| 266 void animatePageScale(double monotonicTime); |
| 267 void animateScrollbars(double monotonicTime); |
| 268 |
| 269 // Exposed for testing. |
| 270 void calculateRenderSurfaceLayerList(CCLayerList&); |
| 271 |
| 272 // Virtual for testing. |
| 273 virtual void animateLayers(double monotonicTime, double wallClockTime); |
| 274 |
| 275 // Virtual for testing. |
| 276 virtual base::TimeDelta lowFrequencyAnimationInterval() const; |
| 277 |
| 278 CCLayerTreeHostImplClient* m_client; |
| 279 int m_sourceFrameNumber; |
| 280 |
| 281 private: |
| 282 void computeDoubleTapZoomDeltas(CCScrollAndScaleSet* scrollInfo); |
| 283 void computePinchZoomDeltas(CCScrollAndScaleSet* scrollInfo); |
| 284 void makeScrollAndScaleSet(CCScrollAndScaleSet* scrollInfo, const IntSize& s
crollOffset, float pageScale); |
| 285 |
| 286 void setPageScaleDelta(float); |
| 287 void updateMaxScrollPosition(); |
| 288 void trackDamageForAllSurfaces(CCLayerImpl* rootDrawLayer, const CCLayerList
& renderSurfaceLayerList); |
| 289 |
| 290 // Returns false if the frame should not be displayed. This function should |
| 291 // only be called from prepareToDraw, as didDrawAllLayers must be called |
| 292 // if this helper function is called. |
| 293 bool calculateRenderPasses(FrameData&); |
| 294 void animateLayersRecursive(CCLayerImpl*, double monotonicTime, double wallC
lockTime, CCAnimationEventsVector*, bool& didAnimate, bool& needsAnimateLayers); |
| 295 void setBackgroundTickingEnabled(bool); |
| 296 IntSize contentSize() const; |
| 297 |
| 298 void sendDidLoseContextRecursive(CCLayerImpl*); |
| 299 void clearRenderSurfaces(); |
| 300 bool ensureRenderSurfaceLayerList(); |
| 301 void clearCurrentlyScrollingLayer(); |
| 302 |
| 303 void animateScrollbarsRecursive(CCLayerImpl*, double monotonicTime); |
| 304 |
| 305 void dumpRenderSurfaces(std::string*, int indent, const CCLayerImpl*) const; |
| 306 |
| 307 scoped_ptr<CCGraphicsContext> m_context; |
| 308 OwnPtr<CCResourceProvider> m_resourceProvider; |
| 309 OwnPtr<CCRenderer> m_renderer; |
| 310 scoped_ptr<CCLayerImpl> m_rootLayerImpl; |
| 311 CCLayerImpl* m_rootScrollLayerImpl; |
| 312 CCLayerImpl* m_currentlyScrollingLayerImpl; |
| 313 CCHeadsUpDisplayLayerImpl* m_hudLayerImpl; |
| 314 int m_scrollingLayerIdFromPreviousTree; |
| 315 bool m_scrollDeltaIsInScreenSpace; |
| 316 CCLayerTreeSettings m_settings; |
| 317 IntSize m_layoutViewportSize; |
| 318 IntSize m_deviceViewportSize; |
| 319 float m_deviceScaleFactor; |
| 320 bool m_visible; |
| 321 bool m_contentsTexturesPurged; |
| 322 size_t m_memoryAllocationLimitBytes; |
| 323 |
| 324 SkColor m_backgroundColor; |
| 325 bool m_hasTransparentBackground; |
| 326 |
| 327 // If this is true, it is necessary to traverse the layer tree ticking the a
nimators. |
| 328 bool m_needsAnimateLayers; |
| 329 bool m_pinchGestureActive; |
| 330 IntPoint m_previousPinchAnchor; |
| 331 |
| 332 OwnPtr<CCPageScaleAnimation> m_pageScaleAnimation; |
| 333 |
| 334 // This is used for ticking animations slowly when hidden. |
| 335 OwnPtr<CCLayerTreeHostImplTimeSourceAdapter> m_timeSourceClientAdapter; |
| 336 |
| 337 CCLayerSorter m_layerSorter; |
| 338 |
| 339 // List of visible layers for the most recently prepared frame. Used for |
| 340 // rendering and input event hit testing. |
| 341 CCLayerList m_renderSurfaceLayerList; |
| 342 |
| 343 CCPinchZoomViewport m_pinchZoomViewport; |
| 344 |
| 345 OwnPtr<CCFrameRateCounter> m_fpsCounter; |
| 346 OwnPtr<CCDebugRectHistory> m_debugRectHistory; |
| 347 |
| 348 size_t m_numImplThreadScrolls; |
| 349 size_t m_numMainThreadScrolls; |
| 350 |
| 351 DISALLOW_COPY_AND_ASSIGN(CCLayerTreeHostImpl); |
| 352 }; |
| 353 |
| 354 }; |
| 355 |
| 356 #endif |
OLD | NEW |