1336191Saraujo/*-
2336191Saraujo * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3336191Saraujo *
4336191Saraujo * Copyright (c) 2015 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
5336191Saraujo * All rights reserved.
6336191Saraujo *
7336191Saraujo * Redistribution and use in source and binary forms, with or without
8336191Saraujo * modification, are permitted provided that the following conditions
9336191Saraujo * are met:
10336191Saraujo * 1. Redistributions of source code must retain the above copyright
11336191Saraujo *    notice, this list of conditions and the following disclaimer.
12336191Saraujo * 2. Redistributions in binary form must reproduce the above copyright
13336191Saraujo *    notice, this list of conditions and the following disclaimer in the
14336191Saraujo *    documentation and/or other materials provided with the distribution.
15336191Saraujo *
16336191Saraujo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17336191Saraujo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18336191Saraujo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19336191Saraujo * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20336191Saraujo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21336191Saraujo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22336191Saraujo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23336191Saraujo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24336191Saraujo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25336191Saraujo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26336191Saraujo * SUCH DAMAGE.
27336191Saraujo */
28336191Saraujo
29300829Sgrehan#include <sys/cdefs.h>
30319139Spfg__FBSDID("$FreeBSD: stable/11/usr.sbin/bhyve/bhyvegc.c 336191 2018-07-11 07:22:05Z araujo $");
31300829Sgrehan
32300829Sgrehan#include <sys/types.h>
33300829Sgrehan
34300829Sgrehan#include <stdlib.h>
35300829Sgrehan#include <stdio.h>
36300829Sgrehan#include <string.h>
37300829Sgrehan
38300829Sgrehan#include "bhyvegc.h"
39300829Sgrehan
40300829Sgrehanstruct bhyvegc {
41300829Sgrehan	struct bhyvegc_image	*gc_image;
42300829Sgrehan	int raw;
43300829Sgrehan};
44300829Sgrehan
45300829Sgrehanstruct bhyvegc *
46300829Sgrehanbhyvegc_init(int width, int height, void *fbaddr)
47300829Sgrehan{
48300829Sgrehan	struct bhyvegc *gc;
49300829Sgrehan	struct bhyvegc_image *gc_image;
50300829Sgrehan
51300829Sgrehan	gc = calloc(1, sizeof (struct bhyvegc));
52300829Sgrehan
53300829Sgrehan	gc_image = calloc(1, sizeof(struct bhyvegc_image));
54300829Sgrehan	gc_image->width = width;
55300829Sgrehan	gc_image->height = height;
56300829Sgrehan	if (fbaddr) {
57300829Sgrehan		gc_image->data = fbaddr;
58300829Sgrehan		gc->raw = 1;
59300829Sgrehan	} else {
60300829Sgrehan		gc_image->data = calloc(width * height, sizeof (uint32_t));
61300829Sgrehan		gc->raw = 0;
62300829Sgrehan	}
63300829Sgrehan
64300829Sgrehan	gc->gc_image = gc_image;
65300829Sgrehan
66300829Sgrehan	return (gc);
67300829Sgrehan}
68300829Sgrehan
69300829Sgrehanvoid
70300829Sgrehanbhyvegc_set_fbaddr(struct bhyvegc *gc, void *fbaddr)
71300829Sgrehan{
72300829Sgrehan	gc->raw = 1;
73300829Sgrehan	if (gc->gc_image->data && gc->gc_image->data != fbaddr)
74300829Sgrehan		free(gc->gc_image->data);
75300829Sgrehan	gc->gc_image->data = fbaddr;
76300829Sgrehan}
77300829Sgrehan
78300829Sgrehanvoid
79300829Sgrehanbhyvegc_resize(struct bhyvegc *gc, int width, int height)
80300829Sgrehan{
81300829Sgrehan	struct bhyvegc_image *gc_image;
82300829Sgrehan
83300829Sgrehan	gc_image = gc->gc_image;
84300829Sgrehan
85300829Sgrehan	gc_image->width = width;
86300829Sgrehan	gc_image->height = height;
87300829Sgrehan	if (!gc->raw) {
88319139Spfg		gc_image->data = reallocarray(gc_image->data, width * height,
89319139Spfg		    sizeof (uint32_t));
90319139Spfg		if (gc_image->data != NULL)
91319139Spfg			memset(gc_image->data, 0, width * height *
92319139Spfg			    sizeof (uint32_t));
93300829Sgrehan	}
94300829Sgrehan}
95300829Sgrehan
96300829Sgrehanstruct bhyvegc_image *
97300829Sgrehanbhyvegc_get_image(struct bhyvegc *gc)
98300829Sgrehan{
99300829Sgrehan	if (gc == NULL)
100300829Sgrehan		return (NULL);
101300829Sgrehan
102300829Sgrehan	return (gc->gc_image);
103300829Sgrehan}
104