#include "surface.hpp"
#include "gpuMemory.hpp"
#include <cstring>

namespace render::kernel
{

Surface::Surface(uint32_t width, uint32_t height, uint32_t stride, SceGxmColorFormat format)
{
    this->data = gpuAllocator(
        SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW,
        SCE_GXM_MEMORY_ATTRIB_RW,
        ALIGN(4 * stride * height, 1 * 1024 * 1024),
        &this->uid);

    memset(this->data, 0, stride * height);

    sceGxmColorSurfaceInit(&this->surface, format,
        SCE_GXM_COLOR_SURFACE_LINEAR,
        SCE_GXM_COLOR_SURFACE_SCALE_NONE,
        SCE_GXM_OUTPUT_REGISTER_SIZE_32BIT,
        width,
        height,
        stride,
        this->data);

    sceGxmSyncObjectCreate(&this->object);
    
}

Surface::~Surface()
{
    // sceGxmDestroyRenderTarget(this->target);
}

SceGxmColorSurface *Surface::GetSurface()
{
    return &this->surface;
}

SceGxmSyncObject *Surface::GetObject()
{
    return this->object;
}

void *Surface::GetData()
{
    return this->data;
}

} // namespace render::kernel
