1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
17  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "third_party/blink/renderer/platform/graphics/filters/fe_blend.h"
27 #include "third_party/blink/renderer/platform/graphics/filters/fe_gaussian_blur.h"
28 #include "third_party/blink/renderer/platform/graphics/filters/fe_merge.h"
29 #include "third_party/blink/renderer/platform/graphics/filters/filter.h"
30 #include "third_party/blink/renderer/platform/graphics/filters/paint_filter_builder.h"
31 #include "third_party/blink/renderer/platform/graphics/filters/source_graphic.h"
32 #include "third_party/blink/renderer/platform/heap/heap.h"
33 
34 using testing::Test;
35 
36 namespace blink {
37 
38 class ImageFilterBuilderTest : public Test {
39  protected:
InterpolationSpaceTest()40   void InterpolationSpaceTest() {
41     // Build filter tree
42     auto* reference_filter = MakeGarbageCollected<Filter>(1.0f);
43 
44     // Add a dummy source graphic input
45     FilterEffect* source_effect = reference_filter->GetSourceGraphic();
46     source_effect->SetOperatingInterpolationSpace(kInterpolationSpaceSRGB);
47 
48     // Add a blur effect (with input : source)
49     auto* blur_effect =
50         MakeGarbageCollected<FEGaussianBlur>(reference_filter, 3.0f, 3.0f);
51     blur_effect->SetOperatingInterpolationSpace(kInterpolationSpaceLinear);
52     blur_effect->InputEffects().push_back(source_effect);
53 
54     // Add a blend effect (with inputs : blur, source)
55     auto* blend_effect =
56         MakeGarbageCollected<FEBlend>(reference_filter, BlendMode::kNormal);
57     blend_effect->SetOperatingInterpolationSpace(kInterpolationSpaceSRGB);
58     FilterEffectVector& blend_inputs = blend_effect->InputEffects();
59     blend_inputs.ReserveCapacity(2);
60     blend_inputs.push_back(source_effect);
61     blend_inputs.push_back(blur_effect);
62 
63     // Add a merge effect (with inputs : blur, blend)
64     auto* merge_effect = MakeGarbageCollected<FEMerge>(reference_filter);
65     merge_effect->SetOperatingInterpolationSpace(kInterpolationSpaceLinear);
66     FilterEffectVector& merge_inputs = merge_effect->InputEffects();
67     merge_inputs.ReserveCapacity(2);
68     merge_inputs.push_back(blur_effect);
69     merge_inputs.push_back(blend_effect);
70     reference_filter->SetLastEffect(merge_effect);
71 
72     // Get PaintFilter resulting tree
73     sk_sp<PaintFilter> filter = paint_filter_builder::Build(
74         reference_filter->LastEffect(), kInterpolationSpaceSRGB);
75 
76     // Let's check that the resulting tree looks like this :
77     //      InterpolationSpace (Linear->Device) : CS (L->D)
78     //                |
79     //             Merge (L)
80     //              |     |
81     //              |    CS (D->L)
82     //              |          |
83     //              |      Blend (D)
84     //              |       /    |
85     //              |  CS (L->D) |
86     //              |  /         |
87     //             Blur (L)      |
88     //                 \         |
89     //               CS (D->L)   |
90     //                   \       |
91     //                 Source Graphic (D)
92 
93     // Should be CS : InterpolationSpace (Linear->Device)
94     EXPECT_EQ(filter->type(), PaintFilter::Type::kColorFilter);
95 
96     // Should be Merge.
97     const auto* merge_effect_pf =
98         static_cast<const ColorFilterPaintFilter*>(filter.get())->input().get();
99     ASSERT_EQ(merge_effect_pf->type(), PaintFilter::Type::kMerge);
100     const auto* merge = static_cast<const MergePaintFilter*>(merge_effect_pf);
101     EXPECT_EQ(merge->input_count(), 2u);
102 
103     // Should be CS (D->L)
104     const auto* color_filter_pf = merge->input_at(1u);
105     ASSERT_EQ(color_filter_pf->type(), PaintFilter::Type::kColorFilter);
106 
107     // Should be Blend
108     const auto* xfermode_filter_pf =
109         static_cast<const ColorFilterPaintFilter*>(color_filter_pf)
110             ->input()
111             .get();
112     ASSERT_TRUE(xfermode_filter_pf);
113     EXPECT_EQ(xfermode_filter_pf->type(), PaintFilter::Type::kXfermode);
114     const auto* xfermode =
115         static_cast<const XfermodePaintFilter*>(xfermode_filter_pf);
116     ASSERT_TRUE(xfermode->background());
117 
118     // Should be CS (L->D)
119     color_filter_pf = xfermode->background().get();
120     ASSERT_EQ(color_filter_pf->type(), PaintFilter::Type::kColorFilter);
121 
122     // Should be Blur
123     const auto* blur_filter_pf =
124         static_cast<const ColorFilterPaintFilter*>(color_filter_pf)
125             ->input()
126             .get();
127     ASSERT_TRUE(blur_filter_pf);
128     EXPECT_EQ(blur_filter_pf->type(), PaintFilter::Type::kBlur);
129 
130     // Should be CS (D->L)
131     color_filter_pf =
132         static_cast<const BlurPaintFilter*>(blur_filter_pf)->input().get();
133     ASSERT_TRUE(color_filter_pf);
134     EXPECT_EQ(color_filter_pf->type(), PaintFilter::Type::kColorFilter);
135   }
136 };
137 
TEST_F(ImageFilterBuilderTest,testInterpolationSpace)138 TEST_F(ImageFilterBuilderTest, testInterpolationSpace) {
139   InterpolationSpaceTest();
140 }
141 
142 }  // namespace blink
143