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