1/*
2 * Copyright (C) 2009 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#include "WebLayer.h"
31
32#include "Font.h"
33#include "GraphicsLayer.h"
34
35namespace WebCore {
36
37using namespace std;
38
39void WebLayer::internalSetNeedsDisplay(const CGRect* dirtyRect)
40{
41    if (m_owner) {
42        if (m_owner->showRepaintCounter()) {
43            CGRect layerBounds = bounds();
44            CGRect repaintCounterRect = layerBounds;
45            // We assume a maximum of 4 digits and a font size of 18.
46            repaintCounterRect.size.width = 80;
47            repaintCounterRect.size.height = 22;
48            if (m_owner->contentsOrientation() == WebCore::GraphicsLayer::CompositingCoordinatesTopDown)
49                repaintCounterRect.origin.y = layerBounds.size.height - (layerBounds.origin.y + repaintCounterRect.size.height);
50            WKCACFLayer::internalSetNeedsDisplay(&repaintCounterRect);
51        }
52        if (dirtyRect && m_owner->contentsOrientation() == WebCore::GraphicsLayer::CompositingCoordinatesTopDown) {
53            CGRect flippedDirtyRect = *dirtyRect;
54            flippedDirtyRect.origin.y = bounds().size.height - (flippedDirtyRect.origin.y + flippedDirtyRect.size.height);
55            WKCACFLayer::internalSetNeedsDisplay(&flippedDirtyRect);
56            return;
57        }
58    }
59
60    WKCACFLayer::internalSetNeedsDisplay(dirtyRect);
61}
62
63void WebLayer::drawInContext(PlatformGraphicsContext* context)
64{
65    if (!m_owner)
66        return;
67
68    CGContextSaveGState(context);
69
70    CGRect layerBounds = bounds();
71    if (m_owner->contentsOrientation() == WebCore::GraphicsLayer::CompositingCoordinatesTopDown) {
72        CGContextScaleCTM(context, 1, -1);
73        CGContextTranslateCTM(context, 0, -layerBounds.size.height);
74    }
75
76    if (m_owner->client()) {
77        GraphicsContext graphicsContext(context);
78
79        // It's important to get the clip from the context, because it may be significantly
80        // smaller than the layer bounds (e.g. tiled layers)
81        CGRect clipBounds = CGContextGetClipBoundingBox(context);
82        IntRect clip(enclosingIntRect(clipBounds));
83        m_owner->paintGraphicsLayerContents(graphicsContext, clip);
84    }
85#ifndef NDEBUG
86    else {
87        ASSERT_NOT_REACHED();
88
89        // FIXME: ideally we'd avoid calling -setNeedsDisplay on a layer that is a plain color,
90        // so CA never makes backing store for it (which is what -setNeedsDisplay will do above).
91        CGContextSetRGBFillColor(context, 0.0f, 1.0f, 0.0f, 1.0f);
92        CGContextFillRect(context, layerBounds);
93    }
94#endif
95
96    if (m_owner->showRepaintCounter()) {
97        String text = String::number(m_owner->incrementRepaintCount());
98
99        CGContextSaveGState(context);
100
101        // Make the background of the counter the same as the border color,
102        // unless there is no border, then make it red
103        float borderWidth = CACFLayerGetBorderWidth(layer());
104        if (borderWidth > 0) {
105            CGColorRef borderColor = CACFLayerGetBorderColor(layer());
106            const CGFloat* colors = CGColorGetComponents(borderColor);
107            CGContextSetRGBFillColor(context, colors[0], colors[1], colors[2], colors[3]);
108        } else
109            CGContextSetRGBFillColor(context, 1.0f, 0.0f, 0.0f, 0.8f);
110
111        CGRect aBounds = layerBounds;
112
113        aBounds.size.width = 10 + 10 * text.length();
114        aBounds.size.height = 22;
115        CGContextFillRect(context, aBounds);
116
117        FontDescription desc;
118
119        NONCLIENTMETRICS metrics;
120        metrics.cbSize = sizeof(metrics);
121        SystemParametersInfo(SPI_GETNONCLIENTMETRICS, metrics.cbSize, &metrics, 0);
122        FontFamily family;
123        family.setFamily(metrics.lfSmCaptionFont.lfFaceName);
124        desc.setFamily(family);
125
126        desc.setComputedSize(18);
127
128        Font font = Font(desc, 0, 0);
129        font.update(0);
130
131        GraphicsContext cg(context);
132        cg.setFillColor(Color::black, ColorSpaceDeviceRGB);
133        cg.drawText(font, TextRun(text), IntPoint(aBounds.origin.x + 5, aBounds.origin.y + 17));
134
135        CGContextRestoreGState(context);
136    }
137
138    CGContextRestoreGState(context);
139}
140
141}
142
143#endif // USE(ACCELERATED_COMPOSITING)
144