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

Latest commit

 

History

History
1471 lines (1301 loc) · 49 KB

SDL_photon_render.c

File metadata and controls

1471 lines (1301 loc) · 49 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),
Oct 10, 2009
Oct 10, 2009
94
95
10,
{SDL_PIXELFORMAT_INDEX8,
May 23, 2009
May 23, 2009
96
97
SDL_PIXELFORMAT_RGB555,
SDL_PIXELFORMAT_RGB565,
Oct 10, 2009
Oct 10, 2009
98
SDL_PIXELFORMAT_RGB24,
May 23, 2009
May 23, 2009
99
100
101
102
103
104
105
106
SDL_PIXELFORMAT_RGB888,
SDL_PIXELFORMAT_ARGB8888,
SDL_PIXELFORMAT_YV12,
SDL_PIXELFORMAT_YUY2,
SDL_PIXELFORMAT_UYVY,
SDL_PIXELFORMAT_YVYU},
0,
0}
107
108
};
May 23, 2009
May 23, 2009
109
110
static SDL_Renderer *
photon_createrenderer(SDL_Window * window, Uint32 flags)
111
{
Jan 21, 2010
Jan 21, 2010
112
SDL_VideoDisplay *display = window->display;
May 23, 2009
May 23, 2009
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
144
145
146
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->SetTexturePalette = photon_settexturepalette;
renderer->GetTexturePalette = photon_gettexturepalette;
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
147
148
renderer->RenderReadPixels = photon_renderreadpixels;
renderer->RenderWritePixels = photon_renderwritepixels;
May 23, 2009
May 23, 2009
149
150
151
152
renderer->RenderPresent = photon_renderpresent;
renderer->DestroyTexture = photon_destroytexture;
renderer->DestroyRenderer = photon_destroyrenderer;
renderer->info = photon_renderdriver.info;
Jan 21, 2010
Jan 21, 2010
153
renderer->window = window;
May 23, 2009
May 23, 2009
154
155
renderer->driverdata = rdata;
Oct 12, 2009
Oct 12, 2009
156
157
158
/* Copy direct_mode status */
rdata->direct_mode=didata->direct_mode;
May 23, 2009
May 23, 2009
159
160
161
162
/* 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
163
renderer->info.flags = 0;
May 23, 2009
May 23, 2009
164
165
166
167
}
/* Check if upper level requested synchronization on vsync signal */
if ((flags & SDL_RENDERER_PRESENTVSYNC) == SDL_RENDERER_PRESENTVSYNC) {
Oct 12, 2009
Oct 12, 2009
168
169
170
171
172
173
174
175
176
177
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
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
216
217
218
} 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
219
/* Create new graphics context for the renderer */
Oct 10, 2009
Oct 10, 2009
220
221
222
223
224
225
if (rdata->gc==NULL)
{
rdata->gc=PgCreateGC(0);
PgDefaultGC(rdata->gc);
}
Oct 11, 2009
Oct 11, 2009
226
227
228
229
/* 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
230
231
232
/* Initialize surfaces */
_photon_recreate_surfaces(renderer);
May 23, 2009
May 23, 2009
233
return renderer;
234
235
}
May 23, 2009
May 23, 2009
236
237
void
photon_addrenderdriver(_THIS)
238
{
May 23, 2009
May 23, 2009
239
uint32_t it;
240
May 23, 2009
May 23, 2009
241
for (it = 0; it < _this->num_displays; it++) {
Dec 1, 2009
Dec 1, 2009
242
SDL_AddRenderDriver(&_this->displays[it], &photon_renderdriver);
May 23, 2009
May 23, 2009
243
}
244
245
246
}
/****************************************************************************/
Oct 13, 2009
Oct 13, 2009
247
/* Renderer helper functions */
248
/****************************************************************************/
Oct 10, 2009
Oct 10, 2009
249
250
static int _photon_recreate_surfaces(SDL_Renderer * renderer)
251
{
May 23, 2009
May 23, 2009
252
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
Oct 10, 2009
Oct 10, 2009
253
254
255
256
257
258
259
260
261
262
263
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
264
display=window->display;
Oct 10, 2009
Oct 10, 2009
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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
289
290
291
292
/* Before destroying surfaces, be sure, that rendering was completed */
PgFlush();
PgWaitHWIdle();
Oct 10, 2009
Oct 10, 2009
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
343
344
345
/* 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;
}
346
Oct 10, 2009
Oct 10, 2009
347
348
349
350
351
352
353
354
355
356
357
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
358
Pg_OSC_MEM_LINEAR_ACCESSIBLE | Pg_OSC_MEM_PAGE_ALIGN |
Oct 10, 2009
Oct 10, 2009
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
454
455
456
/* 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
457
default:
Oct 10, 2009
Oct 10, 2009
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
493
494
495
{
/* 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;
}
496
Oct 10, 2009
Oct 10, 2009
497
498
499
500
501
502
503
504
505
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
506
default:
Oct 10, 2009
Oct 10, 2009
507
508
break;
}
509
May 23, 2009
May 23, 2009
510
return 0;
511
512
}
Oct 10, 2009
Oct 10, 2009
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
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
534
default:
Oct 10, 2009
Oct 10, 2009
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
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
553
default:
Oct 10, 2009
Oct 10, 2009
554
555
556
557
break;
}
}
Oct 13, 2009
Oct 13, 2009
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
625
626
627
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
628
629
630
631
/****************************************************************************/
/* SDL render interface */
/****************************************************************************/
May 23, 2009
May 23, 2009
632
633
static int
photon_activaterenderer(SDL_Renderer * renderer)
634
{
Oct 10, 2009
Oct 10, 2009
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
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
653
default:
Oct 10, 2009
Oct 10, 2009
654
655
656
657
658
break;
}
return 0;
}
659
Oct 10, 2009
Oct 10, 2009
660
661
662
static int
photon_displaymodechanged(SDL_Renderer * renderer)
{
Oct 12, 2009
Oct 12, 2009
663
664
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
Jan 21, 2010
Jan 21, 2010
665
SDL_VideoDisplay *display = window->display;
Oct 12, 2009
Oct 12, 2009
666
667
668
669
670
671
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
672
return _photon_recreate_surfaces(renderer);
673
674
}
May 23, 2009
May 23, 2009
675
676
static int
photon_createtexture(SDL_Renderer * renderer, SDL_Texture * texture)
677
{
Oct 11, 2009
Oct 11, 2009
678
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
May 23, 2009
May 23, 2009
679
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
Jan 21, 2010
Jan 21, 2010
680
SDL_VideoDisplay *display = window->display;
Oct 11, 2009
Oct 11, 2009
681
SDL_DisplayData *didata = (SDL_DisplayData *) display->driverdata;
May 23, 2009
May 23, 2009
682
SDL_TextureData *tdata = NULL;
Oct 11, 2009
Oct 11, 2009
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
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
705
706
707
708
709
710
711
712
713
714
/* 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
715
716
717
718
719
720
721
722
723
724
725
/* 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
726
/* Check if offscreen allocation has been failed or not performed */
Oct 11, 2009
Oct 11, 2009
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
774
775
776
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;
777
778
}
May 23, 2009
May 23, 2009
779
780
781
static int
photon_querytexturepixels(SDL_Renderer * renderer, SDL_Texture * texture,
void **pixels, int *pitch)
782
{
Oct 11, 2009
Oct 11, 2009
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
809
810
811
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;
812
813
}
May 23, 2009
May 23, 2009
814
815
816
817
static int
photon_settexturepalette(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Color * colors, int firstcolor,
int ncolors)
818
{
Oct 11, 2009
Oct 11, 2009
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
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 set texture palette for OpenGL ES window");
return -1;
}
if (texture->format!=SDL_PIXELFORMAT_INDEX8)
{
SDL_SetError("Photon: can't set palette for non-paletted texture");
return -1;
}
Oct 12, 2009
Oct 12, 2009
835
SDL_Unsupported();
Oct 11, 2009
Oct 11, 2009
836
return -1;
837
838
}
May 23, 2009
May 23, 2009
839
840
841
static int
photon_gettexturepalette(SDL_Renderer * renderer, SDL_Texture * texture,
SDL_Color * colors, int firstcolor, int ncolors)
842
{
Oct 11, 2009
Oct 11, 2009
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
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 return texture palette for OpenGL ES window");
return -1;
}
if (texture->format!=SDL_PIXELFORMAT_INDEX8)
{
SDL_SetError("Photon: can't return palette for non-paletted texture");
return -1;
}
Oct 12, 2009
Oct 12, 2009
859
SDL_Unsupported();
Oct 11, 2009
Oct 11, 2009
860
return -1;
861
862
}
May 23, 2009
May 23, 2009
863
864
865
static int
photon_updatetexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
866
{
Oct 11, 2009
Oct 11, 2009
867
868
869
870
871
872
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
906
907
908
909
910
911
912
913
914
915
916
917
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;
918
919
}
May 23, 2009
May 23, 2009
920
921
922
923
static int
photon_locktexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, int markDirty, void **pixels,
int *pitch)
924
{
Oct 11, 2009
Oct 11, 2009
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
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;
958
959
}
May 23, 2009
May 23, 2009
960
961
static void
photon_unlocktexture(SDL_Renderer * renderer, SDL_Texture * texture)
962
{
Oct 11, 2009
Oct 11, 2009
963
964
965
966
967
968
969
970
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;
}
971
972
}
May 23, 2009
May 23, 2009
973
974
975
static void
photon_dirtytexture(SDL_Renderer * renderer, SDL_Texture * texture,
int numrects, const SDL_Rect * rects)
976
{
Oct 12, 2009
Oct 12, 2009
977
978
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
Oct 12, 2009
Oct 12, 2009
979
980
981
982
983
984
/* 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;
}
985
986
}
May 23, 2009
May 23, 2009
987
988
static int
photon_renderpoint(SDL_Renderer * renderer, int x, int y)
989
{
Oct 10, 2009
Oct 10, 2009
990
991
SDL_RenderData *rdata = (SDL_RenderData *) renderer->driverdata;
Oct 11, 2009
Oct 11, 2009
992
993
994
995
996
997
998
/* 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
999
1000
/* Enable blending, if requested */
_photon_set_blending(renderer, renderer->blendMode, renderer->a, SDL_PHOTON_DRAW_BLEND);