src/video/SDL_renderer_gles.c
author Mike Gorchak <lestat@i.com.ua>
Tue, 13 Oct 2009 06:28:31 +0000
changeset 3389 3fe2248fde37
parent 3169 f294338ca6eb
child 3393 fe2f10481878
permissions -rw-r--r--
Fixed OpenGL ES primitive alpha blending.
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
void
hfutrell@2739
   204
GLES_AddRenderDriver(_THIS)
hfutrell@2739
   205
{
hfutrell@2739
   206
    if (_this->GL_CreateContext) {
hfutrell@2739
   207
        SDL_AddRenderDriver(0, &GL_ES_RenderDriver);
hfutrell@2739
   208
    }
hfutrell@2739
   209
}
hfutrell@2739
   210
hfutrell@2739
   211
SDL_Renderer *
hfutrell@2739
   212
GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
hfutrell@2739
   213
{
slouken@2753
   214
hfutrell@2739
   215
    SDL_Renderer *renderer;
hfutrell@2739
   216
    GLES_RenderData *data;
hfutrell@2739
   217
    GLint value;
hfutrell@2739
   218
    int doublebuffer;
hfutrell@2739
   219
hfutrell@2739
   220
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
hfutrell@2739
   221
        if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
hfutrell@2739
   222
            return NULL;
hfutrell@2739
   223
        }
hfutrell@2739
   224
    }
hfutrell@2739
   225
hfutrell@2739
   226
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
hfutrell@2739
   227
    if (!renderer) {
hfutrell@2739
   228
        SDL_OutOfMemory();
hfutrell@2739
   229
        return NULL;
hfutrell@2739
   230
    }
hfutrell@2739
   231
hfutrell@2739
   232
    data = (GLES_RenderData *) SDL_calloc(1, sizeof(*data));
hfutrell@2739
   233
    if (!data) {
hfutrell@2739
   234
        GLES_DestroyRenderer(renderer);
hfutrell@2739
   235
        SDL_OutOfMemory();
hfutrell@2739
   236
        return NULL;
hfutrell@2739
   237
    }
hfutrell@2739
   238
hfutrell@2739
   239
    renderer->ActivateRenderer = GLES_ActivateRenderer;
hfutrell@2739
   240
    renderer->DisplayModeChanged = GLES_DisplayModeChanged;
hfutrell@2739
   241
    renderer->CreateTexture = GLES_CreateTexture;
hfutrell@2739
   242
    renderer->QueryTexturePixels = GLES_QueryTexturePixels;
hfutrell@2739
   243
    renderer->SetTexturePalette = GLES_SetTexturePalette;
hfutrell@2739
   244
    renderer->GetTexturePalette = GLES_GetTexturePalette;
hfutrell@2739
   245
    renderer->SetTextureColorMod = GLES_SetTextureColorMod;
hfutrell@2739
   246
    renderer->SetTextureAlphaMod = GLES_SetTextureAlphaMod;
hfutrell@2739
   247
    renderer->SetTextureBlendMode = GLES_SetTextureBlendMode;
hfutrell@2739
   248
    renderer->SetTextureScaleMode = GLES_SetTextureScaleMode;
hfutrell@2739
   249
    renderer->UpdateTexture = GLES_UpdateTexture;
hfutrell@2739
   250
    renderer->LockTexture = GLES_LockTexture;
hfutrell@2739
   251
    renderer->UnlockTexture = GLES_UnlockTexture;
hfutrell@2739
   252
    renderer->DirtyTexture = GLES_DirtyTexture;
slouken@2936
   253
    renderer->RenderPoint = GLES_RenderPoint;
slouken@2936
   254
    renderer->RenderLine = GLES_RenderLine;
hfutrell@2739
   255
    renderer->RenderFill = GLES_RenderFill;
hfutrell@2739
   256
    renderer->RenderCopy = GLES_RenderCopy;
hfutrell@2739
   257
    renderer->RenderPresent = GLES_RenderPresent;
