Search
lxdream.org :: lxdream/src/drivers/video_gdk.c
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
nkeynes@653
     1
/**
nkeynes@653
     2
 * $Id$
nkeynes@653
     3
 *
nkeynes@653
     4
 * The PC side of the video support (responsible for actually displaying / 
nkeynes@653
     5
 * rendering frames)
nkeynes@653
     6
 *
nkeynes@653
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@653
     8
 *
nkeynes@653
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@653
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@653
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@653
    12
 * (at your option) any later version.
nkeynes@653
    13
 *
nkeynes@653
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@653
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@653
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@653
    17
 * GNU General Public License for more details.
nkeynes@653
    18
 */
nkeynes@653
    19
#include <assert.h>
nkeynes@653
    20
#include <stdlib.h>
nkeynes@653
    21
#include <gdk/gdk.h>
nkeynes@653
    22
#include <gdk-pixbuf/gdk-pixbuf.h>
nkeynes@653
    23
#include <gtk/gtkwidget.h>
nkeynes@653
    24
#include <GL/osmesa.h>
nkeynes@653
    25
#include "lxdream.h"
nkeynes@653
    26
#include "display.h"
nkeynes@653
    27
nkeynes@653
    28
#define MAX_PIXBUF 16
nkeynes@653
    29
nkeynes@653
    30
extern GtkWidget *gtk_video_win;
nkeynes@653
    31
extern int video_width, video_height;
nkeynes@653
    32
nkeynes@653
    33
static render_buffer_t gdk_pixbuf_create_render_buffer( uint32_t width, uint32_t height );
nkeynes@653
    34
static void gdk_pixbuf_destroy_render_buffer( render_buffer_t buffer );
nkeynes@653
    35
static gboolean gdk_pixbuf_set_render_target( render_buffer_t buffer );
nkeynes@653
    36
static gboolean gdk_pixbuf_display_render_buffer( render_buffer_t buffer );
nkeynes@653
    37
static void gdk_pixbuf_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer );
nkeynes@653
    38
static gboolean gdk_pixbuf_display_blank( uint32_t colour );
nkeynes@653
    39
static gboolean gdk_pixbuf_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format );
nkeynes@653
    40
nkeynes@653
    41
static void *pixbuf_array[MAX_PIXBUF];
nkeynes@653
    42
unsigned int pixbuf_max = 0;
nkeynes@653
    43
OSMesaContext osmesa_context = NULL;
nkeynes@653
    44
nkeynes@653
    45
void video_gdk_init_driver( display_driver_t driver )
nkeynes@653
    46
{
nkeynes@653
    47
    pixbuf_max = 0;
nkeynes@653
    48
    driver->create_render_buffer = gdk_pixbuf_create_render_buffer;
nkeynes@653
    49
    driver->destroy_render_buffer = gdk_pixbuf_destroy_render_buffer;
nkeynes@653
    50
    driver->set_render_target = gdk_pixbuf_set_render_target;
nkeynes@653
    51
    driver->display_render_buffer = gdk_pixbuf_display_render_buffer;
nkeynes@653
    52
    driver->load_frame_buffer = gdk_pixbuf_load_frame_buffer;
nkeynes@653
    53
    driver->display_blank = gdk_pixbuf_display_blank;
nkeynes@653
    54
    driver->read_render_buffer = gdk_pixbuf_read_render_buffer;
nkeynes@653
    55
nkeynes@653
    56
    osmesa_context = OSMesaCreateContextExt( OSMESA_RGBA, 32, 0, 0, 0 );
nkeynes@653
    57
    OSMesaMakeCurrent( osmesa_context, NULL, GL_UNSIGNED_BYTE, 640, 480 );
nkeynes@653
    58
    pvr2_setup_gl_context();
nkeynes@653
    59
}
nkeynes@653
    60
nkeynes@653
    61
int video_gdk_find_free()
nkeynes@653
    62
{
nkeynes@653
    63
    unsigned int i;
nkeynes@653
    64
    for( i=0; i<pixbuf_max; i++ ) {
nkeynes@653
    65
	if( pixbuf_array[i] == NULL ) {
nkeynes@653
    66
	    return i;
nkeynes@653
    67
	}
nkeynes@653
    68
    }
nkeynes@653
    69
    if( i < MAX_PIXBUF ) {
nkeynes@653
    70
	return pixbuf_max++;
nkeynes@653
    71
    }
nkeynes@653
    72
    return -1;
nkeynes@653
    73
}
nkeynes@653
    74
