Removed an unnecessary include directive.
[AROS.git] / rom / graphics / graphics_driver.c
blob196fd166dc6bfa0fb9d6531bc063acc9cd9fec3b
1 /*
2 Copyright © 1995-2017, 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 <hidd/compositor.h>
36 #include <hidd/gfx.h>
38 #include <cybergraphx/cybergraphics.h>
40 #include <stdio.h>
41 #include <string.h>
43 #include "graphics_intern.h"
44 #include "compositor_driver.h"
45 #include "fakegfxhidd.h"
46 #include "intregions.h"
47 #include "dispinfo.h"
48 #include "gfxfuncsupport.h"
49 #include "fontsupport.h"
51 #define DEBUG_INIT(x) x
52 #define DEBUG_LOADVIEW(x)
54 /* Define this if you wish to enforce using software mouse sprite
55 even for drivers that support hardware one. Useful for debugging
56 and testing
57 #define FORCE_SOFTWARE_SPRITE */
59 struct ETextFont
61 struct TextFont etf_Font;
64 /* *********************** RastPort extra data handling *********************** */
66 struct gfx_driverdata *AllocDriverData(struct RastPort *rp, BOOL alloc, struct GfxBase *GfxBase)
68 struct gfx_driverdata *dd = ObtainDriverData(rp);
70 if (alloc && !dd)
72 dd = AllocVec(sizeof(struct gfx_driverdata), MEMF_CLEAR);
73 if (dd)
75 rp->RP_Extra = dd;
76 dd->dd_RastPort = rp;
80 return dd;
83 /* *********************** Display driver handling *********************** */
85 int driver_init(struct GfxBase * GfxBase)
87 OOP_Class *baseGfx;
89 EnterFunc(bug("driver_init()\n"));
91 /* Our underlying RTG subsystem core must be already up and running */
92 if (!OpenLibrary("gfx.hidd", 0))
93 return FALSE;
95 baseGfx = OOP_FindClass(CLID_Hidd_Gfx);
97 /* Initialize the semaphores */
98 InitSemaphore(&(PrivGBase(GfxBase)->blit_sema));
100 /* Init the needed attrbases */
102 __IHidd_BitMap = OOP_ObtainAttrBase(IID_Hidd_BitMap);
103 __IHidd_GC = OOP_ObtainAttrBase(IID_Hidd_GC);
104 __IHidd_Sync = OOP_ObtainAttrBase(IID_Hidd_Sync);
105 __IHidd_PixFmt = OOP_ObtainAttrBase(IID_Hidd_PixFmt);
106 __IHidd_PlanarBM = OOP_ObtainAttrBase(IID_Hidd_PlanarBM);
107 __IHidd_Gfx = OOP_ObtainAttrBase(IID_Hidd_Gfx);
108 __IHidd_FakeGfxHidd = OOP_ObtainAttrBase(IID_Hidd_FakeGfxHidd);
110 if (__IHidd_BitMap &&
111 __IHidd_GC &&
112 __IHidd_Sync &&
113 __IHidd_PixFmt &&
114 __IHidd_PlanarBM &&
115 __IHidd_Gfx &&
116 __IHidd_FakeGfxHidd)
118 CDD(GfxBase)->gcClass = OOP_FindClass(CLID_Hidd_GC);
119 if (!CDD(GfxBase)->gcClass)
120 return FALSE;
122 /* Init display mode database */
123 InitSemaphore(&CDD(GfxBase)->displaydb_sem);
124 CDD(GfxBase)->invalid_id = INVALID_ID;
125 CDD(GfxBase)->last_id = AROS_RTG_MONITOR_ID;
127 /* Init software driver */
128 CDD(GfxBase)->memorygfx = HW_AddDriver(PrivGBase(GfxBase)->GfxRoot, baseGfx, NULL);
129 DEBUG_INIT(bug("[driver_init] Memory driver object 0x%p\n", CDD(GfxBase)->memorygfx));
131 if (CDD(GfxBase)->memorygfx)
133 struct TagItem bm_create_tags[] =
135 { aHidd_BitMap_GfxHidd , (IPTR)CDD(GfxBase)->memorygfx },
136 { aHidd_PlanarBM_BitMap, 0 },
137 { TAG_DONE , 0 }
140 CDD(GfxBase)->planarbm_cache = create_object_cache(NULL, CLID_Hidd_PlanarBM, bm_create_tags, GfxBase);
141 DEBUG_INIT(bug("[driver_init] Planar bitmap cache 0x%p\n", CDD(GfxBase)->planarbm_cache));
143 if (CDD(GfxBase)->planarbm_cache)
145 CDD(GfxBase)->gc_cache = create_object_cache(NULL, CLID_Hidd_GC, NULL, GfxBase);
146 DEBUG_INIT(bug("[driver_init] GC cache 0x%p\n", CDD(GfxBase)->planarbm_cache));
148 if (CDD(GfxBase)->gc_cache)
149 ReturnInt("driver_init", int, TRUE);
151 delete_object_cache(CDD(GfxBase)->planarbm_cache, GfxBase);
153 OOP_DisposeObject(CDD(GfxBase)->memorygfx);
157 ReturnInt("driver_init", int, FALSE);
160 /* Called after DOS is up & running */
161 static OOP_Object *create_framebuffer(struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
163 struct TagItem fbtags[] = {
164 { aHidd_BitMap_FrameBuffer, TRUE },
165 { aHidd_BitMap_ModeID, 0 },
166 { TAG_DONE, 0 }
169 HIDDT_ModeID hiddmode;
170 OOP_Object *fb = NULL;
172 /* Get the highest available resolution at the best possible depth */
173 hiddmode = get_best_resolution_and_depth(mdd, GfxBase);
174 if (vHidd_ModeID_Invalid == hiddmode) {
175 D(bug("!!! create_framebuffer(): COULD NOT GET HIDD MODEID !!!\n"));
176 } else {
177 /* Create the framebuffer object */
178 fbtags[1].ti_Data = hiddmode;
179 fb = HIDD_Gfx_CreateObject(mdd->gfxhidd, PrivGBase(GfxBase)->basebm, fbtags);
182 return fb;
186 * Set up graphics display driver.
187 * Creates necessary OS structures around it.
189 * gfxhidd - newly created driver object
190 * result - master driver structure
193 struct monitor_driverdata *driver_Setup(OOP_Object *gfxhidd, struct GfxBase *GfxBase)
195 ULONG cnt = 0;
196 IPTR hwcursor = 0;
197 IPTR fbtype = vHidd_FrameBuffer_Direct; /* We default to direct for historical reasons */
198 UWORD compose = 0;
199 BOOL can_compose = FALSE;
200 BOOL ok = TRUE;
201 ULONG i;
202 HIDDT_ModeID *modes, *m;
203 struct monitor_driverdata *mdd;
204 struct HIDD_ModeProperties props = {0};
206 D(bug("[driver_Setup] gfxhidd=0x%p (%s)\n", gfxhidd, OOP_OCLASS(gfxhidd)->ClassNode.ln_Name));
208 modes = HIDD_Gfx_QueryModeIDs(gfxhidd, NULL);
209 if (!modes)
210 return NULL;
212 /* Count number of display modes */
213 for (m = modes; *m != vHidd_ModeID_Invalid; m ++)
214 cnt++;
216 mdd = AllocVec(sizeof(struct monitor_driverdata) + cnt * sizeof(struct DisplayInfoHandle), MEMF_PUBLIC|MEMF_CLEAR);
217 D(bug("[driver_Setup] Allocated driverdata at 0x%p\n", mdd));
218 if (!mdd)
220 HIDD_Gfx_ReleaseModeIDs(gfxhidd, modes);
221 return NULL;
224 mdd->gfxhidd_orig = gfxhidd;
227 * 1. Fill in ModeID database in the driverdata.
228 * 2. Check if at least one mode supports composition.
229 * 3. Check if the driver has at least one non-palettized mode.
230 * (2) and (3) are needed to determine if we need/can use software screen
231 * composition with this driver.
233 for (i = 0; i <= cnt; i++)
235 mdd->modes[i].id = modes[i];
236 mdd->modes[i].drv = mdd;
238 /* Watch out! The last record in the array is terminator (vHidd_ModeID_Invalid) */
239 if ((i < cnt) && (!compose))
241 HIDD_Gfx_ModeProperties(gfxhidd, modes[i], &props, sizeof(props));
242 compose |= props.CompositionFlags;
244 if (!can_compose)
246 OOP_Object *sync, *pf;
247 IPTR colmod;
249 HIDD_Gfx_GetMode(gfxhidd, modes[i], &sync, &pf);
250 OOP_GetAttr(pf, aHidd_PixFmt_ColorModel, &colmod);
252 if (colmod == vHidd_ColorModel_TrueColor)
255 * At the moment software compositor supports only truecolor screens.
256 * There also definitions for DirectColor, gray, etc, but there is
257 * no such hardware supported by now.
259 can_compose = TRUE;
265 HIDD_Gfx_ReleaseModeIDs(gfxhidd, modes);
267 /* Query properties of our driver */
268 #ifndef FORCE_SOFTWARE_SPRITE
269 OOP_GetAttr(gfxhidd, aHidd_Gfx_HWSpriteTypes, &hwcursor);
270 #endif
271 OOP_GetAttr(gfxhidd, aHidd_Gfx_FrameBufferType, &fbtype);
273 if (hwcursor)
275 mdd->gfxhidd = gfxhidd;
277 else
279 D(bug("[driver_Setup] Hardware mouse cursor is not supported, using fakegfx.hidd\n"));
281 mdd->gfxhidd = init_fakegfxhidd(gfxhidd, GfxBase);
282 if (mdd->gfxhidd)
283 mdd->flags |= DF_UseFakeGfx;
284 else
285 ok = FALSE;
288 if (ok)
290 D(bug("[driver_Setup] Ok, framebuffer type %ld\n", fbtype));
293 * Instantiate framebuffer if needed.
294 * Note that we perform this operation on fakegfx.hidd if it was plugged in.
295 * This enables software mouse sprite on a framebuffer.
297 switch (fbtype)
299 case vHidd_FrameBuffer_Direct:
300 mdd->flags |= DF_DirectFB;
302 case vHidd_FrameBuffer_Mirrored:
303 mdd->framebuffer = create_framebuffer(mdd, GfxBase);
304 break;
307 if ((fbtype == vHidd_FrameBuffer_None) || mdd->framebuffer)
309 D(bug("[driver_Setup] FRAMEBUFFER OK: %p\n", mdd->framebuffer));
311 if ((!compose) && can_compose)
313 D(bug("[driver_Setup] Software screen composition required\n"));
315 mdd->flags |= DF_SoftCompose;
316 compositor_Setup(mdd, GfxBase);
319 return mdd;
322 if (mdd->framebuffer)
323 OOP_DisposeObject(mdd->framebuffer);
324 } /* if (fake gfx stuff ok) */
326 if (mdd->flags & DF_UseFakeGfx)
327 OOP_DisposeObject(mdd->gfxhidd);
329 FreeVec(mdd);
331 return NULL;
335 * Completely remove a driver from the OS.
337 * mdd - Driver structure to remove.
339 * Note that removing a driver is very unsafe operation. You must be
340 * sure that no bitmaps of this driver exist at the moment. Perhaps
341 * something should be done with it.
345 void driver_Expunge(struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
347 /* Notify Intuition */
348 if (CDD(GfxBase)->DriverNotify)
349 CDD(GfxBase)->DriverNotify(mdd->userdata, FALSE, CDD(GfxBase)->notify_data);
351 /* Dispose associated stuff */
352 OOP_DisposeObject(mdd->compositor);
353 OOP_DisposeObject(mdd->framebuffer);
355 if (mdd->flags & DF_UseFakeGfx)
356 OOP_DisposeObject(mdd->gfxhidd);
358 /* Dispose driver object. This will take care about syncs etc */
359 OOP_DisposeObject(mdd->gfxhidd_orig );
361 FreeVec(mdd);
364 #if 0 /* Unused? */
366 static ULONG getbitmappixel(struct BitMap *bm
367 , LONG x
368 , LONG y
369 , UBYTE depth
370 , UBYTE plane_mask)
372 UBYTE i;
373 ULONG idx;
375 ULONG mask;
376 ULONG pen = 0;
378 idx = COORD_TO_BYTEIDX(x, y, bm->BytesPerRow);
379 mask = XCOORD_TO_MASK( x );
381 for (i = depth - 1; depth; i -- , depth -- )
383 pen <<= 1; /* stegerg: moved to here, was inside if!? */
385 if ((1L << i) & plane_mask)
387 UBYTE *plane = bm->Planes[i];
389 if (plane == (PLANEPTR)-1)
391 pen |= 1;
393 else if (plane != NULL)
395 if ((plane[idx] & mask) != 0)
396 pen |= 1;
401 return pen;
404 #endif
406 /* This is called for every ViewPorts chain during MrgCop() */
407 ULONG driver_PrepareViewPorts(struct HIDD_ViewPortData *vpd, struct View *v, struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
409 /* Do not bother the driver if there's nothing to prepare. Can be changed if needed. */
410 return vpd ? HIDD_Gfx_PrepareViewPorts(mdd->gfxhidd, vpd, v) : MCOP_OK;
414 * This is called for every ViewPorts chain during LoadView()
415 * LoadView() actually has no rights for errors because error condition
416 * in it will screw up the display, likely with no chance to recover.
417 * Because of this we always return zero here.
419 ULONG driver_LoadViewPorts(struct HIDD_ViewPortData *vpd, struct View *v, struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
421 struct BitMap *bitmap;
422 OOP_Object *bm, *fb;
423 BOOL compositing = FALSE;
425 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Showing ViewPortData 0x%p, BitMap object 0x%p\n", vpd, vpd ? vpd->Bitmap : NULL));
426 mdd->display = vpd;
428 /* First try the new method */
429 if (HIDD_Gfx_ShowViewPorts(mdd->gfxhidd, vpd))
431 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] ShowViewPorts() worked\n"));
432 return 0;
436 * ShowViewPorts not supported.
437 * Perhaps we can use software screen compositor. But for proper operation
438 * we need to figure out our frontmost bitmap.
440 if (vpd)
442 bitmap = vpd->vpe->ViewPort->RasInfo->BitMap;
443 bm = vpd->Bitmap;
445 else
447 bitmap = NULL;
448 bm = NULL;
450 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Old bitmap 0x%p, New bitmap 0x%p, object 0x%p\n", mdd->frontbm, bitmap, bm));
452 if (mdd->compositor)
455 * Compositor present. Give ViewPorts chain to it.
456 * For improved visual appearance it will call Show itself and return its result.
457 * The compositor is expected to handle all possible failures internally. If some
458 * internal error happens, it must fail back to single HIDD_Gfx_Show().
460 fb = compositor_LoadViewPorts(mdd->compositor, vpd, &compositing, GfxBase);
461 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Compositor returned 0x%p, active: %d\n", fb, compositing));
463 else
466 * First check if the bitmap is already displayed. If so, do nothing (because
467 * displaying the same bitmap twice may cause some problems)
469 if (mdd->frontbm == bitmap)
470 return 0;
472 fb = HIDD_Gfx_Show(mdd->gfxhidd, bm, fHidd_Gfx_Show_CopyBack);
473 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Show() returned 0x%p\n", fb));
476 /* Summary of possible responses (when no error happened):
477 NoFrameBuffer = FALSE: bm = NULL -> fb != NULL and bm != fb
478 NoFrameBuffer = FALSE: bm != NULL -> fb != NULL and bm != fb
479 NoFrameBuffer = TRUE : bm = NULL -> fb == NULL and bm == fb
480 NoFrameBuffer = TRUE : bm != NULL -> fb != NULL and bm == fb
482 if (fb)
484 /* The screen is not empty, 'fb' is on display now. */
485 IPTR width, height;
487 /* Uninstall the framebuffer from old frontmost BitMap (if present) */
488 UninstallFB(mdd, GfxBase);
491 * We need to always remember our new frontmost bitmap, even if we do not work
492 * with a framebuffer. This is needed for the check against showing the same
493 * bitmap twice.
495 mdd->frontbm = bitmap;
498 * Install the framebuffer into new bitmap. Only if software composition is inactive.
499 * If it is active, our BitMap is mirrored, not replaced.
501 if (!compositing)
502 InstallFB(mdd, GfxBase);
504 /* Tell the driver to refresh the screen */
505 OOP_GetAttr(fb, aHidd_BitMap_Width, &width);
506 OOP_GetAttr(fb, aHidd_BitMap_Height, &height);
507 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Updating display, new size: %d x %d\n", width, height));
509 HIDD_BM_UpdateRect(fb, 0, 0, width, height);
511 else
514 * Screen is empty, simply reset the current bitmap.
515 * Only framebuffer-less driver can return NULL. So we don't need
516 * to call UninstallFB() here.
518 mdd->frontbm = NULL;
521 return 0;
525 * In-Place framebuffer install/uninstall routines.
527 * The idea behind: we gave some bitmap to display, the driver *HAS COPIED* it
528 * into the framebuffer. After this we must use a framebuffer bitmap instead of
529 * original one for all rendering operations.
530 * When another bitmap is displayed, the framebuffer contents will be put back
531 * into the original bitmap, and we will swap it back.
533 * This swap actually happens only of DF_DirectFB is set for the driver.
535 * FIXME: THIS IS NOT THREADSAFE
536 * To make this threadsafe we have to lock all gfx access in all the rendering
537 * calls.
539 void InstallFB(struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
541 if ((mdd->flags & DF_DirectFB) && mdd->frontbm)
543 struct BitMap *bitmap = mdd->frontbm;
544 OOP_Object *pf;
546 /* Backup original data */
547 mdd->bm_bak = HIDD_BM_OBJ(bitmap);
548 mdd->colmod_bak = HIDD_BM_COLMOD(bitmap);
549 mdd->colmap_bak = HIDD_BM_COLMAP(bitmap);
551 /* Insert the framebuffer in its place */
552 OOP_GetAttr(mdd->framebuffer, aHidd_BitMap_PixFmt, (IPTR *)&pf);
554 HIDD_BM_OBJ(bitmap) = mdd->framebuffer;
555 HIDD_BM_COLMOD(bitmap) = OOP_GET(pf, aHidd_PixFmt_ColorModel);
556 HIDD_BM_COLMAP(bitmap) = (OOP_Object *)OOP_GET(mdd->framebuffer, aHidd_BitMap_ColorMap);
558 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Installed framebuffer: BitMap 0x%p, object 0x%p\n", mdd->frontbm, mdd->bm_bak));
562 void UninstallFB(struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
564 if (mdd->bm_bak)
566 /* Put back the old values into the old bitmap */
567 struct BitMap *oldbm = mdd->frontbm;
569 DEBUG_LOADVIEW(bug("[driver_LoadViewPorts] Uninstalling framebuffer: BitMap 0x%p, object 0x%p\n", mdd->frontbm, mdd->bm_bak));
571 HIDD_BM_OBJ(oldbm) = mdd->bm_bak;
572 HIDD_BM_COLMOD(oldbm) = mdd->colmod_bak;
573 HIDD_BM_COLMAP(oldbm) = mdd->colmap_bak;
575 /* No framebuffer installed */
576 mdd->bm_bak = NULL;
580 /* Find the first visible ViewPortData for the specified monitor in the View */
581 struct HIDD_ViewPortData *driver_FindViewPorts(struct View *view, struct monitor_driverdata *mdd, struct GfxBase *GfxBase)
583 struct ViewPort *vp;
585 for (vp = view->ViewPort; vp; vp = vp->Next)
587 if (!(vp->Modes & VP_HIDE))
589 struct ViewPortExtra *vpe = (struct ViewPortExtra *)GfxLookUp(vp);
591 if (VPE_DRIVER(vpe) == mdd)
592 return VPE_DATA(vpe);
595 return NULL;
599 * Iterate through HIDD_ViewPortData chains in the view and call
600 * the specified function for every chain and every driver.
601 * The function will be called at least once for every driver.
602 * If there is no ViewPorts for the driver, the function will be
603 * called with ViewPortData = NULL.
605 ULONG DoViewFunction(struct View *view, VIEW_FUNC fn, struct GfxBase *GfxBase)
607 struct monitor_driverdata *mdd;
608 ULONG rc = 0;
610 ObtainSemaphoreShared(&CDD(GfxBase)->displaydb_sem);
612 for (mdd = CDD(GfxBase)->monitors; mdd; mdd = mdd->next)
614 struct HIDD_ViewPortData *vpd = NULL;
617 * Find the first visible ViewPort for this display. It
618 * will be a start of bitmaps chain to process.
620 if (view)
621 vpd = driver_FindViewPorts(view, mdd, GfxBase);
623 rc = fn(vpd, view, mdd, GfxBase);
625 /* Interrupt immediately if the callback returned error */
626 if (rc)
627 break;
630 ReleaseSemaphore(&CDD(GfxBase)->displaydb_sem);
632 return rc;