aboutsummaryrefslogtreecommitdiffstats
path: root/src/hgl/GLView.cpp
blob: 9e01dcc2c60c871914f199232343dd9db0c651b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
 * Copyright 2006-2012, Haiku. All rights reserved.
 * Distributed under the terms of the MIT License.
 *
 * Authors:
 *		Jérôme Duval, korli@users.berlios.de
 *		Philippe Houdoin, philippe.houdoin@free.fr
 *		Stefano Ceccherini, burton666@libero.it
 */

#include <kernel/image.h>

#include <GLView.h>

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <DirectWindow.h>
#include <GLRenderer.h>

#include "interface/DirectWindowPrivate.h"
#include "GLDispatcher.h"
#include "GLRendererRoster.h"


struct glview_direct_info {
	direct_buffer_info* direct_info;
	bool direct_connected;
	bool enable_direct_mode;

	glview_direct_info();
	~glview_direct_info();
};


BGLView::BGLView(BRect rect, const char* name, ulong resizingMode, ulong mode,
	ulong options)
	:
	BView(rect, name, B_FOLLOW_ALL_SIDES, mode | B_WILL_DRAW | B_FRAME_EVENTS),
		//  | B_FULL_UPDATE_ON_RESIZE)
	fGc(NULL),
	fOptions(options),
	fDitherCount(0),
	fDrawLock("BGLView draw lock"),
	fDisplayLock("BGLView display lock"),
	fClipInfo(NULL),
	fRenderer(NULL),
	fRoster(NULL),
	fDitherMap(NULL)
{
	fRoster = new GLRendererRoster(this, options);
	fRenderer = fRoster->GetRenderer();
}


BGLView::~BGLView()
{
	delete fClipInfo;
	if (fRenderer)
		fRenderer->Release();
}


void
BGLView::LockGL()
{
	// TODO: acquire the OpenGL API lock it on this glview

	fDisplayLock.Lock();
	if (fRenderer != NULL && fDisplayLock.CountLocks() == 1)
		fRenderer->LockGL();
}


void
BGLView::UnlockGL()
{
	if (fRenderer != NULL && fDisplayLock.CountLocks() == 1)
		fRenderer->UnlockGL();
	fDisplayLock.Unlock();

	// TODO: release the GL API lock to others glviews
}


void
BGLView::SwapBuffers()
{
	SwapBuffers(false);
}


void
BGLView::SwapBuffers(bool vSync)
{
	if (fRenderer) {
		_LockDraw();
		fRenderer->SwapBuffers(vSync);
		_UnlockDraw();
	}
}


BView*
BGLView::EmbeddedView()
{
	return NULL;
}


void*
BGLView::GetGLProcAddress(const char* procName)
{
	BGLDispatcher* glDispatcher = NULL;

	if (fRenderer)
		glDispatcher = fRenderer->GLDispatcher();

	if (glDispatcher)
		return (void*)glDispatcher->AddressOf(procName);

	return NULL;
}


status_t
BGLView::CopyPixelsOut(BPoint source, BBitmap* dest)
{
	if (!fRenderer)
		return B_ERROR;

	if (!dest || !dest->Bounds().IsValid())
		return B_BAD_VALUE;

	return fRenderer->CopyPixelsOut(source, dest);
}


status_t
BGLView::CopyPixelsIn(BBitmap* source, BPoint dest)
{
	if (!fRenderer)
		return B_ERROR;

	if (!source || !source->Bounds().IsValid())
		return B_BAD_VALUE;

	return fRenderer->CopyPixelsIn(source, dest);
}


/*!	Mesa's GLenum is not ulong but uint, so we can't use GLenum
	without breaking this method signature.
	Instead, we have to use the effective BeOS's SGI OpenGL GLenum type:
	unsigned long.
 */
void
BGLView::ErrorCallback(unsigned long errorCode)
{
	char msg[32];
	sprintf(msg, "GL: Error code $%04lx.", errorCode);
	// TODO: under BeOS R5, it call debugger(msg);
	fprintf(stderr, "%s\n", msg);
}


void
BGLView::Draw(BRect updateRect)
{
	if (fRenderer) {
		_LockDraw();
		fRenderer->Draw(updateRect);
		_UnlockDraw();
		return;
	}
	// TODO: auto-size and center the string
	MovePenTo(8, 32);
	DrawString("No OpenGL renderer available!");
}


