1 /*
2  * Copyright (C) 2022 Intel Corporation.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <assert.h>
12 
13 #include "gc.h"
14 
15 struct gfx_ctx *
gc_init(int width,int height,void * fbaddr)16 gc_init(int width, int height, void *fbaddr)
17 {
18 	struct gfx_ctx *gc;
19 	struct gfx_ctx_image *gc_image;
20 
21 	gc = calloc(1, sizeof(struct gfx_ctx));
22 	assert(gc != NULL);
23 	gc_image = calloc(1, sizeof(struct gfx_ctx_image));
24 	assert(gc_image != NULL);
25 
26 	gc_image->width = width;
27 	gc_image->height = height;
28 	if (fbaddr) {
29 		gc_image->data = fbaddr;
30 		gc->raw = 1;
31 	} else {
32 		gc_image->data = calloc(width * height, sizeof(uint32_t));
33 		gc->raw = 0;
34 	}
35 	gc->gc_image = gc_image;
36 
37 	return gc;
38 }
39 
40 void
gc_deinit(struct gfx_ctx * gc)41 gc_deinit(struct gfx_ctx *gc)
42 {
43 	if (!gc)
44 		return;
45 
46 	if (gc->gc_image) {
47 		if (!gc->raw && gc->gc_image->data) {
48 			free(gc->gc_image->data);
49 			gc->gc_image->data = NULL;
50 		}
51 		free(gc->gc_image);
52 		gc->gc_image = NULL;
53 	}
54 
55 	free(gc);
56 	gc = NULL;
57 }
58 
59 void
gc_set_fbaddr(struct gfx_ctx * gc,void * fbaddr)60 gc_set_fbaddr(struct gfx_ctx *gc, void *fbaddr)
61 {
62 	gc->raw = 1;
63 	if (gc->gc_image->data && gc->gc_image->data != fbaddr)
64 		free(gc->gc_image->data);
65 	gc->gc_image->data = fbaddr;
66 }
67 struct gfx_ctx_image *
gc_get_image(struct gfx_ctx * gc)68 gc_get_image(struct gfx_ctx *gc)
69 {
70 	if (gc == NULL)
71 		return NULL;
72 
73 	return gc->gc_image;
74 }
75 
76 void
gc_resize(struct gfx_ctx * gc,int width,int height)77 gc_resize(struct gfx_ctx *gc, int width, int height)
78 {
79 	struct gfx_ctx_image *gc_image;
80 
81 	gc_image = gc->gc_image;
82 	gc_image->width = width;
83 	gc_image->height = height;
84 	if (!gc->raw) {
85 		gc_image->data = realloc(gc_image->data,
86 				width * height * sizeof(uint32_t));
87 		if (gc_image->data != NULL)
88 			memset(gc_image->data, 0, width * height *
89 					sizeof(uint32_t));
90 	}
91 }
92