Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
1120 lines (944 loc) · 33 KB

SDL_render_gles.c

File metadata and controls

1120 lines (944 loc) · 33 KB
 
Jun 22, 2011
Jun 22, 2011
1
2
/*
Simple DirectMedia Layer
Dec 31, 2011
Dec 31, 2011
3
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
Jun 22, 2011
Jun 22, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
#if SDL_VIDEO_RENDER_OGL_ES && !SDL_RENDER_DISABLED
#include "SDL_hints.h"
#include "SDL_opengles.h"
#include "../SDL_sysrender.h"
#if defined(SDL_VIDEO_DRIVER_PANDORA)
/* Empty function stub to get OpenGL ES 1.x support without */
/* OpenGL ES extension GL_OES_draw_texture supported */
GL_API void GL_APIENTRY
glDrawTexiOES(GLint x, GLint y, GLint z, GLint width, GLint height)
{
return;
}
#endif /* PANDORA */
/* OpenGL ES 1.1 renderer implementation, based on the OpenGL renderer */
Jan 8, 2012
Jan 8, 2012
43
/* Used to re-create the window with OpenGL ES capability */
Jan 8, 2012
Jan 8, 2012
44
45
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
Jun 22, 2011
Jun 22, 2011
46
47
48
49
50
51
52
53
54
55
56
57
58
static const float inv255f = 1.0f / 255.0f;
static SDL_Renderer *GLES_CreateRenderer(SDL_Window * window, Uint32 flags);
static void GLES_WindowEvent(SDL_Renderer * renderer,
const SDL_WindowEvent *event);
static int GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static int GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels,
int pitch);
static int GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch);
static void GLES_UnlockTexture(SDL_Renderer * renderer,
SDL_Texture * texture);
Jan 22, 2012
Jan 22, 2012
59
static int GLES_SetRenderTarget(SDL_Renderer * renderer,
Jan 22, 2012
Jan 22, 2012
60
SDL_Texture * texture);
Jun 22, 2011
Jun 22, 2011
61
62
63
static int GLES_UpdateViewport(SDL_Renderer * renderer);
static int GLES_RenderClear(SDL_Renderer * renderer);
static int GLES_RenderDrawPoints(SDL_Renderer * renderer,
Oct 2, 2012
Oct 2, 2012
64
const SDL_FPoint * points, int count);
Jun 22, 2011
Jun 22, 2011
65
static int GLES_RenderDrawLines(SDL_Renderer * renderer,
Oct 2, 2012
Oct 2, 2012
66
const SDL_FPoint * points, int count);
Jun 22, 2011
Jun 22, 2011
67
static int GLES_RenderFillRects(SDL_Renderer * renderer,
Oct 2, 2012
Oct 2, 2012
68
const SDL_FRect * rects, int count);
Jun 22, 2011
Jun 22, 2011
69
70
static int GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect,
Oct 2, 2012
Oct 2, 2012
71
72
73
74
const SDL_FRect * dstrect);
static int GLES_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip);
Oct 31, 2011
Oct 31, 2011
75
76
static int GLES_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 pixel_format, void * pixels, int pitch);
Jun 22, 2011
Jun 22, 2011
77
78
79
80
static void GLES_RenderPresent(SDL_Renderer * renderer);
static void GLES_DestroyTexture(SDL_Renderer * renderer,
SDL_Texture * texture);
static void GLES_DestroyRenderer(SDL_Renderer * renderer);
Sep 3, 2012
Sep 3, 2012
81
82
static int GLES_BindTexture (SDL_Renderer * renderer, SDL_Texture *texture, float *texw, float *texh);
static int GLES_UnbindTexture (SDL_Renderer * renderer, SDL_Texture *texture);
Jan 19, 2012
Jan 19, 2012
83
84
85
86
87
88
89
90
91
typedef struct GLES_FBOList GLES_FBOList;
struct GLES_FBOList
{
Uint32 w, h;
GLuint FBO;
GLES_FBOList *next;
};
Jun 22, 2011
Jun 22, 2011
92
93
94
95
96
97
SDL_RenderDriver GLES_RenderDriver = {
GLES_CreateRenderer,
{
"opengles",
Jan 20, 2012
Jan 20, 2012
98
(SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
Jun 22, 2011
Jun 22, 2011
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
1,
{SDL_PIXELFORMAT_ABGR8888},
0,
0}
};
typedef struct
{
SDL_GLContext context;
struct {
Uint32 color;
int blendMode;
SDL_bool tex_coords;
} current;
Jan 8, 2012
Jan 8, 2012
114
115
116
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
#include "SDL_glesfuncs.h"
#undef SDL_PROC
Jan 19, 2012
Jan 19, 2012
117
118
SDL_bool GL_OES_framebuffer_object_supported;
GLES_FBOList *framebuffers;
Jan 31, 2012
Jan 31, 2012
119
GLuint window_framebuffer;
Jan 8, 2012
Jan 8, 2012
120
Jun 22, 2011
Jun 22, 2011
121
122
123
124
125
126
127
128
129
130
131
132
133
134
SDL_bool useDrawTexture;
SDL_bool GL_OES_draw_texture_supported;
} GLES_RenderData;
typedef struct
{
GLuint texture;
GLenum type;
GLfloat texw;
GLfloat texh;
GLenum format;
GLenum formattype;
void *pixels;
int pitch;
Jan 19, 2012
Jan 19, 2012
135
GLES_FBOList *fbo;
Jun 22, 2011
Jun 22, 2011
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
} GLES_TextureData;
static void
GLES_SetError(const char *prefix, GLenum result)
{
const char *error;
switch (result) {
case GL_NO_ERROR:
error = "GL_NO_ERROR";
break;
case GL_INVALID_ENUM:
error = "GL_INVALID_ENUM";
break;
case GL_INVALID_VALUE:
error = "GL_INVALID_VALUE";
break;
case GL_INVALID_OPERATION:
error = "GL_INVALID_OPERATION";
break;
case GL_STACK_OVERFLOW:
error = "GL_STACK_OVERFLOW";
break;
case GL_STACK_UNDERFLOW:
error = "GL_STACK_UNDERFLOW";
break;
case GL_OUT_OF_MEMORY:
error = "GL_OUT_OF_MEMORY";
break;
default:
error = "UNKNOWN";
break;
}
SDL_SetError("%s: %s", prefix, error);
}
Jan 8, 2012
Jan 8, 2012
172
173
static int GLES_LoadFunctions(GLES_RenderData * data)
{
Jan 8, 2012
Jan 8, 2012
174
175
176
177
178
179
180
181
#if SDL_VIDEO_DRIVER_UIKIT
#define __SDL_NOGETPROCADDR__
#elif SDL_VIDEO_DRIVER_ANDROID
#define __SDL_NOGETPROCADDR__
#elif SDL_VIDEO_DRIVER_PANDORA
#define __SDL_NOGETPROCADDR__
#endif
Jan 8, 2012
Jan 8, 2012
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#ifdef __SDL_NOGETPROCADDR__
#define SDL_PROC(ret,func,params) data->func=func;
#else
#define SDL_PROC(ret,func,params) \
do { \
data->func = SDL_GL_GetProcAddress(#func); \
if ( ! data->func ) { \
SDL_SetError("Couldn't load GLES function %s: %s\n", #func, SDL_GetError()); \
return -1; \
} \
} while ( 0 );
#endif /* _SDL_NOGETPROCADDR_ */
#include "SDL_glesfuncs.h"
#undef SDL_PROC
return 0;
}
Jun 22, 2011
Jun 22, 2011
200
201
static SDL_GLContext SDL_CurrentContext = NULL;
Jan 19, 2012
Jan 19, 2012
202
203
204
205
206
207
208
209
210
211
212
213
214
GLES_FBOList *
GLES_GetFBO(GLES_RenderData *data, Uint32 w, Uint32 h)
{
GLES_FBOList *result = data->framebuffers;
while ((result) && ((result->w != w) || (result->h != h)) )
{
result = result->next;
}
if (result == NULL)
{
result = SDL_malloc(sizeof(GLES_FBOList));
result->w = w;
result->h = h;
Jan 28, 2012
Jan 28, 2012
215
data->glGenFramebuffersOES(1, &result->FBO);
Jan 19, 2012
Jan 19, 2012
216
217
218
219
220
221
222
result->next = data->framebuffers;
data->framebuffers = result;
}
return result;
}
Jun 22, 2011
Jun 22, 2011
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
static int
GLES_ActivateRenderer(SDL_Renderer * renderer)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
if (SDL_CurrentContext != data->context) {
if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
return -1;
}
SDL_CurrentContext = data->context;
GLES_UpdateViewport(renderer);
}
return 0;
}
/* This is called if we need to invalidate all of the SDL OpenGL state */
static void
GLES_ResetState(SDL_Renderer *renderer)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
if (SDL_CurrentContext == data->context) {
GLES_UpdateViewport(renderer);
} else {
GLES_ActivateRenderer(renderer);
}
data->current.color = 0;
data->current.blendMode = -1;
data->current.tex_coords = SDL_FALSE;
Jan 8, 2012
Jan 8, 2012
255
256
data->glDisable(GL_DEPTH_TEST);
data->glDisable(GL_CULL_FACE);
Jun 22, 2011
Jun 22, 2011
257
Jan 8, 2012
Jan 8, 2012
258
259
data->glMatrixMode(GL_MODELVIEW);
data->glLoadIdentity();
Jun 22, 2011
Jun 22, 2011
260
Jan 8, 2012
Jan 8, 2012
261
262
data->glEnableClientState(GL_VERTEX_ARRAY);
data->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Jun 22, 2011
Jun 22, 2011
263
264
265
266
267
268
269
270
271
}
SDL_Renderer *
GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_Renderer *renderer;
GLES_RenderData *data;
GLint value;
Jan 8, 2012
Jan 8, 2012
272
273
Uint32 windowFlags;
Jul 18, 2012
Jul 18, 2012
274
SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 1);
Jan 8, 2012
Jan 8, 2012
275
276
277
278
279
280
281
282
283
284
285
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
windowFlags = SDL_GetWindowFlags(window);
if (!(windowFlags & SDL_WINDOW_OPENGL)) {
if (SDL_RecreateWindow(window, windowFlags | SDL_WINDOW_OPENGL) < 0) {
/* Uh oh, better try to put it back... */
SDL_RecreateWindow(window, windowFlags);
return NULL;
}
}
Jun 22, 2011
Jun 22, 2011
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
data = (GLES_RenderData *) SDL_calloc(1, sizeof(*data));
if (!data) {
GLES_DestroyRenderer(renderer);
SDL_OutOfMemory();
return NULL;
}
renderer->WindowEvent = GLES_WindowEvent;
renderer->CreateTexture = GLES_CreateTexture;
renderer->UpdateTexture = GLES_UpdateTexture;
renderer->LockTexture = GLES_LockTexture;
renderer->UnlockTexture = GLES_UnlockTexture;
Jan 22, 2012
Jan 22, 2012
305
renderer->SetRenderTarget = GLES_SetRenderTarget;
Jun 22, 2011
Jun 22, 2011
306
307
308
309
310
311
renderer->UpdateViewport = GLES_UpdateViewport;
renderer->RenderClear = GLES_RenderClear;
renderer->RenderDrawPoints = GLES_RenderDrawPoints;
renderer->RenderDrawLines = GLES_RenderDrawLines;
renderer->RenderFillRects = GLES_RenderFillRects;
renderer->RenderCopy = GLES_RenderCopy;
Jun 1, 2012
Jun 1, 2012
312
renderer->RenderCopyEx = GLES_RenderCopyEx;
Oct 2, 2012
Oct 2, 2012
313
renderer->RenderReadPixels = GLES_RenderReadPixels;
Jun 22, 2011
Jun 22, 2011
314
315
316
renderer->RenderPresent = GLES_RenderPresent;
renderer->DestroyTexture = GLES_DestroyTexture;
renderer->DestroyRenderer = GLES_DestroyRenderer;
Sep 3, 2012
Sep 3, 2012
317
318
renderer->GL_BindTexture = GLES_BindTexture;
renderer->GL_UnbindTexture = GLES_UnbindTexture;
Jun 22, 2011
Jun 22, 2011
319
renderer->info = GLES_RenderDriver.info;
Jan 22, 2012
Jan 22, 2012
320
renderer->info.flags = SDL_RENDERER_ACCELERATED;
Jun 22, 2011
Jun 22, 2011
321
renderer->driverdata = data;
Jan 7, 2012
Jan 7, 2012
322
renderer->window = window;
Jun 22, 2011
Jun 22, 2011
323
324
325
326
327
328
329
330
331
332
333
data->context = SDL_GL_CreateContext(window);
if (!data->context) {
GLES_DestroyRenderer(renderer);
return NULL;
}
if (SDL_GL_MakeCurrent(window, data->context) < 0) {
GLES_DestroyRenderer(renderer);
return NULL;
}
Jan 8, 2012
Jan 8, 2012
334
335
336
337
338
if (GLES_LoadFunctions(data) < 0) {
GLES_DestroyRenderer(renderer);
return NULL;
}
Jun 22, 2011
Jun 22, 2011
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
if (flags & SDL_RENDERER_PRESENTVSYNC) {
SDL_GL_SetSwapInterval(1);
} else {
SDL_GL_SetSwapInterval(0);
}
if (SDL_GL_GetSwapInterval() > 0) {
renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
}
#if SDL_VIDEO_DRIVER_PANDORA
data->GL_OES_draw_texture_supported = SDL_FALSE;
data->useDrawTexture = SDL_FALSE;
#else
if (SDL_GL_ExtensionSupported("GL_OES_draw_texture")) {
data->GL_OES_draw_texture_supported = SDL_TRUE;
data->useDrawTexture = SDL_TRUE;
} else {
data->GL_OES_draw_texture_supported = SDL_FALSE;
data->useDrawTexture = SDL_FALSE;
}
#endif
Feb 1, 2012
Feb 1, 2012
361
value = 0;
Jan 8, 2012
Jan 8, 2012
362
data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
Jun 22, 2011
Jun 22, 2011
363
renderer->info.max_texture_width = value;
Feb 1, 2012
Feb 1, 2012
364
value = 0;
Jan 8, 2012
Jan 8, 2012
365
data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
Jun 22, 2011
Jun 22, 2011
366
367
renderer->info.max_texture_height = value;
Jan 19, 2012
Jan 19, 2012
368
369
if (SDL_GL_ExtensionSupported("GL_OES_framebuffer_object")) {
data->GL_OES_framebuffer_object_supported = SDL_TRUE;
Jan 22, 2012
Jan 22, 2012
370
renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
Jan 31, 2012
Jan 31, 2012
371
Feb 1, 2012
Feb 1, 2012
372
value = 0;
Jan 31, 2012
Jan 31, 2012
373
374
data->glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &value);
data->window_framebuffer = (GLuint)value;
Jan 19, 2012
Jan 19, 2012
375
376
377
}
data->framebuffers = NULL;
Jun 22, 2011
Jun 22, 2011
378
379
380
381
382
383
384
385
386
/* Set up parameters for rendering */
GLES_ResetState(renderer);
return renderer;
}
static void
GLES_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
{
Jan 8, 2012
Jan 8, 2012
387
388
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
Jan 23, 2012
Jan 23, 2012
389
390
391
if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED ||
event->event == SDL_WINDOWEVENT_SHOWN ||
event->event == SDL_WINDOWEVENT_HIDDEN) {
Jun 22, 2011
Jun 22, 2011
392
393
394
/* Rebind the context to the window area and update matrices */
SDL_CurrentContext = NULL;
}
Nov 8, 2011
Nov 8, 2011
395
396
397
if (event->event == SDL_WINDOWEVENT_MINIMIZED) {
/* According to Apple documentation, we need to finish drawing NOW! */
Jan 19, 2012
Jan 19, 2012
398
data->glFinish();
Nov 8, 2011
Nov 8, 2011
399
}
Jun 22, 2011
Jun 22, 2011
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
}
static __inline__ int
power_of_2(int input)
{
int value = 1;
while (value < input) {
value <<= 1;
}
return value;
}
static GLenum
GetScaleQuality(void)
{
const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
return GL_NEAREST;
} else {
return GL_LINEAR;
}
}
static int
GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
Jan 8, 2012
Jan 8, 2012
428
GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
Jun 22, 2011
Jun 22, 2011
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
GLES_TextureData *data;
GLint internalFormat;
GLenum format, type;
int texture_w, texture_h;
GLenum scaleMode;
GLenum result;
GLES_ActivateRenderer(renderer);
switch (texture->format) {
case SDL_PIXELFORMAT_ABGR8888:
internalFormat = GL_RGBA;
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
break;
default:
SDL_SetError("Texture format not supported");
return -1;
}
data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data));
if (!data) {
SDL_OutOfMemory();
return -1;
}
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
data->pixels = SDL_calloc(1, texture->h * data->pitch);
if (!data->pixels) {
SDL_OutOfMemory();
SDL_free(data);
return -1;
}
}
texture->driverdata = data;
Jan 19, 2012
Jan 19, 2012
466
467
468
469
470
if (texture->access == SDL_TEXTUREACCESS_TARGET) {
data->fbo = GLES_GetFBO(renderer->driverdata, texture->w, texture->h);
} else {
data->fbo = NULL;
}
Jun 22, 2011
Jun 22, 2011
471
Jan 8, 2012
Jan 8, 2012
472
473
474
renderdata->glGetError();
renderdata->glEnable(GL_TEXTURE_2D);
renderdata->glGenTextures(1, &data->texture);
Jun 22, 2011
Jun 22, 2011
475
476
477
478
479
480
481
482
483
484
485
data->type = GL_TEXTURE_2D;
/* no NPOV textures allowed in OpenGL ES (yet) */
texture_w = power_of_2(texture->w);
texture_h = power_of_2(texture->h);
data->texw = (GLfloat) texture->w / texture_w;
data->texh = (GLfloat) texture->h / texture_h;
data->format = format;
data->formattype = type;
scaleMode = GetScaleQuality();
Jan 8, 2012
Jan 8, 2012
486
487
488
489
490
renderdata->glBindTexture(data->type, data->texture);
renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, scaleMode);
renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER, scaleMode);
renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Jun 22, 2011
Jun 22, 2011
491
Jan 8, 2012
Jan 8, 2012
492
renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
Jun 22, 2011
Jun 22, 2011
493
texture_h, 0, format, type, NULL);
Jan 8, 2012
Jan 8, 2012
494
renderdata->glDisable(GL_TEXTURE_2D);
Jun 22, 2011
Jun 22, 2011
495
Jan 8, 2012
Jan 8, 2012
496
result = renderdata->glGetError();
Jun 22, 2011
Jun 22, 2011
497
498
499
500
501
502
503
504
505
506
507
if (result != GL_NO_ERROR) {
GLES_SetError("glTexImage2D()", result);
return -1;
}
return 0;
}
static int
GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
{
Jan 8, 2012
Jan 8, 2012
508
GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
Jun 22, 2011
Jun 22, 2011
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
Uint8 *blob = NULL;
Uint8 *src;
int srcPitch;
int y;
GLES_ActivateRenderer(renderer);
/* Bail out if we're supposed to update an empty rectangle */
if (rect->w <= 0 || rect->h <= 0)
return 0;
/* Reformat the texture data into a tightly packed array */
srcPitch = rect->w * SDL_BYTESPERPIXEL(texture->format);
src = (Uint8 *)pixels;
if (pitch != srcPitch)
{
blob = (Uint8 *)SDL_malloc(srcPitch * rect->h);
if (!blob)
{
SDL_OutOfMemory();
return -1;
}
src = blob;
for (y = 0; y < rect->h; ++y)
{
SDL_memcpy(src, pixels, srcPitch);
src += srcPitch;
pixels = (Uint8 *)pixels + pitch;
}
src = blob;
}
/* Create a texture subimage with the supplied data */
Jan 8, 2012
Jan 8, 2012
543
544
545
546
547
renderdata->glGetError();
renderdata->glEnable(data->type);
renderdata->glBindTexture(data->type, data->texture);
renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
renderdata->glTexSubImage2D(data->type,
Jun 22, 2011
Jun 22, 2011
548
549
550
551
552
553
554
555
556
557
558
559
0,
rect->x,
rect->y,
rect->w,
rect->h,
data->format,
data->formattype,
src);
if (blob) {
SDL_free(blob);
}
Jan 8, 2012
Jan 8, 2012
560
if (renderdata->glGetError() != GL_NO_ERROR)
Jun 22, 2011
Jun 22, 2011
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
{
SDL_SetError("Failed to update texture");
return -1;
}
return 0;
}
static int
GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch)
{
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
*pixels =
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch = data->pitch;
return 0;
}
static void
GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
SDL_Rect rect;
/* We do whole texture updates, at least for now */
rect.x = 0;
rect.y = 0;
rect.w = texture->w;
rect.h = texture->h;
GLES_UpdateTexture(renderer, texture, &rect, data->pixels, data->pitch);
}
Jan 22, 2012
Jan 22, 2012
595
static int
Jan 22, 2012
Jan 22, 2012
596
GLES_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
Jan 22, 2012
Jan 22, 2012
597
598
599
600
601
602
603
604
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
GLES_TextureData *texturedata = NULL;
GLenum status;
GLES_ActivateRenderer(renderer);
if (texture == NULL) {
Jan 31, 2012
Jan 31, 2012
605
data->glBindFramebufferOES(GL_FRAMEBUFFER_OES, data->window_framebuffer);
Jan 22, 2012
Jan 22, 2012
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
return 0;
}
texturedata = (GLES_TextureData *) texture->driverdata;
data->glBindFramebufferOES(GL_FRAMEBUFFER_OES, texturedata->fbo->FBO);
/* TODO: check if texture pixel format allows this operation */
data->glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, texturedata->type, texturedata->texture, 0);
/* Check FBO status */
status = data->glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
if (status != GL_FRAMEBUFFER_COMPLETE_OES) {
SDL_SetError("glFramebufferTexture2DOES() failed");
return -1;
}
return 0;
}
Jun 22, 2011
Jun 22, 2011
622
623
624
625
626
627
628
629
630
631
static int
GLES_UpdateViewport(SDL_Renderer * renderer)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
if (SDL_CurrentContext != data->context) {
/* We'll update the viewport after we rebind the context */
return 0;
}
Jan 8, 2012
Jan 8, 2012
632
data->glViewport(renderer->viewport.x, renderer->viewport.y,
Jun 22, 2011
Jun 22, 2011
633
634
renderer->viewport.w, renderer->viewport.h);
Jan 8, 2012
Jan 8, 2012
635
636
637
data->glMatrixMode(GL_PROJECTION);
data->glLoadIdentity();
data->glOrthof((GLfloat) 0,
Nov 10, 2011
Nov 10, 2011
638
(GLfloat) renderer->viewport.w,
Jun 22, 2011
Jun 22, 2011
639
640
641
642
643
644
645
646
647
648
649
(GLfloat) renderer->viewport.h,
(GLfloat) 0, 0.0, 1.0);
return 0;
}
static void
GLES_SetColor(GLES_RenderData * data, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
if (color != data->current.color) {
Jan 8, 2012
Jan 8, 2012
650
data->glColor4f((GLfloat) r * inv255f,
Jun 22, 2011
Jun 22, 2011
651
652
653
654
655
656
657
658
659
660
661
662
663
(GLfloat) g * inv255f,
(GLfloat) b * inv255f,
(GLfloat) a * inv255f);
data->current.color = color;
}
}
static void
GLES_SetBlendMode(GLES_RenderData * data, int blendMode)
{
if (blendMode != data->current.blendMode) {
switch (blendMode) {
case SDL_BLENDMODE_NONE:
Jan 8, 2012
Jan 8, 2012
664
665
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
data->glDisable(GL_BLEND);
Jun 22, 2011
Jun 22, 2011
666
667
break;
case SDL_BLENDMODE_BLEND:
Jan 8, 2012
Jan 8, 2012
668
669
670
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
data->glEnable(GL_BLEND);
data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Jun 22, 2011
Jun 22, 2011
671
672
break;
case SDL_BLENDMODE_ADD:
Jan 8, 2012
Jan 8, 2012
673
674
675
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
data->glEnable(GL_BLEND);
data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
Jun 22, 2011
Jun 22, 2011
676
677
break;
case SDL_BLENDMODE_MOD:
Jan 8, 2012
Jan 8, 2012
678
679
680
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
data->glEnable(GL_BLEND);
data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
Jun 22, 2011
Jun 22, 2011
681
682
683
684
685
686
687
688
689
690
691
break;
}
data->current.blendMode = blendMode;
}
}
static void
GLES_SetTexCoords(GLES_RenderData * data, SDL_bool enabled)
{
if (enabled != data->current.tex_coords) {
if (enabled) {
Jan 8, 2012
Jan 8, 2012
692
data->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Jun 22, 2011
Jun 22, 2011
693
} else {
Jan 8, 2012
Jan 8, 2012
694
data->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Jun 22, 2011
Jun 22, 2011
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
}
data->current.tex_coords = enabled;
}
}
static void
GLES_SetDrawingState(SDL_Renderer * renderer)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
GLES_ActivateRenderer(renderer);
GLES_SetColor(data, (GLfloat) renderer->r,
(GLfloat) renderer->g,
(GLfloat) renderer->b,
(GLfloat) renderer->a);
GLES_SetBlendMode(data, renderer->blendMode);
GLES_SetTexCoords(data, SDL_FALSE);
}
static int
GLES_RenderClear(SDL_Renderer * renderer)
{
Jan 8, 2012
Jan 8, 2012
720
721
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
Jun 22, 2011
Jun 22, 2011
722
723
GLES_ActivateRenderer(renderer);
Jan 8, 2012
Jan 8, 2012
724
data->glClearColor((GLfloat) renderer->r * inv255f,
Jun 22, 2011
Jun 22, 2011
725
726
727
728
(GLfloat) renderer->g * inv255f,
(GLfloat) renderer->b * inv255f,
(GLfloat) renderer->a * inv255f);
Jan 8, 2012
Jan 8, 2012
729
data->glClear(GL_COLOR_BUFFER_BIT);
Jun 22, 2011
Jun 22, 2011
730
731
732
733
734
return 0;
}
static int
Oct 2, 2012
Oct 2, 2012
735
GLES_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
Jun 22, 2011
Jun 22, 2011
736
737
int count)
{
Jan 8, 2012
Jan 8, 2012
738
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
Jun 22, 2011
Jun 22, 2011
739
740
741
GLES_SetDrawingState(renderer);
Oct 2, 2012
Oct 2, 2012
742
data->glVertexPointer(2, GL_FLOAT, 0, points);
Jan 8, 2012
Jan 8, 2012
743
data->glDrawArrays(GL_POINTS, 0, count);
Jun 22, 2011
Jun 22, 2011
744
745
746
747
748
return 0;
}
static int
Oct 2, 2012
Oct 2, 2012
749
GLES_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
Jun 22, 2011
Jun 22, 2011
750
751
int count)
{
Jan 8, 2012
Jan 8, 2012
752
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
Jun 22, 2011
Jun 22, 2011
753
754
755
GLES_SetDrawingState(renderer);
Oct 2, 2012
Oct 2, 2012
756
data->glVertexPointer(2, GL_FLOAT, 0, points);
Jun 22, 2011
Jun 22, 2011
757
758
759
760
if (count > 2 &&
points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
/* GL_LINE_LOOP takes care of the final segment */
--count;
Jan 8, 2012
Jan 8, 2012
761
data->glDrawArrays(GL_LINE_LOOP, 0, count);
Jun 22, 2011
Jun 22, 2011
762
} else {
Jan 8, 2012
Jan 8, 2012
763
data->glDrawArrays(GL_LINE_STRIP, 0, count);
Nov 10, 2011
Nov 10, 2011
764
/* We need to close the endpoint of the line */
Jan 8, 2012
Jan 8, 2012
765
data->glDrawArrays(GL_POINTS, count-1, 1);
Jun 22, 2011
Jun 22, 2011
766
767
768
769
770
771
}
return 0;
}
static int
Oct 2, 2012
Oct 2, 2012
772
GLES_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects,
Jun 22, 2011
Jun 22, 2011
773
774
int count)
{
Jan 8, 2012
Jan 8, 2012
775
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
Jun 22, 2011
Jun 22, 2011
776
777
778
779
780
int i;
GLES_SetDrawingState(renderer);
for (i = 0; i < count; ++i) {
Oct 2, 2012
Oct 2, 2012
781
782
783
784
785
786
const SDL_FRect *rect = &rects[i];
GLfloat minx = rect->x;
GLfloat maxx = rect->x + rect->w;
GLfloat miny = rect->y;
GLfloat maxy = rect->y + rect->h;
GLfloat vertices[8];
Jun 22, 2011
Jun 22, 2011
787
788
789
790
791
792
793
794
795
vertices[0] = minx;
vertices[1] = miny;
vertices[2] = maxx;
vertices[3] = miny;
vertices[4] = minx;
vertices[5] = maxy;
vertices[6] = maxx;
vertices[7] = maxy;
Oct 2, 2012
Oct 2, 2012
796
data->glVertexPointer(2, GL_FLOAT, 0, vertices);
Jan 8, 2012
Jan 8, 2012
797
data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Jun 22, 2011
Jun 22, 2011
798
799
800
801
802
803
804
}
return 0;
}
static int
GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Oct 2, 2012
Oct 2, 2012
805
const SDL_Rect * srcrect, const SDL_FRect * dstrect)
Jun 22, 2011
Jun 22, 2011
806
807
808
809
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
Oct 2, 2012
Oct 2, 2012
810
GLfloat minx, miny, maxx, maxy;
Jun 22, 2011
Jun 22, 2011
811
812
813
814
GLfloat minu, maxu, minv, maxv;
GLES_ActivateRenderer(renderer);
Jan 8, 2012
Jan 8, 2012
815
data->glEnable(GL_TEXTURE_2D);
Jun 22, 2011
Jun 22, 2011
816
Jan 8, 2012
Jan 8, 2012
817
data->glBindTexture(texturedata->type, texturedata->texture);
Jun 22, 2011
Jun 22, 2011
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
if (texture->modMode) {
GLES_SetColor(data, texture->r, texture->g, texture->b, texture->a);
} else {
GLES_SetColor(data, 255, 255, 255, 255);
}
GLES_SetBlendMode(data, texture->blendMode);
GLES_SetTexCoords(data, SDL_TRUE);
if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
/* this code is a little funny because the viewport is upside down vs SDL's coordinate system */
GLint cropRect[4];
int w, h;
SDL_Window *window = renderer->window;
SDL_GetWindowSize(window, &w, &h);
Jan 22, 2012
Jan 22, 2012
836
if (renderer->target) {
Jan 19, 2012
Jan 19, 2012
837
838
839
840
841
842
cropRect[0] = srcrect->x;
cropRect[1] = srcrect->y;
cropRect[2] = srcrect->w;
cropRect[3] = srcrect->h;
data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES,
cropRect);
Oct 2, 2012
Oct 2, 2012
843
data->glDrawTexfOES(renderer->viewport.x + dstrect->x, renderer->viewport.y + dstrect->y, 0,
Jan 19, 2012
Jan 19, 2012
844
845
846
847
848
849
850
851
dstrect->w, dstrect->h);
} else {
cropRect[0] = srcrect->x;
cropRect[1] = srcrect->y + srcrect->h;
cropRect[2] = srcrect->w;
cropRect[3] = -srcrect->h;
data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES,
cropRect);
Oct 2, 2012
Oct 2, 2012
852
data->glDrawTexfOES(renderer->viewport.x + dstrect->x,
Jan 19, 2012
Jan 19, 2012
853
854
855
h - (renderer->viewport.y + dstrect->y) - dstrect->h, 0,
dstrect->w, dstrect->h);
}
Jun 22, 2011
Jun 22, 2011
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
} else {
minx = dstrect->x;
miny = dstrect->y;
maxx = dstrect->x + dstrect->w;
maxy = dstrect->y + dstrect->h;
minu = (GLfloat) srcrect->x / texture->w;
minu *= texturedata->texw;
maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
maxu *= texturedata->texw;
minv = (GLfloat) srcrect->y / texture->h;
minv *= texturedata->texh;
maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
maxv *= texturedata->texh;
Oct 2, 2012
Oct 2, 2012
872
GLfloat vertices[8];
Jun 22, 2011
Jun 22, 2011
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
GLfloat texCoords[8];
vertices[0] = minx;
vertices[1] = miny;
vertices[2] = maxx;
vertices[3] = miny;
vertices[4] = minx;
vertices[5] = maxy;
vertices[6] = maxx;
vertices[7] = maxy;
texCoords[0] = minu;
texCoords[1] = minv;
texCoords[2] = maxu;
texCoords[3] = minv;
texCoords[4] = minu;
texCoords[5] = maxv;
texCoords[6] = maxu;
texCoords[7] = maxv;
Oct 2, 2012
Oct 2, 2012
893
data->glVertexPointer(2, GL_FLOAT, 0, vertices);
Jan 8, 2012
Jan 8, 2012
894
895
data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Jun 22, 2011
Jun 22, 2011
896
}
Jan 8, 2012
Jan 8, 2012
897
data->glDisable(GL_TEXTURE_2D);
Jun 22, 2011
Jun 22, 2011
898
899
900
901
return 0;
}
Jun 1, 2012
Jun 1, 2012
902
903
static int
GLES_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
Oct 2, 2012
Oct 2, 2012
904
905
const SDL_Rect * srcrect, const SDL_FRect * dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
Jun 1, 2012
Jun 1, 2012
906
907
908
909
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
Oct 2, 2012
Oct 2, 2012
910
GLfloat minx, miny, maxx, maxy;
Jun 1, 2012
Jun 1, 2012
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
GLfloat minu, maxu, minv, maxv;
GLfloat centerx, centery;
GLES_ActivateRenderer(renderer);
data->glEnable(GL_TEXTURE_2D);
data->glBindTexture(texturedata->type, texturedata->texture);
if (texture->modMode) {
GLES_SetColor(data, texture->r, texture->g, texture->b, texture->a);
} else {
GLES_SetColor(data, 255, 255, 255, 255);
}
GLES_SetBlendMode(data, texture->blendMode);
GLES_SetTexCoords(data, SDL_TRUE);
Oct 2, 2012
Oct 2, 2012
930
931
centerx = center->x;
centery = center->y;
Jun 1, 2012
Jun 1, 2012
932
933
934
// Rotate and translate
data->glPushMatrix();
Oct 2, 2012
Oct 2, 2012
935
936
data->glTranslatef(dstrect->x + centerx, dstrect->y + centery, 0.0f);
data->glRotatef((GLfloat)angle, 0.0f, 0.0f, 1.0f);
Jun 1, 2012
Jun 1, 2012
937
938
if (flip & SDL_FLIP_HORIZONTAL) {
Oct 2, 2012
Oct 2, 2012
939
minx = dstrect->w - centerx;
Jun 1, 2012
Jun 1, 2012
940
maxx = -centerx;
Oct 2, 2012
Oct 2, 2012
941
} else {
Jun 1, 2012
Jun 1, 2012
942
943
944
945
946
947
948
minx = -centerx;
maxx = dstrect->w - centerx;
}
if (flip & SDL_FLIP_VERTICAL) {
miny = dstrect->h - centery;
maxy = -centery;
Oct 2, 2012
Oct 2, 2012
949
} else {
Jun 1, 2012
Jun 1, 2012
950
951
952
953
954
955
956
957
958
959
960
961
962
miny = -centery;
maxy = dstrect->h - centery;
}
minu = (GLfloat) srcrect->x / texture->w;
minu *= texturedata->texw;
maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
maxu *= texturedata->texw;
minv = (GLfloat) srcrect->y / texture->h;
minv *= texturedata->texh;
maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
maxv *= texturedata->texh;
Oct 2, 2012
Oct 2, 2012
963
GLfloat vertices[8];
Jun 1, 2012
Jun 1, 2012
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
GLfloat texCoords[8];
vertices[0] = minx;
vertices[1] = miny;
vertices[2] = maxx;
vertices[3] = miny;
vertices[4] = minx;
vertices[5] = maxy;
vertices[6] = maxx;
vertices[7] = maxy;
texCoords[0] = minu;
texCoords[1] = minv;
texCoords[2] = maxu;
texCoords[3] = minv;
texCoords[4] = minu;
texCoords[5] = maxv;
texCoords[6] = maxu;
texCoords[7] = maxv;
Oct 2, 2012
Oct 2, 2012
983
data->glVertexPointer(2, GL_FLOAT, 0, vertices);
Jun 1, 2012
Jun 1, 2012
984
985
986
987
988
989
990
991
data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
data->glPopMatrix();
data->glDisable(GL_TEXTURE_2D);
return 0;
}
Oct 2, 2012
Oct 2, 2012
992
993
994
995
996
997
998
999
1000
static int
GLES_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 pixel_format, void * pixels, int pitch)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
SDL_Window *window = renderer->window;
Uint32 temp_format = SDL_PIXELFORMAT_ABGR8888;
void *temp_pixels;
int temp_pitch;