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