native_gdi.c revision 94bf657b2390a1cb72d748047e5c7014e4bc1752
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.9 4 * 5 * Copyright (C) 2010 LunarG Inc. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included 15 * in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 * DEALINGS IN THE SOFTWARE. 24 * 25 * Authors: 26 * Chia-I Wu <olv@lunarg.com> 27 */ 28 29#include <windows.h> 30 31#include "pipe/p_compiler.h" 32#include "util/u_memory.h" 33#include "util/u_format.h" 34#include "util/u_inlines.h" 35#include "gdi/gdi_sw_winsys.h" 36 37#include "common/native_helper.h" 38#include "common/native.h" 39 40struct gdi_display { 41 struct native_display base; 42 43 HDC hDC; 44 struct native_event_handler *event_handler; 45 46 struct native_config *configs; 47 int num_configs; 48}; 49 50struct gdi_surface { 51 struct native_surface base; 52 53 HWND hWnd; 54 enum pipe_format color_format; 55 56 struct gdi_display *gdpy; 57 58 unsigned int server_stamp; 59 unsigned int client_stamp; 60 61 struct resource_surface *rsurf; 62}; 63 64static INLINE struct gdi_display * 65gdi_display(const struct native_display *ndpy) 66{ 67 return (struct gdi_display *) ndpy; 68} 69 70static INLINE struct gdi_surface * 71gdi_surface(const struct native_surface *nsurf) 72{ 73 return (struct gdi_surface *) nsurf; 74} 75 76/** 77 * Update the geometry of the surface. This is a slow functions. 78 */ 79static void 80gdi_surface_update_geometry(struct native_surface *nsurf) 81{ 82 struct gdi_surface *gsurf = gdi_surface(nsurf); 83 RECT rect; 84 uint w, h; 85 86 GetClientRect(gsurf->hWnd, &rect); 87 w = rect.right - rect.left; 88 h = rect.bottom - rect.top; 89 90 if (resource_surface_set_size(gsurf->rsurf, w, h)) 91 gsurf->server_stamp++; 92} 93 94/** 95 * Update the buffers of the surface. 96 */ 97static boolean 98gdi_surface_update_buffers(struct native_surface *nsurf, uint buffer_mask) 99{ 100 struct gdi_surface *gsurf = gdi_surface(nsurf); 101 102 if (gsurf->client_stamp != gsurf->server_stamp) { 103 gdi_surface_update_geometry(&gsurf->base); 104 gsurf->client_stamp = gsurf->server_stamp; 105 } 106 107 return resource_surface_add_resources(gsurf->rsurf, buffer_mask); 108} 109 110/** 111 * Emulate an invalidate event. 112 */ 113static void 114gdi_surface_invalidate(struct native_surface *nsurf) 115{ 116 struct gdi_surface *gsurf = gdi_surface(nsurf); 117 struct gdi_display *gdpy = gsurf->gdpy; 118 119 gsurf->server_stamp++; 120 gdpy->event_handler->invalid_surface(&gdpy->base, 121 &gsurf->base, gsurf->server_stamp); 122} 123 124static boolean 125gdi_surface_flush_frontbuffer(struct native_surface *nsurf) 126{ 127 struct gdi_surface *gsurf = gdi_surface(nsurf); 128 HDC hDC; 129 boolean ret; 130 131 hDC = GetDC(gsurf->hWnd); 132 ret = resource_surface_present(gsurf->rsurf, 133 NATIVE_ATTACHMENT_FRONT_LEFT, (void *) hDC); 134 ReleaseDC(gsurf->hWnd, hDC); 135 136 /* force buffers to be updated in next validation call */ 137 gdi_surface_invalidate(&gsurf->base); 138 139 return ret; 140} 141 142static boolean 143gdi_surface_swap_buffers(struct native_surface *nsurf) 144{ 145 struct gdi_surface *gsurf = gdi_surface(nsurf); 146 HDC hDC; 147 boolean ret; 148 149 hDC = GetDC(gsurf->hWnd); 150 ret = resource_surface_present(gsurf->rsurf, 151 NATIVE_ATTACHMENT_BACK_LEFT, (void *) hDC); 152 ReleaseDC(gsurf->hWnd, hDC); 153 154 resource_surface_swap_buffers(gsurf->rsurf, 155 NATIVE_ATTACHMENT_FRONT_LEFT, NATIVE_ATTACHMENT_BACK_LEFT, TRUE); 156 /* the front/back buffers have been swapped */ 157 gdi_surface_invalidate(&gsurf->base); 158 159 return ret; 160} 161 162static boolean 163gdi_surface_present(struct native_surface *nsurf, 164 enum native_attachment natt, 165 boolean preserve, 166 uint swap_interval) 167{ 168 boolean ret; 169 170 if (preserve || swap_interval) 171 return FALSE; 172 173 switch (natt) { 174 case NATIVE_ATTACHMENT_FRONT_LEFT: 175 ret = gdi_surface_flush_frontbuffer(nsurf); 176 break; 177 case NATIVE_ATTACHMENT_BACK_LEFT: 178 ret = gdi_surface_swap_buffers(nsurf); 179 break; 180 default: 181 ret = FALSE; 182 break; 183 } 184 185 return ret; 186} 187 188static boolean 189gdi_surface_validate(struct native_surface *nsurf, uint attachment_mask, 190 unsigned int *seq_num, struct pipe_resource **textures, 191 int *width, int *height) 192{ 193 struct gdi_surface *gsurf = gdi_surface(nsurf); 194 uint w, h; 195 196 if (!gdi_surface_update_buffers(&gsurf->base, attachment_mask)) 197 return FALSE; 198 199 if (seq_num) 200 *seq_num = gsurf->client_stamp; 201 202 if (textures) 203 resource_surface_get_resources(gsurf->rsurf, textures, attachment_mask); 204 205 resource_surface_get_size(gsurf->rsurf, &w, &h); 206 if (width) 207 *width = w; 208 if (height) 209 *height = h; 210 211 return TRUE; 212} 213 214static void 215gdi_surface_wait(struct native_surface *nsurf) 216{ 217 /* no-op */ 218} 219 220static void 221gdi_surface_destroy(struct native_surface *nsurf) 222{ 223 struct gdi_surface *gsurf = gdi_surface(nsurf); 224 225 resource_surface_destroy(gsurf->rsurf); 226 FREE(gsurf); 227} 228 229static struct native_surface * 230gdi_display_create_window_surface(struct native_display *ndpy, 231 EGLNativeWindowType win, 232 const struct native_config *nconf) 233{ 234 struct gdi_display *gdpy = gdi_display(ndpy); 235 struct gdi_surface *gsurf; 236 237 gsurf = CALLOC_STRUCT(gdi_surface); 238 if (!gsurf) 239 return NULL; 240 241 gsurf->gdpy = gdpy; 242 gsurf->color_format = nconf->color_format; 243 gsurf->hWnd = (HWND) win; 244 245 gsurf->rsurf = resource_surface_create(gdpy->base.screen, 246 gsurf->color_format, 247 PIPE_BIND_RENDER_TARGET | 248 PIPE_BIND_SAMPLER_VIEW | 249 PIPE_BIND_DISPLAY_TARGET | 250 PIPE_BIND_SCANOUT); 251 if (!gsurf->rsurf) { 252 FREE(gsurf); 253 return NULL; 254 } 255 256 /* initialize the geometry */ 257 gdi_surface_update_geometry(&gsurf->base); 258 259 gsurf->base.destroy = gdi_surface_destroy; 260 gsurf->base.swap_buffers = gdi_surface_swap_buffers; 261 gsurf->base.flush_frontbuffer = gdi_surface_flush_frontbuffer; 262 xsurf->base.present = gdi_surface_present; 263 gsurf->base.validate = gdi_surface_validate; 264 gsurf->base.wait = gdi_surface_wait; 265 266 return &gsurf->base; 267} 268 269static int 270fill_color_formats(struct native_display *ndpy, enum pipe_format formats[8]) 271{ 272 struct pipe_screen *screen = ndpy->screen; 273 int i, count = 0; 274 275 enum pipe_format candidates[] = { 276 /* 32-bit */ 277 PIPE_FORMAT_B8G8R8A8_UNORM, 278 PIPE_FORMAT_A8R8G8B8_UNORM, 279 /* 24-bit */ 280 PIPE_FORMAT_B8G8R8X8_UNORM, 281 PIPE_FORMAT_X8R8G8B8_UNORM, 282 /* 16-bit */ 283 PIPE_FORMAT_B5G6R5_UNORM 284 }; 285 286 assert(Elements(candidates) <= 8); 287 288 for (i = 0; i < Elements(candidates); i++) { 289 if (screen->is_format_supported(screen, candidates[i], 290 PIPE_TEXTURE_2D, 0, PIPE_BIND_RENDER_TARGET, 0)) 291 formats[count++] = candidates[i]; 292 } 293 294 return count; 295} 296 297static const struct native_config ** 298gdi_display_get_configs(struct native_display *ndpy, int *num_configs) 299{ 300 struct gdi_display *gdpy = gdi_display(ndpy); 301 const struct native_config **configs; 302 int i; 303 304 /* first time */ 305 if (!gdpy->configs) { 306 enum pipe_format formats[8]; 307 int i, count; 308 309 count = fill_color_formats(&gdpy->base, formats); 310 311 gdpy->configs = CALLOC(count, sizeof(*gdpy->configs)); 312 if (!gdpy->configs) 313 return NULL; 314 315 for (i = 0; i < count; i++) { 316 struct native_config *nconf = &gdpy->configs[i]; 317 318 nconf->buffer_mask = 319 (1 << NATIVE_ATTACHMENT_FRONT_LEFT) | 320 (1 << NATIVE_ATTACHMENT_BACK_LEFT); 321 nconf->color_format = formats[i]; 322 323 nconf->window_bit = TRUE; 324 nconf->slow_config = TRUE; 325 } 326 327 gdpy->num_configs = count; 328 } 329 330 configs = MALLOC(gdpy->num_configs * sizeof(*configs)); 331 if (configs) { 332 for (i = 0; i < gdpy->num_configs; i++) 333 configs[i] = (const struct native_config *) &gdpy->configs[i]; 334 if (num_configs) 335 *num_configs = gdpy->num_configs; 336 } 337 return configs; 338} 339 340static int 341gdi_display_get_param(struct native_display *ndpy, 342 enum native_param_type param) 343{ 344 int val; 345 346 switch (param) { 347 case NATIVE_PARAM_USE_NATIVE_BUFFER: 348 /* private buffers are allocated */ 349 val = FALSE; 350 break; 351 case NATIVE_PARAM_PRESERVE_BUFFER: 352 case NATIVE_PARAM_MAX_SWAP_INTERVAL: 353 default: 354 val = 0; 355 break; 356 } 357 358 return val; 359} 360 361static void 362gdi_display_destroy(struct native_display *ndpy) 363{ 364 struct gdi_display *gdpy = gdi_display(ndpy); 365 366 if (gdpy->configs) 367 FREE(gdpy->configs); 368 369 gdpy->base.screen->destroy(gdpy->base.screen); 370 371 FREE(gdpy); 372} 373 374static struct native_display * 375gdi_create_display(HDC hDC, struct native_event_handler *event_handler, 376 void *user_data) 377{ 378 struct gdi_display *gdpy; 379 struct sw_winsys *winsys; 380 381 gdpy = CALLOC_STRUCT(gdi_display); 382 if (!gdpy) 383 return NULL; 384 385 gdpy->hDC = hDC; 386 gdpy->event_handler = event_handler; 387 gdpy->base.user_data = user_data; 388 389 winsys = gdi_create_sw_winsys(); 390 if (!winsys) { 391 FREE(gdpy); 392 return NULL; 393 } 394 395 gdpy->base.screen = gdpy->event_handler->new_sw_screen(&gdpy->base, winsys); 396 if (!gdpy->base.screen) { 397 if (winsys->destroy) 398 winsys->destroy(winsys); 399 FREE(gdpy); 400 return NULL; 401 } 402 403 gdpy->base.destroy = gdi_display_destroy; 404 gdpy->base.get_param = gdi_display_get_param; 405 406 gdpy->base.get_configs = gdi_display_get_configs; 407 gdpy->base.create_window_surface = gdi_display_create_window_surface; 408 409 return &gdpy->base; 410} 411 412static struct native_display * 413native_create_display(void *dpy, struct native_event_handler *event_handler, 414 void *user_data) 415{ 416 return gdi_create_display((HDC) dpy, event_handler, user_data); 417} 418 419static const struct native_platform gdi_platform = { 420 "GDI", /* name */ 421 native_create_display 422}; 423 424const struct native_platform * 425native_get_gdi_platform(void) 426{ 427 return &gdi_platform; 428} 429