The Pedigree Project 0.1
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#include "pedigree/kernel/compiler.h"
23#include "pedigree/kernel/processor/types.h"
24
25#include <config.h>
26
27class Framebuffer;
28
29namespace Graphics {
31inline uint32_t createRgb(uint32_t r, uint32_t g, uint32_t b) {
32 return (r << 16) | (g << 8) | b;
33}
34
35enum PixelFormat {
36 Bits32_Argb, // Alpha + RGB, with alpha in the highest byte
37 Bits32_Rgba, // RGB + alpha, with alpha in the lowest byte
38 Bits32_Rgb, // RGB, no alpha, essentially the same as above
39 Bits32_Bgr, // BGR, no alpha
40
41 Bits24_Rgb, // RGB in a 24-bit pack
42 Bits24_Bgr, // R and B bytes swapped
43
44 Bits16_Argb, // 4:4:4:4 ARGB, alpha most significant nibble
45 Bits16_Rgb565, // 5:6:5 RGB
46 Bits16_Rgb555, // 5:5:5 RGB
47
48 Bits8_Idx, // Index into a palette
49 Bits8_Rgb332, // Intensity values
50};
51
52inline size_t bitsPerPixel(PixelFormat format) {
53 switch (format) {
54 case Bits32_Argb:
55 case Bits32_Rgba:
56 case Bits32_Rgb:
57 case Bits32_Bgr:
58 return 32;
59 case Bits24_Rgb:
60 case Bits24_Bgr:
61 return 24;
62 case Bits16_Argb:
63 case Bits16_Rgb565:
64 case Bits16_Rgb555:
65 return 16;
66 case Bits8_Idx:
67 case Bits8_Rgb332:
68 return 8;
69 }
70
71 return 0;
72}
73
74inline size_t bytesPerPixel(PixelFormat format) {
75 return bitsPerPixel(format) / 8;
76}
77
78struct Buffer {
80 uintptr_t base;
81
83 size_t width;
84
86 size_t height;
87
90 PixelFormat format;
91
94
96 size_t bufferId;
97
100 void* pBacking;
101};
102
103inline bool convertPixel(uint32_t source, PixelFormat srcFormat, uint32_t& dest,
104 PixelFormat destFormat) {
105 if ((srcFormat == destFormat) || (!source)) {
106 dest = source;
107 return true;
108 }
109
110 // Amount of red/green/blue, in 8-bit intensity values
111 uint8_t amtRed = 0, amtGreen = 0, amtBlue = 0, amtAlpha = 0;
112
113 // Unpack the pixel as necessary
114 if ((srcFormat == Bits32_Argb) || (srcFormat == Bits32_Rgb) || (srcFormat == Bits24_Rgb)) {
115 if (srcFormat != Bits24_Rgb)
116 amtAlpha = (source & 0xff000000) >> 24;
117 amtRed = (source & 0xff0000) >> 16;
118 amtGreen = (source & 0xff00) >> 8;
119 amtBlue = (source & 0xff);
120 } else if (srcFormat == Bits32_Rgba) {
121 amtRed = (source & 0xff000000) >> 24;
122 amtGreen = (source & 0xff0000) >> 16;
123 amtBlue = (source & 0xff00) >> 8;
124 amtAlpha = (source & 0xff);
125 } else if ((srcFormat == Bits32_Bgr) || (srcFormat == Bits24_Bgr)) {
126 amtBlue = (source & 0xff0000) >> 16;
127 amtGreen = (source & 0xff00) >> 8;
128 amtRed = (source & 0xff);
129 } else if (srcFormat == Bits16_Argb) {
130 amtAlpha = (((source & 0xF000) >> 12) / 0xF) * 0xFF;
131 amtRed = (((source & 0xF00) >> 8) / 0xF) * 0xFF;
132 amtGreen = (((source & 0xF0) >> 4) / 0xF) * 0xFF;
133 amtBlue = ((source & 0xF) / 0xF) * 0xFF;
134 } else if (srcFormat == Bits16_Rgb565) {
135 amtRed = (((source & 0xF800) >> 11) / 0x1F) * 0xFF;
136 amtGreen = (((source & 0x7E0) >> 5) / 0x3F) * 0xFF;
137 amtBlue = ((source & 0x1F) / 0x1F) * 0xFF;
138 } else if (srcFormat == Bits16_Rgb555) {
139 amtRed = (((source & 0xF800) >> 10) / 0x1F) * 0xFF;
140 amtGreen = (((source & 0x3E0) >> 5) / 0x1F) * 0xFF;
141 amtBlue = ((source & 0x1F) / 0x1F) * 0xFF;
142 } else if (srcFormat == Bits8_Rgb332) {
143 amtRed = (((source & 0xE0) >> 5) / 0x7) * 0xFF;
144 amtGreen = (((source & 0x1C) >> 2) / 0x7) * 0xFF;
145 amtBlue = ((source & 0x3) / 0x3) * 0xFF;
146 }
147
148 // Conversion code. Complicated and ugly. :(
149 switch (destFormat) {
150 case Bits32_Argb:
151 dest = (amtAlpha << 24) | (amtRed << 16) | (amtGreen << 8) | amtBlue;
152 return true;
153 case Bits32_Rgba:
154 dest = (amtRed << 24) | (amtGreen << 16) | (amtBlue << 8) | amtAlpha;
155 return true;
156 case Bits32_Rgb:
157 dest = (amtRed << 16) | (amtGreen << 8) | amtBlue;
158 return true;
159 case Bits32_Bgr:
160 dest = (amtRed << 8) | (amtGreen << 16) | amtBlue;
161 return true;
162 case Bits24_Rgb:
163 dest = (amtRed << 16) | (amtGreen << 8) | amtBlue;
164 return true;
165 case Bits24_Bgr:
166 dest = (amtBlue << 16) | (amtGreen << 8) | amtRed;
167 return true;
168 case Bits16_Rgb555:
169 // 8-bit to 5-bit scaling. Lossy.
170 amtRed >>= 3;
171 amtGreen >>= 3;
172 amtBlue >>= 3;
173
174 dest = (amtRed << 10) | (amtGreen << 5) | (amtBlue);
175 return true;
176 case Bits16_Rgb565:
177 // 8-bit to 5 and 6 -bit scaling. Lossy.
178 amtRed >>= 3;
179 amtGreen >>= 2;
180 amtBlue >>= 3;
181
182 dest = (amtRed << 11) | (amtGreen << 5) | (amtBlue);
183 return true;
184 case Bits16_Argb:
185 // 8-bit to 4-bit scaling. Lossy.
186 amtRed >>= 4;
187 amtGreen >>= 4;
188 amtBlue >>= 4;
189 amtAlpha >>= 4;
190
191 dest = (amtAlpha << 12) | (amtRed << 8) | (amtGreen << 4) | (amtBlue);
192 return true;
193 case Bits8_Idx:
195 break;
196 case Bits8_Rgb332:
197 amtRed >>= 5;
198 amtGreen >>= 5;
199 amtBlue >>= 6;
200
201 dest = (amtRed << 5) | (amtGreen << 2) | amtBlue;
202 return true;
203 }
204
205 return false;
206}
207
211EXPORTED_PUBLIC Framebuffer* createFramebuffer(Framebuffer* pParent, size_t x, size_t y, size_t w,
212 size_t h, void* pFbOverride = 0);
213
215EXPORTED_PUBLIC void destroyFramebuffer(Framebuffer* pFramebuffer);
216}; // namespace Graphics
217
218#endif
size_t bytesPerPixel
Number of bytes per pixel (as it may be different to the format).
Definition Graphics.h:93
PixelFormat format
Definition Graphics.h:90
size_t width
Width of the buffer in pixels.
Definition Graphics.h:83
uintptr_t base
Base of this buffer in memory. For internal use only.
Definition Graphics.h:80
size_t height
Height of the buffer in pixels.
Definition Graphics.h:86
size_t bufferId
Buffer ID, for easy identification within drivers.
Definition Graphics.h:96