WebLayer.mm 10.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
 * Copyright (C) 2009 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"

#if USE(ACCELERATED_COMPOSITING)

#import "WebLayer.h"

#import "GraphicsContext.h"
33
34
#import "GraphicsLayerCA.h"
#import "PlatformCALayer.h"
35
#import "ThemeMac.h"
36
#import "WebCoreSystemInterface.h"
37
#import <objc/runtime.h>
38
39
#import <QuartzCore/QuartzCore.h>

40
41
42
43
@interface CALayer(WebCoreCALayerPrivate)
- (void)reloadValueForKeyPath:(NSString *)keyPath;
@end

44
45
46
47
using namespace WebCore;

@implementation WebLayer

48
void drawLayerContents(CGContextRef context, CALayer *layer, WebCore::PlatformCALayer* platformLayer)
49
{
50
    WebCore::PlatformCALayerClient* layerContents = platformLayer->owner();
51
52
53
    if (!layerContents)
        return;

54
    CGContextSaveGState(context);
55
56

    CGRect layerBounds = [layer bounds];
57
    if (layerContents->platformCALayerContentsOrientation() == WebCore::GraphicsLayer::CompositingCoordinatesBottomUp) {
58
59
60
61
        CGContextScaleCTM(context, 1, -1);
        CGContextTranslateCTM(context, 0, -layerBounds.size.height);
    }

62
    [NSGraphicsContext saveGraphicsState];
63

64
65
66
67
    // Set up an NSGraphicsContext for the context, so that parts of AppKit that rely on
    // the current NSGraphicsContext (e.g. NSCell drawing) get the right one.
    NSGraphicsContext* layerContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:YES];
    [NSGraphicsContext setCurrentContext:layerContext];
68

69
    GraphicsContext graphicsContext(context);
70
    graphicsContext.setIsCALayerContext(true);
71
    graphicsContext.setIsAcceleratedContext(platformLayer->acceleratesDrawing());
72

73
74
75
    if (!layerContents->platformCALayerContentsOpaque()) {
        // Turn off font smoothing to improve the appearance of text rendered onto a transparent background.
        graphicsContext.setShouldSmoothFonts(false);
76
    }
77
78
79
    
    // It's important to get the clip from the context, because it may be significantly
    // smaller than the layer bounds (e.g. tiled layers)
80
81
    FloatRect clipBounds = CGContextGetClipBoundingBox(context);

82
83
    FloatRect focusRingClipRect = clipBounds;
#if __MAC_OS_X_VERSION_MIN_REQUIRED < 1090
84
85
    // Set the focus ring clip rect which needs to be in base coordinates.
    AffineTransform transform = CGContextGetCTM(context);
86
87
88
    focusRingClipRect = transform.mapRect(clipBounds);
#endif
    ThemeMac::setFocusRingClipRect(focusRingClipRect);
89

90
91
92
93
94
95
96
97
98
99
100
101
102
103
    const float wastedSpaceThreshold = 0.75f;
    const unsigned maxRectsToPaint = 5;

    double clipArea = clipBounds.width() * clipBounds.height();
    __block double totalRectArea = 0;
    __block unsigned rectCount = 0;
    __block Vector<FloatRect, maxRectsToPaint> dirtyRects;
    
    wkCALayerEnumerateRectsBeingDrawnWithBlock(layer, context, ^(CGRect rect) {
        if (++rectCount > maxRectsToPaint)
            return;

        totalRectArea += rect.size.width * rect.size.height;
        dirtyRects.append(rect);
104
    });
105

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    if (rectCount < maxRectsToPaint && totalRectArea < clipArea * wastedSpaceThreshold) {
        for (unsigned i = 0; i < rectCount; ++i) {
            const FloatRect& currentRect = dirtyRects[i];
            
            GraphicsContextStateSaver stateSaver(graphicsContext);
            graphicsContext.clip(currentRect);
            
            layerContents->platformCALayerPaintContents(graphicsContext, enclosingIntRect(currentRect));
        }
    } else {
        // CGContextGetClipBoundingBox() gives us the bounds of the dirty region, so clipBounds
        // encompasses all the dirty rects.
        layerContents->platformCALayerPaintContents(graphicsContext, enclosingIntRect(clipBounds));
    }

121
122
    ThemeMac::setFocusRingClipRect(FloatRect());

123
    [NSGraphicsContext restoreGraphicsState];
124

125
126
127
    // Re-fetch the layer owner, since <rdar://problem/9125151> indicates that it might have been destroyed during painting.
    layerContents = platformLayer->owner();
    ASSERT(layerContents);
128

129
130
    CGContextRestoreGState(context);

131
132
133
134
    // Always update the repain count so that it's accurate even if the count itself is not shown. This will be useful
    // for the Web Inspector feeding this information through the LayerTreeAgent. 
    int repaintCount = layerContents->platformCALayerIncrementRepaintCount();

