From c6d03f16d09b5922659777364e9fa42c3ca4c772 Mon Sep 17 00:00:00 2001 From: James Grogan Date: Fri, 11 Nov 2022 16:32:55 +0000 Subject: [PATCH] Clean image types --- src/graphics/DrawingSurface.h | 6 +- src/image/Image.cpp | 67 ++++++++++++------- src/image/Image.h | 37 +++++----- src/image/ImagePrimitives.h | 26 +++++++ src/image/PngReader.cpp | 4 +- src/image/PngReader.h | 21 +++--- src/image/PngWriter.cpp | 2 +- src/image/PngWriter.h | 5 +- src/image/PngWriterBasic.cpp | 4 +- src/image/PngWriterBasic.h | 4 +- src/image/PngWriterImpl.h | 4 +- .../desktop_elements/IPlatformWindow.h | 20 +++++- src/ui_elements/desktop_elements/Window.cpp | 4 +- src/ui_elements/desktop_elements/Window.h | 7 +- .../ui_interfaces/wayland/WaylandSurface.cpp | 34 +++------- .../ui_interfaces/wayland/WaylandSurface.h | 21 +++++- src/windows/ui_interfaces/x11/XcbWindow.cpp | 6 +- src/windows/ui_interfaces/x11/XcbWindow.h | 4 +- 18 files changed, 169 insertions(+), 107 deletions(-) create mode 100644 src/image/ImagePrimitives.h diff --git a/src/graphics/DrawingSurface.h b/src/graphics/DrawingSurface.h index d971b3b..48fbd58 100644 --- a/src/graphics/DrawingSurface.h +++ b/src/graphics/DrawingSurface.h @@ -3,9 +3,9 @@ #include #include "INativeDrawingSurface.h" +#include "Image.h" class Grid; -class Image; class DrawingSurface { @@ -26,12 +26,12 @@ public: void Paint(Grid* grid); - Image* GetAsImage() const; + Image* GetAsImage() const; private: unsigned mWidth = 0; unsigned mHeight = 0; - std::unique_ptr mImageBuffer; + std::unique_ptr > mImageBuffer; std::unique_ptr mNativeDrawingSurface; }; diff --git a/src/image/Image.cpp b/src/image/Image.cpp index bbf6616..6fa4d99 100644 --- a/src/image/Image.cpp +++ b/src/image/Image.cpp @@ -1,91 +1,112 @@ #include "Image.h" -Image::Image(unsigned width, unsigned height) +#include "Color.h" + +template +Image::Image(unsigned width, unsigned height) : mWidth(width), mHeight(height) { } -Image::~Image() +template +Image::~Image() { } -void Image::Initialize() +template +void Image::initialize() { - mData = std::vector(GetBytesPerRow()*mHeight, 0); + mData = std::vector(getBytesPerRow()*mHeight, 0); } -void Image::SetPixelValue(unsigned idx, unsigned jdx, const Color& color) +template +void Image::setPixelValue(unsigned idx, unsigned jdx, const Color& color) { if (mData.empty()) { - Initialize(); + initialize(); } - mData[jdx*GetBytesPerRow() + idx*3] = static_cast(color.GetR()); - mData[jdx*GetBytesPerRow() + idx*3 + 1] = static_cast(color.GetG()); - mData[jdx*GetBytesPerRow() + idx*3 + 2] = static_cast(color.GetB()); + mData[jdx*getBytesPerRow() + idx*3] = static_cast(color.GetR()); + mData[jdx*getBytesPerRow() + idx*3 + 1] = static_cast(color.GetG()); + mData[jdx*getBytesPerRow() + idx*3 + 2] = static_cast(color.GetB()); } -std::unique_ptr Image::Create(unsigned width, unsigned height) +template +std::unique_ptr > Image::Create(unsigned width, unsigned height) { - return std::make_unique(width, height); + return std::make_unique >(width, height); } -unsigned Image::GetBytesPerRow() const +template +unsigned Image::getBytesPerRow() const { const auto bitsPerEntry = mBitDepth <= 8 ? 1 : 2; return mWidth * mNumChannels *bitsPerEntry; } -unsigned Image::GetWidth() const +template +unsigned Image::getWidth() const { return mWidth; } -unsigned Image::GetHeight() const +template +unsigned Image::getHeight() const { return mHeight; } -unsigned Image::GetBitDepth() const +template +unsigned Image::getBitDepth() const { return mBitDepth; } -unsigned char Image::GetByte(unsigned idx, unsigned jdx) const +template +T Image::getByte(unsigned idx, unsigned jdx) const { - return mData[jdx*GetBytesPerRow() + idx]; + return mData[jdx*getBytesPerRow() + idx]; } -unsigned Image::GetNumChannels() const +template +unsigned Image::getNumChannels() const { return mNumChannels; } -void Image::SetData(const std::vector& data) +template +void Image::setData(const std::vector& data) { mData = data; } -void Image::SetWidth(unsigned width) +template +void Image::setWidth(unsigned width) { mWidth = width; } -void Image::SetHeight(unsigned height) +template +void Image::setHeight(unsigned height) { mHeight = height; } -void Image::SetBitDepth(unsigned bitDepth) +template +void Image::setBitDepth(unsigned bitDepth) { mBitDepth = bitDepth; } -void Image::SetNumChannels(unsigned numChannels) +template +void Image::setNumChannels(unsigned numChannels) { mNumChannels = numChannels; } + +template class Image; +//template class Image; diff --git a/src/image/Image.h b/src/image/Image.h index cbda8e4..c07f110 100644 --- a/src/image/Image.h +++ b/src/image/Image.h @@ -1,43 +1,40 @@ #pragma once -#include "Color.h" - #include #include +class Color; +template class Image { public: - Image(unsigned width, unsigned height); ~Image(); - static std::unique_ptr Create(unsigned width, unsigned height); + static std::unique_ptr > Create(unsigned width, unsigned height); - unsigned GetBytesPerRow() const; - unsigned GetWidth() const; - unsigned GetHeight() const; - unsigned GetBitDepth() const; - unsigned GetNumChannels() const; + unsigned getBytesPerRow() const; + unsigned getWidth() const; + unsigned getHeight() const; + unsigned getBitDepth() const; + unsigned getNumChannels() const; - void SetPixelValue(unsigned idx, unsigned jdx, const Color& color); + void setPixelValue(unsigned idx, unsigned jdx, const Color& color); - unsigned char GetByte(unsigned idx, unsigned jdx) const; + T getByte(unsigned idx, unsigned jdx) const; - void SetData(const std::vector& data); - void SetWidth(unsigned width); - void SetHeight(unsigned height); - void SetBitDepth(unsigned bitDepth); - void SetNumChannels(unsigned numChannels); + void setData(const std::vector& data); + void setWidth(unsigned width); + void setHeight(unsigned height); + void setBitDepth(unsigned bitDepth); + void setNumChannels(unsigned numChannels); private: - void Initialize(); + void initialize(); unsigned mWidth{1}; unsigned mHeight{1}; unsigned mBitDepth{8}; unsigned mNumChannels{1}; - std::vector mData; + std::vector mData; }; - -using ImagePtr = std::unique_ptr; diff --git a/src/image/ImagePrimitives.h b/src/image/ImagePrimitives.h new file mode 100644 index 0000000..b7a6d2c --- /dev/null +++ b/src/image/ImagePrimitives.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +class ImagePrimitives +{ +public: + static void drawCheckerboard(uint8_t* data, int width, int height, int offset = 0) + { + auto pixels = (uint32_t *)&(data)[offset]; + for (int y = 0; y < height; ++y) + { + for (int x = 0; x < width; ++x) + { + if ((x + y / 8 * 8) % 16 < 8) + { + pixels[y * width + x] = 0xFF666666; + } + else + { + pixels[y * width + x] = 0xFFEEEEEE; + } + } + } + } +}; diff --git a/src/image/PngReader.cpp b/src/image/PngReader.cpp index 61843b9..dad6b2e 100644 --- a/src/image/PngReader.cpp +++ b/src/image/PngReader.cpp @@ -108,9 +108,9 @@ void PngReader::logHeader() std::cout << sstr.str() << std::endl; } -std::unique_ptr PngReader::read() +std::unique_ptr > PngReader::read() { - auto image = std::make_unique(5, 5); + auto image = std::make_unique >(5, 5); mFile = std::make_unique(mPath); mFile->Open(true); diff --git a/src/image/PngReader.h b/src/image/PngReader.h index 12f0b84..23c7397 100644 --- a/src/image/PngReader.h +++ b/src/image/PngReader.h @@ -5,8 +5,7 @@ #include #include "File.h" - -class Image; +#include "Image.h" class PngReader { @@ -14,19 +13,19 @@ public: ~PngReader(); void setPath(const std::string& path); - std::unique_ptr read(); + std::unique_ptr > read(); private: struct IHDRChunk { - unsigned width{0}; - unsigned height{0}; - char bitDepth{0}; - char colorType{0}; - char compressionMethod{0}; - char filterMethod{0}; - char interlaceMethod{0}; + unsigned width{0}; + unsigned height{0}; + char bitDepth{0}; + char colorType{0}; + char compressionMethod{0}; + char filterMethod{0}; + char interlaceMethod{0}; }; bool readChunk(); @@ -39,7 +38,7 @@ private: IHDRChunk mIHDRChunk; - std::unique_ptr mWorkingImage; + std::unique_ptr > mWorkingImage; std::unique_ptr mFile; std::string mPath; }; diff --git a/src/image/PngWriter.cpp b/src/image/PngWriter.cpp index 1b4c041..985c2de 100644 --- a/src/image/PngWriter.cpp +++ b/src/image/PngWriter.cpp @@ -29,7 +29,7 @@ void PngWriter::SetPath(const std::string& path) mImpl->setPath(path); } -void PngWriter::Write(const std::unique_ptr& image) const +void PngWriter::Write(const std::unique_ptr >& image) const { mImpl->write(image); //auto fp = fopen(mPath.c_str(), "wb"); diff --git a/src/image/PngWriter.h b/src/image/PngWriter.h index b99a587..b90f227 100644 --- a/src/image/PngWriter.h +++ b/src/image/PngWriter.h @@ -3,7 +3,8 @@ #include #include -class Image; +#include "Image.h" + class PngWriterImpl; class PngWriter @@ -15,7 +16,7 @@ public: void SetPath(const std::string& path); - void Write(const std::unique_ptr& image) const; + void Write(const std::unique_ptr >& image) const; private: std::unique_ptr mImpl; diff --git a/src/image/PngWriterBasic.cpp b/src/image/PngWriterBasic.cpp index 003c642..ef21abb 100644 --- a/src/image/PngWriterBasic.cpp +++ b/src/image/PngWriterBasic.cpp @@ -5,7 +5,7 @@ void PngWriterBasic::setPath(const std::string& path) } -void PngWriterBasic::write(const std::unique_ptr& image) const +void PngWriterBasic::write(const std::unique_ptr >& image) const { -} \ No newline at end of file +} diff --git a/src/image/PngWriterBasic.h b/src/image/PngWriterBasic.h index a883555..4fbbfdc 100644 --- a/src/image/PngWriterBasic.h +++ b/src/image/PngWriterBasic.h @@ -7,8 +7,8 @@ class PngWriterBasic : public PngWriterImpl public: void setPath(const std::string& path) override; - void write(const std::unique_ptr& image) const override; + void write(const std::unique_ptr>& image) const override; private: std::string mPath; -}; \ No newline at end of file +}; diff --git a/src/image/PngWriterImpl.h b/src/image/PngWriterImpl.h index 81dd933..084c4a7 100644 --- a/src/image/PngWriterImpl.h +++ b/src/image/PngWriterImpl.h @@ -3,7 +3,7 @@ #include #include -class Image; +#include "Image.h" class PngWriterImpl { @@ -12,5 +12,5 @@ public: virtual void setPath(const std::string& path) = 0; - virtual void write(const std::unique_ptr& image) const = 0; + virtual void write(const std::unique_ptr >& image) const = 0; }; diff --git a/src/ui_elements/desktop_elements/IPlatformWindow.h b/src/ui_elements/desktop_elements/IPlatformWindow.h index 73e5835..ca5d672 100644 --- a/src/ui_elements/desktop_elements/IPlatformWindow.h +++ b/src/ui_elements/desktop_elements/IPlatformWindow.h @@ -5,11 +5,25 @@ namespace mt { class Screen; + class Window; } -class IPlatformWindow +class IPlatformSurface { public: + virtual ~IPlatformSurface() = default; + virtual void paint(mt::Screen* screen) = 0; +}; + +class IPlatformWindow : public IPlatformSurface +{ +public: + IPlatformWindow(mt::Window* window) + : mWindow(window) + { + + } + virtual ~IPlatformWindow() = default; virtual void show() const = 0; @@ -17,8 +31,8 @@ public: virtual void map() const = 0; virtual void clear() const = 0; - - virtual void paint(mt::Screen* screen) const = 0; +protected: + mt::Window* mWindow{nullptr}; }; using IPlatformWindowPtr = std::unique_ptr; diff --git a/src/ui_elements/desktop_elements/Window.cpp b/src/ui_elements/desktop_elements/Window.cpp index bf04950..7036b22 100644 --- a/src/ui_elements/desktop_elements/Window.cpp +++ b/src/ui_elements/desktop_elements/Window.cpp @@ -14,7 +14,7 @@ namespace mt Window::Window() :mWidth(800), mHeight(600), - mBackingImage(std::make_unique(mWidth, mHeight)), + mBackingImage(std::make_unique >(mWidth, mHeight)), mWidget(Widget::Create()) { mWidget->setBounds(mWidth, mHeight); @@ -96,7 +96,7 @@ void Window::SetPlatformWindow(IPlatformWindowPtr window) mPlatformWindow = std::move(window); } -Image* Window::getBackingImage() const +Image* Window::getBackingImage() const { return mBackingImage.get(); } diff --git a/src/ui_elements/desktop_elements/Window.h b/src/ui_elements/desktop_elements/Window.h index 63c1661..19ec0c9 100644 --- a/src/ui_elements/desktop_elements/Window.h +++ b/src/ui_elements/desktop_elements/Window.h @@ -1,15 +1,16 @@ #pragma once #include "Widget.h" +#include "Image.h" #include #include +#include class PaintEvent; class MouseEvent; class KeyboardEvent; class VisualLayer; -class Image; class IPlatformWindow; using IPlatformWindowPtr = std::unique_ptr; @@ -51,7 +52,7 @@ public: void SetPlatformWindow(IPlatformWindowPtr window); - Image* getBackingImage() const; + Image* getBackingImage() const; void map(); @@ -67,7 +68,7 @@ private: unsigned mWidth {800}; unsigned mHeight {600}; IPlatformWindowPtr mPlatformWindow {nullptr}; - std::unique_ptr mBackingImage; + std::unique_ptr > mBackingImage; }; } diff --git a/src/windows/ui_interfaces/wayland/WaylandSurface.cpp b/src/windows/ui_interfaces/wayland/WaylandSurface.cpp index ff899ca..0eebde6 100644 --- a/src/windows/ui_interfaces/wayland/WaylandSurface.cpp +++ b/src/windows/ui_interfaces/wayland/WaylandSurface.cpp @@ -1,9 +1,10 @@ #include "WaylandSurface.h" #include "WaylandEglWindowInterface.h" +#include "ImagePrimitives.h" WaylandSurface::WaylandSurface(mt::Window* window) - : mWindow(window) + : IPlatformWindow(window) { } @@ -36,17 +37,22 @@ void WaylandSurface::initialize(wl_compositor* compositor, xdg_wm_base* xdg_wm_b mXdgTopLevel = xdg_surface_get_toplevel(mXdgSurface); xdg_toplevel_set_title(mXdgTopLevel, "Example client"); - wl_surface_commit(mSurface); - auto region = wl_compositor_create_region(compositor); wl_region_add(region, 0, 0, mWindow->GetWidth(), mWindow->GetHeight()); wl_surface_set_opaque_region(mSurface, region); + + wl_surface_commit(mSurface); } void WaylandSurface::onConfigure(xdg_surface *xdg_surface, uint32_t serial) { xdg_surface_ack_configure(xdg_surface, serial); + paint(nullptr); +} + +void WaylandSurface::paint(mt::Screen* screen) +{ if (mEglWindowInterface) { mEglWindowInterface->initialize(mSurface, mWindow->GetWidth(), mWindow->GetHeight()); @@ -77,29 +83,11 @@ wl_buffer* WaylandSurface::drawFrame() mBuffer->setUpPool(shm_pool_size, width, height, stride); int offset = 0; - drawCheckerboard(width, height, offset); + + ImagePrimitives::drawCheckerboard(mBuffer->getPoolData(), width, height, offset); mBuffer->tearDownPool(shm_pool_size); return mBuffer->getWorkingBuffer(); } -void WaylandSurface::drawCheckerboard(int width, int height, int offset) -{ - uint32_t *pixels = (uint32_t *)&(mBuffer->getPoolData())[offset]; - for (int y = 100; y < height; ++y) - { - for (int x = 0; x < width; ++x) - { - if ((x + y / 8 * 8) % 16 < 8) - { - pixels[y * width + x] = 0xFF666666; - } - else - { - pixels[y * width + x] = 0xFFEEEEEE; - } - } - } -} - diff --git a/src/windows/ui_interfaces/wayland/WaylandSurface.h b/src/windows/ui_interfaces/wayland/WaylandSurface.h index 558342b..670adcf 100644 --- a/src/windows/ui_interfaces/wayland/WaylandSurface.h +++ b/src/windows/ui_interfaces/wayland/WaylandSurface.h @@ -7,6 +7,8 @@ #include "SharedMemory.h" #include "WaylandBuffer.h" +#include "IPlatformWindow.h" + struct wl_surface; struct xdg_surface; struct xdg_toplevel; @@ -14,7 +16,7 @@ struct xdg_toplevel; class WaylandEglInterface; class WaylandEglWindowInterface; -class WaylandSurface +class WaylandSurface : public IPlatformWindow { public: @@ -28,9 +30,24 @@ public: wl_buffer* drawFrame(); - void drawCheckerboard(int width, int height, int offset); + void show() const + { + map(); + } + + void map() const + { + + } + + void clear() const + { + + } private: + void paint(mt::Screen* screen) override; + mt::Window* mWindow{nullptr}; wl_surface* mSurface{nullptr}; diff --git a/src/windows/ui_interfaces/x11/XcbWindow.cpp b/src/windows/ui_interfaces/x11/XcbWindow.cpp index 511c555..99423aa 100644 --- a/src/windows/ui_interfaces/x11/XcbWindow.cpp +++ b/src/windows/ui_interfaces/x11/XcbWindow.cpp @@ -9,8 +9,8 @@ #include XcbWindow::XcbWindow(mt::Window* window, int hwnd, xcb_connection_t* connection) - : mHandle(hwnd), - mWindow(window), + : IPlatformWindow(window), + mHandle(hwnd), mBackingImage(std::make_unique()), mConnection(connection) { @@ -58,7 +58,7 @@ void XcbWindow::show() const map(); } -void XcbWindow::paint(mt::Screen* screen) const +void XcbWindow::paint(mt::Screen* screen) { auto xcb_screen = dynamic_cast(screen->GetPlatformScreen()); for(const auto& layer : mWindow->GetLayers()) diff --git a/src/windows/ui_interfaces/x11/XcbWindow.h b/src/windows/ui_interfaces/x11/XcbWindow.h index 0a6a774..9be33b2 100644 --- a/src/windows/ui_interfaces/x11/XcbWindow.h +++ b/src/windows/ui_interfaces/x11/XcbWindow.h @@ -23,17 +23,15 @@ public: void show() const override; - void paint(mt::Screen* screen) const override; + void paint(mt::Screen* screen) override; void clear() const override; void map() const override; private: - int mHandle{-1}; unsigned mGraphicsContext {0}; - mt::Window* mWindow{nullptr}; xcb_connection_t* mConnection{nullptr}; std::unique_ptr mBackingImage; };