+ // If the last effect explicitly sets an output size, use that.
+ if (output_node->effect->changes_output_size()) {
+ output_node->effect->get_output_size(&phase->output_width, &phase->output_height);
+ return;
+ }
+
+ // If not, look at the input phases and textures.
+ // We select the largest one (by fit into the current aspect).
+ unsigned best_width = 0;
+ for (unsigned i = 0; i < phase->inputs.size(); ++i) {
+ Node *input = phase->inputs[i];
+ assert(input->phase->output_width != 0);
+ assert(input->phase->output_height != 0);
+ unsigned width = fit_rectangle_to_aspect(input->phase->output_width, input->phase->output_height);
+ if (width > best_width) {
+ best_width = width;
+ }
+ }
+ for (unsigned i = 0; i < phase->effects.size(); ++i) {
+ Effect *effect = phase->effects[i]->effect;
+ if (effect->num_inputs() != 0) {
+ continue;
+ }
+
+ Input *input = static_cast<Input *>(effect);
+ unsigned width = fit_rectangle_to_aspect(input->get_width(), input->get_height());
+ if (width > best_width) {
+ best_width = width;
+ }
+ }
+ assert(best_width != 0);
+ phase->output_width = best_width;
+ phase->output_height = best_width * aspect_denom / aspect_nom;
+}
+
+void EffectChain::sort_nodes_topologically()
+{
+ std::set<Node *> visited_nodes;
+ std::vector<Node *> sorted_list;
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ if (nodes[i]->incoming_links.size() == 0) {
+ topological_sort_visit_node(nodes[i], &visited_nodes, &sorted_list);
+ }
+ }
+ reverse(sorted_list.begin(), sorted_list.end());
+ nodes = sorted_list;
+}
+
+void EffectChain::topological_sort_visit_node(Node *node, std::set<Node *> *visited_nodes, std::vector<Node *> *sorted_list)
+{
+ if (visited_nodes->count(node) != 0) {
+ return;
+ }
+ visited_nodes->insert(node);
+ for (unsigned i = 0; i < node->outgoing_links.size(); ++i) {
+ topological_sort_visit_node(node->outgoing_links[i], visited_nodes, sorted_list);
+ }
+ sorted_list->push_back(node);
+}
+
+// Propagate gamma and color space information as far as we can in the graph.
+// The rules are simple: Anything where all the inputs agree, get that as
+// output as well. Anything else keeps having *_INVALID.
+void EffectChain::propagate_gamma_and_color_space()
+{
+ // We depend on going through the nodes in order.
+ sort_nodes_topologically();
+
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ Node *node = nodes[i];
+ if (node->disabled) {
+ continue;
+ }
+ assert(node->incoming_links.size() == node->effect->num_inputs());
+ if (node->incoming_links.size() == 0) {
+ assert(node->output_color_space != COLORSPACE_INVALID);
+ assert(node->output_gamma_curve != GAMMA_INVALID);
+ continue;
+ }
+
+ ColorSpace color_space = node->incoming_links[0]->output_color_space;
+ GammaCurve gamma_curve = node->incoming_links[0]->output_gamma_curve;
+ for (unsigned j = 1; j < node->incoming_links.size(); ++j) {
+ if (node->incoming_links[j]->output_color_space != color_space) {
+ color_space = COLORSPACE_INVALID;
+ }
+ if (node->incoming_links[j]->output_gamma_curve != gamma_curve) {
+ gamma_curve = GAMMA_INVALID;
+ }
+ }
+
+ // The conversion effects already have their outputs set correctly,
+ // so leave them alone.
+ if (node->effect->effect_type_id() != "ColorSpaceConversionEffect") {
+ node->output_color_space = color_space;
+ }
+ if (node->effect->effect_type_id() != "GammaCompressionEffect" &&
+ node->effect->effect_type_id() != "GammaExpansionEffect") {
+ node->output_gamma_curve = gamma_curve;
+ }
+ }
+}
+
+bool EffectChain::node_needs_colorspace_fix(Node *node)
+{
+ if (node->disabled) {
+ return false;
+ }
+ if (node->effect->num_inputs() == 0) {
+ return false;
+ }
+
+ // propagate_gamma_and_color_space() has already set our output
+ // to COLORSPACE_INVALID if the inputs differ, so we can rely on that.
+ if (node->output_color_space == COLORSPACE_INVALID) {
+ return true;
+ }
+ return (node->effect->needs_srgb_primaries() && node->output_color_space != COLORSPACE_sRGB);
+}
+
+// Fix up color spaces so that there are no COLORSPACE_INVALID nodes left in
+// the graph. Our strategy is not always optimal, but quite simple:
+// Find an effect that's as early as possible where the inputs are of
+// unacceptable colorspaces (that is, either different, or, if the effect only
+// wants sRGB, not sRGB.) Add appropriate conversions on all its inputs,
+// propagate the information anew, and repeat until there are no more such
+// effects.
+void EffectChain::fix_internal_color_spaces()
+{
+ unsigned colorspace_propagation_pass = 0;
+ bool found_any;
+ do {
+ found_any = false;
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ Node *node = nodes[i];
+ if (!node_needs_colorspace_fix(node)) {
+ continue;
+ }
+
+ // Go through each input that is not sRGB, and insert
+ // a colorspace conversion before it.
+ for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
+ Node *input = node->incoming_links[j];
+ assert(input->output_color_space != COLORSPACE_INVALID);
+ if (input->output_color_space == COLORSPACE_sRGB) {
+ continue;
+ }
+ Node *conversion = add_node(new ColorSpaceConversionEffect());
+ conversion->effect->set_int("source_space", input->output_color_space);
+ conversion->effect->set_int("destination_space", COLORSPACE_sRGB);
+ conversion->output_color_space = COLORSPACE_sRGB;
+ insert_node_between(input, conversion, node);
+ }
+
+ // Re-sort topologically, and propagate the new information.
+ propagate_gamma_and_color_space();
+
+ found_any = true;
+ break;
+ }
+
+ char filename[256];
+ sprintf(filename, "step3-colorspacefix-iter%u.dot", ++colorspace_propagation_pass);
+ output_dot(filename);
+ assert(colorspace_propagation_pass < 100);
+ } while (found_any);
+
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ Node *node = nodes[i];
+ if (node->disabled) {
+ continue;
+ }
+ assert(node->output_color_space != COLORSPACE_INVALID);
+ }
+}
+
+// Make so that the output is in the desired color space.
+void EffectChain::fix_output_color_space()
+{
+ Node *output = find_output_node();
+ if (output->output_color_space != output_format.color_space) {
+ Node *conversion = add_node(new ColorSpaceConversionEffect());
+ conversion->effect->set_int("source_space", output->output_color_space);
+ conversion->effect->set_int("destination_space", output_format.color_space);
+ conversion->output_color_space = output_format.color_space;
+ connect_nodes(output, conversion);
+ }
+}
+
+bool EffectChain::node_needs_gamma_fix(Node *node)
+{
+ if (node->disabled) {
+ return false;
+ }
+ if (node->effect->num_inputs() == 0) {
+ return false;
+ }
+
+ // propagate_gamma_and_color_space() has already set our output
+ // to GAMMA_INVALID if the inputs differ, so we can rely on that,
+ // except for GammaCompressionEffect.
+ if (node->output_gamma_curve == GAMMA_INVALID) {
+ return true;
+ }
+ if (node->effect->effect_type_id() == "GammaCompressionEffect") {
+ assert(node->incoming_links.size() == 1);
+ return node->incoming_links[0]->output_gamma_curve != GAMMA_LINEAR;
+ }
+ return (node->effect->needs_linear_light() && node->output_gamma_curve != GAMMA_LINEAR);
+}
+
+// Very similar to fix_internal_color_spaces(), but for gamma.
+// There is one difference, though; before we start adding conversion nodes,
+// we see if we can get anything out of asking the sources to deliver
+// linear gamma directly. fix_internal_gamma_by_asking_inputs()
+// does that part, while fix_internal_gamma_by_inserting_nodes()
+// inserts nodes as needed afterwards.
+void EffectChain::fix_internal_gamma_by_asking_inputs(unsigned step)
+{
+ unsigned gamma_propagation_pass = 0;
+ bool found_any;
+ do {
+ found_any = false;
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ Node *node = nodes[i];
+ if (!node_needs_gamma_fix(node)) {
+ continue;
+ }
+
+ // See if all inputs can give us linear gamma. If not, leave it.
+ std::vector<Node *> nonlinear_inputs;
+ find_all_nonlinear_inputs(node, &nonlinear_inputs);
+
+ bool all_ok = true;
+ for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
+ Input *input = static_cast<Input *>(nonlinear_inputs[i]->effect);
+ all_ok &= input->can_output_linear_gamma();
+ }
+
+ if (!all_ok) {
+ continue;
+ }
+
+ for (unsigned i = 0; i < nonlinear_inputs.size(); ++i) {
+ nonlinear_inputs[i]->effect->set_int("output_linear_gamma", 1);
+ nonlinear_inputs[i]->output_gamma_curve = GAMMA_LINEAR;
+ }
+
+ // Re-sort topologically, and propagate the new information.
+ propagate_gamma_and_color_space();
+
+ found_any = true;
+ break;
+ }
+
+ char filename[256];
+ sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
+ output_dot(filename);
+ assert(gamma_propagation_pass < 100);
+ } while (found_any);
+}
+
+void EffectChain::fix_internal_gamma_by_inserting_nodes(unsigned step)
+{
+ unsigned gamma_propagation_pass = 0;
+ bool found_any;
+ do {
+ found_any = false;
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ Node *node = nodes[i];
+ if (!node_needs_gamma_fix(node)) {
+ continue;
+ }
+
+ // Go through each input that is not linear gamma, and insert
+ // a gamma conversion before it.
+ for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
+ Node *input = node->incoming_links[j];
+ assert(input->output_gamma_curve != GAMMA_INVALID);
+ if (input->output_gamma_curve == GAMMA_LINEAR) {
+ continue;
+ }
+ Node *conversion = add_node(new GammaExpansionEffect());
+ conversion->effect->set_int("source_curve", input->output_gamma_curve);
+ conversion->output_gamma_curve = GAMMA_LINEAR;
+ insert_node_between(input, conversion, node);
+ }
+
+ // Re-sort topologically, and propagate the new information.
+ propagate_gamma_and_color_space();
+
+ found_any = true;
+ break;
+ }
+
+ char filename[256];
+ sprintf(filename, "step%u-gammafix-iter%u.dot", step, ++gamma_propagation_pass);
+ output_dot(filename);
+ assert(gamma_propagation_pass < 100);
+ } while (found_any);
+
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ Node *node = nodes[i];
+ if (node->disabled) {
+ continue;
+ }
+ assert(node->output_gamma_curve != GAMMA_INVALID);
+ }
+}
+
+// Make so that the output is in the desired gamma.
+// Note that this assumes linear input gamma, so it might create the need
+// for another pass of fix_internal_gamma().
+void EffectChain::fix_output_gamma()
+{
+ Node *output = find_output_node();
+ if (output->output_gamma_curve != output_format.gamma_curve) {
+ Node *conversion = add_node(new GammaCompressionEffect());
+ conversion->effect->set_int("destination_curve", output_format.gamma_curve);
+ conversion->output_gamma_curve = output_format.gamma_curve;
+ connect_nodes(output, conversion);
+ }
+}
+
+// Find the output node. 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).
+Node *EffectChain::find_output_node()
+{
+ std::vector<Node *> output_nodes;
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ Node *node = nodes[i];
+ if (node->disabled) {
+ continue;
+ }
+ if (node->outgoing_links.empty()) {
+ output_nodes.push_back(node);
+ }
+ }
+ assert(output_nodes.size() == 1);
+ return output_nodes[0];
+}
+
+void EffectChain::finalize()
+{
+ // Output the graph as it is before we do any conversions on it.
+ output_dot("step0-start.dot");
+
+ // Give each effect in turn a chance to rewrite its own part of the graph.
+ // Note that if more effects are added as part of this, they will be
+ // picked up as part of the same for loop, since they are added at the end.
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ nodes[i]->effect->rewrite_graph(this, nodes[i]);
+ }
+ output_dot("step1-rewritten.dot");
+
+ propagate_gamma_and_color_space();
+ output_dot("step2-propagated.dot");
+
+ fix_internal_color_spaces();
+ fix_output_color_space();
+ output_dot("step4-output-colorspacefix.dot");
+
+ // Note that we need to fix gamma after colorspace conversion,
+ // because colorspace conversions might create needs for gamma conversions.
+ // Also, we need to run an extra pass of fix_internal_gamma() after
+ // fixing the output gamma, as we only have conversions to/from linear.
+ fix_internal_gamma_by_asking_inputs(5);
+ fix_internal_gamma_by_inserting_nodes(6);
+ fix_output_gamma();
+ output_dot("step8-output-gammafix.dot");
+ fix_internal_gamma_by_asking_inputs(9);
+ fix_internal_gamma_by_inserting_nodes(10);
+
+ output_dot("step11-final.dot");
+