src/video/SDL_renderer_gles.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 19 Jan 2011 23:47:50 -0800
changeset 5052 4cb4b18cbae3
parent 4990 397e748d901a
child 5053 b5b42be9333c
permissions -rw-r--r--
Fixed bug #1028

pelya 2010-07-21 04:54:41 PDT

GLES_UpdateTexture() ignores pitch value, because of that some textures created
with SDL_CreateTextureFromSurface() are drawn incorrectly, especially if image
width is not multiplier of 2.
     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), 6,
   116      {
   117       /* OpenGL ES 1.x supported formats list */
   118       SDL_PIXELFORMAT_RGBA4444,
   119       SDL_PIXELFORMAT_RGBA5551,
   120       SDL_PIXELFORMAT_RGB565,
   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_RGB565:
   396         internalFormat = GL_RGB;
   397         format = GL_RGB;
   398         type = GL_UNSIGNED_SHORT_5_6_5;
   399         break;
   400     case SDL_PIXELFORMAT_RGBA5551:
   401         internalFormat = GL_RGBA;
   402         format = GL_RGBA;
   403         type = GL_UNSIGNED_SHORT_5_5_5_1;
   404         break;
   405     case SDL_PIXELFORMAT_RGBA4444:
   406         internalFormat = GL_RGBA;
   407         format = GL_RGBA;
   408         type = GL_UNSIGNED_SHORT_4_4_4_4;
   409         break;
   410     default:
   411         SDL_SetError("Texture format %s not supported by OpenGL ES",
   412                      SDL_GetPixelFormatName(texture->format));
   413         return -1;
   414     }
   415 
   416     data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data));
   417     if (!data) {
   418         SDL_OutOfMemory();
   419         return -1;
   420     }
   421 
   422     if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   423         data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
   424         data->pixels = SDL_malloc(texture->h * data->pitch);
   425         if (!data->pixels) {
   426             SDL_OutOfMemory();
   427             SDL_free(data);
   428             return -1;
   429         }
   430     }
   431 
   432     texture->driverdata = data;
   433 
   434     renderdata->glGetError();
   435     renderdata->glEnable(GL_TEXTURE_2D);
   436     renderdata->glGenTextures(1, &data->texture);
   437 
   438     data->type = GL_TEXTURE_2D;
   439     /* no NPOV textures allowed in OpenGL ES (yet) */
   440     texture_w = power_of_2(texture->w);
   441     texture_h = power_of_2(texture->h);
   442     data->texw = (GLfloat) texture->w / texture_w;
   443     data->texh = (GLfloat) texture->h / texture_h;
   444 
   445     data->format = format;
   446     data->formattype = type;
   447     renderdata->glBindTexture(data->type, data->texture);
   448     renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
   449                                 GL_NEAREST);
   450     renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
   451                                 GL_NEAREST);
   452     renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
   453                                 GL_CLAMP_TO_EDGE);
   454     renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
   455                                 GL_CLAMP_TO_EDGE);
   456 
   457     renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
   458                              texture_h, 0, format, type, NULL);
   459     renderdata->glDisable(GL_TEXTURE_2D);
   460 
   461     result = renderdata->glGetError();
   462     if (result != GL_NO_ERROR) {
   463         GLES_SetError("glTexImage2D()", result);
   464         return -1;
   465     }
   466     return 0;
   467 }
   468 
   469 static int
   470 GLES_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   471                         void **pixels, int *pitch)
   472 {
   473     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   474 
   475     *pixels = data->pixels;
   476     *pitch = data->pitch;
   477     return 0;
   478 }
   479 
   480 static int
   481 GLES_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   482                        const SDL_Color * colors, int firstcolor, int ncolors)
   483 {
   484     SDL_SetError("OpenGL ES does not support paletted textures");
   485     return -1;
   486 }
   487 
   488 static int
   489 GLES_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   490                        SDL_Color * colors, int firstcolor, int ncolors)
   491 {
   492     SDL_SetError("OpenGL ES does not support paletted textures");
   493     return -1;
   494 }
   495 
   496 static void
   497 SetupTextureUpdate(GLES_RenderData * renderdata, SDL_Texture * texture,
   498                    int pitch)
   499 {
   500     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   501     renderdata->glBindTexture(data->type, data->texture);
   502     renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   503 }
   504 
   505 static int
   506 GLES_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
   507 {
   508     return 0;
   509 }
   510 
   511 static int
   512 GLES_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
   513 {
   514     return 0;
   515 }
   516 
   517 static int
   518 GLES_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
   519 {
   520     switch (texture->blendMode) {
   521     case SDL_BLENDMODE_NONE:
   522     case SDL_BLENDMODE_MASK:
   523     case SDL_BLENDMODE_BLEND:
   524     case SDL_BLENDMODE_ADD:
   525     case SDL_BLENDMODE_MOD:
   526         return 0;
   527     default:
   528         SDL_Unsupported();
   529         texture->blendMode = SDL_BLENDMODE_NONE;
   530         return -1;
   531     }
   532 }
   533 
   534 static int
   535 GLES_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
   536 {
   537     switch (texture->scaleMode) {
   538     case SDL_SCALEMODE_NONE:
   539     case SDL_SCALEMODE_FAST:
   540     case SDL_SCALEMODE_SLOW:
   541         return 0;
   542     case SDL_SCALEMODE_BEST:
   543         SDL_Unsupported();
   544         texture->scaleMode = SDL_SCALEMODE_SLOW;
   545         return -1;
   546     default:
   547         SDL_Unsupported();
   548         texture->scaleMode = SDL_SCALEMODE_NONE;
   549         return -1;
   550     }
   551 }
   552 
   553 static int
   554 GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   555                    const SDL_Rect * rect, const void *pixels, int pitch)
   556 {
   557     GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   558     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   559     GLenum result;
   560     int bpp = SDL_BYTESPERPIXEL(texture->format);
   561     void * temp_buffer;
   562     void * temp_ptr;
   563     int i;
   564 
   565     renderdata->glGetError();
   566     renderdata->glEnable(data->type);
   567     SetupTextureUpdate(renderdata, texture, pitch);
   568 
   569     if( rect->w * bpp == pitch ) {
   570          temp_buffer = (void *)pixels; /* No need to reformat */
   571     } else {
   572          /* Reformatting of mem area required */
   573          temp_buffer = SDL_malloc(rect->w * rect->h * bpp);
   574          temp_ptr = temp_buffer;
   575          for (i = 0; i < rect->h; i++) {
   576              SDL_memcpy(temp_ptr, pixels, rect->w * bpp);
   577              temp_ptr += rect->w * bpp;
   578              pixels += pitch;
   579          }
   580     }
   581 
   582     renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
   583                                 rect->h, data->format, data->formattype,
   584                                 temp_buffer);
   585 
   586     if( temp_buffer != pixels ) {
   587         SDL_free(temp_buffer);
   588     }
   589 
   590     renderdata->glDisable(data->type);
   591     result = renderdata->glGetError();
   592     if (result != GL_NO_ERROR) {
   593         GLES_SetError("glTexSubImage2D()", result);
   594         return -1;
   595     }
   596     return 0;
   597 }
   598 
   599 static int
   600 GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   601                  const SDL_Rect * rect, int markDirty, void **pixels,
   602                  int *pitch)
   603 {
   604     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   605 
   606     if (markDirty) {
   607         SDL_AddDirtyRect(&data->dirty, rect);
   608     }
   609 
   610     *pixels =
   611         (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
   612                   rect->x * SDL_BYTESPERPIXEL(texture->format));
   613     *pitch = data->pitch;
   614     return 0;
   615 }
   616 
   617 static void
   618 GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   619 {
   620 }
   621 
   622 static void
   623 GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   624                   int numrects, const SDL_Rect * rects)
   625 {
   626     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   627     int i;
   628 
   629     for (i = 0; i < numrects; ++i) {
   630         SDL_AddDirtyRect(&data->dirty, &rects[i]);
   631     }
   632 }
   633 
   634 static void
   635 GLES_SetBlendMode(GLES_RenderData * data, int blendMode, int isprimitive)
   636 {
   637     if (blendMode != data->blendMode) {
   638         switch (blendMode) {
   639         case SDL_BLENDMODE_NONE:
   640             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   641             data->glDisable(GL_BLEND);
   642             break;
   643         case SDL_BLENDMODE_MASK:
   644             if (isprimitive) {
   645                 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   646                 data->glDisable(GL_BLEND);
   647                 /* The same as SDL_BLENDMODE_NONE */
   648                 blendMode = SDL_BLENDMODE_NONE;
   649                 break;
   650             }
   651             /* fall through */
   652         case SDL_BLENDMODE_BLEND:
   653             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   654             data->glEnable(GL_BLEND);
   655             data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   656             break;
   657         case SDL_BLENDMODE_ADD:
   658             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   659             data->glEnable(GL_BLEND);
   660             data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   661             break;
   662         case SDL_BLENDMODE_MOD:
   663             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   664             data->glEnable(GL_BLEND);
   665             data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   666             break;
   667         }
   668         data->blendMode = blendMode;
   669     }
   670 }
   671 
   672 static int
   673 GLES_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
   674                       int count)
   675 {
   676     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   677     int i;
   678     GLshort *vertices;
   679 
   680     GLES_SetBlendMode(data, renderer->blendMode, 1);
   681 
   682     data->glColor4f((GLfloat) renderer->r * inv255f,
   683                     (GLfloat) renderer->g * inv255f,
   684                     (GLfloat) renderer->b * inv255f,
   685                     (GLfloat) renderer->a * inv255f);
   686 
   687     vertices = SDL_stack_alloc(GLshort, count*2);
   688     for (i = 0; i < count; ++i) {
   689         vertices[2*i+0] = (GLshort)points[i].x;
   690         vertices[2*i+1] = (GLshort)points[i].y;
   691     }
   692     data->glVertexPointer(2, GL_SHORT, 0, vertices);
   693     data->glEnableClientState(GL_VERTEX_ARRAY);
   694     data->glDrawArrays(GL_POINTS, 0, count);
   695     data->glDisableClientState(GL_VERTEX_ARRAY);
   696     SDL_stack_free(vertices);
   697 
   698     return 0;
   699 }
   700 
   701 static int
   702 GLES_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
   703                      int count)
   704 {
   705     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   706     int i;
   707     GLshort *vertices;
   708 
   709     GLES_SetBlendMode(data, renderer->blendMode, 1);
   710 
   711     data->glColor4f((GLfloat) renderer->r * inv255f,
   712                     (GLfloat) renderer->g * inv255f,
   713                     (GLfloat) renderer->b * inv255f,
   714                     (GLfloat) renderer->a * inv255f);
   715 
   716     vertices = SDL_stack_alloc(GLshort, count*2);
   717     for (i = 0; i < count; ++i) {
   718         vertices[2*i+0] = (GLshort)points[i].x;
   719         vertices[2*i+1] = (GLshort)points[i].y;
   720     }
   721     data->glVertexPointer(2, GL_SHORT, 0, vertices);
   722     data->glEnableClientState(GL_VERTEX_ARRAY);
   723     if (count > 2 && 
   724         points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
   725         /* GL_LINE_LOOP takes care of the final segment */
   726         --count;
   727         data->glDrawArrays(GL_LINE_LOOP, 0, count);
   728     } else {
   729         data->glDrawArrays(GL_LINE_STRIP, 0, count);
   730     }
   731     data->glDisableClientState(GL_VERTEX_ARRAY);
   732     SDL_stack_free(vertices);
   733 
   734     return 0;
   735 }
   736 
   737 static int
   738 GLES_RenderDrawRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
   739                      int count)
   740 {
   741     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   742     int i;
   743 
   744     GLES_SetBlendMode(data, renderer->blendMode, 1);
   745 
   746     data->glColor4f((GLfloat) renderer->r * inv255f,
   747                     (GLfloat) renderer->g * inv255f,
   748                     (GLfloat) renderer->b * inv255f,
   749                     (GLfloat) renderer->a * inv255f);
   750 
   751     data->glEnableClientState(GL_VERTEX_ARRAY);
   752     for (i = 0; i < count; ++i) {
   753         const SDL_Rect *rect = rects[i];
   754         GLshort minx = rect->x;
   755         GLshort maxx = rect->x + rect->w;
   756         GLshort miny = rect->y;
   757         GLshort maxy = rect->y + rect->h;
   758         GLshort vertices[8];
   759         vertices[0] = minx;
   760         vertices[1] = miny;
   761         vertices[2] = maxx;
   762         vertices[3] = miny;
   763         vertices[4] = minx;
   764         vertices[5] = maxy;
   765         vertices[6] = maxx;
   766         vertices[7] = maxy;
   767 
   768         data->glVertexPointer(2, GL_SHORT, 0, vertices);
   769         data->glDrawArrays(GL_LINE_LOOP, 0, 4);
   770     }
   771     data->glDisableClientState(GL_VERTEX_ARRAY);
   772 
   773     return 0;
   774 }
   775 
   776 static int
   777 GLES_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
   778                      int count)
   779 {
   780     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   781     int i;
   782 
   783     GLES_SetBlendMode(data, renderer->blendMode, 1);
   784 
   785     data->glColor4f((GLfloat) renderer->r * inv255f,
   786                     (GLfloat) renderer->g * inv255f,
   787                     (GLfloat) renderer->b * inv255f,
   788                     (GLfloat) renderer->a * inv255f);
   789 
   790     data->glEnableClientState(GL_VERTEX_ARRAY);
   791     for (i = 0; i < count; ++i) {
   792         const SDL_Rect *rect = rects[i];
   793         GLshort minx = rect->x;
   794         GLshort maxx = rect->x + rect->w;
   795         GLshort miny = rect->y;
   796         GLshort maxy = rect->y + rect->h;
   797         GLshort vertices[8];
   798         vertices[0] = minx;
   799         vertices[1] = miny;
   800         vertices[2] = maxx;
   801         vertices[3] = miny;
   802         vertices[4] = minx;
   803         vertices[5] = maxy;
   804         vertices[6] = maxx;
   805         vertices[7] = maxy;
   806 
   807         data->glVertexPointer(2, GL_SHORT, 0, vertices);
   808         data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   809     }
   810     data->glDisableClientState(GL_VERTEX_ARRAY);
   811 
   812     return 0;
   813 }
   814 
   815 static int
   816 GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   817                 const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   818 {
   819 
   820     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   821     GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
   822     int minx, miny, maxx, maxy;
   823     GLfloat minu, maxu, minv, maxv;
   824     int i;
   825     void *temp_buffer;          /* used for reformatting dirty rect pixels */
   826     void *temp_ptr;
   827 
   828     data->glEnable(GL_TEXTURE_2D);
   829 
   830     if (texturedata->dirty.list) {
   831         SDL_DirtyRect *dirty;
   832         void *pixels;
   833         int bpp = SDL_BYTESPERPIXEL(texture->format);
   834         int pitch = texturedata->pitch;
   835 
   836         SetupTextureUpdate(data, texture, pitch);
   837 
   838         data->glBindTexture(texturedata->type, texturedata->texture);
   839         for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
   840             SDL_Rect *rect = &dirty->rect;
   841             pixels =
   842                 (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
   843                           rect->x * bpp);
   844             /*      There is no GL_UNPACK_ROW_LENGTH in OpenGLES 
   845                we must do this reformatting ourselves(!)
   846 
   847                maybe it'd be a good idea to keep a temp buffer around
   848                for this purpose rather than allocating it each time
   849              */
   850             if( rect->x == 0 && rect->w * bpp == pitch ) {
   851                 temp_buffer = pixels; /* Updating whole texture, no need to reformat */
   852             } else {
   853                 temp_buffer = SDL_malloc(rect->w * rect->h * bpp);
   854                 temp_ptr = temp_buffer;
   855                 for (i = 0; i < rect->h; i++) {
   856                     SDL_memcpy(temp_ptr, pixels, rect->w * bpp);
   857                     temp_ptr += rect->w * bpp;
   858                     pixels += pitch;
   859                 }
   860             }
   861 
   862             data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
   863                                   rect->w, rect->h, texturedata->format,
   864                                   texturedata->formattype, temp_buffer);
   865 
   866             if( temp_buffer != pixels ) {
   867                 SDL_free(temp_buffer);
   868             }
   869         }
   870         SDL_ClearDirtyRects(&texturedata->dirty);
   871     }
   872 
   873     data->glBindTexture(texturedata->type, texturedata->texture);
   874 
   875     if (texture->modMode) {
   876         data->glColor4f((GLfloat) texture->r * inv255f,
   877                         (GLfloat) texture->g * inv255f,
   878                         (GLfloat) texture->b * inv255f,
   879                         (GLfloat) texture->a * inv255f);
   880     } else {
   881         data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
   882     }
   883 
   884     GLES_SetBlendMode(data, texture->blendMode, 0);
   885 
   886     switch (texture->scaleMode) {
   887     case SDL_SCALEMODE_NONE:
   888     case SDL_SCALEMODE_FAST:
   889         data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
   890                               GL_NEAREST);
   891         data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
   892                               GL_NEAREST);
   893         break;
   894     case SDL_SCALEMODE_SLOW:
   895     case SDL_SCALEMODE_BEST:
   896         data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
   897                               GL_LINEAR);
   898         data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
   899                               GL_LINEAR);
   900         break;
   901     }
   902 
   903     if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
   904         /* this code is a little funny because the viewport is upside down vs SDL's coordinate system */
   905         SDL_Window *window = renderer->window;
   906         GLint cropRect[4];
   907         cropRect[0] = srcrect->x;
   908         cropRect[1] = srcrect->y + srcrect->h;
   909         cropRect[2] = srcrect->w;
   910         cropRect[3] = -srcrect->h;
   911         data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES,
   912                                cropRect);
   913         data->glDrawTexiOES(dstrect->x, window->h - dstrect->y - dstrect->h,
   914                             0, dstrect->w, dstrect->h);
   915     } else {
   916 
   917         minx = dstrect->x;
   918         miny = dstrect->y;
   919         maxx = dstrect->x + dstrect->w;
   920         maxy = dstrect->y + dstrect->h;
   921 
   922         minu = (GLfloat) srcrect->x / texture->w;
   923         minu *= texturedata->texw;
   924         maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
   925         maxu *= texturedata->texw;
   926         minv = (GLfloat) srcrect->y / texture->h;
   927         minv *= texturedata->texh;
   928         maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
   929         maxv *= texturedata->texh;
   930 
   931         GLshort vertices[8];
   932         GLfloat texCoords[8];
   933 
   934         vertices[0] = minx;
   935         vertices[1] = miny;
   936         vertices[2] = maxx;
   937         vertices[3] = miny;
   938         vertices[4] = minx;
   939         vertices[5] = maxy;
   940         vertices[6] = maxx;
   941         vertices[7] = maxy;
   942 
   943         texCoords[0] = minu;
   944         texCoords[1] = minv;
   945         texCoords[2] = maxu;
   946         texCoords[3] = minv;
   947         texCoords[4] = minu;
   948         texCoords[5] = maxv;
   949         texCoords[6] = maxu;
   950         texCoords[7] = maxv;
   951 
   952         data->glVertexPointer(2, GL_SHORT, 0, vertices);
   953         data->glEnableClientState(GL_VERTEX_ARRAY);
   954         data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
   955         data->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   956         data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   957         data->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
   958         data->glDisableClientState(GL_VERTEX_ARRAY);
   959     }
   960 
   961     data->glDisable(GL_TEXTURE_2D);
   962 
   963     return 0;
   964 }
   965 
   966 static void
   967 GLES_RenderPresent(SDL_Renderer * renderer)
   968 {
   969     SDL_GL_SwapWindow(renderer->window);
   970 }
   971 
   972 static void
   973 GLES_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   974 {
   975     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   976 
   977     if (!data) {
   978         return;
   979     }
   980     if (data->texture) {
   981         glDeleteTextures(1, &data->texture);
   982     }
   983     if (data->pixels) {
   984         SDL_free(data->pixels);
   985     }
   986     SDL_FreeDirtyRects(&data->dirty);
   987     SDL_free(data);
   988     texture->driverdata = NULL;
   989 }
   990 
   991 static void
   992 GLES_DestroyRenderer(SDL_Renderer * renderer)
   993 {
   994     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   995 
   996     if (data) {
   997         if (data->context) {
   998             SDL_GL_DeleteContext(data->context);
   999         }
  1000         SDL_free(data);
  1001     }
  1002     SDL_free(renderer);
  1003 }
  1004 
  1005 #endif /* SDL_VIDEO_RENDER_OGL_ES */
  1006 
  1007 /* vi: set ts=4 sw=4 expandtab: */