hfutrell@2739
   258
    renderer->DestroyTexture = GLES_DestroyTexture;
hfutrell@2739
   259
    renderer->DestroyRenderer = GLES_DestroyRenderer;
hfutrell@2739
   260
    renderer->info = GL_ES_RenderDriver.info;
hfutrell@2739
   261
    renderer->window = window->id;
hfutrell@2739
   262
    renderer->driverdata = data;
slouken@2753
   263
slouken@2753
   264
    renderer->info.flags =
slouken@2753
   265
        (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
hfutrell@2739
   266
lestat@3165
   267
#if defined(__QNXNTO__)
lestat@3165
   268
#if _NTO_VERSION<=641
lestat@3165
   269
    /* QNX's OpenGL ES implementation is broken regarding             */
lestat@3165
   270
    /* packed textures support, affected versions 6.3.2, 6.4.0, 6.4.1 */
slouken@3169
   271
    renderer->info.num_texture_formats = 2;
slouken@3169
   272
    renderer->info.texture_formats[0] = SDL_PIXELFORMAT_ABGR8888;
slouken@3169
   273
    renderer->info.texture_formats[1] = SDL_PIXELFORMAT_BGR24;
lestat@3165
   274
#endif /* _NTO_VERSION */
lestat@3165
   275
#endif /* __QNXNTO__ */
lestat@3165
   276
hfutrell@2739
   277
    if (GLES_LoadFunctions(data) < 0) {
hfutrell@2739
   278
        GLES_DestroyRenderer(renderer);
hfutrell@2739
   279
        return NULL;
hfutrell@2739
   280
    }
hfutrell@2739
   281
hfutrell@2739
   282
    data->context = SDL_GL_CreateContext(window->id);
hfutrell@2739
   283
    if (!data->context) {
hfutrell@2739
   284
        GLES_DestroyRenderer(renderer);
hfutrell@2739
   285
        return NULL;
hfutrell@2739
   286
    }
hfutrell@2739
   287
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
hfutrell@2739
   288
        GLES_DestroyRenderer(renderer);
hfutrell@2739
   289
        return NULL;
hfutrell@2739
   290
    }
hfutrell@2739
   291
hfutrell@2739
   292
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
hfutrell@2739
   293
        SDL_GL_SetSwapInterval(1);
hfutrell@2739
   294
    } else {
hfutrell@2739
   295
        SDL_GL_SetSwapInterval(0);
hfutrell@2739
   296
    }
hfutrell@2739
   297
    if (SDL_GL_GetSwapInterval() > 0) {
hfutrell@2739
   298
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
hfutrell@2739
   299
    }
hfutrell@2739
   300
hfutrell@2739
   301
    if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
hfutrell@2739
   302
        if (!doublebuffer) {
hfutrell@2739
   303
            renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
hfutrell@2739
   304
        }
hfutrell@2739
   305
    }
slouken@3161
   306
#if SDL_VIDEO_DRIVER_PANDORA
slouken@3161
   307
    data->GL_OES_draw_texture_supported = SDL_FALSE;
slouken@3161
   308
    data->useDrawTexture = SDL_FALSE;
slouken@3161
   309
#else
slouken@2753
   310
    if (SDL_GL_ExtensionSupported("GL_OES_draw_texture")) {
slouken@2753
   311
        data->GL_OES_draw_texture_supported = SDL_TRUE;
slouken@2753
   312
        data->useDrawTexture = SDL_TRUE;
slouken@2753
   313
    } else {
slouken@2753
   314
        data->GL_OES_draw_texture_supported = SDL_FALSE;
slouken@2753
   315
        data->useDrawTexture = SDL_FALSE;
slouken@2753
   316
    }
slouken@3161
   317
#endif
hfutrell@2739
   318
hfutrell@2739
   319
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
hfutrell@2739
   320
    renderer->info.max_texture_width = value;
hfutrell@2739
   321
    data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
