The Pedigree Project  0.1
system/include/pedigree/kernel/graphics/Graphics.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef _GRAPHICS_H
21 #define _GRAPHICS_H
22 
23 #include "pedigree/kernel/compiler.h"
24 #include "pedigree/kernel/processor/types.h"
25 
26 class Framebuffer;
27 
28 namespace Graphics
29 {
31 inline uint32_t createRgb(uint32_t r, uint32_t g, uint32_t b)
32 {
33  return (r << 16) | (g << 8) | b;
34 }
35 
36 enum PixelFormat
37 {
38  Bits32_Argb, // Alpha + RGB, with alpha in the highest byte
39  Bits32_Rgba, // RGB + alpha, with alpha in the lowest byte
40  Bits32_Rgb, // RGB, no alpha, essentially the same as above
41  Bits32_Bgr, // BGR, no alpha
42 
43  Bits24_Rgb, // RGB in a 24-bit pack
44  Bits24_Bgr, // R and B bytes swapped
45 
46  Bits16_Argb, // 4:4:4:4 ARGB, alpha most significant nibble
47  Bits16_Rgb565, // 5:6:5 RGB
48  Bits16_Rgb555, // 5:5:5 RGB
49 
50  Bits8_Idx, // Index into a palette
51  Bits8_Rgb332, // Intensity values
52 };
53 
54 inline size_t bitsPerPixel(PixelFormat format)
55 {
56  switch (format)
57  {
58  case Bits32_Argb:
59  case Bits32_Rgba:
60  case Bits32_Rgb:
61  case Bits32_Bgr:
62  return 32;
63  case Bits24_Rgb:
64  case Bits24_Bgr:
65  return 24;
66  case Bits16_Argb:
67  case Bits16_Rgb565:
68  case Bits16_Rgb555:
69  return 16;
70  case Bits8_Idx:
71  case Bits8_Rgb332:
72  return 8;
73  }
74 
75  return 0;
76 }
77 
78 inline size_t bytesPerPixel(PixelFormat format)
79 {
80  return bitsPerPixel(format) / 8;
81 }
82 
83 struct Buffer
84 {
86  uintptr_t base;
87 
89  size_t width;
90 
92  size_t height;
93 
96  PixelFormat format;
97 
99  size_t bytesPerPixel;
100 
102  size_t bufferId;
103 
106  void *pBacking;
107 };
108 
109 inline bool convertPixel(
110  uint32_t source, PixelFormat srcFormat, uint32_t &dest,
111  PixelFormat destFormat)
112 {
113  if ((srcFormat == destFormat) || (!source))
114  {
115  dest = source;
116  return true;
117  }
118 
119  // Amount of red/green/blue, in 8-bit intensity values
120  uint8_t amtRed = 0, amtGreen = 0, amtBlue = 0, amtAlpha = 0;
121 
122  // Unpack the pixel as necessary
123  if ((srcFormat == Bits32_Argb) || (srcFormat == Bits32_Rgb) ||
124  (srcFormat == Bits24_Rgb))
125  {
126  if (srcFormat != Bits24_Rgb)
127  amtAlpha = (source & 0xff000000) >> 24;
128  amtRed = (source & 0xff0000) >> 16;
129  amtGreen = (source & 0xff00) >> 8;
130  amtBlue = (source & 0xff);
131  }
132  else if (srcFormat == Bits32_Rgba)
133  {
134  amtRed = (source & 0xff000000) >> 24;
135  amtGreen = (source & 0xff0000) >> 16;
136  amtBlue = (source & 0xff00) >> 8;
137  amtAlpha = (source & 0xff);
138  }
139  else if ((srcFormat == Bits32_Bgr) || (srcFormat == Bits24_Bgr))
140  {
141  amtBlue = (source & 0xff0000) >> 16;
142  amtGreen = (source & 0xff00) >> 8;
143  amtRed = (source & 0xff);
144  }
145  else if (srcFormat == Bits16_Argb)
146  {
147  amtAlpha = (((source & 0xF000) >> 12) / 0xF) * 0xFF;
148  amtRed = (((source & 0xF00) >> 8) / 0xF) * 0xFF;
149  amtGreen = (((source & 0xF0) >> 4) / 0xF) * 0xFF;
150  amtBlue = ((source & 0xF) / 0xF) * 0xFF;
151  }
152  else if (srcFormat == Bits16_Rgb565)
153  {
154  amtRed = (((source & 0xF800) >> 11) / 0x1F) * 0xFF;
155  amtGreen = (((source & 0x7E0) >> 5) / 0x3F) * 0xFF;
156  amtBlue = ((source & 0x1F) / 0x1F) * 0xFF;
157  }
158  else if (srcFormat == Bits16_Rgb555)
159  {
160  amtRed = (((source & 0xF800) >> 10) / 0x1F) * 0xFF;
161  amtGreen = (((source & 0x3E0) >> 5) / 0x1F) * 0xFF;
162  amtBlue = ((source & 0x1F) / 0x1F) * 0xFF;
163  }
164  else if (srcFormat == Bits8_Rgb332)
165  {
166  amtRed = (((source & 0xE0) >> 5) / 0x7) * 0xFF;
167  amtGreen = (((source & 0x1C) >> 2) / 0x7) * 0xFF;
168  amtBlue = ((source & 0x3) / 0x3) * 0xFF;
169  }
170 
171  // Conversion code. Complicated and ugly. :(
172  switch (destFormat)
173  {
174  case Bits32_Argb:
175  dest =
176  (amtAlpha << 24) | (amtRed << 16) | (amtGreen << 8) | amtBlue;
177  return true;
178  case Bits32_Rgba:
179  dest =
180  (amtRed << 24) | (amtGreen << 16) | (amtBlue << 8) | amtAlpha;
181  return true;
182  case Bits32_Rgb:
183  dest = (amtRed << 16) | (amtGreen << 8) | amtBlue;
184  return true;
185  case Bits32_Bgr:
186  dest = (amtRed << 8) | (amtGreen << 16) | amtBlue;
187  return true;
188  case Bits24_Rgb:
189  dest = (amtRed << 16) | (amtGreen << 8) | amtBlue;
190  return true;
191  case Bits24_Bgr:
192  dest = (amtBlue << 16) | (amtGreen << 8) | amtRed;
193  return true;
194  case Bits16_Rgb555:
195  // 8-bit to 5-bit scaling. Lossy.
196  amtRed >>= 3;
197  amtGreen >>= 3;
198  amtBlue >>= 3;
199 
200  dest = (amtRed << 10) | (amtGreen << 5) | (amtBlue);
201  return true;
202  case Bits16_Rgb565:
203  // 8-bit to 5 and 6 -bit scaling. Lossy.
204  amtRed >>= 3;
205  amtGreen >>= 2;
206  amtBlue >>= 3;
207 
208  dest = (amtRed << 11) | (amtGreen << 5) | (amtBlue);
209  return true;
210  case Bits16_Argb:
211  // 8-bit to 4-bit scaling. Lossy.
212  amtRed >>= 4;
213  amtGreen >>= 4;
214  amtBlue >>= 4;
215  amtAlpha >>= 4;
216 
217  dest =
218  (amtAlpha << 12) | (amtRed << 8) | (amtGreen << 4) | (amtBlue);
219  return true;
220  case Bits8_Idx:
222  break;
223  case Bits8_Rgb332:
224  amtRed >>= 5;
225  amtGreen >>= 5;
226  amtBlue >>= 6;
227 
228  dest = (amtRed << 5) | (amtGreen << 2) | amtBlue;
229  return true;
230  }
231 
232  return false;
233 }
234 
238 EXPORTED_PUBLIC Framebuffer *createFramebuffer(
239  Framebuffer *pParent, size_t x, size_t y, size_t w, size_t h,
240  void *pFbOverride = 0);
241 
243 EXPORTED_PUBLIC void destroyFramebuffer(Framebuffer *pFramebuffer);
244 }; // namespace Graphics
245 
246 #endif
uintptr_t base
Base of this buffer in memory. For internal use only.
size_t bufferId
Buffer ID, for easy identification within drivers.
Abstracts the system&#39;s framebuffer offering.
size_t width
Width of the buffer in pixels.
size_t height
Height of the buffer in pixels.
size_t bytesPerPixel
Number of bytes per pixel (as it may be different to the format).