xref: /freebsd/usr.sbin/bhyve/bhyvegc.c (revision 4d65a7c6)
135f7c93cSMarcelo Araujo /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
335f7c93cSMarcelo Araujo  *
435f7c93cSMarcelo Araujo  * Copyright (c) 2015 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
535f7c93cSMarcelo Araujo  * All rights reserved.
635f7c93cSMarcelo Araujo  *
735f7c93cSMarcelo Araujo  * Redistribution and use in source and binary forms, with or without
835f7c93cSMarcelo Araujo  * modification, are permitted provided that the following conditions
935f7c93cSMarcelo Araujo  * are met:
1035f7c93cSMarcelo Araujo  * 1. Redistributions of source code must retain the above copyright
1135f7c93cSMarcelo Araujo  *    notice, this list of conditions and the following disclaimer.
1235f7c93cSMarcelo Araujo  * 2. Redistributions in binary form must reproduce the above copyright
1335f7c93cSMarcelo Araujo  *    notice, this list of conditions and the following disclaimer in the
1435f7c93cSMarcelo Araujo  *    documentation and/or other materials provided with the distribution.
1535f7c93cSMarcelo Araujo  *
1635f7c93cSMarcelo Araujo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
1735f7c93cSMarcelo Araujo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1835f7c93cSMarcelo Araujo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1935f7c93cSMarcelo Araujo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2035f7c93cSMarcelo Araujo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2135f7c93cSMarcelo Araujo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2235f7c93cSMarcelo Araujo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2335f7c93cSMarcelo Araujo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2435f7c93cSMarcelo Araujo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2535f7c93cSMarcelo Araujo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2635f7c93cSMarcelo Araujo  * SUCH DAMAGE.
2735f7c93cSMarcelo Araujo  */
2835f7c93cSMarcelo Araujo 
292cf9911fSPeter Grehan #include <sys/types.h>
302cf9911fSPeter Grehan 
312cf9911fSPeter Grehan #include <stdlib.h>
322cf9911fSPeter Grehan #include <stdio.h>
332cf9911fSPeter Grehan #include <string.h>
342cf9911fSPeter Grehan 
352cf9911fSPeter Grehan #include "bhyvegc.h"
362cf9911fSPeter Grehan 
372cf9911fSPeter Grehan struct bhyvegc {
382cf9911fSPeter Grehan 	struct bhyvegc_image	*gc_image;
392cf9911fSPeter Grehan 	int raw;
402cf9911fSPeter Grehan };
412cf9911fSPeter Grehan 
422cf9911fSPeter Grehan struct bhyvegc *
bhyvegc_init(int width,int height,void * fbaddr)432cf9911fSPeter Grehan bhyvegc_init(int width, int height, void *fbaddr)
442cf9911fSPeter Grehan {
452cf9911fSPeter Grehan 	struct bhyvegc *gc;
462cf9911fSPeter Grehan 	struct bhyvegc_image *gc_image;
472cf9911fSPeter Grehan 
482cf9911fSPeter Grehan 	gc = calloc(1, sizeof (struct bhyvegc));
492cf9911fSPeter Grehan 
502cf9911fSPeter Grehan 	gc_image = calloc(1, sizeof(struct bhyvegc_image));
512cf9911fSPeter Grehan 	gc_image->width = width;
522cf9911fSPeter Grehan 	gc_image->height = height;
532cf9911fSPeter Grehan 	if (fbaddr) {
542cf9911fSPeter Grehan 		gc_image->data = fbaddr;
552cf9911fSPeter Grehan 		gc->raw = 1;
562cf9911fSPeter Grehan 	} else {
572cf9911fSPeter Grehan 		gc_image->data = calloc(width * height, sizeof (uint32_t));
582cf9911fSPeter Grehan 		gc->raw = 0;
592cf9911fSPeter Grehan 	}
602cf9911fSPeter Grehan 
612cf9911fSPeter Grehan 	gc->gc_image = gc_image;
622cf9911fSPeter Grehan 
632cf9911fSPeter Grehan 	return (gc);
642cf9911fSPeter Grehan }
652cf9911fSPeter Grehan 
662cf9911fSPeter Grehan void
bhyvegc_set_fbaddr(struct bhyvegc * gc,void * fbaddr)672cf9911fSPeter Grehan bhyvegc_set_fbaddr(struct bhyvegc *gc, void *fbaddr)
682cf9911fSPeter Grehan {
692cf9911fSPeter Grehan 	gc->raw = 1;
702cf9911fSPeter Grehan 	if (gc->gc_image->data && gc->gc_image->data != fbaddr)
712cf9911fSPeter Grehan 		free(gc->gc_image->data);
722cf9911fSPeter Grehan 	gc->gc_image->data = fbaddr;
732cf9911fSPeter Grehan }
742cf9911fSPeter Grehan 
752cf9911fSPeter Grehan void
bhyvegc_resize(struct bhyvegc * gc,int width,int height)762cf9911fSPeter Grehan bhyvegc_resize(struct bhyvegc *gc, int width, int height)
772cf9911fSPeter Grehan {
782cf9911fSPeter Grehan 	struct bhyvegc_image *gc_image;
792cf9911fSPeter Grehan 
802cf9911fSPeter Grehan 	gc_image = gc->gc_image;
812cf9911fSPeter Grehan 
822cf9911fSPeter Grehan 	gc_image->width = width;
832cf9911fSPeter Grehan 	gc_image->height = height;
842cf9911fSPeter Grehan 	if (!gc->raw) {
85880f26f3SPedro F. Giffuni 		gc_image->data = reallocarray(gc_image->data, width * height,
86880f26f3SPedro F. Giffuni 		    sizeof (uint32_t));
87880f26f3SPedro F. Giffuni 		if (gc_image->data != NULL)
88880f26f3SPedro F. Giffuni 			memset(gc_image->data, 0, width * height *
89880f26f3SPedro F. Giffuni 			    sizeof (uint32_t));
902cf9911fSPeter Grehan 	}
912cf9911fSPeter Grehan }
922cf9911fSPeter Grehan 
932cf9911fSPeter Grehan struct bhyvegc_image *
bhyvegc_get_image(struct bhyvegc * gc)942cf9911fSPeter Grehan bhyvegc_get_image(struct bhyvegc *gc)
952cf9911fSPeter Grehan {
962cf9911fSPeter Grehan 	if (gc == NULL)
972cf9911fSPeter Grehan 		return (NULL);
982cf9911fSPeter Grehan 
992cf9911fSPeter Grehan 	return (gc->gc_image);
1002cf9911fSPeter Grehan }
101