Skip to content

Commit

Permalink
optimize ImageLoaderSVG::create_image_from_utf8_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
beru committed Oct 19, 2024
1 parent 9a49cab commit 141f75e
Showing 1 changed file with 4 additions and 22 deletions.
26 changes: 4 additions & 22 deletions modules/svg/image_loader_svg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,51 +106,33 @@ Error ImageLoaderSVG::create_image_from_utf8_buffer(Ref<Image> p_image, const ui
picture->size(width, height);

std::unique_ptr<tvg::SwCanvas> sw_canvas = tvg::SwCanvas::gen();
// Note: memalloc here, be sure to memfree before any return.
uint32_t *buffer = (uint32_t *)memalloc(sizeof(uint32_t) * width * height);
Vector<uint8_t> buffer;
buffer.resize(sizeof(uint32_t) * width * height);

tvg::Result res = sw_canvas->target(buffer, width, width, height, tvg::SwCanvas::ARGB8888S);
tvg::Result res = sw_canvas->target((uint32_t *)buffer.ptrw(), width, width, height, tvg::SwCanvas::ABGR8888S);
if (res != tvg::Result::Success) {
memfree(buffer);
ERR_FAIL_V_MSG(FAILED, "ImageLoaderSVG: Couldn't set target on ThorVG canvas.");
}

res = sw_canvas->push(std::move(picture));
if (res != tvg::Result::Success) {
memfree(buffer);
ERR_FAIL_V_MSG(FAILED, "ImageLoaderSVG: Couldn't insert ThorVG picture on canvas.");
}

res = sw_canvas->draw();
if (res != tvg::Result::Success) {
memfree(buffer);
ERR_FAIL_V_MSG(FAILED, "ImageLoaderSVG: Couldn't draw ThorVG pictures on canvas.");
}

res = sw_canvas->sync();
if (res != tvg::Result::Success) {
memfree(buffer);
ERR_FAIL_V_MSG(FAILED, "ImageLoaderSVG: Couldn't sync ThorVG canvas.");
}

Vector<uint8_t> image;
image.resize(width * height * sizeof(uint32_t));

for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
uint32_t n = buffer[y * width + x];
const size_t offset = sizeof(uint32_t) * width * y + sizeof(uint32_t) * x;
image.write[offset + 0] = (n >> 16) & 0xff;
image.write[offset + 1] = (n >> 8) & 0xff;
image.write[offset + 2] = n & 0xff;
image.write[offset + 3] = (n >> 24) & 0xff;
}
}
p_image->set_data(width, height, false, Image::FORMAT_RGBA8, buffer);

res = sw_canvas->clear(true);
memfree(buffer);

p_image->set_data(width, height, false, Image::FORMAT_RGBA8, image);
return OK;
}

Expand Down

0 comments on commit 141f75e

Please sign in to comment.