Skip to content

Commit

Permalink
More general fix for bug #189
Browse files Browse the repository at this point in the history
The clipping is done at a higher level, and the low level functions are
passed clipped rectangles.  Drivers which don't support source clipping
have not been changed, so the image will be squished instead of clipped,
but at least they will no longer crash when the destination rect was out
of bounds.
  • Loading branch information
slouken committed Apr 17, 2006
1 parent e088fc5 commit c451350
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 129 deletions.
50 changes: 49 additions & 1 deletion src/video/SDL_yuv.c
Expand Up @@ -75,7 +75,55 @@ void SDL_UnlockYUVOverlay(SDL_Overlay *overlay)

int SDL_DisplayYUVOverlay(SDL_Overlay *overlay, SDL_Rect *dstrect)
{
return overlay->hwfuncs->Display(current_video, overlay, dstrect);
SDL_Rect src, dst;
int srcx, srcy, srcw, srch;
int dstx, dsty, dstw, dsth;

/* Clip the rectangle to the screen area */
srcx = 0;
srcy = 0;
srcw = overlay->w;
srch = overlay->h;
dstx = dstrect->x;
dsty = dstrect->y;
dstw = dstrect->w;
dsth = dstrect->h;
if ( dstx < 0 ) {
srcw += (dstx * overlay->w) / dstrect->w;
dstw += dstx;
srcx -= (dstx * overlay->w) / dstrect->w;
dstx = 0;
}
if ( (dstx+dstw) > current_video->screen->w ) {
int extra = (dstx+dstw - current_video->screen->w);
srcw -= (extra * overlay->w) / dstrect->w;
dstw -= extra;
}
if ( dsty < 0 ) {
srch += (dsty * overlay->h) / dstrect->h;
dsth += dsty;
srcy -= (dsty * overlay->h) / dstrect->h;
dsty = 0;
}
if ( (dsty+dsth) > current_video->screen->h ) {
int extra = (dsty+dsth - current_video->screen->h);
srch -= (extra * overlay->h) / dstrect->h;
dsth -= extra;
}
if ( srcw <= 0 || srch <= 0 ||
srch <= 0 || dsth <= 0 ) {
return 0;
}
/* Ugh, I can't wait for SDL_Rect to be int values */
src.x = srcx;
src.y = srcy;
src.w = srcw;
src.h = srch;
dst.x = dstx;
dst.y = dsty;
dst.w = dstw;
dst.h = dsth;
return overlay->hwfuncs->Display(current_video, overlay, &src, &dst);
}

void SDL_FreeYUVOverlay(SDL_Overlay *overlay)
Expand Down
65 changes: 33 additions & 32 deletions src/video/SDL_yuv_sw.c
Expand Up @@ -1164,43 +1164,44 @@ void SDL_UnlockYUV_SW(_THIS, SDL_Overlay *overlay)
return;
}

int SDL_DisplayYUV_SW(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect)
int SDL_DisplayYUV_SW(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst)
{
struct private_yuvhwdata *swdata;
SDL_Surface *stretch;
SDL_Surface *display;
int stretch;
int scale_2x;
SDL_Surface *display;
Uint8 *lum, *Cr, *Cb;
Uint8 *dst;
Uint8 *dstp;
int mod;

swdata = overlay->hwdata;
scale_2x = 0;
stretch = 0;
if ( (overlay->w != dstrect->w) || (overlay->h != dstrect->h) ) {
if ( (dstrect->w == 2*overlay->w) &&
(dstrect->h == 2*overlay->h) ) {
scale_2x = 0;
if ( src->x || src->y || src->w < overlay->w || src->h < overlay->h ) {
stretch = 1;
} else if ( (src->w != dst->w) || (src->h != dst->h) ) {
if ( (dst->w == 2*src->w) &&
(dst->h == 2*src->h) ) {
scale_2x = 1;
} else {
if ( ! swdata->stretch ) {
display = swdata->display;
swdata->stretch = SDL_CreateRGBSurface(
SDL_SWSURFACE,
overlay->w, overlay->h,
display->format->BitsPerPixel,
display->format->Rmask,
display->format->Gmask,
display->format->Bmask, 0);
if ( ! swdata->stretch ) {
return(-1);
}
}
stretch = swdata->stretch;
stretch = 1;
}
}

if ( stretch ) {
display = stretch;
if ( ! swdata->stretch ) {
display = swdata->display;
swdata->stretch = SDL_CreateRGBSurface(
SDL_SWSURFACE,
overlay->w, overlay->h,
display->format->BitsPerPixel,
display->format->Rmask,
display->format->Gmask,
display->format->Bmask, 0);
if ( ! swdata->stretch ) {
return(-1);
}
}
display = swdata->stretch;
} else {
display = swdata->display;
}
Expand Down Expand Up @@ -1240,31 +1241,31 @@ int SDL_DisplayYUV_SW(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect)
}
}
if ( stretch ) {
dst = (Uint8 *)stretch->pixels;
dstp = (Uint8 *)swdata->stretch->pixels;
} else {
dst = (Uint8 *)display->pixels
+ dstrect->x * display->format->BytesPerPixel
+ dstrect->y * display->pitch;
dstp = (Uint8 *)display->pixels
+ dst->x * display->format->BytesPerPixel
+ dst->y * display->pitch;
}
mod = (display->pitch / display->format->BytesPerPixel);

if ( scale_2x ) {
mod -= (overlay->w * 2);
swdata->Display2X(swdata->colortab, swdata->rgb_2_pix,
lum, Cr, Cb, dst, overlay->h, overlay->w,mod);
lum, Cr, Cb, dstp, overlay->h, overlay->w, mod);
} else {
mod -= overlay->w;
swdata->Display1X(swdata->colortab, swdata->rgb_2_pix,
lum, Cr, Cb, dst, overlay->h, overlay->w,mod);
lum, Cr, Cb, dstp, overlay->h, overlay->w, mod);
}
if ( SDL_MUSTLOCK(display) ) {
SDL_UnlockSurface(display);
}
if ( stretch ) {
display = swdata->display;
SDL_SoftStretch(stretch, NULL, display, dstrect);
SDL_SoftStretch(swdata->stretch, src, display, dst);
}
SDL_UpdateRects(display, 1, dstrect);
SDL_UpdateRects(display, 1, dst);

