Added missing properties.
[AROS.git] / rom / graphics / graphics_driver.c
blob375d3294c5eca59d38d5c6bb4c5f8118ccc265dd
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Driver for using gfxhidd for gfx output
6 Lang: english
7 */
9 #include <proto/exec.h>
10 #include <proto/graphics.h>
11 #include <proto/layers.h>
12 #include <proto/arossupport.h>
13 #include <proto/utility.h>
14 #include <proto/oop.h>
16 #include <exec/memory.h>
17 #include <exec/semaphores.h>
18 #include <clib/macros.h>
20 #include <graphics/rastport.h>
21 #include <graphics/gfxbase.h>
22 #include <graphics/text.h>
23 #include <graphics/view.h>
24 #include <graphics/layers.h>
25 #include <graphics/clip.h>
26 #include <graphics/gfxmacros.h>
27 #include <graphics/regions.h>
28 #include <graphics/scale.h>
30 #include <oop/oop.h>
31 #include <utility/tagitem.h>
32 #include <aros/asmcall.h>
34 #include <intuition/intuition.h>
36 #include <hidd/compositing.h>
37 #include <hidd/graphics.h>
39 #include <cybergraphx/cybergraphics.h>
41 #include <stdio.h>
42 #include <string.h>
44 #include "graphics_intern.h"
45 #include "compositing_driver.h"
46 #include "fakegfxhidd.h"
47 #include "intregions.h"
48 #include "dispinfo.h"
49 #include "gfxfuncsupport.h"
50 #include "fontsupport.h"
52 #define SDEBUG 0
53 #define DEBUG 0
54 #include <aros/debug.h>
56 #define DEBUG_INIT(x)
57 #define DEBUG_LOADVIEW(x)
59 /* Define this if you wish to enforce using software mouse sprite
60 even for drivers that support hardware one. Useful for debugging
61 and testing
62 #define FORCE_SOFTWARE_SPRITE */
64 struct ETextFont
66 struct TextFont etf_Font;
69 /* *********************** RastPort extra data handling *********************** */
71 struct gfx_driverdata *AllocDriverData(struct RastPort *rp, BOOL alloc, struct GfxBase *GfxBase)
73 struct gfx_driverdata *dd = ObtainDriverData(rp);
75 if (alloc && !dd)
77 dd = AllocVec(sizeof(struct gfx_driverdata), MEMF_CLEAR);
78 if (dd)
80 rp->RP_Extra = dd;
81 dd->dd_RastPort = rp;
85 return dd;
88 /* *********************** Display driver handling *********************** */
90 int driver_init(struct GfxBase * GfxBase)
93 EnterFunc(bug("driver_init()\n"));
95 /* Our underlying RTG subsystem core must be already up and running */
96 if (!OpenLibrary("graphics.hidd", 0))
97 return FALSE;
99 /* Initialize the semaphores */
100 InitSemaphore(&(PrivGBase(GfxBase)->blit_sema));
102 /* Init the needed attrbases */
104 __IHidd_BitMap = OOP_ObtainAttrBase(IID_Hidd_BitMap);
105 __IHidd_GC = OOP_ObtainAttrBase(IID_Hidd_GC);
106 __IHidd_Sync = OOP_ObtainAttrBase(IID_Hidd_Sync);
107 __IHidd_PixFmt = OOP_ObtainAttrBase(IID_Hidd_PixFmt);
108 __IHidd_PlanarBM = OOP_ObtainAttrBase(IID_Hidd_PlanarBM);
109 __IHidd_Gfx = OOP_ObtainAttrBase(IID_Hidd_Gfx);
110 __IHidd_FakeGfxHidd = OOP_ObtainAttrBase(IID_Hidd_FakeGfxHidd);
112 if (__IHidd_BitMap &&
113 __IHidd_GC &&
114 __IHidd_Sync &&
115 __IHidd_PixFmt &&
116 __IHidd_PlanarBM &&
117 __IHidd_Gfx &&
118 __IHidd_FakeGfxHidd)
120 CDD(GfxBase)->gcClass = OOP_FindClass(CLID_Hidd_GC);
121 if (!CDD(GfxBase)->gcClass)
122 return FALSE;
124 /* Init display mode database */
125 InitSemaphore(&CDD(GfxBase)->displaydb_sem);
126 CDD(GfxBase)->invalid_id = INVALID_ID;
127 CDD(GfxBase)->last_id = AROS_RTG_MONITOR_ID;
129 /* Init memory driver */
130 CDD(GfxBase)->memorygfx = OOP_NewObject(NULL, CLID_Hidd_Gfx, NULL);
131 DEBUG_INIT(bug("[driver_init] Memory driver object 0x%p\n", CDD(GfxBase)->memorygfx));
133 if (CDD(GfxBase)->memorygfx)
135 struct TagItem bm_create_tags[] =
137 { aHidd_BitMap_GfxHidd , (IPTR)CDD(GfxBase)->memorygfx },
138 { aHidd_PlanarBM_BitMap, 0 },
139 { TAG_DONE , 0 }
142 CDD(GfxBase)->planarbm_cache = create_object_cache(NULL, CLID_Hidd_PlanarBM, bm_create_tags, GfxBase);
143 DEBUG_INIT(bug("[driver_init] Planar bitmap cache 0x%p\n", CDD(GfxBase)->planarbm_cache));
145 if (CDD(GfxBase)->planarbm_cache)
147 CDD(GfxBase)->gc_cache = create_object_cache(NULL, CLID_Hidd_GC, NULL, GfxBase);
148 DEBUG_INIT(bug("[driver_init] GC cache 0x%p\n", CDD(GfxBase)->planarbm_cache));
150 if (CDD(GfxBase)->gc_cache)
151 ReturnInt("driver_init", int, TRUE);
153 delete_object_cache(CDD(GfxBase)->planarbm_cache, GfxBase);
155 OOP_DisposeObject(CDD(GfxBase)->memorygfx);
159 ReturnInt("driver_init", int, FALSE);
162 /* Called after DOS is up & running */
163 static OOP_Object *create_framebuffer(struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
165 struct TagItem fbtags[] = {
166 { aHidd_BitMap_FrameBuffer, TRUE },
167 { aHidd_BitMap_ModeID, 0 },
168 { TAG_DONE, 0 }
171 HIDDT_ModeID hiddmode;
172 OOP_Object *fb = NULL;
174 /* Get the highest available resolution at the best possible depth */
175 hiddmode = get_best_resolution_and_depth(mdd, GfxBase);
176 if (vHidd_ModeID_Invalid == hiddmode) {
177 D(bug("!!! create_framebuffer(): COULD NOT GET HIDD MODEID !!!\n"));
178 } else {
179 /* Create the framebuffer object */
180 fbtags[1].ti_Data = hiddmode;
181 fb = HIDD_Gfx_NewBitMap(mdd->gfxhidd, fbtags);
184 return fb;
188 * Set up graphics display driver.
189 * Creates necessary OS structures around it.
191 * gfxhidd - newly created driver object
192 * result - master driver structure
195 struct monitor_driverdata *driver_Setup(OOP_Object *gfxhidd, struct GfxBase *GfxBase)
197 ULONG cnt = 0;
198 IPTR hwcursor = 0;
199 IPTR noframebuffer = 0;
200 UWORD compose = 0;
201 BOOL can_compose = FALSE;
202 BOOL ok = TRUE;
203 ULONG i;
204 HIDDT_ModeID *modes, *m;
205 struct monitor_driverdata *mdd;
206 struct HIDD_ModeProperties props = {0};
208 D(bug("[driver_Setup] gfxhidd=0x%p (%s)\n", gfxhidd, OOP_OCLASS(gfxhidd)->ClassNode.ln_Name));
210 modes = HIDD_Gfx_QueryModeIDs(gfxhidd, NULL);
211 if (!modes)
212 return NULL;
214 /* Count number of display modes */
215 for (m = modes; *m != vHidd_ModeID_Invalid; m ++)
216 cnt++;
218 mdd = AllocVec(sizeof(struct monitor_driverdata) + cnt * sizeof(struct DisplayInfoHandle), MEMF_PUBLIC|MEMF_CLEAR);
219 D(bug("[driver_Setup] Allocated driverdata at 0x%p\n", mdd));
220 if (!mdd)
222 HIDD_Gfx_ReleaseModeIDs(gfxhidd, modes);
223 return NULL;
226 mdd->gfxhidd_orig = gfxhidd;
229 * 1. Fill in ModeID database in the driverdata.
230 * 2. Check if at least one mode supports composition.
231 * 3. Check if the driver has at least one non-palettized mode.
232 * (2) and (3) are needed to determine if we need/can use software screen
233 * composition with this driver.
235 for (i = 0; i <= cnt; i++)
237 mdd->modes[i].id = modes[i];
238 mdd->modes[i].drv = mdd;
240 /* Watch out! The last record in the array is terminator (vHidd_ModeID_Invalid) */
241 if ((i < cnt) && (!compose))
243 HIDD_Gfx_ModeProperties(gfxhidd, modes[i], &props, sizeof(props));
244 compose |= props.CompositionFlags;
246 if (!can_compose)
248 OOP_Object *sync, *pf;
249 IPTR colmod;
251 HIDD_Gfx_GetMode(gfxhidd, modes[i], &sync, &pf);
252 OOP_GetAttr(pf, aHidd_PixFmt_ColorModel, &colmod);
254 if (colmod == vHidd_ColorModel_TrueColor)
257 * At the moment software composer supports only truecolor screens.
258 * There also definitions for DirectColor, gray, etc, but there is
259 * no such hardware supported by now.
261 can_compose = TRUE;
267 HIDD_Gfx_ReleaseModeIDs(gfxhidd, modes);
269 /* Query properties of our driver */
270 #ifndef FORCE_SOFTWARE_SPRITE
271 OOP_GetAttr(gfxhidd, aHidd_Gfx_HWSpriteTypes, &hwcursor);
272 #endif
273 OOP_GetAttr(gfxhidd, aHidd_Gfx_NoFrameBuffer, &noframebuffer);
275 if (hwcursor)
277 mdd->gfxhidd = gfxhidd;
279 else
281 D(bug("[driver_Setup] Hardware mouse cursor is not supported, using fakegfx.hidd\n"));
283 mdd->gfxhidd = init_fakegfxhidd(gfxhidd, GfxBase);
284 if (mdd->gfxhidd)
285 mdd->flags |= DF_UseFakeGfx;
286 else
287 ok = FALSE;
290 if (ok)
292 D(bug("[driver_Setup] Ok\n"));
294 if (!noframebuffer)
297 * Instantiate framebuffer if needed.
298 * Note that we perform this operation on fakegfx.hidd if it was plugged in.
299 * This enables software mouse sprite on a framebuffer.
301 mdd->framebuffer = create_framebuffer(mdd, GfxBase);
302 mdd->flags |= DF_DirectFB;
305 if (noframebuffer || mdd->framebuffer)
307 D(bug("[driver_Setup] FRAMEBUFFER OK: %p\n", mdd->framebuffer));
309 if ((!compose) && can_compose)
311 D(bug("[driver_Setup] Software screen composition required\n"));
313 mdd->flags |= DF_SoftCompose;
314 composer_Setup(mdd, GfxBase);
317 return mdd;
320 if (mdd->framebuffer)
321 OOP_DisposeObject(mdd->framebuffer);
322 } /* if (fake gfx stuff ok) */
324 if (mdd->flags & DF_UseFakeGfx)
325 OOP_DisposeObject(mdd->gfxhidd);
327 FreeVec(mdd);
329 return NULL;
333 * Completely remove a driver from the OS.
335 * mdd - Driver structure to remove.
337 * Note that removing a driver is very unsafe operation. You must be
338 * sure that no bitmaps of this driver exist at the moment. Perhaps
339 * something should be done with it.
343 void driver_Expunge(struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
345 /* Notify Intuition */
346 if (CDD(GfxBase)->DriverNotify)
347 CDD(GfxBase)->DriverNotify(mdd->userdata, FALSE, CDD(GfxBase)->notify_data);
349 /* Dispose associated stuff */
350 OOP_DisposeObject(mdd->composer);
351 OOP_DisposeObject(mdd->framebuffer);
353 if (mdd->flags & DF_UseFakeGfx)
354 OOP_DisposeObject(mdd->gfxhidd);
356 /* Dispose driver object. This will take care about syncs etc */
357 OOP_DisposeObject(mdd->gfxhidd_orig );
359 FreeVec(mdd);
362 #if 0 /* Unused? */
364 static ULONG getbitmappixel(struct BitMap *bm
365 , LONG x
366 , LONG y
367 , UBYTE depth
368 , UBYTE plane_mask)
370 UBYTE i;
371 ULONG idx;
373 ULONG mask;
374 ULONG pen = 0;
376 idx = COORD_TO_BYTEIDX(x, y, bm->BytesPerRow);
377 mask = XCOORD_TO_MASK( x );
379 for (i = depth - 1; depth; i -- , depth -- )
381 pen <<= 1; /* stegerg: moved to here, was inside if!? */
383 if ((1L << i) & plane_mask)
385 UBYTE *plane = bm->Planes[i];
387 if (plane == (PLANEPTR)-1)
389 pen |= 1;
391 else if (plane != NULL)
393 if ((plane[idx] & mask) != 0)
394 pen |= 1;
399 return pen;
402 #endif
404 /* This is called for every ViewPorts chain during MrgCop() */
405 ULONG driver_PrepareViewPorts(struct HIDD_ViewPortData *vpd, struct View *v, struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
407 /* Do not bother the driver if there's nothing to prepare. Can be changed if needed. */
408 return vpd ? HIDD_Gfx_PrepareViewPorts(mdd->gfxhidd, vpd, v) : MCOP_OK;
412 * This is called for every ViewPorts chain during LoadView()
413 * LoadView() actually has no rights for errors because error condition
414 * in it will screw up the display, likely with no chance to recover.
415 * Because of this we always return zero here.
417 ULONG driver_LoadViewPorts(struct HIDD_ViewPortData *vpd, struct View *v, struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
419 struct BitMap *bitmap;
420 OOP_Object *bm, *fb;
421 BOOL compositing = FALSE;
423 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Showing ViewPortData 0x%p, BitMap object 0x%p\n", vpd, vpd ? vpd->Bitmap : NULL));
424 mdd->display = vpd;
426 /* First try the new method */
427 if (HIDD_Gfx_ShowViewPorts(mdd->gfxhidd, vpd))
429 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] ShowViewPorts() worked\n"));
430 return 0;
434 * ShowViewPorts not supported.
435 * Perhaps we can use software screen composer. But for proper operation
436 * we need to figure out our frontmost bitmap.
438 if (vpd)
440 bitmap = vpd->vpe->ViewPort->RasInfo->BitMap;
441 bm = vpd->Bitmap;
443 else
445 bitmap = NULL;
446 bm = NULL;
448 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Old bitmap 0x%p, New bitmap 0x%p, object 0x%p\n", mdd->frontbm, bitmap, bm));
450 if (mdd->composer)
453 * Composer present. Give ViewPorts chain to it.
454 * For improved visual appearance it will call Show itself and return its result.
455 * The composer is expected to handle all possible failures internally. If some
456 * internal error happens, it must fail back to single HIDD_Gfx_Show().
458 fb = composer_LoadViewPorts(mdd->composer, vpd, &compositing, GfxBase);
459 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Composer returned 0x%p, active: %d\n", fb, compositing));
461 else
464 * First check if the bitmap is already displayed. If so, do nothing (because
465 * displaying the same bitmap twice may cause some problems)
467 if (mdd->frontbm == bitmap)
468 return 0;
470 fb = HIDD_Gfx_Show(mdd->gfxhidd, bm, fHidd_Gfx_Show_CopyBack);
471 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Show() returned 0x%p\n", fb));
474 /* Summary of possible responses (when no error happened):
475 NoFrameBuffer = FALSE: bm = NULL -> fb != NULL and bm != fb
476 NoFrameBuffer = FALSE: bm != NULL -> fb != NULL and bm != fb
477 NoFrameBuffer = TRUE : bm = NULL -> fb == NULL and bm == fb
478 NoFrameBuffer = TRUE : bm != NULL -> fb != NULL and bm == fb
480 if (fb)
482 /* The screen is not empty, 'fb' is on display now. */
483 IPTR width, height;
485 /* Uninstall the framebuffer from old frontmost BitMap (if present) */
486 UninstallFB(mdd);
489 * We need to always remember our new frontmost bitmap, even if we do not work
490 * with a framebuffer. This is needed for the check against showing the same
491 * bitmap twice.
493 mdd->frontbm = bitmap;
496 * Install the framebuffer into new bitmap. Only if software composition is inactive.
497 * If it is active, our BitMap is mirrored, not replaced.
499 if (!compositing)
500 InstallFB(mdd, GfxBase);
502 /* Tell the driver to refresh the screen */
503 OOP_GetAttr(fb, aHidd_BitMap_Width, &width);
504 OOP_GetAttr(fb, aHidd_BitMap_Height, &height);
505 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Updating display, new size: %d x %d\n", width, height));
507 HIDD_BM_UpdateRect(fb, 0, 0, width, height);
509 else
512 * Screen is empty, simply reset the current bitmap.
513 * Only framebuffer-less driver can return NULL. So we don't need
514 * to call UninstallFB() here.
516 mdd->frontbm = NULL;
519 return 0;
523 * In-Place framebuffer install/uninstall routines.
525 * The idea behind: we gave some bitmap to display, the driver *HAS COPIED* it
526 * into the framebuffer. After this we must use a framebuffer bitmap instead of
527 * original one for all rendering operations.
528 * When another bitmap is displayed, the framebuffer contents will be put back
529 * into the original bitmap, and we will swap it back.
531 * This swap actually happens only of DF_DirectFB is set for the driver.
533 * FIXME: THIS IS NOT THREADSAFE
534 * To make this threadsafe we have to lock all gfx access in all the rendering
535 * calls.
537 void InstallFB(struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
539 if ((mdd->flags & DF_DirectFB) && mdd->frontbm)
541 struct BitMap *bitmap = mdd->frontbm;
542 OOP_Object *pf;
544 /* Backup original data */
545 mdd->bm_bak = HIDD_BM_OBJ(bitmap);
546 mdd->colmod_bak = HIDD_BM_COLMOD(bitmap);
547 mdd->colmap_bak = HIDD_BM_COLMAP(bitmap);
549 /* Insert the framebuffer in its place */
550 OOP_GetAttr(mdd->framebuffer, aHidd_BitMap_PixFmt, (IPTR *)&pf);
552 HIDD_BM_OBJ(bitmap) = mdd->framebuffer;
553 HIDD_BM_COLMOD(bitmap) = OOP_GET(pf, aHidd_PixFmt_ColorModel);
554 HIDD_BM_COLMAP(bitmap) = (OOP_Object *)OOP_GET(mdd->framebuffer, aHidd_BitMap_ColorMap);
556 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Installed framebuffer: BitMap 0x%p, object 0x%p\n", mdd->frontbm, mdd->bm_bak));
560 void UninstallFB(struct monitor_driverdata *mdd)
562 if (mdd->bm_bak)
564 /* Put back the old values into the old bitmap */
565 struct BitMap *oldbm = mdd->frontbm;
567 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Uninstalling framebuffer: BitMap 0x%p, object 0x%p\n", mdd->frontbm, mdd->bm_bak));
569 HIDD_BM_OBJ(oldbm) = mdd->bm_bak;
570 HIDD_BM_COLMOD(oldbm) = mdd->colmod_bak;
571 HIDD_BM_COLMAP(oldbm) = mdd->colmap_bak;
573 /* No framebuffer installed */
574 mdd->bm_bak = NULL;
578 /* Find the first visible ViewPortData for the specified monitor in the View */
579 struct HIDD_ViewPortData *driver_FindViewPorts(struct View *view, struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
581 struct ViewPort *vp;
583 for (vp = view->ViewPort; vp; vp = vp->Next)
585 if (!(vp->Modes & VP_HIDE))
587 struct ViewPortExtra *vpe = (struct ViewPortExtra *)GfxLookUp(vp);
589 if (VPE_DRIVER(vpe) == mdd)
590 return VPE_DATA(vpe);
593 return NULL;
597 * Iterate through HIDD_ViewPortData chains in the view and call
598 * the specified function for every chain and every driver.
599 * The function will be called at least once for every driver.
600 * If there is no ViewPorts for the driver, the function will be
601 * called with ViewPortData = NULL.
603 ULONG DoViewFunction(struct View *view, VIEW_FUNC fn, struct GfxBase *GfxBase)
605 struct monitor_driverdata *mdd;
606 ULONG rc = 0;
608 ObtainSemaphoreShared(&CDD(GfxBase)->displaydb_sem);
610 for (mdd = CDD(GfxBase)->monitors; mdd; mdd = mdd->next)
612 struct HIDD_ViewPortData *vpd = NULL;
615 * Find the first visible ViewPort for this display. It
616 * will be a start of bitmaps chain to process.
618 if (view)
619 vpd = driver_FindViewPorts(view, mdd, GfxBase);
621 rc = fn(vpd, view, mdd, GfxBase);
623 /* Interrupt immediately if the callback returned error */
624 if (rc)
625 break;
628 ReleaseSemaphore(&CDD(GfxBase)->displaydb_sem);
630 return rc;