+ // Finally, since the phases are found from the output but must be executed
+ // from the input(s), reverse them, too.
+ std::reverse(phases.begin(), phases.end());
+}
+
+void EffectChain::find_output_size(EffectChain::Phase *phase)
+{
+ Effect *output_effect = phase->effects.back();
+
+ // If the last effect explicitly sets an output size,
+ // use that.
+ if (output_effect->changes_output_size()) {
+ output_effect->get_output_size(&phase->output_width, &phase->output_height);
+ return;
+ }
+
+ // If not, look at the input phases, if any. We select the largest one
+ // (really assuming they all have the same aspect currently), by pixel count.
+ if (!phase->inputs.empty()) {
+ unsigned best_width = 0, best_height = 0;
+ for (unsigned i = 0; i < phase->inputs.size(); ++i) {
+ Effect *input = phase->inputs[i];
+ assert(output_effects_to_phase.count(input) != 0);
+ const Phase *input_phase = output_effects_to_phase[input];
+ assert(input_phase->output_width != 0);
+ assert(input_phase->output_height != 0);
+ if (input_phase->output_width * input_phase->output_height > best_width * best_height) {
+ best_width = input_phase->output_width;
+ best_height = input_phase->output_height;
+ }
+ }
+ assert(best_width != 0);
+ assert(best_height != 0);
+ phase->output_width = best_width;
+ phase->output_height = best_height;
+ return;
+ }
+
+ // OK, no inputs. Just use the global width/height.
+ // TODO: We probably want to use the texture's size eventually.
+ phase->output_width = width;
+ phase->output_height = height;
+}
+
+void EffectChain::finalize()
+{
+ // Find the output effect. This is, simply, one that has no outgoing links.
+ // If there are multiple ones, the graph is malformed (we do not support
+ // multiple outputs right now).
+ std::vector<Effect *> output_effects;
+ for (unsigned i = 0; i < effects.size(); ++i) {
+ Effect *effect = effects[i];
+ if (outgoing_links.count(effect) == 0 || outgoing_links[effect].size() == 0) {
+ output_effects.push_back(effect);
+ }
+ }
+ assert(output_effects.size() == 1);
+ Effect *output_effect = output_effects[0];
+
+ // Add normalizers to get the output format right.
+ assert(output_gamma_curve.count(output_effect) != 0);
+ assert(output_color_space.count(output_effect) != 0);
+ ColorSpace current_color_space = output_color_space[output_effect];
+ if (current_color_space != output_format.color_space) {
+ ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
+ colorspace_conversion->set_int("source_space", current_color_space);
+ colorspace_conversion->set_int("destination_space", output_format.color_space);
+ std::vector<Effect *> inputs;
+ inputs.push_back(output_effect);
+ colorspace_conversion->add_self_to_effect_chain(this, inputs);
+ output_color_space[colorspace_conversion] = output_format.color_space;
+ output_effect = colorspace_conversion;
+ }
+ GammaCurve current_gamma_curve = output_gamma_curve[output_effect];
+ if (current_gamma_curve != output_format.gamma_curve) {
+ if (current_gamma_curve != GAMMA_LINEAR) {
+ output_effect = normalize_to_linear_gamma(output_effect);
+ current_gamma_curve = GAMMA_LINEAR;
+ }
+ GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
+ gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
+ std::vector<Effect *> inputs;
+ inputs.push_back(output_effect);
+ gamma_conversion->add_self_to_effect_chain(this, inputs);
+ output_gamma_curve[gamma_conversion] = output_format.gamma_curve;
+ output_effect = gamma_conversion;
+ }