add option for classic magicmenu "mixed" menu style (pull donw on screen title, pop...
[AROS.git] / rom / graphics / allocbitmap.c
blobeec7aed995ab492b9faff4fe7d1edecde7f9300c
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Create a new BitMap
6 Lang: english
7 */
9 #include <aros/debug.h>
10 #include <string.h>
11 #include <exec/memory.h>
12 #include <utility/tagitem.h>
13 #include <proto/exec.h>
14 #include <proto/oop.h>
15 #include <proto/utility.h>
16 #include <cybergraphx/cybergraphics.h>
18 #include "graphics_intern.h"
19 #include "gfxfuncsupport.h"
20 #include "dispinfo.h"
21 #include "fakegfxhidd.h"
23 #define SET_TAG(tags, idx, tag, val) \
24 tags[idx].ti_Tag = tag ; tags[idx].ti_Data = (IPTR)val;
26 #define SET_BM_TAG(tags, idx, tag, val) \
27 SET_TAG(tags, idx, aHidd_BitMap_ ## tag, val)
29 static HIDDT_StdPixFmt const cyber2hidd_pixfmt[] =
31 vHidd_StdPixFmt_LUT8,
32 vHidd_StdPixFmt_RGB15,
33 vHidd_StdPixFmt_BGR15,
34 vHidd_StdPixFmt_RGB15_LE,
35 vHidd_StdPixFmt_BGR15_LE,
36 vHidd_StdPixFmt_RGB16,
37 vHidd_StdPixFmt_BGR16,
38 vHidd_StdPixFmt_RGB16_LE,
39 vHidd_StdPixFmt_BGR16_LE,
40 vHidd_StdPixFmt_RGB24,
41 vHidd_StdPixFmt_BGR24,
42 vHidd_StdPixFmt_ARGB32,
43 vHidd_StdPixFmt_BGRA32,
44 vHidd_StdPixFmt_RGBA32
47 /*****************************************************************************
49 NAME */
50 #include <graphics/gfx.h>
51 #include <proto/graphics.h>
53 AROS_LH5(struct BitMap *, AllocBitMap,
55 /* SYNOPSIS */
56 AROS_LHA(UWORD , sizex, D0),
57 AROS_LHA(UWORD , sizey, D1),
58 AROS_LHA(ULONG , depth, D2),
59 AROS_LHA(ULONG , flags, D3),
60 AROS_LHA(struct BitMap *, friend_bitmap, A0),
62 /* LOCATION */
63 struct GfxBase *, GfxBase, 153, Graphics)
65 /* FUNCTION
66 Allocates and initializes a bitmap structure. Allocates and
67 initializes bitplane data, and sets the bitmap's planes to point to
68 it.
70 INPUTS
71 sizex, sizey - The width and height in pixels
73 depth - The depth of the bitmap. A depth of 1 will allocate a
74 bitmap for two colors, a depth of 24 will allocate a bitmap for
75 16 million colors. Pixels with AT LEAST this many bits will be
76 allocated.
78 flags - One of these flags:
80 BMF_CLEAR: Fill the bitmap with color 0.
82 BMF_DISPLAYABLE: to specify that this bitmap data should
83 be allocated in such a manner that it can be displayed.
84 Displayable data has more severe alignment restrictions
85 than non-displayable data in some systems.
86 Note that it may be not enough to specify only this flag
87 to make the bitmap really displayable. See "INTERNALS"
88 section below.
90 BMF_INTERLEAVED: tells graphics that you would like your
91 bitmap to be allocated with one large chunk of display
92 memory for all bitplanes. This minimizes color flashing on
93 deep displays. If there is not enough contiguous RAM for an
94 interleaved bitmap, graphics.library will fall back to a
95 non-interleaved one.
97 BMF_MINPLANES: causes graphics to only allocate enough
98 space in the bitmap structure for "depth" plane pointers.
99 This is for system use and should not be used by
100 applications use as it is inefficient, and may waste
101 memory.
103 BMF_SPECIALFMT: causes graphics to allocate a bitmap
104 of a standard CyberGraphX format. The format
105 (PIXFMT_????) must be stored in the 8 most significant bits.
107 BMF_RTGTAGS,
108 BMF_RTGCHECK,
109 BMF_FRIENDSTAG: Setting these flags to 1's while BMF_SPECIALFMT
110 and BMF_INVALID are set to 0 means that friend_bitmap
111 points to a taglist instead of BitMap structure.
113 friend_bitmap - pointer to another bitmap, or NULL. If this pointer
114 is passed, then the bitmap data will be allocated in
115 the most efficient form for blitting to friend_bitmap.
117 This pointer can also point to a TagList, if specified by flags.
118 In this case it may contain the following tags:
120 - BMATags_Friend (struct BitMap *)
121 An actual pointer to friend bitmap. Defaults to NULL.
123 - BMATags_Depth (ULONG)
124 Depth of the bitmap to create. Defaults to depth argument
125 of AllocBitMap().
127 - BMATags_Clear (BOOL)
128 Tells if the newly created bitmap should be explicitly
129 cleared. Defaults to the value of BMF_CLEAR flag in
130 AllocBitMap() arguments.
132 - BMATags_Displayable (BOOL)
133 Tells if the bitmap should be displayable by the hardware.
134 Defaults to the value of BMF_DISPLAYABLE flag in AllocBitMap()
135 arguments.
137 - BMATags_NoMemory (BOOL)
138 Tells AllocBitMap() not to allocate actual bitmap storage. Only
139 header is allocated and set up. Default value is FALSE.
141 - BMATags_DisplayID (ULONG)
142 Allocate a displayable bitmap for specified display mode.
144 RESULT
145 A pointer to the new bitmap.
147 NOTES
148 When allocating using a friend_bitmap bitmap, it is not safe to assume
149 anything about the structure of the bitmap data if that friend_bitmap
150 BitMap might not be a standard Amiga bitmap (for instance, if the
151 workbench is running on a non-Amiga display device, its
152 Screen->RastPort->BitMap won't be in standard Amiga format. The
153 only safe operations to perform on a non-standard BitMap are:
155 blitting it to another bitmap, which must be either a
156 standard Amiga bitmap, or a friend_bitmap of this bitmap.
158 blitting from this bitmap to a friend_bitmap bitmap or to a
159 standard Amiga bitmap.
161 attaching it to a rastport and making rendering calls.
163 Good arguments to pass for the friend_bitmap are your window's
164 RPort->BitMap, and your screen's RastPort->BitMap. Do NOT pass
165 &(screenptr->BitMap)!
167 BitMaps not allocated with BMF_DISPLAYABLE may not be used as
168 Intuition Custom BitMaps or as RasInfo->BitMaps. They may be
169 blitted to a BMF_DISPLAYABLE BitMap, using one of the BltBitMap()
170 family of functions.
172 When allocating a displayable bitmap, make sure that its size is
173 within limits allowed by the display driver. Use GetDisplayInfoData()
174 with DTAG_DIMS in order to obtain the needed information.
176 EXAMPLE
178 BUGS
180 SEE ALSO
181 FreeBitMap()
183 INTERNALS
184 In order to allocate a displayable bitmap, you need to pass
185 BMF_MINPLANES in addition to BMF_DISPLAYABLE flag. This is
186 standard CGX convention. You may use BMF_REQUESTVMEM definition
187 for this.
189 *****************************************************************************/
191 AROS_LIBFUNC_INIT
193 struct BitMap *nbm;
194 HIDDT_ModeID hiddmode = vHidd_ModeID_Invalid;
195 struct DisplayInfoHandle *dh;
196 struct monitor_driverdata *drv = NULL;
197 ULONG clear = flags & BMF_CLEAR;
198 BOOL alloc = TRUE;
200 if (BITMAPFLAGS_ARE_EXTENDED(flags))
202 struct TagItem *tag, *tstate = (struct TagItem *)friend_bitmap;
204 friend_bitmap = NULL;
206 while ((tag = NextTagItem(&tstate)))
208 switch (tag->ti_Tag) {
209 case BMATags_Friend:
210 friend_bitmap = (struct BitMap *)tag->ti_Data;
211 break;
213 case BMATags_Depth:
214 depth = tag->ti_Data;
215 break;
217 case BMATags_Clear:
218 clear = tag->ti_Data;
219 break;
221 case BMATags_NoMemory:
222 alloc = !tag->ti_Data;
223 break;
225 case BMATags_DisplayID:
226 dh = FindDisplayInfo(tag->ti_Data);
227 if (!dh)
228 return NULL;
230 drv = dh->drv;
231 hiddmode = dh->id;
232 flags |= BMF_REQUESTVMEM;
234 break;
239 ASSERT_VALID_PTR_OR_NULL(friend_bitmap);
240 D(bug("AllocBitMap(%u, %u, %u, 0x%08lX)\n", sizex, sizey, depth, flags));
241 D(bug("[AllocBitMap] ModeID: 0x%08lX, friend_bitmap: 0x%p\n", hiddmode, friend_bitmap));
244 If the depth is too large or the bitmap should be displayable or
245 there is a friend_bitmap bitmap and that's not a normal bitmap, then
246 call the RTG driver.
248 if ((depth > 8) || (hiddmode != vHidd_ModeID_Invalid) ||
249 (friend_bitmap && (friend_bitmap->Flags & BMF_SPECIALFMT)) ||
250 (flags & BMF_SPECIALFMT) ||
251 (flags & BMF_DISPLAYABLE))
253 struct TagItem bm_tags[7];
254 HIDDT_StdPixFmt stdpf = vHidd_StdPixFmt_Unknown;
256 D(bug("[AllocBitMap] Allocating HIDD bitmap\n"));
258 /* Set size */
259 SET_BM_TAG( bm_tags, 0, Width, sizex );
260 SET_BM_TAG( bm_tags, 1, Height, sizey );
262 /* Set friend bitmap */
263 SET_TAG(bm_tags, 3, TAG_IGNORE, 0);
264 if (friend_bitmap && IS_HIDD_BM(friend_bitmap))
266 OOP_Object *friend_obj = HIDD_BM_OBJ(friend_bitmap);
268 D(bug("[AllocBitMap] Setting friend bitmap 0x%p, object 0x%p\n", friend_bitmap, friend_obj));
271 * Friend bitmap may hold a fakegfx bitmap object.
272 * fakegfx is our proxy layer on top of graphics drivers, providing
273 * software mouse pointer implementation. Graphics drivers do (and must)
274 * not know about fakegfx, so we need to de-masquerade such objects.
276 if (OOP_OCLASS(friend_obj) == CDD(GfxBase)->fakefbclass)
278 OOP_GetAttr(friend_obj, aHidd_FakeFB_RealBitMap, (IPTR *)&friend_obj);
279 D(bug("[AllocBitMap] Fakefb friend de-masqueraded to 0x%p\n", friend_obj));
282 SET_BM_TAG(bm_tags, 3, Friend, friend_obj);
285 * If we have no ModeID specified, obtain it from friend.
286 * This may assist some display drivers (especially hosted ones) in bitmap class selection.
287 * They may want to use host OS bitmap objects for non-displayable bitmaps which are friends
288 * of displayable ones. In this case they may simply check against ModeID != vHidd_ModeID_Invalid,
289 * and don't have to check friend object by themselves.
291 if (hiddmode == vHidd_ModeID_Invalid)
292 hiddmode = HIDD_BM_HIDDMODE(friend_bitmap);
294 if (depth <= 8) /* CHECKME: only set depth if planar? */
295 depth = HIDD_BM_REALDEPTH(friend_bitmap);
297 /* Obtain also GFX driver from friend bitmap */
298 drv = HIDD_BM_DRVDATA(friend_bitmap);
301 SET_BM_TAG( bm_tags, 2, Depth, depth );
303 /* Now let's deal with pixelformat */
304 if (flags & BMF_SPECIALFMT)
306 ULONG cgxpf = DOWNSHIFT_PIXFMT(flags);
308 if (cgxpf <= PIXFMT_RGBA32)
309 stdpf = cyber2hidd_pixfmt[cgxpf];
311 else if ((!friend_bitmap) && (hiddmode == vHidd_ModeID_Invalid))
313 /* If there is neither pixelformat nor friend bitmap nor ModeID specified,
314 we have to use some default pixelformat depending on the depth */
315 if (depth > 24)
316 stdpf = vHidd_StdPixFmt_ARGB32;
317 else if (depth > 16)
318 stdpf = vHidd_StdPixFmt_RGB24;
319 else if (depth > 15)
320 stdpf = vHidd_StdPixFmt_RGB16;
321 else if (depth > 8)
322 stdpf = vHidd_StdPixFmt_RGB15;
323 else
324 stdpf = vHidd_StdPixFmt_LUT8;
326 /* If we have a friend bitmap, pixelformat will be
327 picked up from it */
329 if (stdpf != vHidd_StdPixFmt_Unknown)
331 D(bug("[AllocBitMap] Setting pixelformat to %d\n", stdpf));
332 SET_BM_TAG(bm_tags, 4, StdPixFmt, stdpf);
333 hiddmode = vHidd_ModeID_Invalid;
335 else if (hiddmode != vHidd_ModeID_Invalid)
337 D(bug("[AllocBitMap] Setting ModeID to 0x%08lX\n", hiddmode));
338 SET_BM_TAG(bm_tags, 4, ModeID, hiddmode);
340 else
343 * SET_TAG() is TWO operators, so we absolutely need parenthesis here.
344 * Remember this!
346 SET_TAG(bm_tags, 4, TAG_IGNORE, 0);
347 hiddmode = vHidd_ModeID_Invalid;
350 /* Set Displayable attribute */
351 SET_BM_TAG(bm_tags, 5, Displayable, ((flags & BMF_REQUESTVMEM) == BMF_REQUESTVMEM) || ((flags & BMF_DISPLAYABLE) == BMF_DISPLAYABLE));
352 D(bug("[AllocBitMap] Displayable: %d\n", bm_tags[5].ti_Data));
354 SET_TAG(bm_tags, 6, TAG_DONE, 0);
356 /* Allocate an extra planes for HIDD bitmap info */
357 nbm = AllocMem (sizeof (struct BitMap) + sizeof(PLANEPTR) * HIDD_BM_EXTRAPLANES, MEMF_ANY|MEMF_CLEAR);
358 D(bug("[AllocBitMap] Allocated bitmap structure: 0x%p\n", nbm));
360 if (nbm)
362 OOP_Object *bm_obj = NULL;
363 BOOL ok = TRUE;
365 /* Use the memory driver if we didn't get another object in any way */
366 if (!drv)
367 drv = (struct monitor_driverdata *)CDD(GfxBase);
369 if (alloc)
371 bm_obj = HIDD_Gfx_NewBitMap(drv->gfxhidd, bm_tags);
372 D(bug("[AllocBitMap] Created bitmap object 0x%p\n", bm_obj));
373 if (!bm_obj)
374 ok = FALSE;
377 if (ok)
379 ULONG align = 15;
380 HIDDT_ColorModel colmod = -1;
382 if (alloc)
384 OOP_Object *pf;
387 * It is possible that the HIDD had to allocate a larger depth than that supplied, so
388 * we should get back the correct depth.
389 * This is because layers.library might want to allocate offscreen bitmaps to
390 * store obscured areas, and those offscreen bitmaps should be of the same depth as
391 * onscreen ones.
393 sizex = OOP_GET(bm_obj, aHidd_BitMap_Width);
394 sizey = OOP_GET(bm_obj, aHidd_BitMap_Height);
395 depth = OOP_GET(bm_obj, aHidd_BitMap_Depth);
396 align = OOP_GET(bm_obj, aHidd_BitMap_Align) - 1;
398 OOP_GetAttr(bm_obj, aHidd_BitMap_PixFmt, (IPTR *)&pf);
399 OOP_GetAttr(pf, aHidd_PixFmt_ColorModel, &colmod);
401 D(bug("[AllocBitMap] Resulting HIDD bitmap: %ldx%ldx%ld (%ld px-aligned)\n", sizex, sizey, depth, align));
403 /* Store object and supplementary data in plane array */
404 HIDD_BM_OBJ(nbm) = bm_obj;
405 HIDD_BM_COLMOD(nbm) = colmod;
406 HIDD_BM_COLMAP(nbm) = (OOP_Object *)OOP_GET(bm_obj, aHidd_BitMap_ColorMap);
407 HIDD_BM_REALDEPTH(nbm) = depth;
409 else
411 /* There's nothing to clear if we don't allocate an object */
412 clear = FALSE;
415 HIDD_BM_DRVDATA(nbm) = drv;
416 HIDD_BM_HIDDMODE(nbm) = hiddmode;
418 nbm->Rows = sizey;
419 nbm->BytesPerRow = (((sizex + align) & ~align) >> 3);
420 #if BMDEPTH_COMPATIBILITY
421 nbm->Depth = (depth > 8) ? 8 : depth;
422 #else
423 nbm->Depth = depth;
424 #endif
425 /* TODO: Allow interleaved Amiga chipset bitmaps. */
426 nbm->Flags = (flags & ~BMF_INTERLEAVED) | BMF_SPECIALFMT;
428 /* If this is a displayable bitmap, create a color table for it */
429 if (bm_obj && (friend_bitmap ||
430 (flags & BMF_REQUESTVMEM) == BMF_REQUESTVMEM))
432 HIDD_BM_FLAGS(nbm) |= HIDD_BMF_SCREEN_BITMAP;
434 if (friend_bitmap && IS_HIDD_BM(friend_bitmap))
436 /* We got a friend_bitmap bitmap. We inherit its
437 colormap.
438 !!! NOTE !!! If this is used after the
439 friend_bitmap bitmap is freed it means trouble,
440 as the colortab mem will no longer be valid */
441 OOP_Object *oldcolmap;
443 oldcolmap = HIDD_BM_SetColorMap(bm_obj, HIDD_BM_COLMAP(friend_bitmap));
444 if (oldcolmap)
445 OOP_DisposeObject(oldcolmap);
447 HIDD_BM_COLMAP(nbm) = HIDD_BM_COLMAP(friend_bitmap);
448 HIDD_BM_PIXTAB(nbm) = HIDD_BM_PIXTAB(friend_bitmap);
449 HIDD_BM_COLMOD(nbm) = HIDD_BM_COLMOD(friend_bitmap);
450 HIDD_BM_REALDEPTH(nbm) = HIDD_BM_REALDEPTH(friend_bitmap);
452 HIDD_BM_FLAGS(nbm) |= HIDD_BMF_SHARED_PIXTAB;
454 else
456 /* Allocate a pixtab */
457 HIDD_BM_PIXTAB(nbm) = AllocVec(sizeof (HIDDT_Pixel) * AROS_PALETTE_SIZE, MEMF_ANY);
459 if (HIDD_BM_PIXTAB(nbm))
461 /* Set this palette to all black by default */
463 HIDDT_Color col;
464 ULONG i;
466 col.red = 0;
467 col.green = 0;
468 col.blue = 0;
469 col.alpha = 0;
471 if (vHidd_ColorModel_Palette == colmod || vHidd_ColorModel_TrueColor == colmod)
473 ULONG numcolors;
475 numcolors = 1L << ((depth <= 8) ? depth : 8);
477 /* Set palette to all black */
478 for (i = 0; i < numcolors; i ++)
480 HIDD_BM_SetColors(bm_obj, &col, i, 1);
481 HIDD_BM_PIXTAB(nbm)[i] = col.pixval;
484 } /* if (pixtab successfully allocated) */
485 else
486 ok = FALSE;
490 if (ok)
492 struct BitMap *pbm = NULL;
494 if (clear)
495 BltBitMap(nbm, 0, 0, nbm, 0, 0, sizex, sizey, 0x00, 0xFF, NULL);
497 /* Is there a planar memory map? */
498 OOP_GetAttr(bm_obj, aHidd_PlanarBM_BitMap, (IPTR *)&pbm);
499 if (pbm) {
500 int i;
501 for (i = 0; i < 8; i++)
502 nbm->Planes[i] = pbm->Planes[i];
503 /* Mark this as a 'standard' bitmap */
504 nbm->Flags &= ~BMF_SPECIALFMT;
505 nbm->Flags |= BMF_STANDARD | (flags & BMF_DISPLAYABLE);
508 /* Mark this is a HIDD bitmap via the pad field */
509 nbm->pad = HIDD_BM_PAD_MAGIC;
511 ReturnPtr("AllocBitMap", struct BitMap *, nbm);
514 HIDD_Gfx_DisposeBitMap(drv->gfxhidd, bm_obj);
515 } /* if (bitmap object allocated) */
517 FreeMem(nbm, sizeof (struct BitMap));
518 nbm = NULL;
520 } /* if (nbm) */
523 else /* Otherwise init a plain Amiga bitmap. TODO: BMF_INTERLEAVED support */
525 nbm = AllocMem (sizeof(struct BitMap) + ((depth > 8) ? (depth - 8) * sizeof(PLANEPTR) : 0),
526 MEMF_ANY | MEMF_CLEAR);
528 if (nbm)
530 nbm->BytesPerRow = ((sizex + 15) >> 4) * 2;
531 nbm->Rows = sizey;
532 nbm->Flags = flags | BMF_STANDARD;
533 nbm->Depth = depth;
534 nbm->pad = 0;
536 if (alloc)
538 ULONG plane;
540 for (plane=0; plane<depth; plane++)
542 nbm->Planes[plane] = AllocRaster (sizex, sizey);
544 if (!nbm->Planes[plane])
545 break;
547 if (clear)
548 memset (nbm->Planes[plane], 0, RASSIZE(sizex,sizey));
551 if (plane != depth)
553 for (plane=0; plane<depth; plane++)
554 if (nbm->Planes[plane])
555 FreeRaster (nbm->Planes[plane], sizex, sizey);
557 FreeMem (nbm, sizeof (struct BitMap));
559 nbm = NULL;
563 ReturnPtr("AllocBitMap", struct BitMap *, nbm);
566 return nbm;
568 AROS_LIBFUNC_EXIT
569 } /* AllocBitMap */