• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "TransformationMatrix.h"
30 #include "Path.h"
31 
32 #include <cairo-win32.h>
33 #include "GraphicsContextPlatformPrivateCairo.h"
34 
35 using namespace std;
36 
37 namespace WebCore {
38 
createCairoContextWithHDC(HDC hdc,bool hasAlpha)39 static 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     HBITMAP bitmap = static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP));
45 
46     BITMAP info;
47     GetObject(bitmap, sizeof(info), &info);
48     ASSERT(info.bmBitsPixel == 32);
49 
50     cairo_surface_t* image = cairo_image_surface_create_for_data((unsigned char*)info.bmBits,
51                                                CAIRO_FORMAT_ARGB32,
52                                                info.bmWidth,
53                                                info.bmHeight,
54                                                info.bmWidthBytes);
55 
56     cairo_t* context = cairo_create(image);
57     cairo_surface_destroy(image);
58 
59     return context;
60 }
61 
GraphicsContext(HDC dc,bool hasAlpha)62 GraphicsContext::GraphicsContext(HDC dc, bool hasAlpha)
63     : m_common(createGraphicsContextPrivate())
64     , m_data(new GraphicsContextPlatformPrivate)
65 {
66     if (dc) {
67         m_data->cr = createCairoContextWithHDC(dc, hasAlpha);
68         m_data->m_hdc = dc;
69     } else {
70         setPaintingDisabled(true);
71         m_data->cr = 0;
72         m_data->m_hdc = 0;
73     }
74 
75     if (m_data->cr) {
76         // Make sure the context starts in sync with our state.
77         setPlatformFillColor(fillColor());
78         setPlatformStrokeColor(strokeColor());
79     }
80 }
81 
releaseWindowsContext(HDC hdc,const IntRect & dstRect,bool supportAlphaBlend,bool mayCreateBitmap)82 void GraphicsContext::releaseWindowsContext(HDC hdc, const IntRect& dstRect, bool supportAlphaBlend, bool mayCreateBitmap)
83 {
84     if (!mayCreateBitmap || !hdc || !inTransparencyLayer()) {
85         m_data->restore();
86         return;
87     }
88 
89     if (dstRect.isEmpty())
90         return;
91 
92     HBITMAP bitmap = static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP));
93 
94     BITMAP info;
95     GetObject(bitmap, sizeof(info), &info);
96     ASSERT(info.bmBitsPixel == 32);
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* image = cairo_image_surface_create_for_data((unsigned char*)info.bmBits,
101                                             CAIRO_FORMAT_ARGB32,
102                                             info.bmWidth,
103                                             info.bmHeight,
104                                             info.bmWidthBytes);
105 
106     // Scale the target surface to the new image size, and flip it
107     // so that when we set the srcImage as the surface it will draw
108     // right-side-up.
109     cairo_translate(m_data->cr, 0, dstRect.height());
110     cairo_scale(m_data->cr, dstRect.width(), -dstRect.height());
111     cairo_set_source_surface (m_data->cr, image, dstRect.x(), dstRect.y());
112 
113     if (m_data->layers.size())
114         cairo_paint_with_alpha(m_data->cr, m_data->layers.last());
115     else
116         cairo_paint(m_data->cr);
117 
118     // Delete all our junk.
119     cairo_surface_destroy(image);
120     ::DeleteDC(hdc);
121     ::DeleteObject(bitmap);
122 }
123 
syncContext(PlatformGraphicsContext * cr)124 void GraphicsContextPlatformPrivate::syncContext(PlatformGraphicsContext* cr)
125 {
126     if (!cr)
127        return;
128 
129     cairo_surface_t* surface = cairo_get_target(cr);
130     m_hdc = cairo_win32_surface_get_dc(surface);
131 
132     SetGraphicsMode(m_hdc, GM_ADVANCED); // We need this call for themes to honor world transforms.
133 }
134 
flush()135 void GraphicsContextPlatformPrivate::flush()
136 {
137     cairo_surface_t* surface = cairo_win32_surface_create(m_hdc);
138     cairo_surface_flush(surface);
139     cairo_surface_destroy(surface);
140 }
141 
142 }
143