Commit e4559516 authored by Sam Lantinga's avatar Sam Lantinga

Reduce duplicated code in the texture update code paths

parent 7133afac
...@@ -106,6 +106,7 @@ typedef struct ...@@ -106,6 +106,7 @@ typedef struct
GLenum formattype; GLenum formattype;
void *pixels; void *pixels;
int pitch; int pitch;
SDL_Rect locked_rect;
} GL_TextureData; } GL_TextureData;
...@@ -448,15 +449,6 @@ GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture) ...@@ -448,15 +449,6 @@ GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
return 0; return 0;
} }
static void
SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
int pitch)
{
renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
(pitch / SDL_BYTESPERPIXEL(texture->format)));
}
static int static int
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch) const SDL_Rect * rect, const void *pixels, int pitch)
...@@ -468,7 +460,9 @@ GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, ...@@ -468,7 +460,9 @@ GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
GL_ActivateRenderer(renderer); GL_ActivateRenderer(renderer);
renderdata->glGetError(); renderdata->glGetError();
SetupTextureUpdate(renderdata, texture, pitch); renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
(pitch / SDL_BYTESPERPIXEL(texture->format)));
renderdata->glEnable(data->type); renderdata->glEnable(data->type);
renderdata->glBindTexture(data->type, data->texture); renderdata->glBindTexture(data->type, data->texture);
renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w, renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
...@@ -489,7 +483,8 @@ GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, ...@@ -489,7 +483,8 @@ GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
{ {
GL_TextureData *data = (GL_TextureData *) texture->driverdata; GL_TextureData *data = (GL_TextureData *) texture->driverdata;
*pixels = data->locked_rect = *rect;
*pixels =
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch + (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format)); rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch = data->pitch; *pitch = data->pitch;
...@@ -499,17 +494,15 @@ GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, ...@@ -499,17 +494,15 @@ GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
static void static void
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture) GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{ {
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
GL_TextureData *data = (GL_TextureData *) texture->driverdata; GL_TextureData *data = (GL_TextureData *) texture->driverdata;
const SDL_Rect *rect;
void *pixels;
GL_ActivateRenderer(renderer); rect = &data->locked_rect;
pixels =
SetupTextureUpdate(renderdata, texture, data->pitch); (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
renderdata->glEnable(data->type); rect->x * SDL_BYTESPERPIXEL(texture->format));
renderdata->glBindTexture(data->type, data->texture); GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
renderdata->glTexSubImage2D(data->type, 0, 0, 0, texture->w, texture->h,
data->format, data->formattype, data->pixels);
renderdata->glDisable(data->type);
} }
static void static void
......
...@@ -293,7 +293,6 @@ power_of_2(int input) ...@@ -293,7 +293,6 @@ power_of_2(int input)
static int static int
GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture) GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{ {
GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
GLES_TextureData *data; GLES_TextureData *data;
GLint internalFormat; GLint internalFormat;
GLenum format, type; GLenum format, type;
...@@ -370,46 +369,60 @@ static int ...@@ -370,46 +369,60 @@ static int
GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch) const SDL_Rect * rect, const void *pixels, int pitch)
{ {
GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
GLenum result; Uint8 *blob = NULL;
int bpp = SDL_BYTESPERPIXEL(texture->format); Uint8 *src;
void * temp_buffer; int srcPitch;
void * temp_ptr; int y;
int i;
GLES_ActivateRenderer(renderer); GLES_ActivateRenderer(renderer);
glGetError(); /* Bail out if we're supposed to update an empty rectangle */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); if (rect->w <= 0 || rect->h <= 0)
glEnable(data->type); return 0;
glBindTexture(data->type, data->texture);
if( rect->w * bpp == pitch ) { /* Reformat the texture data into a tightly packed array */
temp_buffer = (void *)pixels; /* No need to reformat */ srcPitch = rect->w * SDL_BYTESPERPIXEL(texture->format);
} else { src = (Uint8 *)pixels;
/* Reformatting of mem area required */ if (pitch != srcPitch)
temp_buffer = SDL_malloc(rect->w * rect->h * bpp); {
temp_ptr = temp_buffer; blob = (Uint8 *)SDL_malloc(srcPitch * rect->h);
for (i = 0; i < rect->h; i++) { if (!blob)
SDL_memcpy(temp_ptr, pixels, rect->w * bpp); {
temp_ptr += rect->w * bpp; SDL_OutOfMemory();
pixels += pitch; return -1;
} }
} src = blob;
for (y = 0; y < rect->h; ++y)
glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w, {
rect->h, data->format, data->formattype, SDL_memcpy(src, pixels, srcPitch);
temp_buffer); src += srcPitch;
pixels = (Uint8 *)pixels + pitch;
if( temp_buffer != pixels ) { }
SDL_free(temp_buffer); src = blob;
} }
glDisable(data->type); /* Create a texture subimage with the supplied data */
result = glGetError(); glGetError();
if (result != GL_NO_ERROR) { glEnable(data->type);
GLES_SetError("glTexSubImage2D()", result); glBindTexture(data->type, data->texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage2D(data->type,
0,
rect->x,
rect->y,
rect->w,
rect->h,
data->format,
data->formattype,
src);
if (blob) {
SDL_free(blob);
}
if (glGetError() != GL_NO_ERROR)
{
SDL_SetError("Failed to update texture");
return -1; return -1;
} }
return 0; return 0;
...@@ -431,24 +444,21 @@ GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, ...@@ -431,24 +444,21 @@ GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
static void static void
GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture) GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{ {
GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
SDL_Rect rect;
GLES_ActivateRenderer(renderer);
/* We do whole texture updates, at least for now */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); rect.x = 0;
glEnable(data->type); rect.y = 0;
glBindTexture(data->type, data->texture); rect.w = texture->w;
glTexSubImage2D(data->type, 0, 0, 0, texture->w, rect.h = texture->h;
texture->h, data->format, data->formattype, GLES_UpdateTexture(renderer, texture, &rect, data->pixels, data->pitch);
data->pixels);
glDisable(data->type);
} }
static void static void
GLES_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect) GLES_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
{ {
GL_ActivateRenderer(renderer); GLES_ActivateRenderer(renderer);
if (rect) { if (rect) {
int w, h; int w, h;
......
...@@ -343,14 +343,14 @@ static void ...@@ -343,14 +343,14 @@ static void
GLES2_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture) GLES2_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
{ {
GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata; GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
SDL_Rect rect;
GLES2_ActivateRenderer(renderer);
/* We do whole texture updates, at least for now */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); rect.x = 0;
glActiveTexture(GL_TEXTURE0); rect.y = 0;
glBindTexture(tdata->texture_type, tdata->texture); rect.w = texture->w;
glTexSubImage2D(tdata->texture_type, 0, 0, 0, texture->w, texture->h, rect.h = texture->h;
tdata->pixel_format, tdata->pixel_type, tdata->pixel_data); GLES2_UpdateTexture(renderer, texture, &rect, tdata->pixel_data, tdata->pitch);
} }
static int static int
...@@ -361,7 +361,6 @@ GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect ...@@ -361,7 +361,6 @@ GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect
Uint8 *blob = NULL; Uint8 *blob = NULL;
Uint8 *src; Uint8 *src;
int srcPitch; int srcPitch;
Uint8 *dest;
int y; int y;
GLES2_ActivateRenderer(renderer); GLES2_ActivateRenderer(renderer);
...@@ -405,7 +404,9 @@ GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect ...@@ -405,7 +404,9 @@ GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect
tdata->pixel_format, tdata->pixel_format,
tdata->pixel_type, tdata->pixel_type,
src); src);
SDL_free(blob); if (blob) {
SDL_free(blob);
}
if (glGetError() != GL_NO_ERROR) if (glGetError() != GL_NO_ERROR)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment