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