hfutrell@2739
   322
    renderer->info.max_texture_height = value;
hfutrell@2739
   323
hfutrell@2739
   324
    /* Set up parameters for rendering */
hfutrell@2739
   325
    data->blendMode = -1;
hfutrell@2739
   326
    data->glDisable(GL_DEPTH_TEST);
hfutrell@2739
   327
    data->glDisable(GL_CULL_FACE);
hfutrell@2739
   328
    data->updateSize = SDL_TRUE;
hfutrell@2739
   329
hfutrell@2739
   330
    return renderer;
hfutrell@2739
   331
}
hfutrell@2739
   332
slouken@2753
   333
static int
slouken@2753
   334
GLES_ActivateRenderer(SDL_Renderer * renderer)
hfutrell@2739
   335
{
slouken@2753
   336
slouken@2753
   337
    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
hfutrell@2739
   338
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@2753
   339
hfutrell@2739
   340
    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
hfutrell@2739
   341
        return -1;
hfutrell@2739
   342
    }
hfutrell@2739
   343
    if (data->updateSize) {
hfutrell@2739
   344
        data->glMatrixMode(GL_PROJECTION);
hfutrell@2739
   345
        data->glLoadIdentity();
hfutrell@2739
   346
        data->glMatrixMode(GL_MODELVIEW);
hfutrell@2739
   347
        data->glLoadIdentity();
hfutrell@2739
   348
        data->glViewport(0, 0, window->w, window->h);
slouken@2753
   349
        data->glOrthof(0.0, (GLfloat) window->w, (GLfloat) window->h, 0.0,
slouken@2753
   350
                       0.0, 1.0);
hfutrell@2739
   351
        data->updateSize = SDL_FALSE;
hfutrell@2739
   352
    }
hfutrell@2739
   353
    return 0;
hfutrell@2739
   354
}
hfutrell@2739
   355
hfutrell@2739
   356
static int
hfutrell@2739
   357
GLES_DisplayModeChanged(SDL_Renderer * renderer)
hfutrell@2739
   358
{
hfutrell@2739
   359
    GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
hfutrell@2739
   360
hfutrell@2739
   361
    data->updateSize = SDL_TRUE;
hfutrell@2739
   362
    return 0;
hfutrell@2739
   363
}
hfutrell@2739
   364
hfutrell@2739
   365
static __inline__ int
hfutrell@2739
   366
power_of_2(int input)
hfutrell@2739
   367
{
hfutrell@2739
   368
    int value = 1;
hfutrell@2739
   369
hfutrell@2739
   370
    while (value < input) {
hfutrell@2739
   371
        value <<= 1;
hfutrell@2739
   372
    }
hfutrell@2739
   373
    return value;
hfutrell@2739
   374
}
hfutrell@2739
   375
hfutrell@2739
   376
static int
slouken@3139
   377
GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
hfutrell@2739
   378
{
hfutrell@2739
   379
    GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
hfutrell@2739
   380
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
hfutrell@2739
   381
    GLES_TextureData *data;
hfutrell@2739
   382
    GLint internalFormat;
hfutrell@2739
   383
    GLenum format, type;
hfutrell@2739
   384
    int texture_w, texture_h;
hfutrell@2739
   385
    GLenum result;
slouken@3099
   386
slouken@2753
   387
    switch (texture->format) {
slouken@2753
   388
    case SDL_PIXELFORMAT_BGR24:
slouken@3139
   389
        internalFormat = GL_RGB;
slouken@3139
   390
        format = GL_RGB;
slouken@3139
   391
        type = GL_UNSIGNED_BYTE;
slouken@3139
   392
        break;
slouken@2753
   393
    case SDL_PIXELFORMAT_ABGR8888:
slouken@3139
   394
        internalFormat = GL_RGBA;
slouken@3139
   395
        format = GL_RGBA;
slouken@3139
   396
        type = GL_UNSIGNED_BYTE;
slouken@3139
   397
        break;
lestat@3163
   398
    case SDL_PIXELFORMAT_BGR565:
lestat@3163
   399
        internalFormat = GL_RGB;
lestat@3163
   400
        format = GL_RGB;
lestat@3163
   401
        type = GL_UNSIGNED_SHORT_5_6_5;
lestat@3163
   402
        break;
lestat@3163
   403
    case SDL_PIXELFORMAT_ABGR1555:
lestat@3163
   404
        internalFormat = GL_RGBA;
lestat@3163
   405
        format = GL_RGBA;
lestat@3163
   406
        type = GL_UNSIGNED_SHORT_5_5_5_1;
lestat@3163
   407
        break;
lestat@3163
   408
    case SDL_PIXELFORMAT_ABGR4444:
lestat@3163
   409
        internalFormat = GL_RGBA;
lestat@3163
   410
        format = GL_RGBA;
lestat@3163
   411
        type = GL_UNSIGNED_SHORT_4_4_4_4;
lestat@3163
   412
        break;
slouken@2753
   413
    default:
lestat@3165
   414
        SDL_SetError("Unsupported by OpenGL ES texture format");
slouken@2753
   415
        return -1;
hfutrell@2739
   416
    }
slouken@2753
   417
slouken@2753
   418
    data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data));
hfutrell@2739
   419
    if (!data) {
hfutrell@2739
   420
        SDL_OutOfMemory();
hfutrell@2739
   421
        return -1;
hfutrell@2739
   422
    }
hfutrell@2739
   423
hfutrell@2739
   424
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
hfutrell@2739
   425
        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
hfutrell@2739
   426
        data->pixels = SDL_malloc(texture->h * data->pitch);
hfutrell@2739
   427
        if (!data->pixels) {
hfutrell@2739
   428
            SDL_OutOfMemory();
hfutrell@2739
   429
            SDL_free(data);
hfutrell@2739
   430
            return -1;
hfutrell@2739
   431
        }
hfutrell@2739
   432
    }
hfutrell@2739
   433
hfutrell@2739
   434
    texture->driverdata = data;
hfutrell@2739
   435
hfutrell@2739
   436
    renderdata->glGetError();
lestat@3122
   437
    renderdata->glEnable(GL_TEXTURE_2D);
hfutrell@2739
   438
    renderdata->glGenTextures(1, &data->texture);
slouken@2753
   439
slouken@2753
   440
    data->type = GL_TEXTURE_2D;
slouken@2753
   441
    /* no NPOV textures allowed in OpenGL ES (yet) */
slouken@2753
   442
    texture_w = power_of_2(texture->w);
slouken@2753
   443
    texture_h = power_of_2(texture->h);
slouken@2753
   444
    data->texw = (GLfloat) texture->w / texture_w;
slouken@2753
   445
    data->texh = (GLfloat) texture->h / texture_h;
slouken@2753
   446
hfutrell@2739
   447
    data->format = format;
hfutrell@2739
   448
    data->formattype = type;
hfutrell@2739
   449
    renderdata->glBindTexture(data->type, data->texture);
hfutrell@2739
   450
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
hfutrell@2739
   451
                                GL_NEAREST);
hfutrell@2739
   452
    renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
hfutrell@2739
   453
                                GL_NEAREST);
hfutrell@2739
   454
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
hfutrell@2739
   455
                                GL_CLAMP_TO_EDGE);
hfutrell@2739
   456
    renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
hfutrell@2739
   457
                                GL_CLAMP_TO_EDGE);
slouken@2753
   458
slouken@2753
   459
    renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
slouken@2753
   460
                             texture_h, 0, format, type, NULL);
lestat@3122
   461
    renderdata->glDisable(GL_TEXTURE_2D);
hfutrell@2739
   462
hfutrell@2739
   463
    result = renderdata->glGetError();
hfutrell@2739
   464
    if (result != GL_NO_ERROR) {
hfutrell@2739
   465
        GLES_SetError("glTexImage2D()", result);
hfutrell@2739
   466
        return -1;
hfutrell@2739
   467
    }
