wip prep commit in lieu of gfx subsystem update changes, part 2.
[AROS.git] / rom / hidds / gfx / gfx_init.c
blobb0ba6bebfe3a5b233cd1d7df3f86cad1a2e47e44
1 /*
2 Copyright © 1995-2017, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Graphics hidd initialization code.
6 Lang: English.
7 */
9 #include "gfx_debug.h"
11 #define __OOP_NOMETHODBASES__
13 #include <stddef.h>
14 #include <exec/types.h>
16 #include <proto/exec.h>
18 #include <aros/symbolsets.h>
20 #include "gfx_intern.h"
22 #include LC_LIBDEFS_FILE
24 #undef csd
26 /****************************************************************************************/
28 /* Since the shift/mask values of a pixel format are designed for pixel
29 access, not byte access, they are endianess dependant */
31 #if AROS_BIG_ENDIAN
32 #include "stdpixfmts_be.h"
33 #else
34 #include "stdpixfmts_le.h"
35 #endif
37 /****************************************************************************************/
40 Parses the tags supplied in 'tags' and puts the result into 'pf'.
41 It also checks to see if all needed attrs are supplied.
42 It uses 'attrcheck' for this, so you may find attrs outside
43 of this function, and mark them as found before calling this function
46 #define PFAF(x) (1L << aoHidd_PixFmt_ ## x)
47 #define PF_COMMON_AF ( PFAF(Depth) | PFAF(BitsPerPixel) | PFAF(BytesPerPixel) \
48 | PFAF(ColorModel) | PFAF(BitMapType) )
50 #define PF_TRUECOLOR_AF ( PFAF(RedMask) | PFAF(GreenMask) | PFAF(BlueMask) | PFAF(AlphaMask) | \
51 PFAF(RedShift) | PFAF(GreenShift) | PFAF(BlueShift) | PFAF(AlphaShift))
53 #define PF_PALETTE_AF ( PFAF(CLUTMask) | PFAF(CLUTShift) | PFAF(RedMask) | PFAF(GreenMask) | \
54 PFAF(BlueMask) )
56 #define PFAO(x) (aoHidd_PixFmt_ ## x)
58 /****************************************************************************************/
60 BOOL parse_pixfmt_tags(struct TagItem *tags, HIDDT_PixelFormat *pf,
61 ULONG ATTRCHECK(pixfmt), struct class_static_data *csd)
63 IPTR attrs[num_Hidd_PixFmt_Attrs] = {0};
64 struct Library *OOPBase = csd->cs_OOPBase;
66 if (0 != OOP_ParseAttrs(tags, attrs, num_Hidd_PixFmt_Attrs,
67 &ATTRCHECK(pixfmt), HiddPixFmtAttrBase))
69 D(bug("!!! parse_pixfmt_tags: ERROR PARSING TAGS THROUGH OOP_ParseAttrs !!!\n"));
70 return FALSE;
73 if (PF_COMMON_AF != (PF_COMMON_AF & ATTRCHECK(pixfmt)))
75 D(bug("!!! parse_pixfmt_tags: Missing PixFmt attributes passed to parse_pixfmt_tags(): %x !!!\n", ATTRCHECK(pixfmt)));
76 return FALSE;
79 /* Set the common attributes */
80 pf->depth = attrs[PFAO(Depth)];
81 pf->size = attrs[PFAO(BitsPerPixel)];
82 pf->bytes_per_pixel = attrs[PFAO(BytesPerPixel)];
83 /* Fill in only real StdPixFmt specification. Special values (Native and Native32)
84 are not allowed here */
85 if (attrs[PFAO(StdPixFmt)] >= num_Hidd_PseudoStdPixFmt)
86 pf->stdpixfmt = attrs[PFAO(StdPixFmt)];
88 SET_PF_COLMODEL( pf, attrs[PFAO(ColorModel)]);
89 SET_PF_BITMAPTYPE(pf, attrs[PFAO(BitMapType)]);
91 if (ATTRCHECK(pixfmt) & PFAF(SwapPixelBytes))
93 SET_PF_SWAPPIXELBYTES_FLAG(pf, attrs[PFAO(SwapPixelBytes)]);
96 /* Set the colormodel specific stuff */
97 switch (HIDD_PF_COLMODEL(pf))
99 case vHidd_ColorModel_TrueColor:
100 /* Check that we got all the truecolor describing stuff */
101 if (PF_TRUECOLOR_AF != (PF_TRUECOLOR_AF & ATTRCHECK(pixfmt)))
103 D(bug("!!! Unsufficient true color format describing attrs to pixfmt in parse_pixfmt_tags() !!!\n"));
104 return FALSE;
107 /* Set the truecolor stuff */
108 pf->red_mask = attrs[PFAO(RedMask)];
109 pf->green_mask = attrs[PFAO(GreenMask)];
110 pf->blue_mask = attrs[PFAO(BlueMask)];
111 pf->alpha_mask = attrs[PFAO(AlphaMask)];
113 pf->red_shift = attrs[PFAO(RedShift)];
114 pf->green_shift = attrs[PFAO(GreenShift)];
115 pf->blue_shift = attrs[PFAO(BlueShift)];
116 pf->alpha_shift = attrs[PFAO(AlphaShift)];
117 break;
119 case vHidd_ColorModel_Palette:
120 case vHidd_ColorModel_StaticPalette:
121 if ( PF_PALETTE_AF != (PF_PALETTE_AF & ATTRCHECK(pixfmt)))
123 D(bug("!!! Unsufficient palette format describing attrs to pixfmt in parse_pixfmt_tags() !!!\n"));
124 return FALSE;
127 /* set palette stuff */
128 pf->clut_mask = attrs[PFAO(CLUTMask)];
129 pf->clut_shift = attrs[PFAO(CLUTShift)];
131 pf->red_mask = attrs[PFAO(RedMask)];
132 pf->green_mask = attrs[PFAO(GreenMask)];
133 pf->blue_mask = attrs[PFAO(BlueMask)];
135 break;
137 } /* shift (colormodel) */
139 return TRUE;
142 /****************************************************************************************/
145 Create an empty object and initialize it the "ugly" way. This only works with
146 CLID_Hidd_PixFmt and CLID_Hidd_Sync classes
149 /****************************************************************************************/
151 static OOP_Object *create_and_init_object(OOP_Class *cl, UBYTE *data, ULONG datasize,
152 struct class_static_data *csd)
154 OOP_Object *o;
155 struct Library *OOPBase = csd->cs_OOPBase;
157 o = OOP_NewObject(cl, NULL, NULL);
158 if (NULL == o)
160 D(bug("!!! UNABLE TO CREATE OBJECT IN create_and_init_object() !!!\n"));
161 return NULL;
164 memcpy(o, data, datasize);
166 return o;
169 /****************************************************************************************/
171 static VOID delete_pixfmts(struct class_static_data *csd)
173 struct Node *n, *safe;
174 struct Library *OOPBase = csd->cs_OOPBase;
176 ForeachNodeSafe(&csd->pflist, n, safe)
177 OOP_DisposeObject((OOP_Object *)PIXFMT_OBJ(n));
180 /****************************************************************************************/
182 static BOOL create_std_pixfmts(struct class_static_data *csd)
184 ULONG i;
185 struct pixfmt_data *pf;
187 memset(csd->std_pixfmts, 0, sizeof (OOP_Object *) * num_Hidd_StdPixFmt);
189 for (i = 0; i < num_Hidd_StdPixFmt; i ++)
191 pf = (struct pixfmt_data *)create_and_init_object(csd->pixfmtclass, (UBYTE *)&stdpfs[i], sizeof (stdpfs[i]), csd);
193 if (!pf)
195 D(bug("FAILED TO CREATE PIXEL FORMAT %d\n", i));
196 delete_pixfmts(csd);
197 ReturnBool("create_stdpixfmts", FALSE);
200 csd->std_pixfmts[i] = &pf->pf;
201 /* We don't use semaphore protection here because we do this only during class init stage */
202 pf->refcount = 1;
203 AddTail((struct List *)&csd->pflist, (struct Node *)&pf->node);
205 ReturnBool("create_stdpixfmts", TRUE);
208 /****************************************************************************************/
210 static const CONST_STRPTR interfaces[NUM_ATTRBASES] =
212 IID_Hidd_BitMap,
213 IID_Hidd_Gfx,
214 IID_Hidd_GC,
215 IID_Hidd_ColorMap,
216 IID_HW,
217 IID_Hidd,
218 IID_Hidd_Overlay,
219 IID_Hidd_Sync,
220 IID_Hidd_PixFmt,
221 IID_Hidd_PlanarBM,
222 IID_Hidd_ChunkyBM
225 static ULONG ObtainAttrBases(OOP_AttrBase *bases, CONST_STRPTR const *interfaces, ULONG count, struct Library *OOPBase)
227 ULONG i;
228 ULONG failed = 0;
230 for (i = 0; i < count; i++)
232 bases[i] = OOP_ObtainAttrBase(interfaces[i]);
233 if (!bases[i])
234 failed++;
237 return failed;
240 static void ReleaseAttrBases(OOP_AttrBase *bases, CONST_STRPTR const *interfaces, ULONG count, struct Library *OOPBase)
242 ULONG i;
244 for (i = 0; i < count; i++)
246 if (bases[i])
247 OOP_ReleaseAttrBase(interfaces[i]);
251 static ULONG GetMethodBases(OOP_MethodID *bases, CONST_STRPTR const *interfaces, ULONG count, struct Library *OOPBase)
253 ULONG i;
254 ULONG failed = 0;
256 for (i = 0; i < count; i++)
258 bases[i] = OOP_GetMethodID(interfaces[i], 0);
259 if (bases[i] == -1)
260 failed++;
263 return failed;
266 static int GFX_ClassInit(LIBBASETYPEPTR LIBBASE)
268 struct class_static_data *csd = &LIBBASE->hdg_csd;
269 struct Library *OOPBase = csd->cs_OOPBase;
270 OOP_Object *hwroot;
272 D(bug("[HiddGfx] %s(0x%p)\n", __func__, LIBBASE));
274 D(bug("[HiddGfx] %s: csd @ 0x%p\n", __func__, csd));
276 D(bug("[HiddGfx] %s: GC class @ 0x%p\n", __func__, csd->gcclass));
277 D(bug("[HiddGfx] %s: BitMap class @ 0x%p\n", __func__, csd->bitmapclass));
279 csd->cs_GfxBase = NULL;
280 NEWLIST(&csd->pflist);
282 if (!(csd->cs_UtilityBase = TaggedOpenLibrary(TAGGEDOPEN_UTILITY)))
284 D(bug("[HiddGfx] %s: failed to open utility.library\n", __func__));
285 ReturnInt("init_gfxhiddclass", ULONG, FALSE);
288 if (ObtainAttrBases(csd->attrBases, interfaces, NUM_ATTRBASES, OOPBase))
290 D(bug("[HiddGfx] %s: Failed to obtain class Attribute Bases\n", __func__));
291 ReturnInt("init_gfxhiddclass", ULONG, FALSE);
294 if (GetMethodBases(csd->methodBases, interfaces, NUM_METHODBASES, OOPBase))
296 D(bug("[HiddGfx] %s: Failed to obtain class Method Bases\n", __func__));
297 ReturnInt("init_gfxhiddclass", ULONG, FALSE);
300 InitSemaphore(&csd->sema);
301 InitSemaphore(&csd->pfsema);
302 InitSemaphore(&csd->rgbconvertfuncs_sem);
304 D(bug("[HiddGfx] %s: Creating std pixelfmts\n", __func__));
305 create_std_pixfmts(csd);
307 D(bug("[HiddGfx] %s: Prepairing Gfx HW Root\n", __func__));
308 hwroot = OOP_NewObject(NULL, CLID_HW_Root, NULL);
309 if ((hwroot) && (HW_AddDriver(hwroot, csd->gfxhwclass, NULL)))
312 bug("[HiddGfx] %s: Initialization complete\n", __func__);
313 bug("[HiddGfx] %s: Gfx HW Root @ 0x%p\n", __func__, csd->gfxhwinstance);
315 return TRUE;
318 ReturnInt("init_gfxhiddclass", BOOL, FALSE);
321 /****************************************************************************************/
323 static int GFX_ClassFree(LIBBASETYPEPTR LIBBASE)
325 struct class_static_data *csd = &LIBBASE->hdg_csd;
326 struct Library *OOPBase = csd->cs_OOPBase;
328 EnterFunc(bug("free_gfxhiddclass(csd=%p)\n", csd));
330 delete_pixfmts(csd);
331 ReleaseAttrBases(csd->attrBases, interfaces, NUM_ATTRBASES, OOPBase);
332 CloseLibrary(csd->cs_UtilityBase);
334 ReturnInt("free_gfxhiddclass", BOOL, TRUE);
337 /****************************************************************************************/
339 ADD2INITLIB(GFX_ClassInit, -2)
340 ADD2EXPUNGELIB(GFX_ClassFree, -2)