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
|
/*
* Copyright 2006-2012, Haiku, Inc. 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
* Artur Wyszynski, harakash@gmail.com
* Alexander von Gluck IV, kallisti5@unixzen.com
*/
#include "SoftwareRenderer.h"
#include <Autolock.h>
#include <interface/DirectWindowPrivate.h>
#include <GraphicsDefs.h>
#include <Screen.h>
#include <stdio.h>
#include <sys/time.h>
#include <new>
#ifdef DEBUG
# define TRACE(x...) printf("SoftwareRenderer: " x)
# define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
#else
# define TRACE(x...)
# define CALLED()
#endif
#define ERROR(x...) printf("SoftwareRenderer: " x)
extern const char* color_space_name(color_space space);
extern "C" _EXPORT BGLRenderer*
instantiate_gl_renderer(BGLView *view, ulong opts, BGLDispatcher *dispatcher)
{
return new SoftwareRenderer(view, opts, dispatcher);
}
SoftwareRenderer::SoftwareRenderer(BGLView *view, ulong options,
BGLDispatcher* dispatcher)
:
BGLRenderer(view, options, dispatcher),
fBitmap(NULL),
fDirectModeEnabled(false),
fInfo(NULL),
fInfoLocker("info locker"),
fOptions(options),
fColorSpace(B_NO_COLOR_SPACE)
{
CALLED();
// Disable double buffer for the moment.
options &= ~BGL_DOUBLE;
// Initialize the "Haiku Software GL Pipe"
time_t beg;
time_t end;
beg = time(NULL);
fContextObj = new GalliumContext(options);
end = time(NULL);
TRACE("Haiku Software GL Pipe initialization time: %f.\n",
difftime(end, beg));
// Allocate a bitmap
BRect b = view->Bounds();
fColorSpace = BScreen(view->Window()).ColorSpace();
TRACE("%s: Colorspace:\t%s\n", __func__, color_space_name(fColorSpace));
fWidth = (GLint)b.IntegerWidth();
fHeight = (GLint)b.IntegerHeight();
fNewWidth = fWidth;
fNewHeight = fHeight;
_AllocateBitmap();
// Initialize the first "Haiku Software GL Pipe" context
beg = time(NULL);
fContextID = fContextObj->CreateContext(fBitmap);
end = time(NULL);
if (fContextID < 0)
ERROR("%s: There was an error creating the context!\n", __func__);
else {
TRACE("%s: Haiku Software GL Pipe context creation time: %f.\n",
__func__, difftime(end, beg));
}
if (!fContextObj->GetCurrentContext())
LockGL();
}
SoftwareRenderer::~SoftwareRenderer()
{
CALLED();
if (fContextObj)
delete fContextObj;
if (fBitmap)
delete fBitmap;
}
void
SoftwareRenderer::LockGL()
{
// CALLED();
BGLRenderer::LockGL();
color_space cs = BScreen(GLView()->Window()).ColorSpace();
BAutolock lock(fInfoLocker);
if (fDirectModeEnabled && fInfo != NULL) {
fNewWidth = fInfo->window_bounds.right - fInfo->window_bounds.left;
fNewHeight = fInfo->window_bounds.bottom - fInfo->window_bounds.top;
}
if (fBitmap && cs == fColorSpace && fNewWidth == fWidth
&& fNewHeight == fHeight) {
fContextObj->SetCurrentContext(fBitmap, fContextID);
return;
}
fColorSpace = cs;
fWidth = fNewWidth;
fHeight = fNewHeight;
_AllocateBitmap();
fContextObj->SetCurrentContext(fBitmap, fContextID);
}
void
SoftwareRenderer::UnlockGL()
{
// CALLED();
if ((fOptions & BGL_DOUBLE) == 0) {
SwapBuffers();
}
fContextObj->SetCurrentContext(NULL, fContextID);
BGLRenderer::UnlockGL();
}
void
SoftwareRenderer::SwapBuffers(bool vsync)
{
// CALLED();
if (!fBitmap)
return;
BScreen screen(GLView()->Window());
fContextObj->SwapBuffers(fContextID);
BAutolock lock(fInfoLocker);
if (!fDirectModeEnabled || fInfo == NULL) {
if (GLView()->LockLooperWithTimeout(1000) == B_OK) {
GLView()->DrawBitmap(fBitmap, B_ORIGIN);
GLView()->UnlockLooper();
if (vsync)
screen.WaitForRetrace();
}
return;
}
// check the bitmap size still matches the size
if (fInfo->window_bounds.bottom - fInfo->window_bounds.top
!= fBitmap->Bounds().IntegerHeight()
|| fInfo->window_bounds.right - fInfo->window_bounds.left
!= fBitmap->Bounds().IntegerWidth()) {
ERROR("%s: Bitmap size doesn't match size!\n", __func__);
return;
}
uint32 bytesPerRow = fBitmap->BytesPerRow();
uint8 bytesPerPixel = bytesPerRow / fBitmap->Bounds().IntegerWidth();
for (uint32 i = 0; i < fInfo->clip_list_count; i++) {
clipping_rect *clip = &fInfo->clip_list[i];
int32 height = clip->bottom - clip->top + 1;
int32 bytesWidth
= (clip->right - clip->left + 1) * bytesPerPixel;
bytesWidth -= bytesPerPixel;
uint8 *p = (uint8 *)fInfo->bits + clip->top
* fInfo->bytes_per_row + clip->left * bytesPerPixel;
uint8 *b = (uint8 *)fBitmap->Bits()
+ (clip->top - fInfo->window_bounds.top) * bytesPerRow
+ (clip->left - fInfo->window_bounds.left) * bytesPerPixel;
for (int y = 0; y < height - 1; y++) {
memcpy(p, b, bytesWidth);
p += fInfo->bytes_per_row;
b += bytesPerRow;
}
}
if (vsync)
screen.WaitForRetrace();
}
void
SoftwareRenderer::Draw(BRect updateRect)
{
// CALLED();
if ((!fDirectModeEnabled || fInfo == NULL) && fBitmap)
GLView()->DrawBitmap(fBitmap, updateRect, updateRect);
}
status_t
SoftwareRenderer::CopyPixelsOut(BPoint location, BBitmap *bitmap)
{
CALLED();
color_space scs = fBitmap->ColorSpace();
color_space dcs = bitmap->ColorSpace();
if (scs != dcs && (scs != B_RGBA32 || dcs != B_RGB32)) {
ERROR("%s::CopyPixelsOut(): incompatible color space: %s != %s\n",
__PRETTY_FUNCTION__, color_space_name(scs), color_space_name(dcs));
return B_BAD_TYPE;
}
BRect sr = fBitmap->Bounds();
BRect dr = bitmap->Bounds();
// int32 w1 = sr.IntegerWidth();
// int32 h1 = sr.IntegerHeight();
// int32 w2 = dr.IntegerWidth();
// int32 h2 = dr.IntegerHeight();
sr = sr & dr.OffsetBySelf(location);
dr = sr.OffsetByCopy(-location.x, -location.y);
uint8 *ps = (uint8 *) fBitmap->Bits();
uint8 *pd = (uint8 *) bitmap->Bits();
uint32 *s, *d;
uint32 y;
for (y = (uint32) sr.top; y <= (uint32) sr.bottom; y++) {
s = (uint32 *)(ps + y * fBitmap->BytesPerRow());
s += (uint32) sr.left;
d = (uint32 *)(pd + (y + (uint32)(dr.top - sr.top))
* bitmap->BytesPerRow());
d += (uint32) dr.left;
memcpy(d, s, dr.IntegerWidth() * 4);
}
return B_OK;
}
status_t
SoftwareRenderer::CopyPixelsIn(BBitmap *bitmap, BPoint location)
{
CALLED();
color_space sourceCS = bitmap->ColorSpace();
color_space destinationCS = fBitmap->ColorSpace();
if (sourceCS != destinationCS
&& (sourceCS != B_RGB32 || destinationCS != B_RGBA32)) {
ERROR("%s::CopyPixelsIn(): incompatible color space: %s != %s\n",
__PRETTY_FUNCTION__, color_space_name(sourceCS),
color_space_name(destinationCS));
return B_BAD_TYPE;
}
BRect sr = bitmap->Bounds();
BRect dr = fBitmap->Bounds();
sr = sr & dr.OffsetBySelf(location);
dr = sr.OffsetByCopy(-location.x, -location.y);
uint8 *ps = (uint8 *) bitmap->Bits();
uint8 *pd = (uint8 *) fBitmap->Bits();
uint32 *s, *d;
uint32 y;
for (y = (uint32) sr.top; y <= (uint32) sr.bottom; y++) {
s = (uint32 *)(ps + y * bitmap->BytesPerRow());
s += (uint32) sr.left;
d = (uint32 *)(pd + (y + (uint32)(dr.top - sr.top))
* fBitmap->BytesPerRow());
d += (uint32) dr.left;
memcpy(d, s, dr.IntegerWidth() * 4);
}
return B_OK;
}
void
SoftwareRenderer::EnableDirectMode(bool enabled)
{
fDirectModeEnabled = enabled;
}
void
SoftwareRenderer::DirectConnected(direct_buffer_info *info)
{
// CALLED();
BAutolock lock(fInfoLocker);
if (info) {
if (!fInfo) {
fInfo = (direct_buffer_info *)calloc(1,
DIRECT_BUFFER_INFO_AREA_SIZE);
}
memcpy(fInfo, info, DIRECT_BUFFER_INFO_AREA_SIZE);
} else if (fInfo) {
free(fInfo);
fInfo = NULL;
}
}
void
SoftwareRenderer::FrameResized(float width, float height)
{
TRACE("%s: %f x %f\n", __func__, width, height);
BAutolock lock(fInfoLocker);
fNewWidth = (GLuint)width;
fNewHeight = (GLuint)height;
}
void
SoftwareRenderer::_AllocateBitmap()
{
// CALLED();
// allocate new size of back buffer bitmap
BAutolock lock(fInfoLocker);
delete fBitmap;
fBitmap = NULL;
if (fWidth < 1 || fHeight < 1) {
TRACE("%s: Can't allocate bitmap of %dx%d\n", __func__,
fWidth, fHeight);
return;
}
BRect rect(0.0, 0.0, fWidth, fHeight);
fBitmap = new (std::nothrow) BBitmap(rect, fColorSpace);
if (fBitmap == NULL) {
TRACE("%s: Can't create bitmap!\n", __func__);
return;
}
TRACE("%s: New bitmap size: %ld x %ld\n", __func__,
fBitmap->Bounds().IntegerWidth(), fBitmap->Bounds().IntegerHeight());
fContextObj->ResizeViewport(fWidth, fHeight);
#if 0
// debug..
void *data = fBitmap->Bits();
memset(data, 0xcc, fBitmap->BitsLength());
#endif
}
|