void
BGLView::AttachedToWindow()
{
	BView::AttachedToWindow();

	fBounds = Bounds();
	for (BView* view = this; view != NULL; view = view->Parent())
		view->ConvertToParent(&fBounds);

	if (fRenderer != NULL) {
		// Jackburton: The following code was commented because it doesn't look
		// good in "direct" mode:
		// when the window is moved, the app_server doesn't paint the view's
		// background, and the stuff behind the window itself shows up.
		// Setting the view color to black, instead, looks a bit more elegant.
#if 0
		// Don't paint white window background when resized
		SetViewColor(B_TRANSPARENT_32_BIT);
#else
		SetViewColor(0, 0, 0);
#endif

		// Set default OpenGL viewport:
		LockGL();
		glViewport(0, 0, Bounds().IntegerWidth(), Bounds().IntegerHeight());
		UnlockGL();
		fRenderer->FrameResized(Bounds().IntegerWidth(),
			Bounds().IntegerHeight());

		if (fClipInfo) {
			fRenderer->DirectConnected(fClipInfo->direct_info);
			fRenderer->EnableDirectMode(fClipInfo->enable_direct_mode);
		}

		return;
	}

	fprintf(stderr, "no renderer found! \n");

	// No Renderer, no rendering. Setup a minimal "No Renderer" string drawing
	// context
	SetFont(be_bold_font);
	// SetFontSize(16);
}


void
BGLView::AllAttached()
{
	BView::AllAttached();
}


void
BGLView::DetachedFromWindow()
{
	if (fRenderer)
		fRenderer->Release();
	fRenderer = NULL;

	BView::DetachedFromWindow();
}


void
BGLView::AllDetached()
{
	BView::AllDetached();
}


void
BGLView::FrameResized(float width, float height)
{
	fBounds = Bounds();
	for (BView* v = this; v; v = v->Parent())
		v->ConvertToParent(&fBounds);

	if (fRenderer) {
		LockGL();
		_LockDraw();
		_CallDirectConnected();
		fRenderer->FrameResized(width, height);
		_UnlockDraw();
		UnlockGL();
	}

	BView::FrameResized(width, height);
}


status_t
BGLView::Perform(perform_code d, void* arg)
{
	return BView::Perform(d, arg);
}


status_t
BGLView::Archive(BMessage* data, bool deep) const
{
	return BView::Archive(data, deep);
}


void
BGLView::MessageReceived(BMessage* msg)
{
	BView::MessageReceived(msg);
}


void
BGLView::SetResizingMode(uint32 mode)
{
	BView::SetResizingMode(mode);
}


void
BGLView::GetPreferredSize(float* _width, float* _height)
{
	if (_width)
		*_width = 0;
	if (_height)
		*_height = 0;
}


void
BGLView::Show()
{
	BView::Show();
}


void
BGLView::Hide()
{
	BView::Hide();
}


BHandler*
BGLView::ResolveSpecifier(BMessage* msg, int32 index, BMessage* specifier,
	int32 form, const char* property)
{
	return BView::ResolveSpecifier(msg, index, specifier, form, property);
}


status_t
BGLView::GetSupportedSuites(BMessage* data)
{
	return BView::GetSupportedSuites(data);
}


void
BGLView::DirectConnected(direct_buffer_info* info)
{
	if (fClipInfo == NULL) {
		fClipInfo = new (std::nothrow) glview_direct_info();
		if (fClipInfo == NULL)
			return;
	}

	direct_buffer_info* localInfo = fClipInfo->direct_info;

	switch (info->buffer_state & B_DIRECT_MODE_MASK) {
		case B_DIRECT_START:
			fClipInfo->direct_connected = true;
			memcpy(localInfo, info, DIRECT_BUFFER_INFO_AREA_SIZE);
			_UnlockDraw();
			break;

		case B_DIRECT_MODIFY:
			_LockDraw();
			memcpy(localInfo, info, DIRECT_BUFFER_INFO_AREA_SIZE);
			_UnlockDraw();
			break;

		case B_DIRECT_STOP:
			fClipInfo->direct_connected = false;
			_LockDraw();
			break;
	}

	if (fRenderer)
		_CallDirectConnected();
}


void
BGLView::EnableDirectMode(bool enabled)
{
	if (fRenderer)
		fRenderer->EnableDirectMode(enabled);
	if (fClipInfo == NULL) {
		fClipInfo = new (std::nothrow) glview_direct_info();
		if (fClipInfo == NULL)
			return;
	}

	fClipInfo->enable_direct_mode = enabled;
}


void
BGLView::_LockDraw()
{
	if (!fClipInfo || !fClipInfo->enable_direct_mode)
		return;

	fDrawLock.Lock();
}


void
BGLView::_UnlockDraw()
{
	if (!fClipInfo || !fClipInfo->enable_direct_mode)
		return;

	fDrawLock.Unlock();
}


void
BGLView::_CallDirectConnected()
{
	if (!fClipInfo)
		return;

	direct_buffer_info* localInfo = fClipInfo->direct_info;
	direct_buffer_info* info = (direct_buffer_info*)malloc(
		DIRECT_BUFFER_INFO_AREA_SIZE);
	if (info == NULL)
		return;

	memcpy(info, localInfo, DIRECT_BUFFER_INFO_AREA_SIZE);

	// Collect the rects into a BRegion, then clip to the view's bounds
	BRegion region;
	for (uint32 c = 0; c < localInfo->clip_list_count; c++)
		region.Include(localInfo->clip_list[c]);
	BRegion boundsRegion = fBounds.OffsetByCopy(localInfo->window_bounds.left,
		localInfo->window_bounds.top);
	info->window_bounds = boundsRegion.RectAtInt(0);
		// window_bounds are now view bounds
	region.IntersectWith(&boundsRegion);

	info->clip_list_count = region.CountRects();
	info->clip_bounds = region.FrameInt();

	for (uint32 c = 0; c < info->clip_list_count; c++)
		info->clip_list[c] = region.RectAtInt(c);
	fRenderer->DirectConnected(info);
	free(info);
}


