src/video/SDL_renderer_gles.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 05 Jan 2011 00:09:19 -0800
changeset 4963 604077962776
parent 4929 aa8888658021
child 4985 047a580d30f3
permissions -rw-r--r--
Fixed screen texture format on Android
I think this also fixes some of the red/blue channel swap bugs reported on iPhone.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #if SDL_VIDEO_RENDER_OGL_ES
    25 
    26 #include "SDL_video.h"
    27 #include "SDL_opengles.h"
    28 #include "SDL_sysvideo.h"
    29 #include "SDL_pixels_c.h"
    30 #include "SDL_rect_c.h"
    31 #include "SDL_yuv_sw_c.h"
    32 
    33 #if defined(__QNXNTO__)
    34 /* Include QNX system header to check QNX version later */
    35 #include <sys/neutrino.h>
    36 #endif /* __QNXNTO__ */
    37 
    38 #if defined(SDL_VIDEO_DRIVER_QNXGF)  ||  \
    39     defined(SDL_VIDEO_DRIVER_PHOTON) ||  \
    40     defined(SDL_VIDEO_DRIVER_PANDORA)
    41 
    42 /* Empty function stub to get OpenGL ES 1.x support without  */
    43 /* OpenGL ES extension GL_OES_draw_texture supported         */
    44 GL_API void GL_APIENTRY
    45 glDrawTexiOES(GLint x, GLint y, GLint z, GLint width, GLint height)
    46 {
    47     return;
    48 }
    49 
    50 #endif /* QNXGF || PHOTON || PANDORA */
    51 
    52 /* OpenGL ES 1.1 renderer implementation, based on the OpenGL renderer */
    53 
    54 static const float inv255f = 1.0f / 255.0f;
    55 
    56 static SDL_Renderer *GLES_CreateRenderer(SDL_Window * window, Uint32 flags);
    57 static int GLES_ActivateRenderer(SDL_Renderer * renderer);
    58 static int GLES_DisplayModeChanged(SDL_Renderer * renderer);
    59 static int GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    60 static int GLES_QueryTexturePixels(SDL_Renderer * renderer,
    61                                    SDL_Texture * texture, void **pixels,
    62                                    int *pitch);
    63 static int GLES_SetTexturePalette(SDL_Renderer * renderer,
    64                                   SDL_Texture * texture,
    65                                   const SDL_Color * colors, int firstcolor,
    66                                   int ncolors);
    67 static int GLES_GetTexturePalette(SDL_Renderer * renderer,
    68                                   SDL_Texture * texture, SDL_Color * colors,
    69                                   int firstcolor, int ncolors);
    70 static int GLES_SetTextureColorMod(SDL_Renderer * renderer,
    71                                    SDL_Texture * texture);
    72 static int GLES_SetTextureAlphaMod(SDL_Renderer * renderer,
    73                                    SDL_Texture * texture);
    74 static int GLES_SetTextureBlendMode(SDL_Renderer * renderer,
    75                                     SDL_Texture * texture);
    76 static int GLES_SetTextureScaleMode(SDL_Renderer * renderer,
    77                                     SDL_Texture * texture);
    78 static int GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    79                               const SDL_Rect * rect, const void *pixels,
    80                               int pitch);
    81 static int GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    82                             const SDL_Rect * rect, int markDirty,
    83                             void **pixels, int *pitch);
    84 static void GLES_UnlockTexture(SDL_Renderer * renderer,
    85                                SDL_Texture * texture);
    86 static void GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    87                               int numrects, const SDL_Rect * rects);
    88 static int GLES_RenderDrawPoints(SDL_Renderer * renderer,
    89                                  const SDL_Point * points, int count);
    90 static int GLES_RenderDrawLines(SDL_Renderer * renderer,
    91                                 const SDL_Point * points, int count);
    92 static int GLES_RenderDrawRects(SDL_Renderer * renderer,
    93                                 const SDL_Rect ** rects, int count);
    94 static int GLES_RenderFillRects(SDL_Renderer * renderer,
    95                                 const SDL_Rect ** rects, int count);
    96 static int GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    97                            const SDL_Rect * srcrect,
    98                            const SDL_Rect * dstrect);
    99 static void GLES_RenderPresent(SDL_Renderer * renderer);
   100 static void GLES_DestroyTexture(SDL_Renderer * renderer,
   101                                 SDL_Texture * texture);
   102 static void GLES_DestroyRenderer(SDL_Renderer * renderer);
   103 
   104 
   105 SDL_RenderDriver GL_ES_RenderDriver = {
   106     GLES_CreateRenderer,
   107     {
   108      "opengl_es",
   109      (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
   110       SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
   111      (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
   112       SDL_TEXTUREMODULATE_ALPHA),
   113      (SDL_BLENDMODE_NONE | SDL_BLENDMODE_MASK |
   114       SDL_BLENDMODE_BLEND | SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD),
   115      (SDL_SCALEMODE_NONE | SDL_SCALEMODE_FAST | SDL_SCALEMODE_SLOW), 5,
   116      {
   117       /* OpenGL ES 1.x supported formats list */
   118       SDL_PIXELFORMAT_ABGR4444,
   119       SDL_PIXELFORMAT_ABGR1555,
   120       SDL_PIXELFORMAT_BGR565,
   121       SDL_PIXELFORMAT_RGB24,
   122       SDL_PIXELFORMAT_BGR888,
   123       SDL_PIXELFORMAT_ABGR8888},
   124      0,
   125      0}
   126 };
   127 
   128 typedef struct
   129 {
   130     SDL_GLContext context;
   131     SDL_bool updateSize;
   132     int blendMode;
   133 
   134 #ifndef APIENTRY
   135 #define APIENTRY
   136 #endif
   137 
   138     SDL_bool useDrawTexture;
   139     SDL_bool GL_OES_draw_texture_supported;
   140 
   141     /* OpenGL ES functions */
   142 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
   143 #include "SDL_glesfuncs.h"
   144 #undef SDL_PROC
   145 
   146 } GLES_RenderData;
   147 
   148 typedef struct
   149 {
   150     GLuint texture;
   151     GLenum type;
   152     GLfloat texw;
   153     GLfloat texh;
   154     GLenum format;
   155     GLenum formattype;
   156     void *pixels;
   157     int pitch;
   158     SDL_DirtyRectList dirty;
   159 } GLES_TextureData;
   160 
   161 static void
   162 GLES_SetError(const char *prefix, GLenum result)
   163 {
   164     const char *error;
   165 
   166     switch (result) {
   167     case GL_NO_ERROR:
   168         error = "GL_NO_ERROR";
   169         break;
   170     case GL_INVALID_ENUM:
   171         error = "GL_INVALID_ENUM";
   172         break;
   173     case GL_INVALID_VALUE:
   174         error = "GL_INVALID_VALUE";
   175         break;
   176     case GL_INVALID_OPERATION:
   177         error = "GL_INVALID_OPERATION";
   178         break;
   179     case GL_STACK_OVERFLOW:
   180         error = "GL_STACK_OVERFLOW";
   181         break;
   182     case GL_STACK_UNDERFLOW:
   183         error = "GL_STACK_UNDERFLOW";
   184         break;
   185     case GL_OUT_OF_MEMORY:
   186         error = "GL_OUT_OF_MEMORY";
   187         break;
   188     default:
   189         error = "UNKNOWN";
   190         break;
   191     }
   192     SDL_SetError("%s: %s", prefix, error);
   193 }
   194 
   195 static int
   196 GLES_LoadFunctions(GLES_RenderData * data)
   197 {
   198 
   199 #define SDL_PROC(ret,func,params) \
   200     data->func = func;
   201 #include "SDL_glesfuncs.h"
   202 #undef SDL_PROC
   203 
   204     return 0;
   205 }
   206 
   207 SDL_Renderer *
   208 GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
   209 {
   210 
   211     SDL_Renderer *renderer;
   212     GLES_RenderData *data;
   213     GLint value;
   214     int doublebuffer;
   215 
   216     if (!(window->flags & SDL_WINDOW_OPENGL)) {
   217         if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
   218             return NULL;
   219         }
   220     }
   221 
   222     renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   223     if (!renderer) {
   224         SDL_OutOfMemory();
   225         return NULL;
   226     }
   227 
   228     data = (GLES_RenderData *) SDL_calloc(1, sizeof(*data));
   229     if (!data) {
   230         GLES_DestroyRenderer(renderer);
   231         SDL_OutOfMemory();
   232         return NULL;
   233     }
   234 
   235     renderer->ActivateRenderer = GLES_ActivateRenderer;
   236     renderer->DisplayModeChanged = GLES_DisplayModeChanged;
   237     renderer->CreateTexture = GLES_CreateTexture;
   238     renderer->QueryTexturePixels = GLES_QueryTexturePixels;
   239     renderer->SetTexturePalette = GLES_SetTexturePalette;
   240     renderer->GetTexturePalette = GLES_GetTexturePalette;
   241     renderer->SetTextureColorMod = GLES_SetTextureColorMod;
   242     renderer->SetTextureAlphaMod = GLES_SetTextureAlphaMod;
   243     renderer->SetTextureBlendMode = GLES_SetTextureBlendMode;
   244     renderer->SetTextureScaleMode = GLES_SetTextureScaleMode;
   245     renderer->UpdateTexture = GLES_UpdateTexture;
   246     renderer->LockTexture = GLES_LockTexture;
   247     renderer->UnlockTexture = GLES_UnlockTexture;
   248     renderer->DirtyTexture = GLES_DirtyTexture;
   249     renderer->RenderDrawPoints = GLES_RenderDrawPoints;
   250     renderer->RenderDrawLines = GLES_RenderDrawLines;
   251     renderer->RenderDrawRects = GLES_RenderDrawRects;
   252     renderer->RenderFillRects = GLES_RenderFillRects;
   253     renderer->RenderCopy = GLES_RenderCopy;
   254     renderer->RenderPresent = GLES_RenderPresent;
   255     renderer->DestroyTexture = GLES_DestroyTexture;
   256     renderer->DestroyRenderer = GLES_DestroyRenderer;
   257     renderer->info = GL_ES_RenderDriver.info;
   258     renderer->window = window;
   259     renderer->driverdata = data;
   260 
   261     renderer->info.flags =
   262         (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
   263 
   264 #if defined(__QNXNTO__)
   265 #if _NTO_VERSION<=641
   266     /* QNX's OpenGL ES implementation is broken regarding             */
   267     /* packed textures support, affected versions 6.3.2, 6.4.0, 6.4.1 */
   268     renderer->info.num_texture_formats = 2;
   269     renderer->info.texture_formats[0] = SDL_PIXELFORMAT_ABGR8888;
   270     renderer->info.texture_formats[1] = SDL_PIXELFORMAT_BGR24;
   271 #endif /* _NTO_VERSION */
   272 #endif /* __QNXNTO__ */
   273 
   274     if (GLES_LoadFunctions(data) < 0) {
   275         GLES_DestroyRenderer(renderer);
   276         return NULL;
   277     }
   278 
   279     data->context = SDL_GL_CreateContext(window);
   280     if (!data->context) {
   281         GLES_DestroyRenderer(renderer);
   282         return NULL;
   283     }
   284     if (SDL_GL_MakeCurrent(window, data->context) < 0) {
   285         GLES_DestroyRenderer(renderer);
   286         return NULL;
   287     }
   288 
   289     if (flags & SDL_RENDERER_PRESENTVSYNC) {
   290         SDL_GL_SetSwapInterval(1);
   291     } else {
   292         SDL_GL_SetSwapInterval(0);
   293     }
   294     if (SDL_GL_GetSwapInterval() > 0) {
   295         renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   296     }
   297 
   298     if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
   299         if (!doublebuffer) {
   300             renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
   301         }
   302     }
   303 #if SDL_VIDEO_DRIVER_PANDORA
   304     data->GL_OES_draw_texture_supported = SDL_FALSE;
   305     data->useDrawTexture = SDL_FALSE;
   306 #else
   307     if (SDL_GL_ExtensionSupported("GL_OES_draw_texture")) {
   308         data->GL_OES_draw_texture_supported = SDL_TRUE;
   309         data->useDrawTexture = SDL_TRUE;
   310     } else {
   311         data->GL_OES_draw_texture_supported = SDL_FALSE;
   312         data->useDrawTexture = SDL_FALSE;
   313     }
   314 #endif
   315 
   316     data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   317     renderer->info.max_texture_width = value;
   318     data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   319     renderer->info.max_texture_height = value;
   320 
   321     /* Set up parameters for rendering */
   322     data->blendMode = -1;
   323     data->glDisable(GL_DEPTH_TEST);
   324     data->glDisable(GL_CULL_FACE);
   325     data->updateSize = SDL_TRUE;
   326 
   327     return renderer;
   328 }
   329 
   330 static int
   331 GLES_ActivateRenderer(SDL_Renderer * renderer)
   332 {
   333 
   334     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   335     SDL_Window *window = renderer->window;
   336 
   337     if (SDL_GL_MakeCurrent(window, data->context) < 0) {
   338         return -1;
   339     }
   340     if (data->updateSize) {
   341         data->glMatrixMode(GL_PROJECTION);
   342         data->glLoadIdentity();
   343         data->glMatrixMode(GL_MODELVIEW);
   344         data->glLoadIdentity();
   345         data->glViewport(0, 0, window->w, window->h);
   346         data->glOrthof(0.0, (GLfloat) window->w, (GLfloat) window->h, 0.0,
   347                        0.0, 1.0);
   348         data->updateSize = SDL_FALSE;
   349     }
   350     return 0;
   351 }
   352 
   353 static int
   354 GLES_DisplayModeChanged(SDL_Renderer * renderer)
   355 {
   356     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   357 
   358     data->updateSize = SDL_TRUE;
   359     return 0;
   360 }
   361 
   362 static __inline__ int
   363 power_of_2(int input)
   364 {
   365     int value = 1;
   366 
   367     while (value < input) {
   368         value <<= 1;
   369     }
   370     return value;
   371 }
   372 
   373 static int
   374 GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   375 {
   376     GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   377     GLES_TextureData *data;
   378     GLint internalFormat;
   379     GLenum format, type;
   380     int texture_w, texture_h;
   381     GLenum result;
   382 
   383     switch (texture->format) {
   384     case SDL_PIXELFORMAT_RGB24:
   385         internalFormat = GL_RGB;
   386         format = GL_RGB;
   387         type = GL_UNSIGNED_BYTE;
   388         break;
   389     case SDL_PIXELFORMAT_BGR888:
   390     case SDL_PIXELFORMAT_ABGR8888:
   391         internalFormat = GL_RGBA;
   392         format = GL_RGBA;
   393         type = GL_UNSIGNED_BYTE;
   394         break;
   395     case SDL_PIXELFORMAT_BGR565:
   396         internalFormat = GL_RGB;
   397         format = GL_RGB;
   398         type = GL_UNSIGNED_SHORT_5_6_5;
   399         break;
   400     case SDL_PIXELFORMAT_ABGR1555:
   401         internalFormat = GL_RGBA;
   402         format = GL_RGBA;
   403         type = GL_UNSIGNED_SHORT_5_5_5_1;
   404         break;
   405     case SDL_PIXELFORMAT_ABGR4444:
   406         internalFormat = GL_RGBA;
   407         format = GL_RGBA;
   408         type = GL_UNSIGNED_SHORT_4_4_4_4;
   409         break;
   410     default:
   411         SDL_SetError("Unsupported by OpenGL ES texture format");
   412         return -1;
   413     }
   414 
   415     data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data));
   416     if (!data) {
   417         SDL_OutOfMemory();
   418         return -1;
   419     }
   420 
   421     if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   422         data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
   423         data->pixels = SDL_malloc(texture->h * data->pitch);
   424         if (!data->pixels) {
   425             SDL_OutOfMemory();
   426             SDL_free(data);
   427             return -1;
   428         }
   429     }
   430 
   431     texture->driverdata = data;
   432 
   433     renderdata->glGetError();
   434     renderdata->glEnable(GL_TEXTURE_2D);
   435     renderdata->glGenTextures(1, &data->texture);
   436 
   437     data->type = GL_TEXTURE_2D;
   438     /* no NPOV textures allowed in OpenGL ES (yet) */
   439     texture_w = power_of_2(texture->w);
   440     texture_h = power_of_2(texture->h);
   441     data->texw = (GLfloat) texture->w / texture_w;
   442     data->texh = (GLfloat) texture->h / texture_h;
   443 
   444     data->format = format;
   445     data->formattype = type;
   446     renderdata->glBindTexture(data->type, data->texture);
   447     renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
   448                                 GL_NEAREST);
   449     renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
   450                                 GL_NEAREST);
   451     renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
   452                                 GL_CLAMP_TO_EDGE);
   453     renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
   454                                 GL_CLAMP_TO_EDGE);
   455 
   456     renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
   457                              texture_h, 0, format, type, NULL);
   458     renderdata->glDisable(GL_TEXTURE_2D);
   459 
   460     result = renderdata->glGetError();
   461     if (result != GL_NO_ERROR) {
   462         GLES_SetError("glTexImage2D()", result);
   463         return -1;
   464     }
   465     return 0;
   466 }
   467 
   468 static int
   469 GLES_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   470                         void **pixels, int *pitch)
   471 {
   472     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   473 
   474     *pixels = data->pixels;
   475     *pitch = data->pitch;
   476     return 0;
   477 }
   478 
   479 static int
   480 GLES_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   481                        const SDL_Color * colors, int firstcolor, int ncolors)
   482 {
   483     SDL_SetError("OpenGL ES does not support paletted textures");
   484     return -1;
   485 }
   486 
   487 static int
   488 GLES_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   489                        SDL_Color * colors, int firstcolor, int ncolors)
   490 {
   491     SDL_SetError("OpenGL ES does not support paletted textures");
   492     return -1;
   493 }
   494 
   495 static void
   496 SetupTextureUpdate(GLES_RenderData * renderdata, SDL_Texture * texture,
   497                    int pitch)
   498 {
   499     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   500     renderdata->glBindTexture(data->type, data->texture);
   501     renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   502 }
   503 
   504 static int
   505 GLES_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
   506 {
   507     return 0;
   508 }
   509 
   510 static int
   511 GLES_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
   512 {
   513     return 0;
   514 }
   515 
   516 static int
   517 GLES_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
   518 {
   519     switch (texture->blendMode) {
   520     case SDL_BLENDMODE_NONE:
   521     case SDL_BLENDMODE_MASK:
   522     case SDL_BLENDMODE_BLEND:
   523     case SDL_BLENDMODE_ADD:
   524     case SDL_BLENDMODE_MOD:
   525         return 0;
   526     default:
   527         SDL_Unsupported();
   528         texture->blendMode = SDL_BLENDMODE_NONE;
   529         return -1;
   530     }
   531 }
   532 
   533 static int
   534 GLES_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
   535 {
   536     switch (texture->scaleMode) {
   537     case SDL_SCALEMODE_NONE:
   538     case SDL_SCALEMODE_FAST:
   539     case SDL_SCALEMODE_SLOW:
   540         return 0;
   541     case SDL_SCALEMODE_BEST:
   542         SDL_Unsupported();
   543         texture->scaleMode = SDL_SCALEMODE_SLOW;
   544         return -1;
   545     default:
   546         SDL_Unsupported();
   547         texture->scaleMode = SDL_SCALEMODE_NONE;
   548         return -1;
   549     }
   550 }
   551 
   552 static int
   553 GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   554                    const SDL_Rect * rect, const void *pixels, int pitch)
   555 {
   556     GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   557     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   558     GLenum result;
   559 
   560     renderdata->glGetError();
   561     renderdata->glEnable(data->type);
   562     SetupTextureUpdate(renderdata, texture, pitch);
   563     renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
   564                                 rect->h, data->format, data->formattype,
   565                                 pixels);
   566     renderdata->glDisable(data->type);
   567     result = renderdata->glGetError();
   568     if (result != GL_NO_ERROR) {
   569         GLES_SetError("glTexSubImage2D()", result);
   570         return -1;
   571     }
   572     return 0;
   573 }
   574 
   575 static int
   576 GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   577                  const SDL_Rect * rect, int markDirty, void **pixels,
   578                  int *pitch)
   579 {
   580     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   581 
   582     if (markDirty) {
   583         SDL_AddDirtyRect(&data->dirty, rect);
   584     }
   585 
   586     *pixels =
   587         (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
   588                   rect->x * SDL_BYTESPERPIXEL(texture->format));
   589     *pitch = data->pitch;
   590     return 0;
   591 }
   592 
   593 static void
   594 GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   595 {
   596 }
   597 
   598 static void
   599 GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   600                   int numrects, const SDL_Rect * rects)
   601 {
   602     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   603     int i;
   604 
   605     for (i = 0; i < numrects; ++i) {
   606         SDL_AddDirtyRect(&data->dirty, &rects[i]);
   607     }
   608 }
   609 
   610 static void
   611 GLES_SetBlendMode(GLES_RenderData * data, int blendMode, int isprimitive)
   612 {
   613     if (blendMode != data->blendMode) {
   614         switch (blendMode) {
   615         case SDL_BLENDMODE_NONE:
   616             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   617             data->glDisable(GL_BLEND);
   618             break;
   619         case SDL_BLENDMODE_MASK:
   620             if (isprimitive) {
   621                 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   622                 data->glDisable(GL_BLEND);
   623                 /* The same as SDL_BLENDMODE_NONE */
   624                 blendMode = SDL_BLENDMODE_NONE;
   625                 break;
   626             }
   627             /* fall through */
   628         case SDL_BLENDMODE_BLEND:
   629             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   630             data->glEnable(GL_BLEND);
   631             data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   632             break;
   633         case SDL_BLENDMODE_ADD:
   634             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   635             data->glEnable(GL_BLEND);
   636             data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   637             break;
   638         case SDL_BLENDMODE_MOD:
   639             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   640             data->glEnable(GL_BLEND);
   641             data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   642             break;
   643         }
   644         data->blendMode = blendMode;
   645     }
   646 }
   647 
   648 static int
   649 GLES_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
   650                       int count)
   651 {
   652     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   653     int i;
   654     GLshort *vertices;
   655 
   656     GLES_SetBlendMode(data, renderer->blendMode, 1);
   657 
   658     data->glColor4f((GLfloat) renderer->r * inv255f,
   659                     (GLfloat) renderer->g * inv255f,
   660                     (GLfloat) renderer->b * inv255f,
   661                     (GLfloat) renderer->a * inv255f);
   662 
   663     vertices = SDL_stack_alloc(GLshort, count*2);
   664     for (i = 0; i < count; ++i) {
   665         vertices[2*i+0] = (GLshort)points[i].x;
   666         vertices[2*i+1] = (GLshort)points[i].y;
   667     }
   668     data->glVertexPointer(2, GL_SHORT, 0, vertices);
   669     data->glEnableClientState(GL_VERTEX_ARRAY);
   670     data->glDrawArrays(GL_POINTS, 0, count);
   671     data->glDisableClientState(GL_VERTEX_ARRAY);
   672     SDL_stack_free(vertices);
   673 
   674     return 0;
   675 }
   676 
   677 static int
   678 GLES_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
   679                      int count)
   680 {
   681     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   682     int i;
   683     GLshort *vertices;
   684 
   685     GLES_SetBlendMode(data, renderer->blendMode, 1);
   686 
   687     data->glColor4f((GLfloat) renderer->r * inv255f,
   688                     (GLfloat) renderer->g * inv255f,
   689                     (GLfloat) renderer->b * inv255f,
   690                     (GLfloat) renderer->a * inv255f);
   691 
   692     vertices = SDL_stack_alloc(GLshort, count*2);
   693     for (i = 0; i < count; ++i) {
   694         vertices[2*i+0] = (GLshort)points[i].x;
   695         vertices[2*i+1] = (GLshort)points[i].y;
   696     }
   697     data->glVertexPointer(2, GL_SHORT, 0, vertices);
   698     data->glEnableClientState(GL_VERTEX_ARRAY);
   699     if (count > 2 && 
   700         points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
   701         /* GL_LINE_LOOP takes care of the final segment */
   702         --count;
   703         data->glDrawArrays(GL_LINE_LOOP, 0, count);
   704     } else {
   705         data->glDrawArrays(GL_LINE_STRIP, 0, count);
   706     }
   707     data->glDisableClientState(GL_VERTEX_ARRAY);
   708     SDL_stack_free(vertices);
   709 
   710     return 0;
   711 }
   712 
   713 static int
   714 GLES_RenderDrawRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
   715                      int count)
   716 {
   717     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   718     int i;
   719 
   720     GLES_SetBlendMode(data, renderer->blendMode, 1);
   721 
   722     data->glColor4f((GLfloat) renderer->r * inv255f,
   723                     (GLfloat) renderer->g * inv255f,
   724                     (GLfloat) renderer->b * inv255f,
   725                     (GLfloat) renderer->a * inv255f);
   726 
   727     data->glEnableClientState(GL_VERTEX_ARRAY);
   728     for (i = 0; i < count; ++i) {
   729         const SDL_Rect *rect = rects[i];
   730         GLshort minx = rect->x;
   731         GLshort maxx = rect->x + rect->w;
   732         GLshort miny = rect->y;
   733         GLshort maxy = rect->y + rect->h;
   734         GLshort vertices[8];
   735         vertices[0] = minx;
   736         vertices[1] = miny;
   737         vertices[2] = maxx;
   738         vertices[3] = miny;
   739         vertices[4] = minx;
   740         vertices[5] = maxy;
   741         vertices[6] = maxx;
   742         vertices[7] = maxy;
   743 
   744         data->glVertexPointer(2, GL_SHORT, 0, vertices);
   745         data->glDrawArrays(GL_LINE_LOOP, 0, 4);
   746     }
   747     data->glDisableClientState(GL_VERTEX_ARRAY);
   748 
   749     return 0;
   750 }
   751 
   752 static int
   753 GLES_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
   754                      int count)
   755 {
   756     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   757     int i;
   758 
   759     GLES_SetBlendMode(data, renderer->blendMode, 1);
   760 
   761     data->glColor4f((GLfloat) renderer->r * inv255f,
   762                     (GLfloat) renderer->g * inv255f,
   763                     (GLfloat) renderer->b * inv255f,
   764                     (GLfloat) renderer->a * inv255f);
   765 
   766     data->glEnableClientState(GL_VERTEX_ARRAY);
   767     for (i = 0; i < count; ++i) {
   768         const SDL_Rect *rect = rects[i];
   769         GLshort minx = rect->x;
   770         GLshort maxx = rect->x + rect->w;
   771         GLshort miny = rect->y;
   772         GLshort maxy = rect->y + rect->h;
   773         GLshort vertices[8];
   774         vertices[0] = minx;
   775         vertices[1] = miny;
   776         vertices[2] = maxx;
   777         vertices[3] = miny;
   778         vertices[4] = minx;
   779         vertices[5] = maxy;
   780         vertices[6] = maxx;
   781         vertices[7] = maxy;
   782 
   783         data->glVertexPointer(2, GL_SHORT, 0, vertices);
   784         data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   785     }
   786     data->glDisableClientState(GL_VERTEX_ARRAY);
   787 
   788     return 0;
   789 }
   790 
   791 static int
   792 GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   793                 const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   794 {
   795 
   796     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   797     GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
   798     int minx, miny, maxx, maxy;
   799     GLfloat minu, maxu, minv, maxv;
   800     int i;
   801     void *temp_buffer;          /* used for reformatting dirty rect pixels */
   802     void *temp_ptr;
   803 
   804     data->glEnable(GL_TEXTURE_2D);
   805 
   806     if (texturedata->dirty.list) {
   807         SDL_DirtyRect *dirty;
   808         void *pixels;
   809         int bpp = SDL_BYTESPERPIXEL(texture->format);
   810         int pitch = texturedata->pitch;
   811 
   812         SetupTextureUpdate(data, texture, pitch);
   813 
   814         data->glBindTexture(texturedata->type, texturedata->texture);
   815         for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
   816             SDL_Rect *rect = &dirty->rect;
   817             pixels =
   818                 (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
   819                           rect->x * bpp);
   820             /*      There is no GL_UNPACK_ROW_LENGTH in OpenGLES 
   821                we must do this reformatting ourselves(!)
   822 
   823                maybe it'd be a good idea to keep a temp buffer around
   824                for this purpose rather than allocating it each time
   825              */
   826             if( rect->x == 0 && rect->w * bpp == pitch ) {
   827                 temp_buffer = pixels; /* Updating whole texture, no need to reformat */
   828             } else {
   829                 temp_buffer = SDL_malloc(rect->w * rect->h * bpp);
   830                 temp_ptr = temp_buffer;
   831                 for (i = 0; i < rect->h; i++) {
   832                     SDL_memcpy(temp_ptr, pixels, rect->w * bpp);
   833                     temp_ptr += rect->w * bpp;
   834                     pixels += pitch;
   835                 }
   836             }
   837 
   838             data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
   839                                   rect->w, rect->h, texturedata->format,
   840                                   texturedata->formattype, temp_buffer);
   841 
   842             if( temp_buffer != pixels ) {
   843                 SDL_free(temp_buffer);
   844             }
   845         }
   846         SDL_ClearDirtyRects(&texturedata->dirty);
   847     }
   848 
   849     data->glBindTexture(texturedata->type, texturedata->texture);
   850 
   851     if (texture->modMode) {
   852         data->glColor4f((GLfloat) texture->r * inv255f,
   853                         (GLfloat) texture->g * inv255f,
   854                         (GLfloat) texture->b * inv255f,
   855                         (GLfloat) texture->a * inv255f);
   856     } else {
   857         data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
   858     }
   859 
   860     GLES_SetBlendMode(data, texture->blendMode, 0);
   861 
   862     switch (texture->scaleMode) {
   863     case SDL_SCALEMODE_NONE:
   864     case SDL_SCALEMODE_FAST:
   865         data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
   866                               GL_NEAREST);
   867         data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
   868                               GL_NEAREST);
   869         break;
   870     case SDL_SCALEMODE_SLOW:
   871     case SDL_SCALEMODE_BEST:
   872         data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
   873                               GL_LINEAR);
   874         data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
   875                               GL_LINEAR);
   876         break;
   877     }
   878 
   879     if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
   880         /* this code is a little funny because the viewport is upside down vs SDL's coordinate system */
   881         SDL_Window *window = renderer->window;
   882         GLint cropRect[4];
   883         cropRect[0] = srcrect->x;
   884         cropRect[1] = srcrect->y + srcrect->h;
   885         cropRect[2] = srcrect->w;
   886         cropRect[3] = -srcrect->h;
   887         data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES,
   888                                cropRect);
   889         data->glDrawTexiOES(dstrect->x, window->h - dstrect->y - dstrect->h,
   890                             0, dstrect->w, dstrect->h);
   891     } else {
   892 
   893         minx = dstrect->x;
   894         miny = dstrect->y;
   895         maxx = dstrect->x + dstrect->w;
   896         maxy = dstrect->y + dstrect->h;
   897 
   898         minu = (GLfloat) srcrect->x / texture->w;
   899         minu *= texturedata->texw;
   900         maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
   901         maxu *= texturedata->texw;
   902         minv = (GLfloat) srcrect->y / texture->h;
   903         minv *= texturedata->texh;
   904         maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
   905         maxv *= texturedata->texh;
   906 
   907         GLshort vertices[8];
   908         GLfloat texCoords[8];
   909 
   910         vertices[0] = minx;
   911         vertices[1] = miny;
   912         vertices[2] = maxx;
   913         vertices[3] = miny;
   914         vertices[4] = minx;
   915         vertices[5] = maxy;
   916         vertices[6] = maxx;
   917         vertices[7] = maxy;
   918 
   919         texCoords[0] = minu;
   920         texCoords[1] = minv;
   921         texCoords[2] = maxu;
   922         texCoords[3] = minv;
   923         texCoords[4] = minu;
   924         texCoords[5] = maxv;
   925         texCoords[6] = maxu;
   926         texCoords[7] = maxv;
   927 
   928         data->glVertexPointer(2, GL_SHORT, 0, vertices);
   929         data->glEnableClientState(GL_VERTEX_ARRAY);
   930         data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
   931         data->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   932         data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   933         data->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
   934         data->glDisableClientState(GL_VERTEX_ARRAY);
   935     }
   936 
   937     data->glDisable(GL_TEXTURE_2D);
   938 
   939     return 0;
   940 }
   941 
   942 static void
   943 GLES_RenderPresent(SDL_Renderer * renderer)
   944 {
   945     SDL_GL_SwapWindow(renderer->window);
   946 }
   947 
   948 static void
   949 GLES_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   950 {
   951     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   952 
   953     if (!data) {
   954         return;
   955     }
   956     if (data->texture) {
   957         glDeleteTextures(1, &data->texture);
   958     }
   959     if (data->pixels) {
   960         SDL_free(data->pixels);
   961     }
   962     SDL_FreeDirtyRects(&data->dirty);
   963     SDL_free(data);
   964     texture->driverdata = NULL;
   965 }
   966 
   967 static void
   968 GLES_DestroyRenderer(SDL_Renderer * renderer)
   969 {
   970     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   971 
   972     if (data) {
   973         if (data->context) {
   974             SDL_GL_DeleteContext(data->context);
   975         }
   976         SDL_free(data);
   977     }
   978     SDL_free(renderer);
   979 }
   980 
   981 #endif /* SDL_VIDEO_RENDER_OGL_ES */
   982 
   983 /* vi: set ts=4 sw=4 expandtab: */