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    void destroyNativeWindow(AWindowBase& window) override;
   23    void beginPaint(AWindowBase& window) override;
   24    void endPaint(AWindowBase& window) override;
   25    void beginResize(AWindowBase& window) override;
   26    void init(const Init& init) override;
   27
   28    IRenderer& renderer() override;
   29
   30    AImage makeScreenshot() override;
   31
   32    inline uint8_t& stencil(const glm::uvec2& position) {
   33        return mStencilBlob.at<uint8_t>(mBitmapSize.x * position.y + position.x);
   34    }
   35
   36    [[nodiscard]]
   37    glm::uvec2 bitmapSize() const {
   38        return mBitmapSize;
   39    }
   40
   41    inline void putPixel(const glm::uvec2& position, const glm::u8vec3& color) noexcept {
   42        putPixel(position, glm::u8vec4(color, 255));
   43    }
   44#if AUI_PLATFORM_WIN
   45    inline void putPixel(const glm::uvec2& position, const glm::u8vec4& color) noexcept {
   46        AUI_ASSERTX(glm::all(glm::lessThan(position, mBitmapSize)), "image out of bounds");
   47
   48        auto dataPtr = reinterpret_cast<uint8_t*>(mBitmapBlob.data() + sizeof(BITMAPINFO)
   49            + (mBitmapSize.x * position.y + position.x) * 4);
   50        dataPtr[0] = color[2];
   51        dataPtr[1] = color[1];
   52        dataPtr[2] = color[0];
   53        dataPtr[3] = color[3];
   54    }
   55    inline glm::u8vec4 getPixel(const glm::uvec2& position) noexcept {
   56        AUI_ASSERTX(glm::all(glm::lessThan(position, mBitmapSize)), "image out of bounds");
   57
   58        auto dataPtr = reinterpret_cast<uint8_t*>(mBitmapBlob.data() + sizeof(BITMAPINFO)
   59                                                  + (mBitmapSize.x * position.y + position.x) * 4);
   60
   61        return { dataPtr[2], dataPtr[1], dataPtr[0], dataPtr[3] };
   62    }
   63#else
   64    inline void putPixel(const glm::uvec2& position, const glm::u8vec4& color) noexcept {
   65        AUI_ASSERTX(glm::all(glm::lessThan(position, mBitmapSize)), "image out of bounds");
   66
   67        auto dataPtr = reinterpret_cast<uint8_t*>(mBitmapBlob + (mBitmapSize.x * position.y + position.x) * 4);
   68        dataPtr[0] = color[2];
   69        dataPtr[1] = color[1];
   70        dataPtr[2] = color[0];
   71        dataPtr[3] = color[3];
   72    }
   73    inline glm::u8vec4 getPixel(const glm::uvec2& position) noexcept {
   74        AUI_ASSERTX(glm::all(glm::lessThan(position, mBitmapSize)), "image out of bounds");
   75
   76        auto dataPtr = reinterpret_cast<uint8_t*>(mBitmapBlob + (mBitmapSize.x * position.y + position.x) * 4);
   77        return {
   78            dataPtr[2],
   79            dataPtr[1],
   80            dataPtr[0],
   81            dataPtr[3],
   82        };
   83    }
   84#endif
   85
   86    void endResize(AWindowBase& window) override;
   87
   88protected:
   89    AByteBuffer mStencilBlob;
   90    glm::uvec2 mBitmapSize;
   91
   92    void reallocateImageBuffers(const AWindowBase& window);
   93
   94private:
   95#if AUI_PLATFORM_WIN
   96    AByteBuffer mBitmapBlob;
   97    BITMAPINFO* mBitmapInfo;
   98#endif
   99#if AUI_PLATFORM_LINUX
  100    std::uint8_t* mBitmapBlob = nullptr;
  101    _<XImage> mXImage;
  102    std::unique_ptr<_XGC, void(*)(GC)> mGC = {nullptr, nullptr};
  103
  104    void reallocate();
  105#endif
  106#if AUI_PLATFORM_ANDROID || AUI_PLATFORM_APPLE || AUI_PLATFORM_EMSCRIPTEN
  107    std::uint8_t* mBitmapBlob = nullptr;
  108#endif
  109};
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:28
Base class for rendering.
Definition IRenderer.h:149
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
#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