+ this_phase_effects.back()->phase = phases.back();
+ this_phase_inputs.clear();
+ this_phase_effects.clear();
+ }
+ assert(this_phase_inputs.empty());
+ assert(this_phase_effects.empty());
+
+ // If we have no effects left, exit.
+ if (effects_todo_other_phases.empty()) {
+ break;
+ }
+
+ Node *node = effects_todo_other_phases.top();
+ effects_todo_other_phases.pop();
+
+ if (completed_effects.count(node) == 0) {
+ // Start a new phase, calculating from this effect.
+ effects_todo_this_phase.push(node);
+ }
+ }
+
+ // 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::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);
+}
+
+unsigned EffectChain::fit_rectangle_to_aspect(unsigned width, unsigned height)
+{
+ if (float(width) * aspect_denom >= float(height) * aspect_nom) {
+ // Same aspect, or W/H > aspect (image is wider than the frame).
+ // In either case, keep width.
+ return width;
+ } else {
+ // W/H < aspect (image is taller than the frame), so keep height,
+ // and adjust width correspondingly.
+ return lrintf(height * aspect_nom / aspect_denom);
+ }
+}
+
+// Propagate input texture sizes throughout, and inform effects downstream.
+// (Like a lot of other code, we depend on effects being in topological order.)
+void EffectChain::inform_input_sizes(Phase *phase)
+{
+ // All effects that have a defined size (inputs and RTT inputs)
+ // get that. Reset all others.
+ for (unsigned i = 0; i < phase->effects.size(); ++i) {
+ Node *node = phase->effects[i];
+ if (node->effect->num_inputs() == 0) {
+ Input *input = static_cast<Input *>(node->effect);
+ node->output_width = input->get_width();
+ node->output_height = input->get_height();
+ assert(node->output_width != 0);
+ assert(node->output_height != 0);
+ } else {
+ node->output_width = node->output_height = 0;
+ }
+ }
+ for (unsigned i = 0; i < phase->inputs.size(); ++i) {
+ Node *input = phase->inputs[i];
+ input->output_width = input->phase->output_width;
+ input->output_height = input->phase->output_height;
+ assert(input->output_width != 0);
+ assert(input->output_height != 0);
+ }
+
+ // Now propagate from the inputs towards the end, and inform as we go.
+ // The rules are simple:
+ //
+ // 1. Don't touch effects that already have given sizes (ie., inputs).
+ // 2. If all of your inputs have the same size, that will be your output size.
+ // 3. Otherwise, your output size is 0x0.
+ for (unsigned i = 0; i < phase->effects.size(); ++i) {
+ Node *node = phase->effects[i];
+ if (node->effect->num_inputs() == 0) {
+ continue;
+ }
+ unsigned this_output_width = 0;
+ unsigned this_output_height = 0;
+ for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
+ Node *input = node->incoming_links[j];
+ node->effect->inform_input_size(j, input->output_width, input->output_height);
+ if (j == 0) {
+ this_output_width = input->output_width;
+ this_output_height = input->output_height;
+ } else if (input->output_width != this_output_width || input->output_height != this_output_height) {
+ // Inputs disagree.
+ this_output_width = 0;
+ this_output_height = 0;
+ }
+ }
+ node->output_width = this_output_width;
+ node->output_height = this_output_height;
+ }
+}
+
+// Note: You should call inform_input_sizes() before this, as the last effect's
+// desired output size might change based on the inputs.
+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 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;