w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

cairo-wgl-context.c
Go to the documentation of this file.
1 /* cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2009 Eric Anholt
4  * Copyright © 2009 Chris Wilson
5  * Copyright © 2005 Red Hat, Inc
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  * Carl Worth <cworth@cworth.org>
36  * Chris Wilson <chris@chris-wilson.co.uk>
37  * Zoxc <zoxc32@gmail.com>
38  */
39 
40 #include "cairoint.h"
41 
42 #include "cairo-gl-private.h"
43 
44 #include "cairo-error-private.h"
45 
46 #define WIN32_LEAN_AND_MEAN
47 #include <windows.h>
48 
49 typedef struct _cairo_wgl_context {
51 
52  HDC dummy_dc;
53  HWND dummy_wnd;
54  HGLRC rc;
55 
56  HDC prev_dc;
57  HGLRC prev_rc;
59 
60 typedef struct _cairo_wgl_surface {
62 
63  HDC dc;
65 
66 static void
67 _wgl_acquire (void *abstract_ctx)
68 {
69  cairo_wgl_context_t *ctx = abstract_ctx;
70 
71  HDC current_dc;
72 
73  ctx->prev_dc = wglGetCurrentDC ();
74  ctx->prev_rc = wglGetCurrentContext ();
75 
76  if (ctx->base.current_target == NULL ||
78  {
79  current_dc = ctx->dummy_dc;
80  }
81  else
82  {
84  current_dc = surface->dc;
85  }
86 
87  if (ctx->prev_dc != current_dc ||
88  (ctx->prev_rc != ctx->rc &&
89  current_dc != ctx->dummy_dc))
90  {
91  wglMakeCurrent (current_dc, ctx->rc);
92  }
93 }
94 
95 static void
96 _wgl_make_current (void *abstract_ctx, cairo_gl_surface_t *abstract_surface)
97 {
98  cairo_wgl_context_t *ctx = abstract_ctx;
99  cairo_wgl_surface_t *surface = (cairo_wgl_surface_t *) abstract_surface;
100 
101  /* Set the window as the target of our context. */
102  wglMakeCurrent (surface->dc, ctx->rc);
103 }
104 
105 static void
106 _wgl_release (void *abstract_ctx)
107 {
108  cairo_wgl_context_t *ctx = abstract_ctx;
109 
110  if (ctx->prev_dc != wglGetCurrentDC () ||
111  ctx->prev_rc != wglGetCurrentContext ())
112  {
113  wglMakeCurrent (ctx->prev_dc,
114  ctx->prev_rc);
115  }
116 }
117 
118 static void
119 _wgl_swap_buffers (void *abstract_ctx,
120  cairo_gl_surface_t *abstract_surface)
121 {
122  cairo_wgl_surface_t *surface = (cairo_wgl_surface_t *) abstract_surface;
123 
124  SwapBuffers (surface->dc);
125 }
126 
127 static void
128 _wgl_destroy (void *abstract_ctx)
129 {
130  cairo_wgl_context_t *ctx = abstract_ctx;
131 
132  if (ctx->dummy_dc != 0) {
133  wglMakeCurrent (ctx->dummy_dc, 0);
134  ReleaseDC (ctx->dummy_wnd, ctx->dummy_dc);
135  DestroyWindow (ctx->dummy_wnd);
136  }
137 }
138 
139 static cairo_status_t
141 {
142  WNDCLASSEXA wincl;
143  PIXELFORMATDESCRIPTOR pfd;
144  int format;
145  HDC dc;
146 
147  ZeroMemory (&wincl, sizeof (WNDCLASSEXA));
148  wincl.cbSize = sizeof (WNDCLASSEXA);
149  wincl.hInstance = GetModuleHandle (0);
150  wincl.lpszClassName = "cairo_wgl_context_dummy";
151  wincl.lpfnWndProc = DefWindowProcA;
152  wincl.style = CS_OWNDC;
153 
154  RegisterClassExA (&wincl);
155 
156  ctx->dummy_wnd = CreateWindowA ("cairo_wgl_context_dummy", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
157  ctx->dummy_dc = GetDC (ctx->dummy_wnd);
158 
159  ZeroMemory (&pfd, sizeof (PIXELFORMATDESCRIPTOR));
160  pfd.nSize = sizeof (PIXELFORMATDESCRIPTOR);
161  pfd.nVersion = 1;
162  pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
163  pfd.iPixelType = PFD_TYPE_RGBA;
164  pfd.cColorBits = 24;
165  pfd.cDepthBits = 16;
166  pfd.iLayerType = PFD_MAIN_PLANE;
167 
168  format = ChoosePixelFormat (ctx->dummy_dc, &pfd);
169  SetPixelFormat (ctx->dummy_dc, format, &pfd);
170 
171  wglMakeCurrent(ctx->dummy_dc, ctx->rc);
172 
173  return CAIRO_STATUS_SUCCESS;
174 }
175 
178 {
179  cairo_wgl_context_t *ctx;
181 
182  ctx = calloc (1, sizeof (cairo_wgl_context_t));
183  if (unlikely (ctx == NULL))
185 
186  ctx->rc = rc;
187  ctx->prev_dc = 0;
188  ctx->prev_rc = 0;
189 
190  status = _wgl_dummy_ctx (ctx);
191  if (unlikely (status)) {
192  free (ctx);
194  }
195 
196  ctx->base.acquire = _wgl_acquire;
197  ctx->base.release = _wgl_release;
200  ctx->base.destroy = _wgl_destroy;
201 
203  (cairo_gl_get_proc_addr_func_t) wglGetProcAddress);
204  if (unlikely (status)) {
205  free (ctx);
207  }
208 
210  if (unlikely (status)) {
211  free (ctx);
213  }
214 
215  ctx->base.release (ctx);
216 
217  return &ctx->base.base;
218 }
219 
220 HGLRC
222 {
223  cairo_wgl_context_t *ctx;
224 
225  if (device->backend->type != CAIRO_DEVICE_TYPE_GL) {
227  return NULL;
228  }
229 
230  ctx = (cairo_wgl_context_t *) device;
231 
232  return ctx->rc;
233 }
234 
237  HDC dc,
238  int width,
239  int height)
240 {
242 
243  if (unlikely (device->status))
244  return _cairo_surface_create_in_error (device->status);
245 
246  if (device->backend->type != CAIRO_DEVICE_TYPE_GL)
248 
249  if (width <= 0 || height <= 0)
251 
252  surface = calloc (1, sizeof (cairo_wgl_surface_t));
253  if (unlikely (surface == NULL))
255 
256  _cairo_gl_surface_init (device, &surface->base,
258  surface->dc = dc;
259 
260  return &surface->base.base;
261 }
#define width(a)
Definition: aptex-macros.h:198
#define height(a)
Definition: aptex-macros.h:200
int rc
Definition: bmpfont.h:9
cairo_status_t _cairo_error(cairo_status_t status)
Definition: cairo-error.c:65
#define _cairo_error_throw(status)
cairo_status_t _cairo_gl_context_init(cairo_gl_context_t *ctx)
cairo_status_t _cairo_gl_dispatch_init(cairo_gl_dispatch_t *dispatch, cairo_gl_get_proc_addr_func_t get_proc_addr)
void _cairo_gl_surface_init(cairo_device_t *device, cairo_gl_surface_t *surface, cairo_content_t content, int width, int height)
static cairo_bool_t _cairo_gl_surface_is_texture(cairo_gl_surface_t *surface)
cairo_gl_generic_func_t(* cairo_gl_get_proc_addr_func_t)(const char *procname)
static cairo_device_t * _cairo_gl_context_create_in_error(cairo_status_t status)
cairo_surface_t * _cairo_surface_create_in_error(cairo_status_t status)
HGLRC cairo_wgl_device_get_context(cairo_device_t *device)
struct _cairo_wgl_surface cairo_wgl_surface_t
cairo_device_t * cairo_wgl_device_create(HGLRC rc)
cairo_surface_t * cairo_gl_surface_create_for_dc(cairo_device_t *device, HDC dc, int width, int height)
struct _cairo_wgl_context cairo_wgl_context_t
@ CAIRO_STATUS_SUCCESS
Definition: cairo.h:315
@ CAIRO_STATUS_INVALID_SIZE
Definition: cairo.h:348
@ CAIRO_STATUS_SURFACE_TYPE_MISMATCH
Definition: cairo.h:329
@ CAIRO_STATUS_DEVICE_TYPE_MISMATCH
Definition: cairo.h:350
@ CAIRO_STATUS_NO_MEMORY
Definition: cairo.h:317
@ CAIRO_CONTENT_COLOR_ALPHA
Definition: cairo.h:382
enum _cairo_status cairo_status_t
@ CAIRO_DEVICE_TYPE_GL
Definition: cairo.h:2165
static void _wgl_acquire(void *abstract_ctx)
static void _wgl_swap_buffers(void *abstract_ctx, cairo_gl_surface_t *abstract_surface)
static void _wgl_release(void *abstract_ctx)
static void _wgl_destroy(void *abstract_ctx)
static cairo_status_t _wgl_dummy_ctx(cairo_wgl_context_t *ctx)
static void _wgl_make_current(void *abstract_ctx, cairo_gl_surface_t *abstract_surface)
#define free(a)
Definition: decNumber.cpp:310
#define unlikely(x)
Definition: jbig2arith.cc:116
#define NULL
Definition: ftobjs.h:61
voidp calloc()
static int format
Definition: pbmclean.c:15
static cairo_surface_t * surface
Definition: pdftocairo.cc:234
#define status
cairo_device_type_t type
cairo_status_t status
const cairo_device_backend_t * backend
void(* make_current)(void *ctx, cairo_gl_surface_t *surface)
void(* acquire)(void *ctx)
void(* destroy)(void *ctx)
cairo_gl_surface_t * current_target
void(* release)(void *ctx)
cairo_device_t base
cairo_gl_dispatch_t dispatch
void(* swap_buffers)(void *ctx, cairo_gl_surface_t *surface)
cairo_gl_context_t base
cairo_gl_surface_t base