swrast* (gallium, classic): add MESA_copy_sub_buffer support (v3)
[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 "hgl_sw_winsys.h"
30
31 #include "target-helpers/inline_sw_helper.h"
32 #include "target-helpers/inline_debug_helper.h"
33 }
34
35
36 #ifdef DEBUG
37 # define TRACE(x...) printf("GalliumContext: " x)
38 # define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
39 #else
40 # define TRACE(x...)
41 # define CALLED()
42 #endif
43 #define ERROR(x...) printf("GalliumContext: " x)
44
45
46 static void
47 hgl_viewport(struct gl_context* glContext, GLint x, GLint y,
48 GLsizei width, GLsizei height)
49 {
50 TRACE("%s(glContext: %p, x: %d, y: %d, w: %d, h: %d\n", __func__,
51 glContext, x, y, width, height);
52
53 _mesa_check_init_viewport(glContext, width, height);
54
55 struct gl_framebuffer *draw = glContext->WinSysDrawBuffer;
56 struct gl_framebuffer *read = glContext->WinSysReadBuffer;
57
58 if (draw)
59 _mesa_resize_framebuffer(glContext, draw, width, height);
60 if (read)
61 _mesa_resize_framebuffer(glContext, read, width, height);
62 }
63
64
65 static st_visual*
66 hgl_fill_st_visual(gl_config* glVisual)
67 {
68 struct st_visual* stVisual = CALLOC_STRUCT(st_visual);
69 if (!stVisual) {
70 ERROR("%s: Couldn't allocate st_visual\n", __func__);
71 return NULL;
72 }
73
74 // Determine color format
75 if (glVisual->redBits == 8) {
76 if (glVisual->alphaBits == 8)
77 stVisual->color_format = PIPE_FORMAT_A8R8G8B8_UNORM;
78 else
79 stVisual->color_format = PIPE_FORMAT_X8R8G8B8_UNORM;
80 } else {
81 // TODO: I think this should be RGB vs BGR
82 stVisual->color_format = PIPE_FORMAT_B5G6R5_UNORM;
83 }
84
85 // Determine depth stencil format
86 switch (glVisual->depthBits) {
87 default:
88 case 0:
89 stVisual->depth_stencil_format = PIPE_FORMAT_NONE;
90 break;
91 case 16:
92 stVisual->depth_stencil_format = PIPE_FORMAT_Z16_UNORM;
93 break;
94 case 24:
95 if (glVisual->stencilBits == 0) {
96 stVisual->depth_stencil_format = PIPE_FORMAT_X8Z24_UNORM;
97 } else {
98 stVisual->depth_stencil_format = PIPE_FORMAT_S8_UINT_Z24_UNORM;
99 }
100 break;
101 case 32:
102 stVisual->depth_stencil_format = PIPE_FORMAT_Z32_UNORM;
103 break;
104 }
105
106 stVisual->accum_format = (glVisual->haveAccumBuffer)
107 ? PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
108
109 stVisual->buffer_mask |= ST_ATTACHMENT_FRONT_LEFT_MASK;
110 stVisual->render_buffer = ST_ATTACHMENT_FRONT_LEFT;
111 if (glVisual->doubleBufferMode) {
112 stVisual->buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
113 stVisual->render_buffer = ST_ATTACHMENT_BACK_LEFT;
114 }
115
116 if (glVisual->stereoMode) {
117 stVisual->buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
118 if (glVisual->doubleBufferMode)
119 stVisual->buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
120 }
121
122 if (glVisual->haveDepthBuffer || glVisual->haveStencilBuffer)
123 stVisual->buffer_mask |= ST_ATTACHMENT_DEPTH_STENCIL_MASK;
124
125 return stVisual;
126 }
127
128
129 static int
130 hook_stm_get_param(struct st_manager *smapi, enum st_manager_param param)
131 {
132 CALLED();
133
134 switch (param) {
135 case ST_MANAGER_BROKEN_INVALIDATE:
136 TRACE("%s: TODO: How should we handle BROKEN_INVALIDATE calls?\n",
137 __func__);
138 // For now we force validation of the framebuffer.
139 return 1;
140 }
141
142 return 0;
143 }
144
145
146 GalliumContext::GalliumContext(ulong options)
147 :
148 fOptions(options),
149 fScreen(NULL),
150 fCurrentContext(0)
151 {
152 CALLED();
153
154 // Make all contexts a known value
155 for (context_id i = 0; i < CONTEXT_MAX; i++)
156 fContext[i] = NULL;
157
158 CreateScreen();
159
160 pipe_mutex_init(fMutex);
161 }
162
163
164 GalliumContext::~GalliumContext()
165 {
166 CALLED();
167
168 // Destroy our contexts
169 Lock();
170 for (context_id i = 0; i < CONTEXT_MAX; i++)
171 DestroyContext(i);
172 Unlock();
173
174 pipe_mutex_destroy(fMutex);
175
176 // TODO: Destroy fScreen
177 }
178
179
180 status_t
181 GalliumContext::CreateScreen()
182 {
183 CALLED();
184
185 // Allocate winsys and attach callback hooks
186 struct sw_winsys* winsys = hgl_create_sw_winsys();
187
188 if (!winsys) {
189 ERROR("%s: Couldn't allocate sw_winsys!\n", __func__);
190 return B_ERROR;
191 }
192
193 fScreen = sw_screen_create(winsys);
194
195 if (fScreen == NULL) {
196 ERROR("%s: Couldn't create screen!\n", __FUNCTION__);
197 FREE(winsys);
198 return B_ERROR;
199 }
200
201 debug_screen_wrap(fScreen);
202
203 const char* driverName = fScreen->get_name(fScreen);
204 ERROR("%s: Using %s driver.\n", __func__, driverName);
205
206 return B_OK;
207 }
208
209
210 context_id
211 GalliumContext::CreateContext(Bitmap *bitmap)
212 {
213 CALLED();
214
215 struct hgl_context* context = CALLOC_STRUCT(hgl_context);
216
217 if (!context) {
218 ERROR("%s: Couldn't create pipe context!\n", __FUNCTION__);
219 return 0;
220 }
221
222 // Set up the initial things our context needs
223 context->bitmap = bitmap;
224 context->colorSpace = get_bitmap_color_space(bitmap);
225 context->draw = NULL;
226 context->read = NULL;
227 context->st = NULL;
228
229 context->api = st_gl_api_create();
230 if (!context->api) {
231 ERROR("%s: Couldn't obtain Mesa state tracker API!\n", __func__);
232 return -1;
233 }
234
235 context->manager = CALLOC_STRUCT(st_manager);
236 if (!context->manager) {
237 ERROR("%s: Couldn't allocate Mesa state tracker manager!\n", __func__);
238 return -1;
239 }
240 context->manager->get_param = hook_stm_get_param;
241
242 // Calculate visual configuration
243 const GLboolean rgbFlag = ((fOptions & BGL_INDEX) == 0);
244 const GLboolean alphaFlag = ((fOptions & BGL_ALPHA) == BGL_ALPHA);
245 const GLboolean dblFlag = ((fOptions & BGL_DOUBLE) == BGL_DOUBLE);
246 const GLboolean stereoFlag = false;
247 const GLint depth = (fOptions & BGL_DEPTH) ? 24 : 0;
248 const GLint stencil = (fOptions & BGL_STENCIL) ? 8 : 0;
249 const GLint accum = (fOptions & BGL_ACCUM) ? 16 : 0;
250 const GLint red = rgbFlag ? 8 : 5;
251 const GLint green = rgbFlag ? 8 : 5;
252 const GLint blue = rgbFlag ? 8 : 5;
253 const GLint alpha = alphaFlag ? 8 : 0;
254
255 TRACE("rgb :\t%d\n", (bool)rgbFlag);
256 TRACE("alpha :\t%d\n", (bool)alphaFlag);
257 TRACE("dbl :\t%d\n", (bool)dblFlag);
258 TRACE("stereo :\t%d\n", (bool)stereoFlag);
259 TRACE("depth :\t%d\n", depth);
260 TRACE("stencil :\t%d\n", stencil);
261 TRACE("accum :\t%d\n", accum);
262 TRACE("red :\t%d\n", red);
263 TRACE("green :\t%d\n", green);
264 TRACE("blue :\t%d\n", blue);
265 TRACE("alpha :\t%d\n", alpha);
266
267 gl_config* glVisual = _mesa_create_visual(dblFlag, stereoFlag, red, green,
268 blue, alpha, depth, stencil, accum, accum, accum, alpha ? accum : 0, 1);
269
270 if (!glVisual) {
271 ERROR("%s: Couldn't create Mesa visual!\n", __func__);
272 return -1;
273 }
274
275 TRACE("depthBits :\t%d\n", glVisual->depthBits);
276 TRACE("stencilBits :\t%d\n", glVisual->stencilBits);
277
278 // Convert Mesa calculated visual into state tracker visual
279 context->stVisual = hgl_fill_st_visual(glVisual);
280
281 context->draw = new GalliumFramebuffer(context->stVisual, (void*)this);
282 context->read = new GalliumFramebuffer(context->stVisual, (void*)this);
283
284 if (!context->draw || !context->read) {
285 ERROR("%s: Problem allocating framebuffer!\n", __func__);
286 _mesa_destroy_visual(glVisual);
287 return -1;
288 }
289
290 // We need to assign the screen *before* calling st_api create_context
291 context->manager->screen = fScreen;
292
293 // Build state tracker attributes
294 struct st_context_attribs attribs;
295 memset(&attribs, 0, sizeof(attribs));
296 attribs.options.force_glsl_extensions_warn = false;
297 attribs.profile = ST_PROFILE_DEFAULT;
298 attribs.visual = *context->stVisual;
299 attribs.major = 1;
300 attribs.minor = 0;
301 //attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
302
303 struct st_api* api = context->api;
304
305 // Create context using state tracker api call
306 enum st_context_error result;
307 context->st = api->create_context(api, context->manager, &attribs,
308 &result, context->st);
309
310 if (!context->st) {
311 ERROR("%s: Couldn't create mesa state tracker context!\n",
312 __func__);
313 switch (result) {
314 case ST_CONTEXT_SUCCESS:
315 ERROR("%s: State tracker error: SUCCESS?\n", __func__);
316 break;
317 case ST_CONTEXT_ERROR_NO_MEMORY:
318 ERROR("%s: State tracker error: NO_MEMORY\n", __func__);
319 break;
320 case ST_CONTEXT_ERROR_BAD_API:
321 ERROR("%s: State tracker error: BAD_API\n", __func__);
322 break;
323 case ST_CONTEXT_ERROR_BAD_VERSION:
324 ERROR("%s: State tracker error: BAD_VERSION\n", __func__);
325 break;
326 case ST_CONTEXT_ERROR_BAD_FLAG:
327 ERROR("%s: State tracker error: BAD_FLAG\n", __func__);
328 break;
329 case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE:
330 ERROR("%s: State tracker error: BAD_ATTRIBUTE\n", __func__);
331 break;
332 case ST_CONTEXT_ERROR_UNKNOWN_FLAG:
333 ERROR("%s: State tracker error: UNKNOWN_FLAG\n", __func__);
334 break;
335 }
336
337 FREE(context);
338 return -1;
339 }
340
341 assert(!context->st->st_manager_private);
342 context->st->st_manager_private = (void*)context;
343
344 struct st_context *stContext = (struct st_context*)context->st;
345
346 stContext->ctx->Driver.Viewport = hgl_viewport;
347
348 // Init Gallium3D Post Processing
349 // TODO: no pp filters are enabled yet through postProcessEnable
350 context->postProcess = pp_init(stContext->pipe, context->postProcessEnable,
351 stContext->cso_context);
352
353 context_id contextNext = -1;
354 Lock();
355 for (context_id i = 0; i < CONTEXT_MAX; i++) {
356 if (fContext[i] == NULL) {
357 fContext[i] = context;
358 contextNext = i;
359 break;
360 }
361 }
362 Unlock();
363
364 if (contextNext < 0) {
365 ERROR("%s: The next context is invalid... something went wrong!\n",
366 __func__);
367 //st_destroy_context(context->st);
368 FREE(context);
369 _mesa_destroy_visual(glVisual);
370 return -1;
371 }
372
373 TRACE("%s: context #%" B_PRIu64 " is the next available context\n",
374 __func__, contextNext);
375
376 return contextNext;
377 }
378
379
380 void
381 GalliumContext::DestroyContext(context_id contextID)
382 {
383 // fMutex should be locked *before* calling DestoryContext
384
385 // See if context is used
386 if (!fContext[contextID])
387 return;
388
389 if (fContext[contextID]->st) {
390 fContext[contextID]->st->flush(fContext[contextID]->st, 0, NULL);
391 fContext[contextID]->st->destroy(fContext[contextID]->st);
392 }
393
394 if (fContext[contextID]->postProcess)
395 pp_free(fContext[contextID]->postProcess);
396
397 // Delete framebuffer objects
398 if (fContext[contextID]->read)
399 delete fContext[contextID]->read;
400 if (fContext[contextID]->draw)
401 delete fContext[contextID]->draw;
402
403 if (fContext[contextID]->stVisual)
404 FREE(fContext[contextID]->stVisual);
405
406 if (fContext[contextID]->manager)
407 FREE(fContext[contextID]->manager);
408
409 FREE(fContext[contextID]);
410 }
411
412
413 status_t
414 GalliumContext::SetCurrentContext(Bitmap *bitmap, context_id contextID)
415 {
416 CALLED();
417
418 if (contextID < 0 || contextID > CONTEXT_MAX) {
419 ERROR("%s: Invalid context ID range!\n", __func__);
420 return B_ERROR;
421 }
422
423 Lock();
424 context_id oldContextID = fCurrentContext;
425 struct hgl_context* context = fContext[contextID];
426 Unlock();
427
428 if (!context) {
429 ERROR("%s: Invalid context provided (#%" B_PRIu64 ")!\n",
430 __func__, contextID);
431 return B_ERROR;
432 }
433
434 struct st_api* api = context->api;
435
436 if (!bitmap) {
437 api->make_current(context->api, NULL, NULL, NULL);
438 return B_OK;
439 }
440
441 // Everything seems valid, lets set the new context.
442 fCurrentContext = contextID;
443
444 if (oldContextID > 0 && oldContextID != contextID) {
445 fContext[oldContextID]->st->flush(fContext[oldContextID]->st,
446 ST_FLUSH_FRONT, NULL);
447 }
448
449 // We need to lock and unlock framebuffers before accessing them
450 context->draw->Lock();
451 context->read->Lock();
452 api->make_current(context->api, context->st, context->draw->fBuffer,
453 context->read->fBuffer);
454 context->draw->Unlock();
455 context->read->Unlock();
456
457 if (context->textures[ST_ATTACHMENT_BACK_LEFT]
458 && context->textures[ST_ATTACHMENT_DEPTH_STENCIL]
459 && context->postProcess) {
460 TRACE("Postprocessing textures...\n");
461 pp_init_fbos(context->postProcess,
462 context->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
463 context->textures[ST_ATTACHMENT_BACK_LEFT]->height0);
464 }
465
466 context->bitmap = bitmap;
467 //context->st->pipe->priv = context;
468
469 return B_OK;
470 }
471
472
473 status_t
474 GalliumContext::SwapBuffers(context_id contextID)
475 {
476 CALLED();
477
478 Lock();
479 struct hgl_context *context = fContext[contextID];
480 Unlock();
481
482 if (!context) {
483 ERROR("%s: context not found\n", __func__);
484 return B_ERROR;
485 }
486
487 // TODO: Where did st_notify_swapbuffers go?
488 //st_notify_swapbuffers(context->draw->stfb);
489
490 context->st->flush(context->st, ST_FLUSH_FRONT, NULL);
491
492 struct st_context *stContext = (struct st_context*)context->st;
493
494 unsigned nColorBuffers = stContext->state.framebuffer.nr_cbufs;
495 for (unsigned i = 0; i < nColorBuffers; i++) {
496 pipe_surface* surface = stContext->state.framebuffer.cbufs[i];
497 if (!surface) {
498 ERROR("%s: Color buffer %d invalid!\n", __func__, i);
499 continue;
500 }
501
502 TRACE("%s: Flushing color buffer #%d\n", __func__, i);
503
504 // We pass our destination bitmap to flush_fronbuffer which passes it
505 // to the private winsys display call.
506 fScreen->flush_frontbuffer(fScreen, surface->texture, 0, 0,
507 context->bitmap, NULL);
508 }
509
510 #if 0
511 // TODO... should we flush the z stencil buffer?
512 pipe_surface* zSurface = stContext->state.framebuffer.zsbuf;
513 fScreen->flush_frontbuffer(fScreen, zSurface->texture, 0, 0,
514 context->bitmap, NULL);
515 #endif
516
517 return B_OK;
518 }
519
520
521 void
522 GalliumContext::ResizeViewport(int32 width, int32 height)
523 {
524 CALLED();
525 for (context_id i = 0; i < CONTEXT_MAX; i++) {
526 if (fContext[i] && fContext[i]->st) {
527 struct st_context *stContext = (struct st_context*)fContext[i]->st;
528 _mesa_set_viewport(stContext->ctx, 0, 0, width, height);
529 st_manager_validate_framebuffers(stContext);
530 }
531 }
532 }
533
534
535 void
536 GalliumContext::Lock()
537 {
538 CALLED();
539 pipe_mutex_lock(fMutex);
540 }
541
542
543 void
544 GalliumContext::Unlock()
545 {
546 CALLED();
547 pipe_mutex_unlock(fMutex);
548 }
549 /* vim: set tabstop=4: */