+void EffectChain::output_dot(const char *filename)
+{
+ FILE *fp = fopen(filename, "w");
+ if (fp == NULL) {
+ perror(filename);
+ exit(1);
+ }
+
+ fprintf(fp, "digraph G {\n");
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ fprintf(fp, " n%ld [label=\"%s\"];\n", (long)nodes[i], nodes[i]->effect->effect_type_id().c_str());
+ for (unsigned j = 0; j < nodes[i]->outgoing_links.size(); ++j) {
+ std::vector<std::string> labels;
+
+ if (nodes[i]->outgoing_links[j]->effect->needs_texture_bounce()) {
+ labels.push_back("needs_bounce");
+ }
+ if (nodes[i]->effect->changes_output_size()) {
+ labels.push_back("resize");
+ }
+
+ switch (nodes[i]->output_color_space) {
+ case COLORSPACE_INVALID:
+ labels.push_back("spc[invalid]");
+ break;
+ case COLORSPACE_REC_601_525:
+ labels.push_back("spc[rec601-525]");
+ break;
+ case COLORSPACE_REC_601_625:
+ labels.push_back("spc[rec601-625]");
+ break;
+ default:
+ break;
+ }
+
+ switch (nodes[i]->output_gamma_curve) {
+ case GAMMA_INVALID:
+ labels.push_back("gamma[invalid]");
+ break;
+ case GAMMA_sRGB:
+ labels.push_back("gamma[sRGB]");
+ break;
+ case GAMMA_REC_601: // and GAMMA_REC_709
+ labels.push_back("gamma[rec601/709]");
+ break;
+ default:
+ break;
+ }
+
+ if (labels.empty()) {
+ fprintf(fp, " n%ld -> n%ld;\n", (long)nodes[i], (long)nodes[i]->outgoing_links[j]);
+ } else {
+ std::string label = labels[0];
+ for (unsigned k = 1; k < labels.size(); ++k) {
+ label += ", " + labels[k];
+ }
+ fprintf(fp, " n%ld -> n%ld [label=\"%s\"];\n", (long)nodes[i], (long)nodes[i]->outgoing_links[j], label.c_str());
+ }
+ }
+ }
+ fprintf(fp, "}\n");
+
+ fclose(fp);
+}
+
+void EffectChain::find_output_size(Phase *phase)
+{
+ Node *output_node = phase->effects.back();
+
+ // 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, 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) {
+ Node *input = phase->inputs[i];
+ 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::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;
+ }