AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
SoftwareRenderingContext.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include <AUI/Platform/CommonRenderingContext.h>
   15#include <AUI/Platform/AWindow.h>
   16
   17class API_AUI_VIEWS SoftwareRenderingContext: public aui::noncopyable, public CommonRenderingContext {
   18public:
   19    SoftwareRenderingContext();
   20    ~SoftwareRenderingContext() override;
   21
   22#if !AUI_PLATFORM_LINUX
   23    // to be implemented by IPlatformAbstraction
   24    void init(const Init& init) override;
   25    void destroyNativeWindow(AWindowBase& window) override;
   26#endif
   27
   28    void beginPaint(AWindowBase& window) override;
   29    void endPaint(AWindowBase& window) override;
   30    void beginResize(AWindowBase& window) override;
   31
   32    IRenderer& renderer() override;
   33
   34    AImage makeScreenshot() override;
   35
   36    inline uint8_t& stencil(const glm::uvec2& position) {
   37        return mStencilBlob.at<uint8_t>(mBitmapSize.x * position.y + position.x);
   38    }
   39
   40    [[nodiscard]]
   41    glm::uvec2 bitmapSize() const {
   42        return mBitmapSize;
   43    }
   44
   45    inline void putPixel(const glm::uvec2& position, const glm::u8vec3& color) noexcept {
   46        putPixel(position, glm::u8vec4(color, 255));
   47    }
   48#if AUI_PLATFORM_WIN
   49    inline void putPixel(const glm::uvec2& position, const glm::u8vec4& color) noexcept {
   50        AUI_ASSERTX(glm::all(glm::lessThan(position, mBitmapSize)), "image out of bounds");
   51
   52        auto dataPtr = reinterpret_cast<uint8_t*>(mBitmapBlob.data() + sizeof(BITMAPINFO)
   53            + (mBitmapSize.x * position.y + position.x) * 4);
   54        dataPtr[0] = color[2];
   55        dataPtr[1] = color[1];
   56        dataPtr[2] = color[0];
   57        dataPtr[3] = color[3];
   58    }
   59    inline glm::u8vec4 getPixel(const glm::uvec2& position) noexcept {
   60        AUI_ASSERTX(glm::all(glm::lessThan(position, mBitmapSize)), "image out of bounds");
   61
   62        auto dataPtr = reinterpret_cast<uint8_t*>(mBitmapBlob.data() + sizeof(BITMAPINFO)
   63                                                  + (mBitmapSize.x * position.y + position.x) * 4);
   64
   65        return { dataPtr[2], dataPtr[1], dataPtr[0], dataPtr[3] };
   66    }
   67#else
   68    inline void putPixel(const glm::uvec2& position, const glm::u8vec4& color) noexcept {
   69        AUI_ASSERTX(glm::all(glm::lessThan(position, mBitmapSize)), "image out of bounds");
   70
   71        auto dataPtr = reinterpret_cast<uint8_t*>(mBitmapBlob + (mBitmapSize.x * position.y + position.x) * 4);
   72        dataPtr[0] = color[2];
   73        dataPtr[1] = color[1];
   74        dataPtr[2] = color[0];
   75        dataPtr[3] = color[3];
   76    }
   77    inline glm::u8vec4 getPixel(const glm::uvec2& position) noexcept {
   78        AUI_ASSERTX(glm::all(glm::lessThan(position, mBitmapSize)), "image out of bounds");
   79
   80        auto dataPtr = reinterpret_cast<uint8_t*>(mBitmapBlob + (mBitmapSize.x * position.y + position.x) * 4);
   81        return {
   82            dataPtr[2],
   83            dataPtr[1],
   84            dataPtr[0],
   85            dataPtr[3],
   86        };
   87    }
   88#endif
   89
   90    void endResize(AWindowBase& window) override;
   91
   92protected:
   93    AByteBuffer mStencilBlob;
   94    glm::uvec2 mBitmapSize;
   95#if AUI_PLATFORM_LINUX
   96    std::uint8_t* mBitmapBlob = nullptr;
   97#endif
   98
   99    void reallocate(const AWindowBase& window);
  100    virtual void reallocate();
  101
  102private:
  103#if AUI_PLATFORM_WIN
  104    AByteBuffer mBitmapBlob;
  105    BITMAPINFO* mBitmapInfo;
  106#endif
  107#if AUI_PLATFORM_ANDROID || AUI_PLATFORM_APPLE || AUI_PLATFORM_EMSCRIPTEN
  108    std::uint8_t* mBitmapBlob = nullptr;
  109#endif
  110};
std::vector-like growing array for byte storage.
Definition AByteBuffer.h:31
Owning image representation.
Definition AImage.h:25
Definition AWindowBase.h:33
Definition CommonRenderingContext.h:19
Base class for rendering.
Definition IRenderer.h:149
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74
Definition IRenderingContext.h:38
Forbids copy of your class.
Definition values.h:45