src/osd/sdl/draw13.c
| r0 | r4071 | |
| 1 | //============================================================ |
| 2 | // |
| 3 | // drawogl.c - SDL software and OpenGL implementation |
| 4 | // |
| 5 | // Copyright (c) 1996-2007, Nicola Salmoria and the MAME Team. |
| 6 | // Visit http://mamedev.org for licensing and usage restrictions. |
| 7 | // |
| 8 | // SDLMAME by Olivier Galibert and R. Belmont |
| 9 | // |
| 10 | // Note: D3D9 goes to a lot of trouble to fiddle with MODULATE |
| 11 | // mode on textures. That is the default in OpenGL so we |
| 12 | // don't have to touch it. |
| 13 | // |
| 14 | //============================================================ |
| 15 | |
| 16 | // standard C headers |
| 17 | #include <math.h> |
| 18 | #include <stdio.h> |
| 19 | |
| 20 | // MAME headers |
| 21 | #include "osdcomm.h" |
| 22 | #include "render.h" |
| 23 | #include "options.h" |
| 24 | #include "driver.h" |
| 25 | |
| 26 | // standard SDL headers |
| 27 | #include <SDL/SDL.h> |
| 28 | |
| 29 | // OSD headers |
| 30 | #include "osdsdl.h" |
| 31 | #include "window.h" |
| 32 | |
| 33 | //============================================================ |
| 34 | // DEBUGGING |
| 35 | //============================================================ |
| 36 | |
| 37 | //============================================================ |
| 38 | // CONSTANTS |
| 39 | //============================================================ |
| 40 | |
| 41 | #define STAT_PIXEL_THRESHOLD (150*150) |
| 42 | |
| 43 | enum |
| 44 | { |
| 45 | TEXTURE_TYPE_NONE, |
| 46 | TEXTURE_TYPE_PLAIN, |
| 47 | TEXTURE_TYPE_SURFACE |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | //============================================================ |
| 52 | // MACROS |
| 53 | //============================================================ |
| 54 | |
| 55 | #define IS_OPAQUE(a) (a >= 1.0f) |
| 56 | #define IS_TRANSPARENT(a) (a < 0.0001f) |
| 57 | |
| 58 | #define MAX4(a, b, c, d) MAX(a, MAX(b, MAX(c, d))) |
| 59 | #define MIN4(a, b, c, d) MIN(a, MIN(b, MIN(c, d))) |
| 60 | |
| 61 | |
| 62 | //============================================================ |
| 63 | // TYPES |
| 64 | //============================================================ |
| 65 | |
| 66 | |
| 67 | typedef struct _quad_setup_data quad_setup_data; |
| 68 | struct _quad_setup_data |
| 69 | { |
| 70 | INT32 dudx, dvdx, dudy, dvdy; |
| 71 | INT32 startu, startv; |
| 72 | INT32 rotwidth, rotheight; |
| 73 | }; |
| 74 | |
| 75 | typedef struct _texture_info texture_info; |
| 76 | |
| 77 | typedef void (*texture_copy_func)(texture_info *texture, const render_texinfo *texsource); |
| 78 | |
| 79 | typedef struct _copy_info copy_info; |
| 80 | struct _copy_info { |
| 81 | int src_fmt; |
| 82 | Uint32 dst_fmt; |
| 83 | int dst_bpp; |
| 84 | int rotate; |
| 85 | texture_copy_func func; |
| 86 | Uint32 bm_mask; |
| 87 | const char *srcname; |
| 88 | const char *dstname; |
| 89 | /* Statistics */ |
| 90 | UINT64 pixel_count; |
| 91 | INT64 time; |
| 92 | int samples; |
| 93 | int perf; |
| 94 | /* list */ |
| 95 | copy_info *next; |
| 96 | }; |
| 97 | |
| 98 | /* texture_info holds information about a texture */ |
| 99 | struct _texture_info |
| 100 | { |
| 101 | texture_info * next; // next texture in the list |
| 102 | |
| 103 | HashT hash; // hash value for the texture (must be >= pointer size) |
| 104 | UINT32 flags; // rendering flags |
| 105 | render_texinfo texinfo; // copy of the texture info |
| 106 | |
| 107 | int rawwidth, rawheight;// raw width/height of the texture |
| 108 | |
| 109 | int format; // texture format |
| 110 | void *pixels; // pixels for the texture |
| 111 | int pitch; |
| 112 | int pixels_own; // do we own / allocated it ? |
| 113 | |
| 114 | SDL_TextureID texture_id; |
| 115 | |
| 116 | copy_info *copyinfo; |
| 117 | Uint32 sdl_access; |
| 118 | Uint32 sdl_blendmode; |
| 119 | quad_setup_data setup; |
| 120 | int is_rotated; |
| 121 | |
| 122 | osd_ticks_t last_access; |
| 123 | }; |
| 124 | |
| 125 | /* sdl_info is the information about SDL for the current screen */ |
| 126 | typedef struct _sdl_info sdl_info; |
| 127 | struct _sdl_info |
| 128 | { |
| 129 | INT32 blittimer; |
| 130 | UINT32 extra_flags; |
| 131 | |
| 132 | texture_info * texlist; // list of active textures |
| 133 | INT32 texture_max_width; // texture maximum width |
| 134 | INT32 texture_max_height; // texture maximum height |
| 135 | |
| 136 | float last_hofs; |
| 137 | float last_vofs; |
| 138 | |
| 139 | // Stats |
| 140 | INT64 last_blit_time; |
| 141 | INT64 last_blit_pixels; |
| 142 | }; |
| 143 | |
| 144 | //============================================================ |
| 145 | // PROTOTYPES |
| 146 | //============================================================ |
| 147 | |
| 148 | // core functions |
| 149 | |
| 150 | static void draw13_exit(void); |
| 151 | static void draw13_attach(sdl_draw_info *info, sdl_window_info *window); |
| 152 | static int draw13_window_create(sdl_window_info *window, int width, int height); |
| 153 | static void draw13_window_resize(sdl_window_info *window, int width, int height); |
| 154 | static void draw13_window_destroy(sdl_window_info *window); |
| 155 | static int draw13_window_draw(sdl_window_info *window, UINT32 dc, int update); |
| 156 | static const render_primitive_list *draw13_window_get_primitives(sdl_window_info *window); |
| 157 | static void draw13_destroy_all_textures(sdl_window_info *window); |
| 158 | static void draw13_window_clear(sdl_window_info *window); |
| 159 | static int draw13_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt); |
| 160 | static void draw13_destroy_texture(sdl_info *sdl, texture_info *texture); |
| 161 | |
| 162 | //============================================================ |
| 163 | // Textures |
| 164 | //============================================================ |
| 165 | |
| 166 | static void texture_set_data(sdl_info *sdl, texture_info *texture, const render_texinfo *texsource, UINT32 flags); |
| 167 | static texture_info *texture_create(sdl_window_info *window, const render_texinfo *texsource, quad_setup_data *setup, UINT32 flags); |
| 168 | static texture_info *texture_find(sdl_info *sdl, const render_primitive *prim, quad_setup_data *setup); |
| 169 | static texture_info * texture_update(sdl_window_info *window, const render_primitive *prim); |
| 170 | |
| 171 | |
| 172 | //============================================================ |
| 173 | // TEXCOPY FUNCS |
| 174 | //============================================================ |
| 175 | |
| 176 | #include "blit13.h" |
| 177 | |
| 178 | //============================================================ |
| 179 | // STATIC VARIABLES |
| 180 | //============================================================ |
| 181 | |
| 182 | #define SDL_TEXFORMAT_LAST SDL_TEXFORMAT_PALETTE16A |
| 183 | #define BM_ALL (-1) |
| 184 | //( SDL_BLENDMODE_MASK | SDL_BLENDMODE_BLEND | SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD) |
| 185 | |
| 186 | #define texcopy_NULL NULL |
| 187 | #define ENTRY(a,b,c,d,f) { SDL_TEXFORMAT_ ## a, SDL_PIXELFORMAT_ ## b, c, d, texcopy_ ## f, BM_ALL, #a, #b, 0, 0, 0, 0} |
| 188 | #define ENTRY_BM(a,b,c,d,f,bm) { SDL_TEXFORMAT_ ## a, SDL_PIXELFORMAT_ ## b, c, d, texcopy_ ## f, bm, #a, #b, 0, 0, 0, 0} |
| 189 | #define ENTRY_LR(a,b,c,d,f) { SDL_TEXFORMAT_ ## a, SDL_PIXELFORMAT_ ## b, c, d, texcopy_ ## f, BM_ALL, #a, #b, 0, 0, 0, -1} |
| 190 | |
| 191 | static copy_info blit_info_default[] = |
| 192 | { |
| 193 | /* no rotation */ |
| 194 | ENTRY(ARGB32, ARGB8888, 4, 0, NULL), |
| 195 | ENTRY_LR(ARGB32, RGB888, 4, 0, argb32_rgb32), |
| 196 | /* Entry for primarily for directfb */ |
| 197 | ENTRY_BM(ARGB32, RGB888, 4, 0, argb32_rgb32, SDL_BLENDMODE_ADD), |
| 198 | ENTRY_BM(ARGB32, RGB888, 4, 0, argb32_rgb32, SDL_BLENDMODE_MOD), |
| 199 | ENTRY_BM(ARGB32, RGB888, 4, 0, argb32_rgb32, SDL_BLENDMODE_NONE), |
| 200 | |
| 201 | ENTRY(RGB32, ARGB8888, 4, 0, rgb32_argb32), |
| 202 | ENTRY(RGB32, RGB888, 4, 0, NULL), |
| 203 | |
| 204 | ENTRY(RGB32_PALETTED, ARGB8888, 4, 0, rgb32pal_argb32), |
| 205 | ENTRY(RGB32_PALETTED, RGB888, 4, 0, rgb32pal_argb32), |
| 206 | |
| 207 | ENTRY(YUY16, UYVY, 2, 0, NULL /* yuv16_uyvy*/), |
| 208 | ENTRY(YUY16, YUY2, 2, 0, yuv16_yuy2), |
| 209 | ENTRY(YUY16, YVYU, 2, 0, yuv16_yvyu), |
| 210 | ENTRY(YUY16, ARGB8888, 4, 0, yuv16_argb32), |
| 211 | ENTRY(YUY16, RGB888, 4, 0, yuv16pal_argb32), |
| 212 | |
| 213 | ENTRY(YUY16_PALETTED, UYVY, 2, 0, yuv16pal_uyvy), |
| 214 | ENTRY(YUY16_PALETTED, YUY2, 2, 0, yuv16pal_yuy2), |
| 215 | ENTRY(YUY16_PALETTED, YVYU, 2, 0, yuv16pal_yvyu), |
| 216 | ENTRY(YUY16_PALETTED, ARGB8888, 4, 0, yuv16pal_argb32), |
| 217 | ENTRY(YUY16_PALETTED, RGB888, 4, 0, yuv16pal_argb32), |
| 218 | |
| 219 | ENTRY(PALETTE16, ARGB8888, 4, 0, pal16_argb32), |
| 220 | ENTRY(PALETTE16, RGB888, 4, 0, pal16_argb32), |
| 221 | |
| 222 | ENTRY(RGB15, RGB555, 2, 0, NULL /* rgb15_argb1555 */), |
| 223 | ENTRY(RGB15, ARGB1555, 2, 0, rgb15_argb1555), |
| 224 | ENTRY(RGB15, ARGB8888, 4, 0, rgb15_argb32), |
| 225 | ENTRY(RGB15, RGB888, 4, 0, rgb15_argb32), |
| 226 | |
| 227 | ENTRY(RGB15_PALETTED, ARGB8888, 4, 0, rgb15pal_argb32), |
| 228 | ENTRY(RGB15_PALETTED, RGB888, 4, 0, rgb15pal_argb32), |
| 229 | |
| 230 | ENTRY(PALETTE16A, ARGB8888, 4, 0, pal16a_argb32), |
| 231 | ENTRY(PALETTE16A, RGB888, 4, 0, pal16a_rgb32), |
| 232 | |
| 233 | /* rotation */ |
| 234 | ENTRY(ARGB32, ARGB8888, 4, 1, rot_argb32_argb32), |
| 235 | ENTRY_LR(ARGB32, RGB888, 4, 1, rot_argb32_rgb32), |
| 236 | /* Entry for primarily for directfb */ |
| 237 | ENTRY_BM(ARGB32, RGB888, 4, 1, rot_argb32_rgb32, SDL_BLENDMODE_ADD), |
| 238 | ENTRY_BM(ARGB32, RGB888, 4, 1, rot_argb32_rgb32, SDL_BLENDMODE_MOD), |
| 239 | ENTRY_BM(ARGB32, RGB888, 4, 1, rot_argb32_rgb32, SDL_BLENDMODE_NONE), |
| 240 | |
| 241 | ENTRY(RGB32, ARGB8888, 4, 1, rot_rgb32_argb32), |
| 242 | ENTRY(RGB32, RGB888, 4, 1, rot_argb32_argb32), |
| 243 | |
| 244 | ENTRY(RGB32_PALETTED, ARGB8888, 4, 1, rot_rgb32pal_argb32), |
| 245 | ENTRY(RGB32_PALETTED, RGB888, 4, 1, rot_rgb32pal_argb32), |
| 246 | |
| 247 | ENTRY(YUY16, ARGB8888, 4, 1, rot_yuv16_argb32), |
| 248 | ENTRY(YUY16, RGB888, 4, 1, rot_yuv16_argb32), |
| 249 | |
| 250 | ENTRY(YUY16_PALETTED, ARGB8888, 4, 1, rot_yuv16pal_argb32), |
| 251 | ENTRY(YUY16_PALETTED, RGB888, 4, 1, rot_yuv16pal_argb32), |
| 252 | |
| 253 | ENTRY(PALETTE16, ARGB8888, 4, 1, rot_pal16_argb32), |
| 254 | ENTRY(PALETTE16, RGB888, 4, 1, rot_pal16_argb32), |
| 255 | |
| 256 | ENTRY(RGB15, RGB555, 2, 1, rot_rgb15_argb1555), |
| 257 | ENTRY(RGB15, ARGB1555, 2, 1, rot_rgb15_argb1555), |
| 258 | ENTRY(RGB15, ARGB8888, 4, 1, rot_rgb15_argb32), |
| 259 | ENTRY(RGB15, RGB888, 4, 1, rot_rgb15_argb32), |
| 260 | |
| 261 | ENTRY(RGB15_PALETTED, ARGB8888, 4, 1, rot_rgb15pal_argb32), |
| 262 | ENTRY(RGB15_PALETTED, RGB888, 4, 1, rot_rgb15pal_argb32), |
| 263 | |
| 264 | ENTRY(PALETTE16A, ARGB8888, 4, 1, rot_pal16a_argb32), |
| 265 | ENTRY(PALETTE16A, RGB888, 4, 1, rot_pal16a_rgb32), |
| 266 | |
| 267 | { -1 }, |
| 268 | }; |
| 269 | |
| 270 | static copy_info blit_info_16bpp[] = |
| 271 | { |
| 272 | /* no rotation */ |
| 273 | ENTRY(PALETTE16, RGB555, 2, 0, pal16_argb1555), |
| 274 | ENTRY(PALETTE16, ARGB1555, 2, 0, pal16_argb1555), |
| 275 | |
| 276 | ENTRY(RGB15_PALETTED, RGB555, 2, 0, rgb15pal_argb1555), |
| 277 | ENTRY(RGB15_PALETTED, ARGB1555, 2, 0, rgb15pal_argb1555), |
| 278 | |
| 279 | /* rotation */ |
| 280 | ENTRY(PALETTE16, RGB555, 2, 1, rot_pal16_argb1555), |
| 281 | ENTRY(PALETTE16, ARGB1555, 2, 1, rot_pal16_argb1555), |
| 282 | |
| 283 | ENTRY(RGB15_PALETTED, RGB555, 2, 1, rot_rgb15pal_argb1555), |
| 284 | ENTRY(RGB15_PALETTED, ARGB1555, 2, 1, rot_rgb15pal_argb1555), |
| 285 | |
| 286 | { -1 }, |
| 287 | }; |
| 288 | |
| 289 | static copy_info *blit_info[SDL_TEXFORMAT_LAST+1]; |
| 290 | |
| 291 | static struct |
| 292 | { |
| 293 | Uint32 format; |
| 294 | int status; |
| 295 | } fmt_support[30] = { { 0, 0 } }; |
| 296 | |
| 297 | |
| 298 | //============================================================ |
| 299 | // INLINES |
| 300 | //============================================================ |
| 301 | |
| 302 | |
| 303 | INLINE float round_nearest(float f) |
| 304 | { |
| 305 | return floor(f + 0.5f); |
| 306 | } |
| 307 | |
| 308 | INLINE HashT texture_compute_hash(const render_texinfo *texture, UINT32 flags) |
| 309 | { |
| 310 | return (HashT)texture->base ^ (flags & (PRIMFLAG_BLENDMODE_MASK | PRIMFLAG_TEXFORMAT_MASK)); |
| 311 | } |
| 312 | |
| 313 | INLINE Uint32 map_blendmode(int blendmode) |
| 314 | { |
| 315 | switch (blendmode) |
| 316 | { |
| 317 | case BLENDMODE_NONE: |
| 318 | return SDL_BLENDMODE_NONE; |
| 319 | case BLENDMODE_ALPHA: |
| 320 | return SDL_BLENDMODE_BLEND; |
| 321 | case BLENDMODE_RGB_MULTIPLY: |
| 322 | return SDL_BLENDMODE_MOD; |
| 323 | case BLENDMODE_ADD: |
| 324 | return SDL_BLENDMODE_ADD; |
| 325 | default: |
| 326 | mame_printf_warning("Unknown Blendmode %d", blendmode); |
| 327 | } |
| 328 | return SDL_BLENDMODE_NONE; |
| 329 | } |
| 330 | |
| 331 | INLINE void set_coloralphamode(SDL_TextureID texture_id, const render_color *color) |
| 332 | { |
| 333 | UINT32 sr = (UINT32)(255.0f * color->r); |
| 334 | UINT32 sg = (UINT32)(255.0f * color->g); |
| 335 | UINT32 sb = (UINT32)(255.0f * color->b); |
| 336 | UINT32 sa = (UINT32)(255.0f * color->a); |
| 337 | |
| 338 | |
| 339 | if (color->r >= 1.0f && color->g >= 1.0f && color->b >= 1.0f && IS_OPAQUE(color->a)) |
| 340 | { |
| 341 | SDL_SetTextureColorMod(texture_id, 0xFF, 0xFF, 0xFF); |
| 342 | SDL_SetTextureAlphaMod(texture_id, 0xFF); |
| 343 | } |
| 344 | /* coloring-only case */ |
| 345 | else if (IS_OPAQUE(color->a)) |
| 346 | { |
| 347 | SDL_SetTextureColorMod(texture_id, sr, sg, sb); |
| 348 | SDL_SetTextureAlphaMod(texture_id, 0xFF); |
| 349 | } |
| 350 | /* alpha and/or coloring case */ |
| 351 | else if (!IS_TRANSPARENT(color->a)) |
| 352 | { |
| 353 | SDL_SetTextureColorMod(texture_id, sr, sg, sb); |
| 354 | SDL_SetTextureAlphaMod(texture_id, sa); |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | SDL_SetTextureColorMod(texture_id, 0xFF, 0xFF, 0xFF); |
| 359 | SDL_SetTextureAlphaMod(texture_id, 0xFF); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | INLINE void render_quad(sdl_info *sdl, texture_info *texture, render_primitive *prim, int x, int y) |
| 364 | { |
| 365 | SDL_TextureID texture_id; |
| 366 | SDL_Rect target_rect; |
| 367 | |
| 368 | target_rect.x = x; |
| 369 | target_rect.y = y; |
| 370 | target_rect.w = round_nearest(prim->bounds.x1 - prim->bounds.x0); |
| 371 | target_rect.h = round_nearest(prim->bounds.y1 - prim->bounds.y0); |
| 372 | |
| 373 | if (texture) |
| 374 | { |
| 375 | texture_id = texture->texture_id; |
| 376 | |
| 377 | texture->copyinfo->time -= osd_ticks(); |
| 378 | if ((PRIMFLAG_GET_SCREENTEX(prim->flags)) && video_config.filter) |
| 379 | { |
| 380 | SDL_SetTextureScaleMode(texture->texture_id, SDL_TEXTURESCALEMODE_BEST); |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | SDL_SetTextureScaleMode(texture->texture_id, SDL_TEXTURESCALEMODE_FAST); |
| 385 | } |
| 386 | SDL_SetTextureBlendMode(texture_id, texture->sdl_blendmode); |
| 387 | set_coloralphamode(texture_id, &prim->color); |
| 388 | SDL_RenderCopy(texture_id, NULL, &target_rect); |
| 389 | texture->copyinfo->time += osd_ticks(); |
| 390 | |
| 391 | texture->copyinfo->pixel_count += MAX(STAT_PIXEL_THRESHOLD , (texture->rawwidth * texture->rawheight)); |
| 392 | if (sdl->last_blit_pixels) |
| 393 | { |
| 394 | texture->copyinfo->time += (sdl->last_blit_time * (INT64) (texture->rawwidth * texture->rawheight)) / (INT64) sdl->last_blit_pixels; |
| 395 | } |
| 396 | texture->copyinfo->samples++; |
| 397 | texture->copyinfo->perf = ( texture->copyinfo->pixel_count * (osd_ticks_per_second()/1000)) / texture->copyinfo->time; |
| 398 | } |
| 399 | else |
| 400 | { |
| 401 | UINT32 sr = (UINT32)(255.0f * prim->color.r); |
| 402 | UINT32 sg = (UINT32)(255.0f * prim->color.g); |
| 403 | UINT32 sb = (UINT32)(255.0f * prim->color.b); |
| 404 | UINT32 sa = (UINT32)(255.0f * prim->color.a); |
| 405 | |
| 406 | SDL_SetRenderDrawBlendMode(map_blendmode(PRIMFLAG_GET_BLENDMODE(prim->flags))); |
| 407 | SDL_SetRenderDrawColor(sr, sg, sb, sa); |
| 408 | SDL_RenderFill(&target_rect); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | #if 0 |
| 413 | int RendererSupportsFormat(Uint32 format) |
| 414 | { |
| 415 | struct SDL_RendererInfo render_info; |
| 416 | int i; |
| 417 | |
| 418 | SDL_GetRendererInfo(&render_info); |
| 419 | |
| 420 | for (i=0; i < render_info.num_texture_formats; i++) |
| 421 | { |
| 422 | if (format == render_info.texture_formats[i]) |
| 423 | return 1; |
| 424 | } |
| 425 | return 0; |
| 426 | } |
| 427 | #endif |
| 428 | |
| 429 | int RendererSupportsFormat(Uint32 format, Uint32 access) |
| 430 | { |
| 431 | int i; |
| 432 | SDL_TextureID texid; |
| 433 | for (i=0; fmt_support[i].format != 0; i++) |
| 434 | { |
| 435 | if (format == fmt_support[i].format) |
| 436 | { |
| 437 | return fmt_support[i].status; |
| 438 | } |
| 439 | } |
| 440 | /* not tested yet */ |
| 441 | fmt_support[i].format = format; |
| 442 | fmt_support[i + 1].format = 0; |
| 443 | texid = SDL_CreateTexture(format, access, 10, 10); |
| 444 | if (texid) |
| 445 | { |
| 446 | fmt_support[i].status = 1; |
| 447 | SDL_DestroyTexture(texid); |
| 448 | return 1; |
| 449 | } |
| 450 | fmt_support[i].status = 0; |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | //============================================================ |
| 455 | // draw13_init |
| 456 | //============================================================ |
| 457 | |
| 458 | static void add_list(copy_info **head, copy_info *element, Uint32 bm) |
| 459 | { |
| 460 | copy_info *new = malloc(sizeof(copy_info)); |
| 461 | *new = *element; |
| 462 | |
| 463 | new->bm_mask = bm; |
| 464 | new->next = *head; |
| 465 | *head = new; |
| 466 | } |
| 467 | |
| 468 | static void expand_copy_info(copy_info *list) |
| 469 | { |
| 470 | copy_info *bi; |
| 471 | |
| 472 | for (bi = list; bi->src_fmt != -1; bi++) |
| 473 | { |
| 474 | if (bi->bm_mask == BM_ALL) |
| 475 | { |
| 476 | add_list(&blit_info[bi->src_fmt], bi, SDL_BLENDMODE_NONE); |
| 477 | add_list(&blit_info[bi->src_fmt], bi, SDL_BLENDMODE_ADD); |
| 478 | add_list(&blit_info[bi->src_fmt], bi, SDL_BLENDMODE_MOD); |
| 479 | add_list(&blit_info[bi->src_fmt], bi, SDL_BLENDMODE_BLEND); |
| 480 | } |
| 481 | else |
| 482 | add_list(&blit_info[bi->src_fmt], bi, bi->bm_mask); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | int draw13_init(sdl_draw_info *callbacks) |
| 487 | { |
| 488 | |
| 489 | // fill in the callbacks |
| 490 | callbacks->exit = draw13_exit; |
| 491 | callbacks->attach = draw13_attach; |
| 492 | |
| 493 | mame_printf_verbose("Using SDL native texturing driver (SDL 1.3+)\n"); |
| 494 | |
| 495 | expand_copy_info(blit_info_default); |
| 496 | //FIXME: -opengl16 should be -opengl -prefer16bpp |
| 497 | //if (video_config.prefer16bpp_tex) |
| 498 | expand_copy_info(blit_info_16bpp); |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | //============================================================ |
| 505 | // draw13_exit |
| 506 | //============================================================ |
| 507 | |
| 508 | static void draw13_exit(void) |
| 509 | { |
| 510 | int i; |
| 511 | copy_info *bi; |
| 512 | for (i = 0; i <= SDL_TEXFORMAT_LAST; i++) |
| 513 | for (bi = blit_info[i]; bi != NULL; bi = bi->next) |
| 514 | { |
| 515 | if (bi->pixel_count) |
| 516 | mame_printf_verbose("%s -> %s %s blendmode 0x%02x, %d samples: %d KPixel/sec\n", bi->srcname, bi->dstname, |
| 517 | bi->rotate ? "rot" : "norot", bi->bm_mask, bi->samples, |
| 518 | (int) bi->perf); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | //============================================================ |
| 523 | // draw13_attach |
| 524 | //============================================================ |
| 525 | |
| 526 | static void draw13_attach(sdl_draw_info *info, sdl_window_info *window) |
| 527 | { |
| 528 | // fill in the callbacks |
| 529 | window->create = draw13_window_create; |
| 530 | window->resize = draw13_window_resize; |
| 531 | window->get_primitives = draw13_window_get_primitives; |
| 532 | window->draw = draw13_window_draw; |
| 533 | window->destroy = draw13_window_destroy; |
| 534 | window->destroy_all_textures = draw13_destroy_all_textures; |
| 535 | window->clear = draw13_window_clear; |
| 536 | window->xy_to_render_target = draw13_xy_to_render_target; |
| 537 | } |
| 538 | |
| 539 | //============================================================ |
| 540 | // draw13_window_create |
| 541 | //============================================================ |
| 542 | |
| 543 | static int draw13_window_create(sdl_window_info *window, int width, int height) |
| 544 | { |
| 545 | sdl_info *sdl = window->dxdata; |
| 546 | |
| 547 | // allocate memory for our structures |
| 548 | sdl = malloc(sizeof(*sdl)); |
| 549 | memset(sdl, 0, sizeof(*sdl)); |
| 550 | |
| 551 | window->dxdata = sdl; |
| 552 | |
| 553 | sdl->extra_flags = (window->fullscreen ? |
| 554 | SDL_WINDOW_BORDERLESS | SDL_WINDOW_INPUT_FOCUS : SDL_WINDOW_RESIZABLE); |
| 555 | |
| 556 | // create the SDL window |
| 557 | SDL_SelectVideoDisplay(window->monitor->handle); |
| 558 | |
| 559 | if (window->fullscreen && video_config.switchres) |
| 560 | { |
| 561 | SDL_DisplayMode mode; |
| 562 | SDL_GetCurrentDisplayMode(&mode); |
| 563 | mode.w = width; |
| 564 | mode.h = height; |
| 565 | if (window->refresh) |
| 566 | mode.refresh_rate = window->refresh; |
| 567 | if (window->depth) |
| 568 | { |
| 569 | switch (window->depth) |
| 570 | { |
| 571 | case 15: |
| 572 | mode.format = SDL_PIXELFORMAT_RGB555; |
| 573 | break; |
| 574 | case 16: |
| 575 | mode.format = SDL_PIXELFORMAT_RGB565; |
| 576 | break; |
| 577 | case 24: |
| 578 | mode.format = SDL_PIXELFORMAT_RGB24; |
| 579 | break; |
| 580 | case 32: |
| 581 | mode.format = SDL_PIXELFORMAT_RGB888; |
| 582 | break; |
| 583 | default: |
| 584 | mame_printf_warning("Ignoring depth %d\n", window->depth); |
| 585 | } |
| 586 | } |
| 587 | SDL_SetFullscreenDisplayMode(&mode); // Try to set mode |
| 588 | } |
| 589 | else |
| 590 | SDL_SetFullscreenDisplayMode(NULL); // Use desktop |
| 591 | |
| 592 | window->window_id = SDL_CreateWindow(window->title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, |
| 593 | width, height, sdl->extra_flags); |
| 594 | |
| 595 | SDL_ShowWindow(window->window_id); |
| 596 | SDL_SetWindowFullscreen(window->window_id, window->fullscreen); |
| 597 | SDL_RaiseWindow(window->window_id); |
| 598 | SDL_GetWindowSize(window->window_id, &window->width, &window->height); |
| 599 | |
| 600 | // create renderer |
| 601 | |
| 602 | if (video_config.waitvsync) |
| 603 | SDL_CreateRenderer(window->window_id, -1, SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC); |
| 604 | else |
| 605 | SDL_CreateRenderer(window->window_id, -1, SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTDISCARD); |
| 606 | |
| 607 | SDL_SelectRenderer(window->window_id); |
| 608 | |
| 609 | sdl->blittimer = 3; |
| 610 | |
| 611 | // in case any textures try to come up before these are validated, |
| 612 | // OpenGL guarantees all implementations can handle something this size. |
| 613 | sdl->texture_max_width = 64; |
| 614 | sdl->texture_max_height = 64; |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | //============================================================ |
| 620 | // draw13_window_resize |
| 621 | //============================================================ |
| 622 | |
| 623 | static void draw13_window_resize(sdl_window_info *window, int width, int height) |
| 624 | { |
| 625 | sdl_info *sdl = window->dxdata; |
| 626 | |
| 627 | SDL_SetWindowSize(window->window_id, width, height); |
| 628 | SDL_GetWindowSize(window->window_id, &window->width, &window->height); |
| 629 | sdl->blittimer = 3; |
| 630 | |
| 631 | } |
| 632 | |
| 633 | //============================================================ |
| 634 | // drawsdl_xy_to_render_target |
| 635 | //============================================================ |
| 636 | |
| 637 | static int draw13_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt) |
| 638 | { |
| 639 | sdl_info *sdl = window->dxdata; |
| 640 | |
| 641 | *xt = x - sdl->last_hofs; |
| 642 | *yt = y - sdl->last_vofs; |
| 643 | if (*xt<0 || *xt >= window->blitwidth) |
| 644 | return 0; |
| 645 | if (*yt<0 || *yt >= window->blitheight) |
| 646 | return 0; |
| 647 | return 1; |
| 648 | } |
| 649 | |
| 650 | //============================================================ |
| 651 | // draw13_window_get_primitives |
| 652 | //============================================================ |
| 653 | |
| 654 | static const render_primitive_list *draw13_window_get_primitives(sdl_window_info *window) |
| 655 | { |
| 656 | if ((!window->fullscreen) || (video_config.switchres)) |
| 657 | { |
| 658 | sdlwindow_blit_surface_size(window, window->width, window->height); |
| 659 | } |
| 660 | else |
| 661 | { |
| 662 | sdlwindow_blit_surface_size(window, window->monitor->center_width, window->monitor->center_height); |
| 663 | } |
| 664 | render_target_set_bounds(window->target, window->blitwidth, window->blitheight, sdlvideo_monitor_get_aspect(window->monitor)); |
| 665 | return render_target_get_primitives(window->target); |
| 666 | } |
| 667 | |
| 668 | //============================================================ |
| 669 | // draw13_window_draw |
| 670 | //============================================================ |
| 671 | |
| 672 | static int draw13_window_draw(sdl_window_info *window, UINT32 dc, int update) |
| 673 | { |
| 674 | sdl_info *sdl = window->dxdata; |
| 675 | render_primitive *prim; |
| 676 | texture_info *texture=NULL; |
| 677 | float vofs, hofs; |
| 678 | int blit_pixels = 0; |
| 679 | |
| 680 | if (video_config.novideo) |
| 681 | { |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | SDL_SelectRenderer(window->window_id); |
| 686 | |
| 687 | if (sdl->blittimer > 0) |
| 688 | { |
| 689 | /* SDL Underlays need alpha = 0 ! */ |
| 690 | SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); |
| 691 | SDL_SetRenderDrawColor(0,0,0,0 /*255*/); |
| 692 | SDL_RenderFill(NULL); |
| 693 | sdl->blittimer--; |
| 694 | } |
| 695 | |
| 696 | // compute centering parameters |
| 697 | vofs = hofs = 0.0f; |
| 698 | |
| 699 | if (video_config.centerv || video_config.centerh) |
| 700 | { |
| 701 | int ch, cw; |
| 702 | |
| 703 | if ((window->fullscreen) && (!video_config.switchres)) |
| 704 | { |
| 705 | ch = window->monitor->center_height; |
| 706 | cw = window->monitor->center_width; |
| 707 | } |
| 708 | else |
| 709 | { |
| 710 | ch = window->height; |
| 711 | cw = window->width; |
| 712 | } |
| 713 | |
| 714 | if (video_config.centerv) |
| 715 | { |
| 716 | vofs = (ch - window->blitheight) / 2.0f; |
| 717 | } |
| 718 | if (video_config.centerh) |
| 719 | { |
| 720 | hofs = (cw - window->blitwidth) / 2.0f; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | sdl->last_hofs = hofs; |
| 725 | sdl->last_vofs = vofs; |
| 726 | |
| 727 | osd_lock_acquire(window->primlist->lock); |
| 728 | |
| 729 | // now draw |
| 730 | for (prim = window->primlist->head; prim != NULL; prim = prim->next) |
| 731 | { |
| 732 | Uint8 sr, sg, sb, sa; |
| 733 | |
| 734 | switch (prim->type) |
| 735 | { |
| 736 | case RENDER_PRIMITIVE_LINE: |
| 737 | sr = (int)(255.0f * prim->color.r); |
| 738 | sg = (int)(255.0f * prim->color.g); |
| 739 | sb = (int)(255.0f * prim->color.b); |
| 740 | sa = (int)(255.0f * prim->color.a); |
| 741 | |
| 742 | SDL_SetRenderDrawBlendMode(map_blendmode(PRIMFLAG_GET_BLENDMODE(prim->flags))); |
| 743 | SDL_SetRenderDrawColor(sr, sg, sb, sa); |
| 744 | SDL_RenderLine(prim->bounds.x0 + hofs, prim->bounds.y0 + vofs, |
| 745 | prim->bounds.x1 + hofs, prim->bounds.y1 + vofs); |
| 746 | break; |
| 747 | case RENDER_PRIMITIVE_QUAD: |
| 748 | texture = texture_update(window, prim); |
| 749 | if (texture) |
| 750 | blit_pixels += (texture->rawheight * texture->rawwidth); |
| 751 | render_quad(sdl, texture, prim, |
| 752 | round_nearest(hofs + prim->bounds.x0), |
| 753 | round_nearest(vofs + prim->bounds.y0)); |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | osd_lock_release(window->primlist->lock); |
| 759 | |
| 760 | sdl->last_blit_pixels = blit_pixels; |
| 761 | sdl->last_blit_time = -osd_ticks(); |
| 762 | SDL_RenderPresent(); |
| 763 | sdl->last_blit_time += osd_ticks(); |
| 764 | |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | |
| 769 | //============================================================ |
| 770 | // draw13_window_clear |
| 771 | //============================================================ |
| 772 | |
| 773 | static void draw13_window_clear(sdl_window_info *window) |
| 774 | { |
| 775 | sdl_info *sdl = window->dxdata; |
| 776 | |
| 777 | sdl->blittimer = 2; |
| 778 | } |
| 779 | |
| 780 | |
| 781 | //============================================================ |
| 782 | // draw13_window_destroy |
| 783 | //============================================================ |
| 784 | |
| 785 | static void draw13_window_destroy(sdl_window_info *window) |
| 786 | { |
| 787 | sdl_info *sdl = window->dxdata; |
| 788 | |
| 789 | // skip if nothing |
| 790 | if (sdl == NULL) |
| 791 | return; |
| 792 | |
| 793 | // free the memory in the window |
| 794 | |
| 795 | draw13_destroy_all_textures(window); |
| 796 | |
| 797 | SDL_DestroyWindow(window->window_id); |
| 798 | |
| 799 | free(sdl); |
| 800 | window->dxdata = NULL; |
| 801 | } |
| 802 | |
| 803 | //============================================================ |
| 804 | // texture handling |
| 805 | //============================================================ |
| 806 | |
| 807 | //============================================================ |
| 808 | // texture_compute_size and type |
| 809 | //============================================================ |
| 810 | |
| 811 | static copy_info *texture_compute_size_type(const render_texinfo *texsource, texture_info *texture, UINT32 flags) |
| 812 | { |
| 813 | copy_info *bi; |
| 814 | copy_info *result = NULL; |
| 815 | int maxperf = 0; |
| 816 | //int bm = PRIMFLAG_GET_BLENDMODE(flags); |
| 817 | |
| 818 | for (bi = blit_info[texture->format]; bi != NULL; bi = bi->next) |
| 819 | { |
| 820 | if ((texture->is_rotated == bi->rotate) |
| 821 | && (texture->sdl_blendmode == bi->bm_mask)) |
| 822 | { |
| 823 | if (RendererSupportsFormat(bi->dst_fmt, texture->sdl_access)) |
| 824 | { |
| 825 | if (bi->perf == 0) |
| 826 | return bi; |
| 827 | else if (bi->perf > (maxperf * 102) / 100) |
| 828 | { |
| 829 | result = bi; |
| 830 | maxperf = bi->perf; |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | if (result) |
| 836 | return result; |
| 837 | /* try last resort handlers */ |
| 838 | for (bi = blit_info[texture->format]; bi != NULL; bi = bi->next) |
| 839 | { |
| 840 | if ((texture->is_rotated == bi->rotate) |
| 841 | && (texture->sdl_blendmode == bi->bm_mask)) |
| 842 | if (RendererSupportsFormat(bi->dst_fmt, texture->sdl_access)) |
| 843 | return bi; |
| 844 | } |
| 845 | //FIXME: crash implement a -do nothing handler */ |
| 846 | return NULL; |
| 847 | } |
| 848 | |
| 849 | //============================================================ |
| 850 | // texture_create |
| 851 | //============================================================ |
| 852 | |
| 853 | static texture_info *texture_create(sdl_window_info *window, const render_texinfo *texsource, quad_setup_data *setup, UINT32 flags) |
| 854 | { |
| 855 | sdl_info *sdl = window->dxdata; |
| 856 | texture_info *texture; |
| 857 | |
| 858 | // allocate a new texture |
| 859 | texture = malloc(sizeof(*texture)); |
| 860 | memset(texture, 0, sizeof(*texture)); |
| 861 | |
| 862 | // fill in the core data |
| 863 | texture->hash = texture_compute_hash(texsource, flags); |
| 864 | texture->flags = flags; |
| 865 | texture->texinfo = *texsource; |
| 866 | texture->texinfo.seqid = -1; // force set data |
| 867 | texture->is_rotated = FALSE; |
| 868 | texture->setup = *setup; |
| 869 | texture->sdl_blendmode = map_blendmode(PRIMFLAG_GET_BLENDMODE(flags)); |
| 870 | |
| 871 | switch (PRIMFLAG_GET_TEXFORMAT(flags)) |
| 872 | { |
| 873 | case TEXFORMAT_ARGB32: |
| 874 | texture->format = SDL_TEXFORMAT_ARGB32; |
| 875 | break; |
| 876 | case TEXFORMAT_RGB32: |
| 877 | texture->format = texsource->palette ? SDL_TEXFORMAT_RGB32_PALETTED : SDL_TEXFORMAT_RGB32; |
| 878 | break; |
| 879 | case TEXFORMAT_PALETTE16: |
| 880 | texture->format = SDL_TEXFORMAT_PALETTE16; |
| 881 | break; |
| 882 | case TEXFORMAT_RGB15: |
| 883 | texture->format = texsource->palette ? SDL_TEXFORMAT_RGB15_PALETTED : SDL_TEXFORMAT_RGB15; |
| 884 | break; |
| 885 | case TEXFORMAT_PALETTEA16: |
| 886 | texture->format = SDL_TEXFORMAT_PALETTE16A; |
| 887 | break; |
| 888 | case TEXFORMAT_YUY16: |
| 889 | texture->format = texsource->palette ? SDL_TEXFORMAT_YUY16_PALETTED : SDL_TEXFORMAT_YUY16; |
| 890 | break; |
| 891 | |
| 892 | default: |
| 893 | mame_printf_error("Unknown textureformat %d\n", PRIMFLAG_GET_TEXFORMAT(flags)); |
| 894 | } |
| 895 | |
| 896 | texture->rawwidth = texsource->width; |
| 897 | texture->rawheight = texsource->height; |
| 898 | if (setup->rotwidth != texture->rawwidth || setup->rotheight != texture->rawheight |
| 899 | || setup->dudx < 0 ) |
| 900 | texture->is_rotated = TRUE; |
| 901 | else |
| 902 | texture->is_rotated = FALSE; |
| 903 | |
| 904 | //texture->sdl_access = SDL_TEXTUREACCESS_STATIC; |
| 905 | texture->sdl_access = SDL_TEXTUREACCESS_STREAMING; |
| 906 | |
| 907 | // compute the size |
| 908 | texture->copyinfo = texture_compute_size_type(texsource, texture, flags); |
| 909 | |
| 910 | texture->texture_id = SDL_CreateTexture(texture->copyinfo->dst_fmt, texture->sdl_access, |
| 911 | texture->setup.rotwidth, texture->setup.rotheight); |
| 912 | |
| 913 | if ( (texture->copyinfo->func != NULL) && (texture->sdl_access == SDL_TEXTUREACCESS_STATIC)) |
| 914 | { |
| 915 | texture->pixels = malloc(texture->setup.rotwidth * texture->setup.rotheight * texture->copyinfo->dst_bpp); |
| 916 | texture->pixels_own=TRUE; |
| 917 | } |
| 918 | /* add us to the texture list */ |
| 919 | texture->next = sdl->texlist; |
| 920 | sdl->texlist = texture; |
| 921 | |
| 922 | texture->last_access = osd_ticks(); |
| 923 | |
| 924 | return texture; |
| 925 | } |
| 926 | |
| 927 | //============================================================ |
| 928 | // texture_set_data |
| 929 | //============================================================ |
| 930 | |
| 931 | static void texture_set_data(sdl_info *sdl, texture_info *texture, const render_texinfo *texsource, UINT32 flags) |
| 932 | { |
| 933 | texture->copyinfo->time -= osd_ticks(); |
| 934 | if (texture->sdl_access == SDL_TEXTUREACCESS_STATIC) |
| 935 | { |
| 936 | if ( texture->copyinfo->func ) |
| 937 | { |
| 938 | texture->pitch = texture->setup.rotwidth * texture->copyinfo->dst_bpp; |
| 939 | texture->copyinfo->func(texture, texsource); |
| 940 | } |
| 941 | else |
| 942 | { |
| 943 | texture->pixels = texsource->base; |
| 944 | texture->pitch = texture->texinfo.rowpixels * texture->copyinfo->dst_bpp; |
| 945 | } |
| 946 | SDL_UpdateTexture(texture->texture_id, NULL, texture->pixels, texture->pitch); |
| 947 | } |
| 948 | else |
| 949 | { |
| 950 | SDL_LockTexture(texture->texture_id, NULL, 1, (void **) &texture->pixels, &texture->pitch); |
| 951 | if ( texture->copyinfo->func ) |
| 952 | texture->copyinfo->func(texture, texsource); |
| 953 | else |
| 954 | { |
| 955 | UINT8 *src = texsource->base; |
| 956 | UINT8 *dst = (UINT8 *) texture->pixels; |
| 957 | int spitch = texsource->rowpixels * texture->copyinfo->dst_bpp; |
| 958 | int num = texsource->width * texture->copyinfo->dst_bpp; |
| 959 | int h = texsource->height; |
| 960 | while (h--) { |
| 961 | memcpy(dst, src, num); |
| 962 | src += spitch; |
| 963 | dst += texture->pitch; |
| 964 | } |
| 965 | } |
| 966 | SDL_UnlockTexture(texture->texture_id); |
| 967 | } |
| 968 | texture->copyinfo->time += osd_ticks(); |
| 969 | } |
| 970 | |
| 971 | //============================================================ |
| 972 | // compute rotation setup |
| 973 | //============================================================ |
| 974 | |
| 975 | static void compute_setup(const render_quad_texuv *texcoords, int texwidth, int texheight, quad_setup_data *setup, int flags) |
| 976 | { |
| 977 | float fdudx, fdvdx, fdudy, fdvdy; |
| 978 | float x0, y0, x1, y1; |
| 979 | float width, height; |
| 980 | float fscale; |
| 981 | float prec; |
| 982 | |
| 983 | /* determine U/V deltas */ |
| 984 | if ((PRIMFLAG_GET_SCREENTEX(flags))) |
| 985 | fscale = (float) video_config.prescale; |
| 986 | else |
| 987 | fscale = 1.0f; |
| 988 | |
| 989 | x0 = MIN4(texcoords->tl.u, texcoords->tr.u, texcoords->br.u, texcoords->bl.u); |
| 990 | x1 = MAX4(texcoords->tl.u, texcoords->tr.u, texcoords->br.u, texcoords->bl.u); |
| 991 | y0 = MIN4(texcoords->tl.v, texcoords->tr.v, texcoords->br.v, texcoords->bl.v); |
| 992 | y1 = MAX4(texcoords->tl.v, texcoords->tr.v, texcoords->br.v, texcoords->bl.v); |
| 993 | |
| 994 | |
| 995 | fdudx = (texcoords->tr.u - texcoords->tl.u) / (x1 - x0) / fscale; |
| 996 | fdvdx = (texcoords->tr.v - texcoords->tl.v) / (x1 - x0) / fscale; |
| 997 | fdudy = (texcoords->bl.u - texcoords->tl.u) / (y1 - y0) / fscale; |
| 998 | fdvdy = (texcoords->bl.v - texcoords->tl.v) / (y1 - y0) / fscale; |
| 999 | |
| 1000 | /* for some reasons the core introduces distortions in the order |
| 1001 | * of 0.001 when scaling to fullscreen. |
| 1002 | * Get rid of them */ |
| 1003 | prec = 100.0f * fscale; |
| 1004 | fdudx = round_nearest(fdudx * prec) / prec; |
| 1005 | fdudy = round_nearest(fdudy * prec) / prec; |
| 1006 | fdvdx = round_nearest(fdvdx * prec) / prec; |
| 1007 | fdvdy = round_nearest(fdvdy * prec) / prec; |
| 1008 | |
| 1009 | width = round_nearest(abs(fdudx * texwidth + fdvdx * texheight) * fscale * fscale); |
| 1010 | height = round_nearest(abs(fdudy * texwidth + fdvdy * texheight) * fscale * fscale); |
| 1011 | |
| 1012 | /* clamp to integers */ |
| 1013 | setup->rotwidth = width; |
| 1014 | setup->rotheight = height; |
| 1015 | |
| 1016 | /* compute start and delta U,V coordinates now */ |
| 1017 | setup->dudx = round_nearest(65536.0f * fdudx); |
| 1018 | setup->dvdx = round_nearest(65536.0f * fdvdx); |
| 1019 | setup->dudy = round_nearest(65536.0f * fdudy); |
| 1020 | setup->dvdy = round_nearest(65536.0f * fdvdy); |
| 1021 | setup->startu = round_nearest(65536.0f * texwidth * texcoords->tl.u); |
| 1022 | setup->startv = round_nearest(65536.0f * texheight * texcoords->tl.v); |
| 1023 | |
| 1024 | //printf("%d %d %d %d \n", setup->dudx, setup->dvdx, setup->dudy, setup->dvdy); |
| 1025 | /* advance U/V to the middle of the first texel |
| 1026 | * the division by 2.1 is a workaround to prevent |
| 1027 | * crashes in certain cases */ |
| 1028 | setup->startu += (float) (setup->dudx + setup->dudy) / 2.1f; |
| 1029 | setup->startv += (float) (setup->dvdx + setup->dvdy) / 2.1f; |
| 1030 | } |
| 1031 | |
| 1032 | //============================================================ |
| 1033 | // texture_find |
| 1034 | //============================================================ |
| 1035 | |
| 1036 | static texture_info *texture_find(sdl_info *sdl, const render_primitive *prim, quad_setup_data *setup) |
| 1037 | { |
| 1038 | HashT texhash = texture_compute_hash(&prim->texture, prim->flags); |
| 1039 | texture_info *texture; |
| 1040 | osd_ticks_t now = osd_ticks(); |
| 1041 | |
| 1042 | // find a match |
| 1043 | for (texture = sdl->texlist; texture != NULL; ) |
| 1044 | if (texture->hash == texhash && |
| 1045 | texture->texinfo.base == prim->texture.base && |
| 1046 | texture->texinfo.width == prim->texture.width && |
| 1047 | texture->texinfo.height == prim->texture.height && |
| 1048 | texture->texinfo.rowpixels == prim->texture.rowpixels && |
| 1049 | texture->setup.dudx == setup->dudx && |
| 1050 | texture->setup.dvdx == setup->dvdx && |
| 1051 | texture->setup.dudy == setup->dudy && |
| 1052 | texture->setup.dvdy == setup->dvdy && |
| 1053 | ((texture->flags ^ prim->flags) & (PRIMFLAG_BLENDMODE_MASK | PRIMFLAG_TEXFORMAT_MASK)) == 0) |
| 1054 | { |
| 1055 | /* would we choose another blitter ? */ |
| 1056 | if ((texture->copyinfo->samples & 0x1f) == 0x1f) |
| 1057 | { |
| 1058 | if (texture->copyinfo != texture_compute_size_type(&texture->texinfo, texture, prim->flags)) |
| 1059 | return NULL; |
| 1060 | #if 0 |
| 1061 | else |
| 1062 | { |
| 1063 | /* reset stats */ |
| 1064 | texture->copyinfo->samples = 0; |
| 1065 | texture->copyinfo->time = 0; |
| 1066 | texture->copyinfo->pixel_count = 0; |
| 1067 | } |
| 1068 | #endif |
| 1069 | } |
| 1070 | texture->last_access = now; |
| 1071 | return texture; |
| 1072 | } |
| 1073 | else |
| 1074 | { |
| 1075 | /* free resources not needed any longer? */ |
| 1076 | texture_info *expire = texture; |
| 1077 | texture = texture->next; |
| 1078 | if (now - expire->last_access > osd_ticks_per_second()) |
| 1079 | draw13_destroy_texture(sdl, expire); |
| 1080 | } |
| 1081 | |
| 1082 | // nothing found |
| 1083 | return NULL; |
| 1084 | } |
| 1085 | |
| 1086 | //============================================================ |
| 1087 | // texture_update |
| 1088 | //============================================================ |
| 1089 | |
| 1090 | static texture_info * texture_update(sdl_window_info *window, const render_primitive *prim) |
| 1091 | { |
| 1092 | sdl_info *sdl = window->dxdata; |
| 1093 | quad_setup_data setup; |
| 1094 | texture_info *texture; |
| 1095 | |
| 1096 | compute_setup(&prim->texcoords, prim->texture.width, prim->texture.height, &setup, prim->flags); |
| 1097 | |
| 1098 | texture = texture_find(sdl, prim, &setup); |
| 1099 | |
| 1100 | // if we didn't find one, create a new texture |
| 1101 | if (texture == NULL && prim->texture.base != NULL) |
| 1102 | { |
| 1103 | texture = texture_create(window, &prim->texture, &setup, prim->flags); |
| 1104 | } |
| 1105 | |
| 1106 | if (texture != NULL) |
| 1107 | { |
| 1108 | if (prim->texture.base != NULL && texture->texinfo.seqid != prim->texture.seqid) |
| 1109 | { |
| 1110 | texture->texinfo.seqid = prim->texture.seqid; |
| 1111 | // if we found it, but with a different seqid, copy the data |
| 1112 | texture_set_data(sdl, texture, &prim->texture, prim->flags); |
| 1113 | } |
| 1114 | |
| 1115 | } |
| 1116 | return texture; |
| 1117 | } |
| 1118 | |
| 1119 | static void draw13_destroy_texture(sdl_info *sdl, texture_info *texture) |
| 1120 | { |
| 1121 | texture_info *p; |
| 1122 | |
| 1123 | SDL_DestroyTexture(texture->texture_id); |
| 1124 | if ( texture->pixels_own ) |
| 1125 | { |
| 1126 | free(texture->pixels); |
| 1127 | texture->pixels=NULL; |
| 1128 | texture->pixels_own=FALSE; |
| 1129 | } |
| 1130 | |
| 1131 | for (p=sdl->texlist; p != NULL; p = p->next) |
| 1132 | if (p->next == texture) |
| 1133 | break; |
| 1134 | if (p == NULL) |
| 1135 | sdl->texlist = NULL; |
| 1136 | else |
| 1137 | p->next = texture->next; |
| 1138 | free(texture); |
| 1139 | } |
| 1140 | |
| 1141 | static void draw13_destroy_all_textures(sdl_window_info *window) |
| 1142 | { |
| 1143 | sdl_info *sdl = window->dxdata; |
| 1144 | texture_info *next_texture=NULL, *texture = NULL; |
| 1145 | int lock=FALSE; |
| 1146 | |
| 1147 | if (sdl == NULL) |
| 1148 | return; |
| 1149 | |
| 1150 | if(window->primlist && window->primlist->lock) |
| 1151 | { |
| 1152 | lock=TRUE; |
| 1153 | osd_lock_acquire(window->primlist->lock); |
| 1154 | } |
| 1155 | |
| 1156 | texture = sdl->texlist; |
| 1157 | |
| 1158 | while (texture) |
| 1159 | { |
| 1160 | next_texture = texture->next; |
| 1161 | draw13_destroy_texture(sdl, texture); |
| 1162 | texture = next_texture; |
| 1163 | } |
| 1164 | |
| 1165 | if (lock) |
| 1166 | osd_lock_release(window->primlist->lock); |
| 1167 | } |
src/osd/sdl/blit13.h
| r0 | r4071 | |
| 1 | |
| 2 | |
| 3 | |
| 4 | //============================================================ |
| 5 | // INLINE |
| 6 | //============================================================ |
| 7 | |
| 8 | INLINE UINT32 premult32(UINT32 pixel) |
| 9 | { |
| 10 | UINT8 a = (pixel >> 24) & 0xff; |
| 11 | UINT8 r = (pixel >> 16) & 0xff; |
| 12 | UINT8 g = (pixel >> 8) & 0xff; |
| 13 | UINT8 b = (pixel >> 0) & 0xff; |
| 14 | |
| 15 | return 0xFF000000 | |
| 16 | (((UINT16)r * (UINT16)a) / 255) << 16 | |
| 17 | (((UINT16)g * (UINT16)a) / 255) << 8 | |
| 18 | (((UINT16)b * (UINT16)a) / 255); |
| 19 | } |
| 20 | |
| 21 | #define CLUL(x) ((int) (x) < 0 ? 0 : ((x) > 65535 ? 255 : (x)>>8)) |
| 22 | |
| 23 | INLINE UINT32 ycc_to_rgb(UINT8 y, UINT8 cb, UINT8 cr) |
| 24 | { |
| 25 | unsigned int r, g, b, common; |
| 26 | |
| 27 | common = 298 * y - 56992; |
| 28 | r = (common + 409 * cr); |
| 29 | g = (common - 100 * cb - 208 * cr + 91776); |
| 30 | b = (common + 516 * cb - 13696); |
| 31 | |
| 32 | return 0xff000000 | (CLUL(r)<<16) | (CLUL(g)<<8) | (CLUL(b)); |
| 33 | } |
| 34 | |
| 35 | INLINE UINT32 pixel_ycc_to_rgb(UINT16 *pixel) |
| 36 | { |
| 37 | UINT32 p = *(UINT32 *)((FPTR) pixel & ~1); |
| 38 | return ycc_to_rgb((*pixel >> 8) & 0xff, (p) & 0xff, (p>>16) & 0xff); |
| 39 | } |
| 40 | |
| 41 | INLINE UINT32 pixel_ycc_to_rgb_pal(UINT16 *pixel, const rgb_t *palette) |
| 42 | { |
| 43 | UINT32 p = *(UINT32 *)((FPTR) pixel & ~1); |
| 44 | return ycc_to_rgb(palette[(*pixel >> 8) & 0xff], (p) & 0xff, (p>>16) & 0xff); |
| 45 | } |
| 46 | |
| 47 | //============================================================ |
| 48 | // Pixel conversions |
| 49 | //============================================================ |
| 50 | |
| 51 | #define OP_ARGB32_ARGB32(_src) (_src) |
| 52 | |
| 53 | #define OP_RGB32_ARGB32(_src) ((_src) | 0xff000000) |
| 54 | |
| 55 | #define OP_RGB32PAL_ARGB32(_src) \ |
| 56 | (texsource->palette[0x200 + RGB_RED(_src)] | \ |
| 57 | texsource->palette[0x100 + RGB_GREEN(_src)] | \ |
| 58 | texsource->palette[RGB_BLUE(_src)] | 0xff000000) |
| 59 | |
| 60 | #define OP_PAL16_ARGB32(_src) (0xff000000 | texsource->palette[_src]) |
| 61 | |
| 62 | #define OP_PAL16A_ARGB32(_src) (texsource->palette[_src]) |
| 63 | |
| 64 | #define OP_RGB15_ARGB32(_src) (0xff000000 | ((_src & 0x7c00) << 9) | ((_src & 0x03e0) << 6) | ((_src & 0x001f) << 3) | \ |
| 65 | ((((_src & 0x7c00) << 9) | ((_src & 0x03e0) << 6) | ((_src & 0x001f) << 3) >> 5) & 0x070707)) |
| 66 | |
| 67 | #define OP_RGB15PAL_ARGB32(_src) (0xff000000 | texsource->palette[0x40 + ((_src >> 10) & 0x1f)] | \ |
| 68 | texsource->palette[0x20 + ((_src >> 5) & 0x1f)] | texsource->palette[0x00 + ((_src >> 0) & 0x1f)]) |
| 69 | |
| 70 | #define OP_ARGB32_RGB32(_pixel) premult32(_pixel) |
| 71 | |
| 72 | #define OP_PAL16A_RGB32(_src) premult32(texsource->palette[_src]) |
| 73 | |
| 74 | #define OP_PAL16_ARGB1555(_src) ((texsource->palette[_src]&0xf80000) >> 9 | \ |
| 75 | (texsource->palette[_src]&0x00f800) >> 6 | \ |
| 76 | (texsource->palette[_src]&0x0000f8) >> 3 | 0x8000) |
| 77 | |
| 78 | #define OP_RGB15_ARGB1555(_src) ((_src) | 0x8000) |
| 79 | |
| 80 | #define OP_RGB15PAL_ARGB1555(_src) ((texsource->palette[(_src) >> 10] & 0xf8) << 7 | \ |
| 81 | (texsource->palette[((_src) >> 5) & 0x1f] & 0xf8) << 2 | \ |
| 82 | (texsource->palette[(_src) & 0x1f] & 0xf8) >> 3 | 0x8000) |
| 83 | |
| 84 | #define OP_YUV16_UYVY(_src) (_src) |
| 85 | |
| 86 | #define OP_YUV16PAL_UYVY(_src) ((texsource->palette[((_src) >> 8) & 0xff] << 8) | ((_src) & 0x00ff)) |
| 87 | |
| 88 | #define OP_YUV16PAL_YVYU(_src) ((texsource->palette[((_src) >> 8) & 0xff] & 0xff) | ((_src & 0xff) << 8)) |
| 89 | |
| 90 | #define OP_YUV16_YVYU(_src) ((((_src) >> 8) & 0xff) | ((_src & 0xff) << 8)) |
| 91 | |
| 92 | #define OP_YUV16_YUY2(_src) ( ((_src) & 0xff00ff00) | \ |
| 93 | (((_src)>>16)&0xff) | (((_src)<<16)&0xff0000) ) |
| 94 | |
| 95 | #define OP_YUV16PAL_YUY2(_src) ( (texsource->palette[((_src)>>8) & 0xff]) | \ |
| 96 | (texsource->palette[((_src)>>24) & 0xff]<<16) | \ |
| 97 | (((_src)<<8)&0xff00ff00) ) |
| 98 | |
| 99 | #define OP_YUV16_ARGB32(_src) \ |
| 100 | (UINT64) ycc_to_rgb(((_src) >> 8) & 0xff, (_src) & 0xff , ((_src)>>16) & 0xff) \ |
| 101 | | ((UINT64)ycc_to_rgb(((_src) >> 24) & 0xff, (_src) & 0xff , ((_src)>>16) & 0xff) << 32) |
| 102 | |
| 103 | #define OP_YUV16PAL_ARGB32(_src) \ |
| 104 | (UINT64)ycc_to_rgb(texsource->palette[((_src) >> 8) & 0xff], (_src) & 0xff , ((_src)>>16) & 0xff) \ |
| 105 | | ((UINT64)ycc_to_rgb(texsource->palette[((_src) >> 24) & 0xff], (_src) & 0xff , ((_src)>>16) & 0xff) << 32) |
| 106 | |
| 107 | #define OP_YUV16_ARGB32ROT(_src) pixel_ycc_to_rgb(&(_src)) |
| 108 | |
| 109 | #define OP_YUV16PAL_ARGB32ROT(_src) pixel_ycc_to_rgb_pal(&(_src), texsource->palette) |
| 110 | |
| 111 | //============================================================ |
| 112 | // Copy and rotation |
| 113 | //============================================================ |
| 114 | |
| 115 | #define TEXCOPY_M( _name, _src_type, _dest_type, _op, _len_div) \ |
| 116 | INLINE void texcopy_##_name (texture_info *texture, const render_texinfo *texsource) { \ |
| 117 | int x, y; \ |
| 118 | /* loop over Y */ \ |
| 119 | for (y = 0; y < texsource->height; y++) { \ |
| 120 | _src_type *src = (_src_type *)texsource->base + y * texsource->rowpixels / (_len_div); \ |
| 121 | _dest_type *dst = (_dest_type *)((UINT8 *)texture->pixels + y * texture->pitch); \ |
| 122 | for (x = 0; x < texsource->width / (_len_div); x++) { \ |
| 123 | *dst++ = _op(*src); \ |
| 124 | src++; \ |
| 125 | } \ |
| 126 | } \ |
| 127 | } |
| 128 | |
| 129 | #define TEXCOPY( _name, _src_type, _dest_type, _op) \ |
| 130 | TEXCOPY_M( _name, _src_type, _dest_type, _op, 1) |
| 131 | |
| 132 | #define TEXROT( _name, _src_type, _dest_type, _op) \ |
| 133 | INLINE void texcopy_rot_##_name (texture_info *texture, const render_texinfo *texsource) { \ |
| 134 | int x, y; \ |
| 135 | quad_setup_data *setup = &texture->setup; \ |
| 136 | int dudx = setup->dudx; \ |
| 137 | int dvdx = setup->dvdx; \ |
| 138 | /* loop over Y */ \ |
| 139 | for (y = 0; y < setup->rotheight; y++) { \ |
| 140 | INT32 curu = setup->startu + y * setup->dudy; \ |
| 141 | INT32 curv = setup->startv + y * setup->dvdy; \ |
| 142 | _dest_type *dst = (_dest_type *)((UINT8 *)texture->pixels + y * texture->pitch); \ |
| 143 | for (x = 0; x < setup->rotwidth; x++) { \ |
| 144 | _src_type *src = (_src_type *) texsource->base + (curv >> 16) * texsource->rowpixels + (curu >> 16); \ |
| 145 | *dst++ = _op(*src); \ |
| 146 | curu += dudx; \ |
| 147 | curv += dvdx; \ |
| 148 | } \ |
| 149 | } \ |
| 150 | } |
| 151 | |
| 152 | //TEXCOPY(argb32_argb32, UINT32, UINT32, OP_ARGB32_ARGB32) |
| 153 | |
| 154 | TEXCOPY(rgb32_argb32, UINT32, UINT32, OP_RGB32_ARGB32) |
| 155 | TEXCOPY(rgb32pal_argb32, UINT32, UINT32, OP_RGB32PAL_ARGB32) |
| 156 | TEXCOPY(pal16_argb32, UINT16, UINT32, OP_PAL16_ARGB32) |
| 157 | TEXCOPY(pal16a_argb32, UINT16, UINT32, OP_PAL16A_ARGB32) |
| 158 | TEXCOPY(rgb15_argb32, UINT16, UINT32, OP_RGB15_ARGB32) |
| 159 | TEXCOPY(rgb15pal_argb32, UINT16, UINT32, OP_RGB15PAL_ARGB32) |
| 160 | |
| 161 | TEXCOPY(pal16_argb1555, UINT16, UINT16, OP_PAL16_ARGB1555) |
| 162 | TEXCOPY(rgb15_argb1555, UINT16, UINT16, OP_RGB15_ARGB1555) |
| 163 | TEXCOPY(rgb15pal_argb1555, UINT16, UINT16, OP_RGB15PAL_ARGB1555) |
| 164 | |
| 165 | TEXCOPY(argb32_rgb32, UINT32, UINT32, OP_ARGB32_RGB32) |
| 166 | TEXCOPY(pal16a_rgb32, UINT16, UINT32, OP_PAL16A_RGB32) |
| 167 | |
| 168 | TEXCOPY_M(yuv16_argb32, UINT32, UINT64, OP_YUV16_ARGB32, 2) |
| 169 | TEXCOPY_M(yuv16pal_argb32, UINT32, UINT64, OP_YUV16PAL_ARGB32, 2) |
| 170 | |
| 171 | //TEXCOPY(yuv16_uyvy, UINT16, UINT16, OP_YUV16_UYVY) |
| 172 | |
| 173 | TEXCOPY(yuv16pal_uyvy, UINT16, UINT16, OP_YUV16PAL_UYVY) |
| 174 | |
| 175 | TEXCOPY(yuv16_yvyu, UINT16, UINT16, OP_YUV16_YVYU) |
| 176 | TEXCOPY(yuv16pal_yvyu, UINT16, UINT16, OP_YUV16PAL_YVYU) |
| 177 | |
| 178 | TEXCOPY_M(yuv16_yuy2, UINT32, UINT32, OP_YUV16_YUY2, 2) |
| 179 | TEXCOPY_M(yuv16pal_yuy2, UINT32, UINT32, OP_YUV16PAL_YUY2, 2) |
| 180 | |
| 181 | |
| 182 | TEXROT(argb32_argb32, UINT32, UINT32, OP_ARGB32_ARGB32) |
| 183 | TEXROT(rgb32_argb32, UINT32, UINT32, OP_RGB32_ARGB32) |
| 184 | TEXROT(rgb32pal_argb32, UINT32, UINT32, OP_RGB32PAL_ARGB32) |
| 185 | TEXROT(pal16_argb32, UINT16, UINT32, OP_PAL16_ARGB32) |
| 186 | TEXROT(pal16a_argb32, UINT16, UINT32, OP_PAL16A_ARGB32) |
| 187 | TEXROT(rgb15_argb32, UINT16, UINT32, OP_RGB15_ARGB32) |
| 188 | TEXROT(rgb15pal_argb32, UINT16, UINT32, OP_RGB15PAL_ARGB32) |
| 189 | |
| 190 | TEXROT(pal16_argb1555, UINT16, UINT16, OP_PAL16_ARGB1555) |
| 191 | TEXROT(rgb15_argb1555, UINT16, UINT16, OP_RGB15_ARGB1555) |
| 192 | TEXROT(rgb15pal_argb1555, UINT16, UINT16, OP_RGB15PAL_ARGB1555) |
| 193 | |
| 194 | TEXROT(argb32_rgb32, UINT32, UINT32, OP_ARGB32_RGB32) |
| 195 | TEXROT(pal16a_rgb32, UINT16, UINT32, OP_PAL16A_RGB32) |
| 196 | |
| 197 | TEXROT(yuv16_argb32, UINT16, UINT32, OP_YUV16_ARGB32ROT) |
| 198 | TEXROT(yuv16pal_argb32, UINT16, UINT32, OP_YUV16PAL_ARGB32ROT) |