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 | |
Matt Sarett | 3d21359 | 2017-03-24 16:18:42 -0400 | [diff] [blame] | 19 | #include <SkBlurDrawLooper.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> |
Matt Sarett | e07a91a | 2017-04-18 18:09:52 -0400 | [diff] [blame] | 23 | #include <SkPathOps.h> |
Matt Sarett | 62feb3a | 2016-09-20 10:34:11 -0400 | [diff] [blame] | 24 | #include <SkShader.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
Chris Craik | 07d8d59 | 2016-02-03 18:43:04 -0800 | [diff] [blame] | 26 | |
| 27 | using namespace android; |
| 28 | using namespace android::uirenderer; |
| 29 | |
sergeyv | c36bd6c | 2016-10-11 15:49:16 -0700 | [diff] [blame] | 30 | SkBitmap createSkBitmap(int width, int height) { |
| 31 | SkBitmap bitmap; |
| 32 | SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType); |
| 33 | bitmap.setInfo(info); |
| 34 | bitmap.allocPixels(info); |
| 35 | return bitmap; |
| 36 | } |
| 37 | |
Chris Craik | 07d8d59 | 2016-02-03 18:43:04 -0800 | [diff] [blame] | 38 | /** |
| 39 | * 1x1 bitmaps must not be optimized into solid color shaders, since HWUI can't |
| 40 | * compose/render color shaders |
| 41 | */ |
| 42 | TEST(SkiaBehavior, CreateBitmapShader1x1) { |
sergeyv | c36bd6c | 2016-10-11 15:49:16 -0700 | [diff] [blame] | 43 | SkBitmap origBitmap = createSkBitmap(1, 1); |
Matt Sarett | c54b76e | 2017-02-13 17:01:54 -0500 | [diff] [blame] | 44 | sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(origBitmap, kNever_SkCopyPixelsMode); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 45 | sk_sp<SkShader> s = |
| 46 | image->makeShader(SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, nullptr); |
Chris Craik | 07d8d59 | 2016-02-03 18:43:04 -0800 | [diff] [blame] | 47 | |
| 48 | SkBitmap bitmap; |
| 49 | SkShader::TileMode xy[2]; |
| 50 | ASSERT_TRUE(s->isABitmap(&bitmap, nullptr, xy)) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 51 | << "1x1 bitmap shader must query as bitmap shader"; |
Chris Craik | 07d8d59 | 2016-02-03 18:43:04 -0800 | [diff] [blame] | 52 | EXPECT_EQ(origBitmap.pixelRef(), bitmap.pixelRef()); |
| 53 | } |
Chris Craik | bee6092 | 2016-03-25 10:27:03 -0700 | [diff] [blame] | 54 | |
| 55 | TEST(SkiaBehavior, genIds) { |
sergeyv | c36bd6c | 2016-10-11 15:49:16 -0700 | [diff] [blame] | 56 | SkBitmap bitmap = createSkBitmap(100, 100); |
Chris Craik | bee6092 | 2016-03-25 10:27:03 -0700 | [diff] [blame] | 57 | uint32_t genId = bitmap.getGenerationID(); |
| 58 | bitmap.notifyPixelsChanged(); |
| 59 | EXPECT_NE(genId, bitmap.getGenerationID()); |
| 60 | } |
Chris Craik | 20136f3 | 2016-04-29 17:38:31 -0700 | [diff] [blame] | 61 | |
| 62 | TEST(SkiaBehavior, lightingColorFilter_simplify) { |
Chris Craik | 36ce80d | 2016-05-12 17:50:34 -0700 | [diff] [blame] | 63 | { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 64 | sk_sp<SkColorFilter> filter(SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0)); |
Chris Craik | 20136f3 | 2016-04-29 17:38:31 -0700 | [diff] [blame] | 65 | |
Chris Craik | 36ce80d | 2016-05-12 17:50:34 -0700 | [diff] [blame] | 66 | SkColor observedColor; |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 67 | SkBlendMode observedMode; |
Chris Craik | 36ce80d | 2016-05-12 17:50:34 -0700 | [diff] [blame] | 68 | ASSERT_TRUE(filter->asColorMode(&observedColor, &observedMode)); |
| 69 | EXPECT_EQ(0xFF223344, observedColor); |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 70 | EXPECT_EQ(SkBlendMode::kModulate, observedMode); |
Chris Craik | 36ce80d | 2016-05-12 17:50:34 -0700 | [diff] [blame] | 71 | } |
Chris Craik | 20136f3 | 2016-04-29 17:38:31 -0700 | [diff] [blame] | 72 | |
Chris Craik | 36ce80d | 2016-05-12 17:50:34 -0700 | [diff] [blame] | 73 | { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 74 | sk_sp<SkColorFilter> failFilter(SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0x1)); |
Chris Craik | 36ce80d | 2016-05-12 17:50:34 -0700 | [diff] [blame] | 75 | EXPECT_FALSE(failFilter->asColorMode(nullptr, nullptr)); |
| 76 | } |
Chris Craik | 20136f3 | 2016-04-29 17:38:31 -0700 | [diff] [blame] | 77 | } |
John Reck | 5d31a29 | 2016-09-15 10:13:10 -0700 | [diff] [blame] | 78 | |
| 79 | TEST(SkiaBehavior, porterDuffCreateIsCached) { |
| 80 | SkPaint paint; |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 81 | paint.setBlendMode(SkBlendMode::kOverlay); |
| 82 | auto expected = paint.getBlendMode(); |
| 83 | paint.setBlendMode(SkBlendMode::kClear); |
| 84 | ASSERT_NE(expected, paint.getBlendMode()); |
| 85 | paint.setBlendMode(SkBlendMode::kOverlay); |
| 86 | ASSERT_EQ(expected, paint.getBlendMode()); |
John Reck | 5d31a29 | 2016-09-15 10:13:10 -0700 | [diff] [blame] | 87 | } |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 88 | |
Matt Sarett | e07a91a | 2017-04-18 18:09:52 -0400 | [diff] [blame] | 89 | TEST(SkiaBehavior, pathIntersection) { |
| 90 | SkPath p0, p1, result; |
| 91 | p0.addRect(SkRect::MakeXYWH(-5.0f, 0.0f, 1080.0f, 242.0f)); |
| 92 | p1.addRect(SkRect::MakeXYWH(0.0f, 0.0f, 1080.0f, 242.0f)); |
| 93 | Op(p0, p1, kIntersect_SkPathOp, &result); |
| 94 | SkRect resultRect; |
| 95 | ASSERT_TRUE(result.isRect(&resultRect)); |
| 96 | ASSERT_EQ(SkRect::MakeXYWH(0.0f, 0.0f, 1075.0f, 242.0f), resultRect); |
| 97 | } |
| 98 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 99 | TEST(SkiaBehavior, srgbColorSpaceIsSingleton) { |
Matt Sarett | 89ddb1f | 2017-02-10 13:31:56 -0500 | [diff] [blame] | 100 | sk_sp<SkColorSpace> sRGB1 = SkColorSpace::MakeSRGB(); |
| 101 | sk_sp<SkColorSpace> sRGB2 = SkColorSpace::MakeSRGB(); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 102 | ASSERT_EQ(sRGB1.get(), sRGB2.get()); |
| 103 | } |
Matt Sarett | 3d21359 | 2017-03-24 16:18:42 -0400 | [diff] [blame] | 104 | |
| 105 | TEST(SkiaBehavior, blurDrawLooper) { |
| 106 | sk_sp<SkDrawLooper> looper = SkBlurDrawLooper::Make(SK_ColorRED, 5.0f, 3.0f, 4.0f); |
| 107 | |
| 108 | SkDrawLooper::BlurShadowRec blur; |
| 109 | bool success = looper->asABlurShadow(&blur); |
| 110 | ASSERT_TRUE(success); |
| 111 | |
| 112 | ASSERT_EQ(SK_ColorRED, blur.fColor); |
| 113 | ASSERT_EQ(5.0f, blur.fSigma); |
| 114 | ASSERT_EQ(3.0f, blur.fOffset.fX); |
| 115 | ASSERT_EQ(4.0f, blur.fOffset.fY); |
| 116 | } |