return(0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/video/SDL_yuv_sw_c.h
Expand Up @@ -32,6 +32,6 @@ extern int SDL_LockYUV_SW(_THIS, SDL_Overlay *overlay);

extern void SDL_UnlockYUV_SW(_THIS, SDL_Overlay *overlay);

extern int SDL_DisplayYUV_SW(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect);
extern int SDL_DisplayYUV_SW(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst);

extern void SDL_FreeYUV_SW(_THIS, SDL_Overlay *overlay);
2 changes: 1 addition & 1 deletion src/video/SDL_yuvfuncs.h
Expand Up @@ -32,6 +32,6 @@
struct private_yuvhwfuncs {
int (*Lock)(_THIS, SDL_Overlay *overlay);
void (*Unlock)(_THIS, SDL_Overlay *overlay);
int (*Display)(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect);
int (*Display)(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst);
void (*FreeHW)(_THIS, SDL_Overlay *overlay);
};
8 changes: 4 additions & 4 deletions src/video/bwindow/SDL_sysyuv.cc
Expand Up @@ -263,7 +263,7 @@ void BE_UnlockYUVOverlay(_THIS, SDL_Overlay* overlay)
overlay->hwdata->locked = 0;
}

int BE_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* dstrect)
int BE_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* src, SDL_Rect *dst)
{
if ((overlay == NULL) || (overlay->hwdata==NULL)
|| (overlay->hwdata->bview==NULL) || (SDL_Win->View() == NULL))
Expand All @@ -277,11 +277,11 @@ int BE_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* dstrect)
if (SDL_Win->IsFullScreen()) {
int left,top;
SDL_Win->GetXYOffset(left,top);
bview->MoveTo(left+dstrect->x,top+dstrect->y);
bview->MoveTo(left+dst->x,top+dst->y);
} else {
bview->MoveTo(dstrect->x,dstrect->y);
bview->MoveTo(dst->x,dst->y);
}
bview->ResizeTo(dstrect->w,dstrect->h);
bview->ResizeTo(dst->w,dst->h);
bview->Flush();
if (overlay->hwdata->first_display) {
bview->Show();
Expand Down
2 changes: 1 addition & 1 deletion src/video/bwindow/SDL_sysyuv.h
Expand Up @@ -65,7 +65,7 @@ extern BBitmap * BE_GetOverlayBitmap(BRect bounds, color_space cs);
extern SDL_Overlay* BE_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface* display);
extern int BE_LockYUVOverlay(_THIS, SDL_Overlay* overlay);
extern void BE_UnlockYUVOverlay(_THIS, SDL_Overlay* overlay);
extern int BE_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* dstrect);
extern int BE_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* src, SDL_Rect* dst);
extern void BE_FreeYUVOverlay(_THIS, SDL_Overlay* overlay);

};
Expand Down
2 changes: 1 addition & 1 deletion src/video/directfb/SDL_DirectFB_yuv.c
Expand Up @@ -241,7 +241,7 @@ void DirectFB_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay)
surface->Unlock (surface);
}

