Added support for Android video textures
authorSam Lantinga <slouken@libsdl.org>
Tue, 12 Dec 2017 12:52:17 -0800
changeset 1176783596c8dabfc
parent 11766 4b2b8bf6ef1e
child 11768 0f2fb9ec6ad6
Added support for Android video textures
include/SDL_pixels.h
src/render/opengles2/SDL_render_gles2.c
src/render/opengles2/SDL_shaders_gles2.c
src/render/opengles2/SDL_shaders_gles2.h
     1.1 --- a/include/SDL_pixels.h	Tue Dec 12 12:52:09 2017 -0800
     1.2 +++ b/include/SDL_pixels.h	Tue Dec 12 12:52:17 2017 -0800
     1.3 @@ -287,7 +287,9 @@
     1.4      SDL_PIXELFORMAT_NV12 =      /**< Planar mode: Y + U/V interleaved  (2 planes) */
     1.5          SDL_DEFINE_PIXELFOURCC('N', 'V', '1', '2'),
     1.6      SDL_PIXELFORMAT_NV21 =      /**< Planar mode: Y + V/U interleaved  (2 planes) */
     1.7 -        SDL_DEFINE_PIXELFOURCC('N', 'V', '2', '1')
     1.8 +        SDL_DEFINE_PIXELFOURCC('N', 'V', '2', '1'),
     1.9 +    SDL_PIXELFORMAT_EXTERNAL_OES =      /**< Android video texture format */
    1.10 +        SDL_DEFINE_PIXELFOURCC('O', 'E', 'S', ' ')
    1.11  };
    1.12  
    1.13  typedef struct SDL_Color
     2.1 --- a/src/render/opengles2/SDL_render_gles2.c	Tue Dec 12 12:52:09 2017 -0800
     2.2 +++ b/src/render/opengles2/SDL_render_gles2.c	Tue Dec 12 12:52:17 2017 -0800
     2.3 @@ -166,7 +166,8 @@
     2.4      GLES2_IMAGESOURCE_TEXTURE_BGR,
     2.5      GLES2_IMAGESOURCE_TEXTURE_YUV,
     2.6      GLES2_IMAGESOURCE_TEXTURE_NV12,
     2.7 -    GLES2_IMAGESOURCE_TEXTURE_NV21
     2.8 +    GLES2_IMAGESOURCE_TEXTURE_NV21,
     2.9 +    GLES2_IMAGESOURCE_TEXTURE_EXTERNAL_OES
    2.10  } GLES2_ImageSource;
    2.11  
    2.12  typedef struct GLES2_DriverContext
    2.13 @@ -593,17 +594,32 @@
    2.14          format = GL_LUMINANCE;
    2.15          type = GL_UNSIGNED_BYTE;
    2.16          break;
    2.17 +#ifdef GL_TEXTURE_EXTERNAL_OES
    2.18 +    case SDL_PIXELFORMAT_EXTERNAL_OES:
    2.19 +        format = GL_NONE;
    2.20 +        type = GL_NONE;
    2.21 +        break;
    2.22 +#endif
    2.23      default:
    2.24          return SDL_SetError("Texture format not supported");
    2.25      }
    2.26  
    2.27 +    if (texture->format == SDL_PIXELFORMAT_EXTERNAL_OES &&
    2.28 +        texture->access != SDL_TEXTUREACCESS_STATIC) {
    2.29 +        return SDL_SetError("Unsupported texture access for SDL_PIXELFORMAT_EXTERNAL_OES");
    2.30 +    }
    2.31 +
    2.32      /* Allocate a texture struct */
    2.33      data = (GLES2_TextureData *)SDL_calloc(1, sizeof(GLES2_TextureData));
    2.34      if (!data) {
    2.35          return SDL_OutOfMemory();
    2.36      }
    2.37      data->texture = 0;
    2.38 +#ifdef GL_TEXTURE_EXTERNAL_OES
    2.39 +    data->texture_type = (texture->format == SDL_PIXELFORMAT_EXTERNAL_OES) ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
    2.40 +#else
    2.41      data->texture_type = GL_TEXTURE_2D;
    2.42 +#endif
    2.43      data->pixel_format = format;
    2.44      data->pixel_type = type;
    2.45      data->yuv = ((texture->format == SDL_PIXELFORMAT_IYUV) || (texture->format == SDL_PIXELFORMAT_YV12));
    2.46 @@ -692,9 +708,11 @@
    2.47      renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MAG_FILTER, scaleMode);
    2.48      renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    2.49      renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    2.50 -    renderdata->glTexImage2D(data->texture_type, 0, format, texture->w, texture->h, 0, format, type, NULL);
    2.51 -    if (GL_CheckError("glTexImage2D()", renderer) < 0) {
    2.52 -        return -1;
    2.53 +    if (texture->format != SDL_PIXELFORMAT_EXTERNAL_OES) {
    2.54 +        renderdata->glTexImage2D(data->texture_type, 0, format, texture->w, texture->h, 0, format, type, NULL);
    2.55 +        if (GL_CheckError("glTexImage2D()", renderer) < 0) {
    2.56 +            return -1;
    2.57 +        }
    2.58      }
    2.59  
    2.60      if (texture->access == SDL_TEXTUREACCESS_TARGET) {
    2.61 @@ -1263,6 +1281,9 @@
    2.62              goto fault;
    2.63          }
    2.64          break;
    2.65 +    case GLES2_IMAGESOURCE_TEXTURE_EXTERNAL_OES:
    2.66 +        ftype = GLES2_SHADER_FRAGMENT_TEXTURE_EXTERNAL_OES_SRC;
    2.67 +        break;
    2.68      default:
    2.69          goto fault;
    2.70      }
    2.71 @@ -1711,6 +1732,9 @@
    2.72              case SDL_PIXELFORMAT_NV21:
    2.73                  sourceType = GLES2_IMAGESOURCE_TEXTURE_NV21;
    2.74                  break;
    2.75 +            case SDL_PIXELFORMAT_EXTERNAL_OES:
    2.76 +                sourceType = GLES2_IMAGESOURCE_TEXTURE_EXTERNAL_OES;
    2.77 +                break;
    2.78              default:
    2.79                  return SDL_SetError("Unsupported texture format");
    2.80              }
    2.81 @@ -1741,6 +1765,9 @@
    2.82              case SDL_PIXELFORMAT_NV21:
    2.83                  sourceType = GLES2_IMAGESOURCE_TEXTURE_NV21;
    2.84                  break;
    2.85 +            case SDL_PIXELFORMAT_EXTERNAL_OES:
    2.86 +                sourceType = GLES2_IMAGESOURCE_TEXTURE_EXTERNAL_OES;
    2.87 +                break;
    2.88              default:
    2.89                  return SDL_SetError("Unsupported texture format");
    2.90          }
    2.91 @@ -2208,6 +2235,9 @@
    2.92      renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
    2.93      renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_NV12;
    2.94      renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_NV21;
    2.95 +#ifdef GL_TEXTURE_EXTERNAL_OES
    2.96 +    renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_EXTERNAL_OES;
    2.97 +#endif
    2.98  
    2.99      GLES2_ResetState(renderer);
   2.100  
     3.1 --- a/src/render/opengles2/SDL_shaders_gles2.c	Tue Dec 12 12:52:09 2017 -0800
     3.2 +++ b/src/render/opengles2/SDL_shaders_gles2.c	Tue Dec 12 12:52:17 2017 -0800
     3.3 @@ -275,6 +275,21 @@
     3.4          NV21_SHADER_BODY \
     3.5  ;
     3.6  
     3.7 +/* Custom Android video format texture */
     3.8 +static const Uint8 GLES2_FragmentSrc_TextureExternalOESSrc_[] = " \
     3.9 +    #extension GL_OES_EGL_image_external : require\n\
    3.10 +    precision mediump float; \
    3.11 +    uniform samplerExternalOES u_texture; \
    3.12 +    uniform vec4 u_modulation; \
    3.13 +    varying vec2 v_texCoord; \
    3.14 +    \
    3.15 +    void main() \
    3.16 +    { \
    3.17 +        gl_FragColor = texture2D(u_texture, v_texCoord); \
    3.18 +        gl_FragColor *= u_modulation; \
    3.19 +    } \
    3.20 +";
    3.21 +
    3.22  static const GLES2_ShaderInstance GLES2_VertexSrc_Default = {
    3.23      GL_VERTEX_SHADER,
    3.24      GLES2_SOURCE_SHADER,
    3.25 @@ -380,6 +395,13 @@
    3.26      GLES2_FragmentSrc_TextureNV12BT709Src_
    3.27  };
    3.28  
    3.29 +static const GLES2_ShaderInstance GLES2_FragmentSrc_TextureExternalOESSrc = {
    3.30 +    GL_FRAGMENT_SHADER,
    3.31 +    GLES2_SOURCE_SHADER,
    3.32 +    sizeof(GLES2_FragmentSrc_TextureExternalOESSrc_),
    3.33 +    GLES2_FragmentSrc_TextureExternalOESSrc_
    3.34 +};
    3.35 +
    3.36  
    3.37  /*************************************************************************************************
    3.38   * Vertex/fragment shader definitions                                                            *
    3.39 @@ -490,6 +512,13 @@
    3.40      }
    3.41  };
    3.42  
    3.43 +static GLES2_Shader GLES2_FragmentShader_TextureExternalOESSrc = {
    3.44 +    1,
    3.45 +    {
    3.46 +        &GLES2_FragmentSrc_TextureExternalOESSrc
    3.47 +    }
    3.48 +};
    3.49 +
    3.50  
    3.51  /*************************************************************************************************
    3.52   * Shader selector                                                                               *
    3.53 @@ -528,6 +557,8 @@
    3.54          return &GLES2_FragmentShader_TextureNV21BT601Src;
    3.55      case GLES2_SHADER_FRAGMENT_TEXTURE_NV21_BT709_SRC:
    3.56          return &GLES2_FragmentShader_TextureNV21BT709Src;
    3.57 +    case GLES2_SHADER_FRAGMENT_TEXTURE_EXTERNAL_OES_SRC:
    3.58 +        return &GLES2_FragmentShader_TextureExternalOESSrc;
    3.59      default:
    3.60          return NULL;
    3.61      }
     4.1 --- a/src/render/opengles2/SDL_shaders_gles2.h	Tue Dec 12 12:52:09 2017 -0800
     4.2 +++ b/src/render/opengles2/SDL_shaders_gles2.h	Tue Dec 12 12:52:17 2017 -0800
     4.3 @@ -56,6 +56,7 @@
     4.4      GLES2_SHADER_FRAGMENT_TEXTURE_NV21_JPEG_SRC,
     4.5      GLES2_SHADER_FRAGMENT_TEXTURE_NV21_BT601_SRC,
     4.6      GLES2_SHADER_FRAGMENT_TEXTURE_NV21_BT709_SRC,
     4.7 +    GLES2_SHADER_FRAGMENT_TEXTURE_EXTERNAL_OES_SRC
     4.8  } GLES2_ShaderType;
     4.9  
    4.10  #define GLES2_SOURCE_SHADER (GLenum)-1