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 -1, 0xFF000000, 0, 0, 0, 0, 0, 0,
37 -1, 0xFF000000, 0, 0, 0, 0, 0, 0,
38 -1, 0xFF000000, 0, 0, 0, 0, 0, 0,
39 -1, 0xFF000000, 0, 0, 0, 0, 0, 0,
40 -1, 0xFF000000, 0, 0, 0, 0, 0, 0,
41 -1, 0xFF000000, 0, 0, 0, 0, 0, 0,
42 -1, 0xFF000000, 0, 0, 0, 0, 0, 0,
43 -1, 0xFF000000, 0, 0, 0, 0, 0, 0,
44 -1, 0xFF000000, 0, 0, 0, 0, 0, 0,
45 -1, 0xFF000000, 0, 0, 0, 0, 0, 0,
46 -1, 0xFF000000, 0, 0, 0, 0, 0, 0,
47 -1, 0xFF000000, 0, 0, 0, 0, 0, 0,
48 -1, 0xFF000000, 0, 0, 0, 0, 0, 0,
49 -1, 0xFF000000, 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 ENTER("pFBInfo xOffset xLength pBuffer",
135 Mode, FBInfo, Offset, Length, Buffer);
136 switch( FBInfo->BufferFormat )
138 case VIDEO_BUFFMT_TEXT:
140 tVT_Char *chars = Buffer;
141 int bytes_per_px = (FBInfo->Depth + 7) / 8;
142 int widthInChars = FBInfo->Width/giVT_CharWidth;
143 int heightInChars = FBInfo->Height/giVT_CharHeight;
146 LOG("bytes_per_px = %i", bytes_per_px);
147 LOG("widthInChars = %i, heightInChars = %i", widthInChars, heightInChars);
149 Length /= sizeof(tVT_Char); Offset /= sizeof(tVT_Char);
151 x = Offset % widthInChars; y = Offset / widthInChars;
152 LOG("x = %i, y = %i", x, y);
155 if(Offset > heightInChars * widthInChars) LEAVE_RET('i', 0);
156 if(y >= heightInChars) LEAVE_RET('i', 0);
158 if( Offset + Length > heightInChars*widthInChars )
160 Length = heightInChars*widthInChars - Offset;
163 dest = FBInfo->Framebuffer;
164 LOG("dest = %p", dest);
165 dest += y * giVT_CharHeight * FBInfo->Pitch;
166 LOG("dest = %p", dest);
168 for( i = 0; i < Length; i++ )
170 if( y >= heightInChars )
172 Log_Notice("DrvUtil", "Stopped at %i", i);
178 dest + x*giVT_CharWidth*bytes_per_px, FBInfo->Depth, FBInfo->Pitch,
179 VT_Colour12toN(chars->BGCol, FBInfo->Depth),
180 VT_Colour12toN(chars->FGCol, FBInfo->Depth)
185 if( x >= widthInChars )
189 dest += FBInfo->Pitch*giVT_CharHeight;
190 LOG("dest = %p", dest);
193 Length = i * sizeof(tVT_Char);
197 case VIDEO_BUFFMT_FRAMEBUFFER:
198 if(FBInfo->Width*FBInfo->Height*4 < Offset+Length)
200 Log_Warning("DrvUtil", "DrvUtil_Video_WriteLFB - Framebuffer Overflow");
204 //TODO: Handle non 32-bpp framebuffer modes
205 if( FBInfo->Depth != 32 ) {
206 Log_Warning("DrvUtil", "DrvUtil_Video_WriteLFB - Don't support non 32-bpp FB mode");
211 //TODO: Handle pitch != Width*BytesPerPixel
212 // Copy to Frambuffer
213 dest = (Uint8 *)FBInfo->Framebuffer + Offset;
214 memcpy(dest, Buffer, Length);
217 case VIDEO_BUFFMT_2DSTREAM:
218 Length = DrvUtil_Video_2DStream(
219 FBInfo, Buffer, Length,
220 &gDrvUtil_Stub_2DFunctions, sizeof(gDrvUtil_Stub_2DFunctions)
232 void DrvUtil_Video_SetCursor(tDrvUtil_Video_BufInfo *Buf, tVideo_IOCtl_Bitmap *Bitmap)
234 int csrX = Buf->CursorX, csrY = Buf->CursorY;
238 if( Buf->CursorBitmap )
240 DrvUtil_Video_RemoveCursor(Buf);
241 if( !Bitmap || Bitmap->W != Buf->CursorBitmap->W || Bitmap->H != Buf->CursorBitmap->H )
243 free( Buf->CursorSaveBuf );
244 Buf->CursorSaveBuf = NULL;
246 if( Buf->CursorBitmap != &gDrvUtil_TextModeCursor)
247 free(Buf->CursorBitmap);
248 Buf->CursorBitmap = NULL;
251 // If the new bitmap is null, disable drawing
259 if( Bitmap != &gDrvUtil_TextModeCursor )
261 // Check the new bitmap is valid
262 size = sizeof(tVideo_IOCtl_Bitmap) + Bitmap->W*Bitmap->H*4;
263 if( !CheckMem(Bitmap, size) ) {
264 Log_Warning("DrvUtil", "DrvUtil_Video_SetCursor: Bitmap (%p) is in invalid memory", Bitmap);
269 Buf->CursorBitmap = malloc( size );
270 memcpy(Buf->CursorBitmap, Bitmap, size);
274 Buf->CursorBitmap = &gDrvUtil_TextModeCursor;
277 // Restore cursor position
278 DrvUtil_Video_DrawCursor(Buf, csrX, csrY);
281 void DrvUtil_Video_DrawCursor(tDrvUtil_Video_BufInfo *Buf, int X, int Y)
283 int render_ox=0, render_oy=0, render_w, render_h;
285 DrvUtil_Video_RemoveCursor(Buf);
287 // X < 0 disables the cursor
294 if( X < 0 || Y < 0 ) return;
295 if( X >= Buf->Width || Y >= Buf->Height ) return;
297 // Ensure the cursor is enabled
298 if( !Buf->CursorBitmap ) return ;
300 // Save cursor position (for changing the bitmap)
301 Buf->CursorX = X; Buf->CursorY = Y;
303 // Apply cursor's center offset
304 X -= Buf->CursorBitmap->XOfs;
305 Y -= Buf->CursorBitmap->YOfs;
307 // Get the width of the cursor on screen (clipping to right/bottom edges)
308 render_w = X > Buf->Width - Buf->CursorBitmap->W ? Buf->Width - X : Buf->CursorBitmap->W;
309 render_h = Y > Buf->Height - Buf->CursorBitmap->H ? Buf->Height - Y : Buf->CursorBitmap->H;
311 // Clipp to left/top edges
312 if(X < 0) { render_ox = -X; X = 0; }
313 if(Y < 0) { render_oy = -Y; Y = 0; }
316 Buf->CursorRenderW = render_w; Buf->CursorRenderH = render_h;
317 Buf->CursorDestX = X; Buf->CursorDestY = Y;
318 Buf->CursorReadX = render_ox; Buf->CursorReadY = render_oy;
320 // Call render routine
321 DrvUtil_Video_RenderCursor(Buf);
324 void DrvUtil_Video_RenderCursor(tDrvUtil_Video_BufInfo *Buf)
326 int src_x = Buf->CursorReadX, src_y = Buf->CursorReadY;
327 int render_w = Buf->CursorRenderW, render_h = Buf->CursorRenderH;
328 int dest_x = Buf->CursorDestX, dest_y = Buf->CursorDestY;
329 int bytes_per_px = (Buf->Depth + 7) / 8;
330 int save_pitch = Buf->CursorBitmap->W * bytes_per_px;
335 // Debug("DrvUtil_Video_RenderCursor: (Buf=%p) dest_x=%i, dest_y=%i", Buf, dest_x, dest_y);
337 dest = (Uint8*)Buf->Framebuffer + dest_y*Buf->Pitch + dest_x*bytes_per_px;
338 src = Buf->CursorBitmap->Data + src_y * Buf->CursorBitmap->W + src_x;
340 // Allocate save buffer if not already
341 if( !Buf->CursorSaveBuf )
342 Buf->CursorSaveBuf = malloc( Buf->CursorBitmap->W*Buf->CursorBitmap->H*bytes_per_px );
344 // Save behind the cursor
345 for( y = 0; y < render_h; y ++ )
347 (Uint8*)Buf->CursorSaveBuf + y*save_pitch,
348 (Uint8*)dest + y*Buf->Pitch,
349 render_w*bytes_per_px
355 for( y = 0; y < render_h; y ++ )
357 for(x = 0; x < render_w; x ++ )
359 // TODO: Should I implement alpha blending?
360 if(src[x] & 0xFF000000)
361 ((Uint32*)dest)[x] = src[x];
363 ; // NOP, completely transparent
365 src += Buf->CursorBitmap->W;
366 dest = (Uint8*)dest + Buf->Pitch;
370 Log_Error("DrvUtil", "TODO: Implement non 32-bpp cursor");
375 void DrvUtil_Video_RemoveCursor(tDrvUtil_Video_BufInfo *Buf)
377 int bytes_per_px = (Buf->Depth + 7) / 8;
381 // Just a little sanity
382 if( !Buf->CursorBitmap || Buf->CursorX == -1 ) return ;
383 if( !Buf->CursorSaveBuf ) return ;
385 // Debug("DrvUtil_Video_RemoveCursor: (Buf=%p) dest_x=%i, dest_y=%i", Buf, Buf->CursorDestX, Buf->CursorDestY);
388 save_pitch = Buf->CursorBitmap->W * bytes_per_px;
389 dest = (Uint8*)Buf->Framebuffer + Buf->CursorDestY * Buf->Pitch + Buf->CursorDestX*bytes_per_px;
390 src = Buf->CursorSaveBuf;
392 // Copy each line back
393 for( y = 0; y < Buf->CursorRenderH; y ++ )
395 memcpy( dest, src, Buf->CursorRenderW * bytes_per_px );
400 // Set the cursor as removed
404 void DrvUtil_Video_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour)
406 tDrvUtil_Video_BufInfo *FBInfo = Ent;
408 // TODO: Handle non-32bit modes
409 if( FBInfo->Depth != 32 ) return;
411 // TODO: Be less hacky
412 int pitch = FBInfo->Pitch/4;
413 Uint32 *buf = (Uint32*)FBInfo->Framebuffer + Y*pitch + X;
418 for(i=W;i--;tmp++) *tmp = Colour;
423 void DrvUtil_Video_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H)
425 tDrvUtil_Video_BufInfo *FBInfo = Ent;
426 int scrnpitch = FBInfo->Pitch;
427 int bytes_per_px = (FBInfo->Depth + 7) / 8;
428 int dst = DstY*scrnpitch + DstX;
429 int src = SrcY*scrnpitch + SrcX;
432 //Log("Vesa_2D_Blit: (Ent=%p, DstX=%i, DstY=%i, SrcX=%i, SrcY=%i, W=%i, H=%i)",
433 // Ent, DstX, DstY, SrcX, SrcY, W, H);
435 if(SrcX + W > FBInfo->Width) W = FBInfo->Width - SrcX;
436 if(DstX + W > FBInfo->Width) W = FBInfo->Width - DstX;
437 if(SrcY + H > FBInfo->Height) H = FBInfo->Height - SrcY;
438 if(DstY + H > FBInfo->Height) H = FBInfo->Height - DstY;
440 //Debug("W = %i, H = %i", W, H);
449 tmp = W*bytes_per_px;
450 for( tmp = W; tmp --; ) {
451 *((Uint8*)FBInfo->Framebuffer + dst + tmp) = *((Uint8*)FBInfo->Framebuffer + src + tmp);
458 memcpy((Uint8*)FBInfo->Framebuffer + dst, (Uint8*)FBInfo->Framebuffer + src, W*bytes_per_px);
463 //Log("Vesa_2D_Blit: RETURN");
467 // --- Disk Driver Helpers ---
468 Uint64 DrvUtil_ReadBlock(Uint64 Start, Uint64 Length, void *Buffer,
469 tDrvUtil_Callback ReadBlocks, Uint64 BlockSize, Uint Argument)
471 Uint8 tmp[BlockSize]; // C99
472 Uint64 block = Start / BlockSize;
473 int offset = Start - block * BlockSize;
474 int leading = BlockSize - offset;
479 ENTER("XStart XLength pBuffer pReadBlocks XBlockSize xArgument",
480 Start, Length, Buffer, ReadBlocks, BlockSize, Argument);
482 // Non aligned start, let's fix that!
485 if(leading > Length) leading = Length;
486 LOG("Reading %i bytes from Block1+%i", leading, offset);
487 ret = ReadBlocks(block, 1, tmp, Argument);
492 memcpy( Buffer, &tmp[offset], leading );
494 if(leading == Length) {
499 Buffer = (Uint8*)Buffer + leading;
501 num = ( Length - leading ) / BlockSize;
502 tailings = Length - num * BlockSize - leading;
505 num = Length / BlockSize;
506 tailings = Length % BlockSize;
509 // Read central blocks
512 LOG("Reading %i blocks", num);
513 ret = ReadBlocks(block, num, Buffer, Argument);
515 LEAVE('X', leading + ret * BlockSize);
516 return leading + ret * BlockSize;
520 // Read last tailing block
523 LOG("Reading %i bytes from last block", tailings);
525 Buffer = (Uint8*)Buffer + num * BlockSize;
526 ret = ReadBlocks(block, 1, tmp, Argument);
528 LEAVE('X', leading + num * BlockSize);
529 return leading + num * BlockSize;
531 memcpy( Buffer, tmp, tailings );
538 Uint64 DrvUtil_WriteBlock(Uint64 Start, Uint64 Length, void *Buffer,
539 tDrvUtil_Callback ReadBlocks, tDrvUtil_Callback WriteBlocks,
540 Uint64 BlockSize, Uint Argument)
542 Uint8 tmp[BlockSize]; // C99
543 Uint64 block = Start / BlockSize;
544 int offset = Start - block * BlockSize;
545 int leading = BlockSize - offset;
550 ENTER("XStart XLength pBuffer pReadBlocks pWriteBlocks XBlockSize xArgument",
551 Start, Length, Buffer, ReadBlocks, WriteBlocks, BlockSize, Argument);
553 // Non aligned start, let's fix that!
556 if(leading > Length) leading = Length;
557 LOG("Writing %i bytes to Block1+%i", leading, offset);
558 // Read a copy of the block
559 ret = ReadBlocks(block, 1, tmp, Argument);
565 memcpy( &tmp[offset], Buffer, leading );
567 ret = WriteBlocks(block, 1, tmp, Argument);
573 if(leading == Length) {
578 Buffer = (Uint8*)Buffer + leading;
580 num = ( Length - leading ) / BlockSize;
581 tailings = Length - num * BlockSize - leading;
584 num = Length / BlockSize;
585 tailings = Length % BlockSize;
588 // Read central blocks
591 LOG("Writing %i blocks", num);
592 ret = WriteBlocks(block, num, Buffer, Argument);
594 LEAVE('X', leading + ret * BlockSize);
595 return leading + ret * BlockSize;
599 // Read last tailing block
602 LOG("Writing %i bytes to last block", tailings);
604 Buffer = (Uint8*)Buffer + num * BlockSize;
606 ret = ReadBlocks(block, 1, tmp, Argument);
608 LEAVE('X', leading + num * BlockSize);
609 return leading + num * BlockSize;
612 memcpy( tmp, Buffer, tailings );
614 ret = WriteBlocks(block, 1, tmp, Argument);
616 LEAVE('X', leading + num * BlockSize);
617 return leading + num * BlockSize;