Usermode/AxWin4 - Working in shared memory usage
[tpg/acess2.git] / Usermode / Applications / axwin4_src / Server / CSurface.cpp
1 /*
2  * Acess2 GUI v4
3  * - By John Hodge (thePowersGang)
4  *
5  * CWindow.cpp
6  * - Window
7  */
8 #include <CSurface.hpp>
9 #include <cassert>
10 #include <stdexcept>
11 #include <cstring>
12 #include <system_error>
13 #include <cerrno>
14
15 namespace AxWin {
16
17 CSurface::CSurface(int x, int y, unsigned int w, unsigned int h):
18         m_rect(x,y, w,h),
19         m_fd(-1),
20         m_data(0)
21 {
22         if( w > 0 && h > 0 )
23         {
24                 m_data = new uint32_t[w * h];
25         }
26 }
27
28 CSurface::~CSurface()
29 {
30 }
31
32 uint64_t CSurface::GetSHMHandle()
33 {
34         // 1. Free local buffer
35         delete m_data;
36         // 2. Allocate a copy in SHM
37         m_fd = _SysOpen("/Devices/shm/anon", OPENFLAG_WRITE|OPENFLAG_READ);
38         if(m_fd==-1)    throw ::std::system_error(errno, ::std::system_category());
39         size_t  size = m_rect.m_w*m_rect.m_h*4;
40         _SysTruncate(m_fd, size);
41         // 3. mmap shm copy
42         m_data = static_cast<uint32_t*>( _SysMMap(nullptr, size, MMAP_PROT_WRITE, 0, m_fd, 0) );
43         if(!m_data)     throw ::std::system_error(errno, ::std::system_category());
44         return _SysMarshalFD(m_fd);
45 }
46
47 void CSurface::Resize(unsigned int W, unsigned int H)
48 {
49         if( m_fd == -1 )
50         {
51                 // Easy realloc
52                 // TODO: Should I maintain window contents sanely? NOPE!
53                 delete m_data;
54                 m_data = new uint32_t[W * H];
55         }
56         else
57         {
58                 //_SysIOCtl(m_fd, SHM_IOCTL_SETSIZE, W*H*4);
59         }
60         m_rect.Resize(W, H);
61 }
62
63 void CSurface::DrawScanline(unsigned int row, unsigned int x_ofs, unsigned int w, const void* data)
64 {
65         if( row >= m_rect.m_h )
66                 throw ::std::out_of_range("CSurface::DrawScanline row");
67         if( x_ofs >= m_rect.m_w )
68                 throw ::std::out_of_range("CSurface::DrawScanline x_ofs");
69
70         if( w > m_rect.m_w )
71                 throw ::std::out_of_range("CSurface::DrawScanline width");
72         
73         size_t  ofs = row*m_rect.m_w + x_ofs;
74         ::memcpy( &m_data[ofs], data, w*4 );
75 }
76
77 const uint32_t* CSurface::GetScanline(unsigned int row, unsigned int x_ofs) const
78 {
79         if( row >= m_rect.m_h )
80                 throw ::std::out_of_range("CSurface::GetScanline row");
81         if( x_ofs >= m_rect.m_w )
82                 throw ::std::out_of_range("CSurface::GetScanline x_ofs");
83
84         return &m_data[row * m_rect.m_w + x_ofs];
85 }
86
87
88 };      // namespace AxWin
89
90

UCC git Repository :: git.ucc.asn.au