comparison src/video/x11/SDL_x11render.c @ 2814:e841aa93e6be

indent
author Sam Lantinga <slouken@libsdl.org>
date Mon, 01 Dec 2008 00:07:45 +0000
parents 7af2419ad5b0
children aec4399c507a
comparison
equal deleted inserted replaced
2813:49243a6e9ff1 2814:e841aa93e6be
102 } X11_TextureData; 102 } X11_TextureData;
103 103
104 #ifndef NO_SHARED_MEMORY 104 #ifndef NO_SHARED_MEMORY
105 /* Shared memory error handler routine */ 105 /* Shared memory error handler routine */
106 static int shm_error; 106 static int shm_error;
107 static int (*X_handler)(Display *, XErrorEvent *) = NULL; 107 static int (*X_handler) (Display *, XErrorEvent *) = NULL;
108 static int shm_errhandler(Display *d, XErrorEvent *e) 108 static int
109 { 109 shm_errhandler(Display * d, XErrorEvent * e)
110 if (e->error_code == BadAccess) { 110 {
111 shm_error = True; 111 if (e->error_code == BadAccess) {
112 return(0); 112 shm_error = True;
113 } else { 113 return (0);
114 return(X_handler(d,e)); 114 } else {
115 } 115 return (X_handler(d, e));
116 }
116 } 117 }
117 #endif /* ! NO_SHARED_MEMORY */ 118 #endif /* ! NO_SHARED_MEMORY */
118 119
119 static void 120 static void
120 UpdateYUVTextureData(SDL_Texture * texture) 121 UpdateYUVTextureData(SDL_Texture * texture)
129 SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w, 130 SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w,
130 texture->h, data->pixels, data->pitch); 131 texture->h, data->pixels, data->pitch);
131 } 132 }
132 133
133 static Uint32 134 static Uint32
134 X11_GetPixelFormatFromDepth(Display *display, int screen, int depth, int bpp) 135 X11_GetPixelFormatFromDepth(Display * display, int screen, int depth, int bpp)
135 { 136 {
136 XVisualInfo vinfo; 137 XVisualInfo vinfo;
137 138
138 if (XMatchVisualInfo(display, screen, depth, DirectColor, &vinfo) || 139 if (XMatchVisualInfo(display, screen, depth, DirectColor, &vinfo) ||
139 XMatchVisualInfo(display, screen, depth, TrueColor, &vinfo)) { 140 XMatchVisualInfo(display, screen, depth, TrueColor, &vinfo)) {
150 return SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask); 151 return SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
151 } 152 }
152 153
153 /* No matching visual, try to pick a safe default */ 154 /* No matching visual, try to pick a safe default */
154 switch (depth) { 155 switch (depth) {
155 case 15: 156 case 15:
156 return SDL_PIXELFORMAT_RGB555; 157 return SDL_PIXELFORMAT_RGB555;
157 case 16: 158 case 16:
158 return SDL_PIXELFORMAT_RGB565; 159 return SDL_PIXELFORMAT_RGB565;
159 default: 160 default:
160 break; 161 break;
161 } 162 }
162 return SDL_PIXELFORMAT_UNKNOWN; 163 return SDL_PIXELFORMAT_UNKNOWN;
163 } 164 }
164 165
165 void 166 void
180 if (pixmapFormats[i].depth == 24) { 181 if (pixmapFormats[i].depth == 24) {
181 bpp = pixmapFormats[i].bits_per_pixel; 182 bpp = pixmapFormats[i].bits_per_pixel;
182 } else { 183 } else {
183 bpp = pixmapFormats[i].depth; 184 bpp = pixmapFormats[i].depth;
184 } 185 }
185 format = X11_GetPixelFormatFromDepth(data->display, DefaultScreen(data->display), pixmapFormats[i].depth, bpp); 186 format =
187 X11_GetPixelFormatFromDepth(data->display,
188 DefaultScreen(data->display),
189 pixmapFormats[i].depth, bpp);
186 if (format != SDL_PIXELFORMAT_UNKNOWN) { 190 if (format != SDL_PIXELFORMAT_UNKNOWN) {
187 info->texture_formats[info->num_texture_formats++] = format; 191 info->texture_formats[info->num_texture_formats++] = format;
188 } 192 }
189 } 193 }
190 XFree(pixmapFormats); 194 XFree(pixmapFormats);
191 195
192 if (info->num_texture_formats == 0) { 196 if (info->num_texture_formats == 0) {
193 return; 197 return;
194 } 198 }
195 info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_YV12; 199 info->texture_formats[info->num_texture_formats++] =
196 info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_IYUV; 200 SDL_PIXELFORMAT_YV12;
197 info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_YUY2; 201 info->texture_formats[info->num_texture_formats++] =
198 info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_UYVY; 202 SDL_PIXELFORMAT_IYUV;
199 info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_YVYU; 203 info->texture_formats[info->num_texture_formats++] =
204 SDL_PIXELFORMAT_YUY2;
205 info->texture_formats[info->num_texture_formats++] =
206 SDL_PIXELFORMAT_UYVY;
207 info->texture_formats[info->num_texture_formats++] =
208 SDL_PIXELFORMAT_YVYU;
200 } 209 }
201 210
202 SDL_AddRenderDriver(0, &X11_RenderDriver); 211 SDL_AddRenderDriver(0, &X11_RenderDriver);
203 } 212 }
204 213
266 renderer->info.flags |= SDL_RENDERER_PRESENTCOPY; 275 renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
267 n = 1; 276 n = 1;
268 } 277 }
269 XGetWindowAttributes(data->display, data->window, &attributes); 278 XGetWindowAttributes(data->display, data->window, &attributes);
270 for (i = 0; i < n; ++i) { 279 for (i = 0; i < n; ++i) {
271 data->pixmaps[i] = XCreatePixmap(data->display, data->window, window->w, window->h, attributes.depth); 280 data->pixmaps[i] =
281 XCreatePixmap(data->display, data->window, window->w, window->h,
282 attributes.depth);
272 if (data->pixmaps[i] == None) { 283 if (data->pixmaps[i] == None) {
273 X11_DestroyRenderer(renderer); 284 X11_DestroyRenderer(renderer);
274 SDL_SetError("XCreatePixmap() failed"); 285 SDL_SetError("XCreatePixmap() failed");
275 return NULL; 286 return NULL;
276 } 287 }
312 return NULL; 323 return NULL;
313 } 324 }
314 325
315 /* Create the drawing context */ 326 /* Create the drawing context */
316 gcv.graphics_exposures = False; 327 gcv.graphics_exposures = False;
317 data->gc = XCreateGC(data->display, data->window, GCGraphicsExposures, &gcv); 328 data->gc =
329 XCreateGC(data->display, data->window, GCGraphicsExposures, &gcv);
318 if (!data->gc) { 330 if (!data->gc) {
319 X11_DestroyRenderer(renderer); 331 X11_DestroyRenderer(renderer);
320 SDL_SetError("XCreateGC() failed"); 332 SDL_SetError("XCreateGC() failed");
321 return NULL; 333 return NULL;
322 } 334 }
347 data->pixmaps[i] = None; 359 data->pixmaps[i] = None;
348 } 360 }
349 } 361 }
350 XGetWindowAttributes(data->display, data->window, &attributes); 362 XGetWindowAttributes(data->display, data->window, &attributes);
351 for (i = 0; i < n; ++i) { 363 for (i = 0; i < n; ++i) {
352 data->pixmaps[i] = XCreatePixmap(data->display, data->window, window->w, window->h, attributes.depth); 364 data->pixmaps[i] =
365 XCreatePixmap(data->display, data->window, window->w, window->h,
366 attributes.depth);
353 if (data->pixmaps[i] == None) { 367 if (data->pixmaps[i] == None) {
354 SDL_SetError("XCreatePixmap() failed"); 368 SDL_SetError("XCreatePixmap() failed");
355 return -1; 369 return -1;
356 } 370 }
357 } 371 }
389 } 403 }
390 data->format = display->current_mode.format; 404 data->format = display->current_mode.format;
391 } else { 405 } else {
392 data->format = texture->format; 406 data->format = texture->format;
393 } 407 }
394 data->pitch = texture->w*SDL_BYTESPERPIXEL(data->format); 408 data->pitch = texture->w * SDL_BYTESPERPIXEL(data->format);
395 409
396 XGetWindowAttributes(renderdata->display, renderdata->window, &attributes); 410 XGetWindowAttributes(renderdata->display, renderdata->window,
411 &attributes);
397 depth = SDL_BITSPERPIXEL(data->format); 412 depth = SDL_BITSPERPIXEL(data->format);
398 order = SDL_PIXELORDER(data->format); 413 order = SDL_PIXELORDER(data->format);
399 if (depth == 32 && (order == SDL_PACKEDORDER_XRGB || order == SDL_PACKEDORDER_RGBX || SDL_PACKEDORDER_XBGR || order == SDL_PACKEDORDER_BGRX)) { 414 if (depth == 32
415 && (order == SDL_PACKEDORDER_XRGB || order == SDL_PACKEDORDER_RGBX
416 || SDL_PACKEDORDER_XBGR || order == SDL_PACKEDORDER_BGRX)) {
400 depth = 24; 417 depth = 24;
401 } 418 }
402 419
403 if (data->yuv || texture->access == SDL_TEXTUREACCESS_STREAMING) { 420 if (data->yuv || texture->access == SDL_TEXTUREACCESS_STREAMING) {
404 #ifndef NO_SHARED_MEMORY 421 #ifndef NO_SHARED_MEMORY
405 XShmSegmentInfo *shminfo = &data->shminfo; 422 XShmSegmentInfo *shminfo = &data->shminfo;
406 423
407 shm_error = True; 424 shm_error = True;
408 425
409 if (SDL_X11_HAVE_SHM) { 426 if (SDL_X11_HAVE_SHM) {
410 shminfo->shmid = shmget(IPC_PRIVATE, texture->h*data->pitch, IPC_CREAT | 0777); 427 shminfo->shmid =
428 shmget(IPC_PRIVATE, texture->h * data->pitch,
429 IPC_CREAT | 0777);
411 if (shminfo->shmid >= 0) { 430 if (shminfo->shmid >= 0) {
412 shminfo->shmaddr = (char *)shmat(shminfo->shmid, 0, 0); 431 shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
413 shminfo->readOnly = False; 432 shminfo->readOnly = False;
414 if (shminfo->shmaddr != (char *)-1) { 433 if (shminfo->shmaddr != (char *) -1) {
415 shm_error = False; 434 shm_error = False;
416 X_handler = XSetErrorHandler(shm_errhandler); 435 X_handler = XSetErrorHandler(shm_errhandler);
417 XShmAttach(renderdata->display, shminfo); 436 XShmAttach(renderdata->display, shminfo);
418 XSync(renderdata->display, True); 437 XSync(renderdata->display, True);
419 XSetErrorHandler(X_handler); 438 XSetErrorHandler(X_handler);
425 } 444 }
426 } 445 }
427 if (!shm_error) { 446 if (!shm_error) {
428 data->pixels = shminfo->shmaddr; 447 data->pixels = shminfo->shmaddr;
429 448
430 data->image = XShmCreateImage(renderdata->display, attributes.visual, depth, ZPixmap, shminfo->shmaddr, shminfo, texture->w, texture->h); 449 data->image =
431 if(!data->image) { 450 XShmCreateImage(renderdata->display, attributes.visual, depth,
451 ZPixmap, shminfo->shmaddr, shminfo,
452 texture->w, texture->h);
453 if (!data->image) {
432 XShmDetach(renderdata->display, shminfo); 454 XShmDetach(renderdata->display, shminfo);
433 XSync(renderdata->display, False); 455 XSync(renderdata->display, False);
434 shmdt(shminfo->shmaddr); 456 shmdt(shminfo->shmaddr);
435 shm_error = True; 457 shm_error = True;
436 } 458 }
445 if (!data->pixels) { 467 if (!data->pixels) {
446 SDL_OutOfMemory(); 468 SDL_OutOfMemory();
447 return -1; 469 return -1;
448 } 470 }
449 471
450 data->image = XCreateImage(renderdata->display, attributes.visual, depth, ZPixmap, 0, data->pixels, texture->w, texture->h, SDL_BYTESPERPIXEL(data->format)*8, data->pitch); 472 data->image =
473 XCreateImage(renderdata->display, attributes.visual, depth,
474 ZPixmap, 0, data->pixels, texture->w, texture->h,
475 SDL_BYTESPERPIXEL(data->format) * 8,
476 data->pitch);
451 if (!data->image) { 477 if (!data->image) {
452 SDL_SetError("XCreateImage() failed"); 478 SDL_SetError("XCreateImage() failed");
453 return -1; 479 return -1;
454 } 480 }
455 } 481 }
456 } else { 482 } else {
457 data->pixmap = XCreatePixmap(renderdata->display, renderdata->window, texture->w, texture->h, depth); 483 data->pixmap =
484 XCreatePixmap(renderdata->display, renderdata->window, texture->w,
485 texture->h, depth);
458 if (data->pixmap == None) { 486 if (data->pixmap == None) {
459 SDL_SetError("XCteatePixmap() failed"); 487 SDL_SetError("XCteatePixmap() failed");
460 return -1; 488 return -1;
461 } 489 }
462 490
463 data->image = XCreateImage(renderdata->display, attributes.visual, depth, ZPixmap, 0, NULL, texture->w, texture->h, SDL_BYTESPERPIXEL(data->format)*8, data->pitch); 491 data->image =
492 XCreateImage(renderdata->display, attributes.visual, depth,
493 ZPixmap, 0, NULL, texture->w, texture->h,
494 SDL_BYTESPERPIXEL(data->format) * 8, data->pitch);
464 if (!data->image) { 495 if (!data->image) {
465 SDL_SetError("XCreateImage() failed"); 496 SDL_SetError("XCreateImage() failed");
466 return -1; 497 return -1;
467 } 498 }
468 } 499 }
543 dst += data->pitch; 574 dst += data->pitch;
544 } 575 }
545 } else { 576 } else {
546 data->image->width = rect->w; 577 data->image->width = rect->w;
547 data->image->height = rect->h; 578 data->image->height = rect->h;
548 data->image->data = (char *)pixels; 579 data->image->data = (char *) pixels;
549 data->image->bytes_per_line = pitch; 580 data->image->bytes_per_line = pitch;
550 XPutImage(renderdata->display, data->pixmap, renderdata->gc, data->image, 0, 0, rect->x, rect->y, rect->w, rect->h); 581 XPutImage(renderdata->display, data->pixmap, renderdata->gc,
582 data->image, 0, 0, rect->x, rect->y, rect->w, rect->h);
551 } 583 }
552 return 0; 584 return 0;
553 } 585 }
554 } 586 }
555 587
597 SDL_AddDirtyRect(&data->dirty, rect); 629 SDL_AddDirtyRect(&data->dirty, rect);
598 } 630 }
599 631
600 foreground = SDL_MapRGBA(data->format, r, g, b, a); 632 foreground = SDL_MapRGBA(data->format, r, g, b, a);
601 XSetForeground(data->display, data->gc, foreground); 633 XSetForeground(data->display, data->gc, foreground);
602 XDrawRectangle(data->display, data->drawable, data->gc, rect->x, rect->y, rect->w, rect->h); 634 XDrawRectangle(data->display, data->drawable, data->gc, rect->x, rect->y,
635 rect->w, rect->h);
603 return 0; 636 return 0;
604 } 637 }
605 638
606 static int 639 static int
607 X11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, 640 X11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
615 } 648 }
616 649
617 if (srcrect->w == dstrect->w && srcrect->h == dstrect->h) { 650 if (srcrect->w == dstrect->w && srcrect->h == dstrect->h) {
618 #ifndef NO_SHARED_MEMORY 651 #ifndef NO_SHARED_MEMORY
619 if (texturedata->shminfo.shmaddr) { 652 if (texturedata->shminfo.shmaddr) {
620 XShmPutImage(data->display, data->drawable, data->gc, texturedata->image, srcrect->x, srcrect->y, dstrect->x, dstrect->y, dstrect->w, dstrect->h, False); 653 XShmPutImage(data->display, data->drawable, data->gc,
621 } 654 texturedata->image, srcrect->x, srcrect->y,
622 else 655 dstrect->x, dstrect->y, dstrect->w, dstrect->h,
656 False);
657 } else
623 #endif 658 #endif
624 if (texturedata->pixels) { 659 if (texturedata->pixels) {
625 XPutImage(data->display, data->drawable, data->gc, texturedata->image, srcrect->x, srcrect->y, dstrect->x, dstrect->y, dstrect->w, dstrect->h); 660 XPutImage(data->display, data->drawable, data->gc,
661 texturedata->image, srcrect->x, srcrect->y, dstrect->x,
662 dstrect->y, dstrect->w, dstrect->h);
626 } else { 663 } else {
627 XCopyArea(data->display, texturedata->pixmap, data->drawable, 664 XCopyArea(data->display, texturedata->pixmap, data->drawable,
628 data->gc, srcrect->x, srcrect->y, dstrect->w, dstrect->h, 665 data->gc, srcrect->x, srcrect->y, dstrect->w,
629 dstrect->x, dstrect->y); 666 dstrect->h, dstrect->x, dstrect->y);
630 } 667 }
631 } else { 668 } else {
632 SDL_SetError("Scaling not supported in the X11 renderer"); 669 SDL_SetError("Scaling not supported in the X11 renderer");
633 return -1; 670 return -1;
634 } 671 }