Blender  V3.3
image_buffer_cache.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2022 Blender Foundation. */
3 
8 #pragma once
9 
10 #include "BLI_vector.hh"
11 
12 #include "IMB_imbuf.h"
13 #include "IMB_imbuf_types.h"
14 
16  ImBuf *source_buffer = nullptr;
17  ImBuf *float_buffer = nullptr;
18  bool is_used = true;
19 
22  {
23  }
24 
26  {
27  source_buffer = other.source_buffer;
28  float_buffer = other.float_buffer;
29  is_used = other.is_used;
30  other.source_buffer = nullptr;
31  other.float_buffer = nullptr;
32  }
33 
35  {
37  float_buffer = nullptr;
38  source_buffer = nullptr;
39  }
40 
42  {
43  this->source_buffer = other.source_buffer;
44  this->float_buffer = other.float_buffer;
45  is_used = other.is_used;
46  other.source_buffer = nullptr;
47  other.float_buffer = nullptr;
48  return *this;
49  }
50 };
51 
53  private:
55 
56  public:
58  {
59  /* Check if we can use the float buffer of the given image_buffer. */
60  if (image_buffer->rect_float != nullptr) {
61  return image_buffer;
62  }
63 
64  /* Do we have a cached float buffer. */
65  for (FloatImageBuffer &item : cache_) {
66  if (item.source_buffer == image_buffer) {
67  item.is_used = true;
68  return item.float_buffer;
69  }
70  }
71 
72  /* Generate a new float buffer. */
73  IMB_float_from_rect(image_buffer);
74  ImBuf *new_imbuf = IMB_allocImBuf(image_buffer->x, image_buffer->y, image_buffer->planes, 0);
75  new_imbuf->rect_float = image_buffer->rect_float;
76  new_imbuf->flags |= IB_rectfloat;
77  new_imbuf->mall |= IB_rectfloat;
78  image_buffer->rect_float = nullptr;
79  image_buffer->flags &= ~IB_rectfloat;
80  image_buffer->mall &= ~IB_rectfloat;
81 
82  cache_.append(FloatImageBuffer(image_buffer, new_imbuf));
83  return new_imbuf;
84  }
85 
87  {
88  for (FloatImageBuffer &buffer : cache_) {
89  buffer.is_used = false;
90  }
91  }
92 
93  void mark_used(const ImBuf *image_buffer)
94  {
95  for (FloatImageBuffer &item : cache_) {
96  if (item.source_buffer == image_buffer) {
97  item.is_used = true;
98  return;
99  }
100  }
101  }
102 
104  {
105  for (int64_t i = cache_.size() - 1; i >= 0; i--) {
106  if (!cache_[i].is_used) {
107  cache_.remove_and_reorder(i);
108  }
109  }
110  }
111 
112  void clear()
113  {
114  cache_.clear();
115  }
116 };
void IMB_float_from_rect(struct ImBuf *ibuf)
Definition: divers.c:805
struct ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
Definition: allocimbuf.c:500
Contains defines and structs used throughout the imbuf module.
@ IB_rectfloat
int64_t size() const
Definition: BLI_vector.hh:694
void remove_and_reorder(const int64_t index)
Definition: BLI_vector.hh:743
void IMB_freeImBuf(ImBuf *UNUSED(ibuf))
ccl_global float * buffer
__int64 int64_t
Definition: stdint.h:89
ImBuf * ensure_float_buffer(ImBuf *image_buffer)
void mark_used(const ImBuf *image_buffer)
FloatImageBuffer & operator=(FloatImageBuffer &&other) noexcept
FloatImageBuffer(FloatImageBuffer &&other) noexcept
FloatImageBuffer(ImBuf *source_buffer, ImBuf *float_buffer)
unsigned char planes
float * rect_float