dont try to convert the pixfmt while calculating the "shade"
[AROS.git] / rom / graphics / allocbitmap.c
blob4064f9c2b16bc4046cdbd1482a892f5271245fcc
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[8];
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));
355 SET_TAG(bm_tags, 7, TAG_DONE, 0);
357 /* Allocate an extra planes for HIDD bitmap info */
358 nbm = AllocMem (sizeof (struct BitMap) + sizeof(PLANEPTR) * HIDD_BM_EXTRAPLANES, MEMF_ANY|MEMF_CLEAR);
359 D(bug("[AllocBitMap] Allocated bitmap structure: 0x%p\n", nbm));
361 if (nbm)
363 OOP_Object *bm_obj = NULL;
364 BOOL ok = TRUE;
366 SET_BM_TAG(bm_tags, 6, BMStruct, nbm);
368 /* Use the memory driver if we didn't get another object in any way */
369 if (!drv)
370 drv = (struct monitor_driverdata *)CDD(GfxBase);
372 if (alloc)
374 bm_obj = HIDD_Gfx_NewBitMap(drv->gfxhidd, bm_tags);
375 D(bug("[AllocBitMap] Created bitmap object 0x%p\n", bm_obj));
376 if (!bm_obj)
377 ok = FALSE;
380 if (ok)
382 ULONG align = 15;
383 HIDDT_ColorModel colmod = -1;
385 if (alloc)
387 OOP_Object *pf;
390 * It is possible that the HIDD had to allocate a larger depth than that supplied, so
391 * we should get back the correct depth.
392 * This is because layers.library might want to allocate offscreen bitmaps to
393 * store obscured areas, and those offscreen bitmaps should be of the same depth as
394 * onscreen ones.
396 sizex = OOP_GET(bm_obj, aHidd_BitMap_Width);
397 sizey = OOP_GET(bm_obj, aHidd_BitMap_Height);
398 depth = OOP_GET(bm_obj, aHidd_BitMap_Depth);
399 align = OOP_GET(bm_obj, aHidd_BitMap_Align) - 1;
401 OOP_GetAttr(bm_obj, aHidd_BitMap_PixFmt, (IPTR *)&pf);
402 OOP_GetAttr(pf, aHidd_PixFmt_ColorModel, &colmod);
404 D(bug("[AllocBitMap] Resulting HIDD bitmap: %ldx%ldx%ld (%ld px-aligned)\n", sizex, sizey, depth, align));
406 /* Store object and supplementary data in plane array */
407 HIDD_BM_OBJ(nbm) = bm_obj;
408 HIDD_BM_COLMOD(nbm) = colmod;
409 HIDD_BM_COLMAP(nbm) = (OOP_Object *)OOP_GET(bm_obj, aHidd_BitMap_ColorMap);
410 HIDD_BM_REALDEPTH(nbm) = depth;
412 else
414 /* There's nothing to clear if we don't allocate an object */
415 clear = FALSE;
418 HIDD_BM_DRVDATA(nbm) = drv;
419 HIDD_BM_HIDDMODE(nbm) = hiddmode;
421 nbm->Rows = sizey;
422 nbm->BytesPerRow = (((sizex + align) & ~align) >> 3);
423 #if BMDEPTH_COMPATIBILITY
424 nbm->Depth = (depth > 8) ? 8 : depth;
425 #else
426 nbm->Depth = depth;
427 #endif
428 /* TODO: Allow interleaved Amiga chipset bitmaps. */
429 nbm->Flags = (flags & ~BMF_INTERLEAVED) | BMF_SPECIALFMT;
431 /* If this is a displayable bitmap, create a color table for it */
432 if (bm_obj && (friend_bitmap ||
433 (flags & BMF_REQUESTVMEM) == BMF_REQUESTVMEM))
435 HIDD_BM_FLAGS(nbm) |= HIDD_BMF_SCREEN_BITMAP;
437 if (friend_bitmap && IS_HIDD_BM(friend_bitmap))
439 /* We got a friend_bitmap bitmap. We inherit its
440 colormap.
441 !!! NOTE !!! If this is used after the
442 friend_bitmap bitmap is freed it means trouble,
443 as the colortab mem will no longer be valid */
444 OOP_Object *oldcolmap;
446 oldcolmap = HIDD_BM_SetColorMap(bm_obj, HIDD_BM_COLMAP(friend_bitmap));
447 if (oldcolmap)
448 OOP_DisposeObject(oldcolmap);
450 HIDD_BM_COLMAP(nbm) = HIDD_BM_COLMAP(friend_bitmap);
451 HIDD_BM_PIXTAB(nbm) = HIDD_BM_PIXTAB(friend_bitmap);
452 HIDD_BM_COLMOD(nbm) = HIDD_BM_COLMOD(friend_bitmap);
453 HIDD_BM_REALDEPTH(nbm) = HIDD_BM_REALDEPTH(friend_bitmap);
455 HIDD_BM_FLAGS(nbm) |= HIDD_BMF_SHARED_PIXTAB;
457 else
459 /* Allocate a pixtab */
460 HIDD_BM_PIXTAB(nbm) = AllocVec(sizeof (HIDDT_Pixel) * AROS_PALETTE_SIZE, MEMF_ANY);
462 if (HIDD_BM_PIXTAB(nbm))
464 /* Set this palette to all black by default */
466 HIDDT_Color col;
467 ULONG i;
469 col.red = 0;
470 col.green = 0;
471 col.blue = 0;
472 col.alpha = 0;
474 if (vHidd_ColorModel_Palette == colmod || vHidd_ColorModel_TrueColor == colmod)
476 ULONG numcolors;
478 numcolors = 1L << ((depth <= 8) ? depth : 8);
480 /* Set palette to all black */
481 for (i = 0; i < numcolors; i ++)
483 HIDD_BM_SetColors(bm_obj, &col, i, 1);
484 HIDD_BM_PIXTAB(nbm)[i] = col.pixval;
487 } /* if (pixtab successfully allocated) */
488 else
489 ok = FALSE;
493 if (ok)
495 struct BitMap *pbm = NULL;
497 if (clear)
498 BltBitMap(nbm, 0, 0, nbm, 0, 0, sizex, sizey, 0x00, 0xFF, NULL);
500 /* Is there a planar memory map? */
501 OOP_GetAttr(bm_obj, aHidd_PlanarBM_BitMap, (IPTR *)&pbm);
502 if (pbm) {
503 int i;
504 for (i = 0; i < 8; i++)
505 nbm->Planes[i] = pbm->Planes[i];
506 /* Mark this as a 'standard' bitmap */
507 nbm->Flags &= ~BMF_SPECIALFMT;
508 nbm->Flags |= BMF_STANDARD | (flags & BMF_DISPLAYABLE);
511 /* Mark this is a HIDD bitmap via the pad field */
512 nbm->pad = HIDD_BM_PAD_MAGIC;
514 ReturnPtr("AllocBitMap", struct BitMap *, nbm);
517 HIDD_Gfx_DisposeBitMap(drv->gfxhidd, bm_obj);
518 } /* if (bitmap object allocated) */
520 FreeMem(nbm, sizeof (struct BitMap));
521 nbm = NULL;
523 } /* if (nbm) */
526 else /* Otherwise init a plain Amiga bitmap. TODO: BMF_INTERLEAVED support */
528 nbm = AllocMem (sizeof(struct BitMap) + ((depth > 8) ? (depth - 8) * sizeof(PLANEPTR) : 0),
529 MEMF_ANY | MEMF_CLEAR);
531 if (nbm)
533 nbm->BytesPerRow = ((sizex + 15) >> 4) * 2;
534 nbm->Rows = sizey;
535 nbm->Flags = flags | BMF_STANDARD;
536 nbm->Depth = depth;
537 nbm->pad = 0;
539 if (alloc)
541 ULONG plane;
543 for (plane=0; plane<depth; plane++)
545 nbm->Planes[plane] = AllocRaster (sizex, sizey);
547 if (!nbm->Planes[plane])
548 break;
550 if (clear)
551 memset (nbm->Planes[plane], 0, RASSIZE(sizex,sizey));
554 if (plane != depth)
556 for (plane=0; plane<depth; plane++)
557 if (nbm->Planes[plane])
558 FreeRaster (nbm->Planes[plane], sizex, sizey);
560 FreeMem (nbm, sizeof (struct BitMap));
562 nbm = NULL;
566 ReturnPtr("AllocBitMap", struct BitMap *, nbm);
569 return nbm;
571 AROS_LIBFUNC_EXIT
572 } /* AllocBitMap */