nkeynes@653
    75
void video_gdk_shutdown()
nkeynes@653
    76
{
nkeynes@653
    77
    unsigned int i;
nkeynes@653
    78
    for( i=0; i<pixbuf_max; i++ ) {
nkeynes@653
    79
	if( pixbuf_array[i] != NULL ) {
nkeynes@653
    80
	    g_free(pixbuf_array[i]);
nkeynes@653
    81
	    pixbuf_array[i] = NULL;
nkeynes@653
    82
	}
nkeynes@653
    83
    }
nkeynes@653
    84
    pixbuf_max = 0;
nkeynes@653
    85
    OSMesaDestroyContext( osmesa_context );
nkeynes@653
    86
}
nkeynes@653
    87
nkeynes@653
    88
nkeynes@653
    89
static render_buffer_t gdk_pixbuf_create_render_buffer( uint32_t width, uint32_t height )
nkeynes@653
    90
{
nkeynes@653
    91
    render_buffer_t buf = g_malloc0(sizeof(struct render_buffer));
nkeynes@653
    92
    gboolean alpha = FALSE;
nkeynes@653
    93
    buf->width = width;
nkeynes@653
    94
    buf->height = height;
nkeynes@653
    95
    buf->buf_id = video_gdk_find_free();
nkeynes@653
    96
    pixbuf_array[buf->buf_id] = g_malloc0( width * height * 4 );
nkeynes@653
    97
    return buf;
nkeynes@653
    98
}
nkeynes@653
    99
nkeynes@653
   100
static void gdk_pixbuf_destroy_render_buffer( render_buffer_t buffer )
nkeynes@653
   101
{
nkeynes@653
   102
    g_free(pixbuf_array[buffer->buf_id] );
nkeynes@653
   103
    pixbuf_array[buffer->buf_id] = NULL;
nkeynes@653
   104
    if( buffer->buf_id == (pixbuf_max-1) ) {
nkeynes@653
   105
	pixbuf_max--;
nkeynes@653
   106
    }
nkeynes@653
   107
}
nkeynes@653
   108
nkeynes@653
   109
static gboolean gdk_pixbuf_display_render_buffer( render_buffer_t buffer )
nkeynes@653
   110
{
nkeynes@653
   111
    glFinish();
nkeynes@653
   112
nkeynes@653
   113
    void *pb = pixbuf_array[buffer->buf_id];
nkeynes@653
   114
    GdkGC *gc = gtk_video_win->style->fg_gc[GTK_STATE_NORMAL];
nkeynes@653
   115
    GdkColor black = {0,0,0,0};
nkeynes@653
   116
nkeynes@653
   117
    assert(gc);
nkeynes@653
   118
nkeynes@653
   119
    int x1=0,y1=0,x2=video_width,y2=video_height;
nkeynes@653
   120
nkeynes@653
   121
    int ah = video_width * 0.75;
nkeynes@653
   122
nkeynes@653
   123
nkeynes@653
   124
    if( ah > video_height ) {
nkeynes@653
   125
	int w = (video_height/0.75);
nkeynes@653
   126
	x1 = (video_width - w)/2;
nkeynes@653
   127
	x2 -= x1;
nkeynes@653
   128
	gdk_gc_set_foreground( gc, &black );
nkeynes@653
   129
	gdk_gc_set_background( gc, &black );
nkeynes@653
   130
	gdk_draw_rectangle( gtk_video_win->window, gc, TRUE, 0, 0, x1, video_height );
nkeynes@653
   131
	gdk_draw_rectangle( gtk_video_win->window, gc, TRUE, x2, 0, video_width, video_height );
nkeynes@653
   132
    } else if( ah < video_height ) {
nkeynes@653
   133
	y1 = (video_height - ah)/2;
nkeynes@653
   134
	y2 -= y1;
nkeynes@653
   135
	gdk_gc_set_foreground( gc, &black );
nkeynes@653
   136
	gdk_gc_set_background( gc, &black );
nkeynes@653
   137
	gdk_draw_rectangle( gtk_video_win->window, gc, TRUE, 0, 0, video_width, y1 );
nkeynes@653
   138
	gdk_draw_rectangle( gtk_video_win->window, gc, TRUE, 0, y2, video_width, video_height );
nkeynes@653
   139
    }
nkeynes@653
   140
    int w = x2-x1;
nkeynes@653
   141
    int h = y2-y1;
nkeynes@653
   142
    
nkeynes@653
   143
    if( w != buffer->width || h != buffer->height ) {
nkeynes@653
   144
	gdk_draw_rgb_32_image( gtk_video_win->window, gc, x1, y1, buffer->width, buffer->height, GDK_RGB_DITHER_NONE,
nkeynes@653
   145
			       pb, buffer->width*4 );
nkeynes@653
   146
    } else {
nkeynes@653
   147
	gdk_draw_rgb_32_image( gtk_video_win->window, gc, x1, y1, buffer->width, buffer->height, GDK_RGB_DITHER_NONE,
nkeynes@653
   148
			       pb, buffer->width*4 );
nkeynes@653
   149
    }
nkeynes@653
   150
}
nkeynes@653
   151
