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

Latest commit

 

History

History
811 lines (700 loc) · 24.2 KB

SDL_renderer_gles.c

File metadata and controls

811 lines (700 loc) · 24.2 KB
 
1
2
3
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
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#if SDL_VIDEO_RENDER_OGL_ES
#include "SDL_video.h"
#include "SDL_opengles.h"
#include "SDL_sysvideo.h"
#include "SDL_pixels_c.h"
#include "SDL_rect_c.h"
#include "SDL_yuv_sw_c.h"
Aug 16, 2008
Aug 16, 2008
33
/* OpenGL ES 1.1 renderer implementation, based on the OpenGL renderer */
34
35
36
static const float inv255f = 1.0f / 255.0f;
Aug 16, 2008
Aug 16, 2008
37
38
39
40
41
static SDL_Renderer *GLES_CreateRenderer(SDL_Window * window, Uint32 flags);
static int GLES_ActivateRenderer(SDL_Renderer * renderer);
static int GLES_DisplayModeChanged(SDL_Renderer * renderer);
static int GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static int GLES_QueryTexturePixels(SDL_Renderer * renderer,
42
43
SDL_Texture * texture, void **pixels,
int *pitch);
Aug 16, 2008
Aug 16, 2008
44
static int GLES_SetTexturePalette(SDL_Renderer * renderer,
45
46
47
SDL_Texture * texture,
const SDL_Color * colors, int firstcolor,
int ncolors);
Aug 16, 2008
Aug 16, 2008
48
static int GLES_GetTexturePalette(SDL_Renderer * renderer,
49
50
SDL_Texture * texture, SDL_Color * colors,
int firstcolor, int ncolors);
Aug 16, 2008
Aug 16, 2008
51
static int GLES_SetTextureColorMod(SDL_Renderer * renderer,
52
SDL_Texture * texture);
Aug 16, 2008
Aug 16, 2008
53
static int GLES_SetTextureAlphaMod(SDL_Renderer * renderer,
54
SDL_Texture * texture);
Aug 16, 2008
Aug 16, 2008
55
static int GLES_SetTextureBlendMode(SDL_Renderer * renderer,
56
SDL_Texture * texture);
Aug 16, 2008
Aug 16, 2008
57
static int GLES_SetTextureScaleMode(SDL_Renderer * renderer,
58
SDL_Texture * texture);
Aug 16, 2008
Aug 16, 2008
59
static int GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
60
61
const SDL_Rect * rect, const void *pixels,
int pitch);
Aug 16, 2008
Aug 16, 2008
62
static int GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
63
64
const SDL_Rect * rect, int markDirty, void **pixels,
int *pitch);
Aug 16, 2008
Aug 16, 2008
65
66
static void GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
67
int numrects, const SDL_Rect * rects);
Aug 16, 2008
Aug 16, 2008
68
static int GLES_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
69
Uint8 a, const SDL_Rect * rect);
Aug 16, 2008
Aug 16, 2008
70
static int GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
71
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
Aug 16, 2008
Aug 16, 2008
72
73
74
static void GLES_RenderPresent(SDL_Renderer * renderer);
static void GLES_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void GLES_DestroyRenderer(SDL_Renderer * renderer);
75
76
77
SDL_RenderDriver GL_ES_RenderDriver = {
Aug 16, 2008
Aug 16, 2008
78
GLES_CreateRenderer,
79
80
81
82
83
84
85
86
87
88
{
"opengl_es",
(SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
(SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
SDL_TEXTUREMODULATE_ALPHA),
(SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
SDL_TEXTUREBLENDMODE_MOD),
(SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
Aug 16, 2008
Aug 16, 2008
89
90
SDL_TEXTURESCALEMODE_SLOW), 2,
{
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
SDL_PIXELFORMAT_RGB24,
SDL_PIXELFORMAT_ABGR8888,
},
0,
0}
};
typedef struct
{
SDL_GLContext context;
SDL_bool updateSize;
int blendMode;
#ifndef APIENTRY
#define APIENTRY
#endif
SDL_bool useDrawTexture;
SDL_bool GL_OES_draw_texture_supported;
/* OpenGL ES functions */
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
#include "SDL_glesfuncs.h"
#undef SDL_PROC
Aug 16, 2008
Aug 16, 2008
116
} GLES_RenderData;
117
118
119
120
121
122
123
124
125
126
127
128
typedef struct
{
GLuint texture;
GLenum type;
GLfloat texw;
GLfloat texh;
GLenum format;
GLenum formattype;
void *pixels;
int pitch;
SDL_DirtyRectList dirty;
Aug 16, 2008
Aug 16, 2008
129
} GLES_TextureData;
Aug 16, 2008
Aug 16, 2008
132
GLES_SetError(const char *prefix, GLenum result)
133
134
135
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
{
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);
}
static int
Aug 16, 2008
Aug 16, 2008
166
GLES_LoadFunctions(GLES_RenderData * data)
167
168
169
170
171
172
173
174
175
176
177
{
#define SDL_PROC(ret,func,params) \
data->func = func;
#include "SDL_glesfuncs.h"
#undef SDL_PROC
return 0;
}
void
Aug 16, 2008
Aug 16, 2008
178
GLES_AddRenderDriver(_THIS)
179
180
181
182
183
184
185
{
if (_this->GL_CreateContext) {
SDL_AddRenderDriver(0, &GL_ES_RenderDriver);
}
}
SDL_Renderer *
Aug 16, 2008
Aug 16, 2008
186
GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
Aug 16, 2008
Aug 16, 2008
188
189
SDL_Renderer *renderer;
Aug 16, 2008
Aug 16, 2008
190
GLES_RenderData *data;
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
GLint value;
int doublebuffer;
if (!(window->flags & SDL_WINDOW_OPENGL)) {
if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
return NULL;
}
}
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
Aug 16, 2008
Aug 16, 2008
206
data = (GLES_RenderData *) SDL_calloc(1, sizeof(*data));
Aug 16, 2008
Aug 16, 2008
208
GLES_DestroyRenderer(renderer);
209
210
211
212
SDL_OutOfMemory();
return NULL;
}
Aug 16, 2008
Aug 16, 2008
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
renderer->ActivateRenderer = GLES_ActivateRenderer;
renderer->DisplayModeChanged = GLES_DisplayModeChanged;
renderer->CreateTexture = GLES_CreateTexture;
renderer->QueryTexturePixels = GLES_QueryTexturePixels;
renderer->SetTexturePalette = GLES_SetTexturePalette;
renderer->GetTexturePalette = GLES_GetTexturePalette;
renderer->SetTextureColorMod = GLES_SetTextureColorMod;
renderer->SetTextureAlphaMod = GLES_SetTextureAlphaMod;
renderer->SetTextureBlendMode = GLES_SetTextureBlendMode;
renderer->SetTextureScaleMode = GLES_SetTextureScaleMode;
renderer->UpdateTexture = GLES_UpdateTexture;
renderer->LockTexture = GLES_LockTexture;
renderer->UnlockTexture = GLES_UnlockTexture;
renderer->DirtyTexture = GLES_DirtyTexture;
renderer->RenderFill = GLES_RenderFill;
renderer->RenderCopy = GLES_RenderCopy;
renderer->RenderPresent = GLES_RenderPresent;
renderer->DestroyTexture = GLES_DestroyTexture;
renderer->DestroyRenderer = GLES_DestroyRenderer;
232
233
234
235
236
237
238
renderer->info = GL_ES_RenderDriver.info;
renderer->window = window->id;
renderer->driverdata = data;
renderer->info.flags = (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
Aug 16, 2008
Aug 16, 2008
239
240
if (GLES_LoadFunctions(data) < 0) {
GLES_DestroyRenderer(renderer);
241
242
243
244
245
return NULL;
}
data->context = SDL_GL_CreateContext(window->id);
if (!data->context) {
Aug 16, 2008
Aug 16, 2008
246
GLES_DestroyRenderer(renderer);
247
248
249
return NULL;
}
if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
Aug 16, 2008
Aug 16, 2008
250
GLES_DestroyRenderer(renderer);
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
return NULL;
}
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_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
if (!doublebuffer) {
renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
}
}
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;
}
data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
renderer->info.max_texture_width = value;
data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
renderer->info.max_texture_height = value;
/* Set up parameters for rendering */
data->blendMode = -1;
data->glDisable(GL_DEPTH_TEST);
data->glDisable(GL_CULL_FACE);
data->glEnable(GL_TEXTURE_2D);
data->updateSize = SDL_TRUE;
return renderer;
}
Aug 16, 2008
Aug 16, 2008
293
static int GLES_ActivateRenderer(SDL_Renderer * renderer)
Aug 16, 2008
Aug 16, 2008
296
GLES_RenderData *data = (GLES_RenderData *)renderer->driverdata;
297
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
Aug 15, 2008
Aug 15, 2008
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
return -1;
}
if (data->updateSize) {
data->glMatrixMode(GL_PROJECTION);
data->glLoadIdentity();
data->glMatrixMode(GL_MODELVIEW);
data->glLoadIdentity();
data->glViewport(0, 0, window->w, window->h);
data->glOrthof(0.0, (GLfloat)window->w, (GLfloat)window->h, 0.0,
0.0, 1.0);
data->updateSize = SDL_FALSE;
}
return 0;
}
static int
Aug 16, 2008
Aug 16, 2008
316
GLES_DisplayModeChanged(SDL_Renderer * renderer)
Aug 16, 2008
Aug 16, 2008
318
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
data->updateSize = SDL_TRUE;
return 0;
}
static __inline__ int
power_of_2(int input)
{
int value = 1;
while (value < input) {
value <<= 1;
}
return value;
}
static int
Aug 16, 2008
Aug 16, 2008
336
GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
Aug 16, 2008
Aug 16, 2008
338
GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
339
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
Aug 16, 2008
Aug 16, 2008
340
GLES_TextureData *data;
341
342
343
344
345
346
347
348
349
350
351
352
353
354
GLint internalFormat;
GLenum format, type;
int texture_w, texture_h;
GLenum result;
switch (texture->format) {
case SDL_PIXELFORMAT_INDEX1LSB:
case SDL_PIXELFORMAT_INDEX1MSB:
case SDL_PIXELFORMAT_INDEX8:
case SDL_PIXELFORMAT_RGB332:
case SDL_PIXELFORMAT_RGB444:
case SDL_PIXELFORMAT_RGB555:
case SDL_PIXELFORMAT_ARGB4444:
case SDL_PIXELFORMAT_ARGB1555:
case SDL_PIXELFORMAT_BGR24:
Aug 16, 2008
Aug 16, 2008
355
case SDL_PIXELFORMAT_BGR888:
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
case SDL_PIXELFORMAT_RGB888:
case SDL_PIXELFORMAT_RGBA8888:
case SDL_PIXELFORMAT_ARGB2101010:
case SDL_PIXELFORMAT_ARGB8888:
case SDL_PIXELFORMAT_RGB24:
internalFormat = GL_RGB;
format = GL_RGB;
type = GL_UNSIGNED_BYTE;
break;
case SDL_PIXELFORMAT_ABGR8888:
internalFormat = GL_RGBA;
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
break;
/*
These formats would be supported if SDL had the necessary pixel formats
case SDL_PIXELFORMAT_BGR565:
internalFormat = GL_RGB;
format = GL_RGB;
type = GL_UNSIGNED_SHORT_5_6_5;
break;
case SDL_PIXELFORMAT_ABGR5551:
internalFormat = GL_RGBA;
format = GL_RGBA;
type = GL_UNSIGNED_SHORT_5_5_5_1;
break;
case SDL_PIXELFORMAT_ABGR4444:
internalFormat = GL_RGBA;
format = GL_RGBA;
type = GL_UNSIGNED_SHORT_4_4_4_4;
break;
*/
default:
SDL_SetError("Unsupported texture format");
return -1;
}
Aug 16, 2008
Aug 16, 2008
392
393
data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data));
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
if (!data) {
SDL_OutOfMemory();
return -1;
}
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
data->pixels = SDL_malloc(texture->h * data->pitch);
if (!data->pixels) {
SDL_OutOfMemory();
SDL_free(data);
return -1;
}
}
texture->driverdata = data;
renderdata->glGetError();
renderdata->glGenTextures(1, &data->texture);
data->type = GL_TEXTURE_2D;
Aug 16, 2008
Aug 16, 2008
415
/* no NPOV textures allowed in OpenGL ES (yet) */
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
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;
renderdata->glBindTexture(data->type, data->texture);
renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
GL_CLAMP_TO_EDGE);
renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
GL_CLAMP_TO_EDGE);
renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
texture_h, 0, format, type, NULL);
result = renderdata->glGetError();
if (result != GL_NO_ERROR) {
Aug 16, 2008
Aug 16, 2008
438
GLES_SetError("glTexImage2D()", result);
439
440
441
442
443
444
return -1;
}
return 0;
}
static int
Aug 16, 2008
Aug 16, 2008
445
GLES_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
446
447
void **pixels, int *pitch)
{
Aug 16, 2008
Aug 16, 2008
448
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
449
450
451
452
453
454
455
*pixels = data->pixels;
*pitch = data->pitch;
return 0;
}
static int
Aug 16, 2008
Aug 16, 2008
456
GLES_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
457
458
459
460
461
462
463
const SDL_Color * colors, int firstcolor, int ncolors)
{
SDL_SetError("OpenGL ES does not support paletted textures");
return -1;
}
static int
Aug 16, 2008
Aug 16, 2008
464
GLES_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
465
466
467
468
469
470
471
SDL_Color * colors, int firstcolor, int ncolors)
{
SDL_SetError("OpenGL ES does not support paletted textures");
return -1;
}
static void
Aug 16, 2008
Aug 16, 2008
472
SetupTextureUpdate(GLES_RenderData * renderdata, SDL_Texture * texture,
473
474
475
476
int pitch)
{
Aug 16, 2008
Aug 16, 2008
477
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
478
479
480
481
482
renderdata->glBindTexture(data->type, data->texture);
renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
static int
Aug 16, 2008
Aug 16, 2008
483
GLES_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
484
485
486
487
488
{
return 0;
}
static int
Aug 16, 2008
Aug 16, 2008
489
GLES_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
490
491
492
493
494
{
return 0;
}
static int
Aug 16, 2008
Aug 16, 2008
495
GLES_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
{
switch (texture->blendMode) {
case SDL_TEXTUREBLENDMODE_NONE:
case SDL_TEXTUREBLENDMODE_MASK:
case SDL_TEXTUREBLENDMODE_BLEND:
case SDL_TEXTUREBLENDMODE_ADD:
case SDL_TEXTUREBLENDMODE_MOD:
return 0;
default:
SDL_Unsupported();
texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
return -1;
}
}
static int
Aug 16, 2008
Aug 16, 2008
512
GLES_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
{
switch (texture->scaleMode) {
case SDL_TEXTURESCALEMODE_NONE:
case SDL_TEXTURESCALEMODE_FAST:
case SDL_TEXTURESCALEMODE_SLOW:
return 0;
case SDL_TEXTURESCALEMODE_BEST:
SDL_Unsupported();
texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
return -1;
default:
SDL_Unsupported();
texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
return -1;
}
}
static int
Aug 16, 2008
Aug 16, 2008
531
GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
532
533
const SDL_Rect * rect, const void *pixels, int pitch)
{
Aug 16, 2008
Aug 16, 2008
534
535
GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
536
537
538
539
540
541
542
543
544
GLenum result;
SetupTextureUpdate(renderdata, texture, pitch);
renderdata->glGetError();
renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
rect->h, data->format, data->formattype,
pixels);
result = renderdata->glGetError();
if (result != GL_NO_ERROR) {
Aug 16, 2008
Aug 16, 2008
545
GLES_SetError("glTexSubImage2D()", result);
546
547
548
549
550
551
return -1;
}
return 0;
}
static int
Aug 16, 2008
Aug 16, 2008
552
GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
553
554
555
const SDL_Rect * rect, int markDirty, void **pixels,
int *pitch)
{
Aug 16, 2008
Aug 16, 2008
556
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
557
558
559
560
561
562
563
564
565
566
567
568
569
if (markDirty) {
SDL_AddDirtyRect(&data->dirty, rect);
}
*pixels =
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch = data->pitch;
return 0;
}
static void
Aug 16, 2008
Aug 16, 2008
570
GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
571
572
573
574
{
}
static void
Aug 16, 2008
Aug 16, 2008
575
GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
576
577
const SDL_Rect * rects)
{
Aug 16, 2008
Aug 16, 2008
578
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
579
580
581
582
583
584
585
586
int i;
for (i = 0; i < numrects; ++i) {
SDL_AddDirtyRect(&data->dirty, &rects[i]);
}
}
static int
Aug 16, 2008
Aug 16, 2008
587
GLES_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
588
589
590
const SDL_Rect * rect)
{
Aug 16, 2008
Aug 16, 2008
591
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
/* set proper drawing color */
GLfloat oldClearColor[4];
data->glGetFloatv(GL_COLOR_CLEAR_VALUE, oldClearColor);
data->glClearColor((GLclampf) r * inv255f, (GLclampf) g * inv255f,
(GLclampf) b * inv255f, (GLclampf) a * inv255f);
data->glScissor(rect->x, window->h - rect->y - rect->h, rect->w, rect->h);
data->glEnable(GL_SCISSOR_TEST);
data->glClear(GL_COLOR_BUFFER_BIT);
data->glDisable(GL_SCISSOR_TEST);
/* reset clear color */
data->glClearColor(oldClearColor[0], oldClearColor[1], oldClearColor[2], oldClearColor[2]);
return 0;
}
static int
Aug 16, 2008
Aug 16, 2008
614
GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
615
616
617
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
{
Aug 16, 2008
Aug 16, 2008
618
619
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
int minx, miny, maxx, maxy;
GLfloat minu, maxu, minv, maxv;
int i;
void *temp_buffer; /* used for reformatting dirty rect pixels */
void *temp_ptr;
if (texturedata->dirty.list) {
SDL_DirtyRect *dirty;
void *pixels;
int bpp = SDL_BYTESPERPIXEL(texture->format);
int pitch = texturedata->pitch;
SetupTextureUpdate(data, texture, pitch);
data->glBindTexture(texturedata->type, texturedata->texture);
for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
SDL_Rect *rect = &dirty->rect;
pixels = (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch + rect->x * bpp);
/* There is no GL_UNPACK_ROW_LENGTH in OpenGLES
we must do this reformatting ourselves(!)
maybe it'd be a good idea to keep a temp buffer around
for this purpose rather than allocating it each time
*/
temp_buffer = SDL_malloc(rect->w * rect->h * bpp);
temp_ptr = temp_buffer;
for (i=0; i<rect->h; i++) {
SDL_memcpy(temp_ptr, pixels, rect->w * bpp);
temp_ptr += rect->w * bpp;
pixels += pitch;
}
data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
rect->w, rect->h, texturedata->format,
texturedata->formattype, temp_buffer);
SDL_free(temp_buffer);
}
SDL_ClearDirtyRects(&texturedata->dirty);
}
data->glBindTexture(texturedata->type, texturedata->texture);
data->glEnable(GL_TEXTURE_2D);
if (texture->modMode) {
data->glColor4f((GLfloat) texture->r * inv255f,
(GLfloat) texture->g * inv255f,
(GLfloat) texture->b * inv255f,
(GLfloat) texture->a * inv255f);
} else {
data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
Aug 16, 2008
Aug 16, 2008
674
if (texture->blendMode != data->blendMode) {
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
switch (texture->blendMode) {
case SDL_TEXTUREBLENDMODE_NONE:
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
data->glDisable(GL_BLEND);
break;
case SDL_TEXTUREBLENDMODE_MASK:
case SDL_TEXTUREBLENDMODE_BLEND:
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);
break;
case SDL_TEXTUREBLENDMODE_ADD:
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
data->glEnable(GL_BLEND);
data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
break;
case SDL_TEXTUREBLENDMODE_MOD:
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
data->glEnable(GL_BLEND);
data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
break;
}
data->blendMode = texture->blendMode;
Aug 16, 2008
Aug 16, 2008
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
}
switch (texture->scaleMode) {
case SDL_TEXTURESCALEMODE_NONE:
case SDL_TEXTURESCALEMODE_FAST:
data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
break;
case SDL_TEXTURESCALEMODE_SLOW:
case SDL_TEXTURESCALEMODE_BEST:
data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
GL_LINEAR);
data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
GL_LINEAR);
break;
}
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
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 */
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
GLint cropRect[4];
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);
data->glDrawTexiOES(dstrect->x, window->h - dstrect->y - dstrect->h, 0, dstrect->w, dstrect->h);
}
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;
GLshort vertices[8];
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;
data->glVertexPointer(2, GL_SHORT, 0, vertices);
data->glEnableClientState(GL_VERTEX_ARRAY);
data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
data->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
return 0;
}
static void
Aug 16, 2008
Aug 16, 2008
769
GLES_RenderPresent(SDL_Renderer * renderer)
770
771
772
773
774
{
SDL_GL_SwapWindow(renderer->window);
}
static void
Aug 16, 2008
Aug 16, 2008
775
GLES_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
Aug 16, 2008
Aug 16, 2008
777
778
GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
if (!data) {
return;
}
if (data->texture) {
glDeleteTextures(1, &data->texture);
}
if (data->pixels) {
SDL_free(data->pixels);
}
SDL_FreeDirtyRects(&data->dirty);
SDL_free(data);
texture->driverdata = NULL;
}
static void
Aug 16, 2008
Aug 16, 2008
795
GLES_DestroyRenderer(SDL_Renderer * renderer)
Aug 16, 2008
Aug 16, 2008
797
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
798
799
800
801
802
803
804
805
806
807
808
809
810
811
if (data) {
if (data->context) {
/* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
SDL_GL_DeleteContext(data->context);
}
SDL_free(data);
}
SDL_free(renderer);
}
#endif /* SDL_VIDEO_RENDER_OGL */
/* vi: set ts=4 sw=4 expandtab: */