src/video/SDL_renderer_gles.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 12 Jan 2011 19:33:04 -0800
changeset 4990 397e748d901a
parent 4986 8431f59485d0
child 5052 4cb4b18cbae3
permissions -rw-r--r--
Improved error message for unsupported texture format
     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 
   561     renderdata->glGetError();
   562     renderdata->glEnable(data->type);
   563     SetupTextureUpdate(renderdata, texture, pitch);
   564     renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
   565                                 rect->h, data->format, data->formattype,
   566                                 pixels);
   567     renderdata->glDisable(data->type);
   568     result = renderdata->glGetError();
   569     if (result != GL_NO_ERROR) {
   570         GLES_SetError("glTexSubImage2D()", result);
   571         return -1;
   572     }
   573     return 0;
   574 }
   575 
   576 static int
   577 GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   578                  const SDL_Rect * rect, int markDirty, void **pixels,
   579                  int *pitch)
   580 {
   581     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   582 
   583     if (markDirty) {
   584         SDL_AddDirtyRect(&data->dirty, rect);
   585     }
   586 
   587     *pixels =
   588         (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
   589                   rect->x * SDL_BYTESPERPIXEL(texture->format));
   590     *pitch = data->pitch;
   591     return 0;
   592 }
   593 
   594 static void
   595 GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   596 {
   597 }
   598 
   599 static void
   600 GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   601                   int numrects, const SDL_Rect * rects)
   602 {
   603     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   604     int i;
   605 
   606     for (i = 0; i < numrects; ++i) {
   607         SDL_AddDirtyRect(&data->dirty, &rects[i]);
   608     }
   609 }
   610 
   611 static void
   612 GLES_SetBlendMode(GLES_RenderData * data, int blendMode, int isprimitive)
   613 {
   614     if (blendMode != data->blendMode) {
   615         switch (blendMode) {
   616         case SDL_BLENDMODE_NONE:
   617             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   618             data->glDisable(GL_BLEND);
   619             break;
   620         case SDL_BLENDMODE_MASK:
   621             if (isprimitive) {
   622                 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   623                 data->glDisable(GL_BLEND);
   624                 /* The same as SDL_BLENDMODE_NONE */
   625                 blendMode = SDL_BLENDMODE_NONE;
   626                 break;
   627             }
   628             /* fall through */
   629         case SDL_BLENDMODE_BLEND:
   630             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   631             data->glEnable(GL_BLEND);
   632             data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   633             break;
   634         case SDL_BLENDMODE_ADD:
   635             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   636             data->glEnable(GL_BLEND);
   637             data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
   638             break;
   639         case SDL_BLENDMODE_MOD:
   640             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   641             data->glEnable(GL_BLEND);
   642             data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   643             break;
   644         }
   645         data->blendMode = blendMode;
   646     }
   647 }
   648 
   649 static int
   650 GLES_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
   651                       int count)
   652 {
   653     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   654     int i;
   655     GLshort *vertices;
   656 
   657     GLES_SetBlendMode(data, renderer->blendMode, 1);
   658 
   659     data->glColor4f((GLfloat) renderer->r * inv255f,
   660                     (GLfloat) renderer->g * inv255f,
   661                     (GLfloat) renderer->b * inv255f,
   662                     (GLfloat) renderer->a * inv255f);
   663 
   664     vertices = SDL_stack_alloc(GLshort, count*2);
   665     for (i = 0; i < count; ++i) {
   666         vertices[2*i+0] = (GLshort)points[i].x;
   667         vertices[2*i+1] = (GLshort)points[i].y;
   668     }
   669     data->glVertexPointer(2, GL_SHORT, 0, vertices);
   670     data->glEnableClientState(GL_VERTEX_ARRAY);
   671     data->glDrawArrays(GL_POINTS, 0, count);
   672     data->glDisableClientState(GL_VERTEX_ARRAY);
   673     SDL_stack_free(vertices);
   674 
   675     return 0;
   676 }
   677 
   678 static int
   679 GLES_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
   680                      int count)
   681 {
   682     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   683     int i;
   684     GLshort *vertices;
   685 
   686     GLES_SetBlendMode(data, renderer->blendMode, 1);
   687 
   688     data->glColor4f((GLfloat) renderer->r * inv255f,
   689                     (GLfloat) renderer->g * inv255f,
   690                     (GLfloat) renderer->b * inv255f,
   691                     (GLfloat) renderer->a * inv255f);
   692 
   693     vertices = SDL_stack_alloc(GLshort, count*2);
   694     for (i = 0; i < count; ++i) {
   695         vertices[2*i+0] = (GLshort)points[i].x;
   696         vertices[2*i+1] = (GLshort)points[i].y;
   697     }
   698     data->glVertexPointer(2, GL_SHORT, 0, vertices);
   699     data->glEnableClientState(GL_VERTEX_ARRAY);
   700     if (count > 2 && 
   701         points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
   702         /* GL_LINE_LOOP takes care of the final segment */
   703         --count;
   704         data->glDrawArrays(GL_LINE_LOOP, 0, count);
   705     } else {
   706         data->glDrawArrays(GL_LINE_STRIP, 0, count);
   707     }
   708     data->glDisableClientState(GL_VERTEX_ARRAY);
   709     SDL_stack_free(vertices);
   710 
   711     return 0;
   712 }
   713 
   714 static int
   715 GLES_RenderDrawRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
   716                      int count)
   717 {
   718     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   719     int i;
   720 
   721     GLES_SetBlendMode(data, renderer->blendMode, 1);
   722 
   723     data->glColor4f((GLfloat) renderer->r * inv255f,
   724                     (GLfloat) renderer->g * inv255f,
   725                     (GLfloat) renderer->b * inv255f,
   726                     (GLfloat) renderer->a * inv255f);
   727 
   728     data->glEnableClientState(GL_VERTEX_ARRAY);
   729     for (i = 0; i < count; ++i) {
   730         const SDL_Rect *rect = rects[i];
   731         GLshort minx = rect->x;
   732         GLshort maxx = rect->x + rect->w;
   733         GLshort miny = rect->y;
   734         GLshort maxy = rect->y + rect->h;
   735         GLshort vertices[8];
   736         vertices[0] = minx;
   737         vertices[1] = miny;
   738         vertices[2] = maxx;
   739         vertices[3] = miny;
   740         vertices[4] = minx;
   741         vertices[5] = maxy;
   742         vertices[6] = maxx;
   743         vertices[7] = maxy;
   744 
   745         data->glVertexPointer(2, GL_SHORT, 0, vertices);
   746         data->glDrawArrays(GL_LINE_LOOP, 0, 4);
   747     }
   748     data->glDisableClientState(GL_VERTEX_ARRAY);
   749 
   750     return 0;
   751 }
   752 
   753 static int
   754 GLES_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
   755                      int count)
   756 {
   757     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   758     int i;
   759 
   760     GLES_SetBlendMode(data, renderer->blendMode, 1);
   761 
   762     data->glColor4f((GLfloat) renderer->r * inv255f,
   763                     (GLfloat) renderer->g * inv255f,
   764                     (GLfloat) renderer->b * inv255f,
   765                     (GLfloat) renderer->a * inv255f);
   766 
   767     data->glEnableClientState(GL_VERTEX_ARRAY);
   768     for (i = 0; i < count; ++i) {
   769         const SDL_Rect *rect = rects[i];
   770         GLshort minx = rect->x;
   771         GLshort maxx = rect->x + rect->w;
   772         GLshort miny = rect->y;
   773         GLshort maxy = rect->y + rect->h;
   774         GLshort vertices[8];
   775         vertices[0] = minx;
   776         vertices[1] = miny;
   777         vertices[2] = maxx;
   778         vertices[3] = miny;
   779         vertices[4] = minx;
   780         vertices[5] = maxy;
   781         vertices[6] = maxx;
   782         vertices[7] = maxy;
   783 
   784         data->glVertexPointer(2, GL_SHORT, 0, vertices);
   785         data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   786     }
   787     data->glDisableClientState(GL_VERTEX_ARRAY);
   788 
   789     return 0;
   790 }
   791 
   792 static int
   793 GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   794                 const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   795 {
   796 
   797     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   798     GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
   799     int minx, miny, maxx, maxy;
   800     GLfloat minu, maxu, minv, maxv;
   801     int i;
   802     void *temp_buffer;          /* used for reformatting dirty rect pixels */
   803     void *temp_ptr;
   804 
   805     data->glEnable(GL_TEXTURE_2D);
   806 
   807     if (texturedata->dirty.list) {
   808         SDL_DirtyRect *dirty;
   809         void *pixels;
   810         int bpp = SDL_BYTESPERPIXEL(texture->format);
   811         int pitch = texturedata->pitch;
   812 
   813         SetupTextureUpdate(data, texture, pitch);
   814 
   815         data->glBindTexture(texturedata->type, texturedata->texture);
   816         for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
   817             SDL_Rect *rect = &dirty->rect;
   818             pixels =
   819                 (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
   820                           rect->x * bpp);
   821             /*      There is no GL_UNPACK_ROW_LENGTH in OpenGLES 
   822                we must do this reformatting ourselves(!)
   823 
   824                maybe it'd be a good idea to keep a temp buffer around
   825                for this purpose rather than allocating it each time
   826              */
   827             if( rect->x == 0 && rect->w * bpp == pitch ) {
   828                 temp_buffer = pixels; /* Updating whole texture, no need to reformat */
   829             } else {
   830                 temp_buffer = SDL_malloc(rect->w * rect->h * bpp);
   831                 temp_ptr = temp_buffer;
   832                 for (i = 0; i < rect->h; i++) {
   833                     SDL_memcpy(temp_ptr, pixels, rect->w * bpp);
   834                     temp_ptr += rect->w * bpp;
   835                     pixels += pitch;
   836                 }
   837             }
   838 
   839             data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
   840                                   rect->w, rect->h, texturedata->format,
   841                                   texturedata->formattype, temp_buffer);
   842 
   843             if( temp_buffer != pixels ) {
   844                 SDL_free(temp_buffer);
   845             }
   846         }
   847         SDL_ClearDirtyRects(&texturedata->dirty);
   848     }
   849 
   850     data->glBindTexture(texturedata->type, texturedata->texture);
   851 
   852     if (texture->modMode) {
   853         data->glColor4f((GLfloat) texture->r * inv255f,
   854                         (GLfloat) texture->g * inv255f,
   855                         (GLfloat) texture->b * inv255f,
   856                         (GLfloat) texture->a * inv255f);
   857     } else {
   858         data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
   859     }
   860 
   861     GLES_SetBlendMode(data, texture->blendMode, 0);
   862 
   863     switch (texture->scaleMode) {
   864     case SDL_SCALEMODE_NONE:
   865     case SDL_SCALEMODE_FAST:
   866         data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
   867                               GL_NEAREST);
   868         data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
   869                               GL_NEAREST);
   870         break;
   871     case SDL_SCALEMODE_SLOW:
   872     case SDL_SCALEMODE_BEST:
   873         data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
   874                               GL_LINEAR);
   875         data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
   876                               GL_LINEAR);
   877         break;
   878     }
   879 
   880     if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
   881         /* this code is a little funny because the viewport is upside down vs SDL's coordinate system */
   882         SDL_Window *window = renderer->window;
   883         GLint cropRect[4];
   884         cropRect[0] = srcrect->x;
   885         cropRect[1] = srcrect->y + srcrect->h;
   886         cropRect[2] = srcrect->w;
   887         cropRect[3] = -srcrect->h;
   888         data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES,
   889                                cropRect);
   890         data->glDrawTexiOES(dstrect->x, window->h - dstrect->y - dstrect->h,
   891                             0, dstrect->w, dstrect->h);
   892     } else {
   893 
   894         minx = dstrect->x;
   895         miny = dstrect->y;
   896         maxx = dstrect->x + dstrect->w;
   897         maxy = dstrect->y + dstrect->h;
   898 
   899         minu = (GLfloat) srcrect->x / texture->w;
   900         minu *= texturedata->texw;
   901         maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
   902         maxu *= texturedata->texw;
   903         minv = (GLfloat) srcrect->y / texture->h;
   904         minv *= texturedata->texh;
   905         maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
   906         maxv *= texturedata->texh;
   907 
   908         GLshort vertices[8];
   909         GLfloat texCoords[8];
   910 
   911         vertices[0] = minx;
   912         vertices[1] = miny;
   913         vertices[2] = maxx;
   914         vertices[3] = miny;
   915         vertices[4] = minx;
   916         vertices[5] = maxy;
   917         vertices[6] = maxx;
   918         vertices[7] = maxy;
   919 
   920         texCoords[0] = minu;
   921         texCoords[1] = minv;
   922         texCoords[2] = maxu;
   923         texCoords[3] = minv;
   924         texCoords[4] = minu;
   925         texCoords[5] = maxv;
   926         texCoords[6] = maxu;
   927         texCoords[7] = maxv;
   928 
   929         data->glVertexPointer(2, GL_SHORT, 0, vertices);
   930         data->glEnableClientState(GL_VERTEX_ARRAY);
   931         data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
   932         data->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   933         data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   934         data->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
   935         data->glDisableClientState(GL_VERTEX_ARRAY);
   936     }
   937 
   938     data->glDisable(GL_TEXTURE_2D);
   939 
   940     return 0;
   941 }
   942 
   943 static void
   944 GLES_RenderPresent(SDL_Renderer * renderer)
   945 {
   946     SDL_GL_SwapWindow(renderer->window);
   947 }
   948 
   949 static void
   950 GLES_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   951 {
   952     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   953 
   954     if (!data) {
   955         return;
   956     }
   957     if (data->texture) {
   958         glDeleteTextures(1, &data->texture);
   959     }
   960     if (data->pixels) {
   961         SDL_free(data->pixels);
   962     }
   963     SDL_FreeDirtyRects(&data->dirty);
   964     SDL_free(data);
   965     texture->driverdata = NULL;
   966 }
   967 
   968 static void
   969 GLES_DestroyRenderer(SDL_Renderer * renderer)
   970 {
   971     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   972 
   973     if (data) {
   974         if (data->context) {
   975             SDL_GL_DeleteContext(data->context);
   976         }
   977         SDL_free(data);
   978     }
   979     SDL_free(renderer);
   980 }
   981 
   982 #endif /* SDL_VIDEO_RENDER_OGL_ES */
   983 
   984 /* vi: set ts=4 sw=4 expandtab: */