Tizen 2.0 Release
[framework/graphics/cairo.git] / src / win32 / cairo-win32-device.c
1 /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2 /* Cairo - a vector graphics library with display and print output
3  *
4  * Copyright © 2005 Red Hat, Inc.
5  * Copyright © 2012 Intel Corporation
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it either under the terms of the GNU Lesser General Public
9  * License version 2.1 as published by the Free Software Foundation
10  * (the "LGPL") or, at your option, under the terms of the Mozilla
11  * Public License Version 1.1 (the "MPL"). If you do not alter this
12  * notice, a recipient may use your version of this file under either
13  * the MPL or the LGPL.
14  *
15  * You should have received a copy of the LGPL along with this library
16  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
18  * You should have received a copy of the MPL along with this library
19  * in the file COPYING-MPL-1.1
20  *
21  * The contents of this file are subject to the Mozilla Public License
22  * Version 1.1 (the "License"); you may not use this file except in
23  * compliance with the License. You may obtain a copy of the License at
24  * http://www.mozilla.org/MPL/
25  *
26  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28  * the specific language governing rights and limitations.
29  *
30  * The Original Code is the cairo graphics library.
31  *
32  * The Initial Developer of the Original Code is Red Hat, Inc.
33  *
34  * Contributor(s):
35  *      Owen Taylor <otaylor@redhat.com>
36  *      Stuart Parmenter <stuart@mozilla.com>
37  *      Vladimir Vukicevic <vladimir@pobox.com>
38  */
39
40 #define WIN32_LEAN_AND_MEAN
41 /* We require Windows 2000 features such as ETO_PDY */
42 #if !defined(WINVER) || (WINVER < 0x0500)
43 # define WINVER 0x0500
44 #endif
45 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
46 # define _WIN32_WINNT 0x0500
47 #endif
48
49 #include "cairoint.h"
50
51 #include "cairo-atomic-private.h"
52 #include "cairo-device-private.h"
53 #include "cairo-win32-private.h"
54
55 #include <wchar.h>
56 #include <windows.h>
57
58 static cairo_device_t *__cairo_win32_device;
59
60 static cairo_status_t
61 _cairo_win32_device_flush (void *device)
62 {
63     GdiFlush ();
64     return CAIRO_STATUS_SUCCESS;
65 }
66
67 static void
68 _cairo_win32_device_finish (void *device)
69 {
70 }
71
72 static void
73 _cairo_win32_device_destroy (void *device)
74 {
75     free (device);
76 }
77
78 static const cairo_device_backend_t _cairo_win32_device_backend = {
79     CAIRO_DEVICE_TYPE_WIN32,
80
81     NULL, NULL, /* lock, unlock */
82
83     _cairo_win32_device_flush,
84     _cairo_win32_device_finish,
85     _cairo_win32_device_destroy,
86 };
87
88 #if 0
89 D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT,
90                                                                    D2D1::PixelFormat(
91                                                                                      DXGI_FORMAT_B8G8R8A8_UNORM,
92                                                                                      D2D1_ALPHA_MODE_IGNORE),
93                                                                    0,
94                                                                    0,
95                                                                    D2D1_RENDER_TARGET_USAGE_NONE,
96                                                                    D2D1_FEATURE_LEVEL_DEFAULT
97                                                                   );
98
99 hr = m_pD2DFactory->CreateDCRenderTarget(&props, &device->d2d);
100 #endif
101
102 static cairo_bool_t is_win98 (void)
103 {
104     OSVERSIONINFO os;
105
106     os.dwOSVersionInfoSize = sizeof (os);
107     GetVersionEx (&os);
108
109     return (VER_PLATFORM_WIN32_WINDOWS != os.dwPlatformId &&
110             os.dwMajorVersion != 4 &&
111             os.dwMinorVersion != 10);
112 }
113
114 static void *
115 _cairo_win32_device_get_alpha_blend (cairo_win32_device_t *device)
116 {
117     void *func = NULL;
118
119     if (is_win98 ())
120         return NULL;
121
122     device->msimg32_dll = LoadLibraryW (L"msimg32");
123     if (device->msimg32_dll)
124         func = GetProcAddress (device->msimg32_dll, "AlphaBlend");
125
126     return func;
127 }
128
129 cairo_device_t *
130 _cairo_win32_device_get (void)
131 {
132     cairo_win32_device_t *device;
133
134     if (__cairo_win32_device)
135         return cairo_device_reference (__cairo_win32_device);
136
137     device = malloc (sizeof (*device));
138
139     _cairo_device_init (&device->base, &_cairo_win32_device_backend);
140
141     device->compositor = _cairo_win32_gdi_compositor_get ();
142
143     device->msimg32_dll = NULL;
144     device->alpha_blend = _cairo_win32_device_get_alpha_blend (device);
145
146     if (_cairo_atomic_ptr_cmpxchg ((void **)&__cairo_win32_device, NULL, device))
147         return cairo_device_reference(&device->base);
148
149     _cairo_win32_device_destroy (device);
150     return cairo_device_reference (__cairo_win32_device);
151 }
152
153 unsigned
154 _cairo_win32_flags_for_dc (HDC dc)
155 {
156     uint32_t flags = 0;
157     int cap;
158
159     cap = GetDeviceCaps(dc, RASTERCAPS);
160     if (cap & RC_BITBLT)
161         flags |= CAIRO_WIN32_SURFACE_CAN_BITBLT;
162     if (cap & RC_STRETCHBLT)
163         flags |= CAIRO_WIN32_SURFACE_CAN_STRETCHBLT;
164     if (cap & RC_STRETCHDIB)
165         flags |= CAIRO_WIN32_SURFACE_CAN_STRETCHDIB;
166
167     if (GetDeviceCaps(dc, TECHNOLOGY) == DT_RASDISPLAY) {
168         flags |= CAIRO_WIN32_SURFACE_IS_DISPLAY;
169
170         /* These will always be possible, but the actual GetDeviceCaps
171          * calls will return whether they're accelerated or not.
172          * We may want to use our own (pixman) routines sometimes
173          * if they're eventually faster, but for now have GDI do
174          * everything.
175          */
176 #if 0
177         flags |= CAIRO_WIN32_SURFACE_CAN_BITBLT;
178         flags |= CAIRO_WIN32_SURFACE_CAN_ALPHABLEND;
179         flags |= CAIRO_WIN32_SURFACE_CAN_STRETCHBLT;
180         flags |= CAIRO_WIN32_SURFACE_CAN_STRETCHDIB;
181 #endif
182     } else {
183         cap = GetDeviceCaps(dc, SHADEBLENDCAPS);
184         if (cap != SB_NONE)
185             flags |= CAIRO_WIN32_SURFACE_CAN_ALPHABLEND;
186     }
187
188     return flags;
189 }