3 * - By John Hodge (thePowersGang)
8 #include <CSurface.hpp>
12 #include <system_error>
14 #include <CColour.hpp>
18 CSurface::CSurface(int x, int y, unsigned int w, unsigned int h):
25 m_data = new uint32_t[w * h];
35 size_t size = m_rect.m_w*m_rect.m_h*4;
36 _SysMUnMap(m_data, size);
40 uint64_t CSurface::GetSHMHandle()
42 size_t size = m_rect.m_w*m_rect.m_h*4;
45 // 2. Allocate a copy in SHM
46 m_fd = _SysOpen("/Devices/shm/anon", OPENFLAG_WRITE|OPENFLAG_READ);
48 _SysDebug("GetSHMHandle: Unable to open anon SHM");
51 // 1. Free local buffer
53 _SysTruncate(m_fd, size);
57 _SysMUnMap(m_data, size);
60 m_data = static_cast<uint32_t*>( _SysMMap(nullptr, size, MMAP_PROT_WRITE, 0, m_fd, 0) );
61 if(!m_data) throw ::std::system_error(errno, ::std::system_category());
63 return _SysMarshalFD(m_fd);
66 void CSurface::Resize(unsigned int W, unsigned int H)
71 // TODO: Should I maintain window contents sanely? NOPE!
73 m_data = new uint32_t[W * H];
77 //_SysIOCtl(m_fd, SHM_IOCTL_SETSIZE, W*H*4);
82 void CSurface::DrawScanline(unsigned int row, unsigned int x_ofs, unsigned int w, const void* data)
84 if( row >= m_rect.m_h )
85 throw ::std::out_of_range("CSurface::DrawScanline row");
86 if( x_ofs >= m_rect.m_w )
87 throw ::std::out_of_range("CSurface::DrawScanline x_ofs");
90 throw ::std::out_of_range("CSurface::DrawScanline width");
92 size_t ofs = row*m_rect.m_w + x_ofs;
93 ::memcpy( &m_data[ofs], data, w*4 );
96 void CSurface::BlendScanline(unsigned int row, unsigned int x_ofs, unsigned int w, const void* data)
98 if( row >= m_rect.m_h )
99 throw ::std::out_of_range("CSurface::DrawScanline row");
100 if( x_ofs >= m_rect.m_w )
101 throw ::std::out_of_range("CSurface::DrawScanline x_ofs");
104 throw ::std::out_of_range("CSurface::DrawScanline width");
106 const uint32_t* in_data = (const uint32_t*)data;
107 size_t ofs = row*m_rect.m_w + x_ofs;
108 for( unsigned int x = 0; x < w; x ++ )
110 CColour out = CColour::from_argb(m_data[ofs+x]).blend( CColour::from_argb(in_data[x]) );
111 m_data[ofs+x] = out.to_argb();
115 void CSurface::FillScanline(unsigned int row, unsigned int x_ofs, unsigned int w, uint32_t colour)
117 if( row >= m_rect.m_h )
118 throw ::std::out_of_range("CSurface::FillScanline row");
119 if( x_ofs >= m_rect.m_w )
120 throw ::std::out_of_range("CSurface::FillScanline x_ofs");
123 throw ::std::out_of_range("CSurface::FillScanline width");
125 size_t ofs = row*m_rect.m_w + x_ofs;
127 m_data[ofs++] = colour;
130 const uint32_t* CSurface::GetScanline(unsigned int row, unsigned int x_ofs) const
132 if( row >= m_rect.m_h )
133 throw ::std::out_of_range("CSurface::GetScanline row");
134 if( x_ofs >= m_rect.m_w )
135 throw ::std::out_of_range("CSurface::GetScanline x_ofs");
137 return &m_data[row * m_rect.m_w + x_ofs];
141 }; // namespace AxWin