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 ANY 14 * 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 ANY 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #include "SkImageFilter.h" 26 #include "core/platform/graphics/filters/FEBlend.h" 27 #include "core/platform/graphics/filters/FEGaussianBlur.h" 28 #include "core/platform/graphics/filters/FEMerge.h" 29 #include "core/platform/graphics/filters/FilterOperations.h" 30 #include "core/platform/graphics/filters/ReferenceFilter.h" 31 #include "core/platform/graphics/filters/SkiaImageFilterBuilder.h" 32 #include "core/platform/graphics/filters/SourceGraphic.h" 33 #include <gtest/gtest.h> 34 35 using testing::Test; 36 using namespace WebCore; 37 38 class ImageFilterBuilderTest : public Test { 39 protected: 40 void colorSpaceTest() 41 { 42 // Build filter tree 43 RefPtr<ReferenceFilter> referenceFilter = ReferenceFilter::create(); 44 45 // Add a dummy source graphic input 46 RefPtr<FilterEffect> sourceEffect = referenceFilter->sourceGraphic(); 47 sourceEffect->setOperatingColorSpace(ColorSpaceDeviceRGB); 48 49 // Add a blur effect (with input : source) 50 RefPtr<FilterEffect> blurEffect = 51 FEGaussianBlur::create(referenceFilter.get(), 3.0f, 3.0f); 52 blurEffect->setOperatingColorSpace(ColorSpaceLinearRGB); 53 blurEffect->inputEffects().append(sourceEffect); 54 55 // Add a blend effect (with inputs : blur, source) 56 RefPtr<FilterEffect> blendEffect = 57 FEBlend::create(referenceFilter.get(), FEBLEND_MODE_NORMAL); 58 blendEffect->setOperatingColorSpace(ColorSpaceDeviceRGB); 59 FilterEffectVector& blendInputs = blendEffect->inputEffects(); 60 blendInputs.reserveCapacity(2); 61 blendInputs.append(sourceEffect); 62 blendInputs.append(blurEffect); 63 64 // Add a merge effect (with inputs : blur, blend) 65 RefPtr<FilterEffect> mergeEffect = FEMerge::create(referenceFilter.get()); 66 mergeEffect->setOperatingColorSpace(ColorSpaceLinearRGB); 67 FilterEffectVector& mergeInputs = mergeEffect->inputEffects(); 68 mergeInputs.reserveCapacity(2); 69 mergeInputs.append(blurEffect); 70 mergeInputs.append(blendEffect); 71 referenceFilter->setLastEffect(mergeEffect); 72 73 // Get SkImageFilter resulting tree 74 SkiaImageFilterBuilder builder; 75 RefPtr<SkImageFilter> filter = builder.build(referenceFilter->lastEffect(), ColorSpaceDeviceRGB); 76 77 // Let's check that the resulting tree looks like this : 78 // ColorSpace (Linear->Device) : CS (L->D) 79 // | 80 // Merge (L) 81 // | | 82 // | CS (D->L) 83 // | | 84 // | Blend (D) 85 // | / | 86 // | CS (L->D) | 87 // | / | 88 // Blur (L) | 89 // \ | 90 // CS (D->L) | 91 // \ | 92 // Source Graphic (D) 93 94 EXPECT_EQ(filter->countInputs(), 1); // Should be CS (L->D) 95 SkImageFilter* child = filter->getInput(0); // Should be Merge 96 EXPECT_EQ(child->asColorFilter(0), false); 97 EXPECT_EQ(child->countInputs(), 2); 98 child = child->getInput(1); // Should be CS (D->L) 99 EXPECT_EQ(child->asColorFilter(0), true); 100 EXPECT_EQ(child->countInputs(), 1); 101 child = child->getInput(0); // Should be Blend 102 EXPECT_EQ(child->asColorFilter(0), false); 103 EXPECT_EQ(child->countInputs(), 2); 104 child = child->getInput(0); // Should be CS (L->D) 105 EXPECT_EQ(child->asColorFilter(0), true); 106 EXPECT_EQ(child->countInputs(), 1); 107 child = child->getInput(0); // Should be Blur 108 EXPECT_EQ(child->asColorFilter(0), false); 109 EXPECT_EQ(child->countInputs(), 1); 110 child = child->getInput(0); // Should be CS (D->L) 111 EXPECT_EQ(child->asColorFilter(0), true); 112 EXPECT_EQ(child->countInputs(), 1); 113 } 114 }; 115 116 namespace { 117 118 TEST_F(ImageFilterBuilderTest, testColorSpace) 119 { 120 colorSpaceTest(); 121 } 122 123 } // namespace 124