Removing using namespace WebCore from WebLayer
[WebKit-https.git] / Source / WebCore / platform / graphics / mac / WebLayer.mm
1 /*
2  * Copyright (C) 2009, 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 #import "WebLayer.h"
28
29 #import "GraphicsContext.h"
30 #import "GraphicsLayerCA.h"
31 #import "PlatformCALayer.h"
32 #import <QuartzCore/QuartzCore.h>
33 #import <pal/spi/cocoa/QuartzCoreSPI.h>
34 #import <wtf/SetForScope.h>
35
36 #if PLATFORM(IOS_FAMILY)
37 #import "WKGraphics.h"
38 #import "WAKWindow.h"
39 #import "WebCoreThread.h"
40 #endif
41
42 #if PLATFORM(IOS_FAMILY)
43 @interface WebLayer(Private)
44 - (void)drawScaledContentsInContext:(CGContextRef)context;
45 @end
46 #endif
47
48 @implementation WebLayer
49
50 - (void)drawInContext:(CGContextRef)context
51 {
52     WebCore::PlatformCALayer* layer = WebCore::PlatformCALayer::platformCALayer((__bridge void*)self);
53     if (layer) {
54         WebCore::PlatformCALayer::RepaintRectList rectsToPaint = WebCore::PlatformCALayer::collectRectsToPaint(context, layer);
55         WebCore::PlatformCALayer::drawLayerContents(context, layer, rectsToPaint, self.isRenderingInContext ? WebCore::GraphicsLayerPaintSnapshotting : WebCore::GraphicsLayerPaintNormal);
56     }
57 }
58
59 @end // implementation WebLayer
60
61 @implementation WebSimpleLayer
62
63 @synthesize isRenderingInContext = _isRenderingInContext;
64
65 - (void)renderInContext:(CGContextRef)context
66 {
67     SetForScope<BOOL> change(_isRenderingInContext, YES);
68     [super renderInContext:context];
69 }
70
71 - (id<CAAction>)actionForKey:(NSString *)key
72 {
73     // Fix for <rdar://problem/9015675>: Force the layer content to be updated when the tree is reparented.
74     if ([key isEqualToString:@"onOrderIn"])
75         [self reloadValueForKeyPath:@"contents"];
76
77     return nil;
78 }
79
80 - (void)setNeedsDisplay
81 {
82     WebCore::PlatformCALayer* layer = WebCore::PlatformCALayer::platformCALayer((__bridge void*)self);
83     if (layer && layer->owner() && layer->owner()->platformCALayerDrawsContent())
84         [super setNeedsDisplay];
85 }
86
87 - (void)setNeedsDisplayInRect:(CGRect)dirtyRect
88 {
89     WebCore::PlatformCALayer* platformLayer = WebCore::PlatformCALayer::platformCALayer((__bridge void*)self);
90     if (!platformLayer) {
91         [super setNeedsDisplayInRect:dirtyRect];
92         return;
93     }
94
95     if (WebCore::PlatformCALayerClient* layerOwner = platformLayer->owner()) {
96         if (layerOwner->platformCALayerDrawsContent()) {
97             [super setNeedsDisplayInRect:dirtyRect];
98
99             if (layerOwner->platformCALayerShowRepaintCounter(platformLayer)) {
100                 CGRect bounds = [self bounds];
101                 CGRect indicatorRect = CGRectMake(bounds.origin.x, bounds.origin.y, 52, 27);
102                 [super setNeedsDisplayInRect:indicatorRect];
103             }
104         }
105     }
106 }
107
108 - (void)display
109 {
110 #if PLATFORM(IOS_FAMILY)
111     if (pthread_main_np())
112         WebThreadLock();
113 #endif
114     ASSERT(isMainThread());
115     [super display];
116     WebCore::PlatformCALayer* layer = WebCore::PlatformCALayer::platformCALayer((__bridge void*)self);
117     if (layer && layer->owner())
118         layer->owner()->platformCALayerLayerDidDisplay(layer);
119 }
120
121 - (void)drawInContext:(CGContextRef)context
122 {
123 #if PLATFORM(IOS_FAMILY)
124     if (pthread_main_np())
125         WebThreadLock();
126 #endif
127     ASSERT(isMainThread());
128     WebCore::PlatformCALayer* layer = WebCore::PlatformCALayer::platformCALayer((__bridge void*)self);
129     if (layer && layer->owner()) {
130         WebCore::GraphicsContext graphicsContext(context);
131         graphicsContext.setIsCALayerContext(true);
132         graphicsContext.setIsAcceleratedContext(layer->acceleratesDrawing());
133
134         WebCore::FloatRect clipBounds = CGContextGetClipBoundingBox(context);
135         layer->owner()->platformCALayerPaintContents(layer, graphicsContext, clipBounds, self.isRenderingInContext ? WebCore::GraphicsLayerPaintSnapshotting : WebCore::GraphicsLayerPaintNormal);
136     }
137 }
138
139 @end // implementation WebSimpleLayer
140
141 #ifndef NDEBUG
142
143 @implementation CALayer(ExtendedDescription)
144
145 - (NSString*)_descriptionWithPrefix:(NSString*)inPrefix
146 {
147     CGRect aBounds = [self bounds];
148     CGPoint aPos = [self position];
149
150     NSString* selfString = [NSString stringWithFormat:@"%@<%@ 0x%p> \"%@\" bounds(%.1f, %.1f, %.1f, %.1f) pos(%.1f, %.1f), sublayers=%lu masking=%d",
151             inPrefix,
152             [self class],
153             self,
154             [self name],
155             aBounds.origin.x, aBounds.origin.y, aBounds.size.width, aBounds.size.height, 
156             aPos.x, aPos.y,
157             static_cast<unsigned long>([[self sublayers] count]),
158             [self masksToBounds]];
159     
160     NSMutableString* curDesc = [NSMutableString stringWithString:selfString];
161     
162     if ([[self sublayers] count] > 0)
163         [curDesc appendString:@"\n"];
164
165     NSString* sublayerPrefix = [inPrefix stringByAppendingString:@"\t"];
166
167     NSEnumerator* sublayersEnum = [[self sublayers] objectEnumerator];
168     CALayer* curLayer;
169     while ((curLayer = [sublayersEnum nextObject]))
170         [curDesc appendString:[curLayer _descriptionWithPrefix:sublayerPrefix]];
171
172     if ([[self sublayers] count] == 0)
173         [curDesc appendString:@"\n"];
174
175     if (CALayer *mask = [self mask]) {
176         [curDesc appendString:@"mask: "];
177         [curDesc appendString:[mask _descriptionWithPrefix:sublayerPrefix]];
178     }
179
180     return curDesc;
181 }
182
183 - (NSString*)extendedDescription
184 {
185     return [self _descriptionWithPrefix:@""];
186 }
187
188 @end  // implementation WebLayer(ExtendedDescription)
189
190 #endif // NDEBUG