all repos — mgba @ a9ba3a2094a217453f87cc6f91024db991b19745

mGBA Game Boy Advance Emulator

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 mSDLSWInit(struct mSDLRenderer* renderer);
 12static void mSDLSWRunloopGBA(struct mSDLRenderer* renderer, void* user);
 13static void mSDLSWDeinit(struct mSDLRenderer* renderer);
 14
 15void mSDLSWCreate(struct mSDLRenderer* renderer) {
 16	renderer->init = mSDLSWInit;
 17	renderer->deinit = mSDLSWDeinit;
 18	renderer->runloop = mSDLSWRunloopGBA;
 19}
 20
 21bool mSDLSWInit(struct mSDLRenderer* 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 mSDLSWRunloopGBA(struct mSDLRenderer* renderer, void* user) {
 86	struct GBAThread* context = user;
 87	SDL_Event event;
 88#if !SDL_VERSION_ATLEAST(2, 0, 0)
 89	SDL_Surface* surface = SDL_GetVideoSurface();
 90#endif
 91
 92	while (context->state < THREAD_EXITING) {
 93		while (SDL_PollEvent(&event)) {
 94			mSDLHandleEventGBA(context, &renderer->player, &event);
 95		}
 96
 97		if (mCoreSyncWaitFrameStart(&context->sync)) {
 98#if SDL_VERSION_ATLEAST(2, 0, 0)
 99			SDL_UnlockTexture(renderer->sdlTex);
100			SDL_RenderCopy(renderer->sdlRenderer, renderer->sdlTex, 0, 0);
101			SDL_RenderPresent(renderer->sdlRenderer);
102			SDL_LockTexture(renderer->sdlTex, 0, (void**) &renderer->d.outputBuffer, &renderer->d.outputBufferStride);
103			renderer->d.outputBufferStride /= BYTES_PER_PIXEL;
104#else
105#ifdef USE_PIXMAN
106			if (renderer->ratio > 1) {
107				pixman_image_composite32(PIXMAN_OP_SRC, renderer->pix, 0, renderer->screenpix,
108				    0, 0, 0, 0, 0, 0,
109				    renderer->viewportWidth, renderer->viewportHeight);
110			}
111#else
112			switch (renderer->ratio) {
113#if defined(__ARM_NEON) && COLOR_16_BIT
114			case 2:
115				_neon2x(surface->pixels, renderer->d.outputBuffer, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
116				break;
117			case 4:
118				_neon4x(surface->pixels, renderer->d.outputBuffer, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
119				break;
120#endif
121			case 1:
122				break;
123			default:
124				abort();
125			}
126#endif
127			SDL_UnlockSurface(surface);
128			SDL_Flip(surface);
129			SDL_LockSurface(surface);
130#endif
131		}
132		mCoreSyncWaitFrameEnd(&context->sync);
133	}
134}
135
136void mSDLSWDeinit(struct mSDLRenderer* renderer) {
137	if (renderer->ratio > 1) {
138		free(renderer->d.outputBuffer);
139	}
140#if !SDL_VERSION_ATLEAST(2, 0, 0)
141	SDL_Surface* surface = SDL_GetVideoSurface();
142	SDL_UnlockSurface(surface);
143#ifdef USE_PIXMAN
144	pixman_image_unref(renderer->pix);
145	pixman_image_unref(renderer->screenpix);
146#endif
147#endif
148}