The Pedigree Project  0.1
modules/subsys/native/user/graphics/Graphics.cc
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 #include "pedigree/native/graphics/Graphics.h"
21 #include "pedigree/kernel/debugger/libudis86/types.h"
22 
23 #include "modules/subsys/pedigree-c/pedigree-syscalls.h"
24 
25 #define PACKED __attribute__((packed))
26 
27 using namespace PedigreeGraphics;
28 
29 struct blitargs
30 {
31  Buffer *pBuffer;
32  uint32_t srcx, srcy, destx, desty, width, height;
33 } PACKED;
34 
35 struct drawargs
36 {
37  void *a;
38  uintptr_t b, c, d, e, f, g, h;
39 } PACKED;
40 
41 struct createargs
42 {
43  void *pFramebuffer;
44  void *pReturnProvider;
45  size_t x, y, w, h;
46 } PACKED;
47 
48 struct fourargs
49 {
50  uintptr_t a, b, c, d;
51 } PACKED;
52 
53 struct sixargs
54 {
55  uintptr_t a, b, c, d, e, f;
56 } PACKED;
57 
58 Rect::Rect() : x(0), y(0), w(0), h(0)
59 {
60 }
61 Rect::Rect(size_t x_, size_t y_, size_t width_, size_t height_)
62  : x(x_), y(y_), w(width_), h(height_)
63 {
64 }
65 
66 Rect::~Rect() = default;
67 
68 void Rect::update(size_t x_, size_t y_, size_t w_, size_t h_)
69 {
70  this->x = x_;
71  this->y = y_;
72  this->w = w_;
73  this->h = h_;
74 }
75 
76 size_t Rect::getX() const
77 {
78  return x;
79 }
80 
81 size_t Rect::getY() const
82 {
83  return y;
84 }
85 
86 size_t Rect::getW() const
87 {
88  return w;
89 }
90 
91 size_t Rect::getH() const
92 {
93  return h;
94 }
95 
96 Framebuffer::Framebuffer()
97  : m_Provider(), m_bProviderValid(false), m_bIsChild(false)
98 {
99  int ret = pedigree_gfx_get_provider(&m_Provider);
100  if (ret >= 0)
101  m_bProviderValid = true;
102 }
103 
104 Framebuffer::Framebuffer(GraphicsProvider &gfx)
105 {
106  m_Provider = gfx;
107  m_bProviderValid = m_bIsChild = true;
108 
109  // Because a GraphicsProvider is a generic structure, we could have been
110  // created in another address space. We need to 'transfer' the provider
111  // into our own address space.
112  // Fortunately, the provider is still valid, so we can call getters here.
113  // We just can't do anything that involves the actual framebuffer.
114  size_t nBytes = (getWidth() * getHeight() * getBytesPerPixel());
115  uint8_t *pBuffer = new uint8_t[nBytes];
116 
117  GraphicsProvider ret = m_Provider;
118  createargs args;
119  args.pReturnProvider = &m_Provider;
120  args.pFramebuffer = reinterpret_cast<void *>(pBuffer);
121  args.x = 0;
122  args.y = 0;
123  args.w = getWidth();
124  args.h = getHeight();
125  int ok = pedigree_gfx_create_fbuffer(&gfx, &args);
126 
127  if (ok < 0)
128  {
129  delete[] pBuffer;
130  m_bProviderValid = false;
131  }
132 }
133 
134 Framebuffer::~Framebuffer()
135 {
136  if (m_bIsChild)
137  pedigree_gfx_delete_fbuffer(&m_Provider);
138 }
139 
140 void Framebuffer::setPalette(uint32_t *palette, size_t entries)
141 {
142  if (!m_bProviderValid)
143  return;
144 
145  pedigree_gfx_setpalette(&m_Provider, palette, entries);
146 }
147 
149 {
150  if (!m_bProviderValid)
151  return 0;
152 
153  size_t ret = 0;
154  pedigree_gfx_fbinfo(&m_Provider, &ret, 0, 0, 0);
155 
156  return ret;
157 }
158 
160 {
161  if (!m_bProviderValid)
162  return 0;
163 
164  size_t ret = 0;
165  pedigree_gfx_fbinfo(&m_Provider, 0, &ret, 0, 0);
166 
167  return ret;
168 }
169 
171 {
172  if (!m_bProviderValid)
173  return Bits32_Argb;
174 
175  uint32_t ret = 0;
176  pedigree_gfx_fbinfo(&m_Provider, 0, 0, &ret, 0);
177 
178  return static_cast<PixelFormat>(ret);
179 }
180 
182 {
183  if (!m_bProviderValid)
184  return 4;
185 
186  size_t ret = 0;
187  pedigree_gfx_fbinfo(&m_Provider, 0, 0, 0, &ret);
188 
189  return ret;
190 }
191 
193 {
194  if (!m_bProviderValid)
195  return 0;
196 
197  return reinterpret_cast<void *>(pedigree_gfx_get_raw_buffer(&m_Provider));
198 }
199 
200 Framebuffer *Framebuffer::createChild(size_t x, size_t y, size_t w, size_t h)
201 {
202  if (!m_bProviderValid)
203  return 0;
204  if (!(w && h))
205  return 0;
206 
207  size_t nBytes = (w * h * getBytesPerPixel());
208  uint8_t *pBuffer = new uint8_t[nBytes];
209 
210  GraphicsProvider ret = m_Provider;
211  createargs args;
212  args.pReturnProvider = &ret;
213  args.pFramebuffer = reinterpret_cast<void *>(pBuffer);
214  args.x = x;
215  args.y = y;
216  args.w = w;
217  args.h = h;
218  int ok = pedigree_gfx_create_fbuffer(&m_Provider, &args);
219 
220  if (ok < 0)
221  {
222  delete[] pBuffer;
223  return 0;
224  }
225 
226  Framebuffer *pNewFB = new Framebuffer(ret);
227  return pNewFB;
228 }
229 
231  const void *srcData, PixelFormat srcFormat, size_t width, size_t height)
232 {
233  if (!m_bProviderValid)
234  return 0;
235 
236  Buffer *ret = 0;
237  fourargs args;
238  args.a = reinterpret_cast<uintptr_t>(srcData);
239  args.b = static_cast<uintptr_t>(srcFormat);
240  args.c = width;
241  args.d = height;
242  int ok = pedigree_gfx_create_buffer(
243  &m_Provider, reinterpret_cast<void **>(&ret), &args);
244 
245  if (ok >= 0)
246  return ret;
247  else
248  {
249  delete ret;
250  return 0;
251  }
252 }
253 
255 {
256  if (!m_bProviderValid)
257  return;
258 
259  pedigree_gfx_destroy_buffer(&m_Provider, pBuffer);
260 }
261 
262 void Framebuffer::redraw(size_t x, size_t y, size_t w, size_t h, bool bChild)
263 {
264  if (!m_bProviderValid)
265  return;
266 
267  sixargs args;
268  args.a = x;
269  args.b = y;
270  args.c = w;
271  args.d = h;
272  args.e = bChild;
273 
274  pedigree_gfx_redraw(&m_Provider, &args);
275 }
276 
278  Buffer *pBuffer, size_t srcx, size_t srcy, size_t destx, size_t desty,
279  size_t width, size_t height)
280 {
281  blitargs args;
282  args.pBuffer = pBuffer;
283  args.srcx = srcx;
284  args.srcy = srcy;
285  args.destx = destx;
286  args.desty = desty;
287  args.width = width;
288  args.height = height;
289 
290  pedigree_gfx_blit(&m_Provider, &args);
291 }
292 
294  void *pBuffer, size_t srcx, size_t srcy, size_t destx, size_t desty,
295  size_t width, size_t height, PixelFormat format)
296 {
297  if (!m_bProviderValid)
298  return;
299 
300  drawargs args;
301  args.a = pBuffer;
302  args.b = srcx;
303  args.c = srcy;
304  args.d = destx;
305  args.e = desty;
306  args.f = width;
307  args.g = height;
308  args.h = static_cast<uint32_t>(format);
309 
310  pedigree_gfx_draw(&m_Provider, &args);
311 }
312 
314  size_t x, size_t y, size_t width, size_t height, uint32_t colour,
315  PixelFormat format)
316 {
317  if (!m_bProviderValid)
318  return;
319 
320  sixargs args;
321  args.a = x;
322  args.b = y;
323  args.c = width;
324  args.d = height;
325  args.e = colour;
326  args.f = static_cast<uint32_t>(format);
327 
328  pedigree_gfx_rect(&m_Provider, &args);
329 }
330 
332  size_t srcx, size_t srcy, size_t destx, size_t desty, size_t w, size_t h)
333 {
334  if (!m_bProviderValid)
335  return;
336 
337  sixargs args;
338  args.a = srcx;
339  args.b = srcy;
340  args.c = destx;
341  args.d = desty;
342  args.e = w;
343  args.f = h;
344 
345  pedigree_gfx_copy(&m_Provider, &args);
346 }
347 
349  size_t x1, size_t y1, size_t x2, size_t y2, uint32_t colour,
350  PixelFormat format)
351 {
352  if (!m_bProviderValid)
353  return;
354 
355  sixargs args;
356  args.a = x1;
357  args.b = y1;
358  args.c = x2;
359  args.d = y2;
360  args.e = colour;
361  args.f = static_cast<uint32_t>(format);
362 
363  pedigree_gfx_line(&m_Provider, &args);
364 }
365 
367  size_t x, size_t y, uint32_t colour, PixelFormat format)
368 {
369  if (!m_bProviderValid)
370  return;
371 
372  pedigree_gfx_set_pixel(
373  &m_Provider, x, y, colour, static_cast<uint32_t>(format));
374 }
void rect(size_t x, size_t y, size_t width, size_t height, uint32_t colour, PedigreeGraphics::PixelFormat format=PedigreeGraphics::Bits32_Argb)
void draw(void *pBuffer, size_t srcx, size_t srcy, size_t destx, size_t desty, size_t width, size_t height, PedigreeGraphics::PixelFormat format=PedigreeGraphics::Bits32_Argb)
void copy(size_t srcx, size_t srcy, size_t destx, size_t desty, size_t w, size_t h)
void destroyBuffer(PedigreeGraphics::Buffer *pBuffer)
void line(size_t x1, size_t y1, size_t x2, size_t y2, uint32_t colour, PedigreeGraphics::PixelFormat format=PedigreeGraphics::Bits32_Argb)
PedigreeGraphics::Buffer * createBuffer(const void *srcData, PedigreeGraphics::PixelFormat srcFormat, size_t width, size_t height)
void setPalette(uint32_t *palette, size_t entries)
void setPixel(size_t x, size_t y, uint32_t colour, PedigreeGraphics::PixelFormat format=PedigreeGraphics::Bits32_Argb)
Framebuffer * createChild(size_t x, size_t y, size_t w, size_t h)
void blit(PedigreeGraphics::Buffer *pBuffer, size_t srcx, size_t srcy, size_t destx, size_t desty, size_t width, size_t height)
void redraw(size_t x=~0UL, size_t y=~0UL, size_t w=~0UL, size_t h=~0UL, bool bChild=false)