src/video/SDL_yuv.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1643 51038e80ae59
child 1668 4da1ee79c9af
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
    27 #include "SDL_sysvideo.h"
    27 #include "SDL_sysvideo.h"
    28 #include "SDL_yuvfuncs.h"
    28 #include "SDL_yuvfuncs.h"
    29 #include "SDL_yuv_sw_c.h"
    29 #include "SDL_yuv_sw_c.h"
    30 
    30 
    31 
    31 
    32 SDL_Overlay *SDL_CreateYUVOverlay(int w, int h, Uint32 format,
    32 SDL_Overlay *
    33                                   SDL_Surface *display)
    33 SDL_CreateYUVOverlay (int w, int h, Uint32 format, SDL_Surface * display)
    34 {
    34 {
    35 	SDL_VideoDevice *video = current_video;
    35     SDL_VideoDevice *_this = SDL_GetVideoDevice ();
    36 	SDL_VideoDevice *this  = current_video;
    36     SDL_Window *window;
    37 	const char *yuv_hwaccel;
    37     const char *yuv_hwaccel;
    38 	SDL_Overlay *overlay;
    38     SDL_Overlay *overlay;
    39 
    39 
    40 	if ( (display->flags & SDL_OPENGL) == SDL_OPENGL ) {
    40     window = SDL_GetWindowFromSurface (display);
    41 		SDL_SetError("YUV overlays are not supported in OpenGL mode");
    41     if (window && (window->flags & SDL_WINDOW_OPENGL)) {
    42 		return NULL;
    42         SDL_SetError ("YUV overlays are not supported in OpenGL mode");
    43 	}
    43         return NULL;
       
    44     }
    44 
    45 
    45 	/* Display directly on video surface, if possible */
    46     /* Display directly on video surface, if possible */
    46 	if ( SDL_getenv("SDL_VIDEO_YUV_DIRECT") ) {
    47     if (SDL_getenv ("SDL_VIDEO_YUV_DIRECT")) {
    47 		if ( (display == SDL_PublicSurface) &&
    48         if (window &&
    48 		     ((SDL_VideoSurface->format->BytesPerPixel == 2) ||
    49             ((window->surface->format->BytesPerPixel == 2) ||
    49 		      (SDL_VideoSurface->format->BytesPerPixel == 4)) ) {
    50              (window->surface->format->BytesPerPixel == 4))) {
    50 			display = SDL_VideoSurface;
    51             display = window->surface;
    51 		}
    52         }
    52 	}
    53     }
    53 	overlay = NULL;
    54     overlay = NULL;
    54         yuv_hwaccel = SDL_getenv("SDL_VIDEO_YUV_HWACCEL");
    55     yuv_hwaccel = SDL_getenv ("SDL_VIDEO_YUV_HWACCEL");
    55 	if ( ((display == SDL_VideoSurface) && video->CreateYUVOverlay) &&
    56     if (((display->flags & SDL_SCREEN_SURFACE) && _this->CreateYUVOverlay) &&
    56 	     (!yuv_hwaccel || (SDL_atoi(yuv_hwaccel) > 0)) ) {
    57         (!yuv_hwaccel || (SDL_atoi (yuv_hwaccel) > 0))) {
    57 		overlay = video->CreateYUVOverlay(this, w, h, format, display);
    58         overlay = _this->CreateYUVOverlay (_this, w, h, format, display);
    58 	}
    59     }
    59 	/* If hardware YUV overlay failed ... */
    60     /* If hardware YUV overlay failed ... */
    60 	if ( overlay == NULL ) {
    61     if (overlay == NULL) {
    61 		overlay = SDL_CreateYUV_SW(this, w, h, format, display);
    62         overlay = SDL_CreateYUV_SW (_this, w, h, format, display);
    62 	}
    63     }
    63 	return overlay;
    64     return overlay;
    64 }
    65 }
    65 
    66 
    66 int SDL_LockYUVOverlay(SDL_Overlay *overlay)
    67 int
       
    68 SDL_LockYUVOverlay (SDL_Overlay * overlay)
    67 {
    69 {
    68 	return overlay->hwfuncs->Lock(current_video, overlay);
    70     SDL_VideoDevice *_this = SDL_GetVideoDevice ();
       
    71     return overlay->hwfuncs->Lock (_this, overlay);
    69 }
    72 }
    70 
    73 
    71 void SDL_UnlockYUVOverlay(SDL_Overlay *overlay)
    74 void
       
    75 SDL_UnlockYUVOverlay (SDL_Overlay * overlay)
    72 {
    76 {
    73 	overlay->hwfuncs->Unlock(current_video, overlay);
    77     SDL_VideoDevice *_this = SDL_GetVideoDevice ();
       
    78     overlay->hwfuncs->Unlock (_this, overlay);
    74 }
    79 }
    75 
    80 
    76 int SDL_DisplayYUVOverlay(SDL_Overlay *overlay, SDL_Rect *dstrect)
    81 int
       
    82 SDL_DisplayYUVOverlay (SDL_Overlay * overlay, SDL_Rect * dstrect)
    77 {
    83 {
    78 	SDL_Rect src, dst;
    84     SDL_VideoDevice *_this = SDL_GetVideoDevice ();
    79 	int srcx, srcy, srcw, srch;
    85     SDL_Rect src, dst;
    80 	int dstx, dsty, dstw, dsth;
    86     int srcx, srcy, srcw, srch;
       
    87     int dstx, dsty, dstw, dsth;
    81 
    88 
    82 	/* Clip the rectangle to the screen area */
    89     /* Clip the rectangle to the screen area */
    83 	srcx = 0;
    90     srcx = 0;
    84 	srcy = 0;
    91     srcy = 0;
    85 	srcw = overlay->w;
    92     srcw = overlay->w;
    86 	srch = overlay->h;
    93     srch = overlay->h;
    87 	dstx = dstrect->x;
    94     dstx = dstrect->x;
    88 	dsty = dstrect->y;
    95     dsty = dstrect->y;
    89 	dstw = dstrect->w;
    96     dstw = dstrect->w;
    90 	dsth = dstrect->h;
    97     dsth = dstrect->h;
    91 	if ( dstx < 0 ) {
    98     if (dstx < 0) {
    92 		srcw += (dstx * overlay->w) / dstrect->w;
    99         srcw += (dstx * overlay->w) / dstrect->w;
    93 		dstw += dstx;
   100         dstw += dstx;
    94 		srcx -= (dstx * overlay->w) / dstrect->w;
   101         srcx -= (dstx * overlay->w) / dstrect->w;
    95 		dstx = 0;
   102         dstx = 0;
    96 	}
   103     }
    97 	if ( (dstx+dstw) > current_video->screen->w ) {
   104     if ((dstx + dstw) > SDL_VideoSurface->w) {
    98 		int extra = (dstx+dstw - current_video->screen->w);
   105         int extra = (dstx + dstw - SDL_VideoSurface->w);
    99 		srcw -= (extra * overlay->w) / dstrect->w;
   106         srcw -= (extra * overlay->w) / dstrect->w;
   100 		dstw -= extra;
   107         dstw -= extra;
   101 	}
   108     }
   102 	if ( dsty < 0 ) {
   109     if (dsty < 0) {
   103 		srch += (dsty * overlay->h) / dstrect->h;
   110         srch += (dsty * overlay->h) / dstrect->h;
   104 		dsth += dsty;
   111         dsth += dsty;
   105 		srcy -= (dsty * overlay->h) / dstrect->h;
   112         srcy -= (dsty * overlay->h) / dstrect->h;
   106 		dsty = 0;
   113         dsty = 0;
   107 	}
   114     }
   108 	if ( (dsty+dsth) > current_video->screen->h ) {
   115     if ((dsty + dsth) > SDL_VideoSurface->h) {
   109 		int extra = (dsty+dsth - current_video->screen->h);
   116         int extra = (dsty + dsth - SDL_VideoSurface->h);
   110 		srch -= (extra * overlay->h) / dstrect->h;
   117         srch -= (extra * overlay->h) / dstrect->h;
   111 		dsth -= extra;
   118         dsth -= extra;
   112 	}
   119     }
   113 	if ( srcw <= 0 || srch <= 0 ||
   120     if (srcw <= 0 || srch <= 0 || srch <= 0 || dsth <= 0) {
   114 	     srch <= 0 || dsth <= 0 ) {
   121         return 0;
   115 		return 0;
   122     }
   116 	}
   123     /* Ugh, I can't wait for SDL_Rect to be int values */
   117 	/* Ugh, I can't wait for SDL_Rect to be int values */
   124     src.x = srcx;
   118 	src.x = srcx;
   125     src.y = srcy;
   119 	src.y = srcy;
   126     src.w = srcw;
   120 	src.w = srcw;
   127     src.h = srch;
   121 	src.h = srch;
   128     dst.x = dstx;
   122 	dst.x = dstx;
   129     dst.y = dsty;
   123 	dst.y = dsty;
   130     dst.w = dstw;
   124 	dst.w = dstw;
   131     dst.h = dsth;
   125 	dst.h = dsth;
   132     return overlay->hwfuncs->Display (_this, overlay, &src, &dst);
   126 	return overlay->hwfuncs->Display(current_video, overlay, &src, &dst);
       
   127 }
   133 }
   128 
   134 
   129 void SDL_FreeYUVOverlay(SDL_Overlay *overlay)
   135 void
       
   136 SDL_FreeYUVOverlay (SDL_Overlay * overlay)
   130 {
   137 {
   131 	if ( overlay ) {
   138     SDL_VideoDevice *_this = SDL_GetVideoDevice ();
   132 		if ( overlay->hwfuncs ) {
   139     if (overlay) {
   133 			overlay->hwfuncs->FreeHW(current_video, overlay);
   140         if (overlay->hwfuncs) {
   134 		}
   141             overlay->hwfuncs->FreeHW (_this, overlay);
   135 		SDL_free(overlay);
   142         }
   136 	}
   143         SDL_free (overlay);
       
   144     }
   137 }
   145 }
       
   146 
       
   147 /* vi: set ts=4 sw=4 expandtab: */