src/video/SDL_renderer_gles.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 04 Dec 2009 08:45:08 +0000
changeset 3520 83518f8fcd61
parent 3393 fe2f10481878
child 3536 0267b8b1595c
permissions -rw-r--r--
Fixed calls to SDL_AddRenderDriver()
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 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_RenderPoint(SDL_Renderer * renderer, int x, int y);
    89 static int GLES_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2,
    90                            int y2);
    91 static int GLES_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect);
    92 static int GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    93                            const SDL_Rect * srcrect,
    94                            const SDL_Rect * dstrect);
    95 static void GLES_RenderPresent(SDL_Renderer * renderer);
    96 static void GLES_DestroyTexture(SDL_Renderer * renderer,
    97                                 SDL_Texture * texture);
    98 static void GLES_DestroyRenderer(SDL_Renderer * renderer);
    99 
   100 
   101 SDL_RenderDriver GL_ES_RenderDriver = {
   102     GLES_CreateRenderer,
   103     {
   104      "opengl_es",
   105      (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
   106       SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
   107      (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
   108       SDL_TEXTUREMODULATE_ALPHA),
   109      (SDL_BLENDMODE_NONE | SDL_BLENDMODE_MASK |
   110       SDL_BLENDMODE_BLEND | SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD),
   111      (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
   112       SDL_TEXTURESCALEMODE_SLOW), 5,
   113      {
   114       /* OpenGL ES 1.x supported formats list */
   115       SDL_PIXELFORMAT_ABGR4444,
   116       SDL_PIXELFORMAT_ABGR1555,
   117       SDL_PIXELFORMAT_BGR565,
   118       SDL_PIXELFORMAT_BGR24,
   119       SDL_PIXELFORMAT_ABGR8888},
   120      0,
   121      0}
   122 };
   123 
   124 typedef struct
   125 {
   126     SDL_GLContext context;
   127     SDL_bool updateSize;
   128     int blendMode;
   129 
   130 #ifndef APIENTRY
   131 #define APIENTRY
   132 #endif
   133 
   134     SDL_bool useDrawTexture;
   135     SDL_bool GL_OES_draw_texture_supported;
   136 
   137     /* OpenGL ES functions */
   138 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
   139 #include "SDL_glesfuncs.h"
   140 #undef SDL_PROC
   141 
   142 } GLES_RenderData;
   143 
   144 typedef struct
   145 {
   146     GLuint texture;
   147     GLenum type;
   148     GLfloat texw;
   149     GLfloat texh;
   150     GLenum format;
   151     GLenum formattype;
   152     void *pixels;
   153     int pitch;
   154     SDL_DirtyRectList dirty;
   155 } GLES_TextureData;
   156 
   157 static void
   158 GLES_SetError(const char *prefix, GLenum result)
   159 {
   160     const char *error;
   161 
   162     switch (result) {
   163     case GL_NO_ERROR:
   164         error = "GL_NO_ERROR";
   165         break;
   166     case GL_INVALID_ENUM:
   167         error = "GL_INVALID_ENUM";
   168         break;
   169     case GL_INVALID_VALUE:
   170         error = "GL_INVALID_VALUE";
   171         break;
   172     case GL_INVALID_OPERATION:
   173         error = "GL_INVALID_OPERATION";
   174         break;
   175     case GL_STACK_OVERFLOW:
   176         error = "GL_STACK_OVERFLOW";
   177         break;
   178     case GL_STACK_UNDERFLOW:
   179         error = "GL_STACK_UNDERFLOW";
   180         break;
   181     case GL_OUT_OF_MEMORY:
   182         error = "GL_OUT_OF_MEMORY";
   183         break;
   184     default:
   185         error = "UNKNOWN";
   186         break;
   187     }
   188     SDL_SetError("%s: %s", prefix, error);
   189 }
   190 
   191 static int
   192 GLES_LoadFunctions(GLES_RenderData * data)
   193 {
   194 
   195 #define SDL_PROC(ret,func,params) \
   196     data->func = func;
   197 #include "SDL_glesfuncs.h"
   198 #undef SDL_PROC
   199 
   200     return 0;
   201 }
   202 
   203 SDL_Renderer *
   204 GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
   205 {
   206 
   207     SDL_Renderer *renderer;
   208     GLES_RenderData *data;
   209     GLint value;
   210     int doublebuffer;
   211 
   212     if (!(window->flags & SDL_WINDOW_OPENGL)) {
   213         if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
   214             return NULL;
   215         }
   216     }
   217 
   218     renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   219     if (!renderer) {
   220         SDL_OutOfMemory();
   221         return NULL;
   222     }
   223 
   224     data = (GLES_RenderData *) SDL_calloc(1, sizeof(*data));
   225     if (!data) {
   226         GLES_DestroyRenderer(renderer);
   227         SDL_OutOfMemory();
   228         return NULL;
   229     }
   230 
   231     renderer->ActivateRenderer = GLES_ActivateRenderer;
   232     renderer->DisplayModeChanged = GLES_DisplayModeChanged;
   233     renderer->CreateTexture = GLES_CreateTexture;
   234     renderer->QueryTexturePixels = GLES_QueryTexturePixels;
   235     renderer->SetTexturePalette = GLES_SetTexturePalette;
   236     renderer->GetTexturePalette = GLES_GetTexturePalette;
   237     renderer->SetTextureColorMod = GLES_SetTextureColorMod;
   238     renderer->SetTextureAlphaMod = GLES_SetTextureAlphaMod;
   239     renderer->SetTextureBlendMode = GLES_SetTextureBlendMode;
   240     renderer->SetTextureScaleMode = GLES_SetTextureScaleMode;
   241     renderer->UpdateTexture = GLES_UpdateTexture;
   242     renderer->LockTexture = GLES_LockTexture;
   243     renderer->UnlockTexture = GLES_UnlockTexture;
   244     renderer->DirtyTexture = GLES_DirtyTexture;
   245     renderer->RenderPoint = GLES_RenderPoint;
   246     renderer->RenderLine = GLES_RenderLine;
   247     renderer->RenderFill = GLES_RenderFill;
   248     renderer->RenderCopy = GLES_RenderCopy;
   249     renderer->RenderPresent = GLES_RenderPresent;
   250     renderer->DestroyTexture = GLES_DestroyTexture;
   251     renderer->DestroyRenderer = GLES_DestroyRenderer;
   252     renderer->info = GL_ES_RenderDriver.info;
   253     renderer->window = window->id;
   254     renderer->driverdata = data;
   255 
   256     renderer->info.flags =
   257         (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
   258 
   259 #if defined(__QNXNTO__)
   260 #if _NTO_VERSION<=641
   261     /* QNX's OpenGL ES implementation is broken regarding             */
   262     /* packed textures support, affected versions 6.3.2, 6.4.0, 6.4.1 */
   263     renderer->info.num_texture_formats = 2;
   264     renderer->info.texture_formats[0] = SDL_PIXELFORMAT_ABGR8888;
   265     renderer->info.texture_formats[1] = SDL_PIXELFORMAT_BGR24;
   266 #endif /* _NTO_VERSION */
   267 #endif /* __QNXNTO__ */
   268 
   269     if (GLES_LoadFunctions(data) < 0) {
   270         GLES_DestroyRenderer(renderer);
   271         return NULL;
   272     }
   273 
   274     data->context = SDL_GL_CreateContext(window->id);
   275     if (!data->context) {
   276         GLES_DestroyRenderer(renderer);
   277         return NULL;
   278     }
   279     if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
   280         GLES_DestroyRenderer(renderer);
   281         return NULL;
   282     }
   283 
   284     if (flags & SDL_RENDERER_PRESENTVSYNC) {
   285         SDL_GL_SetSwapInterval(1);
   286     } else {
   287         SDL_GL_SetSwapInterval(0);
   288     }
   289     if (SDL_GL_GetSwapInterval() > 0) {
   290         renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   291     }
   292 
   293     if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
   294         if (!doublebuffer) {
   295             renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
   296         }
   297     }
   298 #if SDL_VIDEO_DRIVER_PANDORA
   299     data->GL_OES_draw_texture_supported = SDL_FALSE;
   300     data->useDrawTexture = SDL_FALSE;
   301 #else
   302     if (SDL_GL_ExtensionSupported("GL_OES_draw_texture")) {
   303         data->GL_OES_draw_texture_supported = SDL_TRUE;
   304         data->useDrawTexture = SDL_TRUE;
   305     } else {
   306         data->GL_OES_draw_texture_supported = SDL_FALSE;
   307         data->useDrawTexture = SDL_FALSE;
   308     }
   309 #endif
   310 
   311     data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   312     renderer->info.max_texture_width = value;
   313     data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
   314     renderer->info.max_texture_height = value;
   315 
   316     /* Set up parameters for rendering */
   317     data->blendMode = -1;
   318     data->glDisable(GL_DEPTH_TEST);
   319     data->glDisable(GL_CULL_FACE);
   320     data->updateSize = SDL_TRUE;
   321 
   322     return renderer;
   323 }
   324 
   325 static int
   326 GLES_ActivateRenderer(SDL_Renderer * renderer)
   327 {
   328 
   329     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   330     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   331 
   332     if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
   333         return -1;
   334     }
   335     if (data->updateSize) {
   336         data->glMatrixMode(GL_PROJECTION);
   337         data->glLoadIdentity();
   338         data->glMatrixMode(GL_MODELVIEW);
   339         data->glLoadIdentity();
   340         data->glViewport(0, 0, window->w, window->h);
   341         data->glOrthof(0.0, (GLfloat) window->w, (GLfloat) window->h, 0.0,
   342                        0.0, 1.0);
   343         data->updateSize = SDL_FALSE;
   344     }
   345     return 0;
   346 }
   347 
   348 static int
   349 GLES_DisplayModeChanged(SDL_Renderer * renderer)
   350 {
   351     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   352 
   353     data->updateSize = SDL_TRUE;
   354     return 0;
   355 }
   356 
   357 static __inline__ int
   358 power_of_2(int input)
   359 {
   360     int value = 1;
   361 
   362     while (value < input) {
   363         value <<= 1;
   364     }
   365     return value;
   366 }
   367 
   368 static int
   369 GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   370 {
   371     GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   372     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   373     GLES_TextureData *data;
   374     GLint internalFormat;
   375     GLenum format, type;
   376     int texture_w, texture_h;
   377     GLenum result;
   378 
   379     switch (texture->format) {
   380     case SDL_PIXELFORMAT_BGR24:
   381         internalFormat = GL_RGB;
   382         format = GL_RGB;
   383         type = GL_UNSIGNED_BYTE;
   384         break;
   385     case SDL_PIXELFORMAT_ABGR8888:
   386         internalFormat = GL_RGBA;
   387         format = GL_RGBA;
   388         type = GL_UNSIGNED_BYTE;
   389         break;
   390     case SDL_PIXELFORMAT_BGR565:
   391         internalFormat = GL_RGB;
   392         format = GL_RGB;
   393         type = GL_UNSIGNED_SHORT_5_6_5;
   394         break;
   395     case SDL_PIXELFORMAT_ABGR1555:
   396         internalFormat = GL_RGBA;
   397         format = GL_RGBA;
   398         type = GL_UNSIGNED_SHORT_5_5_5_1;
   399         break;
   400     case SDL_PIXELFORMAT_ABGR4444:
   401         internalFormat = GL_RGBA;
   402         format = GL_RGBA;
   403         type = GL_UNSIGNED_SHORT_4_4_4_4;
   404         break;
   405     default:
   406         SDL_SetError("Unsupported by OpenGL ES texture format");
   407         return -1;
   408     }
   409 
   410     data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data));
   411     if (!data) {
   412         SDL_OutOfMemory();
   413         return -1;
   414     }
   415 
   416     if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   417         data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
   418         data->pixels = SDL_malloc(texture->h * data->pitch);
   419         if (!data->pixels) {
   420             SDL_OutOfMemory();
   421             SDL_free(data);
   422             return -1;
   423         }
   424     }
   425 
   426     texture->driverdata = data;
   427 
   428     renderdata->glGetError();
   429     renderdata->glEnable(GL_TEXTURE_2D);
   430     renderdata->glGenTextures(1, &data->texture);
   431 
   432     data->type = GL_TEXTURE_2D;
   433     /* no NPOV textures allowed in OpenGL ES (yet) */
   434     texture_w = power_of_2(texture->w);
   435     texture_h = power_of_2(texture->h);
   436     data->texw = (GLfloat) texture->w / texture_w;
   437     data->texh = (GLfloat) texture->h / texture_h;
   438 
   439     data->format = format;
   440     data->formattype = type;
   441     renderdata->glBindTexture(data->type, data->texture);
   442     renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
   443                                 GL_NEAREST);
   444     renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
   445                                 GL_NEAREST);
   446     renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
   447                                 GL_CLAMP_TO_EDGE);
   448     renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
   449                                 GL_CLAMP_TO_EDGE);
   450 
   451     renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
   452                              texture_h, 0, format, type, NULL);
   453     renderdata->glDisable(GL_TEXTURE_2D);
   454 
   455     result = renderdata->glGetError();
   456     if (result != GL_NO_ERROR) {
   457         GLES_SetError("glTexImage2D()", result);
   458         return -1;
   459     }
   460     return 0;
   461 }
   462 
   463 static int
   464 GLES_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   465                         void **pixels, int *pitch)
   466 {
   467     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   468 
   469     *pixels = data->pixels;
   470     *pitch = data->pitch;
   471     return 0;
   472 }
   473 
   474 static int
   475 GLES_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   476                        const SDL_Color * colors, int firstcolor, int ncolors)
   477 {
   478     SDL_SetError("OpenGL ES does not support paletted textures");
   479     return -1;
   480 }
   481 
   482 static int
   483 GLES_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   484                        SDL_Color * colors, int firstcolor, int ncolors)
   485 {
   486     SDL_SetError("OpenGL ES does not support paletted textures");
   487     return -1;
   488 }
   489 
   490 static void
   491 SetupTextureUpdate(GLES_RenderData * renderdata, SDL_Texture * texture,
   492                    int pitch)
   493 {
   494     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   495     renderdata->glBindTexture(data->type, data->texture);
   496     renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   497 }
   498 
   499 static int
   500 GLES_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
   501 {
   502     return 0;
   503 }
   504 
   505 static int
   506 GLES_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
   507 {
   508     return 0;
   509 }
   510 
   511 static int
   512 GLES_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
   513 {
   514     switch (texture->blendMode) {
   515     case SDL_BLENDMODE_NONE:
   516     case SDL_BLENDMODE_MASK:
   517     case SDL_BLENDMODE_BLEND:
   518     case SDL_BLENDMODE_ADD:
   519     case SDL_BLENDMODE_MOD:
   520         return 0;
   521     default:
   522         SDL_Unsupported();
   523         texture->blendMode = SDL_BLENDMODE_NONE;
   524         return -1;
   525     }
   526 }
   527 
   528 static int
   529 GLES_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
   530 {
   531     switch (texture->scaleMode) {
   532     case SDL_TEXTURESCALEMODE_NONE:
   533     case SDL_TEXTURESCALEMODE_FAST:
   534     case SDL_TEXTURESCALEMODE_SLOW:
   535         return 0;
   536     case SDL_TEXTURESCALEMODE_BEST:
   537         SDL_Unsupported();
   538         texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
   539         return -1;
   540     default:
   541         SDL_Unsupported();
   542         texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
   543         return -1;
   544     }
   545 }
   546 
   547 static int
   548 GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   549                    const SDL_Rect * rect, const void *pixels, int pitch)
   550 {
   551     GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   552     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   553     GLenum result;
   554 
   555     renderdata->glGetError();
   556     renderdata->glEnable(data->type);
   557     SetupTextureUpdate(renderdata, texture, pitch);
   558     renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
   559                                 rect->h, data->format, data->formattype,
   560                                 pixels);
   561     renderdata->glDisable(data->type);
   562     result = renderdata->glGetError();
   563     if (result != GL_NO_ERROR) {
   564         GLES_SetError("glTexSubImage2D()", result);
   565         return -1;
   566     }
   567     return 0;
   568 }
   569 
   570 static int
   571 GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   572                  const SDL_Rect * rect, int markDirty, void **pixels,
   573                  int *pitch)
   574 {
   575     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   576 
   577     if (markDirty) {
   578         SDL_AddDirtyRect(&data->dirty, rect);
   579     }
   580 
   581     *pixels =
   582         (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
   583                   rect->x * SDL_BYTESPERPIXEL(texture->format));
   584     *pitch = data->pitch;
   585     return 0;
   586 }
   587 
   588 static void
   589 GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   590 {
   591 }
   592 
   593 static void
   594 GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   595                   int numrects, const SDL_Rect * rects)
   596 {
   597     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   598     int i;
   599 
   600     for (i = 0; i < numrects; ++i) {
   601         SDL_AddDirtyRect(&data->dirty, &rects[i]);
   602     }
   603 }
   604 
   605 static void
   606 GLES_SetBlendMode(GLES_RenderData * data, int blendMode, int isprimitive)
   607 {
   608     if (blendMode != data->blendMode) {
   609         switch (blendMode) {
   610         case SDL_BLENDMODE_NONE:
   611             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   612             data->glDisable(GL_BLEND);
   613             break;
   614         case SDL_BLENDMODE_MASK:
   615             if (isprimitive) {
   616                 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   617                 data->glDisable(GL_BLEND);
   618                 /* The same as SDL_BLENDMODE_NONE */
   619                 blendMode = SDL_BLENDMODE_NONE;
   620                 break;
   621             }
   622             /* fall through */
   623         case SDL_BLENDMODE_BLEND:
   624             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   625             data->glEnable(GL_BLEND);
   626             data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   627             break;
   628         case SDL_BLENDMODE_ADD:
   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);
   632             break;
   633         case SDL_BLENDMODE_MOD:
   634             data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   635             data->glEnable(GL_BLEND);
   636             data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
   637             break;
   638         }
   639         data->blendMode = blendMode;
   640     }
   641 }
   642 
   643 static int
   644 GLES_RenderPoint(SDL_Renderer * renderer, int x, int y)
   645 {
   646     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   647 
   648     GLES_SetBlendMode(data, renderer->blendMode, 1);
   649 
   650     data->glColor4f((GLfloat) renderer->r * inv255f,
   651                     (GLfloat) renderer->g * inv255f,
   652                     (GLfloat) renderer->b * inv255f,
   653                     (GLfloat) renderer->a * inv255f);
   654 
   655     GLshort vertices[2];
   656     vertices[0] = x;
   657     vertices[1] = y;
   658 
   659     data->glVertexPointer(2, GL_SHORT, 0, vertices);
   660     data->glEnableClientState(GL_VERTEX_ARRAY);
   661     data->glDrawArrays(GL_POINTS, 0, 1);
   662     data->glDisableClientState(GL_VERTEX_ARRAY);
   663 
   664     return 0;
   665 }
   666 
   667 static int
   668 GLES_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
   669 {
   670     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   671 
   672     GLES_SetBlendMode(data, renderer->blendMode, 1);
   673 
   674     data->glColor4f((GLfloat) renderer->r * inv255f,
   675                     (GLfloat) renderer->g * inv255f,
   676                     (GLfloat) renderer->b * inv255f,
   677                     (GLfloat) renderer->a * inv255f);
   678 
   679     GLshort vertices[4];
   680     vertices[0] = x1;
   681     vertices[1] = y1;
   682     vertices[2] = x2;
   683     vertices[3] = y2;
   684 
   685     data->glVertexPointer(2, GL_SHORT, 0, vertices);
   686     data->glEnableClientState(GL_VERTEX_ARRAY);
   687     data->glDrawArrays(GL_LINES, 0, 2);
   688     data->glDisableClientState(GL_VERTEX_ARRAY);
   689 
   690     return 0;
   691 }
   692 
   693 static int
   694 GLES_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect)
   695 {
   696     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   697 
   698     GLES_SetBlendMode(data, renderer->blendMode, 1);
   699 
   700     data->glColor4f((GLfloat) renderer->r * inv255f,
   701                     (GLfloat) renderer->g * inv255f,
   702                     (GLfloat) renderer->b * inv255f,
   703                     (GLfloat) renderer->a * inv255f);
   704 
   705     GLshort minx = rect->x;
   706     GLshort maxx = rect->x + rect->w;
   707     GLshort miny = rect->y;
   708     GLshort maxy = rect->y + rect->h;
   709 
   710     GLshort vertices[8];
   711     vertices[0] = minx;
   712     vertices[1] = miny;
   713     vertices[2] = maxx;
   714     vertices[3] = miny;
   715     vertices[4] = minx;
   716     vertices[5] = maxy;
   717     vertices[6] = maxx;
   718     vertices[7] = maxy;
   719 
   720     data->glVertexPointer(2, GL_SHORT, 0, vertices);
   721     data->glEnableClientState(GL_VERTEX_ARRAY);
   722     data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   723     data->glDisableClientState(GL_VERTEX_ARRAY);
   724 
   725     return 0;
   726 }
   727 
   728 static int
   729 GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   730                 const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   731 {
   732 
   733     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   734     GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
   735     int minx, miny, maxx, maxy;
   736     GLfloat minu, maxu, minv, maxv;
   737     int i;
   738     void *temp_buffer;          /* used for reformatting dirty rect pixels */
   739     void *temp_ptr;
   740 
   741     data->glEnable(GL_TEXTURE_2D);
   742 
   743     if (texturedata->dirty.list) {
   744         SDL_DirtyRect *dirty;
   745         void *pixels;
   746         int bpp = SDL_BYTESPERPIXEL(texture->format);
   747         int pitch = texturedata->pitch;
   748 
   749         SetupTextureUpdate(data, texture, pitch);
   750 
   751         data->glBindTexture(texturedata->type, texturedata->texture);
   752         for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
   753             SDL_Rect *rect = &dirty->rect;
   754             pixels =
   755                 (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
   756                           rect->x * bpp);
   757             /*      There is no GL_UNPACK_ROW_LENGTH in OpenGLES 
   758                we must do this reformatting ourselves(!)
   759 
   760                maybe it'd be a good idea to keep a temp buffer around
   761                for this purpose rather than allocating it each time
   762              */
   763             temp_buffer = SDL_malloc(rect->w * rect->h * bpp);
   764             temp_ptr = temp_buffer;
   765             for (i = 0; i < rect->h; i++) {
   766                 SDL_memcpy(temp_ptr, pixels, rect->w * bpp);
   767                 temp_ptr += rect->w * bpp;
   768                 pixels += pitch;
   769             }
   770 
   771             data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
   772                                   rect->w, rect->h, texturedata->format,
   773                                   texturedata->formattype, temp_buffer);
   774 
   775             SDL_free(temp_buffer);
   776 
   777         }
   778         SDL_ClearDirtyRects(&texturedata->dirty);
   779     }
   780 
   781     data->glBindTexture(texturedata->type, texturedata->texture);
   782 
   783     if (texture->modMode) {
   784         data->glColor4f((GLfloat) texture->r * inv255f,
   785                         (GLfloat) texture->g * inv255f,
   786                         (GLfloat) texture->b * inv255f,
   787                         (GLfloat) texture->a * inv255f);
   788     } else {
   789         data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
   790     }
   791 
   792     GLES_SetBlendMode(data, texture->blendMode, 0);
   793 
   794     switch (texture->scaleMode) {
   795     case SDL_TEXTURESCALEMODE_NONE:
   796     case SDL_TEXTURESCALEMODE_FAST:
   797         data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
   798                               GL_NEAREST);
   799         data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
   800                               GL_NEAREST);
   801         break;
   802     case SDL_TEXTURESCALEMODE_SLOW:
   803     case SDL_TEXTURESCALEMODE_BEST:
   804         data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
   805                               GL_LINEAR);
   806         data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
   807                               GL_LINEAR);
   808         break;
   809     }
   810 
   811     if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
   812         /* this code is a little funny because the viewport is upside down vs SDL's coordinate system */
   813         SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   814         GLint cropRect[4];
   815         cropRect[0] = srcrect->x;
   816         cropRect[1] = srcrect->y + srcrect->h;
   817         cropRect[2] = srcrect->w;
   818         cropRect[3] = -srcrect->h;
   819         data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES,
   820                                cropRect);
   821         data->glDrawTexiOES(dstrect->x, window->h - dstrect->y - dstrect->h,
   822                             0, dstrect->w, dstrect->h);
   823     } else {
   824 
   825         minx = dstrect->x;
   826         miny = dstrect->y;
   827         maxx = dstrect->x + dstrect->w;
   828         maxy = dstrect->y + dstrect->h;
   829 
   830         minu = (GLfloat) srcrect->x / texture->w;
   831         minu *= texturedata->texw;
   832         maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
   833         maxu *= texturedata->texw;
   834         minv = (GLfloat) srcrect->y / texture->h;
   835         minv *= texturedata->texh;
   836         maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
   837         maxv *= texturedata->texh;
   838 
   839         GLshort vertices[8];
   840         GLfloat texCoords[8];
   841 
   842         vertices[0] = minx;
   843         vertices[1] = miny;
   844         vertices[2] = maxx;
   845         vertices[3] = miny;
   846         vertices[4] = minx;
   847         vertices[5] = maxy;
   848         vertices[6] = maxx;
   849         vertices[7] = maxy;
   850 
   851         texCoords[0] = minu;
   852         texCoords[1] = minv;
   853         texCoords[2] = maxu;
   854         texCoords[3] = minv;
   855         texCoords[4] = minu;
   856         texCoords[5] = maxv;
   857         texCoords[6] = maxu;
   858         texCoords[7] = maxv;
   859 
   860         data->glVertexPointer(2, GL_SHORT, 0, vertices);
   861         data->glEnableClientState(GL_VERTEX_ARRAY);
   862         data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
   863         data->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   864         data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   865         data->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
   866         data->glDisableClientState(GL_VERTEX_ARRAY);
   867     }
   868 
   869     data->glDisable(GL_TEXTURE_2D);
   870 
   871     return 0;
   872 }
   873 
   874 static void
   875 GLES_RenderPresent(SDL_Renderer * renderer)
   876 {
   877     SDL_GL_SwapWindow(renderer->window);
   878 }
   879 
   880 static void
   881 GLES_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   882 {
   883     GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
   884     GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
   885 
   886     if (!data) {
   887         return;
   888     }
   889     if (data->texture) {
   890         glDeleteTextures(1, &data->texture);
   891     }
   892     if (data->pixels) {
   893         SDL_free(data->pixels);
   894     }
   895     SDL_FreeDirtyRects(&data->dirty);
   896     SDL_free(data);
   897     texture->driverdata = NULL;
   898 }
   899 
   900 static void
   901 GLES_DestroyRenderer(SDL_Renderer * renderer)
   902 {
   903     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
   904 
   905     if (data) {
   906         if (data->context) {
   907             SDL_GL_DeleteContext(data->context);
   908         }
   909         SDL_free(data);
   910     }
   911     SDL_free(renderer);
   912 }
   913 
   914 #endif /* SDL_VIDEO_RENDER_OGL_ES */
   915 
   916 /* vi: set ts=4 sw=4 expandtab: */