80bf28844ab091165bb4e405dffebd98ff5b3da8
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebPage / mac / PlatformCALayerRemote.cpp
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #if USE(ACCELERATED_COMPOSITING)
29
30 #import "PlatformCALayerRemote.h"
31
32 #import "PlatformCALayerRemoteCustom.h"
33 #import "PlatformCALayerRemoteTiledBacking.h"
34 #import "RemoteLayerBackingStore.h"
35 #import "RemoteLayerTreeContext.h"
36 #import <WebCore/AnimationUtilities.h>
37 #import <WebCore/GraphicsContext.h>
38 #import <WebCore/GraphicsLayerCA.h>
39 #import <WebCore/LengthFunctions.h>
40 #import <WebCore/PlatformCAFilters.h>
41 #import <WebCore/PlatformCALayerMac.h>
42 #import <WebCore/TiledBacking.h>
43 #import <wtf/CurrentTime.h>
44 #import <wtf/RetainPtr.h>
45
46 using namespace WebCore;
47 using namespace WebKit;
48
49 static RemoteLayerTreeTransaction::LayerID generateLayerID()
50 {
51     static RemoteLayerTreeTransaction::LayerID layerID;
52     return ++layerID;
53 }
54
55 static PlatformCALayerRemote* toPlatformCALayerRemote(PlatformCALayer* layer)
56 {
57     ASSERT_WITH_SECURITY_IMPLICATION(!layer || layer->isRemote());
58     return static_cast<PlatformCALayerRemote*>(layer);
59 }
60
61 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::create(LayerType layerType, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
62 {
63     RefPtr<PlatformCALayerRemote> layer;
64
65     if (layerType == LayerTypeTiledBackingLayer ||  layerType == LayerTypePageTiledBackingLayer)
66         layer = adoptRef(new PlatformCALayerRemoteTiledBacking(layerType, owner, context));
67     else
68         layer = adoptRef(new PlatformCALayerRemote(layerType, owner, context));
69
70     context->layerWasCreated(layer.get(), layerType);
71
72     return layer.release();
73 }
74
75 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::create(PlatformLayer *platformLayer, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
76 {
77     RefPtr<PlatformCALayerRemote> layer = adoptRef(new PlatformCALayerRemoteCustom(static_cast<PlatformLayer*>(platformLayer), owner, context));
78
79     context->layerWasCreated(layer.get(), LayerTypeCustom);
80
81     return layer.release();
82 }
83
84 PlatformCALayerRemote::PlatformCALayerRemote(LayerType layerType, PlatformCALayerClient* owner, RemoteLayerTreeContext* context)
85     : PlatformCALayer(layerType, owner)
86     , m_layerID(generateLayerID())
87     , m_superlayer(nullptr)
88     , m_acceleratesDrawing(false)
89     , m_context(context)
90 {
91     // FIXME: match all default values from CA.
92     setContentsScale(1);
93 }
94
95 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::clone(PlatformCALayerClient* owner) const
96 {
97     return nullptr;
98 }
99
100 PlatformCALayerRemote::~PlatformCALayerRemote()
101 {
102     for (const auto& layer : m_children)
103         toPlatformCALayerRemote(layer.get())->m_superlayer = nullptr;
104     m_context->layerWillBeDestroyed(this);
105 }
106
107 void PlatformCALayerRemote::recursiveBuildTransaction(RemoteLayerTreeTransaction& transaction)
108 {
109     if (m_properties.backingStore.display())
110         m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackingStoreChanged);
111
112     if (m_properties.changedProperties != RemoteLayerTreeTransaction::NoChange) {
113         if (m_properties.changedProperties & RemoteLayerTreeTransaction::ChildrenChanged) {
114             m_properties.children.clear();
115             for (auto layer : m_children)
116                 m_properties.children.append(toPlatformCALayerRemote(layer.get())->layerID());
117         }
118
119         transaction.layerPropertiesChanged(this, m_properties);
120         m_properties.changedProperties = RemoteLayerTreeTransaction::NoChange;
121     }
122
123     for (size_t i = 0; i < m_children.size(); ++i) {
124         PlatformCALayerRemote* child = toPlatformCALayerRemote(m_children[i].get());
125         ASSERT(child->superlayer() == this);
126         child->recursiveBuildTransaction(transaction);
127     }
128 }
129
130 void PlatformCALayerRemote::animationStarted(CFTimeInterval beginTime)
131 {
132 }
133
134 void PlatformCALayerRemote::ensureBackingStore()
135 {
136     m_properties.backingStore.ensureBackingStore(this, expandedIntSize(m_properties.size), m_properties.contentsScale, m_acceleratesDrawing);
137 }
138
139 void PlatformCALayerRemote::setNeedsDisplay(const FloatRect* rect)
140 {
141     ensureBackingStore();
142
143     if (!rect) {
144         m_properties.backingStore.setNeedsDisplay();
145         return;
146     }
147
148     // FIXME: Need to map this through contentsRect/etc.
149     m_properties.backingStore.setNeedsDisplay(enclosingIntRect(*rect));
150 }
151
152 void PlatformCALayerRemote::setContentsChanged()
153 {
154 }
155
156 PlatformCALayer* PlatformCALayerRemote::superlayer() const
157 {
158     return m_superlayer;
159 }
160
161 void PlatformCALayerRemote::removeFromSuperlayer()
162 {
163     if (!m_superlayer)
164         return;
165
166     m_superlayer->removeSublayer(this);
167 }
168
169 void PlatformCALayerRemote::removeSublayer(PlatformCALayerRemote* layer)
170 {
171     size_t childIndex = m_children.find(layer);
172     if (childIndex != notFound)
173         m_children.remove(childIndex);
174     layer->m_superlayer = nullptr;
175     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
176 }
177
178 void PlatformCALayerRemote::setSublayers(const PlatformCALayerList& list)
179 {
180     removeAllSublayers();
181     m_children = list;
182
183     for (const auto& layer : list) {
184         layer->removeFromSuperlayer();
185         toPlatformCALayerRemote(layer.get())->m_superlayer = this;
186     }
187
188     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
189 }
190
191 void PlatformCALayerRemote::removeAllSublayers()
192 {
193     PlatformCALayerList layersToRemove = m_children;
194     for (const auto& layer : layersToRemove)
195         layer->removeFromSuperlayer();
196     ASSERT(m_children.isEmpty());
197     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
198 }
199
200 void PlatformCALayerRemote::appendSublayer(PlatformCALayer* layer)
201 {
202     RefPtr<PlatformCALayer> layerProtector(layer);
203
204     layer->removeFromSuperlayer();
205     m_children.append(layer);
206     toPlatformCALayerRemote(layer)->m_superlayer = this;
207     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
208 }
209
210 void PlatformCALayerRemote::insertSublayer(PlatformCALayer* layer, size_t index)
211 {
212     RefPtr<PlatformCALayer> layerProtector(layer);
213
214     layer->removeFromSuperlayer();
215     m_children.insert(index, layer);
216     toPlatformCALayerRemote(layer)->m_superlayer = this;
217     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
218 }
219
220 void PlatformCALayerRemote::replaceSublayer(PlatformCALayer* reference, PlatformCALayer* layer)
221 {
222     ASSERT(reference->superlayer() == this);
223     RefPtr<PlatformCALayer> layerProtector(layer);
224
225     layer->removeFromSuperlayer();
226     size_t referenceIndex = m_children.find(reference);
227     if (referenceIndex != notFound) {
228         m_children[referenceIndex]->removeFromSuperlayer();
229         m_children.remove(referenceIndex);
230         m_children.insert(referenceIndex, layer);
231     }
232
233     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ChildrenChanged);
234 }
235
236 void PlatformCALayerRemote::adoptSublayers(PlatformCALayer* source)
237 {
238     setSublayers(toPlatformCALayerRemote(source)->m_children);
239 }
240
241 void PlatformCALayerRemote::addAnimationForKey(const String& key, PlatformCAAnimation* animation)
242 {
243     ASSERT_NOT_REACHED();
244 }
245
246 void PlatformCALayerRemote::removeAnimationForKey(const String& key)
247 {
248     ASSERT_NOT_REACHED();
249 }
250
251 PassRefPtr<PlatformCAAnimation> PlatformCALayerRemote::animationForKey(const String& key)
252 {
253     ASSERT_NOT_REACHED();
254
255     return nullptr;
256 }
257
258 void PlatformCALayerRemote::setMask(PlatformCALayer* layer)
259 {
260     m_properties.maskLayer = toPlatformCALayerRemote(layer)->layerID();
261     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MaskLayerChanged);
262 }
263
264 bool PlatformCALayerRemote::isOpaque() const
265 {
266     return m_properties.opaque;
267 }
268
269 void PlatformCALayerRemote::setOpaque(bool value)
270 {
271     m_properties.opaque = value;
272     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::OpaqueChanged);
273 }
274
275 FloatRect PlatformCALayerRemote::bounds() const
276 {
277     return FloatRect(FloatPoint(), m_properties.size);
278 }
279
280 void PlatformCALayerRemote::setBounds(const FloatRect& value)
281 {
282     m_properties.size = value.size();
283     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SizeChanged);
284
285     ensureBackingStore();
286 }
287
288 FloatPoint3D PlatformCALayerRemote::position() const
289 {
290     return m_properties.position;
291 }
292
293 void PlatformCALayerRemote::setPosition(const FloatPoint3D& value)
294 {
295     m_properties.position = value;
296     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::PositionChanged);
297 }
298
299 FloatPoint3D PlatformCALayerRemote::anchorPoint() const
300 {
301     return m_properties.anchorPoint;
302 }
303
304 void PlatformCALayerRemote::setAnchorPoint(const FloatPoint3D& value)
305 {
306     m_properties.anchorPoint = value;
307     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::AnchorPointChanged);
308 }
309
310 TransformationMatrix PlatformCALayerRemote::transform() const
311 {
312     return m_properties.transform;
313 }
314
315 void PlatformCALayerRemote::setTransform(const TransformationMatrix& value)
316 {
317     m_properties.transform = value;
318     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TransformChanged);
319 }
320
321 TransformationMatrix PlatformCALayerRemote::sublayerTransform() const
322 {
323     return m_properties.sublayerTransform;
324 }
325
326 void PlatformCALayerRemote::setSublayerTransform(const TransformationMatrix& value)
327 {
328     m_properties.sublayerTransform = value;
329     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SublayerTransformChanged);
330 }
331
332 void PlatformCALayerRemote::setHidden(bool value)
333 {
334     m_properties.hidden = value;
335     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::HiddenChanged);
336 }
337
338 void PlatformCALayerRemote::setGeometryFlipped(bool value)
339 {
340     m_properties.geometryFlipped = value;
341     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::GeometryFlippedChanged);
342 }
343
344 bool PlatformCALayerRemote::isDoubleSided() const
345 {
346     return m_properties.doubleSided;
347 }
348
349 void PlatformCALayerRemote::setDoubleSided(bool value)
350 {
351     m_properties.doubleSided = value;
352     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::DoubleSidedChanged);
353 }
354
355 bool PlatformCALayerRemote::masksToBounds() const
356 {
357     return m_properties.masksToBounds;
358 }
359
360 void PlatformCALayerRemote::setMasksToBounds(bool value)
361 {
362     m_properties.masksToBounds = value;
363     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MasksToBoundsChanged);
364 }
365
366 bool PlatformCALayerRemote::acceleratesDrawing() const
367 {
368     return m_acceleratesDrawing;
369 }
370
371 void PlatformCALayerRemote::setAcceleratesDrawing(bool acceleratesDrawing)
372 {
373     m_acceleratesDrawing = acceleratesDrawing;
374     ensureBackingStore();
375 }
376
377 CFTypeRef PlatformCALayerRemote::contents() const
378 {
379     return nullptr;
380 }
381
382 void PlatformCALayerRemote::setContents(CFTypeRef value)
383 {
384 }
385
386 void PlatformCALayerRemote::setContentsRect(const FloatRect& value)
387 {
388     m_properties.contentsRect = value;
389     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsRectChanged);
390 }
391
392 void PlatformCALayerRemote::setMinificationFilter(FilterType value)
393 {
394     m_properties.minificationFilter = value;
395     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MinificationFilterChanged);
396 }
397
398 void PlatformCALayerRemote::setMagnificationFilter(FilterType value)
399 {
400     m_properties.magnificationFilter = value;
401     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::MagnificationFilterChanged);
402 }
403
404 Color PlatformCALayerRemote::backgroundColor() const
405 {
406     return m_properties.backgroundColor;
407 }
408
409 void PlatformCALayerRemote::setBackgroundColor(const Color& value)
410 {
411     m_properties.backgroundColor = value;
412     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BackgroundColorChanged);
413 }
414
415 void PlatformCALayerRemote::setBorderWidth(float value)
416 {
417     m_properties.borderWidth = value;
418     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderWidthChanged);
419 }
420
421 void PlatformCALayerRemote::setBorderColor(const Color& value)
422 {
423     m_properties.borderColor = value;
424     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::BorderColorChanged);
425 }
426
427 float PlatformCALayerRemote::opacity() const
428 {
429     return m_properties.opacity;
430 }
431
432 void PlatformCALayerRemote::setOpacity(float value)
433 {
434     m_properties.opacity = value;
435     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::OpacityChanged);
436 }
437
438 #if ENABLE(CSS_FILTERS)
439 void PlatformCALayerRemote::setFilters(const FilterOperations& filters)
440 {
441     m_properties.filters = filters;
442     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::FiltersChanged);
443 }
444
445 void PlatformCALayerRemote::copyFiltersFrom(const PlatformCALayer* sourceLayer)
446 {
447     ASSERT_NOT_REACHED();
448 }
449
450 bool PlatformCALayerRemote::filtersCanBeComposited(const FilterOperations& filters)
451 {
452     return PlatformCALayerMac::filtersCanBeComposited(filters);
453 }
454 #endif
455
456 void PlatformCALayerRemote::setName(const String& value)
457 {
458     m_properties.name = value;
459     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::NameChanged);
460 }
461
462 void PlatformCALayerRemote::setSpeed(float value)
463 {
464     m_properties.speed = value;
465     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::SpeedChanged);
466 }
467
468 void PlatformCALayerRemote::setTimeOffset(CFTimeInterval value)
469 {
470     m_properties.timeOffset = value;
471     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::TimeOffsetChanged);
472 }
473
474 float PlatformCALayerRemote::contentsScale() const
475 {
476     return m_properties.contentsScale;
477 }
478
479 void PlatformCALayerRemote::setContentsScale(float value)
480 {
481     m_properties.contentsScale = value;
482     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::ContentsScaleChanged);
483
484     ensureBackingStore();
485 }
486
487 void PlatformCALayerRemote::setEdgeAntialiasingMask(unsigned value)
488 {
489     m_properties.edgeAntialiasingMask = value;
490     m_properties.notePropertiesChanged(RemoteLayerTreeTransaction::EdgeAntialiasingMaskChanged);
491 }
492
493 PassRefPtr<PlatformCALayer> PlatformCALayerRemote::createCompatibleLayer(PlatformCALayer::LayerType layerType, PlatformCALayerClient* client) const
494 {
495     return PlatformCALayerRemote::create(layerType, client, m_context);
496 }
497
498 uint32_t PlatformCALayerRemote::hostingContextID()
499 {
500     ASSERT_NOT_REACHED();
501     return 0;
502 }
503
504 #endif // USE(ACCELERATED_COMPOSITING)