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