annotate src/video/win32/SDL_gapirender.c @ 3280:00cace2d9080

Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
author Sam Lantinga <slouken@libsdl.org>
date Sat, 19 Sep 2009 13:29:40 +0000
parents 6338b7f2d024
children 83518f8fcd61
rev   line source
3168
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /*
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
3 Copyright (C) 1997-2009 Sam Lantinga
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 Lesser General Public License for more details.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 Sam Lantinga
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 slouken@libsdl.org
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 Stefan Klug
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23 klug.stefan@gmx.de
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25 #include "SDL_config.h"
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27 #if SDL_VIDEO_RENDER_GAPI
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 #include "SDL_win32video.h"
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30 //#include "../SDL_sysvideo.h"
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31 #include "../SDL_yuv_sw_c.h"
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 #include "../SDL_renderer_sw.h"
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34 #include "SDL_gapirender_c.h"
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 #define GAPI_RENDERER_DEBUG 1
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
38 /* GAPI renderer implementation */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
39
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
40 static SDL_Renderer *GAPI_CreateRenderer(SDL_Window * window, Uint32 flags);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
41 static int GAPI_RenderPoint(SDL_Renderer * renderer, int x, int y);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
42 static int GAPI_RenderLine(SDL_Renderer * renderer, int x1, int y1,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
43 int x2, int y2);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
44 static int GAPI_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45 static int GAPI_RenderCopy(SDL_Renderer * renderer,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
46 SDL_Texture * texture,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47 const SDL_Rect * srcrect,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
48 const SDL_Rect * dstrect);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
49 static void GAPI_RenderPresent(SDL_Renderer * renderer);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 static void GAPI_DestroyRenderer(SDL_Renderer * renderer);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
53 SDL_RenderDriver GAPI_RenderDriver = {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54 GAPI_CreateRenderer,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
55 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
56 "gapi",
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
57 (SDL_RENDERER_SINGLEBUFFER),
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
58 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59 };
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
61 static HMODULE g_hGapiLib = 0;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63 // for testing with GapiEmu
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64 #define USE_GAPI_EMU 0
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65 #define EMULATE_AXIM_X30 0
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 #if 0
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68 #define GAPI_LOG(...) printf(__VA_ARGS__)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69 #else
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70 #define GAPI_LOG(...)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
71 #endif
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
72
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
73
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74 #if USE_GAPI_EMU && !REPORT_VIDEO_INFO
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75 #pragma message("Warning: Using GapiEmu in release build. I assume you'd like to set USE_GAPI_EMU to zero.")
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
76 #endif
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
78
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
79 static void
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
80 GAPI_SetError(const char *prefix, HRESULT result)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
81 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
82 const char *error;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 switch (result) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85 default:
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86 error = "UNKNOWN";
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
87 break;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89 SDL_SetError("%s: %s", prefix, error);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
90 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92 void
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
93 GAPI_AddRenderDriver(_THIS)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
94 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95 /* TODO: should we check for support of GetRawFramebuffer here?
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96 */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
97 #if USE_GAPI_EMU
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
98 g_hGapiLib = LoadLibrary(L"GAPI_Emu.dll");
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
99 #else
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100 g_hGapiLib = LoadLibrary(L"\\Windows\\gx.dll");
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101 #endif
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
102
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
103 if (g_hGapiLib) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
104 #define LINK(name,import) gx.name = (PFN##name)GetProcAddress( g_hGapiLib, L##import );
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
105
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106 LINK(GXOpenDisplay, "?GXOpenDisplay@@YAHPAUHWND__@@K@Z")
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
107 LINK(GXCloseDisplay, "?GXCloseDisplay@@YAHXZ")
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 LINK(GXBeginDraw, "?GXBeginDraw@@YAPAXXZ")
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109 LINK(GXEndDraw, "?GXEndDraw@@YAHXZ")
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110 LINK(GXOpenInput, "?GXOpenInput@@YAHXZ")
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
111 LINK(GXCloseInput, "?GXCloseInput@@YAHXZ")
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
112 LINK(GXGetDisplayProperties,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 "?GXGetDisplayProperties@@YA?AUGXDisplayProperties@@XZ")
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114 LINK(GXGetDefaultKeys, "?GXGetDefaultKeys@@YA?AUGXKeyList@@H@Z")
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115 LINK(GXSuspend, "?GXSuspend@@YAHXZ")
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
116 LINK(GXResume, "?GXResume@@YAHXZ")
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
117 LINK(GXSetViewport, "?GXSetViewport@@YAHKKKK@Z")
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
118 LINK(GXIsDisplayDRAMBuffer, "?GXIsDisplayDRAMBuffer@@YAHXZ")
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
119
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
120 /* wrong gapi.dll */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
121 if (!gx.GXOpenDisplay) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
122 FreeLibrary(g_hGapiLib);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
123 g_hGapiLib = 0;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
124 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
125 #undef LINK
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
126 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
127
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
128 SDL_AddRenderDriver(0, &GAPI_RenderDriver);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
129 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
130
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
131 typedef enum
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
132 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
133 USAGE_GX_FUNCS = 0x0001, /* enable to use GXOpen/GXClose/GXBeginDraw... */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134 USAGE_DATA_PTR_CONSTANT = 0x0002 /* the framebuffer is at a constant location, don't use values from GXBeginDraw() */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
135 } GAPI_UsageFlags;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
136
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
137
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
138
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
139 typedef struct
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
140 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
141 int w;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
142 int h;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
143 int xPitch; /* bytes to move to go to the next pixel */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
144 int yPitch; /* bytes to move to go to the next line */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
145 int offset; /* data offset, to add to the data returned from GetFramebuffer, before processing */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
146
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
147 void *data;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
148 Uint32 usageFlags; /* these flags contain options to define screen handling and to reliably workarounds */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
149
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
150 Uint32 format; /* pixel format as defined in SDL_pixels.h */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
151
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
152 } GAPI_RenderData;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
153
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
154
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
155 static Uint32
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
156 GuessPixelFormatFromBpp(int bpp)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
157 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
158 switch (bpp) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
159 case 15:
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
160 return SDL_PIXELFORMAT_RGB555;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
161 case 16:
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
162 return SDL_PIXELFORMAT_RGB565;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
163 default:
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
164 return SDL_PIXELFORMAT_UNKNOWN;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
165 break;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
166 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
167 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
168
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
169 static GAPI_RenderData *
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
170 FillRenderDataRawFramebuffer(SDL_Window * window)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
171 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
172 RawFrameBufferInfo rbi;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
173 GAPI_RenderData *renderdata;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
174 HDC hdc;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
175
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
176 //TODO should we use the hdc of the window?
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
177 hdc = GetDC(NULL);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
178 int result = ExtEscape(hdc, GETRAWFRAMEBUFFER, 0, NULL,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
179 sizeof(RawFrameBufferInfo),
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
180 (char *) &rbi);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
181 ReleaseDC(NULL, hdc);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
182
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
183 if (!(result > 0)) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
184 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
185 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
186
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
187 /* Asus A696 returns wrong results so we do a sanity check
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
188 See:
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
189 http://groups.google.com/group/microsoft.public.smartphone.developer/browse_thread/thread/4fde5bddd477de81
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
190 */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
191 if (rbi.cxPixels <= 0 ||
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
192 rbi.cyPixels <= 0 ||
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
193 rbi.cxStride == 0 || rbi.cyStride == 0 || rbi.pFramePointer == 0) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
194 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
195 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
196
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
197
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
198 renderdata = (GAPI_RenderData *) SDL_calloc(1, sizeof(*renderdata));
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
199 if (!renderdata) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
200 SDL_OutOfMemory();
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
201 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
202 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
203 //Try to match the window size
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
204 //TODO add rotation support
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
205 if (rbi.cxPixels != window->w || rbi.cyPixels != window->h) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
206 SDL_free(renderdata);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
207 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
208 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
209 //Check that the display uses a known display format
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
210 switch (rbi.wFormat) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
211 case FORMAT_565:
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
212 renderdata->format = SDL_PIXELFORMAT_RGB565;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
213 break;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
214 case FORMAT_555:
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
215 renderdata->format = SDL_PIXELFORMAT_RGB555;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
216 break;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
217 default:
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
218 //TODO we should add support for other formats
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
219 SDL_free(renderdata);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
220 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
221 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
222
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
223 renderdata->usageFlags = USAGE_DATA_PTR_CONSTANT;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
224 renderdata->data = rbi.pFramePointer;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
225 renderdata->w = rbi.cxPixels;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
226 renderdata->h = rbi.cyPixels;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
227 renderdata->xPitch = rbi.cxStride;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
228 renderdata->yPitch = rbi.cyStride;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
229
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
230 return renderdata;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
231
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
232 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
233
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
234
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
235 static GAPI_RenderData *
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
236 FillRenderDataGAPI(SDL_Window * window)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
237 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
238 GAPI_RenderData *renderdata;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
239 struct GXDisplayProperties gxdp;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
240 int tmp;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
241
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
242 #ifdef _ARM_
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
243 WCHAR oemstr[100];
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
244 #endif
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
245
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
246 if (!g_hGapiLib) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
247 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
248 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
249
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
250 renderdata = (GAPI_RenderData *) SDL_calloc(1, sizeof(GAPI_RenderData));
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
251 if (!renderdata) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
252 SDL_OutOfMemory();
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
253 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
254 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
255
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
256 gxdp = gx.GXGetDisplayProperties();
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
257 renderdata->usageFlags = USAGE_GX_FUNCS;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
258 renderdata->w = gxdp.cxWidth;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
259 renderdata->h = gxdp.cyHeight;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
260 renderdata->xPitch = gxdp.cbxPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
261 renderdata->yPitch = gxdp.cbyPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
262
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
263 //Check that the display uses a known display format
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
264 if (gxdp.ffFormat & kfDirect565) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
265 renderdata->format = SDL_PIXELFORMAT_RGB565;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
266 } else if (gxdp.ffFormat & kfDirect555) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
267 renderdata->format = SDL_PIXELFORMAT_RGB555;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
268 } else {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
269 renderdata->format = SDL_PIXELFORMAT_UNKNOWN;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
270 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
271
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
272 /* apply some device specific corrections */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
273 #ifdef _ARM_
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
274 SystemParametersInfo(SPI_GETOEMINFO, sizeof(oemstr), oemstr, 0);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
275
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
276 // buggy iPaq38xx
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
277 if ((oemstr[12] == 'H') && (oemstr[13] == '3')
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
278 && (oemstr[14] == '8')
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
279 && (gxdp.cbxPitch > 0)) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
280 renderdata->data = (void *) 0xac0755a0;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
281 renderdata->xPitch = -640;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
282 renderdata->yPitch = 2;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
283 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
284 #if (EMULATE_AXIM_X30 == 0)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
285 // buggy Dell Axim X30
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
286 if (_tcsncmp(oemstr, L"Dell Axim X30", 13) == 0)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
287 #endif
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
288 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
289 GXDeviceInfo gxInfo = { 0 };
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
290 HDC hdc = GetDC(NULL);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
291 int result;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
292
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
293 gxInfo.Version = 100;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
294 result =
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
295 ExtEscape(hdc, GETGXINFO, 0, NULL, sizeof(gxInfo),
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
296 (char *) &gxInfo);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
297 if (result > 0) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
298 renderdata->usageFlags = USAGE_DATA_PTR_CONSTANT; /* no more GAPI usage from now */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
299 renderdata->data = gxInfo.pvFrameBuffer;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
300 this->hidden->needUpdate = 0;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
301 renderdata->xPitch = 2;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
302 renderdata->yPitch = 480;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
303 renderdata->w = gxInfo.cxWidth;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
304 renderdata->h = gxInfo.cyHeight;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
305
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
306 //Check that the display uses a known display format
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
307 switch (rbi->wFormat) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
308 case FORMAT_565:
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
309 renderdata->format = SDL_PIXELFORMAT_RGB565;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
310 break;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
311 case FORMAT_555:
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
312 renderdata->format = SDL_PIXELFORMAT_RGB555;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
313 break;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
314 default:
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
315 //TODO we should add support for other formats
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
316 SDL_free(renderdata);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
317 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
318 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
319 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
320 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
321 #endif
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
322
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
323
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
324 if (renderdata->format == SDL_PIXELFORMAT_UNKNOWN) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
325 SDL_SetError("Gapi Pixelformat is unknown");
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
326 SDL_free(renderdata);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
327 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
328 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
329
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
330 /* Gapi always returns values in standard orientation, so we manually apply
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
331 the current orientation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
332 */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
333
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
334 DEVMODE settings;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
335 SDL_memset(&settings, 0, sizeof(DEVMODE));
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
336 settings.dmSize = sizeof(DEVMODE);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
337
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
338 settings.dmFields = DM_DISPLAYORIENTATION;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
339 ChangeDisplaySettingsEx(NULL, &settings, NULL, CDS_TEST, NULL);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
340
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
341 if (settings.dmDisplayOrientation == DMDO_90) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
342
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
343 tmp = renderdata->w;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
344 renderdata->w = renderdata->h;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
345 renderdata->h = tmp;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
346
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
347 tmp = renderdata->xPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
348 renderdata->xPitch = -renderdata->yPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
349 renderdata->yPitch = tmp;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
350
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
351 renderdata->offset = -renderdata->w * renderdata->xPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
352
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
353 } else if (settings.dmDisplayOrientation == DMDO_180) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
354
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
355 renderdata->xPitch = -renderdata->xPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
356 renderdata->yPitch = -renderdata->yPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
357
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
358 renderdata->offset = -renderdata->h * renderdata->yPitch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
359 - renderdata->w * renderdata->xPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
360
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
361 } else if (settings.dmDisplayOrientation == DMDO_270) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
362
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
363 tmp = renderdata->w;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
364 renderdata->w = renderdata->h;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
365 renderdata->h = tmp;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
366
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
367 tmp = renderdata->xPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
368 renderdata->xPitch = renderdata->yPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
369 renderdata->yPitch = -tmp;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
370
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
371 renderdata->offset = -renderdata->h * renderdata->yPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
372
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
373 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
374
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
375 if (renderdata->w != window->w || renderdata->h != window->h) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
376 GAPI_LOG("GAPI open failed, wrong size %i %i %i %i\n", renderdata->w,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
377 renderdata->h, renderdata->xPitch, renderdata->yPitch);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
378 SDL_free(renderdata);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
379 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
380 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
381
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
382 return renderdata;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
383
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
384 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
385
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
386
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
387 /* This function does the whole encapsulation of Gapi/RAWFRAMEBUFFER
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
388 it should handle all the device dependent details and fill the device INDEPENDENT
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
389 RenderData structure.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
390 */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
391 GAPI_RenderData *
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
392 FillRenderData(SDL_Window * window)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
393 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
394 /* We try to match the requested window to the modes available by GAPI and RAWFRAMEBUFFER.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
395 First RAWFRAMEBUFFER is tried, as it is the most reliable one
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
396 Look here for detailed discussions:
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
397 http://pdaphonehome.com/forums/samsung-i700/28087-just-saw.html
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
398 http://blogs.msdn.com/windowsmobile/archive/2007/08/13/have-you-migrated-to-directdraw-yet.aspx
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
399 */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
400
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
401 GAPI_RenderData *res;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
402
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
403 res = FillRenderDataRawFramebuffer(window);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
404 GAPI_LOG("FillRenderDataRawFramebuffer: %p\n", res);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
405 if (res) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
406 return res;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
407 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
408 //Now we try gapi
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
409 res = FillRenderDataGAPI(window);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
410 GAPI_LOG("FillRenderDataGAPI: %p\n", res);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
411
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
412 return res;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
413 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
414
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
415 void *
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
416 GetFramebuffer()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
417 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
418
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
419 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
420
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
421
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
422 SDL_Renderer *
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
423 GAPI_CreateRenderer(SDL_Window * window, Uint32 flags)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
424 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
425 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
426 SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
427 SDL_DisplayMode *displayMode = &display->current_mode;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
428 SDL_Renderer *renderer;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
429 GAPI_RenderData *data;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
430 int i, n;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
431 int bpp;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
432 Uint32 Rmask, Gmask, Bmask, Amask;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
433
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
434 if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
435 SDL_SetError("Gapi supports only fullscreen windows");
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
436 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
437 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
438
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
439 if (!SDL_PixelFormatEnumToMasks
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
440 (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
441 SDL_SetError("Unknown display format");
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
442 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
443 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
444
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
445 renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
446 if (!renderer) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
447 SDL_OutOfMemory();
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
448 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
449 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
450
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
451 data = FillRenderData(window);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
452 if (!data) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
453 GAPI_DestroyRenderer(renderer);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
454 SDL_OutOfMemory();
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
455 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
456 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
457
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
458 renderer->RenderPoint = GAPI_RenderPoint;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
459 renderer->RenderLine = GAPI_RenderLine;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
460 renderer->RenderFill = GAPI_RenderFill;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
461 renderer->RenderCopy = GAPI_RenderCopy;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
462 renderer->RenderPresent = GAPI_RenderPresent;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
463 renderer->DestroyRenderer = GAPI_DestroyRenderer;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
464 renderer->info.name = GAPI_RenderDriver.info.name;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
465 renderer->info.flags = 0;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
466 renderer->window = window->id;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
467 renderer->driverdata = data;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
468
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
469 /* Gapi provides only a framebuffer so lets use software implementation */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
470 Setup_SoftwareRenderer(renderer);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
471
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
472 #ifdef GAPI_RENDERER_DEBUG
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
473 printf("Created gapi renderer\n");
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
474 printf("use GX functions: %i\n", data->usageFlags & USAGE_GX_FUNCS);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
475 printf("framebuffer is constant: %i\n",
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
476 data->usageFlags & USAGE_DATA_PTR_CONSTANT);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
477 printf("w: %i h: %i\n", data->w, data->h);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
478 printf("data ptr: %p\n", data->data); /* this can be 0 in case of GAPI usage */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
479 printf("xPitch: %i\n", data->xPitch);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
480 printf("yPitch: %i\n", data->yPitch);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
481 printf("offset: %i\n", data->offset);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
482 printf("format: %x\n", data->format);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
483 #endif
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
484
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
485 if (data->usageFlags & USAGE_GX_FUNCS) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
486 if (gx.GXOpenDisplay(windowdata->hwnd, GX_FULLSCREEN) == 0) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
487 GAPI_DestroyRenderer(renderer);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
488 return NULL;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
489 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
490 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
491
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
492 return renderer;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
493 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
494
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
495 static int
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
496 GAPI_RenderPoint(SDL_Renderer * renderer, int x, int y)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
497 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
498 //TODO implement
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
499 return -1;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
500 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
501
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
502 static int
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
503 GAPI_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
504 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
505 //TODO implement
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
506 return -11;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
507 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
508
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
509 static int
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
510 GAPI_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
511 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
512 //TODO implement
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
513 return -1;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
514 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
515
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
516 /* Video memory is very slow so lets optimize as much as possible */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
517 static void
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
518 updateLine16to16(char *src, int srcXPitch, int srcYPitch,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
519 char *dst, int dstXPitch, int dstYPitch, int width,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
520 int height)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
521 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
522 char *srcLine, *dstLine;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
523 char *srcPix, *dstPix;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
524
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
525 int x, y;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
526
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
527 //First dumb solution
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
528 if (srcXPitch == 2 && dstXPitch == 2) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
529 srcLine = src;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
530 dstLine = dst;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
531 y = height;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
532 while (y--) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
533 SDL_memcpy(dstLine, srcLine, width * sizeof(Uint16));
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
534 srcLine += srcYPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
535 dstLine += dstYPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
536 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
537 } else {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
538 //printf("GAPI uses slow blit path %i, %i\n", dstXPitch, dstYPitch);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
539 srcLine = src;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
540 dstLine = dst;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
541 y = height;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
542 while (y--) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
543 srcPix = srcLine;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
544 dstPix = dstLine;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
545 x = width;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
546 while (x--) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
547 *((Uint16 *) dstPix) = *((Uint16 *) srcPix);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
548 dstPix += dstXPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
549 srcPix += srcXPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
550 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
551 srcLine += srcYPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
552 dstLine += dstYPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
553 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
554 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
555 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
556
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
557 static int
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
558 GAPI_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
559 const SDL_Rect * srcrect, const SDL_Rect * dstrect)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
560 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
561 GAPI_RenderData *data = (GAPI_RenderData *) renderer->driverdata;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
562 int bpp;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
563 int bytespp;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
564 int status;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
565 Uint32 Rmask, Gmask, Bmask, Amask;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
566
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
567 if (texture->format != data->format) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
568 SDL_SetError("Gapi got wrong texture");
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
569 return -1;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
570 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
571
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
572 GAPI_LOG("GAPI_RenderCopy\n");
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
573
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
574 if (data->usageFlags & USAGE_GX_FUNCS) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
575 char *buffer;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
576 buffer = gx.GXBeginDraw();
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
577 if (!(data->usageFlags & USAGE_DATA_PTR_CONSTANT)) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
578 data->data = buffer;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
579 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
580 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
581
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
582 GAPI_LOG("GAPI_RenderCopy blit\n");
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
583 /* If our framebuffer has an xPitch which matches the pixelsize, we
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
584 can convert the framebuffer to a SDL_surface and blit there,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
585 otherwise, we have to use our own blitting routine
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
586 */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
587 SDL_PixelFormatEnumToMasks(data->format, &bpp, &Rmask, &Gmask, &Bmask,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
588 &Amask);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
589 bytespp = bpp >> 3;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
590 if (data->xPitch == bytespp && 0) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
591 SDL_Surface *screen =
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
592 SDL_CreateRGBSurfaceFrom(data->data, data->w, data->h,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
593 bpp, data->yPitch, Rmask, Gmask, Bmask,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
594 Amask);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
595 status =
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
596 SDL_UpperBlit((SDL_Surface *) texture->driverdata, srcrect,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
597 screen, dstrect);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
598 SDL_FreeSurface(screen);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
599 } else { /* screen is rotated, we have to blit on our own */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
600 SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
601
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
602 char *src, *dst;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
603 src = surface->pixels;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
604 src += srcrect->y * surface->pitch + srcrect->x * 2;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
605
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
606 dst = data->data + data->offset;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
607 dst += dstrect->y * data->yPitch + dstrect->x * data->xPitch;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
608
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
609 updateLine16to16(src, 2, surface->pitch,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
610 dst, data->xPitch, data->yPitch,
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
611 srcrect->w, srcrect->h);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
612
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
613 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
614
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
615 Uint32 ticks = SDL_GetTicks();
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
616 if (data->usageFlags & USAGE_GX_FUNCS) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
617 gx.GXEndDraw();
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
618 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
619 GAPI_LOG("GAPI_RenderCopy %i\n", SDL_GetTicks() - ticks);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
620 return status;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
621 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
622
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
623 static void
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
624 GAPI_RenderPresent(SDL_Renderer * renderer)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
625 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
626 /* Nothing todo as we rendered directly to the screen on RenderCopy */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
627 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
628
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
629 static void
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
630 GAPI_DestroyRenderer(SDL_Renderer * renderer)
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
631 {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
632 GAPI_RenderData *data = (GAPI_RenderData *) renderer->driverdata;
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
633
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
634 if (data->usageFlags & USAGE_GX_FUNCS) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
635 gx.GXCloseDisplay();
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
636 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
637
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
638 if (data) {
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
639 SDL_free(data);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
640 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
641 SDL_free(renderer);
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
642 }
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
643
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
644 #endif /* SDL_VIDEO_RENDER_GAPI */
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
645
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
646 /* vi: set ts=4 sw=4 expandtab: */