2 Copyright © 1995-2015, The AROS Development Team. All rights reserved.
5 Desc: Driver for using gfxhidd for gfx output
9 #include <aros/debug.h>
11 #include <proto/exec.h>
12 #include <proto/graphics.h>
13 #include <proto/arossupport.h>
14 #include <proto/utility.h>
15 #include <proto/oop.h>
17 #include <exec/memory.h>
18 #include <exec/semaphores.h>
19 #include <clib/macros.h>
21 #include <graphics/rastport.h>
22 #include <graphics/gfxbase.h>
23 #include <graphics/text.h>
24 #include <graphics/view.h>
25 #include <graphics/layers.h>
26 #include <graphics/clip.h>
27 #include <graphics/gfxmacros.h>
28 #include <graphics/regions.h>
29 #include <graphics/scale.h>
32 #include <utility/tagitem.h>
33 #include <aros/asmcall.h>
35 #include <intuition/intuition.h>
37 #include <hidd/compositor.h>
38 #include <hidd/graphics.h>
40 #include <cybergraphx/cybergraphics.h>
45 #include "graphics_intern.h"
46 #include "compositor_driver.h"
47 #include "fakegfxhidd.h"
48 #include "intregions.h"
50 #include "gfxfuncsupport.h"
51 #include "fontsupport.h"
54 #define DEBUG_LOADVIEW(x)
56 /* Define this if you wish to enforce using software mouse sprite
57 even for drivers that support hardware one. Useful for debugging
59 #define FORCE_SOFTWARE_SPRITE */
63 struct TextFont etf_Font
;
66 /* *********************** RastPort extra data handling *********************** */
68 struct gfx_driverdata
*AllocDriverData(struct RastPort
*rp
, BOOL alloc
, struct GfxBase
*GfxBase
)
70 struct gfx_driverdata
*dd
= ObtainDriverData(rp
);
74 dd
= AllocVec(sizeof(struct gfx_driverdata
), MEMF_CLEAR
);
85 /* *********************** Display driver handling *********************** */
87 int driver_init(struct GfxBase
* GfxBase
)
90 EnterFunc(bug("driver_init()\n"));
92 /* Our underlying RTG subsystem core must be already up and running */
93 if (!OpenLibrary("graphics.hidd", 0))
96 /* Initialize the semaphores */
97 InitSemaphore(&(PrivGBase(GfxBase
)->blit_sema
));
99 /* Init the needed attrbases */
101 __IHidd_BitMap
= OOP_ObtainAttrBase(IID_Hidd_BitMap
);
102 __IHidd_GC
= OOP_ObtainAttrBase(IID_Hidd_GC
);
103 __IHidd_Sync
= OOP_ObtainAttrBase(IID_Hidd_Sync
);
104 __IHidd_PixFmt
= OOP_ObtainAttrBase(IID_Hidd_PixFmt
);
105 __IHidd_PlanarBM
= OOP_ObtainAttrBase(IID_Hidd_PlanarBM
);
106 __IHidd_Gfx
= OOP_ObtainAttrBase(IID_Hidd_Gfx
);
107 __IHidd_FakeGfxHidd
= OOP_ObtainAttrBase(IID_Hidd_FakeGfxHidd
);
109 if (__IHidd_BitMap
&&
117 CDD(GfxBase
)->gcClass
= OOP_FindClass(CLID_Hidd_GC
);
118 if (!CDD(GfxBase
)->gcClass
)
121 /* Init display mode database */
122 InitSemaphore(&CDD(GfxBase
)->displaydb_sem
);
123 CDD(GfxBase
)->invalid_id
= INVALID_ID
;
124 CDD(GfxBase
)->last_id
= AROS_RTG_MONITOR_ID
;
126 /* Init memory driver */
127 CDD(GfxBase
)->memorygfx
= OOP_NewObject(NULL
, CLID_Hidd_Gfx
, NULL
);
128 DEBUG_INIT(bug("[driver_init] Memory driver object 0x%p\n", CDD(GfxBase
)->memorygfx
));
130 if (CDD(GfxBase
)->memorygfx
)
132 struct TagItem bm_create_tags
[] =
134 { aHidd_BitMap_GfxHidd
, (IPTR
)CDD(GfxBase
)->memorygfx
},
135 { aHidd_PlanarBM_BitMap
, 0 },
139 CDD(GfxBase
)->planarbm_cache
= create_object_cache(NULL
, CLID_Hidd_PlanarBM
, bm_create_tags
, GfxBase
);
140 DEBUG_INIT(bug("[driver_init] Planar bitmap cache 0x%p\n", CDD(GfxBase
)->planarbm_cache
));
142 if (CDD(GfxBase
)->planarbm_cache
)
144 CDD(GfxBase
)->gc_cache
= create_object_cache(NULL
, CLID_Hidd_GC
, NULL
, GfxBase
);
145 DEBUG_INIT(bug("[driver_init] GC cache 0x%p\n", CDD(GfxBase
)->planarbm_cache
));
147 if (CDD(GfxBase
)->gc_cache
)
148 ReturnInt("driver_init", int, TRUE
);
150 delete_object_cache(CDD(GfxBase
)->planarbm_cache
, GfxBase
);
152 OOP_DisposeObject(CDD(GfxBase
)->memorygfx
);
156 ReturnInt("driver_init", int, FALSE
);
159 /* Called after DOS is up & running */
160 static OOP_Object
*create_framebuffer(struct monitor_driverdata
*mdd
, struct GfxBase
*GfxBase
)
162 struct TagItem fbtags
[] = {
163 { aHidd_BitMap_FrameBuffer
, TRUE
},
164 { aHidd_BitMap_ModeID
, 0 },
168 HIDDT_ModeID hiddmode
;
169 OOP_Object
*fb
= NULL
;
171 /* Get the highest available resolution at the best possible depth */
172 hiddmode
= get_best_resolution_and_depth(mdd
, GfxBase
);
173 if (vHidd_ModeID_Invalid
== hiddmode
) {
174 D(bug("!!! create_framebuffer(): COULD NOT GET HIDD MODEID !!!\n"));
176 /* Create the framebuffer object */
177 fbtags
[1].ti_Data
= hiddmode
;
178 fb
= HIDD_Gfx_CreateObject(mdd
->gfxhidd
, PrivGBase(GfxBase
)->basebm
, fbtags
);
185 * Set up graphics display driver.
186 * Creates necessary OS structures around it.
188 * gfxhidd - newly created driver object
189 * result - master driver structure
192 struct monitor_driverdata
*driver_Setup(OOP_Object
*gfxhidd
, struct GfxBase
*GfxBase
)
196 IPTR fbtype
= vHidd_FrameBuffer_Direct
; /* We default to direct for historical reasons */
198 BOOL can_compose
= FALSE
;
201 HIDDT_ModeID
*modes
, *m
;
202 struct monitor_driverdata
*mdd
;
203 struct HIDD_ModeProperties props
= {0};
205 D(bug("[driver_Setup] gfxhidd=0x%p (%s)\n", gfxhidd
, OOP_OCLASS(gfxhidd
)->ClassNode
.ln_Name
));
207 modes
= HIDD_Gfx_QueryModeIDs(gfxhidd
, NULL
);
211 /* Count number of display modes */
212 for (m
= modes
; *m
!= vHidd_ModeID_Invalid
; m
++)
215 mdd
= AllocVec(sizeof(struct monitor_driverdata
) + cnt
* sizeof(struct DisplayInfoHandle
), MEMF_PUBLIC
|MEMF_CLEAR
);
216 D(bug("[driver_Setup] Allocated driverdata at 0x%p\n", mdd
));
219 HIDD_Gfx_ReleaseModeIDs(gfxhidd
, modes
);
223 mdd
->gfxhidd_orig
= gfxhidd
;
226 * 1. Fill in ModeID database in the driverdata.
227 * 2. Check if at least one mode supports composition.
228 * 3. Check if the driver has at least one non-palettized mode.
229 * (2) and (3) are needed to determine if we need/can use software screen
230 * composition with this driver.
232 for (i
= 0; i
<= cnt
; i
++)
234 mdd
->modes
[i
].id
= modes
[i
];
235 mdd
->modes
[i
].drv
= mdd
;
237 /* Watch out! The last record in the array is terminator (vHidd_ModeID_Invalid) */
238 if ((i
< cnt
) && (!compose
))
240 HIDD_Gfx_ModeProperties(gfxhidd
, modes
[i
], &props
, sizeof(props
));
241 compose
|= props
.CompositionFlags
;
245 OOP_Object
*sync
, *pf
;
248 HIDD_Gfx_GetMode(gfxhidd
, modes
[i
], &sync
, &pf
);
249 OOP_GetAttr(pf
, aHidd_PixFmt_ColorModel
, &colmod
);
251 if (colmod
== vHidd_ColorModel_TrueColor
)
254 * At the moment software compositor supports only truecolor screens.
255 * There also definitions for DirectColor, gray, etc, but there is
256 * no such hardware supported by now.
264 HIDD_Gfx_ReleaseModeIDs(gfxhidd
, modes
);
266 /* Query properties of our driver */
267 #ifndef FORCE_SOFTWARE_SPRITE
268 OOP_GetAttr(gfxhidd
, aHidd_Gfx_HWSpriteTypes
, &hwcursor
);
270 OOP_GetAttr(gfxhidd
, aHidd_Gfx_FrameBufferType
, &fbtype
);
274 mdd
->gfxhidd
= gfxhidd
;
278 D(bug("[driver_Setup] Hardware mouse cursor is not supported, using fakegfx.hidd\n"));
280 mdd
->gfxhidd
= init_fakegfxhidd(gfxhidd
, GfxBase
);
282 mdd
->flags
|= DF_UseFakeGfx
;
289 D(bug("[driver_Setup] Ok, framebuffer type %ld\n", fbtype
));
292 * Instantiate framebuffer if needed.
293 * Note that we perform this operation on fakegfx.hidd if it was plugged in.
294 * This enables software mouse sprite on a framebuffer.
298 case vHidd_FrameBuffer_Direct
:
299 mdd
->flags
|= DF_DirectFB
;
301 case vHidd_FrameBuffer_Mirrored
:
302 mdd
->framebuffer
= create_framebuffer(mdd
, GfxBase
);
306 if ((fbtype
== vHidd_FrameBuffer_None
) || mdd
->framebuffer
)
308 D(bug("[driver_Setup] FRAMEBUFFER OK: %p\n", mdd
->framebuffer
));
310 if ((!compose
) && can_compose
)
312 D(bug("[driver_Setup] Software screen composition required\n"));
314 mdd
->flags
|= DF_SoftCompose
;
315 compositor_Setup(mdd
, GfxBase
);
321 if (mdd
->framebuffer
)
322 OOP_DisposeObject(mdd
->framebuffer
);
323 } /* if (fake gfx stuff ok) */
325 if (mdd
->flags
& DF_UseFakeGfx
)
326 OOP_DisposeObject(mdd
->gfxhidd
);
334 * Completely remove a driver from the OS.
336 * mdd - Driver structure to remove.
338 * Note that removing a driver is very unsafe operation. You must be
339 * sure that no bitmaps of this driver exist at the moment. Perhaps
340 * something should be done with it.
344 void driver_Expunge(struct monitor_driverdata
*mdd
, struct GfxBase
*GfxBase
)
346 /* Notify Intuition */
347 if (CDD(GfxBase
)->DriverNotify
)
348 CDD(GfxBase
)->DriverNotify(mdd
->userdata
, FALSE
, CDD(GfxBase
)->notify_data
);
350 /* Dispose associated stuff */
351 OOP_DisposeObject(mdd
->compositor
);
352 OOP_DisposeObject(mdd
->framebuffer
);
354 if (mdd
->flags
& DF_UseFakeGfx
)
355 OOP_DisposeObject(mdd
->gfxhidd
);
357 /* Dispose driver object. This will take care about syncs etc */
358 OOP_DisposeObject(mdd
->gfxhidd_orig
);
365 static ULONG
getbitmappixel(struct BitMap
*bm
377 idx
= COORD_TO_BYTEIDX(x
, y
, bm
->BytesPerRow
);
378 mask
= XCOORD_TO_MASK( x
);
380 for (i
= depth
- 1; depth
; i
-- , depth
-- )
382 pen
<<= 1; /* stegerg: moved to here, was inside if!? */
384 if ((1L << i
) & plane_mask
)
386 UBYTE
*plane
= bm
->Planes
[i
];
388 if (plane
== (PLANEPTR
)-1)
392 else if (plane
!= NULL
)
394 if ((plane
[idx
] & mask
) != 0)
405 /* This is called for every ViewPorts chain during MrgCop() */
406 ULONG
driver_PrepareViewPorts(struct HIDD_ViewPortData
*vpd
, struct View
*v
, struct monitor_driverdata
*mdd
, struct GfxBase
*GfxBase
)
408 /* Do not bother the driver if there's nothing to prepare. Can be changed if needed. */
409 return vpd
? HIDD_Gfx_PrepareViewPorts(mdd
->gfxhidd
, vpd
, v
) : MCOP_OK
;
413 * This is called for every ViewPorts chain during LoadView()
414 * LoadView() actually has no rights for errors because error condition
415 * in it will screw up the display, likely with no chance to recover.
416 * Because of this we always return zero here.
418 ULONG
driver_LoadViewPorts(struct HIDD_ViewPortData
*vpd
, struct View
*v
, struct monitor_driverdata
*mdd
, struct GfxBase
*GfxBase
)
420 struct BitMap
*bitmap
;
422 BOOL compositing
= FALSE
;
424 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Showing ViewPortData 0x%p, BitMap object 0x%p\n", vpd
, vpd
? vpd
->Bitmap
: NULL
));
427 /* First try the new method */
428 if (HIDD_Gfx_ShowViewPorts(mdd
->gfxhidd
, vpd
))
430 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] ShowViewPorts() worked\n"));
435 * ShowViewPorts not supported.
436 * Perhaps we can use software screen compositor. But for proper operation
437 * we need to figure out our frontmost bitmap.
441 bitmap
= vpd
->vpe
->ViewPort
->RasInfo
->BitMap
;
449 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Old bitmap 0x%p, New bitmap 0x%p, object 0x%p\n", mdd
->frontbm
, bitmap
, bm
));
454 * Compositor present. Give ViewPorts chain to it.
455 * For improved visual appearance it will call Show itself and return its result.
456 * The compositor is expected to handle all possible failures internally. If some
457 * internal error happens, it must fail back to single HIDD_Gfx_Show().
459 fb
= compositor_LoadViewPorts(mdd
->compositor
, vpd
, &compositing
, GfxBase
);
460 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Compositor returned 0x%p, active: %d\n", fb
, compositing
));
465 * First check if the bitmap is already displayed. If so, do nothing (because
466 * displaying the same bitmap twice may cause some problems)
468 if (mdd
->frontbm
== bitmap
)
471 fb
= HIDD_Gfx_Show(mdd
->gfxhidd
, bm
, fHidd_Gfx_Show_CopyBack
);
472 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Show() returned 0x%p\n", fb
));
475 /* Summary of possible responses (when no error happened):
476 NoFrameBuffer = FALSE: bm = NULL -> fb != NULL and bm != fb
477 NoFrameBuffer = FALSE: bm != NULL -> fb != NULL and bm != fb
478 NoFrameBuffer = TRUE : bm = NULL -> fb == NULL and bm == fb
479 NoFrameBuffer = TRUE : bm != NULL -> fb != NULL and bm == fb
483 /* The screen is not empty, 'fb' is on display now. */
486 /* Uninstall the framebuffer from old frontmost BitMap (if present) */
487 UninstallFB(mdd
, GfxBase
);
490 * We need to always remember our new frontmost bitmap, even if we do not work
491 * with a framebuffer. This is needed for the check against showing the same
494 mdd
->frontbm
= bitmap
;
497 * Install the framebuffer into new bitmap. Only if software composition is inactive.
498 * If it is active, our BitMap is mirrored, not replaced.
501 InstallFB(mdd
, GfxBase
);
503 /* Tell the driver to refresh the screen */
504 OOP_GetAttr(fb
, aHidd_BitMap_Width
, &width
);
505 OOP_GetAttr(fb
, aHidd_BitMap_Height
, &height
);
506 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Updating display, new size: %d x %d\n", width
, height
));
508 HIDD_BM_UpdateRect(fb
, 0, 0, width
, height
);
513 * Screen is empty, simply reset the current bitmap.
514 * Only framebuffer-less driver can return NULL. So we don't need
515 * to call UninstallFB() here.
524 * In-Place framebuffer install/uninstall routines.
526 * The idea behind: we gave some bitmap to display, the driver *HAS COPIED* it
527 * into the framebuffer. After this we must use a framebuffer bitmap instead of
528 * original one for all rendering operations.
529 * When another bitmap is displayed, the framebuffer contents will be put back
530 * into the original bitmap, and we will swap it back.
532 * This swap actually happens only of DF_DirectFB is set for the driver.
534 * FIXME: THIS IS NOT THREADSAFE
535 * To make this threadsafe we have to lock all gfx access in all the rendering
538 void InstallFB(struct monitor_driverdata
*mdd
, struct GfxBase
*GfxBase
)
540 if ((mdd
->flags
& DF_DirectFB
) && mdd
->frontbm
)
542 struct BitMap
*bitmap
= mdd
->frontbm
;
545 /* Backup original data */
546 mdd
->bm_bak
= HIDD_BM_OBJ(bitmap
);
547 mdd
->colmod_bak
= HIDD_BM_COLMOD(bitmap
);
548 mdd
->colmap_bak
= HIDD_BM_COLMAP(bitmap
);
550 /* Insert the framebuffer in its place */
551 OOP_GetAttr(mdd
->framebuffer
, aHidd_BitMap_PixFmt
, (IPTR
*)&pf
);
553 HIDD_BM_OBJ(bitmap
) = mdd
->framebuffer
;
554 HIDD_BM_COLMOD(bitmap
) = OOP_GET(pf
, aHidd_PixFmt_ColorModel
);
555 HIDD_BM_COLMAP(bitmap
) = (OOP_Object
*)OOP_GET(mdd
->framebuffer
, aHidd_BitMap_ColorMap
);
557 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Installed framebuffer: BitMap 0x%p, object 0x%p\n", mdd
->frontbm
, mdd
->bm_bak
));
561 void UninstallFB(struct monitor_driverdata
*mdd
, struct GfxBase
*GfxBase
)
565 /* Put back the old values into the old bitmap */
566 struct BitMap
*oldbm
= mdd
->frontbm
;
568 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Uninstalling framebuffer: BitMap 0x%p, object 0x%p\n", mdd
->frontbm
, mdd
->bm_bak
));
570 HIDD_BM_OBJ(oldbm
) = mdd
->bm_bak
;
571 HIDD_BM_COLMOD(oldbm
) = mdd
->colmod_bak
;
572 HIDD_BM_COLMAP(oldbm
) = mdd
->colmap_bak
;
574 /* No framebuffer installed */
579 /* Find the first visible ViewPortData for the specified monitor in the View */
580 struct HIDD_ViewPortData
*driver_FindViewPorts(struct View
*view
, struct monitor_driverdata
*mdd
, struct GfxBase
*GfxBase
)
584 for (vp
= view
->ViewPort
; vp
; vp
= vp
->Next
)
586 if (!(vp
->Modes
& VP_HIDE
))
588 struct ViewPortExtra
*vpe
= (struct ViewPortExtra
*)GfxLookUp(vp
);
590 if (VPE_DRIVER(vpe
) == mdd
)
591 return VPE_DATA(vpe
);
598 * Iterate through HIDD_ViewPortData chains in the view and call
599 * the specified function for every chain and every driver.
600 * The function will be called at least once for every driver.
601 * If there is no ViewPorts for the driver, the function will be
602 * called with ViewPortData = NULL.
604 ULONG
DoViewFunction(struct View
*view
, VIEW_FUNC fn
, struct GfxBase
*GfxBase
)
606 struct monitor_driverdata
*mdd
;
609 ObtainSemaphoreShared(&CDD(GfxBase
)->displaydb_sem
);
611 for (mdd
= CDD(GfxBase
)->monitors
; mdd
; mdd
= mdd
->next
)
613 struct HIDD_ViewPortData
*vpd
= NULL
;
616 * Find the first visible ViewPort for this display. It
617 * will be a start of bitmaps chain to process.
620 vpd
= driver_FindViewPorts(view
, mdd
, GfxBase
);
622 rc
= fn(vpd
, view
, mdd
, GfxBase
);
624 /* Interrupt immediately if the callback returned error */
629 ReleaseSemaphore(&CDD(GfxBase
)->displaydb_sem
);