1/*
2 Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB.  If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20
21#ifndef CoordinatedGraphicsLayer_h
22#define CoordinatedGraphicsLayer_h
23
24#include "CoordinatedGraphicsState.h"
25#include "CoordinatedImageBacking.h"
26#include "CoordinatedTile.h"
27#include "FloatPoint3D.h"
28#include "GraphicsLayer.h"
29#include "GraphicsLayerAnimation.h"
30#include "GraphicsLayerTransform.h"
31#include "Image.h"
32#include "IntSize.h"
33#include "TiledBackingStore.h"
34#include "TiledBackingStoreClient.h"
35#include "TransformationMatrix.h"
36#if USE(GRAPHICS_SURFACE)
37#include "GraphicsSurfaceToken.h"
38#endif
39#include <wtf/text/StringHash.h>
40
41#if USE(COORDINATED_GRAPHICS)
42
43namespace WebCore {
44class CoordinatedGraphicsLayer;
45class GraphicsLayerAnimations;
46class ScrollableArea;
47
48class CoordinatedGraphicsLayerClient {
49public:
50    virtual bool isFlushingLayerChanges() const = 0;
51    virtual FloatRect visibleContentsRect() const = 0;
52    virtual PassRefPtr<CoordinatedImageBacking> createImageBackingIfNeeded(Image*) = 0;
53    virtual void detachLayer(CoordinatedGraphicsLayer*) = 0;
54    virtual bool paintToSurface(const IntSize&, CoordinatedSurface::Flags, uint32_t& atlasID, IntPoint&, CoordinatedSurface::Client*) = 0;
55
56    virtual void syncLayerState(CoordinatedLayerID, CoordinatedGraphicsLayerState&) = 0;
57};
58
59class CoordinatedGraphicsLayer : public GraphicsLayer
60    , public TiledBackingStoreClient
61    , public CoordinatedImageBacking::Host
62    , public CoordinatedTileClient {
63public:
64    explicit CoordinatedGraphicsLayer(GraphicsLayerClient&);
65    virtual ~CoordinatedGraphicsLayer();
66
67    // Reimplementations from GraphicsLayer.h.
68    virtual bool setChildren(const Vector<GraphicsLayer*>&) override;
69    virtual void addChild(GraphicsLayer*) override;
70    virtual void addChildAtIndex(GraphicsLayer*, int) override;
71    virtual void addChildAbove(GraphicsLayer*, GraphicsLayer*) override;
72    virtual void addChildBelow(GraphicsLayer*, GraphicsLayer*) override;
73    virtual bool replaceChild(GraphicsLayer*, GraphicsLayer*) override;
74    virtual void removeFromParent() override;
75    virtual void setPosition(const FloatPoint&) override;
76    virtual void setAnchorPoint(const FloatPoint3D&) override;
77    virtual void setSize(const FloatSize&) override;
78    virtual void setTransform(const TransformationMatrix&) override;
79    virtual void setChildrenTransform(const TransformationMatrix&) override;
80    virtual void setPreserves3D(bool) override;
81    virtual void setMasksToBounds(bool) override;
82    virtual void setDrawsContent(bool) override;
83    virtual void setContentsVisible(bool) override;
84    virtual void setContentsOpaque(bool) override;
85    virtual void setBackfaceVisibility(bool) override;
86    virtual void setOpacity(float) override;
87    virtual void setContentsRect(const FloatRect&) override;
88    virtual void setContentsTilePhase(const FloatPoint&) override;
89    virtual void setContentsTileSize(const FloatSize&) override;
90    virtual void setContentsToImage(Image*) override;
91    virtual void setContentsToSolidColor(const Color&) override;
92    virtual void setShowDebugBorder(bool) override;
93    virtual void setShowRepaintCounter(bool) override;
94    virtual bool shouldDirectlyCompositeImage(Image*) const override;
95    virtual void setContentsToCanvas(PlatformLayer*) override;
96    virtual void setMaskLayer(GraphicsLayer*) override;
97    virtual void setReplicatedByLayer(GraphicsLayer*) override;
98    virtual void setNeedsDisplay() override;
99    virtual void setNeedsDisplayInRect(const FloatRect&, ShouldClipToLayer = ClipToLayer) override;
100    virtual void setContentsNeedsDisplay() override;
101    virtual void deviceOrPageScaleFactorChanged() override;
102    virtual void flushCompositingState(const FloatRect&) override;
103    virtual void flushCompositingStateForThisLayerOnly() override;
104#if ENABLE(CSS_FILTERS)
105    virtual bool setFilters(const FilterOperations&) override;
106#endif
107    virtual bool addAnimation(const KeyframeValueList&, const FloatSize&, const Animation*, const String&, double) override;
108    virtual void pauseAnimation(const String&, double) override;
109    virtual void removeAnimation(const String&) override;
110    virtual void suspendAnimations(double time) override;
111    virtual void resumeAnimations() override;
112    virtual bool usesContentsLayer() const override { return m_canvasPlatformLayer || m_compositedImage; }
113
114    void syncPendingStateChangesIncludingSubLayers();
115    void updateContentBuffersIncludingSubLayers();
116
117    FloatPoint computePositionRelativeToBase();
118    void computePixelAlignment(FloatPoint& position, FloatSize&, FloatPoint3D& anchorPoint, FloatSize& alignmentOffset);
119
120    void setVisibleContentRectTrajectoryVector(const FloatPoint&);
121
122    void setScrollableArea(ScrollableArea*);
123    bool isScrollable() const { return !!m_scrollableArea; }
124    void commitScrollOffset(const IntSize&);
125
126    CoordinatedLayerID id() const;
127
128    void setFixedToViewport(bool isFixed);
129
130    IntRect coverRect() const { return m_mainBackingStore ? m_mainBackingStore->mapToContents(m_mainBackingStore->coverRect()) : IntRect(); }
131
132    // TiledBackingStoreClient
133    virtual void tiledBackingStorePaintBegin() override;
134    virtual void tiledBackingStorePaint(GraphicsContext*, const IntRect&) override;
135    virtual void tiledBackingStorePaintEnd(const Vector<IntRect>& paintedArea) override;
136    virtual void tiledBackingStoreHasPendingTileCreation() override;
137    virtual IntRect tiledBackingStoreContentsRect() override;
138    virtual IntRect tiledBackingStoreVisibleRect() override;
139    virtual Color tiledBackingStoreBackgroundColor() const override;
140
141    // CoordinatedTileClient
142    virtual void createTile(uint32_t tileID, const SurfaceUpdateInfo&, const IntRect&) override;
143    virtual void updateTile(uint32_t tileID, const SurfaceUpdateInfo&, const IntRect&) override;
144    virtual void removeTile(uint32_t tileID) override;
145    virtual bool paintToSurface(const IntSize&, uint32_t& /* atlasID */, IntPoint&, CoordinatedSurface::Client*) override;
146
147    void setCoordinator(CoordinatedGraphicsLayerClient*);
148
149    void setNeedsVisibleRectAdjustment();
150    void purgeBackingStores();
151
152    static void setShouldSupportContentsTiling(bool);
153    CoordinatedGraphicsLayer* findFirstDescendantWithContentsRecursively();
154
155private:
156#if USE(GRAPHICS_SURFACE)
157    enum PendingCanvasOperation {
158        None = 0x00,
159        CreateCanvas = 0x01,
160        DestroyCanvas = 0x02,
161        SyncCanvas = 0x04,
162        CreateAndSyncCanvas = CreateCanvas | SyncCanvas,
163        RecreateCanvas = CreateAndSyncCanvas | DestroyCanvas
164    };
165
166    void syncCanvas();
167    void destroyCanvasIfNeeded();
168    void createCanvasIfNeeded();
169#endif
170
171    virtual void setDebugBorder(const Color&, float width) override;
172
173    bool fixedToViewport() const { return m_fixedToViewport; }
174
175    void didChangeLayerState();
176    void didChangeAnimations();
177    void didChangeGeometry();
178    void didChangeChildren();
179#if ENABLE(CSS_FILTERS)
180    void didChangeFilters();
181#endif
182    void didChangeImageBacking();
183
184    void resetLayerState();
185    void syncLayerState();
186    void syncAnimations();
187    void syncChildren();
188#if ENABLE(CSS_FILTERS)
189    void syncFilters();
190#endif
191    void syncImageBacking();
192    void computeTransformedVisibleRect();
193    void updateContentBuffers();
194
195    void createBackingStore();
196    void releaseImageBackingIfNeeded();
197
198    bool notifyFlushRequired();
199
200    // CoordinatedImageBacking::Host
201    virtual bool imageBackingVisible() override;
202    bool shouldHaveBackingStore() const;
203    bool selfOrAncestorHasActiveTransformAnimation() const;
204    bool selfOrAncestorHaveNonAffineTransforms();
205    void adjustContentsScale();
206
207    void setShouldUpdateVisibleRect();
208    float effectiveContentsScale();
209
210    void animationStartedTimerFired(Timer<CoordinatedGraphicsLayer>*);
211
212    CoordinatedLayerID m_id;
213    CoordinatedGraphicsLayerState m_layerState;
214    GraphicsLayerTransform m_layerTransform;
215    TransformationMatrix m_cachedInverseTransform;
216    FloatSize m_pixelAlignmentOffset;
217    FloatSize m_adjustedSize;
218    FloatPoint m_adjustedPosition;
219    FloatPoint3D m_adjustedAnchorPoint;
220
221#ifndef NDEBUG
222    bool m_isPurging;
223#endif
224    bool m_shouldUpdateVisibleRect: 1;
225    bool m_shouldSyncLayerState: 1;
226    bool m_shouldSyncChildren: 1;
227    bool m_shouldSyncFilters: 1;
228    bool m_shouldSyncImageBacking: 1;
229    bool m_shouldSyncAnimations: 1;
230    bool m_fixedToViewport : 1;
231    bool m_movingVisibleRect : 1;
232    bool m_pendingContentsScaleAdjustment : 1;
233    bool m_pendingVisibleRectAdjustment : 1;
234#if USE(GRAPHICS_SURFACE)
235    bool m_isValidCanvas : 1;
236    unsigned m_pendingCanvasOperation : 3;
237#endif
238
239    CoordinatedGraphicsLayerClient* m_coordinator;
240    std::unique_ptr<TiledBackingStore> m_mainBackingStore;
241    std::unique_ptr<TiledBackingStore> m_previousBackingStore;
242
243    RefPtr<Image> m_compositedImage;
244    NativeImagePtr m_compositedNativeImagePtr;
245    RefPtr<CoordinatedImageBacking> m_coordinatedImageBacking;
246
247    PlatformLayer* m_canvasPlatformLayer;
248#if USE(GRAPHICS_SURFACE)
249    IntSize m_canvasSize;
250    GraphicsSurfaceToken m_canvasToken;
251#endif
252    Timer<CoordinatedGraphicsLayer> m_animationStartedTimer;
253    GraphicsLayerAnimations m_animations;
254    double m_lastAnimationStartTime;
255
256    ScrollableArea* m_scrollableArea;
257};
258
259CoordinatedGraphicsLayer* toCoordinatedGraphicsLayer(GraphicsLayer*);
260
261} // namespace WebCore
262#endif // USE(COORDINATED_GRAPHICS)
263
264#endif // CoordinatedGraphicsLayer_h
265