1/*
2 * Copyright (C) 2010 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#import "config.h"
27#import "ChunkedUpdateDrawingAreaProxy.h"
28
29#import "DrawingAreaMessageKinds.h"
30#import "DrawingAreaProxyMessageKinds.h"
31#import "UpdateChunk.h"
32#import "WKAPICast.h"
33#import "WKView.h"
34#import "WebPageProxy.h"
35
36using namespace WebCore;
37
38namespace WebKit {
39
40WebPageProxy* ChunkedUpdateDrawingAreaProxy::page()
41{
42    return toImpl([m_webView pageRef]);
43}
44
45void ChunkedUpdateDrawingAreaProxy::ensureBackingStore()
46{
47    if (m_bitmapContext)
48        return;
49
50    RetainPtr<CGColorSpaceRef> colorSpace(AdoptCF, CGColorSpaceCreateDeviceRGB());
51    const IntSize& viewSize = size();
52    m_bitmapContext.adoptCF(CGBitmapContextCreate(0, viewSize.width(), viewSize.height(), 8, viewSize.width() * 4, colorSpace.get(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
53
54    // Flip the bitmap context coordinate system.
55    CGContextTranslateCTM(m_bitmapContext.get(), 0, viewSize.height());
56    CGContextScaleCTM(m_bitmapContext.get(), 1, -1);
57}
58
59void ChunkedUpdateDrawingAreaProxy::invalidateBackingStore()
60{
61    m_bitmapContext = 0;
62}
63
64bool ChunkedUpdateDrawingAreaProxy::platformPaint(const IntRect& rect, CGContextRef context)
65{
66    if (!m_bitmapContext)
67        return false;
68
69    CGContextSaveGState(context);
70
71    // Use the copy blend mode when drawing a background.
72    if (page()->drawsBackground())
73        CGContextSetBlendMode(context, kCGBlendModeCopy);
74
75    // Flip the destination.
76    CGContextScaleCTM(context, 1, -1);
77    CGContextTranslateCTM(context, 0, -m_size.height());
78
79    RetainPtr<CGImageRef> image(AdoptCF, CGBitmapContextCreateImage(m_bitmapContext.get()));
80    CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image.get()), CGImageGetHeight(image.get())), image.get());
81
82    CGContextRestoreGState(context);
83    return true;
84}
85
86void ChunkedUpdateDrawingAreaProxy::drawUpdateChunkIntoBackingStore(UpdateChunk* updateChunk)
87{
88    ensureBackingStore();
89
90    RetainPtr<CGImageRef> image(updateChunk->createImage());
91    IntRect updateChunkRect = updateChunk->rect();
92
93    CGContextSaveGState(m_bitmapContext.get());
94
95    // Use the copy blend mode to replace existing content.
96    CGContextSetBlendMode(m_bitmapContext.get(), kCGBlendModeCopy);
97
98    // Flip the destination.
99    CGContextScaleCTM(m_bitmapContext.get(), 1, -1);
100    CGContextTranslateCTM(m_bitmapContext.get(), 0, -(updateChunkRect.y() + updateChunkRect.maxY()));
101
102    CGContextDrawImage(m_bitmapContext.get(), updateChunkRect, image.get());
103
104    CGContextRestoreGState(m_bitmapContext.get());
105
106    [m_webView setNeedsDisplayInRect:NSRectFromCGRect(updateChunkRect)];
107}
108
109} // namespace WebKit
110