Skip to content

Commit 700e26a

Browse files
WhalesStateclayjohn
andcommitted
Make DPITexture::_update_texture gets called only once each frame.
Co-authored-by: Clay John <[email protected]>
1 parent 6efa557 commit 700e26a

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

editor/import/resource_importer_svg.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,14 @@ void ResourceImporterSVG::get_import_options(const String &p_path, List<ImportOp
7373
}
7474

7575
Error ResourceImporterSVG::import(ResourceUID::ID p_source_id, const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
76-
Ref<DPITexture> dpi_tex;
77-
dpi_tex.instantiate();
78-
7976
String source = FileAccess::get_file_as_string(p_source_file);
8077
ERR_FAIL_COND_V_MSG(source.is_empty(), ERR_CANT_OPEN, vformat("Cannot open file from path \"%s\".", p_source_file));
8178

8279
double base_scale = p_options["base_scale"];
8380
double saturation = p_options["saturation"];
8481
Dictionary color_map = p_options["color_map"];
8582

86-
dpi_tex->set_base_scale(base_scale);
87-
dpi_tex->set_saturation(saturation);
88-
dpi_tex->set_color_map(color_map);
89-
dpi_tex->set_source(source);
90-
83+
Ref<DPITexture> dpi_tex = DPITexture::create_from_string(source, base_scale, saturation, color_map);
9184
ERR_FAIL_COND_V_MSG(dpi_tex->get_rid().is_null(), ERR_CANT_OPEN, vformat("Failed loading SVG, unsupported or invalid SVG data in \"%s\".", p_source_file));
9285

9386
int flg = 0;

scene/resources/dpi_texture.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,34 +97,35 @@ void DPITexture::set_source(const String &p_source) {
9797
return;
9898
}
9999
source = p_source;
100-
_update_texture();
100+
_request_update();
101101
}
102102

103103
String DPITexture::get_source() const {
104104
return source;
105105
}
106106

107107
void DPITexture::set_base_scale(float p_scale) {
108+
p_scale = MAX(0.01, p_scale);
108109
if (base_scale == p_scale) {
109110
return;
110111
}
111-
ERR_FAIL_COND(p_scale <= 0.0);
112112

113113
base_scale = p_scale;
114-
_update_texture();
114+
_request_update();
115115
}
116116

117117
float DPITexture::get_base_scale() const {
118118
return base_scale;
119119
}
120120

121121
void DPITexture::set_saturation(float p_saturation) {
122+
p_saturation = CLAMP(p_saturation, 0.0, 1.0);
122123
if (saturation == p_saturation) {
123124
return;
124125
}
125126

126127
saturation = p_saturation;
127-
_update_texture();
128+
_request_update();
128129
}
129130

130131
float DPITexture::get_saturation() const {
@@ -140,7 +141,7 @@ void DPITexture::set_color_map(const Dictionary &p_color_map) {
140141
for (const Variant *E = color_map.next(); E; E = color_map.next(E)) {
141142
cmap[*E] = color_map[*E];
142143
}
143-
_update_texture();
144+
_request_update();
144145
}
145146

146147
Dictionary DPITexture::get_color_map() const {
@@ -196,12 +197,13 @@ RID DPITexture::_load_at_scale(double p_scale, bool p_set_size) const {
196197
if (err != OK) {
197198
return RID();
198199
}
199-
#else
200-
img = Image::create_empty(Math::round(16 * p_scale * base_scale), Math::round(16 * p_scale * base_scale), false, Image::FORMAT_RGBA8);
201-
#endif
202200
if (saturation != 1.0) {
203201
img->adjust_bcs(1.0, 1.0, saturation);
204202
}
203+
img->fix_alpha_edges();
204+
#else
205+
img = Image::create_empty(Math::round(16 * p_scale * base_scale), Math::round(16 * p_scale * base_scale), false, Image::FORMAT_RGBA8);
206+
#endif
205207

206208
Size2 current_size = size;
207209
if (p_set_size) {
@@ -244,6 +246,15 @@ void DPITexture::_clear() {
244246
void DPITexture::_update_texture() {
245247
_clear();
246248
emit_changed();
249+
update_requested = false;
250+
}
251+
252+
void DPITexture::_request_update() {
253+
if (update_requested) {
254+
return;
255+
}
256+
update_requested = true;
257+
callable_mp(this, &DPITexture::_update_texture).call_deferred();
247258
}
248259

249260
Ref<Image> DPITexture::get_image() const {

scene/resources/dpi_texture.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class DPITexture : public Texture2D {
4444
float saturation = 1.0;
4545
Dictionary color_map;
4646
Size2 size_override;
47+
bool update_requested = false;
4748

4849
struct ScalingLevel {
4950
HashSet<DPITexture *> textures;
@@ -62,6 +63,7 @@ class DPITexture : public Texture2D {
6263
void _remove_scale(double p_scale);
6364
RID _ensure_scale(double p_scale) const;
6465
RID _load_at_scale(double p_scale, bool p_set_size) const;
66+
void _request_update();
6567
void _update_texture();
6668
void _clear();
6769

0 commit comments

Comments
 (0)