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"
15 #include "bitmap_wrapper.h"
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"
32 #include "target-helpers/inline_sw_helper.h"
33 #include "target-helpers/inline_debug_helper.h"
38 # define TRACE(x...) printf("GalliumContext: " x)
39 # define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
44 #define ERROR(x...) printf("GalliumContext: " x)
48 hgl_viewport(struct gl_context
* glContext
)
50 // TODO: We should try to eliminate this function
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
;
57 TRACE("%s(glContext: %p, x: %d, y: %d, w: %d, h: %d\n", __func__
,
58 glContext
, x
, y
, width
, height
);
60 _mesa_check_init_viewport(glContext
, width
, height
);
62 struct gl_framebuffer
*draw
= glContext
->WinSysDrawBuffer
;
63 struct gl_framebuffer
*read
= glContext
->WinSysReadBuffer
;
66 _mesa_resize_framebuffer(glContext
, draw
, width
, height
);
68 _mesa_resize_framebuffer(glContext
, read
, width
, height
);
72 GalliumContext::GalliumContext(ulong options
)
80 // Make all contexts a known value
81 for (context_id i
= 0; i
< CONTEXT_MAX
; i
++)
86 pipe_mutex_init(fMutex
);
90 GalliumContext::~GalliumContext()
94 // Destroy our contexts
96 for (context_id i
= 0; i
< CONTEXT_MAX
; i
++)
100 pipe_mutex_destroy(fMutex
);
102 // TODO: Destroy fScreen
107 GalliumContext::CreateScreen()
111 // Allocate winsys and attach callback hooks
112 struct sw_winsys
* winsys
= hgl_create_sw_winsys();
115 ERROR("%s: Couldn't allocate sw_winsys!\n", __func__
);
119 fScreen
= sw_screen_create(winsys
);
121 if (fScreen
== NULL
) {
122 ERROR("%s: Couldn't create screen!\n", __FUNCTION__
);
127 debug_screen_wrap(fScreen
);
129 const char* driverName
= fScreen
->get_name(fScreen
);
130 ERROR("%s: Using %s driver.\n", __func__
, driverName
);
137 GalliumContext::CreateContext(Bitmap
*bitmap
)
141 struct hgl_context
* context
= CALLOC_STRUCT(hgl_context
);
144 ERROR("%s: Couldn't create pipe context!\n", __FUNCTION__
);
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
;
155 context
->api
= st_gl_api_create();
157 ERROR("%s: Couldn't obtain Mesa state tracker API!\n", __func__
);
161 // Create state_tracker manager
162 context
->manager
= hgl_create_st_manager(fScreen
);
164 // Create state tracker visual
165 context
->stVisual
= hgl_create_st_visual(fOptions
);
167 // Create state tracker framebuffers
168 context
->draw
= hgl_create_st_framebuffer(context
);
169 context
->read
= hgl_create_st_framebuffer(context
);
171 if (!context
->draw
|| !context
->read
) {
172 ERROR("%s: Problem allocating framebuffer!\n", __func__
);
173 FREE(context
->stVisual
);
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
;
185 //attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
187 struct st_api
* api
= context
->api
;
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
);
195 ERROR("%s: Couldn't create mesa state tracker context!\n",
198 case ST_CONTEXT_SUCCESS
:
199 ERROR("%s: State tracker error: SUCCESS?\n", __func__
);
201 case ST_CONTEXT_ERROR_NO_MEMORY
:
202 ERROR("%s: State tracker error: NO_MEMORY\n", __func__
);
204 case ST_CONTEXT_ERROR_BAD_API
:
205 ERROR("%s: State tracker error: BAD_API\n", __func__
);
207 case ST_CONTEXT_ERROR_BAD_VERSION
:
208 ERROR("%s: State tracker error: BAD_VERSION\n", __func__
);
210 case ST_CONTEXT_ERROR_BAD_FLAG
:
211 ERROR("%s: State tracker error: BAD_FLAG\n", __func__
);
213 case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE
:
214 ERROR("%s: State tracker error: BAD_ATTRIBUTE\n", __func__
);
216 case ST_CONTEXT_ERROR_UNKNOWN_FLAG
:
217 ERROR("%s: State tracker error: UNKNOWN_FLAG\n", __func__
);
221 hgl_destroy_st_visual(context
->stVisual
);
226 assert(!context
->st
->st_manager_private
);
227 context
->st
->st_manager_private
= (void*)context
;
229 struct st_context
*stContext
= (struct st_context
*)context
->st
;
231 stContext
->ctx
->Driver
.Viewport
= hgl_viewport
;
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
);
238 context_id contextNext
= -1;
240 for (context_id i
= 0; i
< CONTEXT_MAX
; i
++) {
241 if (fContext
[i
] == NULL
) {
242 fContext
[i
] = context
;
249 if (contextNext
< 0) {
250 ERROR("%s: The next context is invalid... something went wrong!\n",
252 //st_destroy_context(context->st);
253 FREE(context
->stVisual
);
258 TRACE("%s: context #%" B_PRIu64
" is the next available context\n",
259 __func__
, contextNext
);
266 GalliumContext::DestroyContext(context_id contextID
)
268 // fMutex should be locked *before* calling DestoryContext
270 // See if context is used
271 if (!fContext
[contextID
])
274 if (fContext
[contextID
]->st
) {
275 fContext
[contextID
]->st
->flush(fContext
[contextID
]->st
, 0, NULL
);
276 fContext
[contextID
]->st
->destroy(fContext
[contextID
]->st
);
279 if (fContext
[contextID
]->postProcess
)
280 pp_free(fContext
[contextID
]->postProcess
);
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
;
288 if (fContext
[contextID
]->stVisual
)
289 hgl_destroy_st_visual(fContext
[contextID
]->stVisual
);
291 if (fContext
[contextID
]->manager
)
292 hgl_destroy_st_manager(fContext
[contextID
]->manager
);
294 FREE(fContext
[contextID
]);
299 GalliumContext::SetCurrentContext(Bitmap
*bitmap
, context_id contextID
)
303 if (contextID
< 0 || contextID
> CONTEXT_MAX
) {
304 ERROR("%s: Invalid context ID range!\n", __func__
);
309 context_id oldContextID
= fCurrentContext
;
310 struct hgl_context
* context
= fContext
[contextID
];
314 ERROR("%s: Invalid context provided (#%" B_PRIu64
")!\n",
315 __func__
, contextID
);
319 struct st_api
* api
= context
->api
;
322 api
->make_current(context
->api
, NULL
, NULL
, NULL
);
326 // Everything seems valid, lets set the new context.
327 fCurrentContext
= contextID
;
329 if (oldContextID
> 0 && oldContextID
!= contextID
) {
330 fContext
[oldContextID
]->st
->flush(fContext
[oldContextID
]->st
,
331 ST_FLUSH_FRONT
, NULL
);
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
);
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
);
347 context
->bitmap
= bitmap
;
348 //context->st->pipe->priv = context;
355 GalliumContext::SwapBuffers(context_id contextID
)
360 struct hgl_context
*context
= fContext
[contextID
];
364 ERROR("%s: context not found\n", __func__
);
368 // TODO: Where did st_notify_swapbuffers go?
369 //st_notify_swapbuffers(context->draw->stfbi);
371 context
->st
->flush(context
->st
, ST_FLUSH_FRONT
, NULL
);
373 struct st_context
*stContext
= (struct st_context
*)context
->st
;
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
];
379 ERROR("%s: Color buffer %d invalid!\n", __func__
, i
);
383 TRACE("%s: Flushing color buffer #%d\n", __func__
, i
);
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
);
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
);
403 GalliumContext::ResizeViewport(int32 width
, int32 height
)
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
);
417 GalliumContext::Lock()
420 pipe_mutex_lock(fMutex
);
425 GalliumContext::Unlock()
428 pipe_mutex_unlock(fMutex
);
430 /* vim: set tabstop=4: */