135
    if (!platformLayer->usesTiledBackingLayer() && layerContents && layerContents->platformCALayerShowRepaintCounter(platformLayer)) {
136
137
138
        bool isTiledLayer = [layer isKindOfClass:[CATiledLayer class]];

        char text[16]; // that's a lot of repaints
139
        snprintf(text, sizeof(text), "%d", repaintCount);
140

141
142
143
        CGRect indicatorBox = layerBounds;
        indicatorBox.size.width = 12 + 10 * strlen(text);
        indicatorBox.size.height = 27;
144
        CGContextSaveGState(context);
145
        
mrowe@apple.com's avatar
mrowe@apple.com committed
146
        CGContextSetAlpha(context, 0.5f);
147
148
        CGContextBeginTransparencyLayerWithRect(context, indicatorBox, 0);

149
        if (isTiledLayer)
150
            CGContextSetRGBFillColor(context, 1, 0.5f, 0, 1);
151
        else
152
            CGContextSetRGBFillColor(context, 0, 0.5f, 0.25f, 1);
153
        
154
        CGContextFillRect(context, indicatorBox);
155
        
156
157
158
159
        if (platformLayer->acceleratesDrawing())
            CGContextSetRGBFillColor(context, 1, 0, 0, 1);
        else
            CGContextSetRGBFillColor(context, 1, 1, 1, 1);
160

mrowe@apple.com's avatar
mrowe@apple.com committed
161
162
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
163
164
165
        CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1, -1));
        CGContextSelectFont(context, "Helvetica", 22, kCGEncodingMacRoman);
        CGContextShowTextAtPoint(context, indicatorBox.origin.x + 5, indicatorBox.origin.y + 22, text, strlen(text));
mrowe@apple.com's avatar
mrowe@apple.com committed
166
167
#pragma clang diagnostic pop

168
        CGContextEndTransparencyLayer(context);
mrowe@apple.com's avatar
mrowe@apple.com committed
169
        CGContextRestoreGState(context);
170
171
172
    }
}

173

174
175
- (id<CAAction>)actionForKey:(NSString *)key
{
176
177
178
179
    // Fix for <rdar://problem/9015675>: Force the layer content to be updated when the tree is reparented.
    if ([key isEqualToString:@"onOrderIn"])
        [self reloadValueForKeyPath:@"contents"];

180
181
182
183
184
    return nil;
}

- (void)setNeedsDisplay
{
185
    PlatformCALayer* layer = PlatformCALayer::platformCALayer(self);
186
    if (layer && layer->owner() && layer->owner()->platformCALayerDrawsContent())
187
188
189
190
191
        [super setNeedsDisplay];
}

- (void)setNeedsDisplayInRect:(CGRect)dirtyRect
{
192
193
194
195
196
197
198
199
200
201
202
203
204
    PlatformCALayer* platformLayer = PlatformCALayer::platformCALayer(self);
    if (!platformLayer) {
        [super setNeedsDisplayInRect:dirtyRect];
        return;
    }

    if (PlatformCALayerClient* layerOwner = platformLayer->owner()) {
        if (layerOwner->platformCALayerDrawsContent()) {
            if (layerOwner->platformCALayerContentsOrientation() == WebCore::GraphicsLayer::CompositingCoordinatesBottomUp)
                dirtyRect.origin.y = [self bounds].size.height - dirtyRect.origin.y - dirtyRect.size.height;

            [super setNeedsDisplayInRect:dirtyRect];

205
            if (layerOwner->platformCALayerShowRepaintCounter(platformLayer)) {
206
207
208
209
210
211
212
213
214
                CGRect bounds = [self bounds];
                CGRect indicatorRect = CGRectMake(bounds.origin.x, bounds.origin.y, 52, 27);
                if (layerOwner->platformCALayerContentsOrientation() == WebCore::GraphicsLayer::CompositingCoordinatesBottomUp)
                    indicatorRect.origin.y = [self bounds].size.height - indicatorRect.origin.y - indicatorRect.size.height;

                [super setNeedsDisplayInRect:indicatorRect];
            }
        }
    }
215
216
}

217
218
219
- (void)display
{
    [super display];
220
221
    PlatformCALayer* layer = PlatformCALayer::platformCALayer(self);
    if (layer && layer->owner())
222
        layer->owner()->platformCALayerLayerDidDisplay(self);
223
224
}

225
226
- (void)drawInContext:(CGContextRef)context
{
227
228
    PlatformCALayer* layer = PlatformCALayer::platformCALayer(self);
    if (layer)
229
        drawLayerContents(context, self, layer);
230
231
232
233
}

@end // implementation WebLayer

234
// MARK: -
235
236
237
238
239
240
241
242
243
244

#ifndef NDEBUG

@implementation CALayer(ExtendedDescription)

- (NSString*)_descriptionWithPrefix:(NSString*)inPrefix
{
    CGRect aBounds = [self bounds];
    CGPoint aPos = [self position];

245
    NSString* selfString = [NSString stringWithFormat:@"%@<%@ 0x%p> \"%@\" bounds(%.1f, %.1f, %.1f, %.1f) pos(%.1f, %.1f), sublayers=%lu masking=%d",
246
247
248
249
250
251
            inPrefix,
            [self class],
            self,
            [self name],
            aBounds.origin.x, aBounds.origin.y, aBounds.size.width, aBounds.size.height, 
            aPos.x, aPos.y,
252
            static_cast<unsigned long>([[self sublayers] count]),
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
            [self masksToBounds]];
    
    NSMutableString* curDesc = [NSMutableString stringWithString:selfString];
    
    if ([[self sublayers] count] > 0)
        [curDesc appendString:@"\n"];

    NSString* sublayerPrefix = [inPrefix stringByAppendingString:@"\t"];

    NSEnumerator* sublayersEnum = [[self sublayers] objectEnumerator];
    CALayer* curLayer;
    while ((curLayer = [sublayersEnum nextObject]))
        [curDesc appendString:[curLayer _descriptionWithPrefix:sublayerPrefix]];

    if ([[self sublayers] count] == 0)
        [curDesc appendString:@"\n"];

    return curDesc;
}

- (NSString*)extendedDescription
{
    return [self _descriptionWithPrefix:@""];
}

@end  // implementation WebLayer(ExtendedDescription)

#endif // NDEBUG

#endif // USE(ACCELERATED_COMPOSITING)