hfutrell@2739
   468
    return 0;
hfutrell@2739
   469
}
hfutrell@2739
   470
hfutrell@2739
   471
static int
hfutrell@2739
   472
GLES_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2753
   473
                        void **pixels, int *pitch)
hfutrell@2739
   474
{
hfutrell@2739
   475
    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
hfutrell@2739
   476
hfutrell@2739
   477
    *pixels = data->pixels;
hfutrell@2739
   478
    *pitch = data->pitch;
hfutrell@2739
   479
    return 0;
hfutrell@2739
   480
}
hfutrell@2739
   481
hfutrell@2739
   482
static int
hfutrell@2739
   483
GLES_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2753
   484
                       const 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 int
hfutrell@2739
   491
GLES_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2753
   492
                       SDL_Color * colors, int firstcolor, int ncolors)
hfutrell@2739
   493
{
slouken@2753
   494
    SDL_SetError("OpenGL ES does not support paletted textures");
hfutrell@2739
   495
    return -1;
hfutrell@2739
   496
}
hfutrell@2739
   497
hfutrell@2739
   498
static void
hfutrell@2739
   499
SetupTextureUpdate(GLES_RenderData * renderdata, SDL_Texture * texture,
hfutrell@2739
   500
                   int pitch)
hfutrell@2739
   501
{
slouken@2753
   502
    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
slouken@2753
   503
    renderdata->glBindTexture(data->type, data->texture);
hfutrell@2739
   504
    renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
hfutrell@2739
   505
}
hfutrell@2739
   506
hfutrell@2739
   507
static int
hfutrell@2739
   508
GLES_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
hfutrell@2739
   509
{
hfutrell@2739
   510
    return 0;
hfutrell@2739
   511
}
hfutrell@2739
   512
hfutrell@2739
   513
static int
hfutrell@2739
   514
GLES_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
hfutrell@2739
   515
{
hfutrell@2739
   516
    return 0;
hfutrell@2739
   517
}
hfutrell@2739
   518
hfutrell@2739
   519
static int
hfutrell@2739
   520
GLES_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
hfutrell@2739
   521
{
hfutrell@2739
   522
    switch (texture->blendMode) {
slouken@2884
   523
    case SDL_BLENDMODE_NONE:
slouken@2884
   524
    case SDL_BLENDMODE_MASK:
slouken@2884
   525
    case SDL_BLENDMODE_BLEND:
slouken@2884
   526
    case SDL_BLENDMODE_ADD:
slouken@2884
   527
    case SDL_BLENDMODE_MOD:
hfutrell@2739
   528
        return 0;
hfutrell@2739
   529
    default:
hfutrell@2739
   530
        SDL_Unsupported();
slouken@2884
   531
        texture->blendMode = SDL_BLENDMODE_NONE;
hfutrell@2739
   532
        return -1;
hfutrell@2739
   533
    }
hfutrell@2739
   534
}
hfutrell@2739
   535
hfutrell@2739
   536
static int
hfutrell@2739
   537
GLES_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
hfutrell@2739
   538
{
hfutrell@2739
   539
    switch (texture->scaleMode) {
hfutrell@2739
   540
    case SDL_TEXTURESCALEMODE_NONE:
hfutrell@2739
   541
    case SDL_TEXTURESCALEMODE_FAST:
hfutrell@2739
   542
    case SDL_TEXTURESCALEMODE_SLOW:
hfutrell@2739
   543
        return 0;
hfutrell@2739
   544
    case SDL_TEXTURESCALEMODE_BEST:
hfutrell@2739
   545
        SDL_Unsupported();
slouken@2753
   546
        texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
hfutrell@2739
   547
        return -1;
hfutrell@2739
   548
    default:
hfutrell@2739
   549
        SDL_Unsupported();
hfutrell@2739
   550
        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
hfutrell@2739
   551
        return -1;
hfutrell@2739
   552
    }
hfutrell@2739
   553
}
hfutrell@2739
   554
