Blender V5.3
allocimbuf.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9/* It's become a bit messy... Basically, only the IMB_ prefixed files
10 * should remain. */
11
12#include <algorithm>
13#include <cstddef>
14
16#include "IMB_imbuf.hh"
17#include "IMB_imbuf_types.hh"
18#include "IMB_partial_update.hh"
19
21#include "IMB_metadata.hh"
22
23#include "imbuf.hh"
24
25#include "MEM_guardedalloc.h"
26
27#include "GPU_context.hh"
28#include "GPU_state.hh"
29#include "GPU_texture.hh"
30
31#include "OCIO_colorspace.hh"
32
33#include "CLG_log.h"
34
35#include "atomic_ops.h"
36
37namespace blender {
38
39static CLG_LogRef LOG = {"image.buffer"};
40
41/* Allocate pixel storage of the given buffer. The buffer owns the allocated memory.
42 * Returns true of allocation succeeded, false otherwise. */
43template<class BufferType>
44bool imb_alloc_buffer(BufferType &buffer,
45 const uint x,
46 const uint y,
47 const uint channels,
48 const size_t type_size,
49 bool initialize_pixels)
50{
51 void *data = imb_alloc_pixels(x, y, channels, type_size, initialize_pixels, __func__);
52 if (!data) {
53 return false;
54 }
55 buffer.data = static_cast<decltype(BufferType::data)>(data);
56 buffer.sharing_info = ImplicitSharingPtr<>(implicit_sharing::info_for_mem_free(buffer.data));
57 return true;
58}
59
61{
62 if (!this->byte_buffer.data) {
63 return nullptr;
64 }
65 if (this->byte_buffer.sharing_info->is_mutable()) {
66 this->byte_buffer.sharing_info->tag_ensured_mutable();
67 }
68 else {
69 const size_t size = size_t(this->x) * size_t(this->y) * 4;
70 uint8_t *new_data = MEM_new_array_uninitialized<uint8_t>(size, __func__);
71 std::copy_n(this->byte_buffer.data, size, new_data);
72 this->byte_buffer.data = new_data;
75 }
76 return const_cast<uint8_t *>(this->byte_buffer.data);
77}
78
80{
81 if (!this->float_buffer.data) {
82 return nullptr;
83 }
84 if (this->float_buffer.sharing_info->is_mutable()) {
85 this->float_buffer.sharing_info->tag_ensured_mutable();
86 }
87 else {
88 const size_t size = size_t(this->x) * size_t(this->y) * this->channels;
89 float *new_data = MEM_new_array_uninitialized<float>(size, __func__);
90 std::copy_n(this->float_buffer.data, size, new_data);
91 this->float_buffer.data = new_data;
94 }
95 return const_cast<float *>(this->float_buffer.data);
96}
97
107
117
119{
120 if (ibuf == nullptr) {
121 return;
122 }
123 /* Keep the colorspace, it's used when new pixels are assigned. */
124 ibuf->float_buffer.data = nullptr;
126}
127
129{
130 if (ibuf == nullptr) {
131 return;
132 }
133 /* Keep the colorspace, it's used when new pixels are assigned. */
134 ibuf->byte_buffer.data = nullptr;
136}
137
139{
142}
143
145{
146 if (ibuf == nullptr) {
147 return;
148 }
149
150 bool needs_free = atomic_sub_and_fetch_int32(&ibuf->refcounter, 1) < 0;
151 if (needs_free) {
152 /* Include this check here as the path may be manipulated after creation. */
153 BLI_assert_msg(!(ibuf->filepath[0] == '/' && ibuf->filepath[1] == '/'),
154 "'.blend' relative \"//\" must not be used in ImBuf!");
155
156 IMB_free_all_data(ibuf);
159 MEM_delete(ibuf);
160 }
161}
162
164{
166}
167
169{
170 if (ibuf == nullptr) {
171 return nullptr;
172 }
173
174 const bool is_single = (atomic_load_int32(&ibuf->refcounter) == 0);
175 if (is_single) {
176 return ibuf;
177 }
178
179 ImBuf *rval = IMB_dupImBuf(ibuf);
180
181 IMB_freeImBuf(ibuf);
182
183 return rval;
184}
185
187 uint x, uint y, uint channels, size_t typesize, bool initialize_pixels, const char *alloc_name)
188{
189 /* Protect against buffer overflow vulnerabilities from files specifying
190 * a width and height that overflow and alloc too little memory. */
191 if (!(uint64_t(x) * uint64_t(y) < (SIZE_MAX / (channels * typesize)))) {
192 return nullptr;
193 }
194
195 size_t size = size_t(x) * size_t(y) * size_t(channels) * typesize;
196 return initialize_pixels ? MEM_new_zeroed(size, alloc_name) :
197 MEM_new_uninitialized(size, alloc_name);
198}
199
200bool IMB_alloc_float_pixels(ImBuf *ibuf, const uint channels, bool initialize_pixels)
201{
202 if (ibuf == nullptr) {
203 return false;
204 }
205
207
208 if (!imb_alloc_buffer(
209 ibuf->float_buffer, ibuf->x, ibuf->y, channels, sizeof(float), initialize_pixels))
210 {
211 return false;
212 }
213
214 ibuf->channels = channels;
215
216 return true;
217}
218
219bool IMB_alloc_byte_pixels(ImBuf *ibuf, bool initialize_pixels)
220{
221 /* Question; why also add ZBUF (when `planes > 32`)? */
222
223 if (ibuf == nullptr) {
224 return false;
225 }
226
228
229 if (!imb_alloc_buffer(
230 ibuf->byte_buffer, ibuf->x, ibuf->y, 4, sizeof(uint8_t), initialize_pixels))
231 {
232 return false;
233 }
234
235 return true;
236}
237
247
257
259{
260 if (this->float_buffer.data) {
261 return this->float_buffer.colorspace && this->float_buffer.colorspace->is_data();
262 }
263 return this->byte_buffer.colorspace && this->byte_buffer.colorspace->is_data();
264}
265
267{
268 BLI_assert(data != nullptr);
269 BLI_assert(sharing_ptr.get() != nullptr);
270 this->byte_buffer.data = data;
271 this->byte_buffer.sharing_info = std::move(sharing_ptr);
272}
273
275{
276 BLI_assert(data != nullptr);
277 BLI_assert(sharing_ptr.get() != nullptr);
278 this->float_buffer.data = data;
279 this->float_buffer.sharing_info = std::move(sharing_ptr);
280}
281
283{
284 if (!ibuf || !ibuf->gpu.texture) {
285 return;
286 }
287
288 /* The host buffers are already up-to-date. */
289 if (!(ibuf->userflags & IB_HOST_BUFFER_INVALID)) {
290 return;
291 }
293
294 const bool need_secondary_context = !GPU_context_active_get();
295 if (need_secondary_context) {
297 }
298
300 float *output_buffer = static_cast<float *>(
302 ibuf->assign_float_data(output_buffer);
303
304 if (need_secondary_context) {
306 }
307}
308
310 uint8_t *byte_buffer, float *float_buffer, uint w, uint h, uint channels)
311{
312 if (!(byte_buffer || float_buffer)) {
313 return nullptr;
314 }
315
317
318 ibuf->channels = channels;
319
320 if (float_buffer) {
321 /* TODO(sergey): The 4 channels is the historical code. Should probably be `channels`, but
322 * needs a dedicated investigation. */
323 BLI_assert(MEM_allocN_len(float_buffer) == sizeof(float[4]) * w * h);
324 ibuf->assign_float_data(float_buffer);
325 }
326
327 if (byte_buffer) {
328 BLI_assert(MEM_allocN_len(byte_buffer) == sizeof(uint8_t[4]) * w * h);
329 ibuf->assign_byte_data(byte_buffer);
330 }
331
332 return ibuf;
333}
334
336 const uint8_t *byte_buffer, const float *float_buffer, uint w, uint h, uint channels)
337{
338 ImBuf *ibuf = nullptr;
339
340 if (!(byte_buffer || float_buffer)) {
341 return nullptr;
342 }
343
345
346 ibuf->channels = channels;
347
348 /* NOTE: Avoid #MEM_dupalloc since the buffers might not be allocated using guarded-allocation.
349 */
350 if (float_buffer) {
351 /* TODO(sergey): The 4 channels is the historical code. Should probably be `channels`, but
352 * needs a dedicated investigation. */
353 imb_alloc_buffer(ibuf->float_buffer, w, h, 4, sizeof(float), false);
354
355 memcpy(ibuf->float_data_for_write(), float_buffer, sizeof(float[4]) * w * h);
356 }
357
358 if (byte_buffer) {
359 imb_alloc_buffer(ibuf->byte_buffer, w, h, 4, sizeof(uint8_t), false);
360
361 memcpy(ibuf->byte_data_for_write(), byte_buffer, sizeof(uint8_t[4]) * w * h);
362 }
363
364 return ibuf;
365}
366
368{
369 ImBuf *ibuf = MEM_new<ImBuf>("ImBuf_struct");
370
371 if (ibuf) {
372 if (!IMB_initImBuf(ibuf, x, y, flags)) {
373 IMB_freeImBuf(ibuf);
374 return nullptr;
375 }
376 }
377
378 return ibuf;
379}
380
382{
383 ibuf->~ImBuf();
384 new (ibuf) ImBuf();
385
386 ibuf->x = x;
387 ibuf->y = y;
389 ibuf->ftype = IMB_FTYPE_PNG;
390 /* float option, is set to other values when buffers get assigned. */
391 ibuf->channels = 4;
392 /* IMB_DPI_DEFAULT -> pixels-per-meter. */
393 ibuf->ppm[0] = ibuf->ppm[1] = IMB_DPI_DEFAULT / 0.0254;
395
396 const bool init_pixels = !flag_is_set(flags, ImBufFlags::UninitializedPixels);
397
398 if (flag_is_set(flags, ImBufFlags::ByteData)) {
399 if (!IMB_alloc_byte_pixels(ibuf, init_pixels)) {
400 return false;
401 }
402 }
403
404 if (flag_is_set(flags, ImBufFlags::FloatData)) {
405 if (!IMB_alloc_float_pixels(ibuf, ibuf->channels, init_pixels)) {
406 return false;
407 }
408 }
409
410 /* assign default spaces */
412
413 return true;
414}
415
417{
418 if (ibuf1 == nullptr) {
419 return nullptr;
420 }
421
422 ImBuf *ibuf2 = IMB_allocImBuf(ibuf1->x, ibuf1->y, ImBufFlags::Zero);
423 if (ibuf2 == nullptr) {
424 return nullptr;
425 }
426 ibuf2->x = ibuf1->x;
427 ibuf2->y = ibuf1->y;
428 ibuf2->display_size[0] = ibuf1->display_size[0];
429 ibuf2->display_size[1] = ibuf1->display_size[1];
430 ibuf2->data_offset[0] = ibuf1->data_offset[0];
431 ibuf2->data_offset[1] = ibuf1->data_offset[1];
432 ibuf2->display_offset[0] = ibuf1->display_offset[0];
433 ibuf2->display_offset[1] = ibuf1->display_offset[1];
434 ibuf2->color_mode = ibuf1->color_mode;
435 ibuf2->channels = ibuf1->channels;
436 ibuf2->flags = ibuf1->flags;
437 ibuf2->byte_buffer = ibuf1->byte_buffer;
438 ibuf2->float_buffer = ibuf1->float_buffer;
439 /* GPU textures can not be easily copied, as it is not guaranteed that this function is called
440 * from within an active GPU context. */
441 ibuf2->gpu.texture = nullptr;
442 ibuf2->ppm[0] = ibuf1->ppm[0];
443 ibuf2->ppm[1] = ibuf1->ppm[1];
444 ibuf2->dither = ibuf1->dither;
445 ibuf2->userflags = ibuf1->userflags;
446 ibuf2->userflags = ibuf1->userflags;
447 ibuf2->metadata_ptr = ibuf1->metadata_ptr;
449 ibuf2->ftype = ibuf1->ftype;
450 ibuf2->foptions = ibuf1->foptions;
451 ibuf2->filepath = ibuf1->filepath;
452 ibuf2->fileframe = ibuf1->fileframe;
453 ibuf2->refcounter = 0;
454
455 return ibuf2;
456}
457
458size_t IMB_get_pixel_count(const ImBuf *ibuf)
459{
460 return size_t(ibuf->x) * size_t(ibuf->y);
461}
462
463size_t IMB_get_size_in_memory(const ImBuf *ibuf)
464{
465 size_t size = 0, channel_size = 0;
466
467 size += sizeof(ImBuf);
468
469 if (ibuf->byte_data()) {
470 channel_size += sizeof(char);
471 }
472
473 if (ibuf->float_data()) {
474 channel_size += sizeof(float);
475 }
476
477 size += channel_size * IMB_get_pixel_count(ibuf) * size_t(ibuf->channels);
478
479 return size;
480}
481
483{
484 switch (channels) {
485 case 1:
486 return ImColorMode::BW;
487 case 2:
488 return ImColorMode::BW_A;
489 case 3:
490 return ImColorMode::RGB;
491 case 4:
492 return ImColorMode::RGBA;
493 }
494 return ImColorMode::RGBA;
495}
496
498{
499 return chan_id == "RGB" || chan_id == "RGBA" || chan_id == "RA" || chan_id == "BA" ||
500 chan_id == "GA" || chan_id == "R" || chan_id == "G" || chan_id == "B" || chan_id == "A";
501}
502
503} // namespace blender
#define SIZE_MAX
Read Blender memory allocation and freeing.
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATOMIC_INLINE int32_t atomic_add_and_fetch_int32(int32_t *p, int32_t x)
ATOMIC_INLINE int32_t atomic_load_int32(const int32_t *v)
ATOMIC_INLINE int32_t atomic_sub_and_fetch_int32(int32_t *p, int32_t x)
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
virtual bool is_data() const =0
#define IMB_DPI_DEFAULT
Definition imbuf.hh:26
const ImplicitSharingInfo * info_for_mem_free(T *data)
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void IMB_activate_gpu_context()
Definition module.cc:53
unsigned __int64 uint64_t
Definition BKE_text.h:91
ImBuf * IMB_makeSingleUser(ImBuf *ibuf)
void IMB_free_byte_pixels(ImBuf *ibuf)
ImBuf * IMB_allocFromBufferOwn(uint8_t *byte_buffer, float *float_buffer, unsigned int w, unsigned int h, unsigned int channels)
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool IMB_chan_id_is_color(StringRef chan_id)
size_t IMB_get_pixel_count(const ImBuf *ibuf)
Get the length of the data of the given image buffer in pixels.
bool IMB_alloc_byte_pixels(ImBuf *ibuf, bool initialize_pixels=true)
void IMB_refImBuf(ImBuf *ibuf)
bool IMB_initImBuf(ImBuf *ibuf, unsigned int x, unsigned int y, ImBufFlags flags)
size_t IMB_get_size_in_memory(const ImBuf *ibuf)
ImBuf * IMB_dupImBuf(const ImBuf *ibuf1)
void * imb_alloc_pixels(unsigned int x, unsigned int y, unsigned int channels, size_t typesize, bool initialize_pixels, const char *alloc_name)
void IMB_partial_update_free(ImBuf *ibuf)
void IMB_freeImBuf(ImBuf *ibuf)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
ImColorMode IMB_color_mode_from_channels(int channels)
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
const char * IMB_colormanagement_role_colorspace_name_get(int role)
BMesh const char void * data
unsigned char uint8_t
Definition BKE_text.h:79
void IMB_ensure_host_buffer(ImBuf *ibuf)
@ GPU_BARRIER_TEXTURE_UPDATE
Definition GPU_state.hh:42
bool imb_alloc_buffer(BufferType &buffer, const uint x, const uint y, const uint channels, const size_t type_size, bool initialize_pixels)
Definition allocimbuf.cc:44
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
imbuf::ChangesetID IMB_partial_update_changeset_id_next()
const ColorSpace * IMB_colormanagement_space_get_named(const char *name)
ImBuf * IMB_allocFromBuffer(const uint8_t *byte_buffer, const float *float_buffer, unsigned int w, unsigned int h, unsigned int channels)
void IMB_free_gpu_textures(ImBuf *ibuf)
Definition util_gpu.cc:835
void IMB_free_all_data(ImBuf *ibuf)
static CLG_LogRef LOG
Definition logging.cpp:12
void * GPU_texture_read(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
bool IMB_alloc_float_pixels(ImBuf *ibuf, unsigned int channels, bool initialize_pixels=true)
void colormanage_imbuf_set_default_spaces(ImBuf *ibuf)
void IMB_deactivate_gpu_context()
Definition module.cc:61
void IMB_free_float_pixels(ImBuf *ibuf)
size_t(*) MEM_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT
GPUContext * GPU_context_active_get()
ocio::ColorSpace ColorSpace
Definition BLF_api.hh:42
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ImplicitSharingPtr sharing_info
const ColorSpace * colorspace
ImplicitSharingPtr sharing_info
const ColorSpace * colorspace
std::atomic< gpu::Texture * > texture
ImbFormatOptions foptions
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
IDProperty * metadata_ptr
const ColorSpace & byte_colorspace() const
Definition allocimbuf.cc:98
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
std::atomic< imbuf::ChangesetID > full_update_changeset_id
bool colorspace_is_data() const
ImplicitSharingPtr metadata_sharing_info
const float * float_data() const
std::string filepath
ImColorMode color_mode
void assign_byte_data(uint8_t *data)
const ColorSpace & float_colorspace() const
eImbFileType ftype
float * float_data_for_write()
Definition allocimbuf.cc:79
void assign_float_data(float *data)