Added optimized YUV texture upload path with SDL_UpdateYUVTexture()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
diff --git a/include/SDL_render.h b/include/SDL_render.h
index 1e24619..a765dc7 100644
--- a/include/SDL_render.h
+++ b/include/SDL_render.h
@@ -382,6 +382,31 @@ extern DECLSPEC int SDLCALL SDL_UpdateTexture(SDL_Texture * texture,
const void *pixels, int pitch);
/**
+ * \brief Update a rectangle within a planar YV12 or IYUV texture with new pixel data.
+ *
+ * \param texture The texture to update
+ * \param rect A pointer to the rectangle of pixels to update, or NULL to
+ * update the entire texture.
+ * \param Yplane The raw pixel data for the Y plane.
+ * \param Ypitch The number of bytes between rows of pixel data for the Y plane.
+ * \param Uplane The raw pixel data for the U plane.
+ * \param Upitch The number of bytes between rows of pixel data for the U plane.
+ * \param Vplane The raw pixel data for the V plane.
+ * \param Vpitch The number of bytes between rows of pixel data for the V plane.
+ *
+ * \return 0 on success, or -1 if the texture is not valid.
+ *
+ * \note You can use SDL_UpdateTexture() as long as your pixel data is
+ * a contiguous block of Y and U/V planes in the proper order, but
+ * this function is available if your pixel data is not contiguous.
+ */
+extern DECLSPEC int SDLCALL SDL_UpdateYUVTexture(SDL_Texture * texture,
+ const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *Uplane, int Upitch,
+ const Uint8 *Vplane, int Vpitch);
+
+/**
* \brief Lock a portion of the texture for write-only pixel access.
*
* \param texture The texture to lock for access, which was created with
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index ef690cf..680b324 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -805,6 +805,106 @@ SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
}
static int
+SDL_UpdateTextureYUVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *Uplane, int Upitch,
+ const Uint8 *Vplane, int Vpitch)
+{
+ SDL_Texture *native = texture->native;
+ SDL_Rect full_rect;
+
+ if (SDL_SW_UpdateYUVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch) < 0) {
+ return -1;
+ }
+
+ full_rect.x = 0;
+ full_rect.y = 0;
+ full_rect.w = texture->w;
+ full_rect.h = texture->h;
+ rect = &full_rect;
+
+ if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
+ /* We can lock the texture and copy to it */
+ void *native_pixels;
+ int native_pitch;
+
+ if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
+ return -1;
+ }
+ SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
+ rect->w, rect->h, native_pixels, native_pitch);
+ SDL_UnlockTexture(native);
+ } else {
+ /* Use a temporary buffer for updating */
+ void *temp_pixels;
+ int temp_pitch;
+
+ temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
+ temp_pixels = SDL_malloc(rect->h * temp_pitch);
+ if (!temp_pixels) {
+ return SDL_OutOfMemory();
+ }
+ SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
+ rect->w, rect->h, temp_pixels, temp_pitch);
+ SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
+ SDL_free(temp_pixels);
+ }
+ return 0;
+}
+
+int SDL_UpdateYUVTexture(SDL_Texture * texture, const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *Uplane, int Upitch,
+ const Uint8 *Vplane, int Vpitch)
+{
+ SDL_Renderer *renderer;
+ SDL_Rect full_rect;
+
+ CHECK_TEXTURE_MAGIC(texture, -1);
+
+ if (!Yplane) {
+ return SDL_InvalidParamError("Yplane");
+ }
+ if (!Ypitch) {
+ return SDL_InvalidParamError("Ypitch");
+ }
+ if (!Uplane) {
+ return SDL_InvalidParamError("Uplane");
+ }
+ if (!Upitch) {
+ return SDL_InvalidParamError("Upitch");
+ }
+ if (!Vplane) {
+ return SDL_InvalidParamError("Vplane");
+ }
+ if (!Vpitch) {
+ return SDL_InvalidParamError("Vpitch");
+ }
+
+ if (texture->format != SDL_PIXELFORMAT_YV12 &&
+ texture->format != SDL_PIXELFORMAT_IYUV) {
+ return SDL_SetError("Texture format must by YV12 or IYUV");
+ }
+
+ if (!rect) {
+ full_rect.x = 0;
+ full_rect.y = 0;
+ full_rect.w = texture->w;
+ full_rect.h = texture->h;
+ rect = &full_rect;
+ }
+
+ if (texture->yuv) {
+ return SDL_UpdateTextureYUVPlanar(texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
+ } else {
+ SDL_assert(!texture->native);
+ renderer = texture->renderer;
+ SDL_assert(renderer->UpdateTextureYUV);
+ return renderer->UpdateTextureYUV(renderer, texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
+ }
+}
+
+static int
SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
void **pixels, int *pitch)
{
diff --git a/src/render/SDL_sysrender.h b/src/render/SDL_sysrender.h
index f06ee1d..b3d6868 100644
--- a/src/render/SDL_sysrender.h
+++ b/src/render/SDL_sysrender.h
@@ -89,6 +89,11 @@ struct SDL_Renderer
int (*UpdateTexture) (SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels,
int pitch);
+ int (*UpdateTextureYUV) (SDL_Renderer * renderer, SDL_Texture * texture,
+ const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *Uplane, int Upitch,
+ const Uint8 *Vplane, int Vpitch);
int (*LockTexture) (SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch);
void (*UnlockTexture) (SDL_Renderer * renderer, SDL_Texture * texture);
diff --git a/src/render/SDL_yuv_sw.c b/src/render/SDL_yuv_sw.c
index 2c7e724..a978d4b 100644
--- a/src/render/SDL_yuv_sw.c
+++ b/src/render/SDL_yuv_sw.c
@@ -1185,6 +1185,60 @@ SDL_SW_UpdateYUVTexture(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
}
int
+SDL_SW_UpdateYUVTexturePlanar(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *Uplane, int Upitch,
+ const Uint8 *Vplane, int Vpitch)
+{
+ Uint8 *src, *dst;
+ int row;
+ size_t length;
+
+ /* Copy the Y plane */
+ src = Yplane;
+ dst = swdata->pixels + rect->y * swdata->w + rect->x;
+ length = rect->w;
+ for (row = 0; row < rect->h; ++row) {
+ SDL_memcpy(dst, src, length);
+ src += Ypitch;
+ dst += swdata->w;
+ }
+
+ /* Copy the U plane */
+ src = Uplane;
+ if (swdata->format == SDL_PIXELFORMAT_IYUV) {
+ dst = swdata->pixels + swdata->h * swdata->w;
+ } else {
+ dst = swdata->pixels + swdata->h * swdata->w +
+ (swdata->h * swdata->w) / 4;
+ }
+ dst += rect->y/2 * swdata->w/2 + rect->x/2;
+ length = rect->w / 2;
+ for (row = 0; row < rect->h/2; ++row) {
+ SDL_memcpy(dst, src, length);
+ src += Upitch;
+ dst += swdata->w/2;
+ }
+
+ /* Copy the V plane */
+ src = Vplane;
+ if (swdata->format == SDL_PIXELFORMAT_YV12) {
+ dst = swdata->pixels + swdata->h * swdata->w;
+ } else {
+ dst = swdata->pixels + swdata->h * swdata->w +
+ (swdata->h * swdata->w) / 4;
+ }
+ dst += rect->y/2 * swdata->w/2 + rect->x/2;
+ length = rect->w / 2;
+ for (row = 0; row < rect->h/2; ++row) {
+ SDL_memcpy(dst, src, length);
+ src += Vpitch;
+ dst += swdata->w/2;
+ }
+ return 0;
+}
+
+int
SDL_SW_LockYUVTexture(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
void **pixels, int *pitch)
{
diff --git a/src/render/SDL_yuv_sw_c.h b/src/render/SDL_yuv_sw_c.h
index 9debacb..f128041 100644
--- a/src/render/SDL_yuv_sw_c.h
+++ b/src/render/SDL_yuv_sw_c.h
@@ -57,6 +57,10 @@ int SDL_SW_QueryYUVTexturePixels(SDL_SW_YUVTexture * swdata, void **pixels,
int *pitch);
int SDL_SW_UpdateYUVTexture(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
const void *pixels, int pitch);
+int SDL_SW_UpdateYUVTexturePlanar(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *Uplane, int Upitch,
+ const Uint8 *Vplane, int Vpitch);
int SDL_SW_LockYUVTexture(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
void **pixels, int *pitch);
void SDL_SW_UnlockYUVTexture(SDL_SW_YUVTexture * swdata);
diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c
index cd0a708..824ae8c 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/src/render/opengl/SDL_render_gl.c
@@ -52,6 +52,11 @@ static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels,
int pitch);
+static int GL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
+ const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *Uplane, int Upitch,
+ const Uint8 *Vplane, int Vpitch);
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch);
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
@@ -403,6 +408,7 @@ GL_CreateRenderer(SDL_Window * window, Uint32 flags)
renderer->GetOutputSize = GL_GetOutputSize;
renderer->CreateTexture = GL_CreateTexture;
renderer->UpdateTexture = GL_UpdateTexture;
+ renderer->UpdateTextureYUV = GL_UpdateTextureYUV;
renderer->LockTexture = GL_LockTexture;
renderer->UnlockTexture = GL_UnlockTexture;
renderer->SetRenderTarget = GL_SetRenderTarget;
@@ -802,6 +808,41 @@ GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
}
static int
+GL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
+ const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *Uplane, int Upitch,
+ const Uint8 *Vplane, int Vpitch)
+{
+ GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
+ GL_TextureData *data = (GL_TextureData *) texture->driverdata;
+
+ GL_ActivateRenderer(renderer);
+
+ renderdata->glEnable(data->type);
+ renderdata->glBindTexture(data->type, data->texture);
+ renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Ypitch);
+ renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
+ rect->h, data->format, data->formattype,
+ Yplane);
+
+ renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Upitch);
+ renderdata->glBindTexture(data->type, data->utexture);
+ renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
+ rect->w/2, rect->h/2,
+ data->format, data->formattype, Uplane);
+
+ renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Vpitch);
+ renderdata->glBindTexture(data->type, data->vtexture);
+ renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
+ rect->w/2, rect->h/2,
+ data->format, data->formattype, Vplane);
+ renderdata->glDisable(data->type);
+ return GL_CheckError("glTexSubImage2D()", renderer);
+}
+
+static int
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch)
{