Path: blob/21.2-virgl/src/gallium/winsys/sw/hgl/hgl_sw_winsys.cpp
4573 views
/**************************************************************************1*2* Copyright 2009 Artur Wyszynski <[email protected]>3* Copyright 2013-2014 Alexander von Gluck IV <[email protected]>4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,17* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19* USE OR OTHER DEALINGS IN THE SOFTWARE.20*21* The above copyright notice and this permission notice (including the22* next paragraph) shall be included in all copies or substantial portions23* of the Software.24*25**************************************************************************/2627#include <stdio.h>2829#include "pipe/p_compiler.h"30#include "pipe/p_defines.h"31#include "pipe/p_format.h"32#include "util/u_inlines.h"33#include "util/format/u_format.h"34#include "util/u_math.h"35#include "util/u_memory.h"36#include "frontend/api.h"37#include "frontend/sw_winsys.h"3839#include "hgl_sw_winsys.h"4041#include <Bitmap.h>42#include <OS.h>4344#ifdef DEBUG45# define TRACE(x...) printf("hgl:winsys: " x)46# define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)47#else48# define TRACE(x...)49# define CALLED()50#endif51#define ERROR(x...) printf("hgl:winsys: " x)525354struct haiku_displaytarget55{56enum pipe_format format;57color_space colorSpace;5859unsigned width;60unsigned height;61unsigned stride;6263unsigned size;6465void* data;66BBitmap* bitmap;67};686970// Cast71static inline struct haiku_displaytarget*72hgl_sw_displaytarget(struct sw_displaytarget* target)73{74return (struct haiku_displaytarget *)target;75}767778static void79hgl_winsys_destroy(struct sw_winsys* winsys)80{81FREE(winsys);82}838485static bool86hgl_winsys_is_displaytarget_format_supported(struct sw_winsys* winsys,87unsigned textureUsage, enum pipe_format format)88{89// TODO STUB90return true;91}9293static color_space94hgl_winsys_convert_cs(enum pipe_format format)95{96// TODO: B_RGB24, B_RGB16, B_RGB15?97switch(format) {98case PIPE_FORMAT_B5G6R5_UNORM:99return B_CMAP8;100case PIPE_FORMAT_A8B8G8R8_UNORM:101case PIPE_FORMAT_X8B8G8R8_UNORM:102default:103return B_RGB32;104}105}106107static struct sw_displaytarget*108hgl_winsys_displaytarget_create(struct sw_winsys* winsys,109unsigned textureUsage, enum pipe_format format, unsigned width,110unsigned height, unsigned alignment, const void *front_private,111unsigned* stride)112{113struct haiku_displaytarget* haikuDisplayTarget114= CALLOC_STRUCT(haiku_displaytarget);115assert(haikuDisplayTarget);116117TRACE("%s: %d x %d\n", __func__, width, height);118119haikuDisplayTarget->colorSpace = hgl_winsys_convert_cs(format);120haikuDisplayTarget->format = format;121haikuDisplayTarget->width = width;122haikuDisplayTarget->height = height;123124size_t formatStride = util_format_get_stride(format, width);125unsigned blockSize = util_format_get_nblocksy(format, height);126127haikuDisplayTarget->stride = align(formatStride, alignment);128haikuDisplayTarget->size = haikuDisplayTarget->stride * blockSize;129130if (textureUsage & PIPE_BIND_DISPLAY_TARGET) {131haikuDisplayTarget->data = NULL;132haikuDisplayTarget->bitmap = new BBitmap(133BRect(0, 0, width - 1, height - 1),134haikuDisplayTarget->colorSpace,135haikuDisplayTarget->stride);136} else {137haikuDisplayTarget->data138= align_malloc(haikuDisplayTarget->size, alignment);139140haikuDisplayTarget->bitmap = NULL;141}142143*stride = haikuDisplayTarget->stride;144145// Cast to ghost sw_displaytarget type146return (struct sw_displaytarget*)haikuDisplayTarget;147}148149150static void151hgl_winsys_displaytarget_destroy(struct sw_winsys* winsys,152struct sw_displaytarget* displayTarget)153{154struct haiku_displaytarget* haikuDisplayTarget155= hgl_sw_displaytarget(displayTarget);156157if (!haikuDisplayTarget)158return;159160if (haikuDisplayTarget->data != NULL)161align_free(haikuDisplayTarget->data);162163if (haikuDisplayTarget->bitmap != NULL)164delete haikuDisplayTarget->bitmap;165166FREE(haikuDisplayTarget);167}168169170static struct sw_displaytarget*171hgl_winsys_displaytarget_from_handle(struct sw_winsys* winsys,172const struct pipe_resource* templat, struct winsys_handle* whandle,173unsigned* stride)174{175return NULL;176}177178179static bool180hgl_winsys_displaytarget_get_handle(struct sw_winsys* winsys,181struct sw_displaytarget* displayTarget, struct winsys_handle* whandle)182{183return false;184}185186187static void*188hgl_winsys_displaytarget_map(struct sw_winsys* winsys,189struct sw_displaytarget* displayTarget, unsigned flags)190{191struct haiku_displaytarget* haikuDisplayTarget192= hgl_sw_displaytarget(displayTarget);193194if (haikuDisplayTarget->bitmap != NULL)195return haikuDisplayTarget->bitmap->Bits();196197return haikuDisplayTarget->data;198}199200201static void202hgl_winsys_displaytarget_unmap(struct sw_winsys* winsys,203struct sw_displaytarget* displayTarget)204{205return;206}207208209static void210hgl_winsys_displaytarget_display(struct sw_winsys* winsys,211struct sw_displaytarget* displayTarget, void* contextPrivate,212struct pipe_box *box)213{214assert(contextPrivate);215216struct haiku_displaytarget* haikuDisplayTarget217= hgl_sw_displaytarget(displayTarget);218219HGLWinsysContext *context = (HGLWinsysContext*)contextPrivate;220context->Display(haikuDisplayTarget->bitmap, NULL);221}222223224struct sw_winsys*225hgl_create_sw_winsys()226{227struct sw_winsys* winsys = CALLOC_STRUCT(sw_winsys);228229if (!winsys)230return NULL;231232// Attach winsys hooks for Haiku233winsys->destroy = hgl_winsys_destroy;234winsys->is_displaytarget_format_supported235= hgl_winsys_is_displaytarget_format_supported;236winsys->displaytarget_create = hgl_winsys_displaytarget_create;237winsys->displaytarget_from_handle = hgl_winsys_displaytarget_from_handle;238winsys->displaytarget_get_handle = hgl_winsys_displaytarget_get_handle;239winsys->displaytarget_map = hgl_winsys_displaytarget_map;240winsys->displaytarget_unmap = hgl_winsys_displaytarget_unmap;241winsys->displaytarget_display = hgl_winsys_displaytarget_display;242winsys->displaytarget_destroy = hgl_winsys_displaytarget_destroy;243244return winsys;245}246247248