int DirectFB_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *dst)
int DirectFB_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst)
{
DFBResult ret;
DFBDisplayLayerConfig conf;
Expand Down
2 changes: 1 addition & 1 deletion src/video/directfb/SDL_DirectFB_yuv.h
Expand Up @@ -32,7 +32,7 @@ extern int DirectFB_LockYUVOverlay(_THIS, SDL_Overlay *overlay);

extern void DirectFB_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay);

extern int DirectFB_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect);
extern int DirectFB_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst);

extern void DirectFB_FreeYUVOverlay(_THIS, SDL_Overlay *overlay);

32 changes: 20 additions & 12 deletions src/video/photon/SDL_ph_events.c
Expand Up @@ -245,15 +245,19 @@ static int ph_DispatchEvent(_THIS)
int lockedstate=current_overlay->hwdata->locked;
int chromastate=current_overlay->hwdata->ischromakey;
int error;
SDL_Rect target;
SDL_Rect src, dst;

current_overlay->hwdata->locked=1;
target.x=current_overlay->hwdata->CurrentViewPort.pos.x;
target.y=current_overlay->hwdata->CurrentViewPort.pos.y;
target.w=current_overlay->hwdata->CurrentViewPort.size.w;
target.h=current_overlay->hwdata->CurrentViewPort.size.h;
src.x = 0;
src.y = 0;
src.w = current_overlay->w;
src.y = current_overlay->h;
dst.x=current_overlay->hwdata->CurrentViewPort.pos.x;
dst.y=current_overlay->hwdata->CurrentViewPort.pos.y;
dst.w=current_overlay->hwdata->CurrentViewPort.size.w;
dst.h=current_overlay->hwdata->CurrentViewPort.size.h;
current_overlay->hwdata->ischromakey=0;
error=ph_DisplayYUVOverlay(this, current_overlay, &target);
error=ph_DisplayYUVOverlay(this, current_overlay, &src, &dst);
if (!error)
{
current_overlay->hwdata->ischromakey=chromastate;
Expand Down Expand Up @@ -306,15 +310,19 @@ static int ph_DispatchEvent(_THIS)
{
int lockedstate=current_overlay->hwdata->locked;
int error;
SDL_Rect target;
SDL_Rect src, dst;

current_overlay->hwdata->locked=1;
target.x=current_overlay->hwdata->CurrentViewPort.pos.x;
target.y=current_overlay->hwdata->CurrentViewPort.pos.y;
target.w=current_overlay->hwdata->CurrentViewPort.size.w;
target.h=current_overlay->hwdata->CurrentViewPort.size.h;
src.x = 0;
src.y = 0;
src.w = current_overlay->w;
src.y = current_overlay->h;
dst.x=current_overlay->hwdata->CurrentViewPort.pos.x;
dst.y=current_overlay->hwdata->CurrentViewPort.pos.y;
dst.w=current_overlay->hwdata->CurrentViewPort.size.w;
dst.h=current_overlay->hwdata->CurrentViewPort.size.h;
current_overlay->hwdata->forcedredraw=1;
error=ph_DisplayYUVOverlay(this, current_overlay, &target);
error=ph_DisplayYUVOverlay(this, current_overlay, &src, &dst);
if (!error)
{
current_overlay->hwdata->forcedredraw=0;
Expand Down
24 changes: 12 additions & 12 deletions src/video/photon/SDL_phyuv.c
Expand Up @@ -334,7 +334,7 @@ void ph_UnlockYUVOverlay(_THIS, SDL_Overlay* overlay)
overlay->hwdata->locked = 0;
}

int ph_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* dstrect)
int ph_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* src, SDL_Rect* dst)
{
int rtncode;
PhPoint_t pos;
Expand Down Expand Up @@ -362,10 +362,10 @@ int ph_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* dstrect)
}

/* If CurrentViewPort position/size has been changed, then move/resize the viewport */
if ((overlay->hwdata->CurrentViewPort.pos.x != dstrect->x) ||
(overlay->hwdata->CurrentViewPort.pos.y != dstrect->y) ||
(overlay->hwdata->CurrentViewPort.size.w != dstrect->w) ||
(overlay->hwdata->CurrentViewPort.size.h != dstrect->h) ||
if ((overlay->hwdata->CurrentViewPort.pos.x != dst->x) ||
(overlay->hwdata->CurrentViewPort.pos.y != dst->y) ||
(overlay->hwdata->CurrentViewPort.size.w != dst->w) ||
(overlay->hwdata->CurrentViewPort.size.h != dst->h) ||
(overlay->hwdata->scaler_on==0) || (winchanged==1) ||
(overlay->hwdata->forcedredraw==1))
{
Expand All @@ -381,21 +381,21 @@ int ph_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* dstrect)

/* Draw the new rectangle of the chroma color at the viewport position */
PgSetFillColor(overlay->hwdata->chromakey);
PgDrawIRect(dstrect->x, dstrect->y, dstrect->x+dstrect->w-1, dstrect->y+dstrect->h-1, Pg_DRAW_FILL);
PgDrawIRect(dst->x, dst->y, dst->x+dst->w-1, dst->y+dst->h-1, Pg_DRAW_FILL);
PgFlush();
}

overlay->hwdata->props.flags |= Pg_SCALER_PROP_SCALER_ENABLE;
overlay->hwdata->scaler_on = 1;

PhWindowQueryVisible(Ph_QUERY_CONSOLE, 0, PtWidgetRid(window), &windowextent);
overlay->hwdata->CurrentViewPort.pos.x = pos.x-windowextent.ul.x+dstrect->x;
overlay->hwdata->CurrentViewPort.pos.y = pos.y-windowextent.ul.y+dstrect->y;
overlay->hwdata->CurrentViewPort.size.w = dstrect->w;
overlay->hwdata->CurrentViewPort.size.h = dstrect->h;
overlay->hwdata->CurrentViewPort.pos.x = pos.x-windowextent.ul.x+dst->x;
overlay->hwdata->CurrentViewPort.pos.y = pos.y-windowextent.ul.y+dst->y;
overlay->hwdata->CurrentViewPort.size.w = dst->w;
overlay->hwdata->CurrentViewPort.size.h = dst->h;
PhAreaToRect(&overlay->hwdata->CurrentViewPort, &overlay->hwdata->props.viewport);
overlay->hwdata->CurrentViewPort.pos.x = dstrect->x;
overlay->hwdata->CurrentViewPort.pos.y = dstrect->y;
overlay->hwdata->CurrentViewPort.pos.x = dst->x;
overlay->hwdata->CurrentViewPort.pos.y = dst->y;

rtncode = PgConfigScalerChannel(overlay->hwdata->channel, &(overlay->hwdata->props));

Expand Down
2 changes: 1 addition & 1 deletion src/video/photon/SDL_phyuv_c.h
Expand Up @@ -56,7 +56,7 @@ struct private_yuvhwdata
extern SDL_Overlay* ph_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface* display);
extern int ph_LockYUVOverlay(_THIS, SDL_Overlay* overlay);
extern void ph_UnlockYUVOverlay(_THIS, SDL_Overlay* overlay);
extern int ph_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* dstrect);
extern int ph_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* src, SDL_Rect* dst);
extern void ph_FreeYUVOverlay(_THIS, SDL_Overlay* overlay);

#endif /* __SDL_PH_YUV_H__ */
10 changes: 5 additions & 5 deletions src/video/ps2gs/SDL_gsyuv.c
Expand Up @@ -315,7 +315,7 @@ void GS_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay)
return;
}

int GS_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect)
int GS_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst)
{
struct private_yuvhwdata *hwdata;
__u32 cmd;
Expand Down Expand Up @@ -423,17 +423,17 @@ int GS_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect)

/* Send the current image to the screen and scale it */
screen = this->screen;
x = (unsigned int)dstrect->x;
y = (unsigned int)dstrect->y;
x = (unsigned int)dst->x;
y = (unsigned int)dst->y;
if ( screen->offset ) {
x += (screen->offset % screen->pitch) /
screen->format->BytesPerPixel;
y += (screen->offset / screen->pitch);
}
y += screen_image.y;
*hwdata->stretch_x1y1 = (x * 16) + ((y * 16) << 16);
x += (unsigned int)dstrect->w;
y += (unsigned int)dstrect->h;
x += (unsigned int)dst->w;
y += (unsigned int)dst->h;
*hwdata->stretch_x2y2 = (x * 16) + ((y * 16) << 16);
return ioctl(console_fd, PS2IOC_SENDL, &hwdata->plist);
}
Expand Down
2 changes: 1 addition & 1 deletion src/video/ps2gs/SDL_gsyuv_c.h
Expand Up @@ -32,6 +32,6 @@ extern int GS_LockYUVOverlay(_THIS, SDL_Overlay *overlay);

extern void GS_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay);

extern int GS_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect);
extern int GS_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst);

extern void GS_FreeYUVOverlay(_THIS, SDL_Overlay *overlay);

0 comments on commit c451350

Please sign in to comment.