src/platform/sdl/sw-sdl.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "main.h"
7
8#include "gba/supervisor/thread.h"
9#include "util/arm-algo.h"
10
11static bool GBASDLSWInit(struct SDLSoftwareRenderer* renderer);
12static void GBASDLSWRunloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer);
13static void GBASDLSWDeinit(struct SDLSoftwareRenderer* renderer);
14
15void GBASDLSWCreate(struct SDLSoftwareRenderer* renderer) {
16 renderer->init = GBASDLSWInit;
17 renderer->deinit = GBASDLSWDeinit;
18 renderer->runloop = GBASDLSWRunloop;
19}
20
21bool GBASDLSWInit(struct SDLSoftwareRenderer* renderer) {
22#if !SDL_VERSION_ATLEAST(2, 0, 0)
23#ifdef COLOR_16_BIT
24 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_DOUBLEBUF | SDL_HWSURFACE);
25#else
26 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_DOUBLEBUF | SDL_HWSURFACE);
27#endif
28#endif
29
30#if SDL_VERSION_ATLEAST(2, 0, 0)
31 renderer->window = SDL_CreateWindow(projectName, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer->viewportWidth, renderer->viewportHeight, SDL_WINDOW_OPENGL | (SDL_WINDOW_FULLSCREEN_DESKTOP * renderer->player.fullscreen));
32 SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
33 renderer->player.window = renderer->window;
34 renderer->sdlRenderer = SDL_CreateRenderer(renderer->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
35#ifdef COLOR_16_BIT
36#ifdef COLOR_5_6_5
37 renderer->sdlTex = SDL_CreateTexture(renderer->sdlRenderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
38#else
39 renderer->sdlTex = SDL_CreateTexture(renderer->sdlRenderer, SDL_PIXELFORMAT_ABGR1555, SDL_TEXTUREACCESS_STREAMING, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
40#endif
41#else
42 renderer->sdlTex = SDL_CreateTexture(renderer->sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
43#endif
44
45 SDL_LockTexture(renderer->sdlTex, 0, (void**) &renderer->d.outputBuffer, &renderer->d.outputBufferStride);
46 renderer->d.outputBufferStride /= BYTES_PER_PIXEL;
47#else
48 SDL_Surface* surface = SDL_GetVideoSurface();
49 SDL_LockSurface(surface);
50
51 if (renderer->ratio == 1) {
52 renderer->d.outputBuffer = surface->pixels;
53 renderer->d.outputBufferStride = surface->pitch / BYTES_PER_PIXEL;
54 } else {
55#ifdef USE_PIXMAN
56 renderer->d.outputBuffer = malloc(VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL);
57 renderer->d.outputBufferStride = VIDEO_HORIZONTAL_PIXELS;
58#ifdef COLOR_16_BIT
59#ifdef COLOR_5_6_5
60 pixman_format_code_t format = PIXMAN_r5g6b5;
61#else
62 pixman_format_code_t format = PIXMAN_x1b5g5r5;
63#endif
64#else
65 pixman_format_code_t format = PIXMAN_x8b8g8r8;
66#endif
67 renderer->pix = pixman_image_create_bits(format, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS,
68 renderer->d.outputBuffer, renderer->d.outputBufferStride * BYTES_PER_PIXEL);
69 renderer->screenpix = pixman_image_create_bits(format, renderer->viewportWidth, renderer->viewportHeight, surface->pixels, surface->pitch);
70
71 pixman_transform_t transform;
72 pixman_transform_init_identity(&transform);
73 pixman_transform_scale(0, &transform, pixman_int_to_fixed(renderer->ratio), pixman_int_to_fixed(renderer->ratio));
74 pixman_image_set_transform(renderer->pix, &transform);
75 pixman_image_set_filter(renderer->pix, PIXMAN_FILTER_NEAREST, 0, 0);
76#else
77 return false;
78#endif
79 }
80#endif
81
82 return true;
83}
84
85void GBASDLSWRunloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer) {
86 SDL_Event event;
87#if !SDL_VERSION_ATLEAST(2, 0, 0)
88 SDL_Surface* surface = SDL_GetVideoSurface();
89#endif
90
91 while (context->state < THREAD_EXITING) {
92 while (SDL_PollEvent(&event)) {
93 GBASDLHandleEvent(context, &renderer->player, &event);
94 }
95
96 if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
97#if SDL_VERSION_ATLEAST(2, 0, 0)
98 SDL_UnlockTexture(renderer->sdlTex);
99 SDL_RenderCopy(renderer->sdlRenderer, renderer->sdlTex, 0, 0);
100 SDL_RenderPresent(renderer->sdlRenderer);
101 SDL_LockTexture(renderer->sdlTex, 0, (void**) &renderer->d.outputBuffer, &renderer->d.outputBufferStride);
102 renderer->d.outputBufferStride /= BYTES_PER_PIXEL;
103#else
104#ifdef USE_PIXMAN
105 if (renderer->ratio > 1) {
106 pixman_image_composite32(PIXMAN_OP_SRC, renderer->pix, 0, renderer->screenpix,
107 0, 0, 0, 0, 0, 0,
108 renderer->viewportWidth, renderer->viewportHeight);
109 }
110#else
111 switch (renderer->ratio) {
112#if defined(__ARM_NEON) && COLOR_16_BIT
113 case 2:
114 _neon2x(surface->pixels, renderer->d.outputBuffer, 240, 160);
115 break;
116 case 4:
117 _neon4x(surface->pixels, renderer->d.outputBuffer, 240, 160);
118 break;
119#endif
120 case 1:
121 break;
122 default:
123 abort();
124 }
125#endif
126 SDL_UnlockSurface(surface);
127 SDL_Flip(surface);
128 SDL_LockSurface(surface);
129#endif
130 }
131 GBASyncWaitFrameEnd(&context->sync);
132 }
133}
134
135void GBASDLSWDeinit(struct SDLSoftwareRenderer* renderer) {
136 if (renderer->ratio > 1) {
137 free(renderer->d.outputBuffer);
138 }
139#if !SDL_VERSION_ATLEAST(2, 0, 0)
140 SDL_Surface* surface = SDL_GetVideoSurface();
141 SDL_UnlockSurface(surface);
142#ifdef USE_PIXMAN
143 pixman_image_unref(renderer->pix);
144 pixman_image_unref(renderer->screenpix);
145#endif
146#endif
147}