The Pedigree Project  0.1
system/kernel/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/kernel/graphics/Graphics.h"
21 #include "pedigree/kernel/Log.h"
22 #include "pedigree/kernel/machine/Framebuffer.h"
23 #include "pedigree/kernel/utilities/new"
24 
25 Framebuffer *Graphics::createFramebuffer(
26  Framebuffer *pParent, size_t x, size_t y, size_t w, size_t h,
27  void *pFbOverride)
28 {
29  if (!(w && h))
30  return 0;
31 
32  // Don't allow insane placement, but do allow "offscreen" buffers.
33  // They may be moved later, at which point they will be "onscreen"
34  // buffers.
35  if ((x > pParent->getWidth()) || (y > pParent->getHeight()))
36  return 0;
37 
38  // Every framebuffer in the system uses the same framebuffer format - that
39  // of the graphics card itself.
40  Graphics::PixelFormat format = pParent->getFormat();
41 
42  size_t bytesPerPixel = pParent->getBytesPerPixel();
43  size_t bytesPerLine = bytesPerPixel * w;
44 
45  // Allocate space for the buffer
46  uint8_t *pMem = 0;
47  if (!pFbOverride)
48  pMem = new uint8_t[bytesPerLine * h];
49  else
50  pMem = reinterpret_cast<uint8_t *>(pFbOverride);
51  NOTICE(
52  "pMem: " << reinterpret_cast<uintptr_t>(pMem)
53  << " ov=" << (pFbOverride ? "yes" : "no"));
54 
55  // Create the framebuffer
56  Framebuffer *pRet = new Framebuffer();
57  pRet->setFramebuffer(reinterpret_cast<uintptr_t>(pMem));
58  pRet->setWidth(w);
59  pRet->setHeight(h);
60  pRet->setFormat(format);
61  pRet->setBytesPerPixel(bytesPerPixel);
62  pRet->setBytesPerLine(bytesPerLine);
63  pRet->setXPos(x);
64  pRet->setYPos(y);
65  pRet->m_pParent = pParent;
66  return pRet;
67 }
68 
69 void Graphics::destroyFramebuffer(Framebuffer *pFramebuffer)
70 {
71  if ((!pFramebuffer) || (!pFramebuffer->getRawBuffer()))
72  return;
73 
74  // Let the parent redraw itself onto this region
75  if (pFramebuffer->getParent())
76  pFramebuffer->redraw(
77  0, 0, pFramebuffer->getWidth(), pFramebuffer->getHeight(), false);
78 
79  // Delete our framebuffer memory
81  // delete [] reinterpret_cast<uint8_t*>(pFramebuffer->getRawBuffer());
82 
83  // Finally delete the framebuffer object
84  delete pFramebuffer;
85 }
virtual void * getRawBuffer() const
Definition: Framebuffer.cc:87
Framebuffer * m_pParent
Parent of this framebuffer.
void redraw(size_t x=~0UL, size_t y=~0UL, size_t w=~0UL, size_t h=~0UL, bool bChild=false)
Definition: Framebuffer.cc:112
#define NOTICE(text)
Definition: Log.h:74
Abstracts the system&#39;s framebuffer offering.