Usermode/AxWin4 - Tweak handling of AcessNative (doesn't have SHM and mmap)
[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         size_t  size = m_rect.m_w*m_rect.m_h*4;
35         if( m_fd == -1 )
36         {
37                 // 2. Allocate a copy in SHM
38                 m_fd = _SysOpen("/Devices/shm/anon", OPENFLAG_WRITE|OPENFLAG_READ);
39                 if(m_fd == -1) {
40                         _SysDebug("GetSHMHandle: Unable to open anon SHM");
41                         return -1;
42                 }
43                 // 1. Free local buffer
44                 delete m_data;
45                 _SysTruncate(m_fd, size);
46         }
47         else
48         {
49                 _SysMUnMap(m_data, size);
50         }
51         // 3. mmap shm copy
52         m_data = static_cast<uint32_t*>( _SysMMap(nullptr, size, MMAP_PROT_WRITE, 0, m_fd, 0) );
53         if(!m_data)     throw ::std::system_error(errno, ::std::system_category());
54         
55         return _SysMarshalFD(m_fd);
56 }
57
58 void CSurface::Resize(unsigned int W, unsigned int H)
59 {
60         if( m_fd == -1 )
61         {
62                 // Easy realloc
63                 // TODO: Should I maintain window contents sanely? NOPE!
64                 delete m_data;
65                 m_data = new uint32_t[W * H];
66         }
67         else
68         {
69                 //_SysIOCtl(m_fd, SHM_IOCTL_SETSIZE, W*H*4);
70         }
71         m_rect.Resize(W, H);
72 }
73
74 void CSurface::DrawScanline(unsigned int row, unsigned int x_ofs, unsigned int w, const void* data)
75 {
76         if( row >= m_rect.m_h )
77                 throw ::std::out_of_range("CSurface::DrawScanline row");
78         if( x_ofs >= m_rect.m_w )
79                 throw ::std::out_of_range("CSurface::DrawScanline x_ofs");
80
81         if( w > m_rect.m_w )
82                 throw ::std::out_of_range("CSurface::DrawScanline width");
83         
84         size_t  ofs = row*m_rect.m_w + x_ofs;
85         ::memcpy( &m_data[ofs], data, w*4 );
86 }
87
88 const uint32_t* CSurface::GetScanline(unsigned int row, unsigned int x_ofs) const
89 {
90         if( row >= m_rect.m_h )
91                 throw ::std::out_of_range("CSurface::GetScanline row");
92         if( x_ofs >= m_rect.m_w )
93                 throw ::std::out_of_range("CSurface::GetScanline x_ofs");
94
95         return &m_data[row * m_rect.m_w + x_ofs];
96 }
97
98
99 };      // namespace AxWin
100
101

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