You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-24 15:26:15 +00:00
optimize ImageLoaderSVG::create_image_from_utf8_buffer
This commit is contained in:
@@ -104,51 +104,33 @@ Error ImageLoaderSVG::create_image_from_utf8_buffer(Ref<Image> p_image, const ui
|
|||||||
picture->size(width, height);
|
picture->size(width, height);
|
||||||
|
|
||||||
std::unique_ptr<tvg::SwCanvas> sw_canvas = tvg::SwCanvas::gen();
|
std::unique_ptr<tvg::SwCanvas> sw_canvas = tvg::SwCanvas::gen();
|
||||||
// Note: memalloc here, be sure to memfree before any return.
|
Vector<uint8_t> buffer;
|
||||||
uint32_t *buffer = (uint32_t *)memalloc(sizeof(uint32_t) * width * height);
|
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) {
|
if (res != tvg::Result::Success) {
|
||||||
memfree(buffer);
|
|
||||||
ERR_FAIL_V_MSG(FAILED, "ImageLoaderSVG: Couldn't set target on ThorVG canvas.");
|
ERR_FAIL_V_MSG(FAILED, "ImageLoaderSVG: Couldn't set target on ThorVG canvas.");
|
||||||
}
|
}
|
||||||
|
|
||||||
res = sw_canvas->push(std::move(picture));
|
res = sw_canvas->push(std::move(picture));
|
||||||
if (res != tvg::Result::Success) {
|
if (res != tvg::Result::Success) {
|
||||||
memfree(buffer);
|
|
||||||
ERR_FAIL_V_MSG(FAILED, "ImageLoaderSVG: Couldn't insert ThorVG picture on canvas.");
|
ERR_FAIL_V_MSG(FAILED, "ImageLoaderSVG: Couldn't insert ThorVG picture on canvas.");
|
||||||
}
|
}
|
||||||
|
|
||||||
res = sw_canvas->draw();
|
res = sw_canvas->draw();
|
||||||
if (res != tvg::Result::Success) {
|
if (res != tvg::Result::Success) {
|
||||||
memfree(buffer);
|
|
||||||
ERR_FAIL_V_MSG(FAILED, "ImageLoaderSVG: Couldn't draw ThorVG pictures on canvas.");
|
ERR_FAIL_V_MSG(FAILED, "ImageLoaderSVG: Couldn't draw ThorVG pictures on canvas.");
|
||||||
}
|
}
|
||||||
|
|
||||||
res = sw_canvas->sync();
|
res = sw_canvas->sync();
|
||||||
if (res != tvg::Result::Success) {
|
if (res != tvg::Result::Success) {
|
||||||
memfree(buffer);
|
|
||||||
ERR_FAIL_V_MSG(FAILED, "ImageLoaderSVG: Couldn't sync ThorVG canvas.");
|
ERR_FAIL_V_MSG(FAILED, "ImageLoaderSVG: Couldn't sync ThorVG canvas.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<uint8_t> image;
|
p_image->set_data(width, height, false, Image::FORMAT_RGBA8, buffer);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res = sw_canvas->clear(true);
|
res = sw_canvas->clear(true);
|
||||||
memfree(buffer);
|
|
||||||
|
|
||||||
p_image->set_data(width, height, false, Image::FORMAT_RGBA8, image);
|
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user