5df5234bcbc939c287015f99352f2adaef8e58e5
[mesa.git] / src / gallium / targets / haiku-softpipe / GalliumContext.cpp
1 /*
2 * Copyright 2012, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * Artur Wyszynski, harakash@gmail.com
7 * Alexander von Gluck IV, kallisti5@unixzen.com
8 */
9
10
11 #include "GalliumContext.h"
12
13 #include "GLView.h"
14
15 #include "bitmap_wrapper.h"
16 extern "C" {
17 #include "glapi/glapi.h"
18 #include "main/context.h"
19 #include "main/framebuffer.h"
20 #include "main/renderbuffer.h"
21 #include "main/viewport.h"
22 #include "pipe/p_format.h"
23 #include "state_tracker/st_cb_fbo.h"
24 #include "state_tracker/st_cb_flush.h"
25 #include "state_tracker/st_context.h"
26 #include "state_tracker/st_gl_api.h"
27 #include "state_tracker/st_manager.h"
28 #include "state_tracker/sw_winsys.h"
29 #include "sw/hgl/hgl_sw_winsys.h"
30 #include "util/u_memory.h"
31
32 #include "target-helpers/inline_sw_helper.h"
33 #include "target-helpers/inline_debug_helper.h"
34 }
35
36
37 #ifdef DEBUG
38 # define TRACE(x...) printf("GalliumContext: " x)
39 # define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
40 #else
41 # define TRACE(x...)
42 # define CALLED()
43 #endif
44 #define ERROR(x...) printf("GalliumContext: " x)
45
46
47 static void
48 hgl_viewport(struct gl_context* glContext)
49 {
50 // TODO: We should try to eliminate this function
51
52 GLint x = glContext->ViewportArray[0].X;
53 GLint y = glContext->ViewportArray[0].Y;
54 GLint width = glContext->ViewportArray[0].Width;
55 GLint height = glContext->ViewportArray[0].Height;
56
57 TRACE("%s(glContext: %p, x: %d, y: %d, w: %d, h: %d\n", __func__,
58 glContext, x, y, width, height);
59
60 _mesa_check_init_viewport(glContext, width, height);
61
62 struct gl_framebuffer *draw = glContext->WinSysDrawBuffer;
63 struct gl_framebuffer *read = glContext->WinSysReadBuffer;
64
65 if (draw)
66 _mesa_resize_framebuffer(glContext, draw, width, height);
67 if (read)
68 _mesa_resize_framebuffer(glContext, read, width, height);
69 }
70
71
72 GalliumContext::GalliumContext(ulong options)
73 :
74 fOptions(options),
75 fScreen(NULL),
76 fCurrentContext(0)
77 {
78 CALLED();
79
80 // Make all contexts a known value
81 for (context_id i = 0; i < CONTEXT_MAX; i++)
82 fContext[i] = NULL;
83
84 CreateScreen();
85
86 pipe_mutex_init(fMutex);
87 }
88
89
90 GalliumContext::~GalliumContext()
91 {
92 CALLED();
93
94 // Destroy our contexts
95 Lock();
96 for (context_id i = 0; i < CONTEXT_MAX; i++)
97 DestroyContext(i);
98 Unlock();
99
100 pipe_mutex_destroy(fMutex);
101
102 // TODO: Destroy fScreen
103 }
104
105
106 status_t
107 GalliumContext::CreateScreen()
108 {
109 CALLED();
110
111 // Allocate winsys and attach callback hooks
112 struct sw_winsys* winsys = hgl_create_sw_winsys();
113
114 if (!winsys) {
115 ERROR("%s: Couldn't allocate sw_winsys!\n", __func__);
116 return B_ERROR;
117 }
118
119 fScreen = sw_screen_create(winsys);
120
121 if (fScreen == NULL) {
122 ERROR("%s: Couldn't create screen!\n", __FUNCTION__);
123 FREE(winsys);
124 return B_ERROR;
125 }
126
127 debug_screen_wrap(fScreen);
128
129 const char* driverName = fScreen->get_name(fScreen);
130 ERROR("%s: Using %s driver.\n", __func__, driverName);
131
132 return B_OK;
133 }
134
135
136 context_id
137 GalliumContext::CreateContext(Bitmap *bitmap)
138 {
139 CALLED();
140
141 struct hgl_context* context = CALLOC_STRUCT(hgl_context);
142
143 if (!context) {
144 ERROR("%s: Couldn't create pipe context!\n", __FUNCTION__);
145 return 0;
146 }
147
148 // Set up the initial things our context needs
149 context->bitmap = bitmap;
150 context->colorSpace = get_bitmap_color_space(bitmap);
151 context->draw = NULL;
152 context->read = NULL;
153 context->st = NULL;
154
155 context->api = st_gl_api_create();
156 if (!context->api) {
157 ERROR("%s: Couldn't obtain Mesa state tracker API!\n", __func__);
158 return -1;
159 }
160
161 // Create state_tracker manager
162 context->manager = hgl_create_st_manager(fScreen);
163
164 // Create state tracker visual
165 context->stVisual = hgl_create_st_visual(fOptions);
166
167 // Create state tracker framebuffers
168 context->draw = hgl_create_st_framebuffer(context);
169 context->read = hgl_create_st_framebuffer(context);
170
171 if (!context->draw || !context->read) {
172 ERROR("%s: Problem allocating framebuffer!\n", __func__);
173 FREE(context->stVisual);
174 return -1;
175 }
176
177 // Build state tracker attributes
178 struct st_context_attribs attribs;
179 memset(&attribs, 0, sizeof(attribs));
180 attribs.options.force_glsl_extensions_warn = false;
181 attribs.profile = ST_PROFILE_DEFAULT;
182 attribs.visual = *context->stVisual;
183 attribs.major = 1;
184 attribs.minor = 0;
185 //attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
186
187 struct st_api* api = context->api;
188
189 // Create context using state tracker api call
190 enum st_context_error result;
191 context->st = api->create_context(api, context->manager, &attribs,
192 &result, context->st);
193
194 if (!context->st) {
195 ERROR("%s: Couldn't create mesa state tracker context!\n",
196 __func__);
197 switch (result) {
198 case ST_CONTEXT_SUCCESS:
199 ERROR("%s: State tracker error: SUCCESS?\n", __func__);
200 break;
201 case ST_CONTEXT_ERROR_NO_MEMORY:
202 ERROR("%s: State tracker error: NO_MEMORY\n", __func__);
203 break;
204 case ST_CONTEXT_ERROR_BAD_API:
205 ERROR("%s: State tracker error: BAD_API\n", __func__);
206 break;
207 case ST_CONTEXT_ERROR_BAD_VERSION:
208 ERROR("%s: State tracker error: BAD_VERSION\n", __func__);
209 break;
210 case ST_CONTEXT_ERROR_BAD_FLAG:
211 ERROR("%s: State tracker error: BAD_FLAG\n", __func__);
212 break;
213 case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE:
214 ERROR("%s: State tracker error: BAD_ATTRIBUTE\n", __func__);
215 break;
216 case ST_CONTEXT_ERROR_UNKNOWN_FLAG:
217 ERROR("%s: State tracker error: UNKNOWN_FLAG\n", __func__);
218 break;
219 }
220
221 hgl_destroy_st_visual(context->stVisual);
222 FREE(context);
223 return -1;
224 }
225
226 assert(!context->st->st_manager_private);
227 context->st->st_manager_private = (void*)context;
228
229 struct st_context *stContext = (struct st_context*)context->st;
230
231 stContext->ctx->Driver.Viewport = hgl_viewport;
232
233 // Init Gallium3D Post Processing
234 // TODO: no pp filters are enabled yet through postProcessEnable
235 context->postProcess = pp_init(stContext->pipe, context->postProcessEnable,
236 stContext->cso_context);
237
238 context_id contextNext = -1;
239 Lock();
240 for (context_id i = 0; i < CONTEXT_MAX; i++) {
241 if (fContext[i] == NULL) {
242 fContext[i] = context;
243 contextNext = i;
244 break;
245 }
246 }
247 Unlock();
248
249 if (contextNext < 0) {
250 ERROR("%s: The next context is invalid... something went wrong!\n",
251 __func__);
252 //st_destroy_context(context->st);
253 FREE(context->stVisual);
254 FREE(context);
255 return -1;
256 }
257
258 TRACE("%s: context #%" B_PRIu64 " is the next available context\n",
259 __func__, contextNext);
260
261 return contextNext;
262 }
263
264
265 void
266 GalliumContext::DestroyContext(context_id contextID)
267 {
268 // fMutex should be locked *before* calling DestoryContext
269
270 // See if context is used
271 if (!fContext[contextID])
272 return;
273
274 if (fContext[contextID]->st) {
275 fContext[contextID]->st->flush(fContext[contextID]->st, 0, NULL);
276 fContext[contextID]->st->destroy(fContext[contextID]->st);
277 }
278
279 if (fContext[contextID]->postProcess)
280 pp_free(fContext[contextID]->postProcess);
281
282 // Delete state tracker framebuffer objects
283 if (fContext[contextID]->read)
284 delete fContext[contextID]->read;
285 if (fContext[contextID]->draw)
286 delete fContext[contextID]->draw;
287
288 if (fContext[contextID]->stVisual)
289 hgl_destroy_st_visual(fContext[contextID]->stVisual);
290
291 if (fContext[contextID]->manager)
292 hgl_destroy_st_manager(fContext[contextID]->manager);
293
294 FREE(fContext[contextID]);
295 }
296
297
298 status_t
299 GalliumContext::SetCurrentContext(Bitmap *bitmap, context_id contextID)
300 {
301 CALLED();
302
303 if (contextID < 0 || contextID > CONTEXT_MAX) {
304 ERROR("%s: Invalid context ID range!\n", __func__);
305 return B_ERROR;
306 }
307
308 Lock();
309 context_id oldContextID = fCurrentContext;
310 struct hgl_context* context = fContext[contextID];
311 Unlock();
312
313 if (!context) {
314 ERROR("%s: Invalid context provided (#%" B_PRIu64 ")!\n",
315 __func__, contextID);
316 return B_ERROR;
317 }
318
319 struct st_api* api = context->api;
320
321 if (!bitmap) {
322 api->make_current(context->api, NULL, NULL, NULL);
323 return B_OK;
324 }
325
326 // Everything seems valid, lets set the new context.
327 fCurrentContext = contextID;
328
329 if (oldContextID > 0 && oldContextID != contextID) {
330 fContext[oldContextID]->st->flush(fContext[oldContextID]->st,
331 ST_FLUSH_FRONT, NULL);
332 }
333
334 // We need to lock and unlock framebuffers before accessing them
335 api->make_current(context->api, context->st, context->draw->stfbi,
336 context->read->stfbi);
337
338 if (context->textures[ST_ATTACHMENT_BACK_LEFT]
339 && context->textures[ST_ATTACHMENT_DEPTH_STENCIL]
340 && context->postProcess) {
341 TRACE("Postprocessing textures...\n");
342 pp_init_fbos(context->postProcess,
343 context->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
344 context->textures[ST_ATTACHMENT_BACK_LEFT]->height0);
345 }
346
347 context->bitmap = bitmap;
348 //context->st->pipe->priv = context;
349
350 return B_OK;
351 }
352
353
354 status_t
355 GalliumContext::SwapBuffers(context_id contextID)
356 {
357 CALLED();
358
359 Lock();
360 struct hgl_context *context = fContext[contextID];
361 Unlock();
362
363 if (!context) {
364 ERROR("%s: context not found\n", __func__);
365 return B_ERROR;
366 }
367
368 // TODO: Where did st_notify_swapbuffers go?
369 //st_notify_swapbuffers(context->draw->stfbi);
370
371 context->st->flush(context->st, ST_FLUSH_FRONT, NULL);
372
373 struct st_context *stContext = (struct st_context*)context->st;
374
375 unsigned nColorBuffers = stContext->state.framebuffer.nr_cbufs;
376 for (unsigned i = 0; i < nColorBuffers; i++) {
377 pipe_surface* surface = stContext->state.framebuffer.cbufs[i];
378 if (!surface) {
379 ERROR("%s: Color buffer %d invalid!\n", __func__, i);
380 continue;
381 }
382
383 TRACE("%s: Flushing color buffer #%d\n", __func__, i);
384
385 // We pass our destination bitmap to flush_fronbuffer which passes it
386 // to the private winsys display call.
387 fScreen->flush_frontbuffer(fScreen, surface->texture, 0, 0,
388 context->bitmap, NULL);
389 }
390
391 #if 0
392 // TODO... should we flush the z stencil buffer?
393 pipe_surface* zSurface = stContext->state.framebuffer.zsbuf;
394 fScreen->flush_frontbuffer(fScreen, zSurface->texture, 0, 0,
395 context->bitmap, NULL);
396 #endif
397
398 return B_OK;
399 }
400
401
402 void
403 GalliumContext::ResizeViewport(int32 width, int32 height)
404 {
405 CALLED();
406 for (context_id i = 0; i < CONTEXT_MAX; i++) {
407 if (fContext[i] && fContext[i]->st) {
408 struct st_context *stContext = (struct st_context*)fContext[i]->st;
409 _mesa_set_viewport(stContext->ctx, 0, 0, 0, width, height);
410 st_manager_validate_framebuffers(stContext);
411 }
412 }
413 }
414
415
416 void
417 GalliumContext::Lock()
418 {
419 CALLED();
420 pipe_mutex_lock(fMutex);
421 }
422
423
424 void
425 GalliumContext::Unlock()
426 {
427 CALLED();
428 pipe_mutex_unlock(fMutex);
429 }
430 /* vim: set tabstop=4: */