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
3 // found in the LICENSE file.
5 #ifndef CC_LAYER_TREE_HOST_H_
6 #define CC_LAYER_TREE_HOST_H_
10 #include "base/basictypes.h"
11 #include "base/cancelable_callback.h"
12 #include "base/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/time.h"
16 #include "cc/animation_events.h"
17 #include "cc/cc_export.h"
18 #include "cc/graphics_context.h"
19 #include "cc/layer_tree_host_client.h"
20 #include "cc/layer_tree_host_common.h"
21 #include "cc/occlusion_tracker.h"
22 #include "cc/prioritized_resource_manager.h"
24 #include "cc/rate_limiter.h"
25 #include "cc/rendering_stats.h"
26 #include "cc/scoped_ptr_vector.h"
27 #include "third_party/skia/include/core/SkColor.h"
28 #include "ui/gfx/rect.h"
30 #if defined(COMPILER_GCC)
31 namespace BASE_HASH_NAMESPACE
{
33 struct hash
<WebKit::WebGraphicsContext3D
*> {
34 size_t operator()(WebKit::WebGraphicsContext3D
* ptr
) const {
35 return hash
<size_t>()(reinterpret_cast<size_t>(ptr
));
38 } // namespace BASE_HASH_NAMESPACE
45 class LayerTreeHostImpl
;
46 class LayerTreeHostImplClient
;
47 class PrioritizedResourceManager
;
48 class ResourceUpdateQueue
;
49 class HeadsUpDisplayLayer
;
51 struct ScrollAndScaleSet
;
53 struct CC_EXPORT LayerTreeSettings
{
57 bool acceleratePainting
;
58 bool showPlatformLayerTree
;
60 bool showPropertyChangedRects
;
61 bool showSurfaceDamageRects
;
62 bool showScreenSpaceRects
;
63 bool showReplicaScreenSpaceRects
;
64 bool showOccludingRects
;
65 bool renderVSyncEnabled
;
67 size_t maxPartialTextureUpdates
;
68 gfx::Size defaultTileSize
;
69 gfx::Size maxUntiledLayerSize
;
70 gfx::Size minimumOcclusionTrackingSize
;
72 bool showDebugInfo() const { return showPlatformLayerTree
|| showDebugRects(); }
73 bool showDebugRects() const { return showPaintRects
|| showPropertyChangedRects
|| showSurfaceDamageRects
|| showScreenSpaceRects
|| showReplicaScreenSpaceRects
|| showOccludingRects
; }
76 // Provides information on an Impl's rendering capabilities back to the LayerTreeHost
77 struct CC_EXPORT RendererCapabilities
{
78 RendererCapabilities();
79 ~RendererCapabilities();
81 GLenum bestTextureFormat
;
82 bool contextHasCachedFrontBuffer
;
83 bool usingPartialSwap
;
84 bool usingAcceleratedPainting
;
85 bool usingSetVisibility
;
86 bool usingSwapCompleteCallback
;
87 bool usingGpuMemoryManager
;
88 bool usingDiscardFramebuffer
;
93 class CC_EXPORT LayerTreeHost
: public RateLimiterClient
{
95 static scoped_ptr
<LayerTreeHost
> create(LayerTreeHostClient
*, const LayerTreeSettings
&, scoped_ptr
<Thread
> implThread
);
96 virtual ~LayerTreeHost();
98 void setSurfaceReady();
100 // Returns true if any LayerTreeHost is alive.
101 static bool anyLayerTreeHostInstanceExists();
103 static bool needsFilterContext() { return s_needsFilterContext
; }
104 static void setNeedsFilterContext(bool needsFilterContext
) { s_needsFilterContext
= needsFilterContext
; }
105 bool needsSharedContext() const { return needsFilterContext() || settings().acceleratePainting
; }
107 // LayerTreeHost interface to Proxy.
108 void willBeginFrame() { m_client
->willBeginFrame(); }
109 void didBeginFrame() { m_client
->didBeginFrame(); }
110 void updateAnimations(base::TimeTicks monotonicFrameBeginTime
);
112 void beginCommitOnImplThread(LayerTreeHostImpl
*);
113 void finishCommitOnImplThread(LayerTreeHostImpl
*);
115 void commitComplete();
116 scoped_ptr
<GraphicsContext
> createContext();
117 scoped_ptr
<InputHandler
> createInputHandler();
118 virtual scoped_ptr
<LayerTreeHostImpl
> createLayerTreeHostImpl(LayerTreeHostImplClient
*);
119 void didLoseContext();
120 enum RecreateResult
{
122 RecreateFailedButTryAgain
,
123 RecreateFailedAndGaveUp
,
125 RecreateResult
recreateContext();
126 void didCommitAndDrawFrame() { m_client
->didCommitAndDrawFrame(); }
127 void didCompleteSwapBuffers() { m_client
->didCompleteSwapBuffers(); }
128 void deleteContentsTexturesOnImplThread(ResourceProvider
*);
129 virtual void acquireLayerTextures();
130 // Returns false if we should abort this frame due to initialization failure.
131 bool initializeRendererIfNeeded();
132 void updateLayers(ResourceUpdateQueue
&, size_t contentsMemoryLimitBytes
);
134 LayerTreeHostClient
* client() { return m_client
; }
138 // Only used when compositing on the main thread.
139 void scheduleComposite();
141 // Composites and attempts to read back the result into the provided
142 // buffer. If it wasn't possible, e.g. due to context lost, will return
144 bool compositeAndReadback(void *pixels
, const gfx::Rect
&);
146 void finishAllRendering();
148 void setDeferCommits(bool deferCommits
);
151 virtual void didDeferCommit();
153 int commitNumber() const { return m_commitNumber
; }
155 void renderingStats(RenderingStats
*) const;
157 const RendererCapabilities
& rendererCapabilities() const;
160 void loseContext(int numTimes
);
162 void setNeedsAnimate();
163 // virtual for testing
164 virtual void setNeedsCommit();
165 void setNeedsRedraw();
166 bool commitRequested() const;
168 void setAnimationEvents(scoped_ptr
<AnimationEventsVector
>, base::Time wallClockTime
);
169 virtual void didAddAnimation();
171 Layer
* rootLayer() { return m_rootLayer
.get(); }
172 const Layer
* rootLayer() const { return m_rootLayer
.get(); }
173 void setRootLayer(scoped_refptr
<Layer
>);
175 const LayerTreeSettings
& settings() const { return m_settings
; }
177 void setViewportSize(const gfx::Size
& layoutViewportSize
, const gfx::Size
& deviceViewportSize
);
179 const gfx::Size
& layoutViewportSize() const { return m_layoutViewportSize
; }
180 const gfx::Size
& deviceViewportSize() const { return m_deviceViewportSize
; }
182 void setPageScaleFactorAndLimits(float pageScaleFactor
, float minPageScaleFactor
, float maxPageScaleFactor
);
184 void setBackgroundColor(SkColor color
) { m_backgroundColor
= color
; }
186 void setHasTransparentBackground(bool transparent
) { m_hasTransparentBackground
= transparent
; }
188 PrioritizedResourceManager
* contentsTextureManager() const;
190 bool visible() const { return m_visible
; }
191 void setVisible(bool);
193 void startPageScaleAnimation(gfx::Vector2d targetOffset
, bool useAnchor
, float scale
, base::TimeDelta duration
);
195 void applyScrollAndScale(const ScrollAndScaleSet
&);
196 gfx::PointF
adjustEventPointForPinchZoom(const gfx::PointF
&) const;
197 void setImplTransform(const WebKit::WebTransformationMatrix
&);
199 void startRateLimiter(WebKit::WebGraphicsContext3D
*);
200 void stopRateLimiter(WebKit::WebGraphicsContext3D
*);
202 // RateLimitClient implementation
203 virtual void rateLimit() OVERRIDE
;
205 bool bufferedUpdates();
206 bool requestPartialTextureUpdate();
208 void setDeviceScaleFactor(float);
209 float deviceScaleFactor() const { return m_deviceScaleFactor
; }
211 void setShowFPSCounter(bool show
);
212 void setFontAtlas(scoped_ptr
<FontAtlas
>);
214 HeadsUpDisplayLayer
* hudLayer() const { return m_hudLayer
.get(); }
216 Proxy
* proxy() const { return m_proxy
.get(); }
219 LayerTreeHost(LayerTreeHostClient
*, const LayerTreeSettings
&);
220 bool initialize(scoped_ptr
<Thread
> implThread
);
223 typedef std::vector
<scoped_refptr
<Layer
> > LayerList
;
225 void initializeRenderer();
227 void update(Layer
*, ResourceUpdateQueue
&, const OcclusionTracker
*);
228 bool paintLayerContents(const LayerList
&, ResourceUpdateQueue
&);
229 bool paintMasksForRenderSurface(Layer
*, ResourceUpdateQueue
&);
231 void updateLayers(Layer
*, ResourceUpdateQueue
&);
232 void triggerPrepaint();
234 void prioritizeTextures(const LayerList
&, OverdrawMetrics
&);
235 void setPrioritiesForSurfaces(size_t surfaceMemoryBytes
);
236 void setPrioritiesForLayers(const LayerList
&);
237 size_t calculateMemoryForRenderSurfaces(const LayerList
& updateList
);
239 void animateLayers(base::TimeTicks monotonicTime
);
240 bool animateLayersRecursive(Layer
* current
, base::TimeTicks time
);
241 void setAnimationEventsRecursive(const AnimationEventsVector
&, Layer
*, base::Time wallClockTime
);
243 void createHUDLayerIfNeeded();
246 bool m_needsAnimateLayers
;
248 base::CancelableClosure m_prepaintCallback
;
250 LayerTreeHostClient
* m_client
;
251 scoped_ptr
<Proxy
> m_proxy
;
254 RenderingStats m_renderingStats
;
256 bool m_rendererInitialized
;
258 int m_numTimesRecreateShouldFail
;
259 int m_numFailedRecreateAttempts
;
261 scoped_refptr
<Layer
> m_rootLayer
;
262 scoped_refptr
<HeadsUpDisplayLayer
> m_hudLayer
;
264 scoped_ptr
<PrioritizedResourceManager
> m_contentsTextureManager
;
265 scoped_ptr
<PrioritizedResource
> m_surfaceMemoryPlaceholder
;
267 LayerTreeSettings m_settings
;
269 gfx::Size m_layoutViewportSize
;
270 gfx::Size m_deviceViewportSize
;
271 float m_deviceScaleFactor
;
275 typedef base::hash_map
<WebKit::WebGraphicsContext3D
*, scoped_refptr
<RateLimiter
> > RateLimiterMap
;
276 RateLimiterMap m_rateLimiters
;
278 float m_pageScaleFactor
;
279 float m_minPageScaleFactor
, m_maxPageScaleFactor
;
280 WebKit::WebTransformationMatrix m_implTransform
;
281 bool m_triggerIdleUpdates
;
283 SkColor m_backgroundColor
;
284 bool m_hasTransparentBackground
;
286 typedef ScopedPtrVector
<PrioritizedResource
> TextureList
;
287 size_t m_partialTextureUpdateRequests
;
289 static bool s_needsFilterContext
;
291 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost
);
296 #endif // CC_LAYER_TREE_HOST_H_