1/*
2 * Copyright (C) 2008 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 COMPUTER, 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#include "GraphicsContext.h"
28
29#include "AffineTransform.h"
30#include "Path.h"
31
32#include <cairo-win32.h>
33#include "GraphicsContextPlatformPrivateCairo.h"
34
35using namespace std;
36
37namespace WebCore {
38
39static cairo_t* createCairoContextWithHDC(HDC hdc, bool hasAlpha)
40{
41    // Put the HDC In advanced mode so it will honor affine transforms.
42    SetGraphicsMode(hdc, GM_ADVANCED);
43
44    cairo_surface_t* surface = 0;
45
46    HBITMAP bitmap = static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP));
47
48    BITMAP info;
49    if (!GetObject(bitmap, sizeof(info), &info))
50        surface = cairo_win32_surface_create(hdc);
51    else {
52        ASSERT(info.bmBitsPixel == 32);
53
54        surface = cairo_image_surface_create_for_data((unsigned char*)info.bmBits,
55                                               CAIRO_FORMAT_ARGB32,
56                                               info.bmWidth,
57                                               info.bmHeight,
58                                               info.bmWidthBytes);
59    }
60
61    cairo_t* context = cairo_create(surface);
62    cairo_surface_destroy(surface);
63
64    return context;
65}
66
67GraphicsContext::GraphicsContext(HDC dc, bool hasAlpha)
68    : m_updatingControlTints(false)
69{
70    platformInit(dc, hasAlpha);
71}
72
73void GraphicsContext::platformInit(HDC dc, bool hasAlpha)
74{
75    cairo_t* cr = 0;
76    if (dc)
77        cr = createCairoContextWithHDC(dc, hasAlpha);
78    else
79        setPaintingDisabled(true);
80
81    m_data = new GraphicsContextPlatformPrivateToplevel(new PlatformContextCairo(cr));
82    m_data->m_hdc = dc;
83    if (platformContext()->cr()) {
84        // Make sure the context starts in sync with our state.
85        setPlatformFillColor(fillColor(), fillColorSpace());
86        setPlatformStrokeColor(strokeColor(), strokeColorSpace());
87    }
88}
89
90static void setRGBABitmapAlpha(unsigned char* bytes, size_t length, unsigned char level)
91{
92    for (size_t i = 0; i < length; i += 4)
93        bytes[i + 3] = level;
94}
95
96static void drawBitmapToContext(GraphicsContextPlatformPrivate* context, cairo_t* cr, const DIBPixelData& pixelData, const IntSize& translate)
97{
98    // Need to make a cairo_surface_t out of the bitmap's pixel buffer and then draw
99    // it into our context.
100    cairo_surface_t* surface = cairo_image_surface_create_for_data(pixelData.buffer(),
101                                                                   CAIRO_FORMAT_ARGB32,
102                                                                   pixelData.size().width(),
103                                                                   pixelData.size().height(),
104                                                                   pixelData.bytesPerRow());
105
106    // Flip the target surface so that when we set the srcImage as
107    // the surface it will draw right-side-up.
108    cairo_save(cr);
109    cairo_translate(cr, static_cast<double>(translate.width()), static_cast<double>(translate.height()));
110    cairo_scale(cr, 1, -1);
111    cairo_set_source_surface(cr, surface, 0, 0);
112
113    if (context->layers.size())
114        cairo_paint_with_alpha(cr, context->layers.last());
115    else
116        cairo_paint(cr);
117
118    // Delete all our junk.
119    cairo_surface_destroy(surface);
120    cairo_restore(cr);
121}
122
123void GraphicsContext::releaseWindowsContext(HDC hdc, const IntRect& dstRect, bool supportAlphaBlend, bool mayCreateBitmap)
124{
125    bool createdBitmap = mayCreateBitmap && (!m_data->m_hdc || inTransparencyLayer());
126    if (!hdc || !createdBitmap) {
127        m_data->restore();
128        return;
129    }
130
131    if (dstRect.isEmpty())
132        return;
133
134    OwnPtr<HBITMAP> bitmap = adoptPtr(static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP)));
135
136    DIBPixelData pixelData(bitmap.get());
137    ASSERT(pixelData.bitsPerPixel() == 32);
138
139    // If this context does not support alpha blending, then it may have
140    // been drawn with GDI functions which always set the alpha channel
141    // to zero. We need to manually set the bitmap to be fully opaque.
142    unsigned char* bytes = reinterpret_cast<unsigned char*>(pixelData.buffer());
143    if (!supportAlphaBlend)
144        setRGBABitmapAlpha(bytes, pixelData.size().height() * pixelData.bytesPerRow(), 255);
145
146    drawBitmapToContext(m_data, platformContext()->cr(), pixelData, IntSize(dstRect.x(), dstRect.height() + dstRect.y()));
147
148    ::DeleteDC(hdc);
149}
150
151void GraphicsContext::drawWindowsBitmap(WindowsBitmap* bitmap, const IntPoint& point)
152{
153    drawBitmapToContext(m_data, platformContext()->cr(), bitmap->windowsDIB(), IntSize(point.x(), bitmap->size().height() + point.y()));
154}
155
156void GraphicsContextPlatformPrivate::syncContext(cairo_t* cr)
157{
158    if (!cr)
159       return;
160
161    cairo_surface_t* surface = cairo_get_target(cr);
162    m_hdc = cairo_win32_surface_get_dc(surface);
163
164    SetGraphicsMode(m_hdc, GM_ADVANCED); // We need this call for themes to honor world transforms.
165}
166
167void GraphicsContextPlatformPrivate::flush()
168{
169    cairo_surface_t* surface = cairo_win32_surface_create(m_hdc);
170    cairo_surface_flush(surface);
171    cairo_surface_destroy(surface);
172}
173
174}
175