Skip to content

Commit

Permalink
sw_canvas target() and push() one time.
Browse files Browse the repository at this point in the history
free buffer and image after updating.
  • Loading branch information
beicause committed May 11, 2024
1 parent cf86109 commit 6e56a6c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
39 changes: 21 additions & 18 deletions modules/svg/lottie_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,19 @@ void LottieTexture2D::_update_image() {
}
picture->size(w, h);

image = Image::create_empty(w * _columns, h * _rows, false, Image::FORMAT_RGBA8);
buffer = (uint32_t *)(buffer == nullptr ? memalloc(sizeof(uint32_t) * w * h) : memrealloc(buffer, sizeof(uint32_t) * w * h));
Ref<Image> image = Image::create_empty(w * _columns, h * _rows, false, Image::FORMAT_RGBA8);
uint32_t *buffer = (uint32_t *)memalloc(sizeof(uint32_t) * w * h);

tvg::Result res = sw_canvas->target(buffer, w, w, h, tvg::SwCanvas::ARGB8888S);
if (res != tvg::Result::Success) {
memfree(buffer);
ERR_FAIL_MSG("LottieTexture2D: Couldn't set target on ThorVG canvas.");
}
res = sw_canvas->push(tvg::cast(picture));
if (res != tvg::Result::Success) {
memfree(buffer);
ERR_FAIL_MSG("LottieTexture2D: Couldn't insert ThorVG picture on canvas.");
}

for (int row = 0; row < _rows; row++) {
for (int column = 0; column < _columns; column++) {
Expand All @@ -93,32 +104,23 @@ void LottieTexture2D::_update_image() {
float progress = ((float)(row * _columns + column)) / frame_count;
float current_frame = frame_begin + (frame_end - frame_begin) * progress;

tvg::Result res = animation->frame(current_frame);
if (res == tvg::Result::Success) {
res = animation->frame(current_frame);
if (progress == 0 || res == tvg::Result::Success) {
sw_canvas->update(picture);
}
res = sw_canvas->target(buffer, w, w, h, tvg::SwCanvas::ARGB8888S);
if (res != tvg::Result::Success) {
ERR_FAIL_MSG("LottieTexture2D: Couldn't set target on ThorVG canvas.");
}

res = sw_canvas->push(tvg::cast(picture));
if (res != tvg::Result::Success) {
ERR_FAIL_MSG("LottieTexture2D: Couldn't insert ThorVG picture on canvas.");
}

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

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

res = sw_canvas->clear(true);

for (uint32_t y = 0; y < h; y++) {
for (uint32_t x = 0; x < w; x++) {
uint32_t n = buffer[y * w + x];
Expand All @@ -130,8 +132,12 @@ void LottieTexture2D::_update_image() {
image->set_pixel(x + w * column, y + h * row, color);
}
}
res = sw_canvas->clear(false);
}
}
memfree(buffer);
sw_canvas->clear(true);

if (texture.is_null()) {
texture = RenderingServer::get_singleton()->texture_2d_create(image);
} else {
Expand Down Expand Up @@ -225,9 +231,6 @@ LottieTexture2D::~LottieTexture2D() {
if (texture.is_valid()) {
RenderingServer::get_singleton()->free(texture);
}
if (buffer) {
memfree(buffer);
}
}

void LottieTexture2D::_bind_methods() {
Expand Down
24 changes: 17 additions & 7 deletions modules/svg/lottie_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ class LottieTexture2D : public Texture2D {
std::unique_ptr<tvg::SwCanvas> sw_canvas = tvg::SwCanvas::gen();
std::unique_ptr<tvg::Animation> animation = tvg::Animation::gen();
tvg::Picture *picture = animation->picture();
Ref<Image> image;
mutable RID texture;
uint32_t *buffer = nullptr;
Ref<JSON> json = nullptr;

float scale = 1.0;
Expand Down Expand Up @@ -93,12 +91,24 @@ class LottieTexture2D : public Texture2D {
return Size2(w, h);
}

int get_width() const override { return image.is_valid() ? image->get_width() : 0; };
int get_height() const override { return image.is_valid() ? image->get_height() : 0; };
Size2 get_size() const override { return image.is_valid() ? image->get_size() : Size2i(); };
virtual bool is_pixel_opaque(int p_x, int p_y) const override { return image.is_valid() ? image->get_pixel(p_x, p_y).a > 0.1 : true; };
int get_width() const override {
Ref<Image> image = get_image();
return image.is_valid() ? image->get_width() : 0;
};
int get_height() const override {
Ref<Image> image = get_image();
return image.is_valid() ? image->get_height() : 0;
};
Size2 get_size() const override {
Ref<Image> image = get_image();
return image.is_valid() ? image->get_size() : Size2i();
};
virtual bool is_pixel_opaque(int p_x, int p_y) const override {
Ref<Image> image = get_image();
return image.is_valid() ? image->get_pixel(p_x, p_y).a > 0.1 : true;
};
virtual bool has_alpha() const override { return true; };
virtual Ref<Image> get_image() const override { return image; };
virtual Ref<Image> get_image() const override { return texture.is_valid() ? RenderingServer::get_singleton()->texture_2d_get(texture) : Ref<Image>(); };
virtual RID get_rid() const override;

~LottieTexture2D();
Expand Down

0 comments on commit 6e56a6c

Please sign in to comment.