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> 20 #include <SkBlurDrawLooper.h> 21 #include <SkColorMatrixFilter.h> 22 #include <SkColorSpace.h> 23 #include <SkImagePriv.h> 24 #include <SkPathOps.h> 25 #include <SkShader.h> 26 27 using namespace android; 28 using namespace android::uirenderer; 29 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 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) { 43 SkBitmap origBitmap = createSkBitmap(1, 1); 44 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(origBitmap, kNever_SkCopyPixelsMode); 45 sk_sp<SkShader> s = image->makeShader( 46 SkShader::kClamp_TileMode, 47 SkShader::kRepeat_TileMode, 48 nullptr); 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(origBitmap.pixelRef(), bitmap.pixelRef()); 55 } 56 57 TEST(SkiaBehavior, genIds) { 58 SkBitmap bitmap = createSkBitmap(100, 100); 59 uint32_t genId = bitmap.getGenerationID(); 60 bitmap.notifyPixelsChanged(); 61 EXPECT_NE(genId, bitmap.getGenerationID()); 62 } 63 64 TEST(SkiaBehavior, lightingColorFilter_simplify) { 65 { 66 sk_sp<SkColorFilter> filter( 67 SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0)); 68 69 SkColor observedColor; 70 SkBlendMode observedMode; 71 ASSERT_TRUE(filter->asColorMode(&observedColor, &observedMode)); 72 EXPECT_EQ(0xFF223344, observedColor); 73 EXPECT_EQ(SkBlendMode::kModulate, observedMode); 74 } 75 76 { 77 sk_sp<SkColorFilter> failFilter( 78 SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0x1)); 79 EXPECT_FALSE(failFilter->asColorMode(nullptr, nullptr)); 80 } 81 } 82 83 TEST(SkiaBehavior, porterDuffCreateIsCached) { 84 SkPaint paint; 85 paint.setBlendMode(SkBlendMode::kOverlay); 86 auto expected = paint.getBlendMode(); 87 paint.setBlendMode(SkBlendMode::kClear); 88 ASSERT_NE(expected, paint.getBlendMode()); 89 paint.setBlendMode(SkBlendMode::kOverlay); 90 ASSERT_EQ(expected, paint.getBlendMode()); 91 } 92 93 TEST(SkiaBehavior, pathIntersection) { 94 SkPath p0, p1, result; 95 p0.addRect(SkRect::MakeXYWH(-5.0f, 0.0f, 1080.0f, 242.0f)); 96 p1.addRect(SkRect::MakeXYWH(0.0f, 0.0f, 1080.0f, 242.0f)); 97 Op(p0, p1, kIntersect_SkPathOp, &result); 98 SkRect resultRect; 99 ASSERT_TRUE(result.isRect(&resultRect)); 100 ASSERT_EQ(SkRect::MakeXYWH(0.0f, 0.0f, 1075.0f, 242.0f), resultRect); 101 } 102 103 TEST(SkiaBehavior, srgbColorSpaceIsSingleton) { 104 sk_sp<SkColorSpace> sRGB1 = SkColorSpace::MakeSRGB(); 105 sk_sp<SkColorSpace> sRGB2 = SkColorSpace::MakeSRGB(); 106 ASSERT_EQ(sRGB1.get(), sRGB2.get()); 107 } 108 109 TEST(SkiaBehavior, blurDrawLooper) { 110 sk_sp<SkDrawLooper> looper = SkBlurDrawLooper::Make(SK_ColorRED, 5.0f, 3.0f, 4.0f); 111 112 SkDrawLooper::BlurShadowRec blur; 113 bool success = looper->asABlurShadow(&blur); 114 ASSERT_TRUE(success); 115 116 ASSERT_EQ(SK_ColorRED, blur.fColor); 117 ASSERT_EQ(5.0f, blur.fSigma); 118 ASSERT_EQ(3.0f, blur.fOffset.fX); 119 ASSERT_EQ(4.0f, blur.fOffset.fY); 120 } 121