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