Blender V5.3
operation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <limits>
6#include <memory>
7
8#include "BLI_map.hh"
9#include "BLI_string_ref.hh"
10
11#include "COM_context.hh"
13#include "COM_domain.hh"
15#include "COM_operation.hh"
17#include "COM_result.hh"
19
20namespace blender::compositor {
21
23
24Operation::~Operation() = default;
25
27{
29 this->execute();
30 this->log_data();
31 this->release_inputs();
33}
34
36{
37 return *results_mapped_to_inputs_.lookup(identifier);
38}
39
41{
42 return results_.lookup(identifier);
43}
44
46{
47 results_mapped_to_inputs_.add_new(identifier, result);
48}
49
51{
52 for (Result &result : results_.values()) {
53 result.free();
54 }
55}
56
58{
59 return context_;
60}
61
63{
64 /* Default to an identity domain in case no domain input was found, most likely because all
65 * inputs are single values. */
66 Domain operation_domain = Domain::identity();
67 int current_domain_priority = std::numeric_limits<int>::max();
68
69 /* Go over the inputs and find the domain of the non single value input with the highest domain
70 * priority. */
71 for (StringRef identifier : input_descriptors_.keys()) {
72 const Result &result = get_input(identifier);
74
75 /* A single value input can't be a domain input. */
76 if (result.is_single_value() || descriptor.expects_single_value) {
77 continue;
78 }
79
80 /* An input that skips operation domain realization can't be a domain input. */
81 if (descriptor.realization_mode != InputRealizationMode::OperationDomain) {
82 continue;
83 }
84
85 /* Notice that the lower the domain priority value is, the higher the priority is, hence the
86 * less than comparison. */
87 if (descriptor.domain_priority < current_domain_priority) {
88 operation_domain = result.domain();
89 current_domain_priority = descriptor.domain_priority;
90 }
91 }
92
93 return operation_domain;
94}
95
97{
98 /* Each input processor type is added to all inputs entirely before the next type. This is done
99 * because the construction of the input processors may depend on the result of previous input
100 * processors for all inputs. For instance, the realize on domain input processor considers the
101 * value of all inputs, so previous input processors for all inputs needs to be added and
102 * evaluated first. */
103
104 for (const std::string &identifier : results_mapped_to_inputs_.keys()) {
106 this->context(), this->get_input(identifier), this->get_input_descriptor(identifier));
107 this->add_and_evaluate_input_processor(identifier, conversion);
108 }
109
110 for (const std::string &identifier : results_mapped_to_inputs_.keys()) {
112 this->context(),
113 this->get_input(identifier),
114 this->get_input_descriptor(identifier),
115 this->compute_domain());
116 this->add_and_evaluate_input_processor(identifier, realize_on_domain);
117 }
118}
119
121
123{
124 results_.add_new(identifier, this->context().create_result(type));
125}
126
128{
129 input_descriptors_.add_new(identifier, descriptor);
130}
131
133{
134 return input_descriptors_.lookup(identifier);
135}
136
138{
139 for (Result &result : results_.values()) {
140 if (result.should_compute() && !result.is_allocated()) {
141 result.allocate_invalid();
142 }
143 }
144}
145
146void Operation::add_and_evaluate_input_processor(StringRef identifier, SimpleOperation *processor)
147{
148 /* Allow null inputs to facilitate construct_if_needed pattern of addition. For instance, see the
149 * implementation of the evaluate_input_processors method. */
150 if (!processor) {
151 return;
152 }
153
154 ProcessorsVector &processors = input_processors_.lookup_or_add_default(identifier);
155
156 /* Get the result that should serve as the input for the processor. This is either the result
157 * mapped to the input or the result of the last processor depending on whether this is the first
158 * processor or not. */
159 Result &result = processors.is_empty() ? this->get_input(identifier) :
160 processors.last()->get_result();
161
162 /* Map the input result of the processor and add it to the processors vector. */
163 processor->map_input_to_result(&result);
164 processors.append(std::unique_ptr<SimpleOperation>(processor));
165
166 /* Switch the result mapped to the input to be the output result of the processor. */
167 results_mapped_to_inputs_.lookup(identifier) = &processor->get_result();
168
169 processor->evaluate();
170}
171
172void Operation::release_inputs()
173{
174 for (Result *result : results_mapped_to_inputs_.values()) {
175 result->release();
176 }
177}
178
179} // namespace blender::compositor
static SimpleOperation * construct_if_needed(Context &context, const Result &input_result, const InputDescriptor &input_descriptor)
static Domain identity()
Definition domain.cc:47
Result & get_result(StringRef identifier)
Definition operation.cc:40
Operation(Context &context)
Definition operation.cc:22
void populate_result(StringRef identifier, ResultType type)
Definition operation.cc:122
virtual void evaluate_input_processors()
Definition operation.cc:96
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
void map_input_to_result(StringRef identifier, Result *result)
Definition operation.cc:45
virtual Domain compute_domain()
Definition operation.cc:62
InputDescriptor & get_input_descriptor(StringRef identifier)
Definition operation.cc:132
void declare_input_descriptor(StringRef identifier, InputDescriptor descriptor)
Definition operation.cc:127
static SimpleOperation * construct_if_needed(Context &context, const Result &input_result, const InputDescriptor &input_descriptor, const Domain &operation_domain)
Vector< std::unique_ptr< SimpleOperation > > ProcessorsVector