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 Mode, 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;
291 if( Buf->CursorBitmap )
293 DrvUtil_Video_RemoveCursor(Buf);
294 if( !Bitmap || Bitmap->W != Buf->CursorBitmap->W || Bitmap->H != Buf->CursorBitmap->H )
296 free( Buf->CursorSaveBuf );
297 Buf->CursorSaveBuf = NULL;
299 if( Buf->CursorBitmap != &gDrvUtil_TextModeCursor)
300 free(Buf->CursorBitmap);
301 Buf->CursorBitmap = NULL;
304 // If the new bitmap is null, disable drawing
312 // Sanity check the bitmap
313 if( !CheckMem(Bitmap, sizeof(*Bitmap)) || !CheckMem(Bitmap->Data, Bitmap->W*Bitmap->H*sizeof(Uint32)) )
315 Log_Warning("DrvUtil", "DrvUtil_Video_SetCursor: Bitmap (%p) is in invalid memory", Bitmap);
320 // Don't take a copy of the DrvUtil provided cursor
321 if( Bitmap == &gDrvUtil_TextModeCursor )
323 Buf->CursorBitmap = Bitmap;
327 size = sizeof(tVideo_IOCtl_Bitmap) + Bitmap->W*Bitmap->H*4;
330 Buf->CursorBitmap = malloc( size );
331 memcpy(Buf->CursorBitmap, Bitmap, size);
334 // Restore cursor position
335 DrvUtil_Video_DrawCursor(Buf, csrX, csrY);
339 void DrvUtil_Video_DrawCursor(tDrvUtil_Video_BufInfo *Buf, int X, int Y)
341 int render_ox=0, render_oy=0, render_w, render_h;
343 DrvUtil_Video_RemoveCursor(Buf);
345 // X < 0 disables the cursor
352 if( X < 0 || Y < 0 ) return;
353 if( X >= Buf->Width || Y >= Buf->Height ) return;
355 // Ensure the cursor is enabled
356 if( !Buf->CursorBitmap ) return ;
358 // Save cursor position (for changing the bitmap)
359 Buf->CursorX = X; Buf->CursorY = Y;
361 // Apply cursor's center offset
362 X -= Buf->CursorBitmap->XOfs;
363 Y -= Buf->CursorBitmap->YOfs;
365 // Get the width of the cursor on screen (clipping to right/bottom edges)
366 render_w = X > Buf->Width - Buf->CursorBitmap->W ? Buf->Width - X : Buf->CursorBitmap->W;
367 render_h = Y > Buf->Height - Buf->CursorBitmap->H ? Buf->Height - Y : Buf->CursorBitmap->H;
369 // Clipp to left/top edges
370 if(X < 0) { render_ox = -X; X = 0; }
371 if(Y < 0) { render_oy = -Y; Y = 0; }
374 Buf->CursorRenderW = render_w; Buf->CursorRenderH = render_h;
375 Buf->CursorDestX = X; Buf->CursorDestY = Y;
376 Buf->CursorReadX = render_ox; Buf->CursorReadY = render_oy;
378 // Call render routine
379 DrvUtil_Video_RenderCursor(Buf);
382 void DrvUtil_Video_RenderCursor(tDrvUtil_Video_BufInfo *Buf)
384 int src_x = Buf->CursorReadX, src_y = Buf->CursorReadY;
385 int render_w = Buf->CursorRenderW, render_h = Buf->CursorRenderH;
386 int dest_x = Buf->CursorDestX, dest_y = Buf->CursorDestY;
387 int bytes_per_px = (Buf->Depth + 7) / 8;
388 int save_pitch = Buf->CursorBitmap->W * bytes_per_px;
393 dest = (Uint8*)Buf->Framebuffer + dest_y*Buf->Pitch + dest_x*bytes_per_px;
394 src = Buf->CursorBitmap->Data + src_y * Buf->CursorBitmap->W + src_x;
396 // Allocate save buffer if not already
397 if( !Buf->CursorSaveBuf )
398 Buf->CursorSaveBuf = malloc( Buf->CursorBitmap->W*Buf->CursorBitmap->H*bytes_per_px );
400 // Save behind the cursor
401 for( y = 0; y < render_h; y ++ )
403 (Uint8*)Buf->CursorSaveBuf + y*save_pitch,
404 (Uint8*)dest + y*Buf->Pitch,
405 render_w*bytes_per_px
413 Log_Warning("DrvUtil", "TODO: Support 15/16 bpp modes in cursor draw");
416 for( y = 0; y < render_h; y ++ )
420 for(x = 0; x < render_w; x ++, px += 3)
422 Uint32 value = src[x];
423 // TODO: Should I implement alpha blending?
424 if(value & 0xFF000000)
426 px[0] = value & 0xFF;
427 px[1] = (value >> 8) & 0xFF;
428 px[2] = (value >> 16) & 0xFF;
433 src += Buf->CursorBitmap->W;
434 dest = (Uint8*)dest + Buf->Pitch;
438 for( y = 0; y < render_h; y ++ )
442 for(x = 0; x < render_w; x ++, px ++)
444 Uint32 value = src[x];
445 // TODO: Should I implement alpha blending?
446 if(value & 0xFF000000)
449 ; // NOP, completely transparent
451 src += Buf->CursorBitmap->W;
452 dest = (Uint8*)dest + Buf->Pitch;
456 Log_Error("DrvUtil", "RenderCursor - Unknown bit depth %i", Buf->Depth);
462 void DrvUtil_Video_RemoveCursor(tDrvUtil_Video_BufInfo *Buf)
464 int bytes_per_px = (Buf->Depth + 7) / 8;
468 // Just a little sanity
469 if( !Buf->CursorBitmap || Buf->CursorX == -1 ) return ;
470 if( !Buf->CursorSaveBuf ) return ;
472 // Debug("DrvUtil_Video_RemoveCursor: (Buf=%p) dest_x=%i, dest_y=%i", Buf, Buf->CursorDestX, Buf->CursorDestY);
475 save_pitch = Buf->CursorBitmap->W * bytes_per_px;
476 dest = (Uint8*)Buf->Framebuffer + Buf->CursorDestY * Buf->Pitch + Buf->CursorDestX*bytes_per_px;
477 src = Buf->CursorSaveBuf;
479 // Copy each line back
480 for( y = 0; y < Buf->CursorRenderH; y ++ )
482 memcpy( dest, src, Buf->CursorRenderW * bytes_per_px );
487 // Set the cursor as removed
491 void DrvUtil_Video_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour)
493 tDrvUtil_Video_BufInfo *FBInfo = Ent;
495 // TODO: Handle non-32bit modes
496 if( FBInfo->Depth != 32 ) return;
498 // TODO: Be less hacky
499 int pitch = FBInfo->Pitch/4;
500 Uint32 *buf = (Uint32*)FBInfo->Framebuffer + Y*pitch + X;
505 for(i=W;i--;tmp++) *tmp = Colour;
510 void DrvUtil_Video_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H)
512 tDrvUtil_Video_BufInfo *FBInfo = Ent;
513 int scrnpitch = FBInfo->Pitch;
514 int bytes_per_px = (FBInfo->Depth + 7) / 8;
515 int dst = DstY*scrnpitch + DstX;
516 int src = SrcY*scrnpitch + SrcX;
519 //Log("Vesa_2D_Blit: (Ent=%p, DstX=%i, DstY=%i, SrcX=%i, SrcY=%i, W=%i, H=%i)",
520 // Ent, DstX, DstY, SrcX, SrcY, W, H);
522 if(SrcX + W > FBInfo->Width) W = FBInfo->Width - SrcX;
523 if(DstX + W > FBInfo->Width) W = FBInfo->Width - DstX;
524 if(SrcY + H > FBInfo->Height) H = FBInfo->Height - SrcY;
525 if(DstY + H > FBInfo->Height) H = FBInfo->Height - DstY;
527 //Debug("W = %i, H = %i", W, H);
536 tmp = W*bytes_per_px;
537 for( tmp = W; tmp --; ) {
538 *((Uint8*)FBInfo->Framebuffer + dst + tmp) = *((Uint8*)FBInfo->Framebuffer + src + tmp);
542 else if(W == FBInfo->Width && FBInfo->Pitch == FBInfo->Width*bytes_per_px) {
543 memmove((Uint8*)FBInfo->Framebuffer + dst, (Uint8*)FBInfo->Framebuffer + src, H*FBInfo->Pitch);
548 memcpy((Uint8*)FBInfo->Framebuffer + dst, (Uint8*)FBInfo->Framebuffer + src, W*bytes_per_px);
553 //Log("Vesa_2D_Blit: RETURN");
557 // --- Disk Driver Helpers ---
558 Uint64 DrvUtil_ReadBlock(Uint64 Start, Uint64 Length, void *Buffer,
559 tDrvUtil_Read_Callback ReadBlocks, Uint64 BlockSize, Uint Argument)
561 Uint8 tmp[BlockSize]; // C99
562 Uint64 block = Start / BlockSize;
563 int offset = Start - block * BlockSize;
564 int leading = BlockSize - offset;
569 ENTER("XStart XLength pBuffer pReadBlocks XBlockSize xArgument",
570 Start, Length, Buffer, ReadBlocks, BlockSize, Argument);
572 // Non aligned start, let's fix that!
575 if(leading > Length) leading = Length;
576 LOG("Reading %i bytes from Block1+%i", leading, offset);
577 ret = ReadBlocks(block, 1, tmp, Argument);
582 memcpy( Buffer, &tmp[offset], leading );
584 if(leading == Length) {
589 Buffer = (Uint8*)Buffer + leading;
591 num = ( Length - leading ) / BlockSize;
592 tailings = Length - num * BlockSize - leading;
595 num = Length / BlockSize;
596 tailings = Length % BlockSize;
599 // Read central blocks
602 LOG("Reading %i blocks", num);
603 ret = ReadBlocks(block, num, Buffer, Argument);
605 LEAVE('X', leading + ret * BlockSize);
606 return leading + ret * BlockSize;
610 // Read last tailing block
613 LOG("Reading %i bytes from last block", tailings);
615 Buffer = (Uint8*)Buffer + num * BlockSize;
616 ret = ReadBlocks(block, 1, tmp, Argument);
618 LEAVE('X', leading + num * BlockSize);
619 return leading + num * BlockSize;
621 memcpy( Buffer, tmp, tailings );
628 Uint64 DrvUtil_WriteBlock(Uint64 Start, Uint64 Length, const void *Buffer,
629 tDrvUtil_Read_Callback ReadBlocks, tDrvUtil_Write_Callback WriteBlocks,
630 Uint64 BlockSize, Uint Argument)
632 Uint8 tmp[BlockSize]; // C99
633 Uint64 block = Start / BlockSize;
634 int offset = Start - block * BlockSize;
635 int leading = BlockSize - offset;
640 ENTER("XStart XLength pBuffer pReadBlocks pWriteBlocks XBlockSize xArgument",
641 Start, Length, Buffer, ReadBlocks, WriteBlocks, BlockSize, Argument);
643 // Non aligned start, let's fix that!
646 if(leading > Length) leading = Length;
647 LOG("Writing %i bytes to Block1+%i", leading, offset);
648 // Read a copy of the block
649 ret = ReadBlocks(block, 1, tmp, Argument);
655 memcpy( &tmp[offset], Buffer, leading );
657 ret = WriteBlocks(block, 1, tmp, Argument);
663 if(leading == Length) {
668 Buffer = (Uint8*)Buffer + leading;
670 num = ( Length - leading ) / BlockSize;
671 tailings = Length - num * BlockSize - leading;
674 num = Length / BlockSize;
675 tailings = Length % BlockSize;
678 // Read central blocks
681 LOG("Writing %i blocks", num);
682 ret = WriteBlocks(block, num, Buffer, Argument);
684 LEAVE('X', leading + ret * BlockSize);
685 return leading + ret * BlockSize;
689 // Read last tailing block
692 LOG("Writing %i bytes to last block", tailings);
694 Buffer = (Uint8*)Buffer + num * BlockSize;
696 ret = ReadBlocks(block, 1, tmp, Argument);
698 LEAVE('X', leading + num * BlockSize);
699 return leading + num * BlockSize;
702 memcpy( tmp, Buffer, tailings );
704 ret = WriteBlocks(block, 1, tmp, Argument);
706 LEAVE('X', leading + num * BlockSize);
707 return leading + num * BlockSize;