Blender V5.0
image_buffer_cache.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLI_vector.hh"
12
14#include "IMB_imbuf.hh"
15#include "IMB_imbuf_types.hh"
16
17namespace blender::image_engine {
18
20 ImBuf *source_buffer = nullptr;
21 ImBuf *float_buffer = nullptr;
22 bool is_used = true;
23
28
30 {
31 source_buffer = other.source_buffer;
32 float_buffer = other.float_buffer;
33 is_used = other.is_used;
34 other.source_buffer = nullptr;
35 other.float_buffer = nullptr;
36 }
37
39 {
41 float_buffer = nullptr;
42 source_buffer = nullptr;
43 }
44
46 {
47 this->source_buffer = other.source_buffer;
48 this->float_buffer = other.float_buffer;
49 is_used = other.is_used;
50 other.source_buffer = nullptr;
51 other.float_buffer = nullptr;
52 return *this;
53 }
54};
55
67 private:
69
70 public:
72 {
73 /* Check if we can use the float buffer of the given image_buffer. */
74 if (image_buffer->float_buffer.data != nullptr) {
80 "Expected float buffer to be scene_linear or data - if there are code paths where this "
81 "isn't the case we should convert those and add to the FloatBufferCache as well.");
82 return image_buffer;
83 }
84
85 /* Do we have a cached float buffer. */
86 for (FloatImageBuffer &item : cache_) {
87 if (item.source_buffer == image_buffer) {
88 item.is_used = true;
89 return item.float_buffer;
90 }
91 }
92
93 /* Generate a new float buffer. */
94 IMB_float_from_byte(image_buffer);
95 ImBuf *new_imbuf = IMB_allocImBuf(image_buffer->x, image_buffer->y, image_buffer->planes, 0);
96
98
99 cache_.append(FloatImageBuffer(image_buffer, new_imbuf));
100 return new_imbuf;
101 }
102
104 {
105 for (FloatImageBuffer &buffer : cache_) {
106 buffer.is_used = false;
107 }
108 }
109
110 void mark_used(const ImBuf *image_buffer)
111 {
112 for (FloatImageBuffer &item : cache_) {
113 if (item.source_buffer == image_buffer) {
114 item.is_used = true;
115 return;
116 }
117 }
118 }
119
121 {
122 for (int64_t i = cache_.size() - 1; i >= 0; i--) {
123 if (!cache_[i].is_used) {
124 cache_.remove_and_reorder(i);
125 }
126 }
127 }
128
129 void clear()
130 {
131 cache_.clear();
132 }
133};
134
135} // namespace blender::image_engine
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
bool IMB_colormanagement_space_name_is_scene_linear(const char *name)
bool IMB_colormanagement_space_name_is_data(const char *name)
const char * IMB_colormanagement_get_float_colorspace(const ImBuf *ibuf)
float * IMB_steal_float_buffer(ImBuf *ibuf)
void IMB_assign_float_buffer(ImBuf *ibuf, float *buffer_data, ImBufOwnership ownership)
void IMB_freeImBuf(ImBuf *ibuf)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
void IMB_float_from_byte(ImBuf *ibuf)
@ IB_TAKE_OWNERSHIP
long long int int64_t
ImBufFloatBuffer float_buffer
unsigned char planes
Float buffer cache for image buffers.
ImBuf * cached_float_buffer(ImBuf *image_buffer)
void mark_used(const ImBuf *image_buffer)
FloatImageBuffer & operator=(FloatImageBuffer &&other) noexcept
FloatImageBuffer(ImBuf *source_buffer, ImBuf *float_buffer)
FloatImageBuffer(FloatImageBuffer &&other) noexcept
i
Definition text_draw.cc:230