Path: blob/21.2-virgl/src/gallium/winsys/sw/gdi/gdi_sw_winsys.c
4573 views
/**************************************************************************1*2* Copyright 2009 VMware, Inc.3* All Rights Reserved.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*26**************************************************************************/2728/**29* @file30* GDI software rasterizer support.31*32* @author Jose Fonseca <[email protected]>33*/343536#include <windows.h>3738#include "pipe/p_format.h"39#include "pipe/p_context.h"40#include "util/u_inlines.h"41#include "util/format/u_format.h"42#include "util/u_math.h"43#include "util/u_memory.h"44#include "frontend/sw_winsys.h"45#include "gdi_sw_winsys.h"464748struct gdi_sw_displaytarget49{50enum pipe_format format;51unsigned width;52unsigned height;53unsigned stride;5455unsigned size;5657void *data;5859BITMAPINFO bmi;60};616263/** Cast wrapper */64static inline struct gdi_sw_displaytarget *65gdi_sw_displaytarget( struct sw_displaytarget *buf )66{67return (struct gdi_sw_displaytarget *)buf;68}697071static bool72gdi_sw_is_displaytarget_format_supported( struct sw_winsys *ws,73unsigned tex_usage,74enum pipe_format format )75{76switch(format) {77case PIPE_FORMAT_B8G8R8X8_UNORM:78case PIPE_FORMAT_B8G8R8A8_UNORM:79return true;8081/* TODO: Support other formats possible with BMPs, as described in82* http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx */8384default:85return false;86}87}888990static void *91gdi_sw_displaytarget_map(struct sw_winsys *ws,92struct sw_displaytarget *dt,93unsigned flags )94{95struct gdi_sw_displaytarget *gdt = gdi_sw_displaytarget(dt);9697return gdt->data;98}99100101static void102gdi_sw_displaytarget_unmap(struct sw_winsys *ws,103struct sw_displaytarget *dt )104{105106}107108109static void110gdi_sw_displaytarget_destroy(struct sw_winsys *winsys,111struct sw_displaytarget *dt)112{113struct gdi_sw_displaytarget *gdt = gdi_sw_displaytarget(dt);114115align_free(gdt->data);116FREE(gdt);117}118119120static struct sw_displaytarget *121gdi_sw_displaytarget_create(struct sw_winsys *winsys,122unsigned tex_usage,123enum pipe_format format,124unsigned width, unsigned height,125unsigned alignment,126const void *front_private,127unsigned *stride)128{129struct gdi_sw_displaytarget *gdt;130unsigned cpp;131unsigned bpp;132133gdt = CALLOC_STRUCT(gdi_sw_displaytarget);134if(!gdt)135goto no_gdt;136137gdt->format = format;138gdt->width = width;139gdt->height = height;140141bpp = util_format_get_blocksizebits(format);142cpp = util_format_get_blocksize(format);143144gdt->stride = align(width * cpp, alignment);145gdt->size = gdt->stride * height;146147gdt->data = align_malloc(gdt->size, alignment);148if(!gdt->data)149goto no_data;150151gdt->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);152gdt->bmi.bmiHeader.biWidth = gdt->stride / cpp;153gdt->bmi.bmiHeader.biHeight= -(long)height;154gdt->bmi.bmiHeader.biPlanes = 1;155gdt->bmi.bmiHeader.biBitCount = bpp;156gdt->bmi.bmiHeader.biCompression = BI_RGB;157gdt->bmi.bmiHeader.biSizeImage = 0;158gdt->bmi.bmiHeader.biXPelsPerMeter = 0;159gdt->bmi.bmiHeader.biYPelsPerMeter = 0;160gdt->bmi.bmiHeader.biClrUsed = 0;161gdt->bmi.bmiHeader.biClrImportant = 0;162163*stride = gdt->stride;164return (struct sw_displaytarget *)gdt;165166no_data:167FREE(gdt);168no_gdt:169return NULL;170}171172173static struct sw_displaytarget *174gdi_sw_displaytarget_from_handle(struct sw_winsys *winsys,175const struct pipe_resource *templet,176struct winsys_handle *whandle,177unsigned *stride)178{179assert(0);180return NULL;181}182183184static bool185gdi_sw_displaytarget_get_handle(struct sw_winsys *winsys,186struct sw_displaytarget *dt,187struct winsys_handle *whandle)188{189assert(0);190return false;191}192193194void195gdi_sw_display( struct sw_winsys *winsys,196struct sw_displaytarget *dt,197HDC hDC )198{199struct gdi_sw_displaytarget *gdt = gdi_sw_displaytarget(dt);200201StretchDIBits(hDC,2020, 0, gdt->width, gdt->height,2030, 0, gdt->width, gdt->height,204gdt->data, &gdt->bmi, 0, SRCCOPY);205}206207static void208gdi_sw_displaytarget_display(struct sw_winsys *winsys,209struct sw_displaytarget *dt,210void *context_private,211struct pipe_box *box)212{213/* nasty:214*/215HDC hDC = (HDC)context_private;216217gdi_sw_display(winsys, dt, hDC);218}219220221static void222gdi_sw_destroy(struct sw_winsys *winsys)223{224FREE(winsys);225}226227struct sw_winsys *228gdi_create_sw_winsys(void)229{230static struct sw_winsys *winsys;231232winsys = CALLOC_STRUCT(sw_winsys);233if(!winsys)234return NULL;235236winsys->destroy = gdi_sw_destroy;237winsys->is_displaytarget_format_supported = gdi_sw_is_displaytarget_format_supported;238winsys->displaytarget_create = gdi_sw_displaytarget_create;239winsys->displaytarget_from_handle = gdi_sw_displaytarget_from_handle;240winsys->displaytarget_get_handle = gdi_sw_displaytarget_get_handle;241winsys->displaytarget_map = gdi_sw_displaytarget_map;242winsys->displaytarget_unmap = gdi_sw_displaytarget_unmap;243winsys->displaytarget_display = gdi_sw_displaytarget_display;244winsys->displaytarget_destroy = gdi_sw_displaytarget_destroy;245246return winsys;247}248249250251