[TextureMapper] Scrolling through 01.org/dleyna crashes WebKitWebProcess
[WebKit-https.git] / Source / WebCore / platform / graphics / texmap / coordinated / CoordinatedGraphicsLayer.h
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 #if USE(COORDINATED_GRAPHICS)
25
26 #include "CoordinatedGraphicsState.h"
27 #include "CoordinatedImageBacking.h"
28 #include "FloatPoint3D.h"
29 #include "GraphicsLayer.h"
30 #include "GraphicsLayerTransform.h"
31 #include "Image.h"
32 #include "IntSize.h"
33 #include "TextureMapperAnimation.h"
34 #include "TextureMapperPlatformLayer.h"
35 #include "TiledBackingStore.h"
36 #include "TiledBackingStoreClient.h"
37 #include "TransformationMatrix.h"
38 #if USE(GRAPHICS_SURFACE)
39 #include "GraphicsSurfaceToken.h"
40 #endif
41 #include <wtf/text/StringHash.h>
42
43 namespace WebCore {
44 class CoordinatedGraphicsLayer;
45 class TextureMapperAnimations;
46 class ScrollableArea;
47
48 class CoordinatedGraphicsLayerClient {
49 public:
50     virtual bool isFlushingLayerChanges() const = 0;
51     virtual FloatRect visibleContentsRect() const = 0;
52     virtual Ref<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
59 class CoordinatedGraphicsLayer : public GraphicsLayer
60     , public TiledBackingStoreClient
61 #if USE(COORDINATED_GRAPHICS_THREADED)
62     , public TextureMapperPlatformLayer::Client
63 #endif
64     , public CoordinatedImageBacking::Host {
65 public:
66     explicit CoordinatedGraphicsLayer(Type, GraphicsLayerClient&);
67     virtual ~CoordinatedGraphicsLayer();
68
69     PlatformLayerID primaryLayerID() const override { return id(); }
70
71     // Reimplementations from GraphicsLayer.h.
72     bool setChildren(const Vector<GraphicsLayer*>&) override;
73     void addChild(GraphicsLayer*) override;
74     void addChildAtIndex(GraphicsLayer*, int) override;
75     void addChildAbove(GraphicsLayer*, GraphicsLayer*) override;
76     void addChildBelow(GraphicsLayer*, GraphicsLayer*) override;
77     bool replaceChild(GraphicsLayer*, GraphicsLayer*) override;
78     void removeFromParent() override;
79     void setPosition(const FloatPoint&) override;
80     void setAnchorPoint(const FloatPoint3D&) override;
81     void setSize(const FloatSize&) override;
82     void setTransform(const TransformationMatrix&) override;
83     void setChildrenTransform(const TransformationMatrix&) override;
84     void setPreserves3D(bool) override;
85     void setMasksToBounds(bool) override;
86     void setDrawsContent(bool) override;
87     void setContentsVisible(bool) override;
88     void setContentsOpaque(bool) override;
89     void setBackfaceVisibility(bool) override;
90     void setOpacity(float) override;
91     void setContentsRect(const FloatRect&) override;
92     void setContentsTilePhase(const FloatSize&) override;
93     void setContentsTileSize(const FloatSize&) override;
94     void setContentsToImage(Image*) override;
95     void setContentsToSolidColor(const Color&) override;
96     void setShowDebugBorder(bool) override;
97     void setShowRepaintCounter(bool) override;
98     bool shouldDirectlyCompositeImage(Image*) const override;
99     void setContentsToPlatformLayer(PlatformLayer*, ContentsLayerPurpose) override;
100     void setMaskLayer(GraphicsLayer*) override;
101     void setReplicatedByLayer(GraphicsLayer*) override;
102     void setNeedsDisplay() override;
103     void setNeedsDisplayInRect(const FloatRect&, ShouldClipToLayer = ClipToLayer) override;
104     void setContentsNeedsDisplay() override;
105     void deviceOrPageScaleFactorChanged() override;
106     void flushCompositingState(const FloatRect&, bool) override;
107     void flushCompositingStateForThisLayerOnly(bool) override;
108     bool setFilters(const FilterOperations&) override;
109     bool addAnimation(const KeyframeValueList&, const FloatSize&, const Animation*, const String&, double) override;
110     void pauseAnimation(const String&, double) override;
111     void removeAnimation(const String&) override;
112     void suspendAnimations(double time) override;
113     void resumeAnimations() override;
114     bool usesContentsLayer() const override { return m_platformLayer || m_compositedImage; }
115
116     void syncPendingStateChangesIncludingSubLayers();
117     void updateContentBuffersIncludingSubLayers();
118
119     FloatPoint computePositionRelativeToBase();
120     void computePixelAlignment(FloatPoint& position, FloatSize&, FloatPoint3D& anchorPoint, FloatSize& alignmentOffset);
121
122     void setVisibleContentRectTrajectoryVector(const FloatPoint&);
123
124     void setScrollableArea(ScrollableArea*);
125     bool isScrollable() const { return !!m_scrollableArea; }
126     void commitScrollOffset(const IntSize&);
127
128     CoordinatedLayerID id() const { return m_id; }
129
130     void setFixedToViewport(bool isFixed);
131
132     IntRect coverRect() const { return m_mainBackingStore ? m_mainBackingStore->mapToContents(m_mainBackingStore->coverRect()) : IntRect(); }
133     IntRect transformedVisibleRect();
134
135     // TiledBackingStoreClient
136     void tiledBackingStorePaint(GraphicsContext&, const IntRect&) override;
137     void didUpdateTileBuffers() override;
138     void tiledBackingStoreHasPendingTileCreation() override;
139     void createTile(uint32_t tileID, float) override;
140     void updateTile(uint32_t tileID, const SurfaceUpdateInfo&, const IntRect&) override;
141     void removeTile(uint32_t tileID) override;
142     bool paintToSurface(const IntSize&, uint32_t& /* atlasID */, IntPoint&, CoordinatedSurface::Client&) override;
143
144     void setCoordinator(CoordinatedGraphicsLayerClient*);
145
146     void setNeedsVisibleRectAdjustment();
147     void purgeBackingStores();
148
149     CoordinatedGraphicsLayer* findFirstDescendantWithContentsRecursively();
150
151 private:
152     bool isCoordinatedGraphicsLayer() const override { return true; }
153
154 #if USE(GRAPHICS_SURFACE)
155     enum PendingPlatformLayerOperation {
156         None = 0x00,
157         CreatePlatformLayer = 0x01,
158         DestroyPlatformLayer = 0x02,
159         SyncPlatformLayer = 0x04,
160         CreateAndSyncPlatformLayer = CreatePlatformLayer | SyncPlatformLayer,
161         RecreatePlatformLayer = CreateAndSyncPlatformLayer | DestroyPlatformLayer
162     };
163
164     void destroyPlatformLayerIfNeeded();
165     void createPlatformLayerIfNeeded();
166 #endif
167     void syncPlatformLayer();
168 #if USE(COORDINATED_GRAPHICS_THREADED)
169     void platformLayerWillBeDestroyed() override;
170     void setPlatformLayerNeedsDisplay() override;
171 #endif
172
173     void setDebugBorder(const Color&, float width) override;
174
175     bool fixedToViewport() const { return m_fixedToViewport; }
176
177     void didChangeLayerState();
178     void didChangeAnimations();
179     void didChangeGeometry();
180     void didChangeChildren();
181     void didChangeFilters();
182     void didChangeImageBacking();
183
184     void resetLayerState();
185     void syncLayerState();
186     void syncAnimations();
187     void syncChildren();
188     void syncFilters();
189     void syncImageBacking();
190     void computeTransformedVisibleRect();
191     void updateContentBuffers();
192
193     void createBackingStore();
194     void releaseImageBackingIfNeeded();
195
196     void notifyFlushRequired();
197
198     // CoordinatedImageBacking::Host
199     bool imageBackingVisible() override;
200     bool shouldHaveBackingStore() const;
201     bool selfOrAncestorHasActiveTransformAnimation() const;
202     bool selfOrAncestorHaveNonAffineTransforms();
203     void adjustContentsScale();
204
205     void setShouldUpdateVisibleRect();
206     float effectiveContentsScale();
207
208     void animationStartedTimerFired();
209
210     bool filtersCanBeComposited(const FilterOperations&) const;
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_isValidPlatformLayer : 1;
236     unsigned m_pendingPlatformLayerOperation : 3;
237 #endif
238 #if USE(COORDINATED_GRAPHICS_THREADED)
239     bool m_shouldSyncPlatformLayer : 1;
240 #endif
241
242     CoordinatedGraphicsLayerClient* m_coordinator;
243     std::unique_ptr<TiledBackingStore> m_mainBackingStore;
244     std::unique_ptr<TiledBackingStore> m_previousBackingStore;
245
246     RefPtr<Image> m_compositedImage;
247     NativeImagePtr m_compositedNativeImagePtr;
248     RefPtr<CoordinatedImageBacking> m_coordinatedImageBacking;
249
250     PlatformLayer* m_platformLayer;
251 #if USE(GRAPHICS_SURFACE)
252     IntSize m_platformLayerSize;
253     GraphicsSurfaceToken m_platformLayerToken;
254 #endif
255     Timer m_animationStartedTimer;
256     TextureMapperAnimations m_animations;
257     double m_lastAnimationStartTime;
258
259     ScrollableArea* m_scrollableArea;
260 };
261
262 } // namespace WebCore
263
264 SPECIALIZE_TYPE_TRAITS_GRAPHICSLAYER(WebCore::CoordinatedGraphicsLayer, isCoordinatedGraphicsLayer())
265
266 #endif // USE(COORDINATED_GRAPHICS)
267
268 #endif // CoordinatedGraphicsLayer_h