2 * Copyright 2012, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Artur Wyszynski, harakash@gmail.com
7 * Alexander von Gluck IV, kallisti5@unixzen.com
11 #include "GalliumContext.h"
17 #include "bitmap_wrapper.h"
19 #include "glapi/glapi.h"
20 #include "pipe/p_format.h"
21 //#include "state_tracker/st_cb_fbo.h"
22 //#include "state_tracker/st_cb_flush.h"
23 #include "state_tracker/st_context.h"
24 #include "state_tracker/st_gl_api.h"
25 #include "state_tracker/sw_winsys.h"
26 #include "sw/hgl/hgl_sw_winsys.h"
27 #include "util/u_atomic.h"
28 #include "util/u_memory.h"
30 #include "target-helpers/inline_sw_helper.h"
31 #include "target-helpers/inline_debug_helper.h"
35 # define TRACE(x...) printf("GalliumContext: " x)
36 # define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
41 #define ERROR(x...) printf("GalliumContext: " x)
44 GalliumContext::GalliumContext(ulong options
)
52 // Make all contexts a known value
53 for (context_id i
= 0; i
< CONTEXT_MAX
; i
++)
58 (void) mtx_init(&fMutex
, mtx_plain
);
62 GalliumContext::~GalliumContext()
66 // Destroy our contexts
68 for (context_id i
= 0; i
< CONTEXT_MAX
; i
++)
74 // TODO: Destroy fScreen
79 GalliumContext::CreateScreen()
83 // Allocate winsys and attach callback hooks
84 struct sw_winsys
* winsys
= hgl_create_sw_winsys();
87 ERROR("%s: Couldn't allocate sw_winsys!\n", __func__
);
91 fScreen
= sw_screen_create(winsys
);
93 if (fScreen
== NULL
) {
94 ERROR("%s: Couldn't create screen!\n", __FUNCTION__
);
99 debug_screen_wrap(fScreen
);
101 const char* driverName
= fScreen
->get_name(fScreen
);
102 ERROR("%s: Using %s driver.\n", __func__
, driverName
);
109 GalliumContext::CreateContext(Bitmap
*bitmap
)
113 struct hgl_context
* context
= CALLOC_STRUCT(hgl_context
);
116 ERROR("%s: Couldn't create pipe context!\n", __FUNCTION__
);
120 // Set up the initial things our context needs
121 context
->bitmap
= bitmap
;
122 context
->colorSpace
= get_bitmap_color_space(bitmap
);
123 context
->screen
= fScreen
;
124 context
->draw
= NULL
;
125 context
->read
= NULL
;
129 context
->api
= hgl_create_st_api();
131 ERROR("%s: Couldn't obtain Mesa state tracker API!\n", __func__
);
135 // Create state_tracker manager
136 context
->manager
= hgl_create_st_manager(context
);
138 // Create state tracker visual
139 context
->stVisual
= hgl_create_st_visual(fOptions
);
141 // Create state tracker framebuffers
142 context
->draw
= hgl_create_st_framebuffer(context
);
143 context
->read
= hgl_create_st_framebuffer(context
);
145 if (!context
->draw
|| !context
->read
) {
146 ERROR("%s: Problem allocating framebuffer!\n", __func__
);
147 FREE(context
->stVisual
);
151 // Build state tracker attributes
152 struct st_context_attribs attribs
;
153 memset(&attribs
, 0, sizeof(attribs
));
154 attribs
.options
.force_glsl_extensions_warn
= false;
155 attribs
.profile
= ST_PROFILE_DEFAULT
;
156 attribs
.visual
= *context
->stVisual
;
159 //attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
161 // Create context using state tracker api call
162 enum st_context_error result
;
163 context
->st
= context
->api
->create_context(context
->api
, context
->manager
,
164 &attribs
, &result
, context
->st
);
167 ERROR("%s: Couldn't create mesa state tracker context!\n",
170 case ST_CONTEXT_SUCCESS
:
171 ERROR("%s: State tracker error: SUCCESS?\n", __func__
);
173 case ST_CONTEXT_ERROR_NO_MEMORY
:
174 ERROR("%s: State tracker error: NO_MEMORY\n", __func__
);
176 case ST_CONTEXT_ERROR_BAD_API
:
177 ERROR("%s: State tracker error: BAD_API\n", __func__
);
179 case ST_CONTEXT_ERROR_BAD_VERSION
:
180 ERROR("%s: State tracker error: BAD_VERSION\n", __func__
);
182 case ST_CONTEXT_ERROR_BAD_FLAG
:
183 ERROR("%s: State tracker error: BAD_FLAG\n", __func__
);
185 case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE
:
186 ERROR("%s: State tracker error: BAD_ATTRIBUTE\n", __func__
);
188 case ST_CONTEXT_ERROR_UNKNOWN_FLAG
:
189 ERROR("%s: State tracker error: UNKNOWN_FLAG\n", __func__
);
193 hgl_destroy_st_visual(context
->stVisual
);
198 assert(!context
->st
->st_manager_private
);
199 context
->st
->st_manager_private
= (void*)context
;
201 struct st_context
*stContext
= (struct st_context
*)context
->st
;
203 // Init Gallium3D Post Processing
204 // TODO: no pp filters are enabled yet through postProcessEnable
205 context
->postProcess
= pp_init(stContext
->pipe
, context
->postProcessEnable
,
206 stContext
->cso_context
);
208 context_id contextNext
= -1;
210 for (context_id i
= 0; i
< CONTEXT_MAX
; i
++) {
211 if (fContext
[i
] == NULL
) {
212 fContext
[i
] = context
;
219 if (contextNext
< 0) {
220 ERROR("%s: The next context is invalid... something went wrong!\n",
222 //st_destroy_context(context->st);
223 FREE(context
->stVisual
);
228 TRACE("%s: context #%" B_PRIu64
" is the next available context\n",
229 __func__
, contextNext
);
236 GalliumContext::DestroyContext(context_id contextID
)
238 // fMutex should be locked *before* calling DestoryContext
240 // See if context is used
241 if (!fContext
[contextID
])
244 if (fContext
[contextID
]->st
) {
245 fContext
[contextID
]->st
->flush(fContext
[contextID
]->st
, 0, NULL
);
246 fContext
[contextID
]->st
->destroy(fContext
[contextID
]->st
);
249 if (fContext
[contextID
]->postProcess
)
250 pp_free(fContext
[contextID
]->postProcess
);
252 // Delete state tracker framebuffer objects
253 if (fContext
[contextID
]->read
)
254 delete fContext
[contextID
]->read
;
255 if (fContext
[contextID
]->draw
)
256 delete fContext
[contextID
]->draw
;
258 if (fContext
[contextID
]->stVisual
)
259 hgl_destroy_st_visual(fContext
[contextID
]->stVisual
);
261 if (fContext
[contextID
]->manager
)
262 hgl_destroy_st_manager(fContext
[contextID
]->manager
);
264 FREE(fContext
[contextID
]);
269 GalliumContext::SetCurrentContext(Bitmap
*bitmap
, context_id contextID
)
273 if (contextID
< 0 || contextID
> CONTEXT_MAX
) {
274 ERROR("%s: Invalid context ID range!\n", __func__
);
279 context_id oldContextID
= fCurrentContext
;
280 struct hgl_context
* context
= fContext
[contextID
];
284 ERROR("%s: Invalid context provided (#%" B_PRIu64
")!\n",
285 __func__
, contextID
);
290 context
->api
->make_current(context
->api
, NULL
, NULL
, NULL
);
294 // Everything seems valid, lets set the new context.
295 fCurrentContext
= contextID
;
297 if (oldContextID
> 0 && oldContextID
!= contextID
) {
298 fContext
[oldContextID
]->st
->flush(fContext
[oldContextID
]->st
,
299 ST_FLUSH_FRONT
, NULL
);
302 // We need to lock and unlock framebuffers before accessing them
303 context
->api
->make_current(context
->api
, context
->st
, context
->draw
->stfbi
,
304 context
->read
->stfbi
);
306 //if (context->textures[ST_ATTACHMENT_BACK_LEFT]
307 // && context->textures[ST_ATTACHMENT_DEPTH_STENCIL]
308 // && context->postProcess) {
309 // TRACE("Postprocessing textures...\n");
310 // pp_init_fbos(context->postProcess,
311 // context->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
312 // context->textures[ST_ATTACHMENT_BACK_LEFT]->height0);
315 context
->bitmap
= bitmap
;
316 //context->st->pipe->priv = context;
323 GalliumContext::SwapBuffers(context_id contextID
)
328 struct hgl_context
*context
= fContext
[contextID
];
332 ERROR("%s: context not found\n", __func__
);
336 // TODO: Where did st_notify_swapbuffers go?
337 //st_notify_swapbuffers(context->draw->stfbi);
339 context
->st
->flush(context
->st
, ST_FLUSH_FRONT
, NULL
);
341 struct st_context
*stContext
= (struct st_context
*)context
->st
;
343 unsigned nColorBuffers
= stContext
->state
.framebuffer
.nr_cbufs
;
344 for (unsigned i
= 0; i
< nColorBuffers
; i
++) {
345 pipe_surface
* surface
= stContext
->state
.framebuffer
.cbufs
[i
];
347 ERROR("%s: Color buffer %d invalid!\n", __func__
, i
);
351 TRACE("%s: Flushing color buffer #%d\n", __func__
, i
);
353 // We pass our destination bitmap to flush_fronbuffer which passes it
354 // to the private winsys display call.
355 fScreen
->flush_frontbuffer(fScreen
, surface
->texture
, 0, 0,
356 context
->bitmap
, NULL
);
360 // TODO... should we flush the z stencil buffer?
361 pipe_surface
* zSurface
= stContext
->state
.framebuffer
.zsbuf
;
362 fScreen
->flush_frontbuffer(fScreen
, zSurface
->texture
, 0, 0,
363 context
->bitmap
, NULL
);
371 GalliumContext::Validate(uint32 width
, uint32 height
)
375 if (!fContext
[fCurrentContext
]) {
379 if (fContext
[fCurrentContext
]->width
!= width
380 || fContext
[fCurrentContext
]->height
!= height
) {
381 Invalidate(width
, height
);
389 GalliumContext::Invalidate(uint32 width
, uint32 height
)
393 assert(fContext
[fCurrentContext
]);
395 // Update st_context dimensions
396 fContext
[fCurrentContext
]->width
= width
;
397 fContext
[fCurrentContext
]->height
= height
;
399 // Is this the best way to invalidate?
400 p_atomic_inc(&fContext
[fCurrentContext
]->read
->stfbi
->stamp
);
401 p_atomic_inc(&fContext
[fCurrentContext
]->draw
->stfbi
->stamp
);
406 GalliumContext::Lock()
409 pipe_mutex_lock(fMutex
);
414 GalliumContext::Unlock()
417 pipe_mutex_unlock(fMutex
);
419 /* vim: set tabstop=4: */