📄 idirectfbgl_mesa.c
字号:
/* * Copyright (C) 2004-2007 Claudio Ciccani <klan@directfb.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * * Based on glfbdev.c, written by Brian Paul. * */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#include <directfb.h>#include <directfb_version.h>#include <directfbgl.h>#include <direct/mem.h>#include <direct/messages.h>#include <direct/interface.h>#undef CLAMP#include "glheader.h"#include "buffers.h"#include "context.h"#include "extensions.h"#include "framebuffer.h"#include "renderbuffer.h"#include "imports.h"#include "texformat.h"#include "teximage.h"#include "texstore.h"#include "vbo/vbo.h"#include "swrast/swrast.h"#include "swrast_setup/swrast_setup.h"#include "tnl/tnl.h"#include "tnl/t_context.h"#include "tnl/t_pipeline.h"#include "drivers/common/driverfuncs.h"#define VERSION_CODE( M, m, r ) (((M) * 1000) + ((m) * 100) + ((r)))#define DIRECTFB_VERSION_CODE VERSION_CODE( DIRECTFB_MAJOR_VERSION, \ DIRECTFB_MINOR_VERSION, \ DIRECTFB_MICRO_VERSION )static DFBResultProbe( void *data );static DFBResultConstruct( IDirectFBGL *thiz, IDirectFBSurface *surface );#include <direct/interface_implementation.h>DIRECT_INTERFACE_IMPLEMENTATION( IDirectFBGL, Mesa )/* * private data struct of IDirectFBGL */typedef struct { int ref; /* reference counter */ int locked; IDirectFBSurface *surface; DFBSurfacePixelFormat format; int width; int height; struct { GLubyte *start; GLubyte *end; int pitch; } video; GLvisual visual; GLframebuffer framebuffer; GLcontext context; struct gl_renderbuffer render;} IDirectFBGL_data;/******************************************************************************/static pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER;static unsigned int global_ref = 0;static INLINE int directfbgl_init( void ){ pthread_mutexattr_t attr; int ret; if (global_ref++) return 0; pthread_mutexattr_init( &attr ); pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK ); ret = pthread_mutex_init( &global_lock, &attr ); pthread_mutexattr_destroy( &attr ); return ret;}static INLINE void directfbgl_finish( void ){ if (--global_ref == 0) pthread_mutex_destroy( &global_lock );}#define directfbgl_lock() pthread_mutex_lock( &global_lock )#define directfbgl_unlock() pthread_mutex_unlock( &global_lock )/******************************************************************************/static bool directfbgl_init_visual ( GLvisual *visual, DFBSurfacePixelFormat format );static bool directfbgl_create_context ( GLcontext *context, GLframebuffer *framebuffer, GLvisual *visual, IDirectFBGL_data *data );static void directfbgl_destroy_context( GLcontext *context, GLframebuffer *framebuffer );/******************************************************************************/static voidIDirectFBGL_Mesa_Destruct( IDirectFBGL *thiz ){ IDirectFBGL_data *data = (IDirectFBGL_data*) thiz->priv; directfbgl_destroy_context( &data->context, &data->framebuffer ); if (data->surface) data->surface->Release( data->surface ); DIRECT_DEALLOCATE_INTERFACE( thiz ); directfbgl_finish();}static DFBResultIDirectFBGL_Mesa_AddRef( IDirectFBGL *thiz ){ DIRECT_INTERFACE_GET_DATA( IDirectFBGL ); data->ref++; return DFB_OK;}static DFBResultIDirectFBGL_Mesa_Release( IDirectFBGL *thiz ){ DIRECT_INTERFACE_GET_DATA( IDirectFBGL ) if (--data->ref == 0) IDirectFBGL_Mesa_Destruct( thiz ); return DFB_OK;}static DFBResultIDirectFBGL_Mesa_Lock( IDirectFBGL *thiz ){ IDirectFBSurface *surface; int width = 0; int height = 0; DFBResult ret; DIRECT_INTERFACE_GET_DATA( IDirectFBGL ); if (data->locked) { data->locked++; return DFB_OK; } if (directfbgl_lock()) return DFB_LOCKED; surface = data->surface; surface->GetSize( surface, &width, &height ); ret = surface->Lock( surface, DSLF_READ | DSLF_WRITE, (void*)&data->video.start, &data->video.pitch ); if (ret) { D_ERROR( "DirectFBGL/Mesa: couldn't lock surface.\n" ); directfbgl_unlock(); return ret; } data->video.end = data->video.start + (height-1) * data->video.pitch; data->render.Data = data->video.start; _mesa_make_current( &data->context, &data->framebuffer, &data->framebuffer ); if (data->width != width || data->height != height) { _mesa_resize_framebuffer( &data->context, &data->framebuffer, width, height ); data->width = width; data->height = height; } data->locked++; return DFB_OK;}static DFBResultIDirectFBGL_Mesa_Unlock( IDirectFBGL *thiz ){ DIRECT_INTERFACE_GET_DATA( IDirectFBGL ); if (!data->locked) return DFB_OK; if (--data->locked == 0) { _mesa_make_current( NULL, NULL, NULL ); data->surface->Unlock( data->surface ); directfbgl_unlock(); } return DFB_OK;}static DFBResultIDirectFBGL_Mesa_GetAttributes( IDirectFBGL *thiz, DFBGLAttributes *attributes ){ DFBSurfaceCapabilities caps; GLvisual *visual; DIRECT_INTERFACE_GET_DATA( IDirectFBGL ); if (!attributes) return DFB_INVARG; data->surface->GetCapabilities( data->surface, &caps ); visual = &data->visual; attributes->buffer_size = visual->rgbBits ? : visual->indexBits; attributes->depth_size = visual->depthBits; attributes->stencil_size = visual->stencilBits; attributes->aux_buffers = visual->numAuxBuffers; attributes->red_size = visual->redBits; attributes->green_size = visual->greenBits; attributes->blue_size = visual->blueBits; attributes->alpha_size = visual->alphaBits; attributes->accum_red_size = visual->accumRedBits; attributes->accum_green_size = visual->accumGreenBits; attributes->accum_blue_size = visual->accumBlueBits; attributes->accum_alpha_size = visual->accumAlphaBits; attributes->double_buffer = ((caps & DSCAPS_FLIPPING) != 0); attributes->stereo = (visual->stereoMode != 0); return DFB_OK;}#if DIRECTFBGL_INTERFACE_VERSION >= 1static DFBResultIDirectFBGL_Mesa_GetProcAddress( IDirectFBGL *thiz, const char *name, void **ret_address ){ DIRECT_INTERFACE_GET_DATA( IDirectFBGL ); if (!name) return DFB_INVARG; if (!ret_address) return DFB_INVARG; *ret_address = _glapi_get_proc_address( name ); return (*ret_address) ? DFB_OK : DFB_UNSUPPORTED;}#endif/* exported symbols */static DFBResultProbe( void *data ){ return DFB_OK;}static DFBResultConstruct( IDirectFBGL *thiz, IDirectFBSurface *surface ){ DFBResult ret; /* Initialize global resources. */ if (directfbgl_init()) return DFB_INIT; /* Allocate interface data. */ DIRECT_ALLOCATE_INTERFACE_DATA( thiz, IDirectFBGL ); /* Initialize interface data. */ data->ref = 1; /* Duplicate destination surface. */ ret = surface->GetSubSurface( surface, NULL, &data->surface ); if (ret) { IDirectFBGL_Mesa_Destruct( thiz ); return ret;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -