blob: 50d9f5683a8bf3294599d94051895155fe8715b9 [file] [log] [blame]
Derek Sollenberger35934cc2016-03-23 14:59:10 -04001/*
2 * Copyright (C) 2016 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
Mike Reedc2dbc032019-07-25 12:28:29 -040019#include <hwui/Paint.h>
Matt Sarett44dc2702017-04-13 09:33:18 -040020#include <SkCanvasStateUtils.h>
Kevin Lubick07d6aae2022-04-01 14:03:11 -040021#include <SkColorSpace.h>
Derek Sollenberger35934cc2016-03-23 14:59:10 -040022#include <SkPicture.h>
23#include <SkPictureRecorder.h>
John Reck1bcacfd2017-11-03 10:12:19 -070024#include <gtest/gtest.h>
Derek Sollenberger35934cc2016-03-23 14:59:10 -040025
26using namespace android;
27using namespace android::uirenderer;
28
Derek Sollenberger792fb322017-03-03 14:02:09 -050029TEST(SkiaCanvas, drawShadowLayer) {
30 auto surface = SkSurface::MakeRasterN32Premul(10, 10);
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040031 SkiaCanvas canvas(surface->getCanvas());
Derek Sollenberger792fb322017-03-03 14:02:09 -050032
33 // clear to white
34 canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrc);
35
Mike Reedc2dbc032019-07-25 12:28:29 -040036 Paint paint;
Derek Sollenberger792fb322017-03-03 14:02:09 -050037 // it is transparent to ensure that we still draw the rect since it has a looper
38 paint.setColor(SK_ColorTRANSPARENT);
39 // this is how view's shadow layers are implemented
Mike Reed0f9dce72021-02-12 21:20:33 -050040 paint.setLooper(BlurDrawLooper::Make({0, 0, 0, 240.0f / 255}, nullptr, 6.0f, {0, 10}));
Derek Sollenberger792fb322017-03-03 14:02:09 -050041 canvas.drawRect(3, 3, 7, 7, paint);
42
43 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorWHITE);
44 ASSERT_NE(TestUtils::getColor(surface, 5, 5), SK_ColorWHITE);
45}
Matt Sarettea70d222017-03-29 16:25:10 -040046
47TEST(SkiaCanvas, colorSpaceXform) {
Brian Osmanbe8fac262019-01-14 17:02:23 -050048 sk_sp<SkColorSpace> adobe = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
49 SkNamedGamut::kAdobeRGB);
Matt Sarettea70d222017-03-29 16:25:10 -040050
51 SkImageInfo adobeInfo = SkImageInfo::Make(1, 1, kN32_SkColorType, kOpaque_SkAlphaType, adobe);
52 sk_sp<Bitmap> adobeBitmap = Bitmap::allocateHeapBitmap(adobeInfo);
53 SkBitmap adobeSkBitmap;
54 adobeBitmap->getSkBitmap(&adobeSkBitmap);
John Reck1bcacfd2017-11-03 10:12:19 -070055 *adobeSkBitmap.getAddr32(0, 0) = 0xFF0000F0; // Opaque, almost fully-red
Matt Sarettea70d222017-03-29 16:25:10 -040056
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040057 SkImageInfo info = adobeInfo.makeColorSpace(SkColorSpace::MakeSRGB());
Matt Sarettea70d222017-03-29 16:25:10 -040058 sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(info);
59 SkBitmap skBitmap;
60 bitmap->getSkBitmap(&skBitmap);
61
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040062 // Create a software sRGB canvas.
Matt Sarettea70d222017-03-29 16:25:10 -040063 SkiaCanvas canvas(skBitmap);
64 canvas.drawBitmap(*adobeBitmap, 0, 0, nullptr);
65 // The result should be fully red, since we convert to sRGB at draw time.
Matt Sarettea70d222017-03-29 16:25:10 -040066 ASSERT_EQ(0xFF0000FF, *skBitmap.getAddr32(0, 0));
67
Matt Sarettca9b7032017-04-13 12:18:47 -040068 // Create a software canvas with an Adobe color space.
69 SkiaCanvas adobeSkCanvas(adobeSkBitmap);
70 adobeSkCanvas.drawBitmap(*bitmap, 0, 0, nullptr);
71 // The result should be less than fully red, since we convert to Adobe RGB at draw time.
72 ASSERT_EQ(0xFF0000DC, *adobeSkBitmap.getAddr32(0, 0));
73
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040074 // Test picture recording.
Matt Sarettea70d222017-03-29 16:25:10 -040075 SkPictureRecorder recorder;
Mike Reed63a132d2020-08-21 12:43:09 -040076 SkCanvas* skPicCanvas = recorder.beginRecording(1, 1);
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040077 SkiaCanvas picCanvas(skPicCanvas);
Matt Sarettea70d222017-03-29 16:25:10 -040078 picCanvas.drawBitmap(*adobeBitmap, 0, 0, nullptr);
79 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
80
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040081 // Playback to a software sRGB canvas. The result should be fully red.
John Reck894e85a2019-07-15 16:16:44 -070082 canvas.drawPicture(*picture);
Matt Sarettea70d222017-03-29 16:25:10 -040083 ASSERT_EQ(0xFF0000FF, *skBitmap.getAddr32(0, 0));
84}
Matt Sarett44dc2702017-04-13 09:33:18 -040085
86TEST(SkiaCanvas, captureCanvasState) {
87 // Create a software canvas.
88 SkImageInfo info = SkImageInfo::Make(1, 1, kN32_SkColorType, kOpaque_SkAlphaType);
89 sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(info);
90 SkBitmap skBitmap;
91 bitmap->getSkBitmap(&skBitmap);
92 skBitmap.eraseColor(0);
93 SkiaCanvas canvas(skBitmap);
94
95 // Translate, then capture and verify the CanvasState.
96 canvas.translate(1.0f, 1.0f);
97 SkCanvasState* state = canvas.captureCanvasState();
98 ASSERT_NE(state, nullptr);
99 std::unique_ptr<SkCanvas> newCanvas = SkCanvasStateUtils::MakeFromCanvasState(state);
100 ASSERT_NE(newCanvas.get(), nullptr);
101 newCanvas->translate(-1.0f, -1.0f);
102 ASSERT_TRUE(newCanvas->getTotalMatrix().isIdentity());
103 SkCanvasStateUtils::ReleaseCanvasState(state);
104
105 // Create a picture canvas.
106 SkPictureRecorder recorder;
Mike Reed63a132d2020-08-21 12:43:09 -0400107 SkCanvas* skPicCanvas = recorder.beginRecording(1, 1);
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400108 SkiaCanvas picCanvas(skPicCanvas);
Matt Sarett44dc2702017-04-13 09:33:18 -0400109 state = picCanvas.captureCanvasState();
110
111 // Verify that we cannot get the CanvasState.
112 ASSERT_EQ(state, nullptr);
113}