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 <SkColorMatrixFilter.h>
21#include <SkColorSpace.h>
22#include <SkImagePriv.h>
23#include <SkPathOps.h>
24#include <SkShader.h>
25
26using namespace android;
27using namespace android::uirenderer;
28
29SkBitmap createSkBitmap(int width, int height) {
30    SkBitmap bitmap;
31    SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType);
32    bitmap.setInfo(info);
33    bitmap.allocPixels(info);
34    return bitmap;
35}
36
37/**
38 * 1x1 bitmaps must not be optimized into solid color shaders, since HWUI can't
39 * compose/render color shaders
40 */
41TEST(SkiaBehavior, CreateBitmapShader1x1) {
42    SkBitmap origBitmap = createSkBitmap(1, 1);
43    sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(origBitmap, kNever_SkCopyPixelsMode);
44    sk_sp<SkShader> s = image->makeShader(
45            SkShader::kClamp_TileMode,
46            SkShader::kRepeat_TileMode,
47            nullptr);
48
49    SkBitmap bitmap;
50    SkShader::TileMode xy[2];
51    ASSERT_TRUE(s->isABitmap(&bitmap, nullptr, xy))
52        << "1x1 bitmap shader must query as bitmap shader";
53    EXPECT_EQ(SkShader::kClamp_TileMode, xy[0]);
54    EXPECT_EQ(SkShader::kRepeat_TileMode, xy[1]);
55    EXPECT_EQ(origBitmap.pixelRef(), bitmap.pixelRef());
56}
57
58TEST(SkiaBehavior, genIds) {
59    SkBitmap bitmap = createSkBitmap(100, 100);
60    uint32_t genId = bitmap.getGenerationID();
61    bitmap.notifyPixelsChanged();
62    EXPECT_NE(genId, bitmap.getGenerationID());
63}
64
65TEST(SkiaBehavior, lightingColorFilter_simplify) {
66    {
67        sk_sp<SkColorFilter> filter(
68                SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0));
69
70        SkColor observedColor;
71        SkBlendMode observedMode;
72        ASSERT_TRUE(filter->asColorMode(&observedColor, &observedMode));
73        EXPECT_EQ(0xFF223344, observedColor);
74        EXPECT_EQ(SkBlendMode::kModulate, observedMode);
75    }
76
77    {
78        sk_sp<SkColorFilter> failFilter(
79                SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0x1));
80        EXPECT_FALSE(failFilter->asColorMode(nullptr, nullptr));
81    }
82}
83
84TEST(SkiaBehavior, porterDuffCreateIsCached) {
85    SkPaint paint;
86    paint.setBlendMode(SkBlendMode::kOverlay);
87    auto expected = paint.getBlendMode();
88    paint.setBlendMode(SkBlendMode::kClear);
89    ASSERT_NE(expected, paint.getBlendMode());
90    paint.setBlendMode(SkBlendMode::kOverlay);
91    ASSERT_EQ(expected, paint.getBlendMode());
92}
93
94TEST(SkiaBehavior, pathIntersection) {
95    SkPath p0, p1, result;
96    p0.addRect(SkRect::MakeXYWH(-5.0f, 0.0f, 1080.0f, 242.0f));
97    p1.addRect(SkRect::MakeXYWH(0.0f, 0.0f, 1080.0f, 242.0f));
98    Op(p0, p1, kIntersect_SkPathOp, &result);
99    SkRect resultRect;
100    ASSERT_TRUE(result.isRect(&resultRect));
101    ASSERT_EQ(SkRect::MakeXYWH(0.0f, 0.0f, 1075.0f, 242.0f), resultRect);
102}
103
104TEST(SkiaBehavior, srgbColorSpaceIsSingleton) {
105    sk_sp<SkColorSpace> sRGB1 = SkColorSpace::MakeSRGB();
106    sk_sp<SkColorSpace> sRGB2 = SkColorSpace::MakeSRGB();
107    ASSERT_EQ(sRGB1.get(), sRGB2.get());
108}
109