817cc209f877a4e34f68a75ae06c68550efef4d3
[WebKit-https.git] / Source / WebCore / platform / graphics / ca / PlatformCALayer.cpp
1 /*
2  * Copyright (C) 2013-2014 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 "PlatformCALayer.h"
28
29 #include "LayerPool.h"
30 #include "PlatformCALayerClient.h"
31 #include "TextStream.h"
32 #include <wtf/StringExtras.h>
33
34 #if USE(CA)
35
36 namespace WebCore {
37
38 static GraphicsLayer::PlatformLayerID generateLayerID()
39 {
40     static GraphicsLayer::PlatformLayerID layerID;
41     return ++layerID;
42 }
43
44 #if COMPILER(MSVC)
45 const float PlatformCALayer::webLayerWastedSpaceThreshold = 0.75f;
46 #endif
47
48 PlatformCALayer::PlatformCALayer(LayerType layerType, PlatformCALayerClient* owner)
49     : m_layerType(layerType)
50     , m_layerID(generateLayerID())
51     , m_owner(owner)
52 {
53 }
54
55 PlatformCALayer::~PlatformCALayer()
56 {
57     // Clear the owner, which also clears it in the delegate to prevent attempts
58     // to use the GraphicsLayerCA after it has been destroyed.
59     setOwner(nullptr);
60 }
61
62 void PlatformCALayer::drawRepaintIndicator(CGContextRef context, PlatformCALayer* platformCALayer, int repaintCount, CGColorRef customBackgroundColor)
63 {
64     char text[16]; // that's a lot of repaints
65     snprintf(text, sizeof(text), "%d", repaintCount);
66     
67     CGRect indicatorBox = platformCALayer->bounds();
68
69     CGContextSaveGState(context);
70
71     indicatorBox.size.width = 12 + 10 * strlen(text);
72     indicatorBox.size.height = 27;
73     
74     CGContextSetAlpha(context, 0.5f);
75     CGContextBeginTransparencyLayerWithRect(context, indicatorBox, 0);
76     
77     if (customBackgroundColor)
78         CGContextSetFillColorWithColor(context, customBackgroundColor);
79     else
80         CGContextSetRGBFillColor(context, 0, 0.5f, 0.25f, 1);
81     
82     CGContextFillRect(context, indicatorBox);
83     
84     if (platformCALayer->acceleratesDrawing())
85         CGContextSetRGBFillColor(context, 1, 0, 0, 1);
86     else
87         CGContextSetRGBFillColor(context, 1, 1, 1, 1);
88
89     if (platformCALayer->owner()->isUsingDisplayListDrawing(platformCALayer)) {
90         CGContextSetRGBStrokeColor(context, 0, 0, 0, 0.65);
91         CGContextSetLineWidth(context, 2);
92         CGContextStrokeRect(context, indicatorBox);
93     }
94     
95     platformCALayer->drawTextAtPoint(context, indicatorBox.origin.x + 5, indicatorBox.origin.y + 22, CGSizeMake(1, -1), 22, text, strlen(text));
96     
97     CGContextEndTransparencyLayer(context);
98     CGContextRestoreGState(context);
99 }
100
101 void PlatformCALayer::flipContext(CGContextRef context, CGFloat height)
102 {
103     CGContextScaleCTM(context, 1, -1);
104     CGContextTranslateCTM(context, 0, -height);
105 }
106
107 // This function is needed to work around a bug in Windows CG <rdar://problem/22703470>
108 void PlatformCALayer::drawTextAtPoint(CGContextRef context, CGFloat x, CGFloat y, CGSize scale, CGFloat fontSize, const char* text, size_t length) const
109 {
110 #pragma clang diagnostic push
111 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
112     CGContextSetTextMatrix(context, CGAffineTransformMakeScale(scale.width, scale.height));
113     CGContextSelectFont(context, "Helvetica", fontSize, kCGEncodingMacRoman);
114     CGContextShowTextAtPoint(context, x, y, text, length);
115 #pragma clang diagnostic pop
116 }
117
118 PassRefPtr<PlatformCALayer> PlatformCALayer::createCompatibleLayerOrTakeFromPool(PlatformCALayer::LayerType layerType, PlatformCALayerClient* client, IntSize size)
119 {
120     RefPtr<PlatformCALayer> layer;
121
122     if ((layer = layerPool().takeLayerWithSize(size))) {
123         layer->setOwner(client);
124         return layer.release();
125     }
126
127     layer = createCompatibleLayer(layerType, client);
128     layer->setBounds(FloatRect(FloatPoint(), size));
129     
130     return layer.release();
131 }
132
133 void PlatformCALayer::moveToLayerPool()
134 {
135     ASSERT(!superlayer());
136     layerPool().addLayer(this);
137 }
138
139 LayerPool& PlatformCALayer::layerPool()
140 {
141     static LayerPool* sharedPool = new LayerPool;
142     return *sharedPool;
143 }
144
145 TextStream& operator<<(TextStream& ts, PlatformCALayer::LayerType layerType)
146 {
147     switch (layerType) {
148     case PlatformCALayer::LayerTypeLayer:
149     case PlatformCALayer::LayerTypeWebLayer:
150     case PlatformCALayer::LayerTypeSimpleLayer:
151         ts << "layer";
152         break;
153     case PlatformCALayer::LayerTypeTransformLayer:
154         ts << "transform-layer";
155         break;
156     case PlatformCALayer::LayerTypeWebTiledLayer:
157         ts << "tiled-layer";
158         break;
159     case PlatformCALayer::LayerTypeTiledBackingLayer:
160         ts << "tiled-backing-layer";
161         break;
162     case PlatformCALayer::LayerTypePageTiledBackingLayer:
163         ts << "page-tiled-backing-layer";
164         break;
165     case PlatformCALayer::LayerTypeTiledBackingTileLayer:
166         ts << "tiled-backing-tile";
167         break;
168     case PlatformCALayer::LayerTypeRootLayer:
169         ts << "root-layer";
170         break;
171     case PlatformCALayer::LayerTypeBackdropLayer:
172         ts << "backdrop-layer";
173         break;
174     case PlatformCALayer::LayerTypeAVPlayerLayer:
175         ts << "av-player-layer";
176         break;
177     case PlatformCALayer::LayerTypeWebGLLayer:
178         ts << "web-gl-layer";
179         break;
180     case PlatformCALayer::LayerTypeShapeLayer:
181         ts << "shape-layer";
182         break;
183     case PlatformCALayer::LayerTypeScrollingLayer:
184         ts << "scrolling-layer";
185         break;
186     case PlatformCALayer::LayerTypeCustom:
187         ts << "custom-layer";
188         break;
189     case PlatformCALayer::LayerTypeLightSystemBackdropLayer:
190         ts << "light-system-backdrop-layer";
191         break;
192     case PlatformCALayer::LayerTypeDarkSystemBackdropLayer:
193         ts << "dark-system-backdrop-layer";
194         break;
195     }
196     return ts;
197 }
198
199 TextStream& operator<<(TextStream& ts, PlatformCALayer::FilterType filterType)
200 {
201     switch (filterType) {
202     case PlatformCALayer::Linear:
203         ts << "linear";
204         break;
205     case PlatformCALayer::Nearest:
206         ts << "nearest";
207         break;
208     case PlatformCALayer::Trilinear:
209         ts << "trilinear";
210         break;
211     default:
212         ASSERT_NOT_REACHED();
213         break;
214     }
215     return ts;
216 }
217
218 }
219
220 #endif // USE(CA)