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 "pipe/p_format.h"
19 #include "state_tracker/st_cb_fbo.h"
20 #include "state_tracker/st_cb_flush.h"
21 #include "state_tracker/st_context.h"
22 #include "state_tracker/st_gl_api.h"
23 #include "state_tracker/st_manager.h"
24 #include "state_tracker/sw_winsys.h"
25 #include "sw/hgl/hgl_sw_winsys.h"
26 #include "util/u_atomic.h"
27 #include "util/u_memory.h"
29 #include "target-helpers/inline_sw_helper.h"
30 #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 pipe_mutex_init(fMutex
);
62 GalliumContext::~GalliumContext()
66 // Destroy our contexts
68 for (context_id i
= 0; i
< CONTEXT_MAX
; i
++)
72 pipe_mutex_destroy(fMutex
);
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
;
128 context
->api
= st_gl_api_create();
130 ERROR("%s: Couldn't obtain Mesa state tracker API!\n", __func__
);
134 // Create state_tracker manager
135 context
->manager
= hgl_create_st_manager(context
);
137 // Create state tracker visual
138 context
->stVisual
= hgl_create_st_visual(fOptions
);
140 // Create state tracker framebuffers
141 context
->draw
= hgl_create_st_framebuffer(context
);
142 context
->read
= hgl_create_st_framebuffer(context
);
144 if (!context
->draw
|| !context
->read
) {
145 ERROR("%s: Problem allocating framebuffer!\n", __func__
);
146 FREE(context
->stVisual
);
150 // Build state tracker attributes
151 struct st_context_attribs attribs
;
152 memset(&attribs
, 0, sizeof(attribs
));
153 attribs
.options
.force_glsl_extensions_warn
= false;
154 attribs
.profile
= ST_PROFILE_DEFAULT
;
155 attribs
.visual
= *context
->stVisual
;
158 //attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
160 struct st_api
* api
= context
->api
;
162 // Create context using state tracker api call
163 enum st_context_error result
;
164 context
->st
= api
->create_context(api
, context
->manager
, &attribs
,
165 &result
, context
->st
);
168 ERROR("%s: Couldn't create mesa state tracker context!\n",
171 case ST_CONTEXT_SUCCESS
:
172 ERROR("%s: State tracker error: SUCCESS?\n", __func__
);
174 case ST_CONTEXT_ERROR_NO_MEMORY
:
175 ERROR("%s: State tracker error: NO_MEMORY\n", __func__
);
177 case ST_CONTEXT_ERROR_BAD_API
:
178 ERROR("%s: State tracker error: BAD_API\n", __func__
);
180 case ST_CONTEXT_ERROR_BAD_VERSION
:
181 ERROR("%s: State tracker error: BAD_VERSION\n", __func__
);
183 case ST_CONTEXT_ERROR_BAD_FLAG
:
184 ERROR("%s: State tracker error: BAD_FLAG\n", __func__
);
186 case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE
:
187 ERROR("%s: State tracker error: BAD_ATTRIBUTE\n", __func__
);
189 case ST_CONTEXT_ERROR_UNKNOWN_FLAG
:
190 ERROR("%s: State tracker error: UNKNOWN_FLAG\n", __func__
);
194 hgl_destroy_st_visual(context
->stVisual
);
199 assert(!context
->st
->st_manager_private
);
200 context
->st
->st_manager_private
= (void*)context
;
202 struct st_context
*stContext
= (struct st_context
*)context
->st
;
204 // Init Gallium3D Post Processing
205 // TODO: no pp filters are enabled yet through postProcessEnable
206 context
->postProcess
= pp_init(stContext
->pipe
, context
->postProcessEnable
,
207 stContext
->cso_context
);
209 context_id contextNext
= -1;
211 for (context_id i
= 0; i
< CONTEXT_MAX
; i
++) {
212 if (fContext
[i
] == NULL
) {
213 fContext
[i
] = context
;
220 if (contextNext
< 0) {
221 ERROR("%s: The next context is invalid... something went wrong!\n",
223 //st_destroy_context(context->st);
224 FREE(context
->stVisual
);
229 TRACE("%s: context #%" B_PRIu64
" is the next available context\n",
230 __func__
, contextNext
);
237 GalliumContext::DestroyContext(context_id contextID
)
239 // fMutex should be locked *before* calling DestoryContext
241 // See if context is used
242 if (!fContext
[contextID
])
245 if (fContext
[contextID
]->st
) {
246 fContext
[contextID
]->st
->flush(fContext
[contextID
]->st
, 0, NULL
);
247 fContext
[contextID
]->st
->destroy(fContext
[contextID
]->st
);
250 if (fContext
[contextID
]->postProcess
)
251 pp_free(fContext
[contextID
]->postProcess
);
253 // Delete state tracker framebuffer objects
254 if (fContext
[contextID
]->read
)
255 delete fContext
[contextID
]->read
;
256 if (fContext
[contextID
]->draw
)
257 delete fContext
[contextID
]->draw
;
259 if (fContext
[contextID
]->stVisual
)
260 hgl_destroy_st_visual(fContext
[contextID
]->stVisual
);
262 if (fContext
[contextID
]->manager
)
263 hgl_destroy_st_manager(fContext
[contextID
]->manager
);
265 FREE(fContext
[contextID
]);
270 GalliumContext::SetCurrentContext(Bitmap
*bitmap
, context_id contextID
)
274 if (contextID
< 0 || contextID
> CONTEXT_MAX
) {
275 ERROR("%s: Invalid context ID range!\n", __func__
);
280 context_id oldContextID
= fCurrentContext
;
281 struct hgl_context
* context
= fContext
[contextID
];
285 ERROR("%s: Invalid context provided (#%" B_PRIu64
")!\n",
286 __func__
, contextID
);
290 struct st_api
* api
= context
->api
;
293 api
->make_current(context
->api
, NULL
, NULL
, NULL
);
297 // Everything seems valid, lets set the new context.
298 fCurrentContext
= contextID
;
300 if (oldContextID
> 0 && oldContextID
!= contextID
) {
301 fContext
[oldContextID
]->st
->flush(fContext
[oldContextID
]->st
,
302 ST_FLUSH_FRONT
, NULL
);
305 // We need to lock and unlock framebuffers before accessing them
306 api
->make_current(context
->api
, context
->st
, context
->draw
->stfbi
,
307 context
->read
->stfbi
);
309 //if (context->textures[ST_ATTACHMENT_BACK_LEFT]
310 // && context->textures[ST_ATTACHMENT_DEPTH_STENCIL]
311 // && context->postProcess) {
312 // TRACE("Postprocessing textures...\n");
313 // pp_init_fbos(context->postProcess,
314 // context->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
315 // context->textures[ST_ATTACHMENT_BACK_LEFT]->height0);
318 context
->bitmap
= bitmap
;
319 //context->st->pipe->priv = context;
326 GalliumContext::SwapBuffers(context_id contextID
)
331 struct hgl_context
*context
= fContext
[contextID
];
335 ERROR("%s: context not found\n", __func__
);
339 // TODO: Where did st_notify_swapbuffers go?
340 //st_notify_swapbuffers(context->draw->stfbi);
342 context
->st
->flush(context
->st
, ST_FLUSH_FRONT
, NULL
);
344 struct st_context
*stContext
= (struct st_context
*)context
->st
;
346 unsigned nColorBuffers
= stContext
->state
.framebuffer
.nr_cbufs
;
347 for (unsigned i
= 0; i
< nColorBuffers
; i
++) {
348 pipe_surface
* surface
= stContext
->state
.framebuffer
.cbufs
[i
];
350 ERROR("%s: Color buffer %d invalid!\n", __func__
, i
);
354 TRACE("%s: Flushing color buffer #%d\n", __func__
, i
);
356 // We pass our destination bitmap to flush_fronbuffer which passes it
357 // to the private winsys display call.
358 fScreen
->flush_frontbuffer(fScreen
, surface
->texture
, 0, 0,
359 context
->bitmap
, NULL
);
363 // TODO... should we flush the z stencil buffer?
364 pipe_surface
* zSurface
= stContext
->state
.framebuffer
.zsbuf
;
365 fScreen
->flush_frontbuffer(fScreen
, zSurface
->texture
, 0, 0,
366 context
->bitmap
, NULL
);
374 GalliumContext::Validate(uint32 width
, uint32 height
)
378 if (!fContext
[fCurrentContext
]) {
382 if (fContext
[fCurrentContext
]->width
!= width
383 || fContext
[fCurrentContext
]->height
!= height
) {
384 Invalidate(width
, height
);
392 GalliumContext::Invalidate(uint32 width
, uint32 height
)
396 assert(fContext
[fCurrentContext
]);
398 // Update st_context dimensions
399 fContext
[fCurrentContext
]->width
= width
;
400 fContext
[fCurrentContext
]->height
= height
;
402 // Is this the best way to invalidate?
403 p_atomic_inc(&fContext
[fCurrentContext
]->read
->stfbi
->stamp
);
404 p_atomic_inc(&fContext
[fCurrentContext
]->draw
->stfbi
->stamp
);
409 GalliumContext::Lock()
412 pipe_mutex_lock(fMutex
);
417 GalliumContext::Unlock()
420 pipe_mutex_unlock(fMutex
);
422 /* vim: set tabstop=4: */