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 <SkBlurDrawLooper.h>
20 #include <SkColorMatrixFilter.h>
21 #include <SkColorSpace.h>
22 #include <SkImagePriv.h>
23 #include <SkPathOps.h>
24 #include <SkShader.h>
25 #include <gtest/gtest.h>
26
27 using namespace android;
28 using namespace android::uirenderer;
29
createSkBitmap(int width,int height)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 */
TEST(SkiaBehavior,CreateBitmapShader1x1)42 TEST(SkiaBehavior, CreateBitmapShader1x1) {
43 SkBitmap origBitmap = createSkBitmap(1, 1);
44 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(origBitmap, kNever_SkCopyPixelsMode);
45 sk_sp<SkShader> s =
46 image->makeShader(SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, nullptr);
47
48 SkBitmap bitmap;
49 SkShader::TileMode xy[2];
50 ASSERT_TRUE(s->isABitmap(&bitmap, nullptr, xy))
51 << "1x1 bitmap shader must query as bitmap shader";
52 EXPECT_EQ(origBitmap.pixelRef(), bitmap.pixelRef());
53 }
54
TEST(SkiaBehavior,genIds)55 TEST(SkiaBehavior, genIds) {
56 SkBitmap bitmap = createSkBitmap(100, 100);
57 uint32_t genId = bitmap.getGenerationID();
58 bitmap.notifyPixelsChanged();
59 EXPECT_NE(genId, bitmap.getGenerationID());
60 }
61
TEST(SkiaBehavior,lightingColorFilter_simplify)62 TEST(SkiaBehavior, lightingColorFilter_simplify) {
63 {
64 sk_sp<SkColorFilter> filter(SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0));
65
66 SkColor observedColor;
67 SkBlendMode observedMode;
68 ASSERT_TRUE(filter->asColorMode(&observedColor, &observedMode));
69 EXPECT_EQ(0xFF223344, observedColor);
70 EXPECT_EQ(SkBlendMode::kModulate, observedMode);
71 }
72
73 {
74 sk_sp<SkColorFilter> failFilter(SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0x1));
75 EXPECT_FALSE(failFilter->asColorMode(nullptr, nullptr));
76 }
77 }
78
TEST(SkiaBehavior,porterDuffCreateIsCached)79 TEST(SkiaBehavior, porterDuffCreateIsCached) {
80 SkPaint paint;
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());
87 }
88
TEST(SkiaBehavior,pathIntersection)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
TEST(SkiaBehavior,srgbColorSpaceIsSingleton)99 TEST(SkiaBehavior, srgbColorSpaceIsSingleton) {
100 sk_sp<SkColorSpace> sRGB1 = SkColorSpace::MakeSRGB();
101 sk_sp<SkColorSpace> sRGB2 = SkColorSpace::MakeSRGB();
102 ASSERT_EQ(sRGB1.get(), sRGB2.get());
103 }
104
TEST(SkiaBehavior,blurDrawLooper)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 }
117