Fix IO memory access .. SB128 driver makes noises in VMWare - CMI is untested (Curren...
[AROS.git] / rom / graphics / allocbitmap.c
blobbe14cf0ccc11ef123a37c4f35142c4548265dc99
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(ULONG , sizex, D0),
57 AROS_LHA(ULONG , 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 const struct TagItem *tstate = (const struct TagItem *)friend_bitmap;
203 struct TagItem *tag;
205 friend_bitmap = NULL;
207 while ((tag = NextTagItem(&tstate)))
209 switch (tag->ti_Tag) {
210 case BMATags_Friend:
211 friend_bitmap = (struct BitMap *)tag->ti_Data;
212 break;
214 case BMATags_Depth:
215 depth = tag->ti_Data;
216 break;
218 case BMATags_Clear:
219 clear = tag->ti_Data;
220 break;
222 case BMATags_NoMemory:
223 alloc = !tag->ti_Data;
224 break;
226 case BMATags_DisplayID:
227 dh = FindDisplayInfo(tag->ti_Data);
228 if (!dh)
229 return NULL;
231 drv = dh->drv;
232 hiddmode = dh->id;
233 flags |= BMF_REQUESTVMEM;
235 break;
240 ASSERT_VALID_PTR_OR_NULL(friend_bitmap);
241 D(bug("AllocBitMap(%u, %u, %u, 0x%08lX)\n", sizex, sizey, depth, flags));
242 D(bug("[AllocBitMap] ModeID: 0x%08lX, friend_bitmap: 0x%p\n", hiddmode, friend_bitmap));
245 If the depth is too large or the bitmap should be displayable or
246 there is a friend_bitmap bitmap and that's not a normal bitmap, then
247 call the RTG driver.
249 if ((depth > 8) || (hiddmode != vHidd_ModeID_Invalid) ||
250 (friend_bitmap && (friend_bitmap->Flags & BMF_SPECIALFMT)) ||
251 (flags & BMF_SPECIALFMT))
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));
352 D(bug("[AllocBitMap] Displayable: %d\n", bm_tags[5].ti_Data));
354 SET_TAG(bm_tags, 6, TAG_DONE, 0);
356 nbm = AllocMem (sizeof (struct BitMap), MEMF_ANY|MEMF_CLEAR);
357 D(bug("[AllocBitMap] Allocated bitmap structure: 0x%p\n", nbm));
359 if (nbm)
361 OOP_Object *bm_obj = NULL;
362 BOOL ok = TRUE;
364 /* Use the memory driver if we didn't get another object in any way */
365 if (!drv)
366 drv = (struct monitor_driverdata *)CDD(GfxBase);
368 if (alloc)
370 bm_obj = HIDD_Gfx_NewBitMap(drv->gfxhidd, bm_tags);
371 D(bug("[AllocBitMap] Created bitmap object 0x%p\n", bm_obj));
372 if (!bm_obj)
373 ok = FALSE;
376 if (ok)
378 IPTR width = sizex;
379 IPTR height = sizey;
380 HIDDT_ColorModel colmod = -1;
382 if (alloc)
384 OOP_Object *pf;
385 OOP_Object *colmap = NULL;
386 IPTR bmdepth;
388 OOP_GetAttr(bm_obj, aHidd_BitMap_Width, &width);
389 OOP_GetAttr(bm_obj, aHidd_BitMap_Height, &height);
390 OOP_GetAttr(bm_obj, aHidd_BitMap_Depth, &bmdepth);
391 OOP_GetAttr(bm_obj, aHidd_BitMap_PixFmt, (IPTR *)&pf);
392 OOP_GetAttr(bm_obj, aHidd_BitMap_ColorMap, (IPTR *)&colmap);
394 OOP_GetAttr(pf, aHidd_PixFmt_ColorModel, &colmod);
396 D(bug("[AllocBitMap] Resulting HIDD bitmap: %ldx%ldx%ld\n", width, height, bmdepth));
399 * It is possible that the HIDD had to allocate a larger depth than that supplied, so
400 * we should get back the correct depth.
401 * This is because layers.library might want to allocate offscreen bitmaps to
402 * store obscured areas, and those offscreen bitmaps should be of the same depth as
403 * onscreen ones.
405 depth = bmdepth;
407 /* Store object and supplementary data in plane array */
408 HIDD_BM_OBJ(nbm) = bm_obj;
409 HIDD_BM_COLMOD(nbm) = colmod;
410 HIDD_BM_COLMAP(nbm) = colmap;
411 HIDD_BM_REALDEPTH(nbm) = depth;
413 else
414 /* There's nothing to clear if we don't allocate an object */
415 clear = FALSE;
417 HIDD_BM_DRVDATA(nbm) = drv;
418 HIDD_BM_HIDDMODE(nbm) = hiddmode;
420 nbm->Rows = height;
421 nbm->BytesPerRow = WIDTH_TO_BYTES(width);
422 #if BMDEPTH_COMPATIBILITY
423 nbm->Depth = (depth > 8) ? 8 : depth;
424 #else
425 nbm->Depth = depth;
426 #endif
427 nbm->Flags = flags | BMF_SPECIALFMT;
429 /* If this is a displayable bitmap, create a color table for it */
430 if (alloc && (friend_bitmap ||
431 (flags & BMF_REQUESTVMEM) == BMF_REQUESTVMEM))
433 HIDD_BM_FLAGS(nbm) |= HIDD_BMF_SCREEN_BITMAP;
435 if (friend_bitmap && IS_HIDD_BM(friend_bitmap))
437 /* We got a friend_bitmap bitmap. We inherit its
438 colormap.
439 !!! NOTE !!! If this is used after the
440 friend_bitmap bitmap is freed it means trouble,
441 as the colortab mem will no longer be valid */
442 OOP_Object *oldcolmap;
444 oldcolmap = HIDD_BM_SetColorMap(HIDD_BM_OBJ(nbm), HIDD_BM_COLMAP(friend_bitmap));
445 if (oldcolmap)
446 OOP_DisposeObject(oldcolmap);
448 HIDD_BM_COLMAP(nbm) = HIDD_BM_COLMAP(friend_bitmap);
449 HIDD_BM_PIXTAB(nbm) = HIDD_BM_PIXTAB(friend_bitmap);
450 HIDD_BM_COLMOD(nbm) = HIDD_BM_COLMOD(friend_bitmap);
451 HIDD_BM_REALDEPTH(nbm) = HIDD_BM_REALDEPTH(friend_bitmap);
453 HIDD_BM_FLAGS(nbm) |= HIDD_BMF_SHARED_PIXTAB;
455 else
457 /* Allocate a pixtab */
458 HIDD_BM_PIXTAB(nbm) = AllocVec(sizeof (HIDDT_Pixel) * AROS_PALETTE_SIZE, MEMF_ANY);
460 if (HIDD_BM_PIXTAB(nbm))
462 /* Set this palette to all black by default */
464 HIDDT_Color col;
465 ULONG i;
467 col.red = 0;
468 col.green = 0;
469 col.blue = 0;
470 col.alpha = 0;
472 if (vHidd_ColorModel_Palette == colmod || vHidd_ColorModel_TrueColor == colmod)
474 ULONG numcolors;
476 numcolors = 1L << ((depth <= 8) ? depth : 8);
478 /* Set palette to all black */
479 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 if (clear)
493 BltBitMap(nbm, 0, 0, nbm, 0, 0, width, height, 0x00, 0xFF, NULL);
494 ReturnPtr("AllocBitMap", struct BitMap *, nbm);
497 HIDD_Gfx_DisposeBitMap(drv->gfxhidd, bm_obj);
498 } /* if (bitmap object allocated) */
500 FreeMem(nbm, sizeof (struct BitMap));
501 nbm = NULL;
503 } /* if (nbm) */
506 else /* Otherwise init a plain Amiga bitmap */
508 nbm = AllocMem (sizeof(struct BitMap) + ((depth > 8) ? (depth - 8) * sizeof(PLANEPTR) : 0),
509 MEMF_ANY | MEMF_CLEAR);
511 if (nbm)
513 nbm->BytesPerRow = ((sizex + 15) >> 4) * 2;
514 nbm->Rows = sizey;
515 nbm->Flags = flags | BMF_STANDARD;
516 nbm->Depth = depth;
517 nbm->pad = 0;
519 if (alloc)
521 ULONG plane;
523 for (plane=0; plane<depth; plane++)
525 nbm->Planes[plane] = AllocRaster (sizex, sizey);
527 if (!nbm->Planes[plane])
528 break;
530 if (clear)
531 memset (nbm->Planes[plane], 0, RASSIZE(sizex,sizey));
534 if (plane != depth)
536 for (plane=0; plane<depth; plane++)
537 if (nbm->Planes[plane])
538 FreeRaster (nbm->Planes[plane], sizex, sizey);
540 FreeMem (nbm, sizeof (struct BitMap));
542 nbm = NULL;
548 return nbm;
550 AROS_LIBFUNC_EXIT
551 } /* AllocBitMap */