1 /**************************************************************************
2 *
3 * Copyright 2009 Artur Wyszynski <[email protected]>
4 * Copyright 2013-2014 Alexander von Gluck IV <[email protected]>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28 #include <stdio.h>
29
30 #include "util/compiler.h"
31 #include "pipe/p_defines.h"
32 #include "util/format/u_formats.h"
33 #include "util/u_inlines.h"
34 #include "util/format/u_format.h"
35 #include "util/u_math.h"
36 #include "util/u_memory.h"
37 #include "frontend/api.h"
38 #include "frontend/sw_winsys.h"
39
40 #include "hgl_sw_winsys.h"
41
42 #include <Bitmap.h>
43 #include <OS.h>
44
45 #if MESA_DEBUG
46 # define TRACE(x...) printf("hgl:winsys: " x)
47 # define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
48 #else
49 # define TRACE(x...)
50 # define CALLED()
51 #endif
52 #define ERROR(x...) printf("hgl:winsys: " x)
53
54
55 struct haiku_displaytarget
56 {
57 enum pipe_format format;
58 color_space colorSpace;
59
60 unsigned width;
61 unsigned height;
62 unsigned stride;
63
64 unsigned size;
65
66 void* data;
67 BBitmap* bitmap;
68 };
69
70
71 // Cast
72 static inline struct haiku_displaytarget*
hgl_sw_displaytarget(struct sw_displaytarget * target)73 hgl_sw_displaytarget(struct sw_displaytarget* target)
74 {
75 return (struct haiku_displaytarget *)target;
76 }
77
78
79 static void
hgl_winsys_destroy(struct sw_winsys * winsys)80 hgl_winsys_destroy(struct sw_winsys* winsys)
81 {
82 FREE(winsys);
83 }
84
85
86 static bool
hgl_winsys_is_displaytarget_format_supported(struct sw_winsys * winsys,unsigned textureUsage,enum pipe_format format)87 hgl_winsys_is_displaytarget_format_supported(struct sw_winsys* winsys,
88 unsigned textureUsage, enum pipe_format format)
89 {
90 // TODO STUB
91 return true;
92 }
93
94 static color_space
hgl_winsys_convert_cs(enum pipe_format format)95 hgl_winsys_convert_cs(enum pipe_format format)
96 {
97 // TODO: B_RGB24, B_RGB16, B_RGB15?
98 switch(format) {
99 case PIPE_FORMAT_B5G6R5_UNORM:
100 return B_CMAP8;
101 case PIPE_FORMAT_A8B8G8R8_UNORM:
102 case PIPE_FORMAT_X8B8G8R8_UNORM:
103 default:
104 return B_RGB32;
105 }
106 }
107
108 static struct sw_displaytarget*
hgl_winsys_displaytarget_create(struct sw_winsys * winsys,unsigned textureUsage,enum pipe_format format,unsigned width,unsigned height,unsigned alignment,const void * front_private,unsigned * stride)109 hgl_winsys_displaytarget_create(struct sw_winsys* winsys,
110 unsigned textureUsage, enum pipe_format format, unsigned width,
111 unsigned height, unsigned alignment, const void *front_private,
112 unsigned* stride)
113 {
114 struct haiku_displaytarget* haikuDisplayTarget
115 = CALLOC_STRUCT(haiku_displaytarget);
116 assert(haikuDisplayTarget);
117
118 TRACE("%s: %d x %d\n", __func__, width, height);
119
120 haikuDisplayTarget->colorSpace = hgl_winsys_convert_cs(format);
121 haikuDisplayTarget->format = format;
122 haikuDisplayTarget->width = width;
123 haikuDisplayTarget->height = height;
124
125 size_t formatStride = util_format_get_stride(format, width);
126 unsigned blockSize = util_format_get_nblocksy(format, height);
127
128 haikuDisplayTarget->stride = align(formatStride, alignment);
129 haikuDisplayTarget->size = haikuDisplayTarget->stride * blockSize;
130
131 if (textureUsage & PIPE_BIND_DISPLAY_TARGET) {
132 haikuDisplayTarget->data = NULL;
133 haikuDisplayTarget->bitmap = new BBitmap(
134 BRect(0, 0, width - 1, height - 1),
135 0,
136 haikuDisplayTarget->colorSpace,
137 haikuDisplayTarget->stride);
138 } else {
139 haikuDisplayTarget->data
140 = align_malloc(haikuDisplayTarget->size, alignment);
141
142 haikuDisplayTarget->bitmap = NULL;
143 }
144
145 *stride = haikuDisplayTarget->stride;
146
147 // Cast to ghost sw_displaytarget type
148 return (struct sw_displaytarget*)haikuDisplayTarget;
149 }
150
151
152 static void
hgl_winsys_displaytarget_destroy(struct sw_winsys * winsys,struct sw_displaytarget * displayTarget)153 hgl_winsys_displaytarget_destroy(struct sw_winsys* winsys,
154 struct sw_displaytarget* displayTarget)
155 {
156 struct haiku_displaytarget* haikuDisplayTarget
157 = hgl_sw_displaytarget(displayTarget);
158
159 if (!haikuDisplayTarget)
160 return;
161
162 if (haikuDisplayTarget->data != NULL)
163 align_free(haikuDisplayTarget->data);
164
165 if (haikuDisplayTarget->bitmap != NULL)
166 delete haikuDisplayTarget->bitmap;
167
168 FREE(haikuDisplayTarget);
169 }
170
171
172 static struct sw_displaytarget*
hgl_winsys_displaytarget_from_handle(struct sw_winsys * winsys,const struct pipe_resource * templat,struct winsys_handle * whandle,unsigned * stride)173 hgl_winsys_displaytarget_from_handle(struct sw_winsys* winsys,
174 const struct pipe_resource* templat, struct winsys_handle* whandle,
175 unsigned* stride)
176 {
177 return NULL;
178 }
179
180
181 static bool
hgl_winsys_displaytarget_get_handle(struct sw_winsys * winsys,struct sw_displaytarget * displayTarget,struct winsys_handle * whandle)182 hgl_winsys_displaytarget_get_handle(struct sw_winsys* winsys,
183 struct sw_displaytarget* displayTarget, struct winsys_handle* whandle)
184 {
185 return false;
186 }
187
188
189 static void*
hgl_winsys_displaytarget_map(struct sw_winsys * winsys,struct sw_displaytarget * displayTarget,unsigned flags)190 hgl_winsys_displaytarget_map(struct sw_winsys* winsys,
191 struct sw_displaytarget* displayTarget, unsigned flags)
192 {
193 struct haiku_displaytarget* haikuDisplayTarget
194 = hgl_sw_displaytarget(displayTarget);
195
196 if (haikuDisplayTarget->bitmap != NULL)
197 return haikuDisplayTarget->bitmap->Bits();
198
199 return haikuDisplayTarget->data;
200 }
201
202
203 static void
hgl_winsys_displaytarget_unmap(struct sw_winsys * winsys,struct sw_displaytarget * displayTarget)204 hgl_winsys_displaytarget_unmap(struct sw_winsys* winsys,
205 struct sw_displaytarget* displayTarget)
206 {
207 return;
208 }
209
210
211 static void
hgl_winsys_displaytarget_display(struct sw_winsys * winsys,struct sw_displaytarget * displayTarget,void * contextPrivate,unsigned nboxes,struct pipe_box * box)212 hgl_winsys_displaytarget_display(struct sw_winsys* winsys,
213 struct sw_displaytarget* displayTarget, void* contextPrivate,
214 unsigned nboxes,
215 struct pipe_box *box)
216 {
217 assert(contextPrivate);
218
219 struct haiku_displaytarget* haikuDisplayTarget
220 = hgl_sw_displaytarget(displayTarget);
221
222 BitmapHook *context = (BitmapHook*)contextPrivate;
223 context->SetBitmap(haikuDisplayTarget->bitmap);
224 }
225
226
227 struct sw_winsys*
hgl_create_sw_winsys()228 hgl_create_sw_winsys()
229 {
230 struct sw_winsys* winsys = CALLOC_STRUCT(sw_winsys);
231
232 if (!winsys)
233 return NULL;
234
235 // Attach winsys hooks for Haiku
236 winsys->destroy = hgl_winsys_destroy;
237 winsys->is_displaytarget_format_supported
238 = hgl_winsys_is_displaytarget_format_supported;
239 winsys->displaytarget_create = hgl_winsys_displaytarget_create;
240 winsys->displaytarget_from_handle = hgl_winsys_displaytarget_from_handle;
241 winsys->displaytarget_get_handle = hgl_winsys_displaytarget_get_handle;
242 winsys->displaytarget_map = hgl_winsys_displaytarget_map;
243 winsys->displaytarget_unmap = hgl_winsys_displaytarget_unmap;
244 winsys->displaytarget_display = hgl_winsys_displaytarget_display;
245 winsys->displaytarget_destroy = hgl_winsys_displaytarget_destroy;
246
247 return winsys;
248 }
249