Path: blob/21.2-virgl/src/egl/drivers/haiku/egl_haiku.cpp
4570 views
/*1* Mesa 3-D graphics library2*3* Copyright (C) 2014 Adrián Arroyo Calle <[email protected]>4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included13* in all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*/2324#include <errno.h>25#include <dlfcn.h>26#include <stdint.h>27#include <stdio.h>2829#include "eglconfig.h"30#include "eglcontext.h"31#include "egldevice.h"32#include "egldisplay.h"33#include "egldriver.h"34#include "eglcurrent.h"35#include "egllog.h"36#include "eglsurface.h"37#include "eglimage.h"38#include "egltypedefs.h"3940#include <InterfaceKit.h>41#include <OpenGLKit.h>424344#ifdef DEBUG45# define TRACE(x...) printf("egl_haiku: " x)46# define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)47#else48# define TRACE(x...)49# define CALLED()50#endif51#define ERROR(x...) printf("egl_haiku: " x)525354_EGL_DRIVER_STANDARD_TYPECASTS(haiku_egl)555657struct haiku_egl_config58{59_EGLConfig base;60};6162struct haiku_egl_context63{64_EGLContext ctx;65};6667struct haiku_egl_surface68{69_EGLSurface surf;70BGLView* gl;71};727374/**75* Called via eglCreateWindowSurface(), drv->CreateWindowSurface().76*/77static _EGLSurface *78haiku_create_window_surface(_EGLDisplay *disp,79_EGLConfig *conf, void *native_window, const EGLint *attrib_list)80{81CALLED();8283struct haiku_egl_surface* surface;84surface = (struct haiku_egl_surface*) calloc(1, sizeof (*surface));85if (!surface) {86_eglError(EGL_BAD_ALLOC, "haiku_create_window_surface");87return NULL;88}8990if (!_eglInitSurface(&surface->surf, disp, EGL_WINDOW_BIT,91conf, attrib_list, native_window)) {92free(surface);93return NULL;94}9596(&surface->surf)->SwapInterval = 1;9798TRACE("Creating window\n");99BWindow* win = (BWindow*)native_window;100101TRACE("Creating GL view\n");102surface->gl = new BGLView(win->Bounds(), "OpenGL", B_FOLLOW_ALL_SIDES, 0,103BGL_RGB | BGL_DOUBLE | BGL_ALPHA);104105TRACE("Adding GL\n");106win->AddChild(surface->gl);107108TRACE("Showing window\n");109win->Show();110return &surface->surf;111}112113114static _EGLSurface *115haiku_create_pixmap_surface(_EGLDisplay *disp,116_EGLConfig *conf, void *native_pixmap, const EGLint *attrib_list)117{118return NULL;119}120121122static _EGLSurface *123haiku_create_pbuffer_surface(_EGLDisplay *disp,124_EGLConfig *conf, const EGLint *attrib_list)125{126return NULL;127}128129130static EGLBoolean131haiku_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)132{133if (_eglPutSurface(surf)) {134// XXX: detach haiku_egl_surface::gl from the native window and destroy it135free(surf);136}137return EGL_TRUE;138}139140141static EGLBoolean142haiku_add_configs_for_visuals(_EGLDisplay *disp)143{144CALLED();145146struct haiku_egl_config* conf;147conf = (struct haiku_egl_config*) calloc(1, sizeof (*conf));148if (!conf)149return _eglError(EGL_BAD_ALLOC, "haiku_add_configs_for_visuals");150151_eglInitConfig(&conf->base, disp, 1);152TRACE("Config inited\n");153154conf->base.RedSize = 8;155conf->base.BlueSize = 8;156conf->base.GreenSize = 8;157conf->base.LuminanceSize = 0;158conf->base.AlphaSize = 8;159conf->base.ColorBufferType = EGL_RGB_BUFFER;160conf->base.BufferSize = conf->base.RedSize161+ conf->base.GreenSize162+ conf->base.BlueSize163+ conf->base.AlphaSize;164conf->base.ConfigCaveat = EGL_NONE;165conf->base.ConfigID = 1;166conf->base.BindToTextureRGB = EGL_FALSE;167conf->base.BindToTextureRGBA = EGL_FALSE;168conf->base.StencilSize = 0;169conf->base.TransparentType = EGL_NONE;170conf->base.NativeRenderable = EGL_TRUE; // Let's say yes171conf->base.NativeVisualID = 0; // No visual172conf->base.NativeVisualType = EGL_NONE; // No visual173conf->base.RenderableType = 0x8;174conf->base.SampleBuffers = 0; // TODO: How to get the right value ?175conf->base.Samples = conf->base.SampleBuffers == 0 ? 0 : 0;176conf->base.DepthSize = 24; // TODO: How to get the right value ?177conf->base.Level = 0;178conf->base.MaxPbufferWidth = 0; // TODO: How to get the right value ?179conf->base.MaxPbufferHeight = 0; // TODO: How to get the right value ?180conf->base.MaxPbufferPixels = 0; // TODO: How to get the right value ?181conf->base.SurfaceType = EGL_WINDOW_BIT /*| EGL_PIXMAP_BIT | EGL_PBUFFER_BIT*/;182183TRACE("Config configuated\n");184if (!_eglValidateConfig(&conf->base, EGL_FALSE)) {185_eglLog(_EGL_DEBUG, "Haiku: failed to validate config");186goto cleanup;187}188TRACE("Validated config\n");189190_eglLinkConfig(&conf->base);191if (!_eglGetArraySize(disp->Configs)) {192_eglLog(_EGL_WARNING, "Haiku: failed to create any config");193goto cleanup;194}195TRACE("Config successfull\n");196197return EGL_TRUE;198199cleanup:200free(conf);201return EGL_FALSE;202}203204205extern "C"206EGLBoolean207init_haiku(_EGLDisplay *disp)208{209_EGLDevice *dev;210CALLED();211212dev = _eglAddDevice(-1, true);213if (!dev) {214_eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");215return EGL_FALSE;216}217disp->Device = dev;218219TRACE("Add configs\n");220if (!haiku_add_configs_for_visuals(disp))221return EGL_FALSE;222223TRACE("Initialization finished\n");224225return EGL_TRUE;226}227228229extern "C"230EGLBoolean231haiku_terminate(_EGLDisplay *disp)232{233return EGL_TRUE;234}235236237extern "C"238_EGLContext*239haiku_create_context(_EGLDisplay *disp, _EGLConfig *conf,240_EGLContext *share_list, const EGLint *attrib_list)241{242CALLED();243244struct haiku_egl_context* context;245context = (struct haiku_egl_context*) calloc(1, sizeof (*context));246if (!context) {247_eglError(EGL_BAD_ALLOC, "haiku_create_context");248return NULL;249}250251if (!_eglInitContext(&context->ctx, disp, conf, attrib_list))252goto cleanup;253254TRACE("Context created\n");255return &context->ctx;256257cleanup:258free(context);259return NULL;260}261262263extern "C"264EGLBoolean265haiku_destroy_context(_EGLDisplay *disp, _EGLContext* ctx)266{267struct haiku_egl_context* context = haiku_egl_context(ctx);268269if (_eglPutContext(ctx)) {270// XXX: teardown the context ?271free(context);272ctx = NULL;273}274return EGL_TRUE;275}276277278extern "C"279EGLBoolean280haiku_make_current(_EGLDisplay *disp, _EGLSurface *dsurf,281_EGLSurface *rsurf, _EGLContext *ctx)282{283CALLED();284285struct haiku_egl_context* cont = haiku_egl_context(ctx);286struct haiku_egl_surface* surf = haiku_egl_surface(dsurf);287_EGLContext *old_ctx;288_EGLSurface *old_dsurf, *old_rsurf;289290if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))291return EGL_FALSE;292293//cont->ctx.DrawSurface=&surf->surf;294surf->gl->LockGL();295return EGL_TRUE;296}297298299extern "C"300EGLBoolean301haiku_swap_buffers(_EGLDisplay *disp, _EGLSurface *surf)302{303struct haiku_egl_surface* surface = haiku_egl_surface(surf);304305surface->gl->SwapBuffers();306//gl->Render();307return EGL_TRUE;308}309310311extern "C"312const _EGLDriver _eglDriver = {313.Initialize = init_haiku,314.Terminate = haiku_terminate,315.CreateContext = haiku_create_context,316.DestroyContext = haiku_destroy_context,317.MakeCurrent = haiku_make_current,318.CreateWindowSurface = haiku_create_window_surface,319.CreatePixmapSurface = haiku_create_pixmap_surface,320.CreatePbufferSurface = haiku_create_pbuffer_surface,321.DestroySurface = haiku_destroy_surface,322.SwapBuffers = haiku_swap_buffers,323};324325326