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

Latest commit

 

History

History
1419 lines (1257 loc) · 47.5 KB

SDL_photon_render.c

File metadata and controls

1419 lines (1257 loc) · 47.5 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Jan 24, 2010
Jan 24, 2010
3
Copyright (C) 1997-2010 Sam Lantinga
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
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
QNX Photon GUI SDL driver
Copyright (C) 2009 Mike Gorchak
(mike@malva.ua, lestat@i.com.ua)
*/
#include "SDL_config.h"
#include "../SDL_pixels_c.h"
#include "../SDL_yuv_sw_c.h"
#include "SDL_video.h"
#include "SDL_photon_render.h"
#include "SDL_photon.h"
Oct 22, 2009
Oct 22, 2009
37
38
39
40
41
#ifndef Pg_OSC_MEM_LINEAR_ACCESSIBLE
/* For QNX 6.3.2 compatibility */
#define Pg_OSC_MEM_LINEAR_ACCESSIBLE 0
#endif /* Pg_OSC_MEM_LINEAR_ACCESSIBLE */
May 23, 2009
May 23, 2009
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
static SDL_Renderer *photon_createrenderer(SDL_Window * window, Uint32 flags);
static int photon_displaymodechanged(SDL_Renderer * renderer);
static int photon_activaterenderer(SDL_Renderer * renderer);
static int photon_createtexture(SDL_Renderer * renderer,
SDL_Texture * texture);
static int photon_querytexturepixels(SDL_Renderer * renderer,
SDL_Texture * texture, void **pixels,
int *pitch);
static int photon_settexturepalette(SDL_Renderer * renderer,
SDL_Texture * texture,
const SDL_Color * colors, int firstcolor,
int ncolors);
static int photon_gettexturepalette(SDL_Renderer * renderer,
SDL_Texture * texture, SDL_Color * colors,
int firstcolor, int ncolors);
static int photon_updatetexture(SDL_Renderer * renderer,
SDL_Texture * texture, const SDL_Rect * rect,
const void *pixels, int pitch);
static int photon_locktexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, int markDirty,
void **pixels, int *pitch);
static void photon_unlocktexture(SDL_Renderer * renderer,
SDL_Texture * texture);
static void photon_dirtytexture(SDL_Renderer * renderer,
SDL_Texture * texture, int numrects,
const SDL_Rect * rects);
static int photon_renderpoint(SDL_Renderer * renderer, int x, int y);
static int photon_renderline(SDL_Renderer * renderer, int x1, int y1, int x2,
int y2);
static int photon_renderfill(SDL_Renderer * renderer, const SDL_Rect * rect);
static int photon_rendercopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect,
const SDL_Rect * dstrect);
Nov 19, 2009
Nov 19, 2009
75
76
77
78
static int photon_renderreadpixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, void * pixels, int pitch);
static int photon_renderwritepixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, const void * pixels, int pitch);
May 23, 2009
May 23, 2009
79
80
81
82
83
static void photon_renderpresent(SDL_Renderer * renderer);
static void photon_destroytexture(SDL_Renderer * renderer,
SDL_Texture * texture);
static void photon_destroyrenderer(SDL_Renderer * renderer);
Oct 10, 2009
Oct 10, 2009
84
85
static int _photon_recreate_surfaces(SDL_Renderer * renderer);
May 23, 2009
May 23, 2009
86
SDL_RenderDriver photon_renderdriver = {
87
88
photon_createrenderer,
{
May 23, 2009
May 23, 2009
89
90
91
92
93
"photon",
(SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_PRESENTDISCARD |
SDL_RENDERER_ACCELERATED),
Feb 2, 2011
Feb 2, 2011
94
95
9,
{SDL_PIXELFORMAT_RGB555,
May 23, 2009
May 23, 2009
96
SDL_PIXELFORMAT_RGB565,
Oct 10, 2009
Oct 10, 2009
97
SDL_PIXELFORMAT_RGB24,
May 23, 2009
May 23, 2009
98
99
100
101
102
103
104
105
SDL_PIXELFORMAT_RGB888,
SDL_PIXELFORMAT_ARGB8888,
SDL_PIXELFORMAT_YV12,
SDL_PIXELFORMAT_YUY2,
SDL_PIXELFORMAT_UYVY,
SDL_PIXELFORMAT_YVYU},
0,
0}
106
107
};
May 23, 2009
May 23, 2009
108
109
static SDL_Renderer *
photon_createrenderer(SDL_Window * window, Uint32 flags)
110
{
Jan 21, 2010
Jan 21, 2010
111
SDL_VideoDisplay *display = window->display;
May 23, 2009
May 23, 2009
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
SDL_DisplayData *didata = (SDL_DisplayData *) display->driverdata;
SDL_WindowData *wdata = (SDL_WindowData *) window->driverdata;
SDL_Renderer *renderer = NULL;
SDL_RenderData *rdata = NULL;
/* Allocate new renderer structure */
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(SDL_Renderer));
if (renderer == NULL) {
SDL_OutOfMemory();
return NULL;
}
/* Allocate renderer data */
rdata = (SDL_RenderData *) SDL_calloc(1, sizeof(SDL_RenderData));
if (rdata == NULL) {
SDL_free(renderer);
SDL_OutOfMemory();
return NULL;
}
renderer->DisplayModeChanged = photon_displaymodechanged;
renderer->ActivateRenderer = photon_activaterenderer;
renderer->CreateTexture = photon_createtexture;
renderer->QueryTexturePixels = photon_querytexturepixels;
renderer->UpdateTexture = photon_updatetexture;
renderer->LockTexture = photon_locktexture;
renderer->UnlockTexture = photon_unlocktexture;
renderer->DirtyTexture = photon_dirtytexture;
renderer->RenderPoint = photon_renderpoint;
renderer->RenderLine = photon_renderline;
renderer->RenderFill = photon_renderfill;
renderer->RenderCopy = photon_rendercopy;
Nov 20, 2009
Nov 20, 2009
144
145
renderer->RenderReadPixels = photon_renderreadpixels;
renderer->RenderWritePixels = photon_renderwritepixels;
May 23, 2009
May 23, 2009
146
147
148
149
renderer->RenderPresent = photon_renderpresent;
renderer->DestroyTexture = photon_destroytexture;
renderer->DestroyRenderer = photon_destroyrenderer;
renderer->info = photon_renderdriver.info;
Jan 21, 2010
Jan 21, 2010
150
renderer->window = window;
May 23, 2009
May 23, 2009
151
152
renderer->driverdata = rdata;
Oct 12, 2009
Oct 12, 2009
153
154
155
/* Copy direct_mode status */
rdata->direct_mode=didata->direct_mode;
May 23, 2009
May 23, 2009
156
157
158
159
/* Set render acceleration flag in case it is accelerated */
if ((didata->caps & SDL_PHOTON_ACCELERATED) == SDL_PHOTON_ACCELERATED) {
renderer->info.flags = SDL_RENDERER_ACCELERATED;
} else {
Oct 12, 2009
Oct 12, 2009
160
renderer->info.flags = 0;
May 23, 2009
May 23, 2009
161
162
163
164
}
/* Check if upper level requested synchronization on vsync signal */
if ((flags & SDL_RENDERER_PRESENTVSYNC) == SDL_RENDERER_PRESENTVSYNC) {
Oct 12, 2009
Oct 12, 2009
165
166
167
168
169
170
171
172
173
174
if (rdata->direct_mode==SDL_TRUE)
{
/* We can control vsync only in direct mode */
rdata->enable_vsync = SDL_TRUE;
renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
}
else
{
rdata->enable_vsync = SDL_FALSE;
}
May 23, 2009
May 23, 2009
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
} else {
rdata->enable_vsync = SDL_FALSE;
}
/* Check what buffer copy/flip scheme is requested */
rdata->surfaces_count = 0;
if ((flags & SDL_RENDERER_SINGLEBUFFER) == SDL_RENDERER_SINGLEBUFFER) {
if ((flags & SDL_RENDERER_PRESENTDISCARD) ==
SDL_RENDERER_PRESENTDISCARD) {
renderer->info.flags |=
SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD;
} else {
renderer->info.flags |=
SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY;
}
rdata->surfaces_count = 1;
rdata->surface_visible_idx = 0;
rdata->surface_render_idx = 0;
} else {
if ((flags & SDL_RENDERER_PRESENTFLIP2) == SDL_RENDERER_PRESENTFLIP2) {
renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
rdata->surfaces_count = 2;
rdata->surface_visible_idx = 0;
rdata->surface_render_idx = 1;
} else {
if ((flags & SDL_RENDERER_PRESENTFLIP3) ==
SDL_RENDERER_PRESENTFLIP3) {
renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
rdata->surfaces_count = 3;
rdata->surface_visible_idx = 0;
rdata->surface_render_idx = 1;
} else {
renderer->info.flags |=
SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY;
rdata->surfaces_count = 1;
rdata->surface_visible_idx = 0;
rdata->surface_render_idx = 0;
}
}
}
Oct 12, 2009
Oct 12, 2009
216
/* Create new graphics context for the renderer */
Oct 10, 2009
Oct 10, 2009
217
218
219
220
221
222
if (rdata->gc==NULL)
{
rdata->gc=PgCreateGC(0);
PgDefaultGC(rdata->gc);
}
Oct 11, 2009
Oct 11, 2009
223
224
225
226
/* Setup textures supported by current renderer instance */
renderer->info.num_texture_formats=1;
renderer->info.texture_formats[0]=didata->current_mode.format;
Oct 12, 2009
Oct 12, 2009
227
228
229
/* Initialize surfaces */
_photon_recreate_surfaces(renderer);
May 23, 2009
May 23, 2009
230
return renderer;
231
232
}
May 23, 2009
May 23, 2009
233
234
void
photon_addrenderdriver(_THIS)
235
{
May 23, 2009
May 23, 2009
236
uint32_t it;
237
May 23, 2009
May 23, 2009
238
for (it = 0; it < _this->num_displays; it++) {
Dec 1, 2009
Dec 1, 2009
239
SDL_AddRenderDriver(&_this->displays[it], &photon_renderdriver);
May 23, 2009
May 23, 2009
240
}
241
242
243
}
/****************************************************************************/
Oct 13, 2009
Oct 13, 2009
244
/* Renderer helper functions */
245
/****************************************************************************/
Oct 10, 2009
Oct 10, 2009
246
247
static int _photon_recreate_surfaces(SDL_Renderer * renderer)
248
{
May 23, 2009
May 23, 2009
249
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
Oct 10, 2009
Oct 10, 2009
250
251
252
253
254
255
256
257
258
259
260
SDL_VideoDisplay *display = NULL;
SDL_DisplayData *didata = NULL;
SDL_Window *window = NULL;
SDL_WindowData *wdata = NULL;
SDL_VideoData *phdata = NULL;
uint32_t allocate_task=SDL_PHOTON_SURFTYPE_UNKNOWN;
int32_t status;
/* Obtain window and display structures */
window=SDL_GetWindowFromID(renderer->window);
wdata=(SDL_WindowData*)window->driverdata;
Jan 21, 2010
Jan 21, 2010
261
display=window->display;
Oct 10, 2009
Oct 10, 2009
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
didata=(SDL_DisplayData *) display->driverdata;
phdata=(SDL_VideoData *) display->device->driverdata;
/* Check if it is OpenGL ES window */
if ((window->flags & SDL_WINDOW_OPENGL) == SDL_WINDOW_OPENGL) {
/* If so, do not waste surfaces */
rdata->surfaces_type=SDL_PHOTON_SURFTYPE_UNKNOWN;
return 0;
}
if (rdata->surfaces_type==SDL_PHOTON_SURFTYPE_UNKNOWN)
{
/* Try to allocate offscreen surfaces */
allocate_task=SDL_PHOTON_SURFTYPE_OFFSCREEN;
}
else
{
uint32_t it;
if (rdata->surfaces_type==SDL_PHOTON_SURFTYPE_OFFSCREEN)
{
/* Create offscreen surfaces */
allocate_task=SDL_PHOTON_SURFTYPE_OFFSCREEN;
Nov 20, 2009
Nov 20, 2009
286
287
288
289
/* Before destroying surfaces, be sure, that rendering was completed */
PgFlush();
PgWaitHWIdle();
Oct 10, 2009
Oct 10, 2009
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* Destroy current surfaces */
for (it=0; it<SDL_PHOTON_MAX_SURFACES; it++)
{
if (rdata->osurfaces[it] != NULL)
{
PhDCRelease(rdata->osurfaces[it]);
rdata->osurfaces[it] = NULL;
}
}
}
else
{
if (rdata->surfaces_type==SDL_PHOTON_SURFTYPE_PHIMAGE)
{
/* Create shared phimage surfaces */
allocate_task=SDL_PHOTON_SURFTYPE_PHIMAGE;
/* Destroy current surfaces */
for (it=0; it<SDL_PHOTON_MAX_SURFACES; it++)
{
if (rdata->pcontexts[it]!=NULL)
{
PmMemReleaseMC(rdata->pcontexts[it]);
rdata->pcontexts[it]=NULL;
}
if (rdata->psurfaces[it]!=NULL)
{
if (rdata->psurfaces[it]->palette!=NULL)
{
SDL_free(rdata->psurfaces[it]->palette);
rdata->psurfaces[it]->palette=NULL;
}
/* Destroy shared memory for PhImage_t */
PgShmemDestroy(rdata->psurfaces[it]->image);
rdata->psurfaces[it]->image=NULL;
SDL_free(rdata->psurfaces[it]);
rdata->psurfaces[it]=NULL;
}
}
}
}
}
/* Check if current device is not the same as target */
if (phdata->current_device_id != didata->device_id) {
/* Set target device as default for Pd and Pg functions */
status = PdSetTargetDevice(NULL, phdata->rid[didata->device_id]);
if (status != 0) {
SDL_SetError("Photon: Can't set default target device\n");
return -1;
}
phdata->current_device_id = didata->device_id;
}
343
Oct 10, 2009
Oct 10, 2009
344
345
346
347
348
349
350
351
352
353
354
switch (allocate_task)
{
case SDL_PHOTON_SURFTYPE_OFFSCREEN:
{
int32_t it;
int32_t jt;
/* Try the hardware accelerated offscreen surfaces first */
for (it=0; it<rdata->surfaces_count; it++)
{
rdata->osurfaces[it]=PdCreateOffscreenContext(0, window->w, window->h,
Oct 11, 2009
Oct 11, 2009
355
Pg_OSC_MEM_LINEAR_ACCESSIBLE | Pg_OSC_MEM_PAGE_ALIGN |
Oct 10, 2009
Oct 10, 2009
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
392
393
394
395
396
397
398
399
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
428
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
/* in case if 2D acceleration is available use it */
Pg_OSC_MEM_2D_WRITABLE | Pg_OSC_MEM_2D_READABLE);
/* If we can't create an offscreen surface, then fallback to software */
if (rdata->osurfaces[it]==NULL)
{
/* Destroy previously allocated surface(s) */
for (jt = it - 1; jt > 0; jt--)
{
PhDCRelease(rdata->osurfaces[jt]);
rdata->osurfaces[jt] = NULL;
}
break;
}
}
/* Check if all required surfaces have been created */
if (rdata->osurfaces[0]!=NULL)
{
rdata->surfaces_type=SDL_PHOTON_SURFTYPE_OFFSCREEN;
/* exit from switch if surfaces have been created */
break;
}
else
{
/* else fall through to software phimage surface allocation */
}
}
/* fall through */
case SDL_PHOTON_SURFTYPE_PHIMAGE:
{
int32_t it;
int32_t jt;
uint32_t image_pfmt=photon_sdl_to_image_pixelformat(didata->current_mode.format);
/* Try to allocate the software surfaces in shared memory */
for (it=0; it<rdata->surfaces_count; it++)
{
/* If this surface with palette, create a palette space */
if (image_pfmt==Pg_IMAGE_PALETTE_BYTE)
{
rdata->psurfaces[it]=PhCreateImage(NULL, window->w, window->h,
image_pfmt, NULL, 256, 1);
}
else
{
rdata->psurfaces[it]=PhCreateImage(NULL, window->w, window->h,
image_pfmt, NULL, 0, 1);
}
if (rdata->psurfaces[it]!=NULL)
{
PhPoint_t translation={0, 0};
PhDim_t dimension={window->w, window->h};
/* Create memory context for PhImage_t */
rdata->pcontexts[it]=PmMemCreateMC(rdata->psurfaces[it], &dimension, &translation);
}
if ((rdata->psurfaces[it]==NULL) || (rdata->pcontexts[it]==NULL))
{
/* Destroy previously allocated surface(s) */
for (jt = it - 1; jt > 0; jt--)
{
if (rdata->pcontexts[jt]!=NULL)
{
PmMemReleaseMC(rdata->pcontexts[it]);
rdata->pcontexts[jt]=NULL;
}
if (rdata->psurfaces[jt]!=NULL)
{
if (rdata->psurfaces[jt]->palette!=NULL)
{
SDL_free(rdata->psurfaces[jt]->palette);
rdata->psurfaces[jt]->palette=NULL;
}
/* Destroy shared memory for PhImage_t */
PgShmemDestroy(rdata->psurfaces[jt]->image);
rdata->psurfaces[jt]->image=NULL;
SDL_free(rdata->psurfaces[jt]);
rdata->psurfaces[jt] = NULL;
}
}
break;
}
}
/* Check if all required surfaces have been created */
if (rdata->psurfaces[0]!=NULL)
{
rdata->surfaces_type=SDL_PHOTON_SURFTYPE_PHIMAGE;
}
else
{
rdata->surfaces_type=SDL_PHOTON_SURFTYPE_UNKNOWN;
}
}
break;
case SDL_PHOTON_SURFTYPE_UNKNOWN:
Oct 11, 2009
Oct 11, 2009
454
default:
Oct 10, 2009
Oct 10, 2009
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
{
/* do nothing with surface allocation */
rdata->surfaces_type=SDL_PHOTON_SURFTYPE_UNKNOWN;
}
break;
}
/* Check if one of two allocation scheme was successful */
if (rdata->surfaces_type==SDL_PHOTON_SURFTYPE_UNKNOWN)
{
SDL_SetError("Photon: primary surface(s) allocation failure");
return -1;
}
/* Store current surface dimensions */
rdata->window_width=window->w;
rdata->window_height=window->h;
/* If current copy/flip scheme is single buffer, then set initial parameters */
if ((renderer->info.flags & SDL_RENDERER_SINGLEBUFFER)==SDL_RENDERER_SINGLEBUFFER)
{
rdata->surface_visible_idx = 0;
rdata->surface_render_idx = 0;
}
/* If current copy/flip scheme is double buffer, then set initial parameters */
if ((renderer->info.flags & SDL_RENDERER_PRESENTFLIP2)==SDL_RENDERER_PRESENTFLIP2)
{
rdata->surface_visible_idx = 0;
rdata->surface_render_idx = 1;
}
/* If current copy/flip scheme is triple buffer, then set initial parameters */
if ((renderer->info.flags & SDL_RENDERER_PRESENTFLIP3)==SDL_RENDERER_PRESENTFLIP3)
{
rdata->surface_visible_idx = 0;
rdata->surface_render_idx = 1;
}
493
Oct 10, 2009
Oct 10, 2009
494
495
496
497
498
499
500
501
502
switch (rdata->surfaces_type)
{
case SDL_PHOTON_SURFTYPE_OFFSCREEN:
PgSetGCCx(rdata->osurfaces[rdata->surface_render_idx], rdata->gc);
break;
case SDL_PHOTON_SURFTYPE_PHIMAGE:
PgSetGCCx(rdata->pcontexts[rdata->surface_render_idx], rdata->gc);
break;
case SDL_PHOTON_SURFTYPE_UNKNOWN:
Oct 11, 2009
Oct 11, 2009
503
default:
Oct 10, 2009
Oct 10, 2009
504
505
break;
}
506
May 23, 2009
May 23, 2009
507
return 0;
508
509
}
Oct 10, 2009
Oct 10, 2009
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
int _photon_update_rectangles(SDL_Renderer* renderer, PhRect_t* rect)
{
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
SDL_Window *window = window=SDL_GetWindowFromID(renderer->window);
SDL_WindowData *wdata = (SDL_WindowData *) window->driverdata;
PhPoint_t src_point;
/* If currently single buffer is in use, we have to flush all data */
if (rdata->surface_render_idx==rdata->surface_visible_idx)
{
/* Flush all undrawn graphics data to surface */
switch (rdata->surfaces_type)
{
case SDL_PHOTON_SURFTYPE_OFFSCREEN:
PgFlushCx(rdata->osurfaces[rdata->surface_visible_idx]);
PgWaitHWIdle();
break;
case SDL_PHOTON_SURFTYPE_PHIMAGE:
PmMemFlush(rdata->pcontexts[rdata->surface_visible_idx], rdata->psurfaces[rdata->surface_visible_idx]);
break;
case SDL_PHOTON_SURFTYPE_UNKNOWN:
Oct 11, 2009
Oct 11, 2009
531
default:
Oct 10, 2009
Oct 10, 2009
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
return;
}
}
PgSetRegionCx(PhDCGetCurrent(), PtWidgetRid(wdata->window));
src_point.x = rect->ul.x;
src_point.y = rect->ul.y;
switch (rdata->surfaces_type)
{
case SDL_PHOTON_SURFTYPE_OFFSCREEN:
PgContextBlit(rdata->osurfaces[rdata->surface_visible_idx], rect, NULL, rect);
break;
case SDL_PHOTON_SURFTYPE_PHIMAGE:
PgDrawPhImageRectv(&src_point, rdata->psurfaces[rdata->surface_visible_idx], rect, 0);
break;
case SDL_PHOTON_SURFTYPE_UNKNOWN:
Oct 11, 2009
Oct 11, 2009
550
default:
Oct 10, 2009
Oct 10, 2009
551
552
553
554
break;
}
}
Oct 13, 2009
Oct 13, 2009
555
556
557
558
559
560
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
int _photon_set_blending(SDL_Renderer* renderer, uint32_t blendmode, uint32_t globalalpha, uint32_t blendsource)
{
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
/* Switch on requested graphics context modifiers */
switch (blendmode)
{
case SDL_BLENDMODE_BLEND:
/* Enable and set chroma key and alpha blending */
if (blendsource==SDL_PHOTON_TEXTURE_BLEND)
{
PgSetChromaCx(rdata->gc, PgRGB(255, 255, 255), Pg_CHROMA_SRC_MATCH | Pg_CHROMA_NODRAW);
PgChromaOnCx(rdata->gc);
}
PgSetAlphaCx(rdata->gc, Pg_ALPHA_OP_SRC_GLOBAL | Pg_BLEND_SRC_As | Pg_BLEND_DST_1mAs, NULL, NULL, globalalpha, 0);
PgAlphaOnCx(rdata->gc);
break;
case SDL_BLENDMODE_ADD:
/* Enable and set chroma key and alpha blending */
if (blendsource==SDL_PHOTON_TEXTURE_BLEND)
{
PgSetChromaCx(rdata->gc, PgRGB(255, 255, 255), Pg_CHROMA_SRC_MATCH | Pg_CHROMA_NODRAW);
PgChromaOnCx(rdata->gc);
}
PgSetAlphaCx(rdata->gc, Pg_ALPHA_OP_SRC_GLOBAL | Pg_BLEND_SRC_As | Pg_BLEND_DST_1, NULL, NULL, globalalpha, 0);
PgAlphaOnCx(rdata->gc);
break;
case SDL_BLENDMODE_NONE:
/* Do nothing */
break;
default:
return -1;
}
return 0;
}
int _photon_reset_blending(SDL_Renderer* renderer, uint32_t blendmode, uint32_t blendsource)
{
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
/* Switch off graphics context modifiers */
switch (blendmode)
{
case SDL_BLENDMODE_BLEND:
/* Disable chroma key and alpha blending */
if (blendsource==SDL_PHOTON_TEXTURE_BLEND)
{
PgChromaOffCx(rdata->gc);
}
PgAlphaOffCx(rdata->gc);
break;
case SDL_BLENDMODE_ADD:
/* Disable chroma key and alpha blending */
if (blendsource==SDL_PHOTON_TEXTURE_BLEND)
{
PgChromaOffCx(rdata->gc);
}
PgAlphaOffCx(rdata->gc);
break;
case SDL_BLENDMODE_NONE:
/* Do nothing */
break;
default:
return -1;
}
return 0;
}
Oct 10, 2009
Oct 10, 2009
625
626
627
628
/****************************************************************************/
/* SDL render interface */
/****************************************************************************/
May 23, 2009
May 23, 2009
629
630
static int
photon_activaterenderer(SDL_Renderer * renderer)
631
{
Oct 10, 2009
Oct 10, 2009
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
SDL_WindowData *wdata = (SDL_WindowData *)window->driverdata;
if ((rdata->window_width!=window->w) || (rdata->window_height!=window->h))
{
return _photon_recreate_surfaces(renderer);
}
switch (rdata->surfaces_type)
{
case SDL_PHOTON_SURFTYPE_OFFSCREEN:
PgSetGCCx(rdata->osurfaces[rdata->surface_render_idx], rdata->gc);
break;
case SDL_PHOTON_SURFTYPE_PHIMAGE:
PgSetGCCx(rdata->pcontexts[rdata->surface_render_idx], rdata->gc);
break;
case SDL_PHOTON_SURFTYPE_UNKNOWN:
Oct 11, 2009
Oct 11, 2009
650
default:
Oct 10, 2009
Oct 10, 2009
651
652
653
654
655
break;
}
return 0;
}
656
Oct 10, 2009
Oct 10, 2009
657
658
659
static int
photon_displaymodechanged(SDL_Renderer * renderer)
{
Oct 12, 2009
Oct 12, 2009
660
661
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
Jan 21, 2010
Jan 21, 2010
662
SDL_VideoDisplay *display = window->display;
Oct 12, 2009
Oct 12, 2009
663
664
665
666
667
668
SDL_DisplayData *didata = (SDL_DisplayData *) display->driverdata;
/* Copy direct_mode status */
rdata->direct_mode=didata->direct_mode;
/* Update the surfaces */
Oct 10, 2009
Oct 10, 2009
669
return _photon_recreate_surfaces(renderer);
670
671
}
May 23, 2009
May 23, 2009
672
673
static int
photon_createtexture(SDL_Renderer * renderer, SDL_Texture * texture)
674
{
Oct 11, 2009
Oct 11, 2009
675
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
May 23, 2009
May 23, 2009
676
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
Jan 21, 2010
Jan 21, 2010
677
SDL_VideoDisplay *display = window->display;
Oct 11, 2009
Oct 11, 2009
678
SDL_DisplayData *didata = (SDL_DisplayData *) display->driverdata;
May 23, 2009
May 23, 2009
679
SDL_TextureData *tdata = NULL;
Oct 11, 2009
Oct 11, 2009
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
uint32_t it;
/* Check, if it is not initialized */
if (rdata->surfaces_type==SDL_PHOTON_SURFTYPE_UNKNOWN)
{
SDL_SetError("Photon: can't create texture for OpenGL ES window");
return -1;
}
/* Check if requested texture format is supported */
for (it=0; it<renderer->info.num_texture_formats; it++)
{
if (renderer->info.texture_formats[it]==texture->format)
{
break;
}
}
if (it==renderer->info.num_texture_formats)
{
SDL_SetError("Photon: requested texture format is not supported");
return -1;
}
May 23, 2009
May 23, 2009
702
703
704
705
706
707
708
709
710
711
/* Allocate texture driver data */
tdata = (SDL_TextureData *) SDL_calloc(1, sizeof(SDL_TextureData));
if (tdata == NULL) {
SDL_OutOfMemory();
return -1;
}
/* Set texture driver data */
texture->driverdata = tdata;
Oct 11, 2009
Oct 11, 2009
712
713
714
715
716
717
718
719
720
721
722
/* Try offscreen allocation only in case if displayable buffers are also offscreen */
if (rdata->surfaces_type==SDL_PHOTON_SURFTYPE_OFFSCREEN)
{
/* Try to allocate offscreen memory first */
tdata->osurface=PdCreateOffscreenContext(0, texture->w, texture->h,
Pg_OSC_MEM_LINEAR_ACCESSIBLE | Pg_OSC_MEM_PAGE_ALIGN |
/* in case if 2D acceleration is available use it */
Pg_OSC_MEM_2D_WRITABLE | Pg_OSC_MEM_2D_READABLE);
}
Oct 12, 2009
Oct 12, 2009
723
/* Check if offscreen allocation has been failed or not performed */
Oct 11, 2009
Oct 11, 2009
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
769
770
771
772
773
if (tdata->osurface==NULL)
{
PhPoint_t translation={0, 0};
PhDim_t dimension={texture->w, texture->h};
uint32_t image_pfmt=photon_sdl_to_image_pixelformat(didata->current_mode.format);
/* Allocate photon image */
if (image_pfmt==Pg_IMAGE_PALETTE_BYTE)
{
tdata->psurface=PhCreateImage(NULL, texture->w, texture->h,
image_pfmt, NULL, 256, 1);
}
else
{
tdata->psurface=PhCreateImage(NULL, texture->w, texture->h,
image_pfmt, NULL, 0, 1);
}
if (tdata->psurface==NULL)
{
return -1;
}
/* Create memory context for PhImage_t */
tdata->pcontext=PmMemCreateMC(tdata->psurface, &dimension, &translation);
if (tdata->pcontext==NULL)
{
/* Destroy PhImage */
if (tdata->psurface!=NULL)
{
if (tdata->psurface->palette!=NULL)
{
SDL_free(tdata->psurface->palette);
tdata->psurface->palette=NULL;
}
/* Destroy shared memory for PhImage_t */
PgShmemDestroy(tdata->psurface->image);
tdata->psurface->image=NULL;
SDL_free(tdata->psurface);
tdata->psurface=NULL;
}
}
tdata->surface_type=SDL_PHOTON_SURFTYPE_PHIMAGE;
}
else
{
tdata->surface_type=SDL_PHOTON_SURFTYPE_OFFSCREEN;
}
return 0;
774
775
}
May 23, 2009
May 23, 2009
776
777
778
static int
photon_querytexturepixels(SDL_Renderer * renderer, SDL_Texture * texture,
void **pixels, int *pitch)
779
{
Oct 11, 2009
Oct 11, 2009
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
SDL_TextureData* tdata=(SDL_TextureData*)texture->driverdata;
/* Check, if it is not initialized */
if (rdata->surfaces_type==SDL_PHOTON_SURFTYPE_UNKNOWN)
{
SDL_SetError("Photon: can't query texture pixels for OpenGL ES window");
return -1;
}
/* Clear outcoming parameters */
*pixels=NULL;
*pitch=0;
switch (tdata->surface_type)
{
case SDL_PHOTON_SURFTYPE_OFFSCREEN:
*pixels=(void*)PdGetOffscreenContextPtr(tdata->osurface);
*pitch=tdata->osurface->pitch;
break;
case SDL_PHOTON_SURFTYPE_PHIMAGE:
*pixels=(void*)tdata->psurface->image;
*pitch=tdata->psurface->bpl;
break;
default:
break;
}
return 0;
809
810
}
May 23, 2009
May 23, 2009
811
812
813
static int
photon_updatetexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
814
{
Oct 11, 2009
Oct 11, 2009
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
SDL_TextureData* tdata=(SDL_TextureData*)texture->driverdata;
uint8_t* src=(uint8_t*)pixels;
uint8_t* dst;
uint32_t dst_pitch;
uint32_t it;
uint32_t copy_length;
/* Check, if it is not initialized */
if (rdata->surfaces_type==SDL_PHOTON_SURFTYPE_UNKNOWN)
{
SDL_SetError("Photon: can't update texture for OpenGL ES window");
return -1;
}
switch (tdata->surface_type)
{
case SDL_PHOTON_SURFTYPE_OFFSCREEN:
dst=(uint8_t*)PdGetOffscreenContextPtr(tdata->osurface);
dst_pitch=tdata->osurface->pitch;
if (dst==NULL)
{
SDL_SetError("Photon: can't get pointer to texture surface");
return -1;
}
break;
case SDL_PHOTON_SURFTYPE_PHIMAGE:
dst=(uint8_t*)tdata->psurface->image;
dst_pitch=tdata->psurface->bpl;
if (dst==NULL)
{
SDL_SetError("Photon: can't get pointer to texture surface");
return -1;
}
break;
default:
SDL_SetError("Photon: invalid internal surface type");
return -1;
}
dst+=rect->y * dst_pitch + rect->x * SDL_BYTESPERPIXEL(texture->format);
copy_length=rect->w * SDL_BYTESPERPIXEL(texture->format);
for (it = 0; it < rect->h; it++)
{
SDL_memcpy(dst, src, copy_length);
src+=pitch;
dst+=dst_pitch;
}
return 0;
866
867
}
May 23, 2009
May 23, 2009
868
869
870
871
static int
photon_locktexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, int markDirty, void **pixels,
int *pitch)
872
{
Oct 11, 2009
Oct 11, 2009
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
SDL_TextureData* tdata=(SDL_TextureData*)texture->driverdata;
/* Check, if it is not initialized */
if (rdata->surfaces_type==SDL_PHOTON_SURFTYPE_UNKNOWN)
{
SDL_SetError("Photon: can't lock texture for OpenGL ES window");
return -1;
}
/* Clear outcoming parameters */
*pixels=NULL;
*pitch=0;
switch (tdata->surface_type)
{
case SDL_PHOTON_SURFTYPE_OFFSCREEN:
*pixels=(void*)((uint8_t*)PdGetOffscreenContextPtr(tdata->osurface) +
rect->y * tdata->osurface->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch=tdata->osurface->pitch;
break;
case SDL_PHOTON_SURFTYPE_PHIMAGE:
*pixels=(void*)((uint8_t*)tdata->psurface->image +
rect->y * tdata->osurface->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch=tdata->psurface->bpl;
break;
default:
break;
}
return 0;
906
907
}
May 23, 2009
May 23, 2009
908
909
static void
photon_unlocktexture(SDL_Renderer * renderer, SDL_Texture * texture)
910
{
Oct 11, 2009
Oct 11, 2009
911
912
913
914
915
916
917
918
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
/* Check, if it is not initialized */
if (rdata->surfaces_type==SDL_PHOTON_SURFTYPE_UNKNOWN)
{
SDL_SetError("Photon: can't unlock texture for OpenGL ES window");
return;
}
919
920
}
May 23, 2009
May 23, 2009
921
922
923
static void
photon_dirtytexture(SDL_Renderer * renderer, SDL_Texture * texture,
int numrects, const SDL_Rect * rects)
924
{
Oct 12, 2009
Oct 12, 2009
925
926
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
Oct 12, 2009
Oct 12, 2009
927
928
929
930
931
932
/* Check, if it is not initialized */
if (rdata->surfaces_type==SDL_PHOTON_SURFTYPE_UNKNOWN)
{
SDL_SetError("Photon: can't update dirty texture for OpenGL ES window");
return;
}
933
934
}
May 23, 2009
May 23, 2009
935
936
static int
photon_renderpoint(SDL_Renderer * renderer, int x, int y)
937
{
Oct 10, 2009
Oct 10, 2009
938
939
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
Oct 11, 2009
Oct 11, 2009
940
941
942
943
944
945
946
/* Check, if it is not initialized */
if (rdata->surfaces_type==SDL_PHOTON_SURFTYPE_UNKNOWN)
{
SDL_SetError("Photon: can't render point in OpenGL ES window");
return -1;
}
Oct 13, 2009
Oct 13, 2009
947
948
949
/* Enable blending, if requested */
_photon_set_blending(renderer, renderer->blendMode, renderer->a, SDL_PHOTON_DRAW_BLEND);
Oct 10, 2009
Oct 10, 2009
950
951
952
953
954
955
956
957
958
switch (rdata->surfaces_type)
{
case SDL_PHOTON_SURFTYPE_OFFSCREEN:
PgDrawIPixelCx(rdata->osurfaces[rdata->surface_render_idx], x, y);
break;
case SDL_PHOTON_SURFTYPE_PHIMAGE:
PgDrawIPixelCx(rdata->pcontexts[rdata->surface_render_idx], x, y);
break;
case SDL_PHOTON_SURFTYPE_UNKNOWN:
Oct 11, 2009
Oct 11, 2009
959
default:
Oct 10, 2009
Oct 10, 2009
960
961
break;
}
Oct 11, 2009
Oct 11, 2009
962
Oct 13, 2009
Oct 13, 2009
963
964
965
/* Disable blending, if it was enabled */
_photon_reset_blending(renderer, renderer->blendMode, SDL_PHOTON_DRAW_BLEND);
Oct 11, 2009
Oct 11, 2009
966
return 0;
967
968
}
May 23, 2009
May 23, 2009
969
970
static int
photon_renderline(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
971
{
Oct 10, 2009
Oct 10, 2009
972
973
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
Oct 11, 2009
Oct 11, 2009
974
975
976
977
978
979
980
/* Check, if it is not initialized */
if (rdata->surfaces_type==SDL_PHOTON_SURFTYPE_UNKNOWN)
{
SDL_SetError("Photon: can't render line in OpenGL ES window");
return -1;
}
Oct 13, 2009
Oct 13, 2009
981
982
983
/* Enable blending, if requested */
_photon_set_blending(renderer, renderer->blendMode, renderer->a, SDL_PHOTON_DRAW_BLEND);
Oct 10, 2009
Oct 10, 2009
984
985
986
987
988
989
990
991
992
switch (rdata->surfaces_type)
{
case SDL_PHOTON_SURFTYPE_OFFSCREEN:
PgDrawILineCx(rdata->osurfaces[rdata->surface_render_idx], x1, y1, x2, y2);
break;
case SDL_PHOTON_SURFTYPE_PHIMAGE:
PgDrawILineCx(rdata->pcontexts[rdata->surface_render_idx], x1, y1, x2, y2);
break;
case SDL_PHOTON_SURFTYPE_UNKNOWN:
Oct 11, 2009
Oct 11, 2009
993
default:
Oct 10, 2009
Oct 10, 2009
994
995
break;
}
Oct 11, 2009
Oct 11, 2009
996
Oct 13, 2009
Oct 13, 2009
997
998
999
/* Disable blending, if it was enabled */
_photon_reset_blending(renderer, renderer->blendMode, SDL_PHOTON_DRAW_BLEND);
Oct 11, 2009
Oct 11, 2009
1000
return 0;