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)
134 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 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 )
239 // Pitch isn't 4*Width
240 x = Offset % FBInfo->Width;
241 y = Offset / FBInfo->Height;
243 dest = (Uint8*)FBInfo->Framebuffer + y*FBInfo->Pitch;
245 for( ; Length >= 4; Length -= 4, x )
247 ((Uint32*)dest)[x++] = *src ++;
248 if( x == FBInfo->Width ) {
250 dest += FBInfo->Pitch;
256 dest = (Uint8 *)FBInfo->Framebuffer + Offset;
257 memcpy(dest, Buffer, Length);
261 Log_Warning("DrvUtil", "DrvUtil_Video_WriteLFB - Unknown bit depthn %i", FBInfo->Depth);
266 case VIDEO_BUFFMT_2DSTREAM:
267 Length = DrvUtil_Video_2DStream(
268 FBInfo, Buffer, Length,
269 &gDrvUtil_Stub_2DFunctions, sizeof(gDrvUtil_Stub_2DFunctions)
278 DrvUtil_Video_DrawCursor(FBInfo, csr_x, csr_y);
284 int DrvUtil_Video_SetCursor(tDrvUtil_Video_BufInfo *Buf, tVideo_IOCtl_Bitmap *Bitmap)
286 int csrX = Buf->CursorX, csrY = Buf->CursorY;
290 if( Buf->CursorBitmap )
292 DrvUtil_Video_RemoveCursor(Buf);
293 if( !Bitmap || Bitmap->W != Buf->CursorBitmap->W || Bitmap->H != Buf->CursorBitmap->H )
295 free( Buf->CursorSaveBuf );
296 Buf->CursorSaveBuf = NULL;
298 if( Buf->CursorBitmap != &gDrvUtil_TextModeCursor)
299 free(Buf->CursorBitmap);
300 Buf->CursorBitmap = NULL;
303 // If the new bitmap is null, disable drawing
311 // Sanity check the bitmap
312 if( !CheckMem(Bitmap, sizeof(*Bitmap)) || !CheckMem(Bitmap->Data, Bitmap->W*Bitmap->H*sizeof(Uint32)) )
314 Log_Warning("DrvUtil", "DrvUtil_Video_SetCursor: Bitmap (%p) is in invalid memory", Bitmap);
319 // Don't take a copy of the DrvUtil provided cursor
320 if( Bitmap == &gDrvUtil_TextModeCursor )
322 Buf->CursorBitmap = Bitmap;
326 size = sizeof(tVideo_IOCtl_Bitmap) + Bitmap->W*Bitmap->H*4;
329 Buf->CursorBitmap = malloc( size );
330 memcpy(Buf->CursorBitmap, Bitmap, size);
333 // Restore cursor position
334 DrvUtil_Video_DrawCursor(Buf, csrX, csrY);
338 void DrvUtil_Video_DrawCursor(tDrvUtil_Video_BufInfo *Buf, int X, int Y)
340 int render_ox=0, render_oy=0, render_w, render_h;
342 DrvUtil_Video_RemoveCursor(Buf);
344 // X < 0 disables the cursor
351 if( X < 0 || Y < 0 ) return;
352 if( X >= Buf->Width || Y >= Buf->Height ) return;
354 // Ensure the cursor is enabled
355 if( !Buf->CursorBitmap ) return ;
357 // Save cursor position (for changing the bitmap)
358 Buf->CursorX = X; Buf->CursorY = Y;
360 // Apply cursor's center offset
361 X -= Buf->CursorBitmap->XOfs;
362 Y -= Buf->CursorBitmap->YOfs;
364 // Get the width of the cursor on screen (clipping to right/bottom edges)
365 render_w = X > Buf->Width - Buf->CursorBitmap->W ? Buf->Width - X : Buf->CursorBitmap->W;
366 render_h = Y > Buf->Height - Buf->CursorBitmap->H ? Buf->Height - Y : Buf->CursorBitmap->H;
368 // Clipp to left/top edges
369 if(X < 0) { render_ox = -X; X = 0; }
370 if(Y < 0) { render_oy = -Y; Y = 0; }
373 Buf->CursorRenderW = render_w; Buf->CursorRenderH = render_h;
374 Buf->CursorDestX = X; Buf->CursorDestY = Y;
375 Buf->CursorReadX = render_ox; Buf->CursorReadY = render_oy;
377 // Call render routine
378 DrvUtil_Video_RenderCursor(Buf);
381 void DrvUtil_Video_RenderCursor(tDrvUtil_Video_BufInfo *Buf)
383 int src_x = Buf->CursorReadX, src_y = Buf->CursorReadY;
384 int render_w = Buf->CursorRenderW, render_h = Buf->CursorRenderH;
385 int dest_x = Buf->CursorDestX, dest_y = Buf->CursorDestY;
386 int bytes_per_px = (Buf->Depth + 7) / 8;
387 int save_pitch = Buf->CursorBitmap->W * bytes_per_px;
392 dest = (Uint8*)Buf->Framebuffer + dest_y*Buf->Pitch + dest_x*bytes_per_px;
393 src = Buf->CursorBitmap->Data + src_y * Buf->CursorBitmap->W + src_x;
395 // Allocate save buffer if not already
396 if( !Buf->CursorSaveBuf )
397 Buf->CursorSaveBuf = malloc( Buf->CursorBitmap->W*Buf->CursorBitmap->H*bytes_per_px );
399 // Save behind the cursor
400 for( y = 0; y < render_h; y ++ )
402 (Uint8*)Buf->CursorSaveBuf + y*save_pitch,
403 (Uint8*)dest + y*Buf->Pitch,
404 render_w*bytes_per_px
412 Log_Warning("DrvUtil", "TODO: Support 15/16 bpp modes in cursor draw");
415 for( y = 0; y < render_h; y ++ )
419 for(x = 0; x < render_w; x ++, px += 3)
421 Uint32 value = src[x];
422 // TODO: Should I implement alpha blending?
423 if(value & 0xFF000000)
425 px[0] = value & 0xFF;
426 px[1] = (value >> 8) & 0xFF;
427 px[2] = (value >> 16) & 0xFF;
432 src += Buf->CursorBitmap->W;
433 dest = (Uint8*)dest + Buf->Pitch;
437 for( y = 0; y < render_h; y ++ )
441 for(x = 0; x < render_w; x ++, px ++)
443 Uint32 value = src[x];
444 // TODO: Should I implement alpha blending?
445 if(value & 0xFF000000)
448 ; // NOP, completely transparent
450 src += Buf->CursorBitmap->W;
451 dest = (Uint8*)dest + Buf->Pitch;
455 Log_Error("DrvUtil", "RenderCursor - Unknown bit depth %i", Buf->Depth);
461 void DrvUtil_Video_RemoveCursor(tDrvUtil_Video_BufInfo *Buf)
463 int bytes_per_px = (Buf->Depth + 7) / 8;
467 // Just a little sanity
468 if( !Buf->CursorBitmap || Buf->CursorX == -1 ) return ;
469 if( !Buf->CursorSaveBuf ) return ;
471 // Debug("DrvUtil_Video_RemoveCursor: (Buf=%p) dest_x=%i, dest_y=%i", Buf, Buf->CursorDestX, Buf->CursorDestY);
474 save_pitch = Buf->CursorBitmap->W * bytes_per_px;
475 dest = (Uint8*)Buf->Framebuffer + Buf->CursorDestY * Buf->Pitch + Buf->CursorDestX*bytes_per_px;
476 src = Buf->CursorSaveBuf;
478 // Copy each line back
479 for( y = 0; y < Buf->CursorRenderH; y ++ )
481 memcpy( dest, src, Buf->CursorRenderW * bytes_per_px );
486 // Set the cursor as removed
490 void DrvUtil_Video_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour)
492 tDrvUtil_Video_BufInfo *FBInfo = Ent;
494 // TODO: Handle non-32bit modes
495 if( FBInfo->Depth != 32 ) return;
497 // TODO: Be less hacky
498 int pitch = FBInfo->Pitch/4;
499 Uint32 *buf = (Uint32*)FBInfo->Framebuffer + Y*pitch + X;
504 for(i=W;i--;tmp++) *tmp = Colour;
509 void DrvUtil_Video_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H)
511 tDrvUtil_Video_BufInfo *FBInfo = Ent;
512 int scrnpitch = FBInfo->Pitch;
513 int bytes_per_px = (FBInfo->Depth + 7) / 8;
514 int dst = DstY*scrnpitch + DstX;
515 int src = SrcY*scrnpitch + SrcX;
518 //Log("Vesa_2D_Blit: (Ent=%p, DstX=%i, DstY=%i, SrcX=%i, SrcY=%i, W=%i, H=%i)",
519 // Ent, DstX, DstY, SrcX, SrcY, W, H);
521 if(SrcX + W > FBInfo->Width) W = FBInfo->Width - SrcX;
522 if(DstX + W > FBInfo->Width) W = FBInfo->Width - DstX;
523 if(SrcY + H > FBInfo->Height) H = FBInfo->Height - SrcY;
524 if(DstY + H > FBInfo->Height) H = FBInfo->Height - DstY;
526 //Debug("W = %i, H = %i", W, H);
535 tmp = W*bytes_per_px;
536 for( tmp = W; tmp --; ) {
537 *((Uint8*)FBInfo->Framebuffer + dst + tmp) = *((Uint8*)FBInfo->Framebuffer + src + tmp);
544 memcpy((Uint8*)FBInfo->Framebuffer + dst, (Uint8*)FBInfo->Framebuffer + src, W*bytes_per_px);
549 //Log("Vesa_2D_Blit: RETURN");
553 // --- Disk Driver Helpers ---
554 Uint64 DrvUtil_ReadBlock(Uint64 Start, Uint64 Length, void *Buffer,
555 tDrvUtil_Callback ReadBlocks, 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 XBlockSize xArgument",
566 Start, Length, Buffer, ReadBlocks, BlockSize, Argument);
568 // Non aligned start, let's fix that!
571 if(leading > Length) leading = Length;
572 LOG("Reading %i bytes from Block1+%i", leading, offset);
573 ret = ReadBlocks(block, 1, tmp, Argument);
578 memcpy( Buffer, &tmp[offset], leading );
580 if(leading == Length) {
585 Buffer = (Uint8*)Buffer + leading;
587 num = ( Length - leading ) / BlockSize;
588 tailings = Length - num * BlockSize - leading;
591 num = Length / BlockSize;
592 tailings = Length % BlockSize;
595 // Read central blocks
598 LOG("Reading %i blocks", num);
599 ret = ReadBlocks(block, num, Buffer, Argument);
601 LEAVE('X', leading + ret * BlockSize);
602 return leading + ret * BlockSize;
606 // Read last tailing block
609 LOG("Reading %i bytes from last block", tailings);
611 Buffer = (Uint8*)Buffer + num * BlockSize;
612 ret = ReadBlocks(block, 1, tmp, Argument);
614 LEAVE('X', leading + num * BlockSize);
615 return leading + num * BlockSize;
617 memcpy( Buffer, tmp, tailings );
624 Uint64 DrvUtil_WriteBlock(Uint64 Start, Uint64 Length, void *Buffer,
625 tDrvUtil_Callback ReadBlocks, tDrvUtil_Callback WriteBlocks,
626 Uint64 BlockSize, Uint Argument)
628 Uint8 tmp[BlockSize]; // C99
629 Uint64 block = Start / BlockSize;
630 int offset = Start - block * BlockSize;
631 int leading = BlockSize - offset;
636 ENTER("XStart XLength pBuffer pReadBlocks pWriteBlocks XBlockSize xArgument",
637 Start, Length, Buffer, ReadBlocks, WriteBlocks, BlockSize, Argument);
639 // Non aligned start, let's fix that!
642 if(leading > Length) leading = Length;
643 LOG("Writing %i bytes to Block1+%i", leading, offset);
644 // Read a copy of the block
645 ret = ReadBlocks(block, 1, tmp, Argument);
651 memcpy( &tmp[offset], Buffer, leading );
653 ret = WriteBlocks(block, 1, tmp, Argument);
659 if(leading == Length) {
664 Buffer = (Uint8*)Buffer + leading;
666 num = ( Length - leading ) / BlockSize;
667 tailings = Length - num * BlockSize - leading;
670 num = Length / BlockSize;
671 tailings = Length % BlockSize;
674 // Read central blocks
677 LOG("Writing %i blocks", num);
678 ret = WriteBlocks(block, num, Buffer, Argument);
680 LEAVE('X', leading + ret * BlockSize);
681 return leading + ret * BlockSize;
685 // Read last tailing block
688 LOG("Writing %i bytes to last block", tailings);
690 Buffer = (Uint8*)Buffer + num * BlockSize;
692 ret = ReadBlocks(block, 1, tmp, Argument);
694 LEAVE('X', leading + num * BlockSize);
695 return leading + num * BlockSize;
698 memcpy( tmp, Buffer, tailings );
700 ret = WriteBlocks(block, 1, tmp, Argument);
702 LEAVE('X', leading + num * BlockSize);
703 return leading + num * BlockSize;