Chris Craik | 07d8d59 | 2016-02-03 18:43:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "tests/common/TestUtils.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
Chris Craik | 20136f3 | 2016-04-29 17:38:31 -0700 | [diff] [blame] | 20 | #include <SkColorMatrixFilter.h> |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 21 | #include <SkColorSpace.h> |
Matt Sarett | 62feb3a | 2016-09-20 10:34:11 -0400 | [diff] [blame] | 22 | #include <SkImagePriv.h> |
| 23 | #include <SkShader.h> |
Chris Craik | 07d8d59 | 2016-02-03 18:43:04 -0800 | [diff] [blame] | 24 | |
| 25 | using namespace android; |
| 26 | using namespace android::uirenderer; |
| 27 | |
sergeyv | c36bd6c | 2016-10-11 15:49:16 -0700 | [diff] [blame] | 28 | SkBitmap createSkBitmap(int width, int height) { |
| 29 | SkBitmap bitmap; |
| 30 | SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType); |
| 31 | bitmap.setInfo(info); |
| 32 | bitmap.allocPixels(info); |
| 33 | return bitmap; |
| 34 | } |
| 35 | |
Chris Craik | 07d8d59 | 2016-02-03 18:43:04 -0800 | [diff] [blame] | 36 | /** |
| 37 | * 1x1 bitmaps must not be optimized into solid color shaders, since HWUI can't |
| 38 | * compose/render color shaders |
| 39 | */ |
| 40 | TEST(SkiaBehavior, CreateBitmapShader1x1) { |
sergeyv | c36bd6c | 2016-10-11 15:49:16 -0700 | [diff] [blame] | 41 | SkBitmap origBitmap = createSkBitmap(1, 1); |
Matt Sarett | 62feb3a | 2016-09-20 10:34:11 -0400 | [diff] [blame] | 42 | sk_sp<SkShader> s = SkMakeBitmapShader( |
Chris Craik | 07d8d59 | 2016-02-03 18:43:04 -0800 | [diff] [blame] | 43 | origBitmap, |
| 44 | SkShader::kClamp_TileMode, |
Matt Sarett | 62feb3a | 2016-09-20 10:34:11 -0400 | [diff] [blame] | 45 | SkShader::kRepeat_TileMode, |
| 46 | nullptr, |
| 47 | kNever_SkCopyPixelsMode, |
| 48 | nullptr); |
Chris Craik | 07d8d59 | 2016-02-03 18:43:04 -0800 | [diff] [blame] | 49 | |
| 50 | SkBitmap bitmap; |
| 51 | SkShader::TileMode xy[2]; |
| 52 | ASSERT_TRUE(s->isABitmap(&bitmap, nullptr, xy)) |
| 53 | << "1x1 bitmap shader must query as bitmap shader"; |
| 54 | EXPECT_EQ(SkShader::kClamp_TileMode, xy[0]); |
| 55 | EXPECT_EQ(SkShader::kRepeat_TileMode, xy[1]); |
| 56 | EXPECT_EQ(origBitmap.pixelRef(), bitmap.pixelRef()); |
| 57 | } |
Chris Craik | bee6092 | 2016-03-25 10:27:03 -0700 | [diff] [blame] | 58 | |
| 59 | TEST(SkiaBehavior, genIds) { |
sergeyv | c36bd6c | 2016-10-11 15:49:16 -0700 | [diff] [blame] | 60 | SkBitmap bitmap = createSkBitmap(100, 100); |
Chris Craik | bee6092 | 2016-03-25 10:27:03 -0700 | [diff] [blame] | 61 | uint32_t genId = bitmap.getGenerationID(); |
| 62 | bitmap.notifyPixelsChanged(); |
| 63 | EXPECT_NE(genId, bitmap.getGenerationID()); |
| 64 | } |
Chris Craik | 20136f3 | 2016-04-29 17:38:31 -0700 | [diff] [blame] | 65 | |
| 66 | TEST(SkiaBehavior, lightingColorFilter_simplify) { |
Chris Craik | 36ce80d | 2016-05-12 17:50:34 -0700 | [diff] [blame] | 67 | { |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 68 | sk_sp<SkColorFilter> filter( |
| 69 | SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0)); |
Chris Craik | 20136f3 | 2016-04-29 17:38:31 -0700 | [diff] [blame] | 70 | |
Chris Craik | 36ce80d | 2016-05-12 17:50:34 -0700 | [diff] [blame] | 71 | SkColor observedColor; |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame^] | 72 | SkBlendMode observedMode; |
Chris Craik | 36ce80d | 2016-05-12 17:50:34 -0700 | [diff] [blame] | 73 | ASSERT_TRUE(filter->asColorMode(&observedColor, &observedMode)); |
| 74 | EXPECT_EQ(0xFF223344, observedColor); |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame^] | 75 | EXPECT_EQ(SkBlendMode::kModulate, observedMode); |
Chris Craik | 36ce80d | 2016-05-12 17:50:34 -0700 | [diff] [blame] | 76 | } |
Chris Craik | 20136f3 | 2016-04-29 17:38:31 -0700 | [diff] [blame] | 77 | |
Chris Craik | 36ce80d | 2016-05-12 17:50:34 -0700 | [diff] [blame] | 78 | { |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 79 | sk_sp<SkColorFilter> failFilter( |
| 80 | SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0x1)); |
Chris Craik | 36ce80d | 2016-05-12 17:50:34 -0700 | [diff] [blame] | 81 | EXPECT_FALSE(failFilter->asColorMode(nullptr, nullptr)); |
| 82 | } |
Chris Craik | 20136f3 | 2016-04-29 17:38:31 -0700 | [diff] [blame] | 83 | } |
John Reck | 5d31a29 | 2016-09-15 10:13:10 -0700 | [diff] [blame] | 84 | |
| 85 | TEST(SkiaBehavior, porterDuffCreateIsCached) { |
| 86 | SkPaint paint; |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 87 | paint.setBlendMode(SkBlendMode::kOverlay); |
| 88 | auto expected = paint.getBlendMode(); |
| 89 | paint.setBlendMode(SkBlendMode::kClear); |
| 90 | ASSERT_NE(expected, paint.getBlendMode()); |
| 91 | paint.setBlendMode(SkBlendMode::kOverlay); |
| 92 | ASSERT_EQ(expected, paint.getBlendMode()); |
John Reck | 5d31a29 | 2016-09-15 10:13:10 -0700 | [diff] [blame] | 93 | } |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 94 | |
| 95 | TEST(SkiaBehavior, srgbColorSpaceIsSingleton) { |
| 96 | sk_sp<SkColorSpace> sRGB1 = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
| 97 | sk_sp<SkColorSpace> sRGB2 = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
| 98 | ASSERT_EQ(sRGB1.get(), sRGB2.get()); |
| 99 | } |