Initial support for software rendering using Cairo

This commit is contained in:
Joshua Granick
2015-05-01 18:13:40 -07:00
parent 491a15a7e9
commit 6e705aa23f
14 changed files with 325 additions and 156 deletions

View File

@@ -3,6 +3,7 @@
#include <ui/Window.h>
#include <hx/CFFI.h>
namespace lime {
@@ -14,6 +15,8 @@ namespace lime {
public:
virtual void Flip () = 0;
virtual value Lock () = 0;
virtual void Unlock () = 0;
Window* currentWindow;

View File

@@ -708,6 +708,21 @@ namespace lime {
}
value lime_renderer_lock (value renderer) {
return ((Renderer*)(intptr_t)val_float (renderer))->Lock ();
}
value lime_renderer_unlock (value renderer) {
((Renderer*)(intptr_t)val_float (renderer))->Unlock ();
return alloc_null ();
}
value lime_system_get_directory (value type, value company, value title) {
const char* companyName = "";
@@ -987,6 +1002,8 @@ namespace lime {
DEFINE_PRIM (lime_neko_execute, 1);
DEFINE_PRIM (lime_renderer_create, 1);
DEFINE_PRIM (lime_renderer_flip, 1);
DEFINE_PRIM (lime_renderer_lock, 1);
DEFINE_PRIM (lime_renderer_unlock, 1);
DEFINE_PRIM (lime_render_event_manager_register, 2);
DEFINE_PRIM (lime_system_get_directory, 3);
DEFINE_PRIM (lime_system_get_timer, 0);

View File

@@ -10,8 +10,19 @@ namespace lime {
currentWindow = window;
sdlWindow = ((SDLWindow*)window)->sdlWindow;
sdlTexture = 0;
int sdlFlags = SDL_RENDERER_ACCELERATED;
int sdlFlags = 0;
if (window->flags & WINDOW_FLAG_HARDWARE) {
sdlFlags |= SDL_RENDERER_ACCELERATED;
} else {
sdlFlags |= SDL_RENDERER_SOFTWARE;
}
if (window->flags & WINDOW_FLAG_VSYNC) sdlFlags |= SDL_RENDERER_PRESENTVSYNC;
@@ -46,6 +57,51 @@ namespace lime {
}
value SDLRenderer::Lock () {
int width;
int height;
SDL_GetRendererOutputSize (sdlRenderer, &width, &height);
if (!sdlTexture) {
sdlTexture = SDL_CreateTexture (sdlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height);
}
value result = alloc_empty_object ();
void *pixels;
int pitch;
if (SDL_LockTexture (sdlTexture, NULL, &pixels, &pitch) == 0) {
alloc_field (result, val_id ("width"), alloc_int (width));
alloc_field (result, val_id ("height"), alloc_int (height));
alloc_field (result, val_id ("pixels"), alloc_float ((intptr_t)pixels));
alloc_field (result, val_id ("pitch"), alloc_int (pitch));
}
return result;
}
void SDLRenderer::Unlock () {
if (sdlTexture) {
SDL_UnlockTexture (sdlTexture);
SDL_RenderClear (sdlRenderer);
SDL_RenderCopy (sdlRenderer, sdlTexture, NULL, NULL);
}
}
Renderer* CreateRenderer (Window* window) {
return new SDLRenderer (window);

View File

@@ -17,8 +17,11 @@ namespace lime {
~SDLRenderer ();
virtual void Flip ();
virtual value Lock ();
virtual void Unlock ();
SDL_Renderer* sdlRenderer;
SDL_Texture* sdlTexture;
SDL_Window* sdlWindow;
};

View File

@@ -16,42 +16,46 @@ namespace lime {
currentApplication = application;
this->flags = flags;
int sdlFlags = SDL_WINDOW_OPENGL;
int sdlFlags = 0;
if (flags & WINDOW_FLAG_FULLSCREEN) sdlFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
if (flags & WINDOW_FLAG_RESIZABLE) sdlFlags |= SDL_WINDOW_RESIZABLE;
if (flags & WINDOW_FLAG_BORDERLESS) sdlFlags |= SDL_WINDOW_BORDERLESS;
#if defined (HX_WINDOWS) && defined (NATIVE_TOOLKIT_SDL_ANGLE)
SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute (SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_SetHint (SDL_HINT_VIDEO_WIN_D3DCOMPILER, "d3dcompiler_47.dll");
#endif
if (flags & WINDOW_FLAG_DEPTH_BUFFER) {
if (flags & WINDOW_FLAG_HARDWARE) {
SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 32 - (flags & WINDOW_FLAG_STENCIL_BUFFER) ? 8 : 0);
sdlFlags |= SDL_WINDOW_OPENGL;
}
if (flags & WINDOW_FLAG_STENCIL_BUFFER) {
#if defined (HX_WINDOWS) && defined (NATIVE_TOOLKIT_SDL_ANGLE)
SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute (SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_SetHint (SDL_HINT_VIDEO_WIN_D3DCOMPILER, "d3dcompiler_47.dll");
#endif
SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
if (flags & WINDOW_FLAG_DEPTH_BUFFER) {
SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 32 - (flags & WINDOW_FLAG_STENCIL_BUFFER) ? 8 : 0);
}
}
if (flags & WINDOW_FLAG_HW_AA_HIRES) {
if (flags & WINDOW_FLAG_STENCIL_BUFFER) {
SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
}
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, true);
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, 4);
} else if (flags & WINDOW_FLAG_HW_AA) {
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, true);
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, 2);
if (flags & WINDOW_FLAG_HW_AA_HIRES) {
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, true);
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, 4);
} else if (flags & WINDOW_FLAG_HW_AA) {
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, true);
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, 2);
}
}

