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 "frontend/sw_winsys.h"
26 #include "sw/hgl/hgl_sw_winsys.h"
27 #include "util/u_atomic.h"
28 #include "util/u_memory.h"
29 #include "util/u_framebuffer.h"
31 #include "target-helpers/inline_sw_helper.h"
32 #include "target-helpers/inline_debug_helper.h"
36 # define TRACE(x...) printf("GalliumContext: " x)
37 # define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
42 #define ERROR(x...) printf("GalliumContext: " x)
45 GalliumContext::GalliumContext(ulong options
)
53 // Make all contexts a known value
54 for (context_id i
= 0; i
< CONTEXT_MAX
; i
++)
59 (void) mtx_init(&fMutex
, mtx_plain
);
63 GalliumContext::~GalliumContext()
67 // Destroy our contexts
69 for (context_id i
= 0; i
< CONTEXT_MAX
; i
++)
75 // TODO: Destroy fScreen
80 GalliumContext::CreateScreen()
84 // Allocate winsys and attach callback hooks
85 struct sw_winsys
* winsys
= hgl_create_sw_winsys();
88 ERROR("%s: Couldn't allocate sw_winsys!\n", __func__
);
92 fScreen
= sw_screen_create(winsys
);
94 if (fScreen
== NULL
) {
95 ERROR("%s: Couldn't create screen!\n", __FUNCTION__
);
100 debug_screen_wrap(fScreen
);
102 const char* driverName
= fScreen
->get_name(fScreen
);
103 ERROR("%s: Using %s driver.\n", __func__
, driverName
);
110 GalliumContext::CreateContext(Bitmap
*bitmap
)
114 struct hgl_context
* context
= CALLOC_STRUCT(hgl_context
);
117 ERROR("%s: Couldn't create pipe context!\n", __FUNCTION__
);
121 // Set up the initial things our context needs
122 context
->bitmap
= bitmap
;
123 context
->colorSpace
= get_bitmap_color_space(bitmap
);
124 context
->screen
= fScreen
;
125 context
->draw
= NULL
;
126 context
->read
= NULL
;
130 context
->api
= hgl_create_st_api();
132 ERROR("%s: Couldn't obtain Mesa state tracker API!\n", __func__
);
136 // Create state_tracker manager
137 context
->manager
= hgl_create_st_manager(context
);
139 // Create state tracker visual
140 context
->stVisual
= hgl_create_st_visual(fOptions
);
142 // Create state tracker framebuffers
143 context
->draw
= hgl_create_st_framebuffer(context
);
144 context
->read
= hgl_create_st_framebuffer(context
);
146 if (!context
->draw
|| !context
->read
) {
147 ERROR("%s: Problem allocating framebuffer!\n", __func__
);
148 FREE(context
->stVisual
);
152 // Build state tracker attributes
153 struct st_context_attribs attribs
;
154 memset(&attribs
, 0, sizeof(attribs
));
155 attribs
.options
.force_glsl_extensions_warn
= false;
156 attribs
.profile
= ST_PROFILE_DEFAULT
;
157 attribs
.visual
= *context
->stVisual
;
160 //attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
162 // Create context using state tracker api call
163 enum st_context_error result
;
164 context
->st
= context
->api
->create_context(context
->api
, context
->manager
,
165 &attribs
, &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
, NULL
, 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
);
291 context
->api
->make_current(context
->api
, NULL
, NULL
, NULL
);
295 // Everything seems valid, lets set the new context.
296 fCurrentContext
= contextID
;
298 if (oldContextID
> 0 && oldContextID
!= contextID
) {
299 fContext
[oldContextID
]->st
->flush(fContext
[oldContextID
]->st
,
300 ST_FLUSH_FRONT
, NULL
, NULL
, NULL
);
303 // We need to lock and unlock framebuffers before accessing them
304 context
->api
->make_current(context
->api
, context
->st
, context
->draw
->stfbi
,
305 context
->read
->stfbi
);
307 //if (context->textures[ST_ATTACHMENT_BACK_LEFT]
308 // && context->textures[ST_ATTACHMENT_DEPTH_STENCIL]
309 // && context->postProcess) {
310 // TRACE("Postprocessing textures...\n");
311 // pp_init_fbos(context->postProcess,
312 // context->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
313 // context->textures[ST_ATTACHMENT_BACK_LEFT]->height0);
316 context
->bitmap
= bitmap
;
317 //context->st->pipe->priv = context;
324 GalliumContext::SwapBuffers(context_id contextID
)
329 struct hgl_context
*context
= fContext
[contextID
];
333 ERROR("%s: context not found\n", __func__
);
336 context
->st
->flush(context
->st
, ST_FLUSH_FRONT
, NULL
, NULL
, NULL
);
338 struct hgl_buffer
* buffer
= hgl_st_framebuffer(context
->draw
->stfbi
);
339 pipe_surface
* surface
= buffer
->surface
;
341 ERROR("%s: Invalid drawable surface!\n", __func__
);
345 fScreen
->flush_frontbuffer(fScreen
, surface
->texture
, 0, 0,
346 context
->bitmap
, NULL
);
353 GalliumContext::Validate(uint32 width
, uint32 height
)
357 if (!fContext
[fCurrentContext
]) {
361 if (fContext
[fCurrentContext
]->width
!= width
362 || fContext
[fCurrentContext
]->height
!= height
) {
363 Invalidate(width
, height
);
371 GalliumContext::Invalidate(uint32 width
, uint32 height
)
375 assert(fContext
[fCurrentContext
]);
377 // Update st_context dimensions
378 fContext
[fCurrentContext
]->width
= width
;
379 fContext
[fCurrentContext
]->height
= height
;
381 // Is this the best way to invalidate?
382 p_atomic_inc(&fContext
[fCurrentContext
]->read
->stfbi
->stamp
);
383 p_atomic_inc(&fContext
[fCurrentContext
]->draw
->stfbi
->stamp
);
388 GalliumContext::Lock()
396 GalliumContext::Unlock()
401 /* vim: set tabstop=4: */