blob: 3f80d6ec65772b42095c291f7b2881fb89c89bf8 [file] [log] [blame]
Chris Craik07d8d592016-02-03 18:43:04 -08001/*
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>
Chris Craik20136f32016-04-29 17:38:31 -070020#include <SkColorMatrixFilter.h>
Romain Guy253f2c22016-09-28 17:34:42 -070021#include <SkColorSpace.h>
Matt Sarett62feb3a2016-09-20 10:34:11 -040022#include <SkImagePriv.h>
23#include <SkShader.h>
Chris Craik07d8d592016-02-03 18:43:04 -080024
25using namespace android;
26using namespace android::uirenderer;
27
sergeyvc36bd6c2016-10-11 15:49:16 -070028SkBitmap createSkBitmap(int width, int height) {
29 SkBitmap bitmap;
30 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType);
31 bitmap.setInfo(info);
32 bitmap.allocPixels(info);
33 return bitmap;
34}
35
Chris Craik07d8d592016-02-03 18:43:04 -080036/**
37 * 1x1 bitmaps must not be optimized into solid color shaders, since HWUI can't
38 * compose/render color shaders
39 */
40TEST(SkiaBehavior, CreateBitmapShader1x1) {
sergeyvc36bd6c2016-10-11 15:49:16 -070041 SkBitmap origBitmap = createSkBitmap(1, 1);
Matt Sarett62feb3a2016-09-20 10:34:11 -040042 sk_sp<SkShader> s = SkMakeBitmapShader(
Chris Craik07d8d592016-02-03 18:43:04 -080043 origBitmap,
44 SkShader::kClamp_TileMode,
Matt Sarett62feb3a2016-09-20 10:34:11 -040045 SkShader::kRepeat_TileMode,
46 nullptr,
47 kNever_SkCopyPixelsMode,
48 nullptr);
Chris Craik07d8d592016-02-03 18:43:04 -080049
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(SkShader::kClamp_TileMode, xy[0]);
55 EXPECT_EQ(SkShader::kRepeat_TileMode, xy[1]);
56 EXPECT_EQ(origBitmap.pixelRef(), bitmap.pixelRef());
57}
Chris Craikbee60922016-03-25 10:27:03 -070058
59TEST(SkiaBehavior, genIds) {
sergeyvc36bd6c2016-10-11 15:49:16 -070060 SkBitmap bitmap = createSkBitmap(100, 100);
Chris Craikbee60922016-03-25 10:27:03 -070061 uint32_t genId = bitmap.getGenerationID();
62 bitmap.notifyPixelsChanged();
63 EXPECT_NE(genId, bitmap.getGenerationID());
64}
Chris Craik20136f32016-04-29 17:38:31 -070065
66TEST(SkiaBehavior, lightingColorFilter_simplify) {
Chris Craik36ce80d2016-05-12 17:50:34 -070067 {
Mike Reed260ab722016-10-07 15:59:20 -040068 sk_sp<SkColorFilter> filter(
69 SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0));
Chris Craik20136f32016-04-29 17:38:31 -070070
Chris Craik36ce80d2016-05-12 17:50:34 -070071 SkColor observedColor;
Mike Reedc2f31df2016-10-28 17:21:45 -040072 SkBlendMode observedMode;
Chris Craik36ce80d2016-05-12 17:50:34 -070073 ASSERT_TRUE(filter->asColorMode(&observedColor, &observedMode));
74 EXPECT_EQ(0xFF223344, observedColor);
Mike Reedc2f31df2016-10-28 17:21:45 -040075 EXPECT_EQ(SkBlendMode::kModulate, observedMode);
Chris Craik36ce80d2016-05-12 17:50:34 -070076 }
Chris Craik20136f32016-04-29 17:38:31 -070077
Chris Craik36ce80d2016-05-12 17:50:34 -070078 {
Mike Reed260ab722016-10-07 15:59:20 -040079 sk_sp<SkColorFilter> failFilter(
80 SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0x1));
Chris Craik36ce80d2016-05-12 17:50:34 -070081 EXPECT_FALSE(failFilter->asColorMode(nullptr, nullptr));
82 }
Chris Craik20136f32016-04-29 17:38:31 -070083}
John Reck5d31a292016-09-15 10:13:10 -070084
85TEST(SkiaBehavior, porterDuffCreateIsCached) {
86 SkPaint paint;
Mike Reed260ab722016-10-07 15:59:20 -040087 paint.setBlendMode(SkBlendMode::kOverlay);
88 auto expected = paint.getBlendMode();
89 paint.setBlendMode(SkBlendMode::kClear);
90 ASSERT_NE(expected, paint.getBlendMode());
91 paint.setBlendMode(SkBlendMode::kOverlay);
92 ASSERT_EQ(expected, paint.getBlendMode());
John Reck5d31a292016-09-15 10:13:10 -070093}
Romain Guy253f2c22016-09-28 17:34:42 -070094
95TEST(SkiaBehavior, srgbColorSpaceIsSingleton) {
96 sk_sp<SkColorSpace> sRGB1 = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
97 sk_sp<SkColorSpace> sRGB2 = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
98 ASSERT_EQ(sRGB1.get(), sRGB2.get());
99}