aboutsummaryrefslogtreecommitdiff
path: root/mesalib/src/mesa/drivers/haiku/swrast/SoftwareRast.cpp
blob: 813ad1ff27dc63be7628666477ec83d81d18143d (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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
 * 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, kallisti5@unixzen.com
 */


#include <kernel/image.h>
#include "SoftwareRast.h"

#include <Autolock.h>
#include <interface/DirectWindowPrivate.h>
#include <GraphicsDefs.h>
#include <Screen.h>
#include <stdio.h>
#include <string.h>

extern "C" {
#include "extensions.h"
#include "drivers/common/driverfuncs.h"
#include "drivers/common/meta.h"
#include "main/api_exec.h"
#include "main/colormac.h"
#include "main/cpuinfo.h"
#include "main/buffers.h"
#include "main/formats.h"
#include "main/framebuffer.h"
#include "main/renderbuffer.h"
#include "main/version.h"
#include "main/vtxfmt.h"
#include "swrast/swrast.h"
#include "swrast/s_renderbuffer.h"
#include "swrast_setup/swrast_setup.h"
#include "tnl/tnl.h"
#include "tnl/t_context.h"
#include "tnl/t_pipeline.h"
#include "vbo/vbo.h"


#ifdef DEBUG
#	define TRACE(x...) printf("MesaSoftwareRast: " x)
#	define CALLED() printf("MesaSoftwareRast: %s\n", __PRETTY_FUNCTION__)
#else
#	define TRACE(x...)
#	define CALLED()
#endif

#define ERROR(x...) printf("MesaSoftwareRast: " x)
}


extern const char* color_space_name(color_space space);


extern "C" _EXPORT BGLRenderer*
instantiate_gl_renderer(BGLView* view, ulong options,
	BGLDispatcher* dispatcher)
{
	return new MesaSoftwareRast(view, options, dispatcher);
}


MesaSoftwareRast::MesaSoftwareRast(BGLView* view, ulong options,
	BGLDispatcher* dispatcher)
	: BGLRenderer(view, options, dispatcher),
	fBitmap(NULL),
	fDirectModeEnabled(false),
	fInfo(NULL),
	fInfoLocker("info locker"),
	fVisual(NULL),
	fFrameBuffer(NULL),
	fFrontRenderBuffer(NULL),
	fBackRenderBuffer(NULL),
	fColorSpace(B_NO_COLOR_SPACE)
{
	CALLED();

	fColorSpace = BScreen(GLView()->Window()).ColorSpace();

	// We force single buffering for the time being
	options &= ~BGL_DOUBLE;

	const GLboolean rgbFlag = ((options & BGL_INDEX) == 0);
	const GLboolean alphaFlag = ((options & BGL_ALPHA) == BGL_ALPHA);
	const GLboolean dblFlag = ((options & BGL_DOUBLE) == BGL_DOUBLE);
	const GLboolean stereoFlag = false;
	const GLint depth = (options & BGL_DEPTH) ? 16 : 0;
	const GLint stencil = (options & BGL_STENCIL) ? 8 : 0;
	const GLint accum = (options & BGL_ACCUM) ? 16 : 0;
	const GLint red = rgbFlag ? 8 : 0;
	const GLint green = rgbFlag ? 8 : 0;
	const GLint blue = rgbFlag ? 8 : 0;
	const GLint alpha = alphaFlag ? 8 : 0;

	fOptions = options; // | BGL_INDIRECT;
	struct dd_function_table functions;

	fVisual = _mesa_create_visual(dblFlag, stereoFlag, red, green,
		blue, alpha, depth, stencil, accum, accum, accum,
		alpha ? accum : 0, 1);

	// Initialize device driver function table
	_mesa_init_driver_functions(&functions);

	functions.GetString = _GetString;
	functions.UpdateState = _UpdateState;
	functions.MapRenderbuffer = _RenderBufferMap;
	functions.Flush = _Flush;

	// create core context
	// We inherit gl_context to this class
	_mesa_initialize_context(this, API_OPENGL_COMPAT, fVisual, NULL,
		&functions);

	/* Initialize the software rasterizer and helper modules. */
	_swrast_CreateContext(this);
	_vbo_CreateContext(this);
	_tnl_CreateContext(this);
	_swsetup_CreateContext(this);
	_swsetup_Wakeup(this);

	// Use default TCL pipeline
	TNL_CONTEXT(this)->Driver.RunPipeline = _tnl_run_pipeline;

	_mesa_meta_init(this);
	_mesa_enable_sw_extensions(this);

	_mesa_compute_version(this);

	_mesa_initialize_dispatch_tables(this);
	_mesa_initialize_vbo_vtxfmt(this);

	// create core framebuffer
	fFrameBuffer = _mesa_create_framebuffer(fVisual);
	if (fFrameBuffer == NULL) {
		ERROR("%s: Unable to calloc GL FrameBuffer!\n", __func__);
		_mesa_destroy_visual(fVisual);
		return;
	}

	// Setup front render buffer
	fFrontRenderBuffer = _NewRenderBuffer(true);
	if (fFrontRenderBuffer == NULL) {
		ERROR("%s: FrontRenderBuffer is requested but unallocated!\n",
			__func__);
		_mesa_destroy_visual(fVisual);
		free(fFrameBuffer);
		return;
	}
	_mesa_add_renderbuffer(fFrameBuffer, BUFFER_FRONT_LEFT,
		&fFrontRenderBuffer->Base);

	// Setup back render buffer (if requested)
	if (fVisual->doubleBufferMode) {
		fBackRenderBuffer = _NewRenderBuffer(false);
		if (fBackRenderBuffer == NULL) {
			ERROR("%s: BackRenderBuffer is requested but unallocated!\n",
				__func__);
			_mesa_destroy_visual(fVisual);
			free(fFrameBuffer);
			return;
		}
		_mesa_add_renderbuffer(fFrameBuffer, BUFFER_BACK_LEFT,
			&fBackRenderBuffer->Base);
	}

	_swrast_add_soft_renderbuffers(fFrameBuffer, GL_FALSE,
		fVisual->haveDepthBuffer, fVisual->haveStencilBuffer,
		fVisual->haveAccumBuffer, alphaFlag, GL_FALSE);

	BRect bounds = view->Bounds();
	fWidth = (GLint)bounds.Width();
	fHeight = (GLint)bounds.Height();

	// some stupid applications (Quake2) don't even think about calling LockGL()
	// before using glGetString and its glGet*() friends...
	// so make sure there is at least a valid context.

	if (!_mesa_get_current_context()) {
		LockGL();
		// not needed, we don't have a looper yet: UnlockLooper();
	}
}


MesaSoftwareRast::~MesaSoftwareRast()
{
	CALLED();
	_swsetup_DestroyContext(this);
	_swrast_DestroyContext(this);
	_tnl_DestroyContext(this);
	_vbo_DestroyContext(this);
	_mesa_destroy_visual(fVisual);
	_mesa_destroy_framebuffer(fFrameBuffer);
	_mesa_destroy_context(this);

	free(fInfo);
	free(fFrameBuffer);

	delete fBitmap;
}


void
MesaSoftwareRast::LockGL()
{
	CALLED();
	BGLRenderer::LockGL();

	_mesa_make_current(this, fFrameBuffer, fFrameBuffer);

	color_space colorSpace = BScreen(GLView()->Window()).ColorSpace();

	GLuint width = fWidth;
	GLuint height = fHeight;

	BAutolock lock(fInfoLocker);
	if (fDirectModeEnabled && fInfo != NULL) {
		width = fInfo->window_bounds.right
			- fInfo->window_bounds.left + 1;
		height = fInfo->window_bounds.bottom
			- fInfo->window_bounds.top + 1;
	}

	if (fColorSpace != colorSpace) {
		fColorSpace = colorSpace;
		_SetupRenderBuffer(&fFrontRenderBuffer->Base, fColorSpace);
		if (fVisual->doubleBufferMode)
			_SetupRenderBuffer(&fBackRenderBuffer->Base, fColorSpace);
	}

	_CheckResize(width, height);
}


void
MesaSoftwareRast::UnlockGL()
{
	CALLED();
	_mesa_make_current(this, NULL, NULL);
	BGLRenderer::UnlockGL();
}


void
MesaSoftwareRast::SwapBuffers(bool VSync)
{
	CALLED();

	if (!fBitmap)
		return;

	if (fVisual->doubleBufferMode)
		_mesa_notifySwapBuffers(this);

	if (!fDirectModeEnabled || fInfo == NULL) {
		if (GLView()->LockLooperWithTimeout(1000) == B_OK) {
			GLView()->DrawBitmap(fBitmap, B_ORIGIN);
			GLView()->UnlockLooper();
		}
	} else {
		// TODO: Here the BGLView needs to be drawlocked.
		_CopyToDirect();
	}

	if (VSync) {
		BScreen screen(GLView()->Window());
		screen.WaitForRetrace();
	}
}


void
MesaSoftwareRast::Draw(BRect updateRect)
{
	CALLED();
	if (fBitmap && (!fDirectModeEnabled || (fInfo == NULL)))
		GLView()->DrawBitmap(fBitmap, updateRect, updateRect);
}


status_t
MesaSoftwareRast::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)) {
		fprintf(stderr, "CopyPixelsOut(): incompatible color space: %s != %s\n",
			color_space_name(scs),
			color_space_name(dcs));
		return B_BAD_TYPE;
	}

	BRect sr = fBitmap->Bounds();
	BRect dr = bitmap->Bounds();

	sr = sr & dr.OffsetBySelf(location);
	dr = sr.OffsetByCopy(-location.x, -location.y);

	uint8* ps = (uint8*)fBitmap->Bits();
	uint8* pd = (uint8*)bitmap->Bits();
	uint32* s;
	uint32* 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
