The Pedigree Project  0.1
modules/subsys/native/include/pedigree/native/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/native/compiler.h"
24 #include "pedigree/native/types.h"
25 
26 namespace PedigreeGraphics
27 {
29 {
30  public:
31  Rect();
32  Rect(size_t x_, size_t y_, size_t width_, size_t height_);
33 
34  virtual ~Rect();
35 
36  void update(size_t x_, size_t y_, size_t w_, size_t h_);
37 
38  size_t getX() const;
39  size_t getY() const;
40  size_t getW() const;
41  size_t getH() const;
42 
43  private:
44  size_t x, y;
45  size_t w, h;
46 };
47 
48 enum PixelFormat
49 {
50  Bits32_Argb, // Alpha + RGB, with alpha in the highest byte
51  Bits32_Rgba, // RGB + alpha, with alpha in the lowest byte
52  Bits32_Rgb, // RGB, no alpha, essentially the same as above
53  Bits32_Bgr, // BGR, no alpha
54 
55  Bits24_Rgb, // RGB in a 24-bit pack
56  Bits24_Bgr, // R and B bytes swapped
57 
58  Bits16_Argb, // 4:4:4:4 ARGB, alpha most significant nibble
59  Bits16_Rgb565, // 5:6:5 RGB
60  Bits16_Rgb555, // 5:5:5 RGB
61 
62  Bits8_Idx, // Index into a palette
63  Bits8_Rgb332,
64 };
65 
66 inline size_t bitsPerPixel(PixelFormat format)
67 {
68  switch (format)
69  {
70  case Bits32_Argb:
71  case Bits32_Rgba:
72  case Bits32_Rgb:
73  case Bits32_Bgr:
74  return 32;
75  case Bits24_Rgb:
76  case Bits24_Bgr:
77  return 24;
78  case Bits16_Argb:
79  case Bits16_Rgb565:
80  case Bits16_Rgb555:
81  return 16;
82  case Bits8_Idx:
83  return 8;
84  default:
85  return 4;
86  }
87 }
88 
89 inline size_t bytesPerPixel(PixelFormat format)
90 {
91  return bitsPerPixel(format) / 8;
92 }
93 
95 inline uint32_t createRgb(uint32_t r, uint32_t g, uint32_t b)
96 {
97  return (r << 16) | (g << 8) | b;
98 }
99 
100 struct Buffer
101 {
102  uint32_t empty;
103 };
104 
105 class Framebuffer;
106 
108 {
111  void *pDisplay;
112 
113  /* Some form of hardware caps here... */
114  bool bHardwareAccel;
115 
116  Framebuffer *pFramebuffer;
117 
118  size_t maxWidth;
119  size_t maxHeight;
120  size_t maxDepth;
121 
122  bool bReserved1;
123 
125  size_t contextId;
126 };
127 
134 {
135  public:
136  Framebuffer();
137  virtual ~Framebuffer();
138 
140  size_t getWidth();
141 
143  size_t getHeight();
144 
146  PedigreeGraphics::PixelFormat getFormat();
147 
149  size_t getBytesPerPixel();
150 
154  void setPalette(uint32_t *palette, size_t entries);
155 
161  void *getRawBuffer();
162 
165  Framebuffer *createChild(size_t x, size_t y, size_t w, size_t h);
166 
168  bool convertPixel(
169  uint32_t source, PedigreeGraphics::PixelFormat srcFormat,
170  uint32_t &dest, PedigreeGraphics::PixelFormat destFormat);
171 
186  PedigreeGraphics::Buffer *createBuffer(
187  const void *srcData, PedigreeGraphics::PixelFormat srcFormat,
188  size_t width, size_t height);
189 
192  void destroyBuffer(PedigreeGraphics::Buffer *pBuffer);
193 
202  void redraw(
203  size_t x = ~0UL, size_t y = ~0UL, size_t w = ~0UL, size_t h = ~0UL,
204  bool bChild = false);
205 
207  void blit(
208  PedigreeGraphics::Buffer *pBuffer, size_t srcx, size_t srcy,
209  size_t destx, size_t desty, size_t width, size_t height);
210 
214  void draw(
215  void *pBuffer, size_t srcx, size_t srcy, size_t destx, size_t desty,
216  size_t width, size_t height,
217  PedigreeGraphics::PixelFormat format = PedigreeGraphics::Bits32_Argb);
218 
220  void rect(
221  size_t x, size_t y, size_t width, size_t height, uint32_t colour,
222  PedigreeGraphics::PixelFormat format = PedigreeGraphics::Bits32_Argb);
223 
225  void copy(
226  size_t srcx, size_t srcy, size_t destx, size_t desty, size_t w,
227  size_t h);
228 
230  void line(
231  size_t x1, size_t y1, size_t x2, size_t y2, uint32_t colour,
232  PedigreeGraphics::PixelFormat format = PedigreeGraphics::Bits32_Argb);
233 
235  void setPixel(
236  size_t x, size_t y, uint32_t colour,
237  PedigreeGraphics::PixelFormat format = PedigreeGraphics::Bits32_Argb);
238 
243 
246  {
247  return m_Provider;
248  }
249 
250  private:
251  GraphicsProvider m_Provider;
252 
253  bool m_bProviderValid;
254 
255  bool m_bIsChild;
256 };
257 
258 inline bool convertPixel(
259  uint32_t source, PixelFormat srcFormat, uint32_t &dest,
260  PixelFormat destFormat)
261 {
262  if ((srcFormat == destFormat) || (!source))
263  {
264  dest = source;
265  return true;
266  }
267 
268  // Amount of red/green/blue, in 8-bit intensity values
269  uint8_t amtRed = 0, amtGreen = 0, amtBlue = 0, amtAlpha = 0;
270 
271  // Unpack the pixel as necessary
272  if ((srcFormat == Bits32_Argb) || (srcFormat == Bits32_Rgb) ||
273  (srcFormat == Bits24_Rgb))
274  {
275  if (srcFormat != Bits24_Rgb)
276  amtAlpha = (source & 0xff000000) >> 24;
277  amtRed = (source & 0xff0000) >> 16;
278  amtGreen = (source & 0xff00) >> 8;
279  amtBlue = (source & 0xff);
280  }
281  else if (srcFormat == Bits32_Rgba)
282  {
283  amtRed = (source & 0xff000000) >> 24;
284  amtGreen = (source & 0xff0000) >> 16;
285  amtBlue = (source & 0xff00) >> 8;
286  amtAlpha = (source & 0xff);
287  }
288  else if ((srcFormat == Bits32_Bgr) || (srcFormat == Bits24_Bgr))
289  {
290  amtBlue = (source & 0xff0000) >> 16;
291  amtGreen = (source & 0xff00) >> 8;
292  amtRed = (source & 0xff);
293  }
294  else if (srcFormat == Bits16_Argb)
295  {
296  amtAlpha = (((source & 0xF000) >> 12) / 0xF) * 0xFF;
297  amtRed = (((source & 0xF00) >> 8) / 0xF) * 0xFF;
298  amtGreen = (((source & 0xF0) >> 4) / 0xF) * 0xFF;
299  amtBlue = ((source & 0xF) / 0xF) * 0xFF;
300  }
301  else if (srcFormat == Bits16_Rgb565)
302  {
303  amtRed = (((source & 0xF800) >> 11) / 0x1F) * 0xFF;
304  amtGreen = (((source & 0x7E0) >> 5) / 0x3F) * 0xFF;
305  amtBlue = ((source & 0x1F) / 0x1F) * 0xFF;
306  }
307  else if (srcFormat == Bits16_Rgb555)
308  {
309  amtRed = (((source & 0xF800) >> 10) / 0x1F) * 0xFF;
310  amtGreen = (((source & 0x3E0) >> 5) / 0x1F) * 0xFF;
311  amtBlue = ((source & 0x1F) / 0x1F) * 0xFF;
312  }
313  else if (srcFormat == Bits8_Rgb332)
314  {
315  amtRed = (((source & 0xE0) >> 5) / 0x7) * 0xFF;
316  amtGreen = (((source & 0x1C) >> 2) / 0x7) * 0xFF;
317  amtBlue = ((source & 0x3) / 0x3) * 0xFF;
318  }
319 
320  // Conversion code. Complicated and ugly. :(
321  switch (destFormat)
322  {
323  case Bits32_Argb:
324  dest =
325  (amtAlpha << 24) | (amtRed << 16) | (amtGreen << 8) | amtBlue;
326  return true;
327  case Bits32_Rgba:
328  dest =
329  (amtRed << 24) | (amtGreen << 16) | (amtBlue << 8) | amtAlpha;
330  return true;
331  case Bits32_Rgb:
332  dest = (amtRed << 16) | (amtGreen << 8) | amtBlue;
333  return true;
334  case Bits24_Rgb:
335  dest = (amtRed << 16) | (amtGreen << 8) | amtBlue;
336  return true;
337  case Bits24_Bgr:
338  dest = (amtBlue << 16) | (amtGreen << 8) | amtRed;
339  return true;
340  case Bits16_Rgb555:
341  // 8-bit to 5-bit scaling. Lossy.
342  amtRed >>= 3;
343  amtGreen >>= 3;
344  amtBlue >>= 3;
345 
346  dest = (amtRed << 10) | (amtGreen << 5) | (amtBlue);
347  return true;
348  case Bits16_Rgb565:
349  // 8-bit to 5 and 6 -bit scaling. Lossy.
350  amtRed >>= 3;
351  amtGreen >>= 2;
352  amtBlue >>= 3;
353 
354  dest = (amtRed << 11) | (amtGreen << 5) | (amtBlue);
355  return true;
356  case Bits16_Argb:
357  // 8-bit to 4-bit scaling. Lossy.
358  amtRed >>= 4;
359  amtGreen >>= 4;
360  amtBlue >>= 4;
361  amtAlpha >>= 4;
362 
363  dest =
364  (amtAlpha << 12) | (amtRed << 8) | (amtGreen << 4) | (amtBlue);
365  return true;
366  case Bits8_Idx:
368  break;
369  case Bits8_Rgb332:
370  amtRed >>= 5;
371  amtGreen >>= 5;
372  amtBlue >>= 6;
373 
374  dest = (amtRed << 5) | (amtGreen << 2) | amtBlue;
375  return true;
376  default:
377  break;
378  }
379 
380  return false;
381 }
382 }; // namespace PedigreeGraphics
383 
384 #endif
size_t contextId
For sharing framebuffers across process boundaries.