Add SDL_UpdateNVTexture() to update NV12/21 Texture (bug #5430) for renderer software, opengl, and opengles2
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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
diff --git a/include/SDL_render.h b/include/SDL_render.h
index fc669d3..980109a 100644
--- a/include/SDL_render.h
+++ b/include/SDL_render.h
@@ -451,6 +451,28 @@ extern DECLSPEC int SDLCALL SDL_UpdateYUVTexture(SDL_Texture * texture,
const Uint8 *Vplane, int Vpitch);
/**
+ * \brief Update a rectangle within a planar NV12 or NV21 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 UVplane The raw pixel data for the UV plane.
+ * \param UVpitch The number of bytes between rows of pixel data for the UV 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 NV12/21 planes in the proper order, but
+ * this function is available if your pixel data is not contiguous.
+ */
+extern DECLSPEC int SDLCALL SDL_UpdateNVTexture(SDL_Texture * texture,
+ const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *UVplane, int UVpitch);
+
+/**
* \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 57632ea..4102b7a 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -1628,6 +1628,59 @@ SDL_UpdateTextureYUVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
}
return 0;
}
+
+static int
+SDL_UpdateTextureNVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *UVplane, int UVpitch)
+{
+ SDL_Texture *native = texture->native;
+ SDL_Rect full_rect;
+
+ if (SDL_SW_UpdateNVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, UVplane, UVpitch) < 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 (!rect->w || !rect->h) {
+ return 0; /* nothing to do. */
+ }
+
+ if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
+ /* We can lock the texture and copy to it */
+ void *native_pixels = NULL;
+ int native_pitch = 0;
+
+ 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 */
+ const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
+ const size_t alloclen = rect->h * temp_pitch;
+ if (alloclen > 0) {
+ void *temp_pixels = SDL_malloc(alloclen);
+ 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;
+}
+
+
#endif /* SDL_HAVE_YUV */
int SDL_UpdateYUVTexture(SDL_Texture * texture, const SDL_Rect * rect,
@@ -1697,6 +1750,68 @@ int SDL_UpdateYUVTexture(SDL_Texture * texture, const SDL_Rect * rect,
#endif
}
+int SDL_UpdateNVTexture(SDL_Texture * texture, const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *UVplane, int UVpitch)
+{
+#if SDL_HAVE_YUV
+ 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 (!UVplane) {
+ return SDL_InvalidParamError("UVplane");
+ }
+ if (!UVpitch) {
+ return SDL_InvalidParamError("UVpitch");
+ }
+
+ if (texture->format != SDL_PIXELFORMAT_NV12 &&
+ texture->format != SDL_PIXELFORMAT_NV21) {
+ return SDL_SetError("Texture format must by NV12 or NV21");
+ }
+
+ if (!rect) {
+ full_rect.x = 0;
+ full_rect.y = 0;
+ full_rect.w = texture->w;
+ full_rect.h = texture->h;
+ rect = &full_rect;
+ }
+
+ if (!rect->w || !rect->h) {
+ return 0; /* nothing to do. */
+ }
+
+ if (texture->yuv) {
+ return SDL_UpdateTextureNVPlanar(texture, rect, Yplane, Ypitch, UVplane, UVpitch);
+ } else {
+ SDL_assert(!texture->native);
+ renderer = texture->renderer;
+ SDL_assert(renderer->UpdateTextureNV);
+ if (renderer->UpdateTextureNV) {
+ if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
+ return -1;
+ }
+ return renderer->UpdateTextureNV(renderer, texture, rect, Yplane, Ypitch, UVplane, UVpitch);
+ } else {
+ return SDL_Unsupported();
+ }
+ }
+#else
+ return -1;
+#endif
+}
+
+
+
#if SDL_HAVE_YUV
static int
SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
diff --git a/src/render/SDL_sysrender.h b/src/render/SDL_sysrender.h
index 969ff5c..3150195 100644
--- a/src/render/SDL_sysrender.h
+++ b/src/render/SDL_sysrender.h
@@ -136,6 +136,10 @@ struct SDL_Renderer
const Uint8 *Yplane, int Ypitch,
const Uint8 *Uplane, int Upitch,
const Uint8 *Vplane, int Vpitch);
+ int (*UpdateTextureNV) (SDL_Renderer * renderer, SDL_Texture * texture,
+ const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *UVplane, int UVpitch);
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 97cec9b..ae153c2 100644
--- a/src/render/SDL_yuv_sw.c
+++ b/src/render/SDL_yuv_sw.c
@@ -299,6 +299,40 @@ SDL_SW_UpdateYUVTexturePlanar(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
return 0;
}
+int SDL_SW_UpdateNVTexturePlanar(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *UVplane, int UVpitch)
+{
+ const Uint8 *src;
+ Uint8 *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 UV or VU plane */
+ src = UVplane;
+ dst = swdata->pixels + swdata->h * swdata->w;
+ dst += rect->y/2 * ((swdata->w + 1)/2) + rect->x/2;
+ length = (rect->w + 1) / 2;
+ length *= 2;
+ for (row = 0; row < (rect->h + 1)/2; ++row) {
+ SDL_memcpy(dst, src, length);
+ src += UVpitch;
+ dst += 2 * ((swdata->w + 1)/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 7e3800d..14938fe 100644
--- a/src/render/SDL_yuv_sw_c.h
+++ b/src/render/SDL_yuv_sw_c.h
@@ -55,6 +55,9 @@ int SDL_SW_UpdateYUVTexturePlanar(SDL_SW_YUVTexture * swdata, const SDL_Rect * r
const Uint8 *Yplane, int Ypitch,
const Uint8 *Uplane, int Upitch,
const Uint8 *Vplane, int Vpitch);
+int SDL_SW_UpdateNVTexturePlanar(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *UVplane, int UVpitch);
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 5995e7c..722d30e 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/src/render/opengl/SDL_render_gl.c
@@ -729,6 +729,38 @@ GL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
}
static int
+GL_UpdateTextureNV(SDL_Renderer * renderer, SDL_Texture * texture,
+ const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *UVplane, int UVpitch)
+{
+ GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
+ const GLenum textype = renderdata->textype;
+ GL_TextureData *data = (GL_TextureData *) texture->driverdata;
+
+ GL_ActivateRenderer(renderer);
+
+ renderdata->drawstate.texture = NULL; /* we trash this state. */
+
+ renderdata->glBindTexture(textype, data->texture);
+ renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Ypitch);
+ renderdata->glTexSubImage2D(textype, 0, rect->x, rect->y, rect->w,
+ rect->h, data->format, data->formattype,
+ Yplane);
+
+
+ renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, UVpitch / 2);
+ renderdata->glBindTexture(textype, data->utexture);
+ renderdata->glTexSubImage2D(textype, 0, rect->x/2, rect->y/2,
+ (rect->w + 1)/2, (rect->h + 1)/2,
+ GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, UVplane);
+
+ return GL_CheckError("glTexSubImage2D()", renderer);
+}
+
+
+static int
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch)
{
@@ -1590,6 +1622,7 @@ GL_CreateRenderer(SDL_Window * window, Uint32 flags)
renderer->CreateTexture = GL_CreateTexture;
renderer->UpdateTexture = GL_UpdateTexture;
renderer->UpdateTextureYUV = GL_UpdateTextureYUV;
+ renderer->UpdateTextureNV = GL_UpdateTextureNV;
renderer->LockTexture = GL_LockTexture;
renderer->UnlockTexture = GL_UnlockTexture;
renderer->SetTextureScaleMode = GL_SetTextureScaleMode;
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index c61d9a1..bf6bf93 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -1749,6 +1749,48 @@ GLES2_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
}
static int
+GLES2_UpdateTextureNV(SDL_Renderer * renderer, SDL_Texture * texture,
+ const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *UVplane, int UVpitch)
+{
+ GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
+ GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
+
+ GLES2_ActivateRenderer(renderer);
+
+ /* Bail out if we're supposed to update an empty rectangle */
+ if (rect->w <= 0 || rect->h <= 0) {
+ return 0;
+ }
+
+ data->drawstate.texture = NULL; /* we trash this state. */
+
+ data->glBindTexture(tdata->texture_type, tdata->texture_u);
+ GLES2_TexSubImage2D(data, tdata->texture_type,
+ rect->x / 2,
+ rect->y / 2,
+ (rect->w + 1) / 2,
+ (rect->h + 1) / 2,
+ GL_LUMINANCE_ALPHA,
+ GL_UNSIGNED_BYTE,
+ UVplane, UVpitch / 2, 1);
+
+ data->glBindTexture(tdata->texture_type, tdata->texture);
+ GLES2_TexSubImage2D(data, tdata->texture_type,
+ rect->x,
+ rect->y,
+ rect->w,
+ rect->h,
+ tdata->pixel_format,
+ tdata->pixel_type,
+ Yplane, Ypitch, 1);
+
+ return GL_CheckError("glTexSubImage2D()", renderer);
+}
+
+
+static int
GLES2_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
void **pixels, int *pitch)
{
@@ -2126,6 +2168,7 @@ GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
renderer->CreateTexture = GLES2_CreateTexture;
renderer->UpdateTexture = GLES2_UpdateTexture;
renderer->UpdateTextureYUV = GLES2_UpdateTextureYUV;
+ renderer->UpdateTextureNV = GLES2_UpdateTextureNV;
renderer->LockTexture = GLES2_LockTexture;
renderer->UnlockTexture = GLES2_UnlockTexture;
renderer->SetTextureScaleMode = GLES2_SetTextureScaleMode;