View File

@@ -175,6 +175,74 @@ namespace lime {
}
value lime_cairo_pattern_create_for_surface (value surface) {
return alloc_float ((intptr_t)cairo_pattern_create_for_surface ((cairo_surface_t*)(intptr_t)val_float (surface)));
}
value lime_cairo_pattern_destroy (value handle) {
cairo_pattern_destroy ((cairo_pattern_t*)(intptr_t)val_float (handle));
return alloc_null ();
}
value lime_cairo_pattern_get_extend (value handle) {
return alloc_int (cairo_pattern_get_extend ((cairo_pattern_t*)(intptr_t)val_float (handle)));
}
value lime_cairo_pattern_set_extend (value handle, value extend) {
cairo_pattern_set_extend ((cairo_pattern_t*)(intptr_t)val_float (handle), (cairo_extend_t)val_int (extend));
return alloc_null ();
}
value lime_cairo_pattern_get_filter (value handle) {
return alloc_int (cairo_pattern_get_filter ((cairo_pattern_t*)(intptr_t)val_float (handle)));
}
value lime_cairo_pattern_set_filter (value handle, value filter) {
cairo_pattern_set_filter ((cairo_pattern_t*)(intptr_t)val_float (handle), (cairo_filter_t)val_int (filter));
return alloc_null ();
}
value lime_cairo_pattern_get_matrix (value handle) {
cairo_matrix_t cm;
cairo_pattern_get_matrix ((cairo_pattern_t*)(intptr_t)val_float (handle), &cm);
Matrix3 mat3 = Matrix3 (cm.xx, cm.yx, cm.xy, cm.yy, cm.x0, cm.y0);
return mat3.Value ();
}
value lime_cairo_pattern_set_matrix (value handle, value matrix) {
Matrix3 mat3 = Matrix3 (matrix);
cairo_matrix_t cm;
cairo_matrix_init (&cm, mat3.a, mat3.b, mat3.c, mat3.d, mat3.tx, mat3.ty);
cairo_pattern_set_matrix ((cairo_pattern_t*)(intptr_t)val_float (handle), &cm);
return alloc_null ();
}
value lime_cairo_pop_group (value handle) {
return alloc_float ((intptr_t)cairo_pop_group ((cairo_t*)(intptr_t)val_float (handle)));
@@ -339,6 +407,22 @@ namespace lime {
}
value lime_cairo_surface_destroy (value handle) {
cairo_surface_destroy ((cairo_surface_t*)(intptr_t)val_float (handle));
return alloc_null ();
}
value lime_cairo_surface_flush (value handle) {
cairo_surface_flush ((cairo_surface_t*)(intptr_t)val_float (handle));
return alloc_null ();
}
value lime_cairo_transform (value handle, value matrix) {
Matrix3 mat3 = Matrix3 (matrix);
@@ -352,82 +436,6 @@ namespace lime {
}
value lime_cairo_pattern_create_for_surface (value surface) {
return alloc_float ((intptr_t)cairo_pattern_create_for_surface ((cairo_surface_t*)(intptr_t)val_float (surface)));
}
value lime_cairo_pattern_destroy (value handle) {
cairo_pattern_destroy ((cairo_pattern_t*)(intptr_t)val_float (handle));
return alloc_null ();
}
value lime_cairo_pattern_get_extend (value handle) {
return alloc_int (cairo_pattern_get_extend ((cairo_pattern_t*)(intptr_t)val_float (handle)));
}
value lime_cairo_pattern_set_extend (value handle, value extend) {
cairo_pattern_set_extend ((cairo_pattern_t*)(intptr_t)val_float (handle), (cairo_extend_t)val_int (extend));
return alloc_null ();
}
value lime_cairo_pattern_get_filter (value handle) {
return alloc_int (cairo_pattern_get_filter ((cairo_pattern_t*)(intptr_t)val_float (handle)));
}
value lime_cairo_pattern_set_filter (value handle, value filter) {
cairo_pattern_set_filter ((cairo_pattern_t*)(intptr_t)val_float (handle), (cairo_filter_t)val_int (filter));
return alloc_null ();
}
value lime_cairo_pattern_get_matrix (value handle) {
cairo_matrix_t cm;
cairo_pattern_get_matrix ((cairo_pattern_t*)(intptr_t)val_float (handle), &cm);
Matrix3 mat3 = Matrix3 (cm.xx, cm.yx, cm.xy, cm.yy, cm.x0, cm.y0);
return mat3.Value ();
}
value lime_cairo_pattern_set_matrix (value handle, value matrix) {
Matrix3 mat3 = Matrix3 (matrix);
cairo_matrix_t cm;
cairo_matrix_init (&cm, mat3.a, mat3.b, mat3.c, mat3.d, mat3.tx, mat3.ty);
cairo_pattern_set_matrix ((cairo_pattern_t*)(intptr_t)val_float (handle), &cm);
return alloc_null ();
}
value lime_cairo_surface_destroy (value handle) {
cairo_surface_destroy ((cairo_surface_t*)(intptr_t)val_float (handle));
return alloc_null ();
}
value lime_cairo_version () {
return alloc_int (cairo_version ());
@@ -464,6 +472,14 @@ namespace lime {
DEFINE_PRIM (lime_cairo_new_path, 1);
DEFINE_PRIM (lime_cairo_paint, 1);
DEFINE_PRIM (lime_cairo_paint_with_alpha, 2);
DEFINE_PRIM (lime_cairo_pattern_create_for_surface, 1);
DEFINE_PRIM (lime_cairo_pattern_destroy, 1);
DEFINE_PRIM (lime_cairo_pattern_get_extend, 1);
DEFINE_PRIM (lime_cairo_pattern_get_filter, 1);
DEFINE_PRIM (lime_cairo_pattern_get_matrix, 1);
DEFINE_PRIM (lime_cairo_pattern_set_extend, 2);
DEFINE_PRIM (lime_cairo_pattern_set_filter, 2);
DEFINE_PRIM (lime_cairo_pattern_set_matrix, 2);
DEFINE_PRIM (lime_cairo_pop_group, 1);
DEFINE_PRIM (lime_cairo_pop_group_to_source, 1);
DEFINE_PRIM (lime_cairo_push_group, 1);
@@ -484,16 +500,9 @@ namespace lime {
DEFINE_PRIM (lime_cairo_set_source_surface, 4);
DEFINE_PRIM (lime_cairo_stroke, 1);
DEFINE_PRIM (lime_cairo_stroke_preserve, 1);
DEFINE_PRIM (lime_cairo_transform, 2);
DEFINE_PRIM (lime_cairo_pattern_create_for_surface, 1);
DEFINE_PRIM (lime_cairo_pattern_destroy, 1);
DEFINE_PRIM (lime_cairo_pattern_get_extend, 1);
DEFINE_PRIM (lime_cairo_pattern_get_filter, 1);
DEFINE_PRIM (lime_cairo_pattern_get_matrix, 1);
DEFINE_PRIM (lime_cairo_pattern_set_extend, 2);
DEFINE_PRIM (lime_cairo_pattern_set_filter, 2);
DEFINE_PRIM (lime_cairo_pattern_set_matrix, 2);
DEFINE_PRIM (lime_cairo_surface_destroy, 1);
DEFINE_PRIM (lime_cairo_surface_flush, 1);
DEFINE_PRIM (lime_cairo_transform, 2);
DEFINE_PRIM (lime_cairo_version, 0);
DEFINE_PRIM (lime_cairo_version_string, 0);