1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CC_LAYER_TREE_HOST_IMPL_H_
#define CC_LAYER_TREE_HOST_IMPL_H_
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/time.h"
#include "cc/animation_events.h"
#include "cc/animation_registrar.h"
#include "cc/cc_export.h"
#include "cc/input_handler.h"
#include "cc/output_surface_client.h"
#include "cc/render_pass.h"
#include "cc/render_pass_sink.h"
#include "cc/renderer.h"
#include "cc/tile_manager.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/rect.h"
namespace cc {
class CompletionEvent;
class CompositorFrameMetadata;
class DebugRectHistory;
class FrameRateCounter;
class LayerImpl;
class LayerTreeHostImplTimeSourceAdapter;
class LayerTreeImpl;
class PageScaleAnimation;
class RenderPassDrawQuad;
class ResourceProvider;
struct RendererCapabilities;
struct RenderingStats;
// LayerTreeHost->Proxy callback interface.
class LayerTreeHostImplClient {
public:
virtual void didLoseOutputSurfaceOnImplThread() = 0;
virtual void onSwapBuffersCompleteOnImplThread() = 0;
virtual void onVSyncParametersChanged(base::TimeTicks timebase, base::TimeDelta interval) = 0;
virtual void onCanDrawStateChanged(bool canDraw) = 0;
virtual void onHasPendingTreeStateChanged(bool hasPendingTree) = 0;
virtual void setNeedsRedrawOnImplThread() = 0;
virtual void setNeedsCommitOnImplThread() = 0;
virtual void setNeedsManageTilesOnImplThread() = 0;
virtual void postAnimationEventsToMainThreadOnImplThread(scoped_ptr<AnimationEventsVector>, base::Time wallClockTime) = 0;
// Returns true if resources were deleted by this call.
virtual bool reduceContentsTextureMemoryOnImplThread(size_t limitBytes, int priorityCutoff) = 0;
virtual void sendManagedMemoryStats() = 0;
};
// PinchZoomViewport models the bounds and offset of the viewport that is used during a pinch-zoom operation.
// It tracks the layout-space dimensions of the viewport before any applied scale, and then tracks the layout-space
// coordinates of the viewport respecting the pinch settings.
class CC_EXPORT PinchZoomViewport {
public:
PinchZoomViewport();
float totalPageScaleFactor() const;
void setPageScaleFactor(float factor) { m_pageScaleFactor = factor; }
float pageScaleFactor() const { return m_pageScaleFactor; }
void setPageScaleDelta(float delta);
float pageScaleDelta() const { return m_pageScaleDelta; }
float minPageScaleFactor() const { return m_minPageScaleFactor; }
float maxPageScaleFactor() const { return m_maxPageScaleFactor; }
void setSentPageScaleDelta(float delta) { m_sentPageScaleDelta = delta; }
float sentPageScaleDelta() const { return m_sentPageScaleDelta; }
void setDeviceScaleFactor(float factor) { m_deviceScaleFactor = factor; }
float deviceScaleFactor() const { return m_deviceScaleFactor; }
// Returns true if the passed parameters were different from those previously
// cached.
bool setPageScaleFactorAndLimits(float pageScaleFactor,
float minPageScaleFactor,
float maxPageScaleFactor);
// Returns the bounds and offset of the scaled and translated viewport to use for pinch-zoom.
gfx::RectF bounds() const;
const gfx::Vector2dF& zoomedViewportOffset() const { return m_zoomedViewportOffset; }
void setLayoutViewportSize(const gfx::SizeF& size) { m_layoutViewportSize = size; }
// Apply the scroll offset in layout space to the offset of the pinch-zoom viewport. The viewport cannot be
// scrolled outside of the layout viewport bounds. Returns the component of the scroll that is un-applied due to
// this constraint.
gfx::Vector2dF applyScroll(const gfx::Vector2dF&);
// The implTransform goes from the origin of the unzoomedDeviceViewport to the
// origin of the zoomedDeviceViewport.
//
// implTransform = S[pageScale] * Tr[-zoomedDeviceViewportOffset]
gfx::Transform implTransform(bool pageScalePinchZoomEnabled) const;
private:
float m_pageScaleFactor;
float m_pageScaleDelta;
float m_sentPageScaleDelta;
float m_maxPageScaleFactor;
float m_minPageScaleFactor;
float m_deviceScaleFactor;
gfx::Vector2dF m_zoomedViewportOffset;
gfx::SizeF m_layoutViewportSize;
};
// LayerTreeHostImpl owns the LayerImpl tree as well as associated rendering state
class CC_EXPORT LayerTreeHostImpl : public InputHandlerClient,
public RendererClient,
public TileManagerClient,
public OutputSurfaceClient {
typedef std::vector<LayerImpl*> LayerList;
public:
static scoped_ptr<LayerTreeHostImpl> create(const LayerTreeSettings&, LayerTreeHostImplClient*, Proxy*);
virtual ~LayerTreeHostImpl();
// InputHandlerClient implementation
virtual InputHandlerClient::ScrollStatus scrollBegin(gfx::Point, InputHandlerClient::ScrollInputType) OVERRIDE;
virtual bool scrollBy(const gfx::Point&, const gfx::Vector2d&) OVERRIDE;
virtual void scrollEnd() OVERRIDE;
virtual void pinchGestureBegin() OVERRIDE;
virtual void pinchGestureUpdate(float, gfx::Point) OVERRIDE;
virtual void pinchGestureEnd() OVERRIDE;
virtual void startPageScaleAnimation(gfx::Vector2d targetOffset, bool anchorPoint, float pageScale, base::TimeTicks startTime, base::TimeDelta duration) OVERRIDE;
virtual void scheduleAnimation() OVERRIDE;
virtual bool haveTouchEventHandlersAt(const gfx::Point&) OVERRIDE;
struct CC_EXPORT FrameData : public RenderPassSink {
FrameData();
~FrameData();
std::vector<gfx::Rect> occludingScreenSpaceRects;
std::vector<gfx::Rect> nonOccludingScreenSpaceRects;
RenderPassList renderPasses;
RenderPassIdHashMap renderPassesById;
const LayerList* renderSurfaceLayerList;
LayerList willDrawLayers;
// RenderPassSink implementation.
virtual void appendRenderPass(scoped_ptr<RenderPass>) OVERRIDE;
};
// Virtual for testing.
virtual void beginCommit();
virtual void commitComplete();
virtual void animate(base::TimeTicks monotonicTime, base::Time wallClockTime);
void manageTiles();
// Returns false if problems occured preparing the frame, and we should try
// to avoid displaying the frame. If prepareToDraw is called,
// didDrawAllLayers must also be called, regardless of whether drawLayers is
// called between the two.
virtual bool prepareToDraw(FrameData&);
virtual void drawLayers(FrameData&);
// Must be called if and only if prepareToDraw was called.
void didDrawAllLayers(const FrameData&);
// RendererClient implementation
virtual const gfx::Size& deviceViewportSize() const OVERRIDE;
virtual const LayerTreeSettings& settings() const OVERRIDE;
virtual void didLoseOutputSurface() OVERRIDE;
virtual void onSwapBuffersComplete() OVERRIDE;
virtual void setFullRootLayerDamage() OVERRIDE;
virtual void setManagedMemoryPolicy(const ManagedMemoryPolicy& policy) OVERRIDE;
virtual void enforceManagedMemoryPolicy(const ManagedMemoryPolicy& policy) OVERRIDE;
virtual bool hasImplThread() const OVERRIDE;
virtual bool shouldClearRootRenderPass() const OVERRIDE;
virtual CompositorFrameMetadata makeCompositorFrameMetadata() const OVERRIDE;
// TileManagerClient implementation.
virtual void ScheduleManageTiles() OVERRIDE;
virtual void ScheduleCheckForCompletedSetPixels() OVERRIDE;
// OutputSurfaceClient implementation.
virtual void OnVSyncParametersChanged(base::TimeTicks timebase, base::TimeDelta interval) OVERRIDE;
virtual void OnSendFrameToParentCompositorAck(const CompositorFrameAck&) OVERRIDE;
// Called from LayerTreeImpl.
void OnCanDrawStateChangedForTree(LayerTreeImpl*);
// Implementation
bool canDraw();
OutputSurface* outputSurface() const;
std::string layerTreeAsText() const;
std::string layerTreeAsJson() const;
void finishAllRendering();
int sourceAnimationFrameNumber() const;
bool initializeRenderer(scoped_ptr<OutputSurface>);
bool isContextLost();
TileManager* tileManager() { return m_tileManager.get(); }
Renderer* renderer() { return m_renderer.get(); }
const RendererCapabilities& rendererCapabilities() const;
bool swapBuffers();
void readback(void* pixels, const gfx::Rect&);
LayerTreeImpl* activeTree() { return m_activeTree.get(); }
const LayerTreeImpl* activeTree() const { return m_activeTree.get(); }
LayerTreeImpl* pendingTree() { return m_pendingTree.get(); }
const LayerTreeImpl* pendingTree() const { return m_pendingTree.get(); }
void createPendingTree();
void activatePendingTreeIfNeeded();
// TODO(nduca): Remove these in favor of LayerTreeImpl.
void setRootLayer(scoped_ptr<LayerImpl>);
LayerImpl* rootLayer() const;
// Release ownership of the current layer tree and replace it with an empty
// tree. Returns the root layer of the detached tree.
scoped_ptr<LayerImpl> detachLayerTree();
LayerImpl* rootScrollLayer() const;
// TOOD(nduca): This goes away when scrolling moves to LayerTreeImpl.
LayerImpl* currentlyScrollingLayer() const;
bool visible() const { return m_visible; }
void setVisible(bool);
bool contentsTexturesPurged() const { return m_contentsTexturesPurged; }
void setContentsTexturesPurged();
void resetContentsTexturesPurged();
size_t memoryAllocationLimitBytes() const { return m_managedMemoryPolicy.bytesLimitWhenVisible; }
void setViewportSize(const gfx::Size& layoutViewportSize, const gfx::Size& deviceViewportSize);
const gfx::Size& layoutViewportSize() const { return m_layoutViewportSize; }
float deviceScaleFactor() const { return m_deviceScaleFactor; }
void setDeviceScaleFactor(float);
float pageScaleFactor() const;
void setPageScaleFactorAndLimits(float pageScaleFactor, float minPageScaleFactor, float maxPageScaleFactor);
scoped_ptr<ScrollAndScaleSet> processScrollDeltas();
gfx::Transform implTransform() const;
void startPageScaleAnimation(gfx::Vector2d targetOffset, bool useAnchor, float scale, base::TimeDelta duration);
SkColor backgroundColor() const { return m_backgroundColor; }
void setBackgroundColor(SkColor color) { m_backgroundColor = color; }
bool hasTransparentBackground() const { return m_hasTransparentBackground; }
void setHasTransparentBackground(bool transparent) { m_hasTransparentBackground = transparent; }
bool needsAnimateLayers() const { return !m_animationRegistrar->active_animation_controllers().empty(); }
bool needsUpdateDrawProperties() const { return m_needsUpdateDrawProperties; }
void setNeedsUpdateDrawProperties() { m_needsUpdateDrawProperties = true; }
void setNeedsRedraw();
void renderingStats(RenderingStats*) const;
void sendManagedMemoryStats(
size_t memoryVisibleBytes,
size_t memoryVisibleAndNearbyBytes,
size_t memoryUseBytes);
FrameRateCounter* fpsCounter() const { return m_fpsCounter.get(); }
DebugRectHistory* debugRectHistory() const { return m_debugRectHistory.get(); }
ResourceProvider* resourceProvider() const { return m_resourceProvider.get(); }
Proxy* proxy() const { return m_proxy; }
AnimationRegistrar* animationRegistrar() const { return m_animationRegistrar.get(); }
void setDebugState(const LayerTreeDebugState& debugState) { m_debugState = debugState; }
const LayerTreeDebugState& debugState() const { return m_debugState; }
class CC_EXPORT CullRenderPassesWithCachedTextures {
public:
bool shouldRemoveRenderPass(const RenderPassDrawQuad&, const FrameData&) const;
// Iterates from the root first, in order to remove the surfaces closest
// to the root with cached textures, and all surfaces that draw into
// them.
size_t renderPassListBegin(const RenderPassList& list) const { return list.size() - 1; }
size_t renderPassListEnd(const RenderPassList&) const { return 0 - 1; }
size_t renderPassListNext(size_t it) const { return it - 1; }
CullRenderPassesWithCachedTextures(Renderer& renderer) : m_renderer(renderer) { }
private:
Renderer& m_renderer;
};
class CC_EXPORT CullRenderPassesWithNoQuads {
public:
bool shouldRemoveRenderPass(const RenderPassDrawQuad&, const FrameData&) const;
// Iterates in draw order, so that when a surface is removed, and its
// target becomes empty, then its target can be removed also.
size_t renderPassListBegin(const RenderPassList&) const { return 0; }
size_t renderPassListEnd(const RenderPassList& list) const { return list.size(); }
size_t renderPassListNext(size_t it) const { return it + 1; }
};
template<typename RenderPassCuller>
static void removeRenderPasses(RenderPassCuller, FrameData&);
float totalPageScaleFactorForTesting() const { return m_pinchZoomViewport.totalPageScaleFactor(); }
const PinchZoomViewport& pinchZoomViewport() const { return m_pinchZoomViewport; }
protected:
LayerTreeHostImpl(const LayerTreeSettings&, LayerTreeHostImplClient*, Proxy*);
void activatePendingTree();
// Virtual for testing.
virtual void animateLayers(base::TimeTicks monotonicTime, base::Time wallClockTime);
// Virtual for testing.
virtual base::TimeDelta lowFrequencyAnimationInterval() const;
const AnimationRegistrar::AnimationControllerMap& activeAnimationControllers() const { return m_animationRegistrar->active_animation_controllers(); }
LayerTreeHostImplClient* m_client;
Proxy* m_proxy;
private:
void animatePageScale(base::TimeTicks monotonicTime);
void animateScrollbars(base::TimeTicks monotonicTime);
void updateDrawProperties();
void computeDoubleTapZoomDeltas(ScrollAndScaleSet* scrollInfo);
void computePinchZoomDeltas(ScrollAndScaleSet* scrollInfo);
void makeScrollAndScaleSet(ScrollAndScaleSet* scrollInfo, gfx::Vector2d scrollOffset, float pageScale);
void setPageScaleDelta(float);
void updateMaxScrollOffset();
void trackDamageForAllSurfaces(LayerImpl* rootDrawLayer, const LayerList& renderSurfaceLayerList);
// Returns false if the frame should not be displayed. This function should
// only be called from prepareToDraw, as didDrawAllLayers must be called
// if this helper function is called.
bool calculateRenderPasses(FrameData&);
void animateLayersRecursive(LayerImpl*, base::TimeTicks monotonicTime, base::Time wallClockTime, AnimationEventsVector*, bool& didAnimate, bool& needsAnimateLayers);
void setBackgroundTickingEnabled(bool);
gfx::Size contentSize() const;
void sendDidLoseOutputSurfaceRecursive(LayerImpl*);
void clearRenderSurfaces();
bool ensureRenderSurfaceLayerList();
void clearCurrentlyScrollingLayer();
void animateScrollbarsRecursive(LayerImpl*, base::TimeTicks monotonicTime);
void dumpRenderSurfaces(std::string*, int indent, const LayerImpl*) const;
scoped_ptr<OutputSurface> m_outputSurface;
scoped_ptr<ResourceProvider> m_resourceProvider;
scoped_ptr<Renderer> m_renderer;
scoped_ptr<TileManager> m_tileManager;
scoped_ptr<LayerTreeImpl> m_pendingTree;
scoped_ptr<LayerTreeImpl> m_activeTree;
bool m_scrollDeltaIsInViewportSpace;
LayerTreeSettings m_settings;
LayerTreeDebugState m_debugState;
gfx::Size m_layoutViewportSize;
gfx::Size m_deviceViewportSize;
float m_deviceScaleFactor;
bool m_visible;
bool m_contentsTexturesPurged;
ManagedMemoryPolicy m_managedMemoryPolicy;
SkColor m_backgroundColor;
bool m_hasTransparentBackground;
bool m_needsUpdateDrawProperties;
bool m_pinchGestureActive;
gfx::Point m_previousPinchAnchor;
scoped_ptr<PageScaleAnimation> m_pageScaleAnimation;
// This is used for ticking animations slowly when hidden.
scoped_ptr<LayerTreeHostImplTimeSourceAdapter> m_timeSourceClientAdapter;
PinchZoomViewport m_pinchZoomViewport;
scoped_ptr<FrameRateCounter> m_fpsCounter;
scoped_ptr<DebugRectHistory> m_debugRectHistory;
int64 m_numImplThreadScrolls;
int64 m_numMainThreadScrolls;
int64 m_cumulativeNumLayersDrawn;
int64 m_cumulativeNumMissingTiles;
size_t m_lastSentMemoryVisibleBytes;
size_t m_lastSentMemoryVisibleAndNearbyBytes;
size_t m_lastSentMemoryUseBytes;
scoped_ptr<AnimationRegistrar> m_animationRegistrar;
DISALLOW_COPY_AND_ASSIGN(LayerTreeHostImpl);
};
} // namespace cc
#endif // CC_LAYER_TREE_HOST_IMPL_H_
|