2 * Copyright 2006-2008, Haiku. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Philippe Houdoin, philippe.houdoin@free.fr
9 * Mesa 3-D graphics library
12 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
15 #include "FakeRenderer.h"
20 #include <DirectWindowPrivate.h>
21 #include <GraphicsDefs.h>
25 extern const char * color_space_name(color_space space
);
28 extern "C" _EXPORT BGLRenderer
*
29 instantiate_gl_renderer(BGLView
* view
, ulong options
,
30 BGLDispatcher
* dispatcher
)
32 return new FakeRenderer(view
, options
, dispatcher
);
36 FakeRenderer::FakeRenderer(BGLView
* view
, ulong options
,
37 BGLDispatcher
* dispatcher
)
38 : BGLRenderer(view
, options
, dispatcher
),
40 fDrawLocker("direct draw locker"),
42 fColorSpace(B_NO_COLOR_SPACE
),
47 fDrawSem
= create_sem(0, "FakeRenderer draw");
48 fDrawThread
= spawn_thread(_DirectDrawThread
, "FakeRenderer direct draw", B_DISPLAY_PRIORITY
, this);
49 resume_thread(fDrawThread
);
53 FakeRenderer::~FakeRenderer()
55 // Wakeup the draw thread by murdering its favorite semaphore
59 wait_for_thread(fDrawThread
, &exit_value
);
66 FakeRenderer::SwapBuffers(bool VSync
)
68 if (VSync
&& GLView()->Window()) {
69 // TODO: find a way to check VSync support is actually working...
70 BScreen
screen(GLView()->Window());
71 screen
.WaitForRetrace();
74 // Simulate rendering a new buffer: randomized buffer color ;-)
75 fDrawColor
= make_color(rand() % 0xFF, rand() % 0xFF, rand() % 0xFF);
77 if (!fConnected
|| fDisabled
) {
78 GLView()->LockLooper();
79 // TODO : refresh area
80 GLView()->UnlockLooper();
84 // Direct mode: wake up drawing thread
85 release_sem(fDrawSem
);
90 FakeRenderer::Draw(BRect updateRect
)
93 if (fBitmap && (!fDirectModeEnabled || (fInfo == NULL)))
94 GLView()->DrawBitmap(fBitmap, updateRect, updateRect);
100 FakeRenderer::EnableDirectMode(bool enabled
)
102 fDisabled
= ! enabled
;
107 FakeRenderer::DirectConnected(direct_buffer_info
*info
)
109 if (!fConnected
&& fDisabled
)
112 BAutolock
lock(fDrawLocker
);
114 switch (info
->buffer_state
& B_DIRECT_MODE_MASK
) {
118 case B_DIRECT_MODIFY
:
119 fFrameBuffer
= (uint8
*) info
->bits
;
120 fBytesPerRow
= info
->bytes_per_row
;
121 fColorSpace
= info
->pixel_format
;
124 fRectsCount
= info
->clip_list_count
;
125 fRects
= (clipping_rect
*) malloc(fRectsCount
* sizeof(clipping_rect
));
126 memcpy(fRects
, info
->clip_list
, fRectsCount
* sizeof(clipping_rect
));
128 fprintf(stderr
, "fFrameBuffer = %p\n"
129 "fBytesPerRow = %d\n"
130 "fColorSpace = %s\n", fFrameBuffer
, fBytesPerRow
, color_space_name(fColorSpace
));
131 for (int i
= 0; i
< fRectsCount
; i
++) {
132 fprintf(stderr
, "fRects[%d] = %d, %d to %d, %d\n",
133 i
, fRects
[i
].left
, fRects
[i
].top
, fRects
[i
].right
, fRects
[i
].bottom
);
142 fprintf(stderr
, "fConnected = %s\n", fConnected
? "true" : "false");
150 FakeRenderer::_DirectDrawThread(void *data
)
152 FakeRenderer
*me
= (FakeRenderer
*) data
;
153 return me
->_DirectDrawThread();
158 FakeRenderer::_DirectDrawThread(void)
160 // Let's wait forever/until semaphore death next redraw
161 while (acquire_sem(fDrawSem
) == B_OK
) {
163 BAutolock
lock(fDrawLocker
);
168 switch(fColorSpace
) {
171 for (i
= 0; i
< fRectsCount
; i
++) {
172 for (y
= fRects
[i
].top
; y
<= fRects
[i
].bottom
; y
++) {
173 uint8
* p
= fFrameBuffer
+ ( y
* fBytesPerRow
) + fRects
[i
].left
* 4;
174 for (x
= fRects
[i
].left
; x
<= fRects
[i
].right
; x
++) {
175 *p
++ = fDrawColor
.blue
;
176 *p
++ = fDrawColor
.green
;
177 *p
++ = fDrawColor
.red
;
178 *p
++ = fDrawColor
.alpha
;
188 /* fill this in with the color-space conversion
189 * code of your choosing
207 /* unsupported mode */