MesaSoftwareRast::CopyPixelsIn(BBitmap* bitmap, BPoint location)
{
	CALLED();
	color_space scs = bitmap->ColorSpace();
	color_space dcs = fBitmap->ColorSpace();

	if (scs != dcs && (dcs != B_RGBA32 || scs != B_RGB32)) {
		fprintf(stderr, "CopyPixelsIn(): incompatible color space: %s != %s\n",
			color_space_name(scs),
			color_space_name(dcs));
		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;
	uint32* 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
MesaSoftwareRast::EnableDirectMode(bool enabled)
{
	fDirectModeEnabled = enabled;
}


void
MesaSoftwareRast::DirectConnected(direct_buffer_info* info)
{
	// TODO: I'm not sure we need to do this: BGLView already
	// keeps a local copy of the direct_buffer_info passed by
	// BDirectWindow::DirectConnected().
	BAutolock lock(fInfoLocker);
	if (info) {
		if (!fInfo) {
			fInfo = (direct_buffer_info*)malloc(DIRECT_BUFFER_INFO_AREA_SIZE);
			if (!fInfo)
				return;
		}
		memcpy(fInfo, info, DIRECT_BUFFER_INFO_AREA_SIZE);
	} else if (fInfo) {
		free(fInfo);
		fInfo = NULL;
	}
}


void
MesaSoftwareRast::FrameResized(float width, float height)
{
	BAutolock lock(fInfoLocker);
	_CheckResize((GLuint)width, (GLuint)height);
}


void
MesaSoftwareRast::_CheckResize(GLuint newWidth, GLuint newHeight)
{
	CALLED();

	if (fBitmap && newWidth == fWidth
		&& newHeight == fHeight) {
		return;
	}

	_mesa_resize_framebuffer(this, fFrameBuffer, newWidth, newHeight);
	fHeight = newHeight;
	fWidth = newWidth;

	_AllocateBitmap();
}


void
MesaSoftwareRast::_AllocateBitmap()
{
	CALLED();

	// allocate new size of back buffer bitmap
	delete fBitmap;
	fBitmap = NULL;

	if (fWidth < 1 || fHeight < 1) {
		TRACE("%s: Cannot allocate bitmap < 1x1!\n", __func__);
		return;
	}

	BRect rect(0.0, 0.0, fWidth - 1, fHeight - 1);
	fBitmap = new BBitmap(rect, fColorSpace);

	#if 0
	// Used for platform optimized drawing
	for (uint i = 0; i < fHeight; i++) {
		fRowAddr[fHeight - i - 1] = (GLvoid *)((GLubyte *)fBitmap->Bits()
			+ i * fBitmap->BytesPerRow());
	}
	#endif

	fFrameBuffer->Width = fWidth;
	fFrameBuffer->Height = fHeight;
	TRACE("%s: Bitmap Size: %" B_PRIu32 "\n", __func__, fBitmap->BitsLength());

	fFrontRenderBuffer->Buffer = (GLubyte*)fBitmap->Bits();
}


// #pragma mark - static


const GLubyte*
MesaSoftwareRast::_GetString(gl_context* ctx, GLenum name)
{
	switch (name) {
		case GL_VENDOR:
			return (const GLubyte*) "Mesa Project";
		case GL_RENDERER:
			return (const GLubyte*) "Software Rasterizer";
		default:
			// Let core library handle all other cases
			return NULL;
	}
}


void
MesaSoftwareRast::_UpdateState(gl_context* ctx, GLuint new_state)
{
	if (!ctx)
		return;

	CALLED();
	_swrast_InvalidateState(ctx, new_state);
	_swsetup_InvalidateState(ctx, new_state);
	_vbo_InvalidateState(ctx, new_state);
	_tnl_InvalidateState(ctx, new_state);
}


GLboolean
MesaSoftwareRast::_RenderBufferStorage(gl_context* ctx,
	struct gl_renderbuffer* render, GLenum internalFormat,
	GLuint width, GLuint height)
{
	CALLED();

	render->Width = width;
	render->Height = height;

	struct swrast_renderbuffer *swRenderBuffer = swrast_renderbuffer(render);

	swRenderBuffer->RowStride = width * _mesa_get_format_bytes(render->Format);

	return GL_TRUE;
}


GLboolean
MesaSoftwareRast::_RenderBufferStorageMalloc(gl_context* ctx,
	struct gl_renderbuffer* render, GLenum internalFormat,
	GLuint width, GLuint height)
{
	CALLED();

	render->Width = width;
	render->Height = height;

	struct swrast_renderbuffer *swRenderBuffer = swrast_renderbuffer(render);

	if (swRenderBuffer != NULL) {
		free(swRenderBuffer->Buffer);
		swRenderBuffer->RowStride
			= width * _mesa_get_format_bytes(render->Format);

		uint32 size = swRenderBuffer->RowStride * height;
		TRACE("%s: Allocate %" B_PRIu32 " bytes for RenderBuffer\n",
			__func__, size);
		swRenderBuffer->Buffer = (GLubyte*)malloc(size);
		if (!swRenderBuffer->Buffer) {
			ERROR("%s: Memory allocation failure!\n", __func__);
			return GL_FALSE;
		}
	} else {
		ERROR("%s: Couldn't obtain software renderbuffer!\n",
			__func__);
		return GL_FALSE;
	}

	return GL_TRUE;
}


void
MesaSoftwareRast::_Flush(gl_context* ctx)
{
	CALLED();
	MesaSoftwareRast* driverContext = static_cast<MesaSoftwareRast*>(ctx);

	//MesaSoftwareRast* driverContext = (MesaSoftwareRast*)ctx->DriverCtx;
	if ((driverContext->fOptions & BGL_DOUBLE) == 0) {
		// TODO: SwapBuffers() can call _CopyToDirect(), which should
		// be always called with with the BGLView drawlocked.
		// This is not always the case if called from here.
		driverContext->SwapBuffers();
	}
}


struct swrast_renderbuffer*
MesaSoftwareRast::_NewRenderBuffer(bool front)
{
	CALLED();
	struct swrast_renderbuffer *swRenderBuffer
		= (struct swrast_renderbuffer*)calloc(1, sizeof *swRenderBuffer);

	if (!swRenderBuffer) {
		ERROR("%s: Failed calloc RenderBuffer\n", __func__);
		return NULL;
	}

	_mesa_init_renderbuffer(&swRenderBuffer->Base, 0);

	swRenderBuffer->Base.ClassID = HAIKU_SWRAST_RENDERBUFFER_CLASS;
	swRenderBuffer->Base.RefCount = 1;
	swRenderBuffer->Base.Delete = _RenderBufferDelete;

	if (!front)
		swRenderBuffer->Base.AllocStorage = _RenderBufferStorageMalloc;
	else
		swRenderBuffer->Base.AllocStorage = _RenderBufferStorage;

	if (_SetupRenderBuffer(&swRenderBuffer->Base, fColorSpace) != B_OK) {
		free(swRenderBuffer);
		return NULL;
	}

	return swRenderBuffer;
}


status_t
MesaSoftwareRast::_SetupRenderBuffer(struct gl_renderbuffer* rb,
	color_space colorSpace)
{
	CALLED();

	rb->InternalFormat = GL_RGBA;

	switch (colorSpace) {
		case B_RGBA32:
			rb->_BaseFormat = GL_RGBA;
			rb->Format = MESA_FORMAT_B8G8R8A8_UNORM;
			break;
		case B_RGB32:
			rb->_BaseFormat = GL_RGB;
			rb->Format = MESA_FORMAT_B8G8R8X8_UNORM;
			break;
		case B_RGB24:
			rb->_BaseFormat = GL_RGB;
			rb->Format = MESA_FORMAT_BGR_UNORM8;
			break;
		case B_RGB16:
			rb->_BaseFormat = GL_RGB;
			rb->Format = MESA_FORMAT_B5G6R5_UNORM;
			break;
		case B_RGB15:
			rb->_BaseFormat = GL_RGB;
			rb->Format = MESA_FORMAT_B5G5R5A1_UNORM;
			break;
		default:
			fprintf(stderr, "Unsupported screen color space %s\n",
				color_space_name(fColorSpace));
			debugger("Unsupported OpenGL color space");
			return B_ERROR;
	}
	return B_OK;
}


/*!	Y inverted Map RenderBuffer function
	We use a BBitmap for storage which has Y inverted.
	If the Mesa provided Map function ever allows external
	control of this we can omit this function.
*/
void
MesaSoftwareRast::_RenderBufferMap(gl_context *ctx,
	struct gl_renderbuffer *rb, GLuint x, GLuint y, GLuint w, GLuint h,
	GLbitfield mode, GLubyte **mapOut, GLint *rowStrideOut)
{
	if (rb->ClassID == HAIKU_SWRAST_RENDERBUFFER_CLASS) {
		struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
		const GLuint bpp = _mesa_get_format_bytes(rb->Format);
		GLint rowStride = rb->Width * bpp; // in Bytes

		y = rb->Height - y - 1;

		*rowStrideOut = -rowStride;
		*mapOut = (GLubyte *) srb->Buffer + y * rowStride + x * bpp;
	} else {
		_swrast_map_soft_renderbuffer(ctx, rb, x, y, w, h, mode,
			mapOut, rowStrideOut);
	}
}


void
MesaSoftwareRast::_RenderBufferDelete(struct gl_context *ctx,
	struct gl_renderbuffer* rb)
{
	CALLED();
	if (rb != NULL) {
		struct swrast_renderbuffer *swRenderBuffer
			= swrast_renderbuffer(rb);
		if (swRenderBuffer != NULL)
			free(swRenderBuffer->Buffer);
	}
	free(rb);
}


void
MesaSoftwareRast::_CopyToDirect()
{
	BAutolock lock(fInfoLocker);

	// 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())
		return;

	uint8 bytesPerPixel = fInfo->bits_per_pixel / 8;
	uint32 bytesPerRow = fBitmap->BytesPerRow();
	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;
		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; y++) {
			memcpy(p, b, bytesWidth);
			p += fInfo->bytes_per_row;
			b += bytesPerRow;
		}
	}
}