nkeynes@653
   152
static gboolean gdk_pixbuf_display_blank( uint32_t colour )
nkeynes@653
   153
{
nkeynes@653
   154
    GdkGC *gc = gtk_video_win->style->fg_gc[GTK_STATE_NORMAL];
nkeynes@653
   155
    GdkColor col = { };
nkeynes@653
   156
nkeynes@653
   157
    gdk_gc_set_foreground( gc, &col );
nkeynes@653
   158
    gdk_gc_set_background( gc, &col );
nkeynes@653
   159
    gdk_draw_rectangle( gtk_video_win->window, gc, TRUE, 0, 0, video_width, video_height );
nkeynes@653
   160
    return TRUE;
nkeynes@653
   161
}
nkeynes@653
   162
nkeynes@653
   163
static gboolean gdk_pixbuf_set_render_target( render_buffer_t buffer )
nkeynes@653
   164
{
nkeynes@653
   165
    glFinish();
nkeynes@653
   166
    void *pb = pixbuf_array[buffer->buf_id];
nkeynes@653
   167
    OSMesaMakeCurrent( osmesa_context, pb, GL_UNSIGNED_BYTE,
nkeynes@653
   168
		       buffer->width, buffer->height );
nkeynes@653
   169
    //OSMesaPixelStore( OSMESA_Y_UP, 0 );
nkeynes@653
   170
    glViewport( 0, 0, buffer->width, buffer->height );
nkeynes@653
   171
    glDrawBuffer(GL_FRONT);
nkeynes@653
   172
    return TRUE;
nkeynes@653
   173
}
nkeynes@653
   174
nkeynes@653
   175
static void gdk_pixbuf_load_frame_buffer( frame_buffer_t frame, render_buffer_t buffer )
nkeynes@653
   176
{
nkeynes@653
   177
    glFinish();
nkeynes@653
   178
    void *pb = pixbuf_array[buffer->buf_id];
nkeynes@653
   179
    OSMesaMakeCurrent( osmesa_context, pb, GL_UNSIGNED_BYTE,
nkeynes@653
   180
		       buffer->width, buffer->height );
nkeynes@653
   181
    GLenum type = colour_formats[frame->colour_format].type;
nkeynes@653
   182
    GLenum format = colour_formats[frame->colour_format].format;
nkeynes@653
   183
    int bpp = colour_formats[frame->colour_format].bpp;
nkeynes@653
   184
    int rowstride = (frame->rowstride / bpp) - frame->width;
nkeynes@653
   185
    
nkeynes@653
   186
    gl_reset_state();
nkeynes@653
   187
    glPixelStorei( GL_UNPACK_ROW_LENGTH, rowstride );
nkeynes@653
   188
    glRasterPos2f(0.375, frame->height-0.375);
nkeynes@653
   189
    glPixelZoom( 1.0, 1.0 );
nkeynes@653
   190
    glDrawPixels( frame->width, frame->height, format, type, frame->data );
nkeynes@653
   191
    glFlush();
nkeynes@653
   192
}
nkeynes@653
   193
nkeynes@653
   194
static gboolean gdk_pixbuf_read_render_buffer( unsigned char *target, render_buffer_t buffer, int rowstride, int format )
nkeynes@653
   195
{
nkeynes@653
   196
    glFinish();
nkeynes@653
   197
    void *pb = pixbuf_array[buffer->buf_id];
nkeynes@653
   198
    OSMesaMakeCurrent( osmesa_context, pb, GL_UNSIGNED_BYTE,
nkeynes@653
   199
		       buffer->width, buffer->height );
nkeynes@653
   200
    glReadBuffer( GL_FRONT );
nkeynes@653
   201
    return gl_read_render_buffer( target, buffer, rowstride, format );
nkeynes@653
   202
nkeynes@653
   203
}
.