gcc-4.6.2: Update with patch for gengtype.c
[AROS.git] / rom / graphics / graphics_driver.c
blobc31cdcd0a80d2b0c21e7f1e98bbbca02ad88c6fb
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/arossupport.h>
12 #include <proto/utility.h>
13 #include <proto/oop.h>
15 #include <exec/memory.h>
16 #include <exec/semaphores.h>
17 #include <clib/macros.h>
19 #include <graphics/rastport.h>
20 #include <graphics/gfxbase.h>
21 #include <graphics/text.h>
22 #include <graphics/view.h>
23 #include <graphics/layers.h>
24 #include <graphics/clip.h>
25 #include <graphics/gfxmacros.h>
26 #include <graphics/regions.h>
27 #include <graphics/scale.h>
29 #include <oop/oop.h>
30 #include <utility/tagitem.h>
31 #include <aros/asmcall.h>
33 #include <intuition/intuition.h>
35 #include <hidd/compositing.h>
36 #include <hidd/graphics.h>
38 #include <cybergraphx/cybergraphics.h>
40 #include <stdio.h>
41 #include <string.h>
43 #include "graphics_intern.h"
44 #include "compositing_driver.h"
45 #include "fakegfxhidd.h"
46 #include "intregions.h"
47 #include "dispinfo.h"
48 #include "gfxfuncsupport.h"
49 #include "fontsupport.h"
51 #define SDEBUG 0
52 #define DEBUG 0
53 #include <aros/debug.h>
55 #define DEBUG_INIT(x)
56 #define DEBUG_LOADVIEW(x)
58 /* Define this if you wish to enforce using software mouse sprite
59 even for drivers that support hardware one. Useful for debugging
60 and testing
61 #define FORCE_SOFTWARE_SPRITE */
63 struct ETextFont
65 struct TextFont etf_Font;
68 /* *********************** RastPort extra data handling *********************** */
70 struct gfx_driverdata *AllocDriverData(struct RastPort *rp, BOOL alloc, struct GfxBase *GfxBase)
72 struct gfx_driverdata *dd = ObtainDriverData(rp);
74 if (alloc && !dd)
76 dd = AllocVec(sizeof(struct gfx_driverdata), MEMF_CLEAR);
77 if (dd)
79 rp->RP_Extra = dd;
80 dd->dd_RastPort = rp;
84 return dd;
87 /* *********************** Display driver handling *********************** */
89 int driver_init(struct GfxBase * GfxBase)
92 EnterFunc(bug("driver_init()\n"));
94 /* Our underlying RTG subsystem core must be already up and running */
95 if (!OpenLibrary("graphics.hidd", 0))
96 return FALSE;
98 /* Initialize the semaphores */
99 InitSemaphore(&(PrivGBase(GfxBase)->blit_sema));
101 /* Init the needed attrbases */
103 __IHidd_BitMap = OOP_ObtainAttrBase(IID_Hidd_BitMap);
104 __IHidd_GC = OOP_ObtainAttrBase(IID_Hidd_GC);
105 __IHidd_Sync = OOP_ObtainAttrBase(IID_Hidd_Sync);
106 __IHidd_PixFmt = OOP_ObtainAttrBase(IID_Hidd_PixFmt);
107 __IHidd_PlanarBM = OOP_ObtainAttrBase(IID_Hidd_PlanarBM);
108 __IHidd_Gfx = OOP_ObtainAttrBase(IID_Hidd_Gfx);
109 __IHidd_FakeGfxHidd = OOP_ObtainAttrBase(IID_Hidd_FakeGfxHidd);
111 if (__IHidd_BitMap &&
112 __IHidd_GC &&
113 __IHidd_Sync &&
114 __IHidd_PixFmt &&
115 __IHidd_PlanarBM &&
116 __IHidd_Gfx &&
117 __IHidd_FakeGfxHidd)
119 CDD(GfxBase)->gcClass = OOP_FindClass(CLID_Hidd_GC);
120 if (!CDD(GfxBase)->gcClass)
121 return FALSE;
123 /* Init display mode database */
124 InitSemaphore(&CDD(GfxBase)->displaydb_sem);
125 CDD(GfxBase)->invalid_id = INVALID_ID;
126 CDD(GfxBase)->last_id = AROS_RTG_MONITOR_ID;
128 /* Init memory driver */
129 CDD(GfxBase)->memorygfx = OOP_NewObject(NULL, CLID_Hidd_Gfx, NULL);
130 DEBUG_INIT(bug("[driver_init] Memory driver object 0x%p\n", CDD(GfxBase)->memorygfx));
132 if (CDD(GfxBase)->memorygfx)
134 struct TagItem bm_create_tags[] =
136 { aHidd_BitMap_GfxHidd , (IPTR)CDD(GfxBase)->memorygfx },
137 { aHidd_PlanarBM_BitMap, 0 },
138 { TAG_DONE , 0 }
141 CDD(GfxBase)->planarbm_cache = create_object_cache(NULL, CLID_Hidd_PlanarBM, bm_create_tags, GfxBase);
142 DEBUG_INIT(bug("[driver_init] Planar bitmap cache 0x%p\n", CDD(GfxBase)->planarbm_cache));
144 if (CDD(GfxBase)->planarbm_cache)
146 CDD(GfxBase)->gc_cache = create_object_cache(NULL, CLID_Hidd_GC, NULL, GfxBase);
147 DEBUG_INIT(bug("[driver_init] GC cache 0x%p\n", CDD(GfxBase)->planarbm_cache));
149 if (CDD(GfxBase)->gc_cache)
150 ReturnInt("driver_init", int, TRUE);
152 delete_object_cache(CDD(GfxBase)->planarbm_cache, GfxBase);
154 OOP_DisposeObject(CDD(GfxBase)->memorygfx);
158 ReturnInt("driver_init", int, FALSE);
161 /* Called after DOS is up & running */
162 static OOP_Object *create_framebuffer(struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
164 struct TagItem fbtags[] = {
165 { aHidd_BitMap_FrameBuffer, TRUE },
166 { aHidd_BitMap_ModeID, 0 },
167 { TAG_DONE, 0 }
170 HIDDT_ModeID hiddmode;
171 OOP_Object *fb = NULL;
173 /* Get the highest available resolution at the best possible depth */
174 hiddmode = get_best_resolution_and_depth(mdd, GfxBase);
175 if (vHidd_ModeID_Invalid == hiddmode) {
176 D(bug("!!! create_framebuffer(): COULD NOT GET HIDD MODEID !!!\n"));
177 } else {
178 /* Create the framebuffer object */
179 fbtags[1].ti_Data = hiddmode;
180 fb = HIDD_Gfx_NewBitMap(mdd->gfxhidd, fbtags);
183 return fb;
187 * Set up graphics display driver.
188 * Creates necessary OS structures around it.
190 * gfxhidd - newly created driver object
191 * result - master driver structure
194 struct monitor_driverdata *driver_Setup(OOP_Object *gfxhidd, struct GfxBase *GfxBase)
196 ULONG cnt = 0;
197 IPTR hwcursor = 0;
198 IPTR noframebuffer = 0;
199 UWORD compose = 0;
200 BOOL can_compose = FALSE;
201 BOOL ok = TRUE;
202 ULONG i;
203 HIDDT_ModeID *modes, *m;
204 struct monitor_driverdata *mdd;
205 struct HIDD_ModeProperties props = {0};
207 D(bug("[driver_Setup] gfxhidd=0x%p (%s)\n", gfxhidd, OOP_OCLASS(gfxhidd)->ClassNode.ln_Name));
209 modes = HIDD_Gfx_QueryModeIDs(gfxhidd, NULL);
210 if (!modes)
211 return NULL;
213 /* Count number of display modes */
214 for (m = modes; *m != vHidd_ModeID_Invalid; m ++)
215 cnt++;
217 mdd = AllocVec(sizeof(struct monitor_driverdata) + cnt * sizeof(struct DisplayInfoHandle), MEMF_PUBLIC|MEMF_CLEAR);
218 D(bug("[driver_Setup] Allocated driverdata at 0x%p\n", mdd));
219 if (!mdd)
221 HIDD_Gfx_ReleaseModeIDs(gfxhidd, modes);
222 return NULL;
225 mdd->gfxhidd_orig = gfxhidd;
228 * 1. Fill in ModeID database in the driverdata.
229 * 2. Check if at least one mode supports composition.
230 * 3. Check if the driver has at least one non-palettized mode.
231 * (2) and (3) are needed to determine if we need/can use software screen
232 * composition with this driver.
234 for (i = 0; i <= cnt; i++)
236 mdd->modes[i].id = modes[i];
237 mdd->modes[i].drv = mdd;
239 /* Watch out! The last record in the array is terminator (vHidd_ModeID_Invalid) */
240 if ((i < cnt) && (!compose))
242 HIDD_Gfx_ModeProperties(gfxhidd, modes[i], &props, sizeof(props));
243 compose |= props.CompositionFlags;
245 if (!can_compose)
247 OOP_Object *sync, *pf;
248 IPTR colmod;
250 HIDD_Gfx_GetMode(gfxhidd, modes[i], &sync, &pf);
251 OOP_GetAttr(pf, aHidd_PixFmt_ColorModel, &colmod);
253 if (colmod == vHidd_ColorModel_TrueColor)
256 * At the moment software composer supports only truecolor screens.
257 * There also definitions for DirectColor, gray, etc, but there is
258 * no such hardware supported by now.
260 can_compose = TRUE;
266 HIDD_Gfx_ReleaseModeIDs(gfxhidd, modes);
268 /* Query properties of our driver */
269 #ifndef FORCE_SOFTWARE_SPRITE
270 OOP_GetAttr(gfxhidd, aHidd_Gfx_HWSpriteTypes, &hwcursor);
271 #endif
272 OOP_GetAttr(gfxhidd, aHidd_Gfx_NoFrameBuffer, &noframebuffer);
274 if (hwcursor)
276 mdd->gfxhidd = gfxhidd;
278 else
280 D(bug("[driver_Setup] Hardware mouse cursor is not supported, using fakegfx.hidd\n"));
282 mdd->gfxhidd = init_fakegfxhidd(gfxhidd, GfxBase);
283 if (mdd->gfxhidd)
284 mdd->flags |= DF_UseFakeGfx;
285 else
286 ok = FALSE;
289 if (ok)
291 D(bug("[driver_Setup] Ok\n"));
293 if (!noframebuffer)
296 * Instantiate framebuffer if needed.
297 * Note that we perform this operation on fakegfx.hidd if it was plugged in.
298 * This enables software mouse sprite on a framebuffer.
300 mdd->framebuffer = create_framebuffer(mdd, GfxBase);
301 mdd->flags |= DF_DirectFB;
304 if (noframebuffer || mdd->framebuffer)
306 D(bug("[driver_Setup] FRAMEBUFFER OK: %p\n", mdd->framebuffer));
308 if ((!compose) && can_compose)
310 D(bug("[driver_Setup] Software screen composition required\n"));
312 mdd->flags |= DF_SoftCompose;
313 composer_Setup(mdd, GfxBase);
316 return mdd;
319 if (mdd->framebuffer)
320 OOP_DisposeObject(mdd->framebuffer);
321 } /* if (fake gfx stuff ok) */
323 if (mdd->flags & DF_UseFakeGfx)
324 OOP_DisposeObject(mdd->gfxhidd);
326 FreeVec(mdd);
328 return NULL;
332 * Completely remove a driver from the OS.
334 * mdd - Driver structure to remove.
336 * Note that removing a driver is very unsafe operation. You must be
337 * sure that no bitmaps of this driver exist at the moment. Perhaps
338 * something should be done with it.
342 void driver_Expunge(struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
344 /* Notify Intuition */
345 if (CDD(GfxBase)->DriverNotify)
346 CDD(GfxBase)->DriverNotify(mdd->userdata, FALSE, CDD(GfxBase)->notify_data);
348 /* Dispose associated stuff */
349 OOP_DisposeObject(mdd->composer);
350 OOP_DisposeObject(mdd->framebuffer);
352 if (mdd->flags & DF_UseFakeGfx)
353 OOP_DisposeObject(mdd->gfxhidd);
355 /* Dispose driver object. This will take care about syncs etc */
356 OOP_DisposeObject(mdd->gfxhidd_orig );
358 FreeVec(mdd);
361 #if 0 /* Unused? */
363 static ULONG getbitmappixel(struct BitMap *bm
364 , LONG x
365 , LONG y
366 , UBYTE depth
367 , UBYTE plane_mask)
369 UBYTE i;
370 ULONG idx;
372 ULONG mask;
373 ULONG pen = 0;
375 idx = COORD_TO_BYTEIDX(x, y, bm->BytesPerRow);
376 mask = XCOORD_TO_MASK( x );
378 for (i = depth - 1; depth; i -- , depth -- )
380 pen <<= 1; /* stegerg: moved to here, was inside if!? */
382 if ((1L << i) & plane_mask)
384 UBYTE *plane = bm->Planes[i];
386 if (plane == (PLANEPTR)-1)
388 pen |= 1;
390 else if (plane != NULL)
392 if ((plane[idx] & mask) != 0)
393 pen |= 1;
398 return pen;
401 #endif
403 /* This is called for every ViewPorts chain during MrgCop() */
404 ULONG driver_PrepareViewPorts(struct HIDD_ViewPortData *vpd, struct View *v, struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
406 /* Do not bother the driver if there's nothing to prepare. Can be changed if needed. */
407 return vpd ? HIDD_Gfx_PrepareViewPorts(mdd->gfxhidd, vpd, v) : MCOP_OK;
411 * This is called for every ViewPorts chain during LoadView()
412 * LoadView() actually has no rights for errors because error condition
413 * in it will screw up the display, likely with no chance to recover.
414 * Because of this we always return zero here.
416 ULONG driver_LoadViewPorts(struct HIDD_ViewPortData *vpd, struct View *v, struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
418 struct BitMap *bitmap;
419 OOP_Object *bm, *fb;
420 BOOL compositing = FALSE;
422 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Showing ViewPortData 0x%p, BitMap object 0x%p\n", vpd, vpd ? vpd->Bitmap : NULL));
423 mdd->display = vpd;
425 /* First try the new method */
426 if (HIDD_Gfx_ShowViewPorts(mdd->gfxhidd, vpd))
428 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] ShowViewPorts() worked\n"));
429 return 0;
433 * ShowViewPorts not supported.
434 * Perhaps we can use software screen composer. But for proper operation
435 * we need to figure out our frontmost bitmap.
437 if (vpd)
439 bitmap = vpd->vpe->ViewPort->RasInfo->BitMap;
440 bm = vpd->Bitmap;
442 else
444 bitmap = NULL;
445 bm = NULL;
447 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Old bitmap 0x%p, New bitmap 0x%p, object 0x%p\n", mdd->frontbm, bitmap, bm));
449 if (mdd->composer)
452 * Composer present. Give ViewPorts chain to it.
453 * For improved visual appearance it will call Show itself and return its result.
454 * The composer is expected to handle all possible failures internally. If some
455 * internal error happens, it must fail back to single HIDD_Gfx_Show().
457 fb = composer_LoadViewPorts(mdd->composer, vpd, &compositing, GfxBase);
458 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Composer returned 0x%p, active: %d\n", fb, compositing));
460 else
463 * First check if the bitmap is already displayed. If so, do nothing (because
464 * displaying the same bitmap twice may cause some problems)
466 if (mdd->frontbm == bitmap)
467 return 0;
469 fb = HIDD_Gfx_Show(mdd->gfxhidd, bm, fHidd_Gfx_Show_CopyBack);
470 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Show() returned 0x%p\n", fb));
473 /* Summary of possible responses (when no error happened):
474 NoFrameBuffer = FALSE: bm = NULL -> fb != NULL and bm != fb
475 NoFrameBuffer = FALSE: bm != NULL -> fb != NULL and bm != fb
476 NoFrameBuffer = TRUE : bm = NULL -> fb == NULL and bm == fb
477 NoFrameBuffer = TRUE : bm != NULL -> fb != NULL and bm == fb
479 if (fb)
481 /* The screen is not empty, 'fb' is on display now. */
482 IPTR width, height;
484 /* Uninstall the framebuffer from old frontmost BitMap (if present) */
485 UninstallFB(mdd);
488 * We need to always remember our new frontmost bitmap, even if we do not work
489 * with a framebuffer. This is needed for the check against showing the same
490 * bitmap twice.
492 mdd->frontbm = bitmap;
495 * Install the framebuffer into new bitmap. Only if software composition is inactive.
496 * If it is active, our BitMap is mirrored, not replaced.
498 if (!compositing)
499 InstallFB(mdd, GfxBase);
501 /* Tell the driver to refresh the screen */
502 OOP_GetAttr(fb, aHidd_BitMap_Width, &width);
503 OOP_GetAttr(fb, aHidd_BitMap_Height, &height);
504 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Updating display, new size: %d x %d\n", width, height));
506 HIDD_BM_UpdateRect(fb, 0, 0, width, height);
508 else
511 * Screen is empty, simply reset the current bitmap.
512 * Only framebuffer-less driver can return NULL. So we don't need
513 * to call UninstallFB() here.
515 mdd->frontbm = NULL;
518 return 0;
522 * In-Place framebuffer install/uninstall routines.
524 * The idea behind: we gave some bitmap to display, the driver *HAS COPIED* it
525 * into the framebuffer. After this we must use a framebuffer bitmap instead of
526 * original one for all rendering operations.
527 * When another bitmap is displayed, the framebuffer contents will be put back
528 * into the original bitmap, and we will swap it back.
530 * This swap actually happens only of DF_DirectFB is set for the driver.
532 * FIXME: THIS IS NOT THREADSAFE
533 * To make this threadsafe we have to lock all gfx access in all the rendering
534 * calls.
536 void InstallFB(struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
538 if ((mdd->flags & DF_DirectFB) && mdd->frontbm)
540 struct BitMap *bitmap = mdd->frontbm;
541 OOP_Object *pf;
543 /* Backup original data */
544 mdd->bm_bak = HIDD_BM_OBJ(bitmap);
545 mdd->colmod_bak = HIDD_BM_COLMOD(bitmap);
546 mdd->colmap_bak = HIDD_BM_COLMAP(bitmap);
548 /* Insert the framebuffer in its place */
549 OOP_GetAttr(mdd->framebuffer, aHidd_BitMap_PixFmt, (IPTR *)&pf);
551 HIDD_BM_OBJ(bitmap) = mdd->framebuffer;
552 HIDD_BM_COLMOD(bitmap) = OOP_GET(pf, aHidd_PixFmt_ColorModel);
553 HIDD_BM_COLMAP(bitmap) = (OOP_Object *)OOP_GET(mdd->framebuffer, aHidd_BitMap_ColorMap);
555 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Installed framebuffer: BitMap 0x%p, object 0x%p\n", mdd->frontbm, mdd->bm_bak));
559 void UninstallFB(struct monitor_driverdata *mdd)
561 if (mdd->bm_bak)
563 /* Put back the old values into the old bitmap */
564 struct BitMap *oldbm = mdd->frontbm;
566 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Uninstalling framebuffer: BitMap 0x%p, object 0x%p\n", mdd->frontbm, mdd->bm_bak));
568 HIDD_BM_OBJ(oldbm) = mdd->bm_bak;
569 HIDD_BM_COLMOD(oldbm) = mdd->colmod_bak;
570 HIDD_BM_COLMAP(oldbm) = mdd->colmap_bak;
572 /* No framebuffer installed */
573 mdd->bm_bak = NULL;
577 /* Find the first visible ViewPortData for the specified monitor in the View */
578 struct HIDD_ViewPortData *driver_FindViewPorts(struct View *view, struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
580 struct ViewPort *vp;
582 for (vp = view->ViewPort; vp; vp = vp->Next)
584 if (!(vp->Modes & VP_HIDE))
586 struct ViewPortExtra *vpe = (struct ViewPortExtra *)GfxLookUp(vp);
588 if (VPE_DRIVER(vpe) == mdd)
589 return VPE_DATA(vpe);
592 return NULL;
596 * Iterate through HIDD_ViewPortData chains in the view and call
597 * the specified function for every chain and every driver.
598 * The function will be called at least once for every driver.
599 * If there is no ViewPorts for the driver, the function will be
600 * called with ViewPortData = NULL.
602 ULONG DoViewFunction(struct View *view, VIEW_FUNC fn, struct GfxBase *GfxBase)
604 struct monitor_driverdata *mdd;
605 ULONG rc = 0;
607 ObtainSemaphoreShared(&CDD(GfxBase)->displaydb_sem);
609 for (mdd = CDD(GfxBase)->monitors; mdd; mdd = mdd->next)
611 struct HIDD_ViewPortData *vpd = NULL;
614 * Find the first visible ViewPort for this display. It
615 * will be a start of bitmaps chain to process.
617 if (view)
618 vpd = driver_FindViewPorts(view, mdd, GfxBase);
620 rc = fn(vpd, view, mdd, GfxBase);
622 /* Interrupt immediately if the callback returned error */
623 if (rc)
624 break;
627 ReleaseSemaphore(&CDD(GfxBase)->displaydb_sem);
629 return rc;