hfutrell@2739
   555
static int
slouken@3139
   556
GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@3139
   557
                   const SDL_Rect * rect, const void *pixels, int pitch)
hfutrell@2739
   558
{
hfutrell@2739
   559
    GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
hfutrell@2739
   560
    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
hfutrell@2739
   561
    GLenum result;
hfutrell@2739
   562
lestat@3122
   563
    renderdata->glGetError();
lestat@3122
   564
    renderdata->glEnable(data->type);
hfutrell@2739
   565
    SetupTextureUpdate(renderdata, texture, pitch);
slouken@2753
   566
    renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
hfutrell@2739
   567
                                rect->h, data->format, data->formattype,
hfutrell@2739
   568
                                pixels);
lestat@3122
   569
    renderdata->glDisable(data->type);
hfutrell@2739
   570
    result = renderdata->glGetError();
hfutrell@2739
   571
    if (result != GL_NO_ERROR) {
hfutrell@2739
   572
        GLES_SetError("glTexSubImage2D()", result);
hfutrell@2739
   573
        return -1;
hfutrell@2739
   574
    }
hfutrell@2739
   575
    return 0;
hfutrell@2739
   576
}
hfutrell@2739
   577
hfutrell@2739
   578
static int
hfutrell@2739
   579
GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2753
   580
                 const SDL_Rect * rect, int markDirty, void **pixels,
slouken@2753
   581
                 int *pitch)
hfutrell@2739
   582
{
hfutrell@2739
   583
    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
hfutrell@2739
   584
hfutrell@2739
   585
    if (markDirty) {
hfutrell@2739
   586
        SDL_AddDirtyRect(&data->dirty, rect);
hfutrell@2739
   587
    }
hfutrell@2739
   588
hfutrell@2739
   589
    *pixels =
hfutrell@2739
   590
        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
hfutrell@2739
   591
                  rect->x * SDL_BYTESPERPIXEL(texture->format));
hfutrell@2739
   592
    *pitch = data->pitch;
hfutrell@2739
   593
    return 0;
hfutrell@2739
   594
}
hfutrell@2739
   595
hfutrell@2739
   596
static void
hfutrell@2739
   597
GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
hfutrell@2739
   598
{
hfutrell@2739
   599
}
hfutrell@2739
   600
hfutrell@2739
   601
static void
slouken@2753
   602
GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2753
   603
                  int numrects, const SDL_Rect * rects)
hfutrell@2739
   604
{
hfutrell@2739
   605
    GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
hfutrell@2739
   606
    int i;
hfutrell@2739
   607
hfutrell@2739
   608
    for (i = 0; i < numrects; ++i) {
hfutrell@2739
   609
        SDL_AddDirtyRect(&data->dirty, &rects[i]);
hfutrell@2739
   610
    }
hfutrell@2739
   611
}
hfutrell@2739
   612
slouken@2936
   613
static void
slouken@2936
   614
GLES_SetBlendMode(GLES_RenderData * data, int blendMode)
slouken@2936
   615
{
slouken@2936
   616
    if (blendMode != data->blendMode) {
slouken@2936
   617
        switch (blendMode) {
slouken@2936
   618
        case SDL_BLENDMODE_NONE:
slouken@2936
   619
            data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
slouken@2936
   620
            data->glDisable(GL_BLEND);
slouken@2936
   621
            break;
slouken@2936
   622
        case SDL_BLENDMODE_MASK:
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
slouken@2936
   648
    GLES_SetBlendMode(data, renderer->blendMode);
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
slouken@2936
   672
    GLES_SetBlendMode(data, renderer->blendMode);
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
slouken@2936
   698
    GLES_SetBlendMode(data, renderer->blendMode);
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
slouken@2936
   792
    GLES_SetBlendMode(data, texture->blendMode);
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: */