| // Copyright 2016 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "base/test/task_environment.h" |
| #include "mojo/public/cpp/bindings/receiver_set.h" |
| #include "mojo/public/cpp/bindings/remote.h" |
| #include "skia/public/mojom/test/traits_test_service.mojom.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "third_party/skia/include/core/SkColorFilter.h" |
| #include "third_party/skia/include/core/SkImageInfo.h" |
| #include "third_party/skia/include/core/SkString.h" |
| #include "third_party/skia/include/effects/SkBlurImageFilter.h" |
| #include "third_party/skia/include/effects/SkColorFilterImageFilter.h" |
| #include "third_party/skia/include/effects/SkDropShadowImageFilter.h" |
| #include "ui/gfx/skia_util.h" |
| |
| namespace skia { |
| |
| namespace { |
| |
| class StructTraitsTest : public testing::Test, public mojom::TraitsTestService { |
| public: |
| StructTraitsTest() = default; |
| |
| protected: |
| mojo::Remote<mojom::TraitsTestService> GetTraitsTestRemote() { |
| mojo::Remote<mojom::TraitsTestService> remote; |
| traits_test_receivers_.Add(this, remote.BindNewPipeAndPassReceiver()); |
| return remote; |
| } |
| |
| private: |
| // TraitsTestService: |
| void EchoImageInfo(const SkImageInfo& i, |
| EchoImageInfoCallback callback) override { |
| std::move(callback).Run(i); |
| } |
| |
| void EchoBitmap(const SkBitmap& b, EchoBitmapCallback callback) override { |
| std::move(callback).Run(b); |
| } |
| |
| void EchoBlurImageFilterTileMode( |
| SkBlurImageFilter::TileMode t, |
| EchoBlurImageFilterTileModeCallback callback) override { |
| std::move(callback).Run(t); |
| } |
| |
| base::test::TaskEnvironment task_environment_; |
| mojo::ReceiverSet<TraitsTestService> traits_test_receivers_; |
| |
| DISALLOW_COPY_AND_ASSIGN(StructTraitsTest); |
| }; |
| |
| } // namespace |
| |
| TEST_F(StructTraitsTest, ImageInfo) { |
| SkImageInfo input = SkImageInfo::Make( |
| 34, 56, SkColorType::kGray_8_SkColorType, |
| SkAlphaType::kUnpremul_SkAlphaType, |
| SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kAdobeRGB)); |
| mojo::Remote<mojom::TraitsTestService> remote = GetTraitsTestRemote(); |
| SkImageInfo output; |
| remote->EchoImageInfo(input, &output); |
| EXPECT_EQ(input, output); |
| |
| SkImageInfo another_input_with_null_color_space = |
| SkImageInfo::Make(54, 43, SkColorType::kRGBA_8888_SkColorType, |
| SkAlphaType::kPremul_SkAlphaType, nullptr); |
| remote->EchoImageInfo(another_input_with_null_color_space, &output); |
| EXPECT_FALSE(output.colorSpace()); |
| EXPECT_EQ(another_input_with_null_color_space, output); |
| } |
| |
| TEST_F(StructTraitsTest, Bitmap) { |
| SkBitmap input; |
| input.allocPixels(SkImageInfo::MakeN32Premul( |
| 10, 5, |
| SkColorSpace::MakeRGB(SkNamedTransferFn::kLinear, |
| SkNamedGamut::kRec2020))); |
| input.eraseColor(SK_ColorYELLOW); |
| input.erase(SK_ColorTRANSPARENT, SkIRect::MakeXYWH(0, 1, 2, 3)); |
| mojo::Remote<mojom::TraitsTestService> remote = GetTraitsTestRemote(); |
| SkBitmap output; |
| remote->EchoBitmap(input, &output); |
| EXPECT_EQ(input.info(), output.info()); |
| EXPECT_EQ(input.rowBytes(), output.rowBytes()); |
| EXPECT_TRUE(gfx::BitmapsAreEqual(input, output)); |
| } |
| |
| TEST_F(StructTraitsTest, BitmapWithExtraRowBytes) { |
| SkBitmap input; |
| // Ensure traits work with bitmaps containing additional bytes between rows. |
| SkImageInfo info = |
| SkImageInfo::MakeN32(8, 5, kPremul_SkAlphaType, SkColorSpace::MakeSRGB()); |
| // Any extra bytes on each row must be a multiple of the row's pixel size to |
| // keep every row's pixels aligned. |
| size_t extra = info.bytesPerPixel(); |
| input.allocPixels(info, info.minRowBytes() + extra); |
| input.eraseColor(SK_ColorRED); |
| input.erase(SK_ColorTRANSPARENT, SkIRect::MakeXYWH(0, 1, 2, 3)); |
| mojo::Remote<mojom::TraitsTestService> remote = GetTraitsTestRemote(); |
| SkBitmap output; |
| remote->EchoBitmap(input, &output); |
| EXPECT_EQ(input.info(), output.info()); |
| EXPECT_EQ(input.rowBytes(), output.rowBytes()); |
| EXPECT_TRUE(gfx::BitmapsAreEqual(input, output)); |
| } |
| |
| TEST_F(StructTraitsTest, BlurImageFilterTileMode) { |
| SkBlurImageFilter::TileMode input(SkBlurImageFilter::kClamp_TileMode); |
| mojo::Remote<mojom::TraitsTestService> remote = GetTraitsTestRemote(); |
| SkBlurImageFilter::TileMode output; |
| remote->EchoBlurImageFilterTileMode(input, &output); |
| EXPECT_EQ(input, output); |
| } |
| |
| } // namespace skia |