6 * - Video Driver Helper Functions
10 #include <api_drv_video.h>
15 //int DrvUtil_Video_2DStream(void *Ent, void *Buffer, int Length, tDrvUtil_Video_2DHandlers *Handlers, int SizeofHandlers);
16 //size_t DrvUtil_Video_WriteLFB(int Mode, tDrvUtil_Video_BufInfo *FBInfo, size_t Offset, size_t Length, void *Src);
17 //void DrvUtil_Video_SetCursor(tDrvUtil_Video_BufInfo *Buf, tVideo_IOCtl_Bitmap *Bitmap);
18 //void DrvUtil_Video_DrawCursor(tDrvUtil_Video_BufInfo *Buf, int X, int Y);
19 void DrvUtil_Video_RenderCursor(tDrvUtil_Video_BufInfo *Buf);
20 //void DrvUtil_Video_RemoveCursor(tDrvUtil_Video_BufInfo *Buf);
21 void DrvUtil_Video_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour);
22 void DrvUtil_Video_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H);
25 tDrvUtil_Video_2DHandlers gDrvUtil_Stub_2DFunctions = {
27 DrvUtil_Video_2D_Fill,
30 tVideo_IOCtl_Bitmap gDrvUtil_TextModeCursor = {
34 0, 0, 0 , 0, 0, 0, 0, 0,
35 0,-1, 0xFF000000, 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, 0, 0 , 0, 0, 0, 0, 0,
49 0, 0, 0 , 0, 0, 0, 0, 0
54 // --- Video Driver Helpers ---
55 int DrvUtil_Video_2DStream(void *Ent, const void *Buffer, int Length,
56 tDrvUtil_Video_2DHandlers *Handlers, int SizeofHandlers)
58 const Uint8 *stream = Buffer;
67 if(op > NUM_VIDEO_2DOPS) {
68 Log_Warning("DrvUtil",
69 "DrvUtil_Video_2DStream: Unknown operation %i",
74 if(op*sizeof(void*) > SizeofHandlers) {
75 Log_Warning("DrvUtil",
76 "DrvUtil_Video_2DStream: Driver does not support op %i",
83 case VIDEO_2DOP_NOP: break;
86 if(rem < 10) return Length-rem;
89 Log_Warning("DrvUtil", "DrvUtil_Video_2DStream: Driver"
90 " does not support VIDEO_2DOP_FILL");
96 ((const Uint16*)stream)[0], ((const Uint16*)stream)[1],
97 ((const Uint16*)stream)[2], ((const Uint16*)stream)[3],
98 ((const Uint32*)stream)[4]
105 case VIDEO_2DOP_BLIT:
106 if(rem < 12) return Length-rem;
108 if(!Handlers->Blit) {
109 Log_Warning("DrvUtil", "DrvUtil_Video_2DStream: Driver"
110 " does not support VIDEO_2DOP_BLIT");
116 ((const Uint16*)stream)[0], ((const Uint16*)stream)[1],
117 ((const Uint16*)stream)[2], ((const Uint16*)stream)[3],
118 ((const Uint16*)stream)[4], ((const Uint16*)stream)[5]
130 int DrvUtil_Video_WriteLFB(tDrvUtil_Video_BufInfo *FBInfo, size_t Offset, size_t Length, const void *Buffer)
133 const Uint32 *src = Buffer;
136 int bytes_per_px = (FBInfo->Depth + 7) / 8;
137 ENTER("pFBInfo xOffset xLength pBuffer",
138 FBInfo, Offset, Length, Buffer);
140 csr_x = FBInfo->CursorX;
141 csr_y = FBInfo->CursorY;
143 DrvUtil_Video_RemoveCursor(FBInfo);
145 switch( FBInfo->BufferFormat )
147 case VIDEO_BUFFMT_TEXT:
149 const tVT_Char *chars = Buffer;
150 int widthInChars = FBInfo->Width/giVT_CharWidth;
151 int heightInChars = FBInfo->Height/giVT_CharHeight;
154 LOG("bytes_per_px = %i", bytes_per_px);
155 LOG("widthInChars = %i, heightInChars = %i", widthInChars, heightInChars);
157 Length /= sizeof(tVT_Char); Offset /= sizeof(tVT_Char);
159 x = Offset % widthInChars; y = Offset / widthInChars;
160 LOG("x = %i, y = %i", x, y);
163 if(Offset > heightInChars * widthInChars) LEAVE_RET('i', 0);
164 if(y >= heightInChars) LEAVE_RET('i', 0);
166 if( Offset + Length > heightInChars*widthInChars )
168 Length = heightInChars*widthInChars - Offset;
171 dest = FBInfo->Framebuffer;
172 LOG("dest = %p", dest);
173 dest += y * giVT_CharHeight * FBInfo->Pitch;
174 LOG("dest = %p", dest);
176 for( i = 0; i < Length; i++ )
178 if( y >= heightInChars )
180 Log_Notice("DrvUtil", "Stopped at %i", i);
186 dest + x*giVT_CharWidth*bytes_per_px, FBInfo->Depth, FBInfo->Pitch,
187 VT_Colour12toN(chars->BGCol, FBInfo->Depth),
188 VT_Colour12toN(chars->FGCol, FBInfo->Depth)
193 if( x >= widthInChars )
197 dest += FBInfo->Pitch*giVT_CharHeight;
198 LOG("dest = %p", dest);
201 Length = i * sizeof(tVT_Char);
205 case VIDEO_BUFFMT_FRAMEBUFFER:
206 if(FBInfo->Width*FBInfo->Height*4 < Offset+Length)
208 Log_Warning("DrvUtil", "DrvUtil_Video_WriteLFB - Framebuffer Overflow");
212 switch(FBInfo->Depth)
216 Log_Warning("DrvUtil", "TODO: Support 15/16 bpp modes in LFB write");
219 x = Offset % FBInfo->Width;
220 y = Offset / FBInfo->Width;
221 dest = (Uint8*)FBInfo->Framebuffer + y*FBInfo->Pitch;
222 for( ; Length >= 4; Length -= 4 )
224 dest[x*3+0] = *src & 0xFF;
225 dest[x*3+1] = (*src >> 8) & 0xFF;
226 dest[x*3+2] = (*src >> 16) & 0xFF;
228 if(x == FBInfo->Width) {
229 dest += FBInfo->Pitch;
235 // Copy to Frambuffer
236 if( FBInfo->Pitch != FBInfo->Width*4 )
239 // Pitch isn't 4*Width
240 x = Offset % FBInfo->Width;
241 y = Offset / FBInfo->Height;
243 px = (Uint32*)FBInfo->Framebuffer + y*FBInfo->Pitch/4;
245 for( ; Length >= 4; Length -= 4, x )
248 if( x == FBInfo->Width ) {
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;
289 ENTER("pBuf pBitmap", Buf, Bitmap);
292 if( Buf->CursorBitmap )
294 LOG("Clearing old cursor");
295 DrvUtil_Video_RemoveCursor(Buf);
296 if( !Bitmap || Bitmap->W != Buf->CursorBitmap->W || Bitmap->H != Buf->CursorBitmap->H )
298 free( Buf->CursorSaveBuf );
299 Buf->CursorSaveBuf = NULL;
301 if( Buf->CursorBitmap != &gDrvUtil_TextModeCursor)
302 free(Buf->CursorBitmap);
303 Buf->CursorBitmap = NULL;
306 // If the new bitmap is null, disable drawing
315 // Sanity check the bitmap
316 LOG("Sanity checking plox");
317 if( !CheckMem(Bitmap, sizeof(*Bitmap)) || !CheckMem(Bitmap->Data, Bitmap->W*Bitmap->H*sizeof(Uint32)) )
319 Log_Warning("DrvUtil", "DrvUtil_Video_SetCursor: Bitmap (%p) is in invalid memory", Bitmap);
325 // Don't take a copy of the DrvUtil provided cursor
326 if( Bitmap == &gDrvUtil_TextModeCursor )
328 LOG("No copy (provided cursor)");
329 Buf->CursorBitmap = Bitmap;
334 size = sizeof(tVideo_IOCtl_Bitmap) + Bitmap->W*Bitmap->H*4;
337 Buf->CursorBitmap = malloc( size );
338 memcpy(Buf->CursorBitmap, Bitmap, size);
341 // Restore cursor position
343 DrvUtil_Video_DrawCursor(Buf, csrX, csrY);
348 void DrvUtil_Video_DrawCursor(tDrvUtil_Video_BufInfo *Buf, int X, int Y)
350 int render_ox=0, render_oy=0, render_w, render_h;
352 ENTER("pBuf iX iY", Buf, X, Y);
353 DrvUtil_Video_RemoveCursor(Buf);
355 // X < 0 disables the cursor
363 if( X < 0 || Y < 0 || X >= Buf->Width || Y >= Buf->Height ) {
368 // Ensure the cursor is enabled
369 if( !Buf->CursorBitmap ) {
374 // Save cursor position (for changing the bitmap)
375 Buf->CursorX = X; Buf->CursorY = Y;
376 // Apply cursor's center offset
377 X -= Buf->CursorBitmap->XOfs;
378 Y -= Buf->CursorBitmap->YOfs;
380 // Get the width of the cursor on screen (clipping to right/bottom edges)
381 render_w = X > Buf->Width - Buf->CursorBitmap->W ? Buf->Width - X : Buf->CursorBitmap->W;
382 render_h = Y > Buf->Height - Buf->CursorBitmap->H ? Buf->Height - Y : Buf->CursorBitmap->H;
384 // Clipp to left/top edges
385 if(X < 0) { render_ox = -X; X = 0; }
386 if(Y < 0) { render_oy = -Y; Y = 0; }
389 Buf->CursorRenderW = render_w; Buf->CursorRenderH = render_h;
390 Buf->CursorDestX = X; Buf->CursorDestY = Y;
391 Buf->CursorReadX = render_ox; Buf->CursorReadY = render_oy;
393 LOG("%ix%i at %i,%i offset %i,%i",
394 render_w, render_h, X, Y, render_ox, render_oy);
396 // Call render routine
397 DrvUtil_Video_RenderCursor(Buf);
401 void DrvUtil_Video_RenderCursor(tDrvUtil_Video_BufInfo *Buf)
403 int src_x = Buf->CursorReadX, src_y = Buf->CursorReadY;
404 int render_w = Buf->CursorRenderW, render_h = Buf->CursorRenderH;
405 int dest_x = Buf->CursorDestX, dest_y = Buf->CursorDestY;
406 int bytes_per_px = (Buf->Depth + 7) / 8;
407 int save_pitch = Buf->CursorBitmap->W * bytes_per_px;
412 dest = (Uint8*)Buf->Framebuffer + dest_y*Buf->Pitch + dest_x*bytes_per_px;
413 src = Buf->CursorBitmap->Data + src_y * Buf->CursorBitmap->W + src_x;
415 LOG("dest = %p, src = %p", dest, src);
417 // Allocate save buffer if not already
418 if( !Buf->CursorSaveBuf )
419 Buf->CursorSaveBuf = malloc( Buf->CursorBitmap->W*Buf->CursorBitmap->H*bytes_per_px );
422 // Save behind the cursor
423 for( y = 0; y < render_h; y ++ )
425 (Uint8*)Buf->CursorSaveBuf + y*save_pitch,
426 (Uint8*)dest + y*Buf->Pitch,
427 render_w*bytes_per_px
435 Log_Warning("DrvUtil", "TODO: Support 15/16 bpp modes in cursor draw");
438 LOG("24-bit render");
439 for( y = 0; y < render_h; y ++ )
443 for(x = 0; x < render_w; x ++, px += 3)
445 Uint32 value = src[x];
446 // TODO: Should I implement alpha blending?
447 if(value & 0xFF000000)
449 px[0] = value & 0xFF;
450 px[1] = (value >> 8) & 0xFF;
451 px[2] = (value >> 16) & 0xFF;
456 src += Buf->CursorBitmap->W;
457 dest = (Uint8*)dest + Buf->Pitch;
461 LOG("32-bit render");
462 for( y = 0; y < render_h; y ++ )
466 for(x = 0; x < render_w; x ++, px ++)
468 Uint32 value = src[x];
469 // TODO: Should I implement alpha blending?
470 if(value & 0xFF000000)
473 ; // NOP, completely transparent
475 LOG("row %i/%i (%p-%P) done", y+1, render_h, dest, MM_GetPhysAddr(dest));
476 src += Buf->CursorBitmap->W;
477 dest = (Uint8*)dest + Buf->Pitch;
481 Log_Error("DrvUtil", "RenderCursor - Unknown bit depth %i", Buf->Depth);
487 void DrvUtil_Video_RemoveCursor(tDrvUtil_Video_BufInfo *Buf)
489 int bytes_per_px = (Buf->Depth + 7) / 8;
493 // Just a little sanity
494 if( !Buf->CursorBitmap || Buf->CursorX == -1 ) return ;
495 if( !Buf->CursorSaveBuf ) return ;
497 // Debug("DrvUtil_Video_RemoveCursor: (Buf=%p) dest_x=%i, dest_y=%i", Buf, Buf->CursorDestX, Buf->CursorDestY);
500 save_pitch = Buf->CursorBitmap->W * bytes_per_px;
501 dest = (Uint8*)Buf->Framebuffer + Buf->CursorDestY * Buf->Pitch + Buf->CursorDestX*bytes_per_px;
502 src = Buf->CursorSaveBuf;
504 // Copy each line back
505 for( y = 0; y < Buf->CursorRenderH; y ++ )
507 memcpy( dest, src, Buf->CursorRenderW * bytes_per_px );
512 // Set the cursor as removed
516 void DrvUtil_Video_2D_Fill(void *Ent, Uint16 X, Uint16 Y, Uint16 W, Uint16 H, Uint32 Colour)
518 tDrvUtil_Video_BufInfo *FBInfo = Ent;
520 // TODO: Handle non-32bit modes
521 if( FBInfo->Depth != 32 ) return;
523 // TODO: Be less hacky
524 int pitch = FBInfo->Pitch/4;
525 Uint32 *buf = (Uint32*)FBInfo->Framebuffer + Y*pitch + X;
530 for(i=W;i--;tmp++) *tmp = Colour;
535 void DrvUtil_Video_2D_Blit(void *Ent, Uint16 DstX, Uint16 DstY, Uint16 SrcX, Uint16 SrcY, Uint16 W, Uint16 H)
537 tDrvUtil_Video_BufInfo *FBInfo = Ent;
538 int scrnpitch = FBInfo->Pitch;
539 int bytes_per_px = (FBInfo->Depth + 7) / 8;
540 int dst = DstY*scrnpitch + DstX;
541 int src = SrcY*scrnpitch + SrcX;
544 //Log("Vesa_2D_Blit: (Ent=%p, DstX=%i, DstY=%i, SrcX=%i, SrcY=%i, W=%i, H=%i)",
545 // Ent, DstX, DstY, SrcX, SrcY, W, H);
547 if(SrcX + W > FBInfo->Width) W = FBInfo->Width - SrcX;
548 if(DstX + W > FBInfo->Width) W = FBInfo->Width - DstX;
549 if(SrcY + H > FBInfo->Height) H = FBInfo->Height - SrcY;
550 if(DstY + H > FBInfo->Height) H = FBInfo->Height - DstY;
552 //Debug("W = %i, H = %i", W, H);
561 tmp = W*bytes_per_px;
562 for( tmp = W; tmp --; ) {
563 *((Uint8*)FBInfo->Framebuffer + dst + tmp) = *((Uint8*)FBInfo->Framebuffer + src + tmp);
567 else if(W == FBInfo->Width && FBInfo->Pitch == FBInfo->Width*bytes_per_px) {
568 memmove((Uint8*)FBInfo->Framebuffer + dst, (Uint8*)FBInfo->Framebuffer + src, H*FBInfo->Pitch);
573 memcpy((Uint8*)FBInfo->Framebuffer + dst, (Uint8*)FBInfo->Framebuffer + src, W*bytes_per_px);
578 //Log("Vesa_2D_Blit: RETURN");