Use "= default" to denote default constructor or destructor
[WebKit-https.git] / Source / WebCore / platform / graphics / ca / win / WebTiledBackingLayerWin.cpp
1 /*
2  * Copyright (C) 2015 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 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 #include "WebTiledBackingLayerWin.h"
28
29 #if USE(CA)
30
31 #include "GraphicsContext.h"
32 #include "PlatformCALayer.h"
33 #include "TileController.h"
34 #include "TiledBacking.h"
35 #include <QuartzCore/CACFLayer.h>
36 #include <wtf/MainThread.h>
37
38 using namespace WebCore;
39
40 WebTiledBackingLayerWin::WebTiledBackingLayerWin(PlatformCALayer* owner)
41     : PlatformCALayerWinInternal(owner)
42 {
43 }
44
45 WebTiledBackingLayerWin::~WebTiledBackingLayerWin() = default;
46
47 struct DisplayOnMainThreadContext {
48     RetainPtr<CACFLayerRef> layer;
49     RetainPtr<CGContextRef> context;
50
51     DisplayOnMainThreadContext(CACFLayerRef caLayer, CGContextRef caContext)
52         : layer(caLayer)
53         , context(caContext)
54     {
55     }
56 };
57
58 static void redispatchOnMainQueue(void* context)
59 {
60     ASSERT(isMainThread());
61     std::unique_ptr<DisplayOnMainThreadContext> retainedContext(reinterpret_cast<DisplayOnMainThreadContext*>(context));
62     if (!retainedContext)
63         return;
64
65     PlatformCALayerWinInternal* self = static_cast<PlatformCALayerWinInternal*>(CACFLayerGetUserData(retainedContext->layer.get()));
66
67     self->displayCallback(retainedContext->layer.get(), retainedContext->context.get());
68 }
69
70 void WebTiledBackingLayerWin::displayCallback(CACFLayerRef caLayer, CGContextRef context)
71 {
72     if (!isMainThread()) {
73         dispatch_async_f(dispatch_get_main_queue(), new DisplayOnMainThreadContext(caLayer, context), redispatchOnMainQueue);
74         return;
75     }
76     
77     if (!owner() || !owner()->owner())
78         return;
79
80     CGContextSaveGState(context);
81
82     CGRect layerBounds = owner()->bounds();
83     PlatformCALayer::LayerType layerType = owner()->layerType();
84     ASSERT(layerType == PlatformCALayer::LayerTypeTiledBackingLayer || layerType == PlatformCALayer::LayerTypePageTiledBackingLayer);
85
86     PlatformCALayerClient* client = owner()->owner();
87     GraphicsLayer::CompositingCoordinatesOrientation orientation = client->platformCALayerContentsOrientation();
88
89     GraphicsContext graphicsContext(context);
90
91     // It's important to get the clip from the context, because it may be significantly
92     // smaller than the layer bounds (e.g. tiled layers)
93     CGRect clipBounds = CGContextGetClipBoundingBox(context);
94     IntRect clip(enclosingIntRect(clipBounds));
95     client->platformCALayerPaintContents(owner(), graphicsContext, clip, GraphicsLayerPaintNormal);
96
97     if (client->platformCALayerShowRepaintCounter(owner())) {
98         int drawCount = client->platformCALayerIncrementRepaintCount(owner());
99         drawRepaintCounters(caLayer, context, layerBounds, drawCount);
100     }
101
102     CGContextRestoreGState(context);
103
104     client->platformCALayerLayerDidDisplay(owner());
105 }
106
107 void WebTiledBackingLayerWin::setNeedsDisplay()
108 {
109     if (m_tileController)
110         return m_tileController->setNeedsDisplay();
111 }
112
113 void WebTiledBackingLayerWin::setNeedsDisplayInRect(const FloatRect& dirtyRect)
114 {
115     if (m_tileController)
116         return m_tileController->setNeedsDisplayInRect(enclosingIntRect(dirtyRect));
117 }
118
119 void WebTiledBackingLayerWin::setBounds(const FloatRect& bounds)
120 {
121     if (CGRectEqualToRect(bounds, owner()->bounds()))
122         return;
123
124     PlatformCALayerWinInternal::setBounds(bounds);
125
126     if (m_tileController)
127         m_tileController->tileCacheLayerBoundsChanged();
128 }
129
130 bool WebTiledBackingLayerWin::isOpaque() const
131 {
132     return m_tileController ? m_tileController->tilesAreOpaque() : true;
133 }
134
135 void WebTiledBackingLayerWin::setOpaque(bool value)
136 {
137     if (m_tileController)
138         return m_tileController->setTilesOpaque(value);
139 }
140
141 float WebTiledBackingLayerWin::contentsScale() const
142 {
143     return m_tileController ? m_tileController->contentsScale() : 1.0f;
144 }
145
146 void WebTiledBackingLayerWin::setContentsScale(float scaleFactor)
147 {
148     if (m_tileController)
149         return m_tileController->setContentsScale(scaleFactor);
150 }
151
152 void WebTiledBackingLayerWin::setBorderWidth(float value)
153 {
154     if (m_tileController)
155         return m_tileController->setTileDebugBorderWidth(value / 2);
156 }
157
158 void WebTiledBackingLayerWin::setBorderColor(const Color& value)
159 {
160     if (m_tileController)
161         return m_tileController->setTileDebugBorderColor(value);
162 }
163
164 TileController* WebTiledBackingLayerWin::createTileController(PlatformCALayer* rootLayer)
165 {
166     ASSERT(!m_tileController);
167     m_tileController = std::make_unique<TileController>(rootLayer);
168     return m_tileController.get();
169 }
170
171 TiledBacking* WebTiledBackingLayerWin::tiledBacking()
172 {
173     return m_tileController.get();
174 }
175
176 void WebTiledBackingLayerWin::invalidate()
177 {
178     ASSERT(isMainThread());
179     ASSERT(m_tileController);
180     m_tileController = nullptr;
181 }
182
183 #endif