Blender V5.3
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;
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_data() != nullptr) {
78 "Expected float buffer to be scene_linear or data - if there are code paths where this "
79 "isn't the case we should convert those and add to the FloatBufferCache as well.");
80 return image_buffer;
81 }
82
83 /* Do we have a cached float buffer. */
84 for (FloatImageBuffer &item : cache_) {
85 if (item.source_buffer == image_buffer) {
86 item.is_used = true;
87 return item.float_buffer;
88 }
89 }
90
91 /* Generate a new float buffer. */
92 IMB_float_from_byte(image_buffer);
93 ImBuf *new_imbuf = IMB_allocImBuf(image_buffer->x, image_buffer->y, ImBufFlags::Zero);
94 new_imbuf->color_mode = image_buffer->color_mode;
95
96 new_imbuf->float_buffer = image_buffer->float_buffer;
97 image_buffer->float_buffer = {};
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)
__int64 int64_t
Definition BKE_text.h:90
void IMB_float_from_byte(ImBuf *ibuf)
bool IMB_colormanagement_space_is_scene_linear(const ColorSpace *colorspace)
void IMB_freeImBuf(ImBuf *ibuf)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
bool IMB_colormanagement_space_is_data(const ColorSpace *colorspace)
ImBufFloatBuffer float_buffer
const float * float_data() const
ImColorMode color_mode
const ColorSpace & float_colorspace() const
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