Search
lxdream.org :: lxdream/src/drivers/video_gdk.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/video_gdk.c
changeset 653:3202ff01d48e
next661:99d9494955a9
author nkeynes
date Fri Mar 28 12:32:25 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Merge lxdream-render branch (643:670) to trunk
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/drivers/video_gdk.c Fri Mar 28 12:32:25 2008 +0000
1.3 @@ -0,0 +1,203 @@
1.4 +/**
1.5 + * $Id$
1.6 + *
1.7 + * The PC side of the video support (responsible for actually displaying /
1.8 + * rendering frames)
1.9 + *
1.10 + * Copyright (c) 2005 Nathan Keynes.
1.11 + *
1.12 + * This program is free software; you can redistribute it and/or modify
1.13 + * it under the terms of the GNU General Public License as published by
1.14 + * the Free Software Foundation; either version 2 of the License, or
1.15 + * (at your option) any later version.
1.16 + *
1.17 + * This program is distributed in the hope that it will be useful,
1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.20 + * GNU General Public License for more details.
1.21 + */
1.22 +#include <assert.h>
1.23 +#include <stdlib.h>
1.24 +#include <gdk/gdk.h>
1.25 +#include <gdk-pixbuf/gdk-pixbuf.h>
1.26 +#include <gtk/gtkwidget.h>
1.27 +#include <GL/osmesa.h>
1.28 +#include "lxdream.h"
1.29 +#include "display.h"
1.30 +
1.31 +#define MAX_PIXBUF 16
1.32 +
1.33 +extern GtkWidget *gtk_video_win;
1.34 +extern int video_width, video_height;
1.35 +
1.36 +static render_buffer_t gdk_pixbuf_create_render_buffer( uint32_t width, uint32_t height );
1.37 +static void gdk_pixbuf_destroy_render_buffer( render_buffer_t buffer );
1.38 +static gboolean gdk_pixbuf_set_render_target( render_buffer_t buffer );
1.39 +static gboolean gdk_pixbuf_display_render_buffer( render_buffer_t buffer );
1.40 +static void gdk_pixbuf_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
1.41 +static gboolean gdk_pixbuf_display_blank( uint32_t colour );
1.42 +static gboolean gdk_pixbuf_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
1.43 +
1.44 +static void *pixbuf_array[MAX_PIXBUF];
1.45 +unsigned int pixbuf_max = 0;
1.46 +OSMesaContext osmesa_context = NULL;
1.47 +
1.48 +void video_gdk_init_driver( display_driver_t driver )
1.49 +{
1.50 + pixbuf_max = 0;
1.51 + driver->create_render_buffer = gdk_pixbuf_create_render_buffer;
1.52 + driver->destroy_render_buffer = gdk_pixbuf_destroy_render_buffer;
1.53 + driver->set_render_target = gdk_pixbuf_set_render_target;
1.54 + driver->display_render_buffer = gdk_pixbuf_display_render_buffer;
1.55 + driver->load_frame_buffer = gdk_pixbuf_load_frame_buffer;
1.56 + driver->display_blank = gdk_pixbuf_display_blank;
1.57 + driver->read_render_buffer = gdk_pixbuf_read_render_buffer;
1.58 +
1.59 + osmesa_context = OSMesaCreateContextExt( OSMESA_RGBA, 32, 0, 0, 0 );
1.60 + OSMesaMakeCurrent( osmesa_context, NULL, GL_UNSIGNED_BYTE, 640, 480 );
1.61 + pvr2_setup_gl_context();
1.62 +}
1.63 +
1.64 +int video_gdk_find_free()
1.65 +{
1.66 + unsigned int i;
1.67 + for( i=0; i<pixbuf_max; i++ ) {
1.68 + if( pixbuf_array[i] == NULL ) {
1.69 + return i;
1.70 + }
1.71 + }
1.72 + if( i < MAX_PIXBUF ) {
1.73 + return pixbuf_max++;
1.74 + }
1.75 + return -1;
1.76 +}
1.77 +
1.78 +void video_gdk_shutdown()
1.79 +{
1.80 + unsigned int i;
1.81 + for( i=0; i<pixbuf_max; i++ ) {
1.82 + if( pixbuf_array[i] != NULL ) {
1.83 + g_free(pixbuf_array[i]);
1.84 + pixbuf_array[i] = NULL;
1.85 + }
1.86 + }
1.87 + pixbuf_max = 0;
1.88 + OSMesaDestroyContext( osmesa_context );
1.89 +}
1.90 +
1.91 +
1.92 +static render_buffer_t gdk_pixbuf_create_render_buffer( uint32_t width, uint32_t height )
1.93 +{
1.94 + render_buffer_t buf = g_malloc0(sizeof(struct render_buffer));
1.95 + gboolean alpha = FALSE;
1.96 + buf->width = width;
1.97 + buf->height = height;
1.98 + buf->buf_id = video_gdk_find_free();
1.99 + pixbuf_array[buf->buf_id] = g_malloc0( width * height * 4 );
1.100 + return buf;
1.101 +}
1.102 +
1.103 +static void gdk_pixbuf_destroy_render_buffer( render_buffer_t buffer )
1.104 +{
1.105 + g_free(pixbuf_array[buffer->buf_id] );
1.106 + pixbuf_array[buffer->buf_id] = NULL;
1.107 + if( buffer->buf_id == (pixbuf_max-1) ) {
1.108 + pixbuf_max--;
1.109 + }
1.110 +}
1.111 +
1.112 +static gboolean gdk_pixbuf_display_render_buffer( render_buffer_t buffer )
1.113 +{
1.114 + glFinish();
1.115 +
1.116 + void *pb = pixbuf_array[buffer->buf_id];
1.117 + GdkGC *gc = gtk_video_win->style->fg_gc[GTK_STATE_NORMAL];
1.118 + GdkColor black = {0,0,0,0};
1.119 +
1.120 + assert(gc);
1.121 +
1.122 + int x1=0,y1=0,x2=video_width,y2=video_height;
1.123 +
1.124 + int ah = video_width * 0.75;
1.125 +
1.126 +
1.127 + if( ah > video_height ) {
1.128 + int w = (video_height/0.75);
1.129 + x1 = (video_width - w)/2;
1.130 + x2 -= x1;
1.131 + gdk_gc_set_foreground( gc, &black );
1.132 + gdk_gc_set_background( gc, &black );
1.133 + gdk_draw_rectangle( gtk_video_win->window, gc, TRUE, 0, 0, x1, video_height );
1.134 + gdk_draw_rectangle( gtk_video_win->window, gc, TRUE, x2, 0, video_width, video_height );
1.135 + } else if( ah < video_height ) {
1.136 + y1 = (video_height - ah)/2;
1.137 + y2 -= y1;
1.138 + gdk_gc_set_foreground( gc, &black );
1.139 + gdk_gc_set_background( gc, &black );
1.140 + gdk_draw_rectangle( gtk_video_win->window, gc, TRUE, 0, 0, video_width, y1 );
1.141 + gdk_draw_rectangle( gtk_video_win->window, gc, TRUE, 0, y2, video_width, video_height );
1.142 + }
1.143 + int w = x2-x1;
1.144 + int h = y2-y1;
1.145 +
1.146 + if( w != buffer->width || h != buffer->height ) {
1.147 + gdk_draw_rgb_32_image( gtk_video_win->window, gc, x1, y1, buffer->width, buffer->height, GDK_RGB_DITHER_NONE,
1.148 + pb, buffer->width*4 );
1.149 + } else {
1.150 + gdk_draw_rgb_32_image( gtk_video_win->window, gc, x1, y1, buffer->width, buffer->height, GDK_RGB_DITHER_NONE,
1.151 + pb, buffer->width*4 );
1.152 + }
1.153 +}
1.154 +
1.155 +static gboolean gdk_pixbuf_display_blank( uint32_t colour )
1.156 +{
1.157 + GdkGC *gc = gtk_video_win->style->fg_gc[GTK_STATE_NORMAL];
1.158 + GdkColor col = { };
1.159 +
1.160 + gdk_gc_set_foreground( gc, &col );
1.161 + gdk_gc_set_background( gc, &col );
1.162 + gdk_draw_rectangle( gtk_video_win->window, gc, TRUE, 0, 0, video_width, video_height );
1.163 + return TRUE;
1.164 +}
1.165 +
1.166 +static gboolean gdk_pixbuf_set_render_target( render_buffer_t buffer )
1.167 +{
1.168 + glFinish();
1.169 + void *pb = pixbuf_array[buffer->buf_id];
1.170 + OSMesaMakeCurrent( osmesa_context, pb, GL_UNSIGNED_BYTE,
1.171 + buffer->width, buffer->height );
1.172 + //OSMesaPixelStore( OSMESA_Y_UP, 0 );
1.173 + glViewport( 0, 0, buffer->width, buffer->height );
1.174 + glDrawBuffer(GL_FRONT);
1.175 + return TRUE;
1.176 +}
1.177 +
1.178 +static void gdk_pixbuf_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
1.179 +{
1.180 + glFinish();
1.181 + void *pb = pixbuf_array[buffer->buf_id];
1.182 + OSMesaMakeCurrent( osmesa_context, pb, GL_UNSIGNED_BYTE,
1.183 + buffer->width, buffer->height );
1.184 + GLenum type = colour_formats[frame->colour_format].type;
1.185 + GLenum format = colour_formats[frame->colour_format].format;
1.186 + int bpp = colour_formats[frame->colour_format].bpp;
1.187 + int rowstride = (frame->rowstride / bpp) - frame->width;
1.188 +
1.189 + gl_reset_state();
1.190 + glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
1.191 + glRasterPos2f(0.375, frame->height-0.375);
1.192 + glPixelZoom( 1.0, 1.0 );
1.193 + glDrawPixels( frame->width, frame->height, format, type, frame->data );
1.194 + glFlush();
1.195 +}
1.196 +
1.197 +static gboolean gdk_pixbuf_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format )
1.198 +{
1.199 + glFinish();
1.200 + void *pb = pixbuf_array[buffer->buf_id];
1.201 + OSMesaMakeCurrent( osmesa_context, pb, GL_UNSIGNED_BYTE,
1.202 + buffer->width, buffer->height );
1.203 + glReadBuffer( GL_FRONT );
1.204 + return gl_read_render_buffer( target, buffer, rowstride, format );
1.205 +
1.206 +}
.