add option for classic magicmenu "mixed" menu style (pull donw on screen title, pop...
[AROS.git] / rom / graphics / graphics_driver.c
blobeccb581c6b066a8a92bf38ca1b4247c3d376f465
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Driver for using gfxhidd for gfx output
6 Lang: english
7 */
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>
31 #include <oop/oop.h>
32 #include <utility/tagitem.h>
33 #include <aros/asmcall.h>
35 #include <intuition/intuition.h>
37 #include <hidd/compositing.h>
38 #include <hidd/graphics.h>
40 #include <cybergraphx/cybergraphics.h>
42 #include <stdio.h>
43 #include <string.h>
45 #include "graphics_intern.h"
46 #include "compositing_driver.h"
47 #include "fakegfxhidd.h"
48 #include "intregions.h"
49 #include "dispinfo.h"
50 #include "gfxfuncsupport.h"
51 #include "fontsupport.h"
53 #define DEBUG_INIT(x)
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
58 and testing
59 #define FORCE_SOFTWARE_SPRITE */
61 struct ETextFont
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);
72 if (alloc && !dd)
74 dd = AllocVec(sizeof(struct gfx_driverdata), MEMF_CLEAR);
75 if (dd)
77 rp->RP_Extra = dd;
78 dd->dd_RastPort = rp;
82 return dd;
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))
94 return FALSE;
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 &&
110 __IHidd_GC &&
111 __IHidd_Sync &&
112 __IHidd_PixFmt &&
113 __IHidd_PlanarBM &&
114 __IHidd_Gfx &&
115 __IHidd_FakeGfxHidd)
117 CDD(GfxBase)->gcClass = OOP_FindClass(CLID_Hidd_GC);
118 if (!CDD(GfxBase)->gcClass)
119 return FALSE;
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 },
136 { TAG_DONE , 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 },
165 { TAG_DONE, 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"));
175 } else {
176 /* Create the framebuffer object */
177 fbtags[1].ti_Data = hiddmode;
178 fb = HIDD_Gfx_NewBitMap(mdd->gfxhidd, fbtags);
181 return fb;
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)
194 ULONG cnt = 0;
195 IPTR hwcursor = 0;
196 IPTR fbtype = vHidd_FrameBuffer_Direct; /* We default to direct for historical reasons */
197 UWORD compose = 0;
198 BOOL can_compose = FALSE;
199 BOOL ok = TRUE;
200 ULONG i;
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);
208 if (!modes)
209 return NULL;
211 /* Count number of display modes */
212 for (m = modes; *m != vHidd_ModeID_Invalid; m ++)
213 cnt++;
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));
217 if (!mdd)
219 HIDD_Gfx_ReleaseModeIDs(gfxhidd, modes);
220 return NULL;
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;
243 if (!can_compose)
245 OOP_Object *sync, *pf;
246 IPTR colmod;
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 composer supports only truecolor screens.
255 * There also definitions for DirectColor, gray, etc, but there is
256 * no such hardware supported by now.
258 can_compose = TRUE;
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);
269 #endif
270 OOP_GetAttr(gfxhidd, aHidd_Gfx_FrameBufferType, &fbtype);
272 if (hwcursor)
274 mdd->gfxhidd = gfxhidd;
276 else
278 D(bug("[driver_Setup] Hardware mouse cursor is not supported, using fakegfx.hidd\n"));
280 mdd->gfxhidd = init_fakegfxhidd(gfxhidd, GfxBase);
281 if (mdd->gfxhidd)
282 mdd->flags |= DF_UseFakeGfx;
283 else
284 ok = FALSE;
287 if (ok)
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.
296 switch (fbtype)
298 case vHidd_FrameBuffer_Direct:
299 mdd->flags |= DF_DirectFB;
301 case vHidd_FrameBuffer_Mirrored:
302 mdd->framebuffer = create_framebuffer(mdd, GfxBase);
303 break;
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 composer_Setup(mdd, GfxBase);
318 return mdd;
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);
328 FreeVec(mdd);
330 return NULL;
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->composer);
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 );
360 FreeVec(mdd);
363 #if 0 /* Unused? */
365 static ULONG getbitmappixel(struct BitMap *bm
366 , LONG x
367 , LONG y
368 , UBYTE depth
369 , UBYTE plane_mask)
371 UBYTE i;
372 ULONG idx;
374 ULONG mask;
375 ULONG pen = 0;
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)
390 pen |= 1;
392 else if (plane != NULL)
394 if ((plane[idx] & mask) != 0)
395 pen |= 1;
400 return pen;
403 #endif
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;
421 OOP_Object *bm, *fb;
422 BOOL compositing = FALSE;
424 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Showing ViewPortData 0x%p, BitMap object 0x%p\n", vpd, vpd ? vpd->Bitmap : NULL));
425 mdd->display = vpd;
427 /* First try the new method */
428 if (HIDD_Gfx_ShowViewPorts(mdd->gfxhidd, vpd))
430 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] ShowViewPorts() worked\n"));
431 return 0;
435 * ShowViewPorts not supported.
436 * Perhaps we can use software screen composer. But for proper operation
437 * we need to figure out our frontmost bitmap.
439 if (vpd)
441 bitmap = vpd->vpe->ViewPort->RasInfo->BitMap;
442 bm = vpd->Bitmap;
444 else
446 bitmap = NULL;
447 bm = NULL;
449 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Old bitmap 0x%p, New bitmap 0x%p, object 0x%p\n", mdd->frontbm, bitmap, bm));
451 if (mdd->composer)
454 * Composer present. Give ViewPorts chain to it.
455 * For improved visual appearance it will call Show itself and return its result.
456 * The composer 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 = composer_LoadViewPorts(mdd->composer, vpd, &compositing, GfxBase);
460 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Composer returned 0x%p, active: %d\n", fb, compositing));
462 else
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)
469 return 0;
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
481 if (fb)
483 /* The screen is not empty, 'fb' is on display now. */
484 IPTR width, height;
486 /* Uninstall the framebuffer from old frontmost BitMap (if present) */
487 UninstallFB(mdd);
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
492 * bitmap twice.
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.
500 if (!compositing)
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);
510 else
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.
517 mdd->frontbm = NULL;
520 return 0;
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
536 * calls.
538 void InstallFB(struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
540 if ((mdd->flags & DF_DirectFB) && mdd->frontbm)
542 struct BitMap *bitmap = mdd->frontbm;
543 OOP_Object *pf;
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)
563 if (mdd->bm_bak)
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 */
575 mdd->bm_bak = NULL;
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)
582 struct ViewPort *vp;
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);
594 return NULL;
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;
607 ULONG rc = 0;
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.
619 if (view)
620 vpd = driver_FindViewPorts(view, mdd, GfxBase);
622 rc = fn(vpd, view, mdd, GfxBase);
624 /* Interrupt immediately if the callback returned error */
625 if (rc)
626 break;
629 ReleaseSemaphore(&CDD(GfxBase)->displaydb_sem);
631 return rc;