+// The identity effect needs premultiplied alpha, and thus will get conversions on both sides.
+TEST(EffectChainTest, IdentityThroughAlphaConversions) {
+ const int size = 3;
+ float data[4 * size] = {
+ 0.8f, 0.0f, 0.0f, 0.5f,
+ 0.0f, 0.2f, 0.2f, 0.3f,
+ 0.1f, 0.0f, 1.0f, 1.0f,
+ };
+ float out_data[6];
+ EffectChainTester tester(data, size, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
+ tester.get_chain()->add_effect(new IdentityEffect());
+ tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+ expect_equal(data, out_data, 4, size);
+}
+
+TEST(EffectChainTest, NoAlphaConversionsWhenPremultipliedAlphaNotNeeded) {
+ const int size = 3;
+ float data[4 * size] = {
+ 0.8f, 0.0f, 0.0f, 0.5f,
+ 0.0f, 0.2f, 0.2f, 0.3f,
+ 0.1f, 0.0f, 1.0f, 1.0f,
+ };
+ float expected_data[4 * size] = {
+ 0.1f, 0.0f, 1.0f, 1.0f,
+ 0.0f, 0.2f, 0.2f, 0.3f,
+ 0.8f, 0.0f, 0.0f, 0.5f,
+ };
+ float out_data[4 * size];
+ EffectChainTester tester(data, size, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
+ RewritingEffect<MirrorEffect> *effect = new RewritingEffect<MirrorEffect>();
+ tester.get_chain()->add_effect(effect);
+ tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+ Node *node = effect->replaced_node;
+ ASSERT_EQ(1, node->incoming_links.size());
+ EXPECT_EQ(0, node->outgoing_links.size());
+ EXPECT_EQ("FlatInput", node->incoming_links[0]->effect->effect_type_id());
+
+ expect_equal(expected_data, out_data, 4, size);
+}
+
+// An input that outputs only blue, which has blank alpha.
+class BlueInput : public Input {
+public:
+ BlueInput() { register_int("needs_mipmaps", &needs_mipmaps); }
+ virtual std::string effect_type_id() const { return "IdentityEffect"; }
+ std::string output_fragment_shader() { return read_file("blue.frag"); }
+ virtual AlphaHandling alpha_handling() const { return OUTPUT_BLANK_ALPHA; }
+ virtual void finalize() {}
+ virtual bool can_output_linear_gamma() const { return true; }
+ virtual unsigned get_width() const { return 1; }
+ virtual unsigned get_height() const { return 1; }
+ virtual Colorspace get_color_space() const { return COLORSPACE_sRGB; }
+ virtual GammaCurve get_gamma_curve() const { return GAMMA_LINEAR; }
+
+private:
+ int needs_mipmaps;
+};
+
+// Like RewritingEffect<InvertEffect>, but splicing in a BlueInput instead,
+// which outputs blank alpha.
+class RewritingToBlueInput : public Input {
+public:
+ RewritingToBlueInput() { register_int("needs_mipmaps", &needs_mipmaps); }
+ virtual std::string effect_type_id() const { return "RewritingToBlueInput"; }
+ std::string output_fragment_shader() { EXPECT_TRUE(false); return read_file("identity.frag"); }
+ virtual void rewrite_graph(EffectChain *graph, Node *self) {
+ Node *blue_node = graph->add_node(new BlueInput());
+ graph->replace_receiver(self, blue_node);
+ graph->replace_sender(self, blue_node);
+
+ self->disabled = true;
+ this->blue_node = blue_node;
+ }
+
+ // Dummy values that we need to implement because we inherit from Input.
+ // Same as BlueInput.
+ virtual AlphaHandling alpha_handling() const { return OUTPUT_BLANK_ALPHA; }
+ virtual void finalize() {}
+ virtual bool can_output_linear_gamma() const { return true; }
+ virtual unsigned get_width() const { return 1; }
+ virtual unsigned get_height() const { return 1; }
+ virtual Colorspace get_color_space() const { return COLORSPACE_sRGB; }
+ virtual GammaCurve get_gamma_curve() const { return GAMMA_LINEAR; }
+
+ Node *blue_node;
+
+private:
+ int needs_mipmaps;
+};
+
+TEST(EffectChainTest, NoAlphaConversionsWithBlankAlpha) {
+ const int size = 3;
+ float data[4 * size] = {
+ 0.0f, 0.0f, 1.0f, 1.0f,
+ 0.0f, 0.0f, 1.0f, 1.0f,
+ 0.0f, 0.0f, 1.0f, 1.0f,
+ };
+ float out_data[4 * size];
+ EffectChainTester tester(NULL, size, 1);
+ RewritingToBlueInput *input = new RewritingToBlueInput();
+ tester.get_chain()->add_input(input);
+ tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_PREMULTIPLIED);
+
+ Node *node = input->blue_node;
+ EXPECT_EQ(0, node->incoming_links.size());
+ EXPECT_EQ(0, node->outgoing_links.size());
+
+ expect_equal(data, out_data, 4, size);
+}
+