Blender V4.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
9#pragma once
10
11#include "BLI_vector.hh"
12
14#include "IMB_imbuf.hh"
15#include "IMB_imbuf_types.hh"
16
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_buffer.data != nullptr) {
78 "Expected float buffer to be scene_linear - 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_rect(image_buffer);
93 ImBuf *new_imbuf = IMB_allocImBuf(image_buffer->x, image_buffer->y, image_buffer->planes, 0);
94
96
97 cache_.append(FloatImageBuffer(image_buffer, new_imbuf));
98 return new_imbuf;
99 }
100
102 {
103 for (FloatImageBuffer &buffer : cache_) {
104 buffer.is_used = false;
105 }
106 }
107
108 void mark_used(const ImBuf *image_buffer)
109 {
110 for (FloatImageBuffer &item : cache_) {
111 if (item.source_buffer == image_buffer) {
112 item.is_used = true;
113 return;
114 }
115 }
116 }
117
119 {
120 for (int64_t i = cache_.size() - 1; i >= 0; i--) {
121 if (!cache_[i].is_used) {
122 cache_.remove_and_reorder(i);
123 }
124 }
125 }
126
127 void clear()
128 {
129 cache_.clear();
130 }
131};
132
133} // namespace blender::draw::image_engine
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:57
bool IMB_colormanagement_space_name_is_scene_linear(const char *name)
const char * IMB_colormanagement_get_float_colorspace(ImBuf *ibuf)
float * IMB_steal_float_buffer(ImBuf *ibuf)
void IMB_assign_float_buffer(ImBuf *ibuf, float *buffer_data, ImBufOwnership ownership)
void IMB_float_from_rect(ImBuf *ibuf)
Definition divers.cc:802
Contains defines and structs used throughout the imbuf module.
@ IB_TAKE_OWNERSHIP
int64_t size() const
void remove_and_reorder(const int64_t index)
void append(const T &value)
struct ImBuf * IMB_allocImBuf(unsigned int, unsigned int, unsigned char, unsigned int)
void IMB_freeImBuf(ImBuf *)
__int64 int64_t
Definition stdint.h:89
ImBufFloatBuffer float_buffer
unsigned char planes
Float buffer cache for image buffers.
FloatImageBuffer(FloatImageBuffer &&other) noexcept
FloatImageBuffer & operator=(FloatImageBuffer &&other) noexcept
FloatImageBuffer(ImBuf *source_buffer, ImBuf *float_buffer)