Merge branch 'master' of git://github.com/thepowersgang/acess2
[tpg/acess2.git] / Usermode / Applications / axwin4_src / Server / CSurface.cpp
index 07d8bfb..d0eddc4 100644 (file)
@@ -11,6 +11,7 @@
 #include <cstring>
 #include <system_error>
 #include <cerrno>
+#include <CColour.hpp>
 
 namespace AxWin {
 
@@ -27,20 +28,38 @@ CSurface::CSurface(int x, int y, unsigned int w, unsigned int h):
 
 CSurface::~CSurface()
 {
+       if( m_fd == -1 ) {
+               delete[] m_data;
+       }
+       else {
+               size_t  size = m_rect.m_w*m_rect.m_h*4;
+               _SysMUnMap(m_data, size);
+       }
 }
 
 uint64_t CSurface::GetSHMHandle()
 {
-       // 1. Free local buffer
-       delete m_data;
-       // 2. Allocate a copy in SHM
-       m_fd = _SysOpen("/Devices/shm/anon", OPENFLAG_WRITE|OPENFLAG_READ);
-       if(m_fd==-1)    throw ::std::system_error(errno, ::std::system_category());
        size_t  size = m_rect.m_w*m_rect.m_h*4;
-       _SysTruncate(m_fd, size);
+       if( m_fd == -1 )
+       {
+               // 2. Allocate a copy in SHM
+               m_fd = _SysOpen("/Devices/shm/anon", OPENFLAG_WRITE|OPENFLAG_READ);
+               if(m_fd == -1) {
+                       _SysDebug("GetSHMHandle: Unable to open anon SHM");
+                       return -1;
+               }
+               // 1. Free local buffer
+               delete m_data;
+               _SysTruncate(m_fd, size);
+       }
+       else
+       {
+               _SysMUnMap(m_data, size);
+       }
        // 3. mmap shm copy
        m_data = static_cast<uint32_t*>( _SysMMap(nullptr, size, MMAP_PROT_WRITE, 0, m_fd, 0) );
        if(!m_data)     throw ::std::system_error(errno, ::std::system_category());
+       
        return _SysMarshalFD(m_fd);
 }
 
@@ -74,6 +93,40 @@ void CSurface::DrawScanline(unsigned int row, unsigned int x_ofs, unsigned int w
        ::memcpy( &m_data[ofs], data, w*4 );
 }
 
+void CSurface::BlendScanline(unsigned int row, unsigned int x_ofs, unsigned int w, const void* data)
+{
+       if( row >= m_rect.m_h )
+               throw ::std::out_of_range("CSurface::DrawScanline row");
+       if( x_ofs >= m_rect.m_w )
+               throw ::std::out_of_range("CSurface::DrawScanline x_ofs");
+
+       if( w > m_rect.m_w )
+               throw ::std::out_of_range("CSurface::DrawScanline width");
+       
+       const uint32_t* in_data = (const uint32_t*)data;
+       size_t  ofs = row*m_rect.m_w + x_ofs;
+       for( unsigned int x = 0; x < w; x ++ )
+       {
+               CColour out = CColour::from_argb(m_data[ofs+x]).blend( CColour::from_argb(in_data[x]) );
+               m_data[ofs+x] = out.to_argb();
+       }
+}
+
+void CSurface::FillScanline(unsigned int row, unsigned int x_ofs, unsigned int w, uint32_t colour)
+{
+       if( row >= m_rect.m_h )
+               throw ::std::out_of_range("CSurface::FillScanline row");
+       if( x_ofs >= m_rect.m_w )
+               throw ::std::out_of_range("CSurface::FillScanline x_ofs");
+
+       if( w > m_rect.m_w )
+               throw ::std::out_of_range("CSurface::FillScanline width");
+       
+       size_t  ofs = row*m_rect.m_w + x_ofs;
+       while( w -- )
+               m_data[ofs++] = colour;
+}
+
 const uint32_t* CSurface::GetScanline(unsigned int row, unsigned int x_ofs) const
 {
        if( row >= m_rect.m_h )

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