Debian Codegraph @

src/IOGfxDisplaySW.cpp

raw
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #include "IOGfxDisplaySW.h"
6 #include "IOGfxSurfaceSW.h"
7 #include "gfx_palette.h"
8 #include "ImageLoader.h" /* GFX_ref_pal */ // TODO: break dep
9
10 #include "log.h"
11
12 IOGfxDisplaySW::IOGfxDisplaySW(int w, int h, bool truecolor, Uint32 flags)
13 : IOGfxDisplay(w, h, truecolor, flags), renderer(NULL),
14 render_texture_linear(NULL), render_texture_nearest(NULL), rgb_screen(NULL) {
15 }
16
17 IOGfxDisplaySW::~IOGfxDisplaySW() {
18 close();
19 }
20
21 bool IOGfxDisplaySW::open() {
22 int req_w = w;
23 int req_h = h;
24
25 if (!IOGfxDisplay::open()) return false;
26 if (!createRenderer()) return false;
27 if (!createRenderTexture(req_w, req_h)) return false;
28
29 return true;
30 }
31
32 void IOGfxDisplaySW::close() {
33 if (render_texture_linear) SDL_DestroyTexture(render_texture_linear);
34 render_texture_linear = NULL;
35 if (rgb_screen) SDL_FreeSurface(rgb_screen);
36 rgb_screen = NULL;
37
38 if (renderer) SDL_DestroyRenderer(renderer);
39 renderer = NULL;
40
41 IOGfxDisplay::close();
42 }
43
44 bool IOGfxDisplaySW::createRenderer() {
45 /* TODO SDL2: make it configurable for speed: nearest/linear/DX-specific-best */
46 //SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
47 /* TODO SDL2: make render driver configurable to ease software-mode testing */
48 renderer = SDL_CreateRenderer(window, -1/*autoselect*/, 0);
49 /* TODO SDL2: optionally pass SDL_RENDERER_PRESENTVSYNC to 3rd param */
50 if (renderer == NULL) {
51 log_error("Unable to create renderer: %s\n", SDL_GetError());
52 return false;
53 }
54 // Specify aspect ratio
55 SDL_RenderSetLogicalSize(renderer, w, h);
56 return true;
57 }
58
59 /**
60 * Screen-like destination texture
61 */
62 bool IOGfxDisplaySW::createRenderTexture(int w, int h) {
63 SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best");
64 render_texture_linear = SDL_CreateTexture(renderer,
65 SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, w, h);
66 if (render_texture_linear == NULL) {
67 log_error("Unable to create render texture: %s", SDL_GetError());
68 return false;
69 }
70
71 SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
72 render_texture_nearest = SDL_CreateTexture(renderer,
73 SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, w, h);
74 if (render_texture_nearest == NULL) {
75 log_error("Unable to create debug render texture: %s", SDL_GetError());
76 return false;
77 }
78
79 Uint32 render_texture_format;
80 Uint32 Rmask, Gmask, Bmask, Amask; int bpp;
81 SDL_QueryTexture(render_texture_linear, &render_texture_format, NULL, NULL, NULL);
82 SDL_PixelFormatEnumToMasks(render_texture_format, &bpp,
83 &Rmask, &Gmask, &Bmask, &Amask);
84 if (!truecolor)
85 rgb_screen = SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask);
86
87 return true;
88 }
89
90 static void logRendererInfo(SDL_RendererInfo* info) {
91 log_info(" Renderer driver: %s", info->name);
92 log_info(" Renderer flags:");
93 if (info->flags & SDL_RENDERER_SOFTWARE)
94 log_info(" SDL_RENDERER_SOFTWARE");
95 if (info->flags & SDL_RENDERER_ACCELERATED)
96 log_info(" SDL_RENDERER_ACCELERATED");
97 if (info->flags & SDL_RENDERER_PRESENTVSYNC)
98 log_info(" SDL_RENDERER_PRESENTVSYNC");
99 if (info->flags & SDL_RENDERER_TARGETTEXTURE)
100 log_info(" SDL_RENDERER_TARGETTEXTURE");
101 log_info(" Renderer texture formats:");
102 for (unsigned int i = 0; i < info->num_texture_formats; i++)
103 log_info(" %s", SDL_GetPixelFormatName(info->texture_formats[i]));
104 log_info(" Renderer max texture width: %d", info->max_texture_width);
105 log_info(" Renderer max texture height: %d", info->max_texture_height);
106 }
107
108 void IOGfxDisplaySW::logRenderersInfo() {
109 log_info("Current renderer:\n");
110 SDL_RendererInfo info;
111 SDL_GetRendererInfo(renderer, &info);
112 logRendererInfo(&info);
113
114 log_info("Available renderers:");
115 for (int i = 0; i < SDL_GetNumRenderDrivers(); i++) {
116 SDL_RendererInfo info;
117 SDL_GetRenderDriverInfo(i, &info);
118 log_info("%d:\n", i);
119 logRendererInfo(&info);
120 }
121 }
122
123 void IOGfxDisplaySW::logRenderTextureInfo() {
124 Uint32 format;
125 int access, w, h;
126 SDL_QueryTexture(render_texture_linear, &format, &access, &w, &h);
127 log_info("Render texture: format: %s", SDL_GetPixelFormatName(format));
128 char* str_access;
129 switch(access) {
130 case SDL_TEXTUREACCESS_STATIC:
131 str_access = "SDL_TEXTUREACCESS_STATIC"; break;
132 case SDL_TEXTUREACCESS_STREAMING:
133 str_access = "SDL_TEXTUREACCESS_STREAMING"; break;
134 case SDL_TEXTUREACCESS_TARGET:
135 str_access = "SDL_TEXTUREACCESS_TARGET"; break;
136 default:
137 str_access = "Unknown!"; break;
138 }
139 log_info("Render texture: access: %s", str_access);
140 log_info("Render texture: width : %d", w);
141 log_info("Render texture: height: %d", h);
142 Uint32 Rmask, Gmask, Bmask, Amask; int bpp;
143 SDL_PixelFormatEnumToMasks(format, &bpp,
144 &Rmask, &Gmask, &Bmask, &Amask);
145 log_info("Render texture: bpp : %d", bpp);
146 log_info("Render texture: masks : %08x %08x %08x %08x", Rmask, Gmask, Bmask, Amask);
147 }
148
149 void IOGfxDisplaySW::logDisplayInfo() {
150 log_info("FreeDink graphics mode: IOGfxDisplaySW");
151 IOGfxDisplay::logDisplayInfo();
152 logRenderersInfo();
153 logRenderTextureInfo();
154 }
155
156
157
158 void IOGfxDisplaySW::clear() {
159 SDL_RenderClear(renderer);
160 }
161
162 void gfx_fade_apply(SDL_Surface* screen, int brightness) {
163 /* Check SDL_blit.h in the SDL source code for guidance */
164 SDL_LockSurface(screen);
165 /* Progress per pixel rather than per byte */
166 int remainder = (screen->pitch - (screen->w * screen->format->BytesPerPixel))
167 / screen->format->BytesPerPixel;
168 /* Using aligned Uint32 is faster than working with Uint8 values */
169 Uint32 *p = (Uint32*)screen->pixels;
170 int height = screen->h;
171 while (height--) {
172 int x;
173 for (x = 0; x < screen->w; x++) {
174 /* Assume that pixel order is RGBA */
175 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
176 if (*p != 0x00FFFFFF) { // skip white
177 #else
178 if (*p != 0xFFFFFF00) { // TODO: I need a PPC tester for this
179 #endif
180 *((Uint8*)p) = *((Uint8*)p) * brightness >> 8;
181 *((Uint8*)p+1) = *((Uint8*)p+1) * brightness >> 8;
182 *((Uint8*)p+2) = *((Uint8*)p+2) * brightness >> 8;
183 }
184 p++;
185 }
186 p += remainder;
187 }
188 SDL_UnlockSurface(screen);
189 }
190
191 void IOGfxDisplaySW::flip(IOGfxSurface* backbuffer, SDL_Rect* dstrect,
192 bool interpolation, bool hwflip) {
193 /* For now we do all operations on the CPU side and perform a big
194 texture update at each frame; this is necessary to support
195 palette and fade_down/fade_up. */
196
197 /* Convert to destination buffer format */
198 SDL_Surface* source = dynamic_cast<IOGfxSurfaceSW*>(backbuffer)->image;
199
200 if (truecolor && brightness < 256)
201 gfx_fade_apply(source, brightness);
202
203 if (truecolor) {
204 if (source->format->format != getFormat())
205 log_error("Wrong backbuffer format");
206 } else {
207 /* Convert 8-bit buffer for truecolor texture upload */
208
209 /* Use "physical" screen palette - use SDL_SetPaletteColors to invalidate SDL cache */
210 SDL_Color pal_bak[256];
211 SDL_Color pal_phys[256];
212 memcpy(pal_bak, source->format->palette->colors, sizeof(pal_bak));
213 gfx_palette_get_phys(pal_phys);
214 SDL_SetPaletteColors(source->format->palette, pal_phys, 0, 256);
215
216 if (SDL_BlitSurface(source, NULL, rgb_screen, NULL) < 0) {
217 log_error("ERROR: 8-bit->truecolor conversion failed: %s", SDL_GetError());
218 }
219 SDL_SetPaletteColors(source->format->palette, pal_bak, 0, 256);
220
221 source = rgb_screen;
222 }
223
224 if (interpolation) {
225 SDL_UpdateTexture(render_texture_linear, NULL, source->pixels, source->pitch);
226 SDL_RenderCopy(renderer, render_texture_linear, NULL, dstrect);
227 } else {
228 SDL_UpdateTexture(render_texture_nearest, NULL, source->pixels, source->pitch);
229 SDL_RenderCopy(renderer, render_texture_nearest, NULL, dstrect);
230 }
231
232 if (hwflip)
233 SDL_RenderPresent(renderer);
234 }
235
236 void IOGfxDisplaySW::onSizeChange(int w, int h) {
237 this->w = w;
238 this->h = h;
239 SDL_RenderSetLogicalSize(renderer, w, h);
240 }
241
242 SDL_Surface* IOGfxDisplaySW::screenshot(SDL_Rect* rect) {
243 Uint32 Rmask=0, Gmask=0, Bmask=0, Amask=0; int bpp=0;
244 SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_RGBA8888, &bpp,
245 &Rmask, &Gmask, &Bmask, &Amask);
246 SDL_Surface* surface = SDL_CreateRGBSurface(0,
247 rect->w, rect->h, 32, Rmask, Gmask, Bmask, Amask);
248
249 if (SDL_RenderReadPixels(renderer,
250 rect, SDL_PIXELFORMAT_RGBA8888,
251 surface->pixels, surface->pitch) < 0) {
252 log_error("IOGfxDisplaySW::screenshot: %s", SDL_GetError());
253 SDL_FreeSurface(surface);
254 return NULL;
255 }
256
257 return surface;
258 }
259
260 IOGfxSurface* IOGfxDisplaySW::upload(SDL_Surface* image) {
261 /* Set RLE encoding to save memory space and improve perfs if colorkey */
262 SDL_SetSurfaceRLE(image, SDL_TRUE);
263 /* Force RLE-encode */
264 SDL_LockSurface(image);
265 SDL_UnlockSurface(image);
266
267 return new IOGfxSurfaceSW(image);
268 }
269
270 IOGfxSurface* IOGfxDisplaySW::allocBuffer(int surfW, int surfH) {
271 Uint32 Rmask=0, Gmask=0, Bmask=0, Amask=0; int bpp=0;
272 SDL_PixelFormatEnumToMasks(getFormat(), &bpp, &Rmask, &Gmask, &Bmask, &Amask);
273 SDL_Surface* image = SDL_CreateRGBSurface(0, surfW, surfH, bpp, Rmask, Gmask, Bmask, Amask);
274
275 /* When a new image is loaded in DX, it's color-converted using the
276 main palette (possibly altering the colors to match the palette);
277 currently we emulate that by wrapping SDL_LoadBMP, converting
278 image to the internal palette at load time - and we never change
279 the buffer's palette again, so we're sure there isn't any
280 conversion even if we change the screen palette: */
281 if (!truecolor)
282 SDL_SetPaletteColors(image->format->palette, GFX_ref_pal, 0, 256);
283
284 return new IOGfxSurfaceSW(image);
285 }