1/*
2 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3 * Copyright (C) 2013 Company 100, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef CompositingCoordinator_h
28#define CompositingCoordinator_h
29
30#if USE(COORDINATED_GRAPHICS)
31
32#include "CoordinatedGraphicsLayer.h"
33#include "CoordinatedGraphicsState.h"
34#include "CoordinatedImageBacking.h"
35#include "FloatPoint.h"
36#include "GraphicsLayerClient.h"
37#include "GraphicsLayerFactory.h"
38#include "IntRect.h"
39#include "Timer.h"
40#include "UpdateAtlas.h"
41
42namespace WebCore {
43
44class Page;
45class GraphicsContext;
46class GraphicsLayer;
47class CoordinatedSurface;
48
49class CompositingCoordinator : public GraphicsLayerClient
50    , public CoordinatedGraphicsLayerClient
51    , public CoordinatedImageBacking::Client
52    , public UpdateAtlas::Client
53    , public GraphicsLayerFactory {
54    WTF_MAKE_NONCOPYABLE(CompositingCoordinator); WTF_MAKE_FAST_ALLOCATED;
55public:
56    class Client {
57    public:
58        virtual void didFlushRootLayer(const FloatRect& visibleContentRect) = 0;
59        virtual void notifyFlushRequired() = 0;
60        virtual void commitSceneState(const CoordinatedGraphicsState&) = 0;
61        virtual void paintLayerContents(const GraphicsLayer*, GraphicsContext&, const IntRect& clipRect) = 0;
62    };
63
64    CompositingCoordinator(Page*, CompositingCoordinator::Client*);
65    virtual ~CompositingCoordinator();
66
67    void setRootCompositingLayer(GraphicsLayer* compositingLayer, GraphicsLayer* overlayLayer);
68    void sizeDidChange(const IntSize& newSize);
69    void deviceOrPageScaleFactorChanged();
70
71    void setVisibleContentsRect(const FloatRect&, const FloatPoint&);
72    void renderNextFrame();
73    void purgeBackingStores();
74    void commitScrollOffset(uint32_t layerID, const IntSize& offset);
75
76    void createRootLayer(const IntSize&);
77    void clearRootLayer() { m_rootLayer = nullptr; }
78    GraphicsLayer* rootLayer() const { return m_rootLayer.get(); }
79    CoordinatedGraphicsLayer* mainContentsLayer();
80
81    bool flushPendingLayerChanges();
82    CoordinatedGraphicsState& state() { return m_state; }
83
84    void syncDisplayState();
85
86#if ENABLE(REQUEST_ANIMATION_FRAME)
87    double nextAnimationServiceTime() const;
88#endif
89
90private:
91    // GraphicsLayerClient
92    virtual void notifyAnimationStarted(const GraphicsLayer*, double time) override;
93    virtual void notifyFlushRequired(const GraphicsLayer*) override;
94    virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& clipRect) override;
95    virtual float deviceScaleFactor() const override;
96    virtual float pageScaleFactor() const override;
97
98    // CoordinatedImageBacking::Client
99    virtual void createImageBacking(CoordinatedImageBackingID) override;
100    virtual void updateImageBacking(CoordinatedImageBackingID, PassRefPtr<CoordinatedSurface>) override;
101    virtual void clearImageBackingContents(CoordinatedImageBackingID) override;
102    virtual void removeImageBacking(CoordinatedImageBackingID) override;
103
104    // CoordinatedGraphicsLayerClient
105    virtual bool isFlushingLayerChanges() const override { return m_isFlushingLayerChanges; }
106    virtual FloatRect visibleContentsRect() const override;
107    virtual PassRefPtr<CoordinatedImageBacking> createImageBackingIfNeeded(Image*) override;
108    virtual void detachLayer(CoordinatedGraphicsLayer*) override;
109    virtual bool paintToSurface(const WebCore::IntSize&, WebCore::CoordinatedSurface::Flags, uint32_t& /* atlasID */, WebCore::IntPoint&, WebCore::CoordinatedSurface::Client*) override;
110    virtual void syncLayerState(CoordinatedLayerID, CoordinatedGraphicsLayerState&) override;
111
112    // UpdateAtlas::Client
113    virtual void createUpdateAtlas(uint32_t atlasID, PassRefPtr<CoordinatedSurface>) override;
114    virtual void removeUpdateAtlas(uint32_t atlasID) override;
115
116    // GraphicsLayerFactory
117    virtual std::unique_ptr<GraphicsLayer> createGraphicsLayer(GraphicsLayerClient&) override;
118
119    void initializeRootCompositingLayerIfNeeded();
120    void flushPendingImageBackingChanges();
121    void clearPendingStateChanges();
122
123    void scheduleReleaseInactiveAtlases();
124
125    void releaseInactiveAtlasesTimerFired(Timer<CompositingCoordinator>*);
126
127    Page* m_page;
128    CompositingCoordinator::Client* m_client;
129
130    std::unique_ptr<GraphicsLayer> m_rootLayer;
131    GraphicsLayer* m_rootCompositingLayer;
132
133    CoordinatedGraphicsState m_state;
134
135    typedef HashMap<CoordinatedLayerID, CoordinatedGraphicsLayer*> LayerMap;
136    LayerMap m_registeredLayers;
137    typedef HashMap<CoordinatedImageBackingID, RefPtr<CoordinatedImageBacking> > ImageBackingMap;
138    ImageBackingMap m_imageBackings;
139    Vector<std::unique_ptr<UpdateAtlas>> m_updateAtlases;
140
141    // We don't send the messages related to releasing resources to renderer during purging, because renderer already had removed all resources.
142    bool m_isPurging;
143    bool m_isFlushingLayerChanges;
144
145    FloatRect m_visibleContentsRect;
146
147    bool m_shouldSyncFrame;
148    bool m_didInitializeRootCompositingLayer;
149    Timer<CompositingCoordinator> m_releaseInactiveAtlasesTimer;
150
151#if ENABLE(REQUEST_ANIMATION_FRAME)
152    double m_lastAnimationServiceTime;
153#endif
154};
155
156}
157
158#endif
159
160#endif // CompositingCoordinator_h
161