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, void *Buffer, int Length,
57 tDrvUtil_Video_2DHandlers *Handlers, int SizeofHandlers)
59 void *stream = Buffer;
66 stream = (void*)((tVAddr)stream + 1);
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 < 12) return Length-rem;
90 Log_Warning("DrvUtil", "DrvUtil_Video_2DStream: Driver"
91 " does not support VIDEO_2DOP_FILL");
97 ((Uint16*)stream)[0], ((Uint16*)stream)[1],
98 ((Uint16*)stream)[2], ((Uint16*)stream)[3],
103 stream = (void*)((tVAddr)stream + 12);
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 ((Uint16*)stream)[0], ((Uint16*)stream)[1],
118 ((Uint16*)stream)[2], ((Uint16*)stream)[3],
119 ((Uint16*)stream)[4], ((Uint16*)stream)[5]
123 stream = (void*)((tVAddr)stream + 12);
131 int DrvUtil_Video_WriteLFB(tDrvUtil_Video_BufInfo *FBInfo, size_t Offset, size_t Length, void *Buffer)
135 ENTER("pFBInfo xOffset xLength pBuffer",
136 Mode, FBInfo, Offset, Length, Buffer);
138 csr_x = FBInfo->CursorX;
139 csr_y = FBInfo->CursorY;
141 DrvUtil_Video_RemoveCursor(FBInfo);
143 switch( FBInfo->BufferFormat )
145 case VIDEO_BUFFMT_TEXT:
147 tVT_Char *chars = Buffer;
148 int bytes_per_px = (FBInfo->Depth + 7) / 8;
149 int widthInChars = FBInfo->Width/giVT_CharWidth;
150 int heightInChars = FBInfo->Height/giVT_CharHeight;
153 LOG("bytes_per_px = %i", bytes_per_px);
154 LOG("widthInChars = %i, heightInChars = %i", widthInChars, heightInChars);
156 Length /= sizeof(tVT_Char); Offset /= sizeof(tVT_Char);
158 x = Offset % widthInChars; y = Offset / widthInChars;
159 LOG("x = %i, y = %i", x, y);
162 if(Offset > heightInChars * widthInChars) LEAVE_RET('i', 0);
163 if(y >= heightInChars) LEAVE_RET('i', 0);
165 if( Offset + Length > heightInChars*widthInChars )
167 Length = heightInChars*widthInChars - Offset;
170 dest = FBInfo->Framebuffer;
171 LOG("dest = %p", dest);
172 dest += y * giVT_CharHeight * FBInfo->Pitch;
173 LOG("dest = %p", dest);
175 for( i = 0; i < Length; i++ )
177 if( y >= heightInChars )
179 Log_Notice("DrvUtil", "Stopped at %i", i);
185 dest + x*giVT_CharWidth*bytes_per_px, FBInfo->Depth, FBInfo->Pitch,
186 VT_Colour12toN(chars->BGCol, FBInfo->Depth),
187 VT_Colour12toN(chars->FGCol, FBInfo->Depth)
192 if( x >= widthInChars )
196 dest += FBInfo->Pitch*giVT_CharHeight;
197 LOG("dest = %p", dest);
200 Length = i * sizeof(tVT_Char);
204 case VIDEO_BUFFMT_FRAMEBUFFER:
205 if(FBInfo->Width*FBInfo->Height*4 < Offset+Length)
207 Log_Warning("DrvUtil", "DrvUtil_Video_WriteLFB - Framebuffer Overflow");
211 //TODO: Handle non 32-bpp framebuffer modes
212 if( FBInfo->Depth != 32 ) {
213 Log_Warning("DrvUtil", "DrvUtil_Video_WriteLFB - Don't support non 32-bpp FB mode");
218 //TODO: Handle pitch != Width*BytesPerPixel
219 // Copy to Frambuffer
220 dest = (Uint8 *)FBInfo->Framebuffer + Offset;
221 memcpy(dest, Buffer, Length);
224 case VIDEO_BUFFMT_2DSTREAM:
225 Length = DrvUtil_Video_2DStream(
226 FBInfo, Buffer, Length,
227 &gDrvUtil_Stub_2DFunctions, sizeof(gDrvUtil_Stub_2DFunctions)
236 DrvUtil_Video_DrawCursor(FBInfo, csr_x, csr_y);
242 int DrvUtil_Video_SetCursor(tDrvUtil_Video_BufInfo *Buf, tVideo_IOCtl_Bitmap *Bitmap)
244 int csrX = Buf->CursorX, csrY = Buf->CursorY;
248 if( Buf->CursorBitmap )
250 DrvUtil_Video_RemoveCursor(Buf);
251 if( !Bitmap || Bitmap->W != Buf->CursorBitmap->W || Bitmap->H != Buf->CursorBitmap->H )
253 free( Buf->CursorSaveBuf );
254 Buf->CursorSaveBuf = NULL;
256 if( Buf->CursorBitmap != &gDrvUtil_TextModeCursor)
257 free(Buf->CursorBitmap);
258 Buf->CursorBitmap = NULL;
261 // If the new bitmap is null, disable drawing
269 // Sanity check the bitmap
270 if( !CheckMem(Bitmap, sizeof(*Bitmap)) || !CheckMem(Bitmap->Data, Bitmap->W*Bitmap->H*sizeof(Uint32)) )
272 Log_Warning("DrvUtil", "DrvUtil_Video_SetCursor: Bitmap (%p) is in invalid memory", Bitmap);
277 // Don't take a copy of the DrvUtil provided cursor
278 if( Bitmap == &gDrvUtil_TextModeCursor )
280 Buf->CursorBitmap = Bitmap;
284 size = sizeof(tVideo_IOCtl_Bitmap) + Bitmap->W*Bitmap->H*4;
287 Buf->CursorBitmap = malloc( size );
288 memcpy(Buf->CursorBitmap, Bitmap, size);
291 // Restore cursor position
292 DrvUtil_Video_DrawCursor(Buf, csrX, csrY);
296 void DrvUtil_Video_DrawCursor(tDrvUtil_Video_BufInfo *Buf, int X, int Y)
298 int render_ox=0, render_oy=0, render_w, render_h;
300 DrvUtil_Video_RemoveCursor(Buf);
302 // X < 0 disables the cursor
309 if( X < 0 || Y < 0 ) return;
310 if( X >= Buf->Width || Y >= Buf->Height ) return;
312 // Ensure the cursor is enabled
313 if( !Buf->CursorBitmap ) return ;
315 // Save cursor position (for changing the bitmap)
316 Buf->CursorX = X; Buf->CursorY = Y;
318 // Apply cursor's center offset
319 X -= Buf->CursorBitmap->XOfs;
320 Y -= Buf->CursorBitmap->YOfs;
322 // Get the width of the cursor on screen (clipping to right/bottom edges)
323 render_w = X > Buf->Width - Buf->CursorBitmap->W ? Buf->Width - X : Buf->CursorBitmap->W;
324 render_h = Y > Buf->Height - Buf->CursorBitmap->H ? Buf->Height - Y : Buf->CursorBitmap->H;
326 // Clipp to left/top edges
327 if(X < 0) { render_ox = -X; X = 0; }
328 if(Y < 0) { render_oy = -Y; Y = 0; }
331 Buf->CursorRenderW = render_w; Buf->CursorRenderH = render_h;
332 Buf->CursorDestX = X; Buf->CursorDestY = Y;
333 Buf->CursorReadX = render_ox; Buf->CursorReadY = render_oy;
335 // Call render routine
336 DrvUtil_Video_RenderCursor(Buf);
339 void DrvUtil_Video_RenderCursor(tDrvUtil_Video_BufInfo *Buf)
341 int src_x = Buf->CursorReadX, src_y = Buf->CursorReadY;
342 int render_w = Buf->CursorRenderW, render_h = Buf->CursorRenderH;
343 int dest_x = Buf->CursorDestX, dest_y = Buf->CursorDestY;
344 int bytes_per_px = (Buf->Depth + 7) / 8;
345 int save_pitch = Buf->CursorBitmap->W * bytes_per_px;
350 // Debug("DrvUtil_Video_RenderCursor: (Buf=%p) dest_x=%i, dest_y=%i", Buf, dest_x, dest_y);
352 dest = (Uint8*)Buf->Framebuffer + dest_y*Buf->Pitch + dest_x*bytes_per_px;
353 src = Buf->CursorBitmap->Data + src_y * Buf->CursorBitmap->W + src_x;
355 // Allocate save buffer if not already
356 if( !Buf->CursorSaveBuf )
357 Buf->CursorSaveBuf = malloc( Buf->CursorBitmap->W*Buf->CursorBitmap->H*bytes_per_px );
359 // Save behind the cursor
360 for( y = 0; y < render_h; y ++ )
362 (Uint8*)Buf->CursorSaveBuf + y*save_pitch,
363 (Uint8*)dest + y*Buf->Pitch,
364 render_w*bytes_per_px
370 for( y = 0; y < render_h; y ++ )
372 for(x = 0; x < render_w; x ++ )
374 // TODO: Should I implement alpha blending?
375 if(src[x] & 0xFF000000)
376 ((Uint32*)dest)[x] = src[x];
378 ; // NOP, completely transparent
380 src += Buf->CursorBitmap->W;
381 dest = (Uint8*)dest + Buf->Pitch;
385 Log_Error("DrvUtil", "TODO: Implement non 32-bpp cursor");
390 void DrvUtil_Video_RemoveCursor(tDrvUtil_Video_BufInfo *Buf)
392 int bytes_per_px = (Buf->Depth + 7) / 8;
396 // Just a little sanity
397 if( !Buf->CursorBitmap || Buf->CursorX == -1 ) return ;
398 if( !Buf->CursorSaveBuf ) return ;
400 // Debug("DrvUtil_Video_RemoveCursor: (Buf=%p) dest_x=%i, dest_y=%i", Buf, Buf->CursorDestX, Buf->CursorDestY);
403 save_pitch = Buf->CursorBitmap->W * bytes_per_px;
404 dest = (Uint8*)Buf->Framebuffer + Buf->CursorDestY * Buf->Pitch + Buf->CursorDestX*bytes_per_px;
405 src = Buf->CursorSaveBuf;
407 // Copy each line back
408 for( y = 0; y < Buf->CursorRenderH; y ++ )
410 memcpy( dest, src, Buf->CursorRenderW * bytes_per_px );
415 // Set the cursor as removed
419 void DrvUtil_Video_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour)
421 tDrvUtil_Video_BufInfo *FBInfo = Ent;
423 // TODO: Handle non-32bit modes
424 if( FBInfo->Depth != 32 ) return;
426 // TODO: Be less hacky
427 int pitch = FBInfo->Pitch/4;
428 Uint32 *buf = (Uint32*)FBInfo->Framebuffer + Y*pitch + X;
433 for(i=W;i--;tmp++) *tmp = Colour;
438 void DrvUtil_Video_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H)
440 tDrvUtil_Video_BufInfo *FBInfo = Ent;
441 int scrnpitch = FBInfo->Pitch;
442 int bytes_per_px = (FBInfo->Depth + 7) / 8;
443 int dst = DstY*scrnpitch + DstX;
444 int src = SrcY*scrnpitch + SrcX;
447 //Log("Vesa_2D_Blit: (Ent=%p, DstX=%i, DstY=%i, SrcX=%i, SrcY=%i, W=%i, H=%i)",
448 // Ent, DstX, DstY, SrcX, SrcY, W, H);
450 if(SrcX + W > FBInfo->Width) W = FBInfo->Width - SrcX;
451 if(DstX + W > FBInfo->Width) W = FBInfo->Width - DstX;
452 if(SrcY + H > FBInfo->Height) H = FBInfo->Height - SrcY;
453 if(DstY + H > FBInfo->Height) H = FBInfo->Height - DstY;
455 //Debug("W = %i, H = %i", W, H);
464 tmp = W*bytes_per_px;
465 for( tmp = W; tmp --; ) {
466 *((Uint8*)FBInfo->Framebuffer + dst + tmp) = *((Uint8*)FBInfo->Framebuffer + src + tmp);
473 memcpy((Uint8*)FBInfo->Framebuffer + dst, (Uint8*)FBInfo->Framebuffer + src, W*bytes_per_px);
478 //Log("Vesa_2D_Blit: RETURN");
482 // --- Disk Driver Helpers ---
483 Uint64 DrvUtil_ReadBlock(Uint64 Start, Uint64 Length, void *Buffer,
484 tDrvUtil_Callback ReadBlocks, Uint64 BlockSize, Uint Argument)
486 Uint8 tmp[BlockSize]; // C99
487 Uint64 block = Start / BlockSize;
488 int offset = Start - block * BlockSize;
489 int leading = BlockSize - offset;
494 ENTER("XStart XLength pBuffer pReadBlocks XBlockSize xArgument",
495 Start, Length, Buffer, ReadBlocks, BlockSize, Argument);
497 // Non aligned start, let's fix that!
500 if(leading > Length) leading = Length;
501 LOG("Reading %i bytes from Block1+%i", leading, offset);
502 ret = ReadBlocks(block, 1, tmp, Argument);
507 memcpy( Buffer, &tmp[offset], leading );
509 if(leading == Length) {
514 Buffer = (Uint8*)Buffer + leading;
516 num = ( Length - leading ) / BlockSize;
517 tailings = Length - num * BlockSize - leading;
520 num = Length / BlockSize;
521 tailings = Length % BlockSize;
524 // Read central blocks
527 LOG("Reading %i blocks", num);
528 ret = ReadBlocks(block, num, Buffer, Argument);
530 LEAVE('X', leading + ret * BlockSize);
531 return leading + ret * BlockSize;
535 // Read last tailing block
538 LOG("Reading %i bytes from last block", tailings);
540 Buffer = (Uint8*)Buffer + num * BlockSize;
541 ret = ReadBlocks(block, 1, tmp, Argument);
543 LEAVE('X', leading + num * BlockSize);
544 return leading + num * BlockSize;
546 memcpy( Buffer, tmp, tailings );
553 Uint64 DrvUtil_WriteBlock(Uint64 Start, Uint64 Length, void *Buffer,
554 tDrvUtil_Callback ReadBlocks, tDrvUtil_Callback WriteBlocks,
555 Uint64 BlockSize, Uint Argument)
557 Uint8 tmp[BlockSize]; // C99
558 Uint64 block = Start / BlockSize;
559 int offset = Start - block * BlockSize;
560 int leading = BlockSize - offset;
565 ENTER("XStart XLength pBuffer pReadBlocks pWriteBlocks XBlockSize xArgument",
566 Start, Length, Buffer, ReadBlocks, WriteBlocks, BlockSize, Argument);
568 // Non aligned start, let's fix that!
571 if(leading > Length) leading = Length;
572 LOG("Writing %i bytes to Block1+%i", leading, offset);
573 // Read a copy of the block
574 ret = ReadBlocks(block, 1, tmp, Argument);
580 memcpy( &tmp[offset], Buffer, leading );
582 ret = WriteBlocks(block, 1, tmp, Argument);
588 if(leading == Length) {
593 Buffer = (Uint8*)Buffer + leading;
595 num = ( Length - leading ) / BlockSize;
596 tailings = Length - num * BlockSize - leading;
599 num = Length / BlockSize;
600 tailings = Length % BlockSize;
603 // Read central blocks
606 LOG("Writing %i blocks", num);
607 ret = WriteBlocks(block, num, Buffer, Argument);
609 LEAVE('X', leading + ret * BlockSize);
610 return leading + ret * BlockSize;
614 // Read last tailing block
617 LOG("Writing %i bytes to last block", tailings);
619 Buffer = (Uint8*)Buffer + num * BlockSize;
621 ret = ReadBlocks(block, 1, tmp, Argument);
623 LEAVE('X', leading + num * BlockSize);
624 return leading + num * BlockSize;
627 memcpy( tmp, Buffer, tailings );
629 ret = WriteBlocks(block, 1, tmp, Argument);
631 LEAVE('X', leading + num * BlockSize);
632 return leading + num * BlockSize;