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
TEST(SkiaBehavior,genIds)38 TEST(SkiaBehavior, genIds) {
39 SkBitmap bitmap = createSkBitmap(100, 100);
40 uint32_t genId = bitmap.getGenerationID();
41 bitmap.notifyPixelsChanged();
42 EXPECT_NE(genId, bitmap.getGenerationID());
43 }
44
TEST(SkiaBehavior,lightingColorFilter_simplify)45 TEST(SkiaBehavior, lightingColorFilter_simplify) {
46 {
47 sk_sp<SkColorFilter> filter(SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0));
48
49 SkColor observedColor;
50 SkBlendMode observedMode;
51 ASSERT_TRUE(filter->asAColorMode(&observedColor, &observedMode));
52 EXPECT_EQ(0xFF223344, observedColor);
53 EXPECT_EQ(SkBlendMode::kModulate, observedMode);
54 }
55
56 {
57 sk_sp<SkColorFilter> failFilter(SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0x1));
58 EXPECT_FALSE(failFilter->asAColorMode(nullptr, nullptr));
59 }
60 }
61
TEST(SkiaBehavior,porterDuffCreateIsCached)62 TEST(SkiaBehavior, porterDuffCreateIsCached) {
63 SkPaint paint;
64 paint.setBlendMode(SkBlendMode::kOverlay);
65 auto expected = paint.getBlendMode();
66 paint.setBlendMode(SkBlendMode::kClear);
67 ASSERT_NE(expected, paint.getBlendMode());
68 paint.setBlendMode(SkBlendMode::kOverlay);
69 ASSERT_EQ(expected, paint.getBlendMode());
70 }
71
TEST(SkiaBehavior,pathIntersection)72 TEST(SkiaBehavior, pathIntersection) {
73 SkPath p0, p1, result;
74 p0.addRect(SkRect::MakeXYWH(-5.0f, 0.0f, 1080.0f, 242.0f));
75 p1.addRect(SkRect::MakeXYWH(0.0f, 0.0f, 1080.0f, 242.0f));
76 Op(p0, p1, kIntersect_SkPathOp, &result);
77 SkRect resultRect;
78 ASSERT_TRUE(result.isRect(&resultRect));
79 ASSERT_EQ(SkRect::MakeXYWH(0.0f, 0.0f, 1075.0f, 242.0f), resultRect);
80 }
81
TEST(SkiaBehavior,srgbColorSpaceIsSingleton)82 TEST(SkiaBehavior, srgbColorSpaceIsSingleton) {
83 sk_sp<SkColorSpace> sRGB1 = SkColorSpace::MakeSRGB();
84 sk_sp<SkColorSpace> sRGB2 = SkColorSpace::MakeSRGB();
85 ASSERT_EQ(sRGB1.get(), sRGB2.get());
86 }
87
TEST(SkiaBehavior,blurDrawLooper)88 TEST(SkiaBehavior, blurDrawLooper) {
89 sk_sp<SkDrawLooper> looper = SkBlurDrawLooper::Make(SK_ColorRED, 5.0f, 3.0f, 4.0f);
90
91 SkDrawLooper::BlurShadowRec blur;
92 bool success = looper->asABlurShadow(&blur);
93 ASSERT_TRUE(success);
94
95 ASSERT_EQ(SK_ColorRED, blur.fColor);
96 ASSERT_EQ(5.0f, blur.fSigma);
97 ASSERT_EQ(3.0f, blur.fOffset.fX);
98 ASSERT_EQ(4.0f, blur.fOffset.fY);
99 }
100