blob: bc742b0c5a6321f56f01bca366db184d152949e3 [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
Matt Sarett3d213592017-03-24 16:18:42 -040019#include <SkBlurDrawLooper.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>
Matt Sarette07a91a2017-04-18 18:09:52 -040023#include <SkPathOps.h>
Matt Sarett62feb3a2016-09-20 10:34:11 -040024#include <SkShader.h>
John Reck1bcacfd2017-11-03 10:12:19 -070025#include <gtest/gtest.h>
Chris Craik07d8d592016-02-03 18:43:04 -080026
27using namespace android;
28using namespace android::uirenderer;
29
sergeyvc36bd6c2016-10-11 15:49:16 -070030SkBitmap 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
Chris Craik07d8d592016-02-03 18:43:04 -080038/**
39 * 1x1 bitmaps must not be optimized into solid color shaders, since HWUI can't
40 * compose/render color shaders
41 */
42TEST(SkiaBehavior, CreateBitmapShader1x1) {
sergeyvc36bd6c2016-10-11 15:49:16 -070043 SkBitmap origBitmap = createSkBitmap(1, 1);
Matt Sarettc54b76e2017-02-13 17:01:54 -050044 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(origBitmap, kNever_SkCopyPixelsMode);
John Reck1bcacfd2017-11-03 10:12:19 -070045 sk_sp<SkShader> s =
46 image->makeShader(SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode, nullptr);
Chris Craik07d8d592016-02-03 18:43:04 -080047
48 SkBitmap bitmap;
49 SkShader::TileMode xy[2];
50 ASSERT_TRUE(s->isABitmap(&bitmap, nullptr, xy))
John Reck1bcacfd2017-11-03 10:12:19 -070051 << "1x1 bitmap shader must query as bitmap shader";
Chris Craik07d8d592016-02-03 18:43:04 -080052 EXPECT_EQ(origBitmap.pixelRef(), bitmap.pixelRef());
53}
Chris Craikbee60922016-03-25 10:27:03 -070054
55TEST(SkiaBehavior, genIds) {
sergeyvc36bd6c2016-10-11 15:49:16 -070056 SkBitmap bitmap = createSkBitmap(100, 100);
Chris Craikbee60922016-03-25 10:27:03 -070057 uint32_t genId = bitmap.getGenerationID();
58 bitmap.notifyPixelsChanged();
59 EXPECT_NE(genId, bitmap.getGenerationID());
60}
Chris Craik20136f32016-04-29 17:38:31 -070061
62TEST(SkiaBehavior, lightingColorFilter_simplify) {
Chris Craik36ce80d2016-05-12 17:50:34 -070063 {
John Reck1bcacfd2017-11-03 10:12:19 -070064 sk_sp<SkColorFilter> filter(SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0));
Chris Craik20136f32016-04-29 17:38:31 -070065
Chris Craik36ce80d2016-05-12 17:50:34 -070066 SkColor observedColor;
Mike Reedc2f31df2016-10-28 17:21:45 -040067 SkBlendMode observedMode;
Chris Craik36ce80d2016-05-12 17:50:34 -070068 ASSERT_TRUE(filter->asColorMode(&observedColor, &observedMode));
69 EXPECT_EQ(0xFF223344, observedColor);
Mike Reedc2f31df2016-10-28 17:21:45 -040070 EXPECT_EQ(SkBlendMode::kModulate, observedMode);
Chris Craik36ce80d2016-05-12 17:50:34 -070071 }
Chris Craik20136f32016-04-29 17:38:31 -070072
Chris Craik36ce80d2016-05-12 17:50:34 -070073 {
John Reck1bcacfd2017-11-03 10:12:19 -070074 sk_sp<SkColorFilter> failFilter(SkColorMatrixFilter::MakeLightingFilter(0x11223344, 0x1));
Chris Craik36ce80d2016-05-12 17:50:34 -070075 EXPECT_FALSE(failFilter->asColorMode(nullptr, nullptr));
76 }
Chris Craik20136f32016-04-29 17:38:31 -070077}
John Reck5d31a292016-09-15 10:13:10 -070078
79TEST(SkiaBehavior, porterDuffCreateIsCached) {
80 SkPaint paint;
Mike Reed260ab722016-10-07 15:59:20 -040081 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());
John Reck5d31a292016-09-15 10:13:10 -070087}
Romain Guy253f2c22016-09-28 17:34:42 -070088
Matt Sarette07a91a2017-04-18 18:09:52 -040089TEST(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
Romain Guy253f2c22016-09-28 17:34:42 -070099TEST(SkiaBehavior, srgbColorSpaceIsSingleton) {
Matt Sarett89ddb1f2017-02-10 13:31:56 -0500100 sk_sp<SkColorSpace> sRGB1 = SkColorSpace::MakeSRGB();
101 sk_sp<SkColorSpace> sRGB2 = SkColorSpace::MakeSRGB();
Romain Guy253f2c22016-09-28 17:34:42 -0700102 ASSERT_EQ(sRGB1.get(), sRGB2.get());
103}
Matt Sarett3d213592017-03-24 16:18:42 -0400104
105TEST(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}