Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/dpi_texture.cpp
9903 views
1
/**************************************************************************/
2
/* dpi_texture.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "dpi_texture.h"
32
33
#include "core/io/image_loader.h"
34
#include "scene/main/canvas_item.h"
35
#include "scene/main/viewport.h"
36
#include "scene/resources/bit_map.h"
37
#include "scene/resources/placeholder_textures.h"
38
39
#include "modules/modules_enabled.gen.h" // For svg.
40
#ifdef MODULE_SVG_ENABLED
41
#include "modules/svg/image_loader_svg.h"
42
#endif
43
44
Mutex DPITexture::mutex;
45
HashMap<double, DPITexture::ScalingLevel> DPITexture::scaling_levels;
46
47
void DPITexture::reference_scaling_level(double p_scale) {
48
uint32_t oversampling = CLAMP(p_scale, 0.1, 100.0) * 64;
49
if (oversampling == 64) {
50
return;
51
}
52
double scale = double(oversampling) / 64.0;
53
54
MutexLock lock(mutex);
55
ScalingLevel *sl = scaling_levels.getptr(scale);
56
if (sl) {
57
sl->refcount++;
58
} else {
59
ScalingLevel new_sl;
60
scaling_levels.insert(scale, new_sl);
61
}
62
}
63
64
void DPITexture::unreference_scaling_level(double p_scale) {
65
uint32_t oversampling = CLAMP(p_scale, 0.1, 100.0) * 64;
66
if (oversampling == 64) {
67
return;
68
}
69
double scale = double(oversampling) / 64.0;
70
71
MutexLock lock(mutex);
72
ScalingLevel *sl = scaling_levels.getptr(scale);
73
if (sl) {
74
sl->refcount--;
75
if (sl->refcount == 0) {
76
for (DPITexture *tx : sl->textures) {
77
tx->_remove_scale(scale);
78
}
79
sl->textures.clear();
80
scaling_levels.erase(scale);
81
}
82
}
83
}
84
85
Ref<DPITexture> DPITexture::create_from_string(const String &p_source, float p_scale, float p_saturation, const Dictionary &p_color_map) {
86
Ref<DPITexture> dpi_texture;
87
dpi_texture.instantiate();
88
dpi_texture->set_source(p_source);
89
dpi_texture->set_base_scale(p_scale);
90
dpi_texture->set_saturation(p_saturation);
91
dpi_texture->set_color_map(p_color_map);
92
return dpi_texture;
93
}
94
95
void DPITexture::set_source(const String &p_source) {
96
if (source == p_source) {
97
return;
98
}
99
source = p_source;
100
_update_texture();
101
}
102
103
String DPITexture::get_source() const {
104
return source;
105
}
106
107
void DPITexture::set_base_scale(float p_scale) {
108
if (base_scale == p_scale) {
109
return;
110
}
111
ERR_FAIL_COND(p_scale <= 0.0);
112
113
base_scale = p_scale;
114
_update_texture();
115
}
116
117
float DPITexture::get_base_scale() const {
118
return base_scale;
119
}
120
121
void DPITexture::set_saturation(float p_saturation) {
122
if (saturation == p_saturation) {
123
return;
124
}
125
126
saturation = p_saturation;
127
_update_texture();
128
}
129
130
float DPITexture::get_saturation() const {
131
return saturation;
132
}
133
134
void DPITexture::set_color_map(const Dictionary &p_color_map) {
135
if (color_map == p_color_map) {
136
return;
137
}
138
color_map = p_color_map;
139
cmap.clear();
140
for (const Variant *E = color_map.next(); E; E = color_map.next(E)) {
141
cmap[*E] = color_map[*E];
142
}
143
_update_texture();
144
}
145
146
Dictionary DPITexture::get_color_map() const {
147
return color_map;
148
}
149
150
void DPITexture::_remove_scale(double p_scale) {
151
if (Math::is_equal_approx(p_scale, 1.0)) {
152
return;
153
}
154
155
RID *rid = texture_cache.getptr(p_scale);
156
if (rid) {
157
if (rid->is_valid()) {
158
RenderingServer::get_singleton()->free(*rid);
159
}
160
texture_cache.erase(p_scale);
161
}
162
}
163
164
RID DPITexture::_ensure_scale(double p_scale) const {
165
uint32_t oversampling = CLAMP(p_scale, 0.1, 100.0) * 64;
166
if (oversampling == 64) {
167
if (base_texture.is_null()) {
168
base_texture = _load_at_scale(p_scale, true);
169
}
170
return base_texture;
171
}
172
double scale = double(oversampling) / 64.0;
173
174
RID *rid = texture_cache.getptr(scale);
175
if (rid) {
176
return *rid;
177
}
178
179
MutexLock lock(mutex);
180
ScalingLevel *sl = scaling_levels.getptr(scale);
181
ERR_FAIL_NULL_V_MSG(sl, RID(), "Invalid scaling level");
182
sl->textures.insert(const_cast<DPITexture *>(this));
183
184
RID new_rid = _load_at_scale(scale, false);
185
texture_cache[scale] = new_rid;
186
return new_rid;
187
}
188
189
RID DPITexture::_load_at_scale(double p_scale, bool p_set_size) const {
190
Ref<Image> img;
191
img.instantiate();
192
#ifdef MODULE_SVG_ENABLED
193
const bool upsample = !Math::is_equal_approx(Math::round(p_scale * base_scale), p_scale * base_scale);
194
195
Error err = ImageLoaderSVG::create_image_from_string(img, source, p_scale * base_scale, upsample, cmap);
196
if (err != OK) {
197
return RID();
198
}
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
202
if (saturation != 1.0) {
203
img->adjust_bcs(1.0, 1.0, saturation);
204
}
205
206
Size2 current_size = size;
207
if (p_set_size) {
208
size.x = img->get_width();
209
base_size.x = img->get_width();
210
if (size_override.x != 0) {
211
size.x = size_override.x;
212
}
213
size.y = img->get_height();
214
base_size.y = img->get_height();
215
if (size_override.y != 0) {
216
size.y = size_override.y;
217
}
218
current_size = size;
219
}
220
if (current_size.is_zero_approx()) {
221
current_size.x = img->get_width();
222
current_size.y = img->get_height();
223
}
224
225
RID rid = RenderingServer::get_singleton()->texture_2d_create(img);
226
RenderingServer::get_singleton()->texture_set_size_override(rid, current_size.x, current_size.y);
227
return rid;
228
}
229
230
void DPITexture::_clear() {
231
for (KeyValue<double, RID> &tx : texture_cache) {
232
if (tx.value.is_valid()) {
233
RenderingServer::get_singleton()->free(tx.value);
234
}
235
}
236
texture_cache.clear();
237
if (base_texture.is_valid()) {
238
RenderingServer::get_singleton()->free(base_texture);
239
}
240
base_texture = RID();
241
alpha_cache.unref();
242
}
243
244
void DPITexture::_update_texture() {
245
_clear();
246
emit_changed();
247
}
248
249
Ref<Image> DPITexture::get_image() const {
250
RID rid = _ensure_scale(1.0);
251
if (rid.is_valid()) {
252
return RenderingServer::get_singleton()->texture_2d_get(rid);
253
} else {
254
return Ref<Image>();
255
}
256
}
257
258
int DPITexture::get_width() const {
259
_ensure_scale(1.0);
260
return size.x;
261
}
262
263
int DPITexture::get_height() const {
264
_ensure_scale(1.0);
265
return size.y;
266
}
267
268
RID DPITexture::get_rid() const {
269
return _ensure_scale(1.0);
270
}
271
272
bool DPITexture::has_alpha() const {
273
return true;
274
}
275
276
RID DPITexture::get_scaled_rid() const {
277
double scale = 1.0;
278
CanvasItem *ci = CanvasItem::get_current_item_drawn();
279
if (ci) {
280
Viewport *vp = ci->get_viewport();
281
if (vp) {
282
scale = vp->get_oversampling();
283
}
284
}
285
return _ensure_scale(scale);
286
}
287
288
void DPITexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
289
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, size), get_scaled_rid(), false, p_modulate, p_transpose);
290
}
291
292
void DPITexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
293
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, get_scaled_rid(), p_tile, p_modulate, p_transpose);
294
}
295
296
void DPITexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const {
297
RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, get_scaled_rid(), p_src_rect, p_modulate, p_transpose, p_clip_uv);
298
}
299
300
bool DPITexture::is_pixel_opaque(int p_x, int p_y) const {
301
if (alpha_cache.is_null()) {
302
Ref<Image> img = get_image();
303
if (img.is_valid()) {
304
alpha_cache.instantiate();
305
alpha_cache->create_from_image_alpha(img);
306
}
307
}
308
309
if (alpha_cache.is_valid()) {
310
int aw = int(alpha_cache->get_size().width);
311
int ah = int(alpha_cache->get_size().height);
312
if (aw == 0 || ah == 0) {
313
return true;
314
}
315
316
int x = p_x * aw / size.x;
317
int y = p_y * ah / size.y;
318
319
x = CLAMP(x, 0, aw - 1);
320
y = CLAMP(y, 0, ah - 1);
321
322
return alpha_cache->get_bit(x, y);
323
}
324
325
return true;
326
}
327
328
void DPITexture::set_size_override(const Size2i &p_size) {
329
if (size_override == p_size) {
330
return;
331
}
332
size_override = p_size;
333
if (size_override.x == 0 || size_override.y == 0) {
334
_ensure_scale(1.0);
335
size = base_size;
336
}
337
if (size_override.x != 0) {
338
size.x = size_override.x;
339
}
340
if (size_override.y != 0) {
341
size.y = size_override.y;
342
}
343
for (KeyValue<double, RID> &tx : texture_cache) {
344
if (tx.value.is_valid()) {
345
RenderingServer::get_singleton()->texture_set_size_override(tx.value, size.x, size.y);
346
}
347
}
348
if (base_texture.is_valid()) {
349
RenderingServer::get_singleton()->texture_set_size_override(base_texture, size.x, size.y);
350
}
351
352
emit_changed();
353
}
354
355
void DPITexture::_bind_methods() {
356
ClassDB::bind_static_method("DPITexture", D_METHOD("create_from_string", "source", "scale", "saturation", "color_map"), &DPITexture::create_from_string, DEFVAL(1.0), DEFVAL(1.0), DEFVAL(Dictionary()));
357
358
ClassDB::bind_method(D_METHOD("set_source", "source"), &DPITexture::set_source);
359
ClassDB::bind_method(D_METHOD("get_source"), &DPITexture::get_source);
360
ClassDB::bind_method(D_METHOD("set_base_scale", "base_scale"), &DPITexture::set_base_scale);
361
ClassDB::bind_method(D_METHOD("get_base_scale"), &DPITexture::get_base_scale);
362
ClassDB::bind_method(D_METHOD("set_saturation", "saturation"), &DPITexture::set_saturation);
363
ClassDB::bind_method(D_METHOD("get_saturation"), &DPITexture::get_saturation);
364
ClassDB::bind_method(D_METHOD("set_color_map", "color_map"), &DPITexture::set_color_map);
365
ClassDB::bind_method(D_METHOD("get_color_map"), &DPITexture::get_color_map);
366
ClassDB::bind_method(D_METHOD("set_size_override", "size"), &DPITexture::set_size_override);
367
ClassDB::bind_method(D_METHOD("get_scaled_rid"), &DPITexture::get_scaled_rid);
368
369
ADD_PROPERTY(PropertyInfo(Variant::STRING, "_source", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_STORAGE), "set_source", "get_source");
370
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "base_scale", PROPERTY_HINT_RANGE, "0.01,10.0,0.01"), "set_base_scale", "get_base_scale");
371
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "saturation", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_saturation", "get_saturation");
372
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "color_map", PROPERTY_HINT_DICTIONARY_TYPE, "Color;Color"), "set_color_map", "get_color_map");
373
}
374
375
DPITexture::~DPITexture() {
376
_clear();
377
378
MutexLock lock(mutex);
379
for (KeyValue<double, ScalingLevel> &sl : scaling_levels) {
380
sl.value.textures.erase(this);
381
}
382
}
383
384