//---- virtual reserved methods ----------


void BGLView::_ReservedGLView1() {}
void BGLView::_ReservedGLView2() {}
void BGLView::_ReservedGLView3() {}
void BGLView::_ReservedGLView4() {}
void BGLView::_ReservedGLView5() {}
void BGLView::_ReservedGLView6() {}
void BGLView::_ReservedGLView7() {}
void BGLView::_ReservedGLView8() {}


// #pragma mark -


// BeOS compatibility: contrary to others BView's contructors,
// BGLView one wants a non-const name argument.
BGLView::BGLView(BRect rect, char* name, ulong resizingMode, ulong mode,
	ulong options)
	:
	BView(rect, name, B_FOLLOW_ALL_SIDES, mode | B_WILL_DRAW | B_FRAME_EVENTS),
	fGc(NULL),
	fOptions(options),
	fDitherCount(0),
	fDrawLock("BGLView draw lock"),
	fDisplayLock("BGLView display lock"),
	fClipInfo(NULL),
	fRenderer(NULL),
	fRoster(NULL),
	fDitherMap(NULL)
{
	fRoster = new GLRendererRoster(this, options);
}


#if 0
// TODO: implement BGLScreen class...


BGLScreen::BGLScreen(char* name, ulong screenMode, ulong options,
		status_t* error, bool debug)
	:
	BWindowScreen(name, screenMode, error, debug)
{
}


BGLScreen::~BGLScreen()
{
}


void
BGLScreen::LockGL()
{
}


void
BGLScreen::UnlockGL()
{
}


void
BGLScreen::SwapBuffers()
{
}


void
BGLScreen::ErrorCallback(unsigned long errorCode)
{
	// Mesa's GLenum is not ulong but uint!
	char msg[32];
	sprintf(msg, "GL: Error code $%04lx.", errorCode);
	// debugger(msg);
	fprintf(stderr, "%s\n", msg);
	return;
}


void
BGLScreen::ScreenConnected(bool enabled)
{
}


void
BGLScreen::FrameResized(float width, float height)
{
	return BWindowScreen::FrameResized(width, height);
}


status_t
BGLScreen::Perform(perform_code d, void* arg)
{
	return BWindowScreen::Perform(d, arg);
}


status_t
BGLScreen::Archive(BMessage* data, bool deep) const
{
	return BWindowScreen::Archive(data, deep);
}


void
BGLScreen::MessageReceived(BMessage* msg)
{
	BWindowScreen::MessageReceived(msg);
}


void
BGLScreen::Show()
{
	BWindowScreen::Show();
}


void
BGLScreen::Hide()
{
	BWindowScreen::Hide();
}


BHandler*
BGLScreen::ResolveSpecifier(BMessage* msg, int32 index, BMessage* specifier,
	int32 form, const char* property)
{
	return BWindowScreen::ResolveSpecifier(msg, index, specifier,
		form, property);
}


status_t
BGLScreen::GetSupportedSuites(BMessage* data)
{
	return BWindowScreen::GetSupportedSuites(data);
}


//---- virtual reserved methods ----------

void BGLScreen::_ReservedGLScreen1() {}
void BGLScreen::_ReservedGLScreen2() {}
void BGLScreen::_ReservedGLScreen3() {}
void BGLScreen::_ReservedGLScreen4() {}
void BGLScreen::_ReservedGLScreen5() {}
void BGLScreen::_ReservedGLScreen6() {}
void BGLScreen::_ReservedGLScreen7() {}
void BGLScreen::_ReservedGLScreen8() {}
#endif


const char* color_space_name(color_space space)
{
#define C2N(a)	case a:	return #a

	switch (space) {
	C2N(B_RGB24);
	C2N(B_RGB32);
	C2N(B_RGBA32);
	C2N(B_RGB32_BIG);
	C2N(B_RGBA32_BIG);
	C2N(B_GRAY8);
	C2N(B_GRAY1);
	C2N(B_RGB16);
	C2N(B_RGB15);
	C2N(B_RGBA15);
	C2N(B_CMAP8);
	default:
		return "Unknown!";
	};

#undef C2N
};


glview_direct_info::glview_direct_info()
{
	// TODO: See direct_window_data() in app_server's ServerWindow.cpp
	direct_info = (direct_buffer_info*)calloc(1, DIRECT_BUFFER_INFO_AREA_SIZE);
	direct_connected = false;
	enable_direct_mode = false;
}


glview_direct_info::~glview_direct_info()
{
	free(direct_info);
}