Mercurial > sdl-ios-xcode
comparison src/SDL_compat.c @ 2222:926294b2bb4e
Emphasized the separation between SDL_Surface and SDL_Texture
- SDL_Surface is a system memory representation of pixel data
- SDL_Texture is a video memory representation of pixel data
The concept of SDL_Surface with SDL_HWSURFACE is no longer used.
Separated SDL_Texture types by usage rather than memory type
- SDL_TEXTUREACCESS_STATIC is for rarely changed pixel data,
can be placed in video memory.
- SDL_TEXTUREACCESS_STREAMING is for frequently changing pixel
data, usually placed in system memory or AGP memory.
Optimized the SDL_compat usage of the OpenGL renderer by only
using one copy of the framebuffer instead of two.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 11 Aug 2007 20:54:31 +0000 |
parents | 98e76ba7d5a4 |
children | 0e70b4b8cf84 |
comparison
equal
deleted
inserted
replaced
2221:1d75c38e1e5c | 2222:926294b2bb4e |
---|---|
326 *x = (mode.w - w) / 2; | 326 *x = (mode.w - w) / 2; |
327 *y = (mode.h - h) / 2; | 327 *y = (mode.h - h) / 2; |
328 } | 328 } |
329 } | 329 } |
330 | 330 |
331 static SDL_Surface * | |
332 CreateVideoSurface(SDL_TextureID textureID) | |
333 { | |
334 SDL_Surface *surface; | |
335 Uint32 format; | |
336 int w, h; | |
337 int bpp; | |
338 Uint32 Rmask, Gmask, Bmask, Amask; | |
339 void *pixels; | |
340 int pitch; | |
341 | |
342 if (SDL_QueryTexture(textureID, &format, NULL, &w, &h) < 0) { | |
343 return NULL; | |
344 } | |
345 | |
346 if (!SDL_PixelFormatEnumToMasks | |
347 (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) { | |
348 SDL_SetError("Unknown texture format"); | |
349 return NULL; | |
350 } | |
351 | |
352 if (SDL_QueryTexturePixels(textureID, &pixels, &pitch) == 0) { | |
353 surface = | |
354 SDL_CreateRGBSurfaceFrom(pixels, w, h, bpp, pitch, Rmask, Gmask, | |
355 Bmask, Amask); | |
356 } else { | |
357 surface = | |
358 SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask); | |
359 } | |
360 return surface; | |
361 } | |
362 | |
331 SDL_Surface * | 363 SDL_Surface * |
332 SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags) | 364 SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags) |
333 { | 365 { |
334 SDL_DisplayMode desktop_mode; | 366 SDL_DisplayMode desktop_mode; |
335 SDL_DisplayMode mode; | 367 SDL_DisplayMode mode; |
481 if (SDL_CreateRenderer | 513 if (SDL_CreateRenderer |
482 (SDL_VideoWindow, -1, | 514 (SDL_VideoWindow, -1, |
483 SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD) < 0) { | 515 SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD) < 0) { |
484 return NULL; | 516 return NULL; |
485 } | 517 } |
486 SDL_GetRenderDriverInfo(-1, &SDL_VideoRendererInfo); | 518 SDL_GetRendererInfo(&SDL_VideoRendererInfo); |
487 | 519 |
488 /* Create a texture for the screen surface */ | 520 /* Create a texture for the screen surface */ |
489 SDL_VideoTexture = | 521 SDL_VideoTexture = |
490 SDL_CreateTexture(desired_format, SDL_TEXTUREACCESS_LOCAL, width, | 522 SDL_CreateTexture(desired_format, SDL_TEXTUREACCESS_STREAMING, width, |
491 height); | 523 height); |
492 if (!SDL_VideoTexture) { | 524 if (!SDL_VideoTexture) { |
493 SDL_VideoTexture = | 525 SDL_VideoTexture = |
494 SDL_CreateTexture(SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_LOCAL, | 526 SDL_CreateTexture(SDL_PIXELFORMAT_RGB888, |
495 width, height); | 527 SDL_TEXTUREACCESS_STREAMING, width, height); |
496 } | 528 } |
497 if (!SDL_VideoTexture) { | 529 if (!SDL_VideoTexture) { |
498 return NULL; | 530 return NULL; |
499 } | 531 } |
500 | 532 |
501 /* Create the screen surface */ | 533 /* Create the screen surface */ |
502 SDL_VideoSurface = SDL_CreateRGBSurfaceFromTexture(SDL_VideoTexture); | 534 SDL_VideoSurface = CreateVideoSurface(SDL_VideoTexture); |
503 if (!SDL_VideoSurface) { | 535 if (!SDL_VideoSurface) { |
504 return NULL; | 536 return NULL; |
505 } | 537 } |
506 SDL_VideoSurface->flags |= surface_flags; | 538 SDL_VideoSurface->flags |= surface_flags; |
507 | 539 |
516 SDL_VideoSurface->format->palette->colors, 0, | 548 SDL_VideoSurface->format->palette->colors, 0, |
517 SDL_VideoSurface->format->palette->ncolors); | 549 SDL_VideoSurface->format->palette->ncolors); |
518 } | 550 } |
519 | 551 |
520 /* Create a shadow surface if necessary */ | 552 /* Create a shadow surface if necessary */ |
521 if (((bpp != SDL_VideoSurface->format->BitsPerPixel) | 553 if ((bpp != SDL_VideoSurface->format->BitsPerPixel) |
522 && !(flags & SDL_ANYFORMAT)) | 554 && !(flags & SDL_ANYFORMAT)) { |
523 || ((SDL_VideoSurface->flags & SDL_HWSURFACE) | 555 SDL_ShadowSurface = |
524 && !(flags & SDL_HWSURFACE))) { | 556 SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0); |
525 if ((bpp == SDL_VideoSurface->format->BitsPerPixel) | |
526 || (flags & SDL_ANYFORMAT)) { | |
527 SDL_ShadowSurface = | |
528 SDL_CreateRGBSurface(0, width, height, | |
529 SDL_VideoSurface->format->BitsPerPixel, | |
530 SDL_VideoSurface->format->Rmask, | |
531 SDL_VideoSurface->format->Gmask, | |
532 SDL_VideoSurface->format->Bmask, | |
533 SDL_VideoSurface->format->Amask); | |
534 } else { | |
535 SDL_ShadowSurface = | |
536 SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0); | |
537 } | |
538 if (!SDL_ShadowSurface) { | 557 if (!SDL_ShadowSurface) { |
539 return NULL; | 558 return NULL; |
540 } | 559 } |
541 SDL_ShadowSurface->flags |= surface_flags; | 560 SDL_ShadowSurface->flags |= surface_flags; |
542 | 561 |
636 /* We have no other optimised formats right now. When/if a new | 655 /* We have no other optimised formats right now. When/if a new |
637 optimised alpha format is written, add the converter here */ | 656 optimised alpha format is written, add the converter here */ |
638 break; | 657 break; |
639 } | 658 } |
640 format = SDL_AllocFormat(32, rmask, gmask, bmask, amask); | 659 format = SDL_AllocFormat(32, rmask, gmask, bmask, amask); |
641 flags = SDL_PublicSurface->flags & SDL_HWSURFACE; | 660 flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
642 flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); | |
643 converted = SDL_ConvertSurface(surface, format, flags); | 661 converted = SDL_ConvertSurface(surface, format, flags); |
644 SDL_FreeFormat(format); | 662 SDL_FreeFormat(format); |
645 return converted; | 663 return converted; |
646 } | 664 } |
647 | 665 |
679 | 697 |
680 /* Fall through to video surface update */ | 698 /* Fall through to video surface update */ |
681 screen = SDL_VideoSurface; | 699 screen = SDL_VideoSurface; |
682 } | 700 } |
683 if (screen == SDL_VideoSurface) { | 701 if (screen == SDL_VideoSurface) { |
702 if (screen->flags & SDL_PREALLOC) { | |
703 /* The surface memory is maintained by the renderer */ | |
704 SDL_DirtyTexture(SDL_VideoTexture, numrects, rects); | |
705 } else { | |
706 /* The surface memory needs to be copied to texture */ | |
707 void *pixels; | |
708 int pitch = screen->pitch; | |
709 int psize = screen->format->BytesPerPixel; | |
710 for (i = 0; i < numrects; ++i) { | |
711 const SDL_Rect *rect = &rects[i]; | |
712 void *pixels = | |
713 (Uint8 *) screen->pixels + rect->y * pitch + | |
714 rect->x * psize; | |
715 SDL_UpdateTexture(SDL_VideoTexture, rect, pixels, pitch); | |
716 } | |
717 } | |
684 if (SDL_VideoRendererInfo.flags & SDL_RENDERER_PRESENTCOPY) { | 718 if (SDL_VideoRendererInfo.flags & SDL_RENDERER_PRESENTCOPY) { |
685 for (i = 0; i < numrects; ++i) { | 719 for (i = 0; i < numrects; ++i) { |
686 SDL_RenderCopy(SDL_VideoTexture, &rects[i], &rects[i]); | 720 SDL_RenderCopy(SDL_VideoTexture, &rects[i], &rects[i]); |
687 } | 721 } |
688 } else { | 722 } else { |
1172 | 1206 |
1173 /* Unlock the screen and update if necessary */ | 1207 /* Unlock the screen and update if necessary */ |
1174 if (SDL_MUSTLOCK(screen)) { | 1208 if (SDL_MUSTLOCK(screen)) { |
1175 SDL_UnlockSurface(screen); | 1209 SDL_UnlockSurface(screen); |
1176 } | 1210 } |
1177 if ((screen->flags & SDL_SCREEN_SURFACE) && | 1211 if (screen->flags & SDL_SCREEN_SURFACE) { |
1178 !(screen->flags & SDL_HWSURFACE)) { | |
1179 SDL_VideoDevice *_this = SDL_GetVideoDevice(); | 1212 SDL_VideoDevice *_this = SDL_GetVideoDevice(); |
1180 SDL_Window *window; | 1213 SDL_Window *window; |
1181 SDL_Rect area; | 1214 SDL_Rect area; |
1182 | 1215 |
1183 window = SDL_GetWindowFromSurface(screen); | 1216 window = SDL_GetWindowFromSurface(screen); |
1261 | 1294 |
1262 /* Unlock the screen and update if necessary */ | 1295 /* Unlock the screen and update if necessary */ |
1263 if (SDL_MUSTLOCK(screen)) { | 1296 if (SDL_MUSTLOCK(screen)) { |
1264 SDL_UnlockSurface(screen); | 1297 SDL_UnlockSurface(screen); |
1265 } | 1298 } |
1266 if ((screen->flags & SDL_SCREEN_SURFACE) && | 1299 if (screen->flags & SDL_SCREEN_SURFACE) { |
1267 !(screen->flags & SDL_HWSURFACE)) { | |
1268 SDL_VideoDevice *_this = SDL_GetVideoDevice(); | 1300 SDL_VideoDevice *_this = SDL_GetVideoDevice(); |
1269 SDL_Window *window; | 1301 SDL_Window *window; |
1270 SDL_Rect area; | 1302 SDL_Rect area; |
1271 | 1303 |
1272 window = SDL_GetWindowFromSurface(screen); | 1304 window = SDL_GetWindowFromSurface(screen); |
1383 overlay->pitches[0] = overlay->w * 2; | 1415 overlay->pitches[0] = overlay->w * 2; |
1384 break; | 1416 break; |
1385 } | 1417 } |
1386 | 1418 |
1387 overlay->hwdata->textureID = | 1419 overlay->hwdata->textureID = |
1388 SDL_CreateTexture(texture_format, SDL_TEXTUREACCESS_LOCAL, w, h); | 1420 SDL_CreateTexture(texture_format, SDL_TEXTUREACCESS_STREAMING, w, h); |
1389 if (!overlay->hwdata->textureID) { | 1421 if (!overlay->hwdata->textureID) { |
1390 SDL_FreeYUVOverlay(overlay); | 1422 SDL_FreeYUVOverlay(overlay); |
1391 return NULL; | 1423 return NULL; |
1392 } | 1424 } |
1393 | 1425 |