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