6 * - Common Driver/Filesystem Helper Functions
10 #include <api_drv_disk.h>
11 #include <api_drv_video.h>
16 //int DrvUtil_Video_2DStream(void *Ent, void *Buffer, int Length, tDrvUtil_Video_2DHandlers *Handlers, int SizeofHandlers);
17 //size_t DrvUtil_Video_WriteLFB(int Mode, tDrvUtil_Video_BufInfo *FBInfo, size_t Offset, size_t Length, void *Src);
18 //void DrvUtil_Video_SetCursor(tDrvUtil_Video_BufInfo *Buf, tVideo_IOCtl_Bitmap *Bitmap);
19 //void DrvUtil_Video_DrawCursor(tDrvUtil_Video_BufInfo *Buf, int X, int Y);
20 void DrvUtil_Video_RenderCursor(tDrvUtil_Video_BufInfo *Buf);
21 //void DrvUtil_Video_RemoveCursor(tDrvUtil_Video_BufInfo *Buf);
22 void DrvUtil_Video_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour);
23 void DrvUtil_Video_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H);
26 tDrvUtil_Video_2DHandlers gDrvUtil_Stub_2DFunctions = {
28 DrvUtil_Video_2D_Fill,
31 tVideo_IOCtl_Bitmap gDrvUtil_TextModeCursor = {
35 0, 0, 0 , 0, 0, 0, 0, 0,
36 0,-1, 0xFF000000, 0, 0, 0, 0, 0,
37 0,-1, 0xFF000000, 0, 0, 0, 0, 0,
38 0,-1, 0xFF000000, 0, 0, 0, 0, 0,
39 0,-1, 0xFF000000, 0, 0, 0, 0, 0,
40 0,-1, 0xFF000000, 0, 0, 0, 0, 0,
41 0,-1, 0xFF000000, 0, 0, 0, 0, 0,
42 0,-1, 0xFF000000, 0, 0, 0, 0, 0,
43 0,-1, 0xFF000000, 0, 0, 0, 0, 0,
44 0,-1, 0xFF000000, 0, 0, 0, 0, 0,
45 0,-1, 0xFF000000, 0, 0, 0, 0, 0,
46 0,-1, 0xFF000000, 0, 0, 0, 0, 0,
47 0,-1, 0xFF000000, 0, 0, 0, 0, 0,
48 0,-1, 0xFF000000, 0, 0, 0, 0, 0,
49 0, 0, 0 , 0, 0, 0, 0, 0,
50 0, 0, 0 , 0, 0, 0, 0, 0
55 // --- Video Driver Helpers ---
56 int DrvUtil_Video_2DStream(void *Ent, const void *Buffer, int Length,
57 tDrvUtil_Video_2DHandlers *Handlers, int SizeofHandlers)
59 const Uint8 *stream = Buffer;
68 if(op > NUM_VIDEO_2DOPS) {
69 Log_Warning("DrvUtil",
70 "DrvUtil_Video_2DStream: Unknown operation %i",
75 if(op*sizeof(void*) > SizeofHandlers) {
76 Log_Warning("DrvUtil",
77 "DrvUtil_Video_2DStream: Driver does not support op %i",
84 case VIDEO_2DOP_NOP: break;
87 if(rem < 10) return Length-rem;
90 Log_Warning("DrvUtil", "DrvUtil_Video_2DStream: Driver"
91 " does not support VIDEO_2DOP_FILL");
97 ((const Uint16*)stream)[0], ((const Uint16*)stream)[1],
98 ((const Uint16*)stream)[2], ((const Uint16*)stream)[3],
99 ((const Uint32*)stream)[4]
106 case VIDEO_2DOP_BLIT:
107 if(rem < 12) return Length-rem;
109 if(!Handlers->Blit) {
110 Log_Warning("DrvUtil", "DrvUtil_Video_2DStream: Driver"
111 " does not support VIDEO_2DOP_BLIT");
117 ((const Uint16*)stream)[0], ((const Uint16*)stream)[1],
118 ((const Uint16*)stream)[2], ((const Uint16*)stream)[3],
119 ((const Uint16*)stream)[4], ((const Uint16*)stream)[5]
131 int DrvUtil_Video_WriteLFB(tDrvUtil_Video_BufInfo *FBInfo, size_t Offset, size_t Length, const void *Buffer)
134 const Uint32 *src = Buffer;
137 int bytes_per_px = (FBInfo->Depth + 7) / 8;
138 ENTER("pFBInfo xOffset xLength pBuffer",
139 FBInfo, Offset, Length, Buffer);
141 csr_x = FBInfo->CursorX;
142 csr_y = FBInfo->CursorY;
144 DrvUtil_Video_RemoveCursor(FBInfo);
146 switch( FBInfo->BufferFormat )
148 case VIDEO_BUFFMT_TEXT:
150 const tVT_Char *chars = Buffer;
151 int widthInChars = FBInfo->Width/giVT_CharWidth;
152 int heightInChars = FBInfo->Height/giVT_CharHeight;
155 LOG("bytes_per_px = %i", bytes_per_px);
156 LOG("widthInChars = %i, heightInChars = %i", widthInChars, heightInChars);
158 Length /= sizeof(tVT_Char); Offset /= sizeof(tVT_Char);
160 x = Offset % widthInChars; y = Offset / widthInChars;
161 LOG("x = %i, y = %i", x, y);
164 if(Offset > heightInChars * widthInChars) LEAVE_RET('i', 0);
165 if(y >= heightInChars) LEAVE_RET('i', 0);
167 if( Offset + Length > heightInChars*widthInChars )
169 Length = heightInChars*widthInChars - Offset;
172 dest = FBInfo->Framebuffer;
173 LOG("dest = %p", dest);
174 dest += y * giVT_CharHeight * FBInfo->Pitch;
175 LOG("dest = %p", dest);
177 for( i = 0; i < Length; i++ )
179 if( y >= heightInChars )
181 Log_Notice("DrvUtil", "Stopped at %i", i);
187 dest + x*giVT_CharWidth*bytes_per_px, FBInfo->Depth, FBInfo->Pitch,
188 VT_Colour12toN(chars->BGCol, FBInfo->Depth),
189 VT_Colour12toN(chars->FGCol, FBInfo->Depth)
194 if( x >= widthInChars )
198 dest += FBInfo->Pitch*giVT_CharHeight;
199 LOG("dest = %p", dest);
202 Length = i * sizeof(tVT_Char);
206 case VIDEO_BUFFMT_FRAMEBUFFER:
207 if(FBInfo->Width*FBInfo->Height*4 < Offset+Length)
209 Log_Warning("DrvUtil", "DrvUtil_Video_WriteLFB - Framebuffer Overflow");
213 switch(FBInfo->Depth)
217 Log_Warning("DrvUtil", "TODO: Support 15/16 bpp modes in LFB write");
220 x = Offset % FBInfo->Width;
221 y = Offset / FBInfo->Width;
222 dest = (Uint8*)FBInfo->Framebuffer + y*FBInfo->Pitch;
223 for( ; Length >= 4; Length -= 4 )
225 dest[x*3+0] = *src & 0xFF;
226 dest[x*3+1] = (*src >> 8) & 0xFF;
227 dest[x*3+2] = (*src >> 16) & 0xFF;
229 if(x == FBInfo->Width) {
230 dest += FBInfo->Pitch;
236 // Copy to Frambuffer
237 if( FBInfo->Pitch != FBInfo->Width*4 )
240 // Pitch isn't 4*Width
241 x = Offset % FBInfo->Width;
242 y = Offset / FBInfo->Height;
244 px = (Uint32*)FBInfo->Framebuffer + y*FBInfo->Pitch/4;
246 for( ; Length >= 4; Length -= 4, x )
249 if( x == FBInfo->Width ) {
257 dest = (Uint8 *)FBInfo->Framebuffer + Offset;
258 memcpy(dest, Buffer, Length);
262 Log_Warning("DrvUtil", "DrvUtil_Video_WriteLFB - Unknown bit depthn %i", FBInfo->Depth);
267 case VIDEO_BUFFMT_2DSTREAM:
268 Length = DrvUtil_Video_2DStream(
269 FBInfo, Buffer, Length,
270 &gDrvUtil_Stub_2DFunctions, sizeof(gDrvUtil_Stub_2DFunctions)
279 DrvUtil_Video_DrawCursor(FBInfo, csr_x, csr_y);
285 int DrvUtil_Video_SetCursor(tDrvUtil_Video_BufInfo *Buf, tVideo_IOCtl_Bitmap *Bitmap)
287 int csrX = Buf->CursorX, csrY = Buf->CursorY;
290 ENTER("pBuf pBitmap", Buf, Bitmap);
293 if( Buf->CursorBitmap )
295 LOG("Clearing old cursor");
296 DrvUtil_Video_RemoveCursor(Buf);
297 if( !Bitmap || Bitmap->W != Buf->CursorBitmap->W || Bitmap->H != Buf->CursorBitmap->H )
299 free( Buf->CursorSaveBuf );
300 Buf->CursorSaveBuf = NULL;
302 if( Buf->CursorBitmap != &gDrvUtil_TextModeCursor)
303 free(Buf->CursorBitmap);
304 Buf->CursorBitmap = NULL;
307 // If the new bitmap is null, disable drawing
316 // Sanity check the bitmap
317 LOG("Sanity checking plox");
318 if( !CheckMem(Bitmap, sizeof(*Bitmap)) || !CheckMem(Bitmap->Data, Bitmap->W*Bitmap->H*sizeof(Uint32)) )
320 Log_Warning("DrvUtil", "DrvUtil_Video_SetCursor: Bitmap (%p) is in invalid memory", Bitmap);
326 // Don't take a copy of the DrvUtil provided cursor
327 if( Bitmap == &gDrvUtil_TextModeCursor )
329 LOG("No copy (provided cursor)");
330 Buf->CursorBitmap = Bitmap;
335 size = sizeof(tVideo_IOCtl_Bitmap) + Bitmap->W*Bitmap->H*4;
338 Buf->CursorBitmap = malloc( size );
339 memcpy(Buf->CursorBitmap, Bitmap, size);
342 // Restore cursor position
344 DrvUtil_Video_DrawCursor(Buf, csrX, csrY);
349 void DrvUtil_Video_DrawCursor(tDrvUtil_Video_BufInfo *Buf, int X, int Y)
351 int render_ox=0, render_oy=0, render_w, render_h;
353 ENTER("pBuf iX iY", Buf, X, Y);
354 DrvUtil_Video_RemoveCursor(Buf);
356 // X < 0 disables the cursor
364 if( X < 0 || Y < 0 || X >= Buf->Width || Y >= Buf->Height ) {
369 // Ensure the cursor is enabled
370 if( !Buf->CursorBitmap ) {
375 // Save cursor position (for changing the bitmap)
376 Buf->CursorX = X; Buf->CursorY = Y;
377 // Apply cursor's center offset
378 X -= Buf->CursorBitmap->XOfs;
379 Y -= Buf->CursorBitmap->YOfs;
381 // Get the width of the cursor on screen (clipping to right/bottom edges)
382 render_w = X > Buf->Width - Buf->CursorBitmap->W ? Buf->Width - X : Buf->CursorBitmap->W;
383 render_h = Y > Buf->Height - Buf->CursorBitmap->H ? Buf->Height - Y : Buf->CursorBitmap->H;
385 // Clipp to left/top edges
386 if(X < 0) { render_ox = -X; X = 0; }
387 if(Y < 0) { render_oy = -Y; Y = 0; }
390 Buf->CursorRenderW = render_w; Buf->CursorRenderH = render_h;
391 Buf->CursorDestX = X; Buf->CursorDestY = Y;
392 Buf->CursorReadX = render_ox; Buf->CursorReadY = render_oy;
394 LOG("%ix%i at %i,%i offset %i,%i",
395 render_w, render_h, X, Y, render_ox, render_oy);
397 // Call render routine
398 DrvUtil_Video_RenderCursor(Buf);
402 void DrvUtil_Video_RenderCursor(tDrvUtil_Video_BufInfo *Buf)
404 int src_x = Buf->CursorReadX, src_y = Buf->CursorReadY;
405 int render_w = Buf->CursorRenderW, render_h = Buf->CursorRenderH;
406 int dest_x = Buf->CursorDestX, dest_y = Buf->CursorDestY;
407 int bytes_per_px = (Buf->Depth + 7) / 8;
408 int save_pitch = Buf->CursorBitmap->W * bytes_per_px;
413 dest = (Uint8*)Buf->Framebuffer + dest_y*Buf->Pitch + dest_x*bytes_per_px;
414 src = Buf->CursorBitmap->Data + src_y * Buf->CursorBitmap->W + src_x;
416 LOG("dest = %p, src = %p", dest, src);
418 // Allocate save buffer if not already
419 if( !Buf->CursorSaveBuf )
420 Buf->CursorSaveBuf = malloc( Buf->CursorBitmap->W*Buf->CursorBitmap->H*bytes_per_px );
423 // Save behind the cursor
424 for( y = 0; y < render_h; y ++ )
426 (Uint8*)Buf->CursorSaveBuf + y*save_pitch,
427 (Uint8*)dest + y*Buf->Pitch,
428 render_w*bytes_per_px
436 Log_Warning("DrvUtil", "TODO: Support 15/16 bpp modes in cursor draw");
439 LOG("24-bit render");
440 for( y = 0; y < render_h; y ++ )
444 for(x = 0; x < render_w; x ++, px += 3)
446 Uint32 value = src[x];
447 // TODO: Should I implement alpha blending?
448 if(value & 0xFF000000)
450 px[0] = value & 0xFF;
451 px[1] = (value >> 8) & 0xFF;
452 px[2] = (value >> 16) & 0xFF;
457 src += Buf->CursorBitmap->W;
458 dest = (Uint8*)dest + Buf->Pitch;
462 LOG("32-bit render");
463 for( y = 0; y < render_h; y ++ )
467 for(x = 0; x < render_w; x ++, px ++)
469 Uint32 value = src[x];
470 // TODO: Should I implement alpha blending?
471 if(value & 0xFF000000)
474 ; // NOP, completely transparent
476 LOG("row %i/%i (%p-%P) done", y+1, render_h, dest, MM_GetPhysAddr(dest));
477 src += Buf->CursorBitmap->W;
478 dest = (Uint8*)dest + Buf->Pitch;
482 Log_Error("DrvUtil", "RenderCursor - Unknown bit depth %i", Buf->Depth);
488 void DrvUtil_Video_RemoveCursor(tDrvUtil_Video_BufInfo *Buf)
490 int bytes_per_px = (Buf->Depth + 7) / 8;
494 // Just a little sanity
495 if( !Buf->CursorBitmap || Buf->CursorX == -1 ) return ;
496 if( !Buf->CursorSaveBuf ) return ;
498 // Debug("DrvUtil_Video_RemoveCursor: (Buf=%p) dest_x=%i, dest_y=%i", Buf, Buf->CursorDestX, Buf->CursorDestY);
501 save_pitch = Buf->CursorBitmap->W * bytes_per_px;
502 dest = (Uint8*)Buf->Framebuffer + Buf->CursorDestY * Buf->Pitch + Buf->CursorDestX*bytes_per_px;
503 src = Buf->CursorSaveBuf;
505 // Copy each line back
506 for( y = 0; y < Buf->CursorRenderH; y ++ )
508 memcpy( dest, src, Buf->CursorRenderW * bytes_per_px );
513 // Set the cursor as removed
517 void DrvUtil_Video_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour)
519 tDrvUtil_Video_BufInfo *FBInfo = Ent;
521 // TODO: Handle non-32bit modes
522 if( FBInfo->Depth != 32 ) return;
524 // TODO: Be less hacky
525 int pitch = FBInfo->Pitch/4;
526 Uint32 *buf = (Uint32*)FBInfo->Framebuffer + Y*pitch + X;
531 for(i=W;i--;tmp++) *tmp = Colour;
536 void DrvUtil_Video_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H)
538 tDrvUtil_Video_BufInfo *FBInfo = Ent;
539 int scrnpitch = FBInfo->Pitch;
540 int bytes_per_px = (FBInfo->Depth + 7) / 8;
541 int dst = DstY*scrnpitch + DstX;
542 int src = SrcY*scrnpitch + SrcX;
545 //Log("Vesa_2D_Blit: (Ent=%p, DstX=%i, DstY=%i, SrcX=%i, SrcY=%i, W=%i, H=%i)",
546 // Ent, DstX, DstY, SrcX, SrcY, W, H);
548 if(SrcX + W > FBInfo->Width) W = FBInfo->Width - SrcX;
549 if(DstX + W > FBInfo->Width) W = FBInfo->Width - DstX;
550 if(SrcY + H > FBInfo->Height) H = FBInfo->Height - SrcY;
551 if(DstY + H > FBInfo->Height) H = FBInfo->Height - DstY;
553 //Debug("W = %i, H = %i", W, H);
562 tmp = W*bytes_per_px;
563 for( tmp = W; tmp --; ) {
564 *((Uint8*)FBInfo->Framebuffer + dst + tmp) = *((Uint8*)FBInfo->Framebuffer + src + tmp);
568 else if(W == FBInfo->Width && FBInfo->Pitch == FBInfo->Width*bytes_per_px) {
569 memmove((Uint8*)FBInfo->Framebuffer + dst, (Uint8*)FBInfo->Framebuffer + src, H*FBInfo->Pitch);
574 memcpy((Uint8*)FBInfo->Framebuffer + dst, (Uint8*)FBInfo->Framebuffer + src, W*bytes_per_px);
579 //Log("Vesa_2D_Blit: RETURN");
583 // --- Disk Driver Helpers ---
584 Uint64 DrvUtil_ReadBlock(Uint64 Start, Uint64 Length, void *Buffer,
585 tDrvUtil_Read_Callback ReadBlocks, Uint64 BlockSize, void *Argument)
587 Uint8 tmp[BlockSize]; // C99
588 Uint64 block = Start / BlockSize;
589 int offset = Start - block * BlockSize;
590 int leading = BlockSize - offset;
595 ENTER("XStart XLength pBuffer pReadBlocks XBlockSize pArgument",
596 Start, Length, Buffer, ReadBlocks, BlockSize, Argument);
598 // Non aligned start, let's fix that!
601 if(leading > Length) leading = Length;
602 LOG("Reading %i bytes from Block1+%i", leading, offset);
603 ret = ReadBlocks(block, 1, tmp, Argument);
608 memcpy( Buffer, &tmp[offset], leading );
610 if(leading == Length) {
615 Buffer = (Uint8*)Buffer + leading;
617 num = ( Length - leading ) / BlockSize;
618 tailings = Length - num * BlockSize - leading;
621 num = Length / BlockSize;
622 tailings = Length % BlockSize;
625 // Read central blocks
628 LOG("Reading %i blocks", num);
629 ret = ReadBlocks(block, num, Buffer, Argument);
631 LEAVE('X', leading + ret * BlockSize);
632 return leading + ret * BlockSize;
636 // Read last tailing block
639 LOG("Reading %i bytes from last block", tailings);
641 Buffer = (Uint8*)Buffer + num * BlockSize;
642 ret = ReadBlocks(block, 1, tmp, Argument);
644 LEAVE('X', leading + num * BlockSize);
645 return leading + num * BlockSize;
647 memcpy( Buffer, tmp, tailings );
654 Uint64 DrvUtil_WriteBlock(Uint64 Start, Uint64 Length, const void *Buffer,
655 tDrvUtil_Read_Callback ReadBlocks, tDrvUtil_Write_Callback WriteBlocks,
656 Uint64 BlockSize, void *Argument)
658 Uint8 tmp[BlockSize]; // C99
659 Uint64 block = Start / BlockSize;
660 int offset = Start - block * BlockSize;
661 int leading = BlockSize - offset;
666 ENTER("XStart XLength pBuffer pReadBlocks pWriteBlocks XBlockSize pArgument",
667 Start, Length, Buffer, ReadBlocks, WriteBlocks, BlockSize, Argument);
669 // Non aligned start, let's fix that!
672 if(leading > Length) leading = Length;
673 LOG("Writing %i bytes to Block1+%i", leading, offset);
674 // Read a copy of the block
675 ret = ReadBlocks(block, 1, tmp, Argument);
681 memcpy( &tmp[offset], Buffer, leading );
683 ret = WriteBlocks(block, 1, tmp, Argument);
689 if(leading == Length) {
694 Buffer = (Uint8*)Buffer + leading;
696 num = ( Length - leading ) / BlockSize;
697 tailings = Length - num * BlockSize - leading;
700 num = Length / BlockSize;
701 tailings = Length % BlockSize;
704 // Read central blocks
707 LOG("Writing %i blocks", num);
708 ret = WriteBlocks(block, num, Buffer, Argument);
710 LEAVE('X', leading + ret * BlockSize);
711 return leading + ret * BlockSize;
715 // Read last tailing block
718 LOG("Writing %i bytes to last block", tailings);
720 Buffer = (Uint8*)Buffer + num * BlockSize;
722 ret = ReadBlocks(block, 1, tmp, Argument);
724 LEAVE('X', leading + num * BlockSize);
725 return leading + num * BlockSize;
728 memcpy( tmp, Buffer, tailings );
730 ret = WriteBlocks(block, 1, tmp, Argument);
732 LEAVE('X', leading + num * BlockSize);
733 return leading + num * BlockSize;