1*5c4a5fe1SAndy Fiddaman /*-
2*5c4a5fe1SAndy Fiddaman * SPDX-License-Identifier: BSD-2-Clause
3*5c4a5fe1SAndy Fiddaman *
4*5c4a5fe1SAndy Fiddaman * Copyright (c) 2015 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
5*5c4a5fe1SAndy Fiddaman * All rights reserved.
6*5c4a5fe1SAndy Fiddaman *
7*5c4a5fe1SAndy Fiddaman * Redistribution and use in source and binary forms, with or without
8*5c4a5fe1SAndy Fiddaman * modification, are permitted provided that the following conditions
9*5c4a5fe1SAndy Fiddaman * are met:
10*5c4a5fe1SAndy Fiddaman * 1. Redistributions of source code must retain the above copyright
11*5c4a5fe1SAndy Fiddaman * notice, this list of conditions and the following disclaimer.
12*5c4a5fe1SAndy Fiddaman * 2. Redistributions in binary form must reproduce the above copyright
13*5c4a5fe1SAndy Fiddaman * notice, this list of conditions and the following disclaimer in the
14*5c4a5fe1SAndy Fiddaman * documentation and/or other materials provided with the distribution.
15*5c4a5fe1SAndy Fiddaman *
16*5c4a5fe1SAndy Fiddaman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17*5c4a5fe1SAndy Fiddaman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*5c4a5fe1SAndy Fiddaman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*5c4a5fe1SAndy Fiddaman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*5c4a5fe1SAndy Fiddaman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*5c4a5fe1SAndy Fiddaman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*5c4a5fe1SAndy Fiddaman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*5c4a5fe1SAndy Fiddaman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*5c4a5fe1SAndy Fiddaman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*5c4a5fe1SAndy Fiddaman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*5c4a5fe1SAndy Fiddaman * SUCH DAMAGE.
27*5c4a5fe1SAndy Fiddaman */
28*5c4a5fe1SAndy Fiddaman
29*5c4a5fe1SAndy Fiddaman
30*5c4a5fe1SAndy Fiddaman #include <sys/types.h>
31*5c4a5fe1SAndy Fiddaman
32*5c4a5fe1SAndy Fiddaman #include <stdlib.h>
33*5c4a5fe1SAndy Fiddaman #include <stdio.h>
34*5c4a5fe1SAndy Fiddaman #include <string.h>
35*5c4a5fe1SAndy Fiddaman
36*5c4a5fe1SAndy Fiddaman #ifndef __FreeBSD__
37*5c4a5fe1SAndy Fiddaman #include <pthread.h>
38*5c4a5fe1SAndy Fiddaman #endif
39*5c4a5fe1SAndy Fiddaman
40*5c4a5fe1SAndy Fiddaman #include "bhyvegc.h"
41*5c4a5fe1SAndy Fiddaman
42*5c4a5fe1SAndy Fiddaman struct bhyvegc {
43*5c4a5fe1SAndy Fiddaman struct bhyvegc_image *gc_image;
44*5c4a5fe1SAndy Fiddaman int raw;
45*5c4a5fe1SAndy Fiddaman };
46*5c4a5fe1SAndy Fiddaman
47*5c4a5fe1SAndy Fiddaman struct bhyvegc *
bhyvegc_init(int width,int height,void * fbaddr)48*5c4a5fe1SAndy Fiddaman bhyvegc_init(int width, int height, void *fbaddr)
49*5c4a5fe1SAndy Fiddaman {
50*5c4a5fe1SAndy Fiddaman struct bhyvegc *gc;
51*5c4a5fe1SAndy Fiddaman struct bhyvegc_image *gc_image;
52*5c4a5fe1SAndy Fiddaman
53*5c4a5fe1SAndy Fiddaman gc = calloc(1, sizeof (struct bhyvegc));
54*5c4a5fe1SAndy Fiddaman
55*5c4a5fe1SAndy Fiddaman gc_image = calloc(1, sizeof(struct bhyvegc_image));
56*5c4a5fe1SAndy Fiddaman gc_image->width = width;
57*5c4a5fe1SAndy Fiddaman gc_image->height = height;
58*5c4a5fe1SAndy Fiddaman if (fbaddr) {
59*5c4a5fe1SAndy Fiddaman gc_image->data = fbaddr;
60*5c4a5fe1SAndy Fiddaman gc->raw = 1;
61*5c4a5fe1SAndy Fiddaman } else {
62*5c4a5fe1SAndy Fiddaman gc_image->data = calloc(width * height, sizeof (uint32_t));
63*5c4a5fe1SAndy Fiddaman gc->raw = 0;
64*5c4a5fe1SAndy Fiddaman }
65*5c4a5fe1SAndy Fiddaman
66*5c4a5fe1SAndy Fiddaman gc->gc_image = gc_image;
67*5c4a5fe1SAndy Fiddaman
68*5c4a5fe1SAndy Fiddaman #ifndef __FreeBSD__
69*5c4a5fe1SAndy Fiddaman pthread_mutex_init(&gc_image->mtx, NULL);
70*5c4a5fe1SAndy Fiddaman #endif
71*5c4a5fe1SAndy Fiddaman
72*5c4a5fe1SAndy Fiddaman return (gc);
73*5c4a5fe1SAndy Fiddaman }
74*5c4a5fe1SAndy Fiddaman
75*5c4a5fe1SAndy Fiddaman void
bhyvegc_set_fbaddr(struct bhyvegc * gc,void * fbaddr)76*5c4a5fe1SAndy Fiddaman bhyvegc_set_fbaddr(struct bhyvegc *gc, void *fbaddr)
77*5c4a5fe1SAndy Fiddaman {
78*5c4a5fe1SAndy Fiddaman #ifndef __FreeBSD__
79*5c4a5fe1SAndy Fiddaman pthread_mutex_lock(&gc->gc_image->mtx);
80*5c4a5fe1SAndy Fiddaman #endif
81*5c4a5fe1SAndy Fiddaman gc->raw = 1;
82*5c4a5fe1SAndy Fiddaman if (gc->gc_image->data && gc->gc_image->data != fbaddr)
83*5c4a5fe1SAndy Fiddaman free(gc->gc_image->data);
84*5c4a5fe1SAndy Fiddaman gc->gc_image->data = fbaddr;
85*5c4a5fe1SAndy Fiddaman #ifndef __FreeBSD__
86*5c4a5fe1SAndy Fiddaman pthread_mutex_unlock(&gc->gc_image->mtx);
87*5c4a5fe1SAndy Fiddaman #endif
88*5c4a5fe1SAndy Fiddaman }
89*5c4a5fe1SAndy Fiddaman
90*5c4a5fe1SAndy Fiddaman void
bhyvegc_resize(struct bhyvegc * gc,int width,int height)91*5c4a5fe1SAndy Fiddaman bhyvegc_resize(struct bhyvegc *gc, int width, int height)
92*5c4a5fe1SAndy Fiddaman {
93*5c4a5fe1SAndy Fiddaman struct bhyvegc_image *gc_image;
94*5c4a5fe1SAndy Fiddaman
95*5c4a5fe1SAndy Fiddaman #ifndef __FreeBSD__
96*5c4a5fe1SAndy Fiddaman pthread_mutex_lock(&gc->gc_image->mtx);
97*5c4a5fe1SAndy Fiddaman #endif
98*5c4a5fe1SAndy Fiddaman gc_image = gc->gc_image;
99*5c4a5fe1SAndy Fiddaman
100*5c4a5fe1SAndy Fiddaman gc_image->width = width;
101*5c4a5fe1SAndy Fiddaman gc_image->height = height;
102*5c4a5fe1SAndy Fiddaman if (!gc->raw) {
103*5c4a5fe1SAndy Fiddaman gc_image->data = reallocarray(gc_image->data, width * height,
104*5c4a5fe1SAndy Fiddaman sizeof (uint32_t));
105*5c4a5fe1SAndy Fiddaman if (gc_image->data != NULL)
106*5c4a5fe1SAndy Fiddaman memset(gc_image->data, 0, width * height *
107*5c4a5fe1SAndy Fiddaman sizeof (uint32_t));
108*5c4a5fe1SAndy Fiddaman }
109*5c4a5fe1SAndy Fiddaman #ifndef __FreeBSD__
110*5c4a5fe1SAndy Fiddaman pthread_mutex_unlock(&gc->gc_image->mtx);
111*5c4a5fe1SAndy Fiddaman #endif
112*5c4a5fe1SAndy Fiddaman }
113*5c4a5fe1SAndy Fiddaman
114*5c4a5fe1SAndy Fiddaman struct bhyvegc_image *
bhyvegc_get_image(struct bhyvegc * gc)115*5c4a5fe1SAndy Fiddaman bhyvegc_get_image(struct bhyvegc *gc)
116*5c4a5fe1SAndy Fiddaman {
117*5c4a5fe1SAndy Fiddaman if (gc == NULL)
118*5c4a5fe1SAndy Fiddaman return (NULL);
119*5c4a5fe1SAndy Fiddaman
120*5c4a5fe1SAndy Fiddaman return (gc->gc_image);
121*5c4a5fe1SAndy Fiddaman }
122