blob: f3a764874e2ba7050e008c11bb9da4141e985bf4 [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
Derek Sollenberger792fb322017-03-03 14:02:09 -050019#include <SkBlurDrawLooper.h>
Matt Sarett44dc2702017-04-13 09:33:18 -040020#include <SkCanvasStateUtils.h>
Derek Sollenberger35934cc2016-03-23 14:59:10 -040021#include <SkPicture.h>
22#include <SkPictureRecorder.h>
John Reck1bcacfd2017-11-03 10:12:19 -070023#include <gtest/gtest.h>
Derek Sollenberger35934cc2016-03-23 14:59:10 -040024
25using namespace android;
26using namespace android::uirenderer;
27
Derek Sollenberger792fb322017-03-03 14:02:09 -050028TEST(SkiaCanvas, drawShadowLayer) {
29 auto surface = SkSurface::MakeRasterN32Premul(10, 10);
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040030 SkiaCanvas canvas(surface->getCanvas());
Derek Sollenberger792fb322017-03-03 14:02:09 -050031
32 // clear to white
33 canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrc);
34
35 SkPaint paint;
36 // it is transparent to ensure that we still draw the rect since it has a looper
37 paint.setColor(SK_ColorTRANSPARENT);
38 // this is how view's shadow layers are implemented
39 paint.setLooper(SkBlurDrawLooper::Make(0xF0000000, 6.0f, 0, 10));
40 canvas.drawRect(3, 3, 7, 7, paint);
41
42 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorWHITE);
43 ASSERT_NE(TestUtils::getColor(surface, 5, 5), SK_ColorWHITE);
44}
Matt Sarettea70d222017-03-29 16:25:10 -040045
46TEST(SkiaCanvas, colorSpaceXform) {
47 sk_sp<SkColorSpace> adobe = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
48 SkColorSpace::kAdobeRGB_Gamut);
49
50 SkImageInfo adobeInfo = SkImageInfo::Make(1, 1, kN32_SkColorType, kOpaque_SkAlphaType, adobe);
51 sk_sp<Bitmap> adobeBitmap = Bitmap::allocateHeapBitmap(adobeInfo);
52 SkBitmap adobeSkBitmap;
53 adobeBitmap->getSkBitmap(&adobeSkBitmap);
John Reck1bcacfd2017-11-03 10:12:19 -070054 *adobeSkBitmap.getAddr32(0, 0) = 0xFF0000F0; // Opaque, almost fully-red
Matt Sarettea70d222017-03-29 16:25:10 -040055
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040056 SkImageInfo info = adobeInfo.makeColorSpace(SkColorSpace::MakeSRGB());
Matt Sarettea70d222017-03-29 16:25:10 -040057 sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(info);
58 SkBitmap skBitmap;
59 bitmap->getSkBitmap(&skBitmap);
60
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040061 // Create a software sRGB canvas.
Matt Sarettea70d222017-03-29 16:25:10 -040062 SkiaCanvas canvas(skBitmap);
63 canvas.drawBitmap(*adobeBitmap, 0, 0, nullptr);
64 // The result should be fully red, since we convert to sRGB at draw time.
Matt Sarettea70d222017-03-29 16:25:10 -040065 ASSERT_EQ(0xFF0000FF, *skBitmap.getAddr32(0, 0));
66
Matt Sarettca9b7032017-04-13 12:18:47 -040067 // Create a software canvas with an Adobe color space.
68 SkiaCanvas adobeSkCanvas(adobeSkBitmap);
69 adobeSkCanvas.drawBitmap(*bitmap, 0, 0, nullptr);
70 // The result should be less than fully red, since we convert to Adobe RGB at draw time.
71 ASSERT_EQ(0xFF0000DC, *adobeSkBitmap.getAddr32(0, 0));
72
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040073 // Test picture recording.
Matt Sarettea70d222017-03-29 16:25:10 -040074 SkPictureRecorder recorder;
75 SkCanvas* skPicCanvas = recorder.beginRecording(1, 1, NULL, 0);
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040076 SkiaCanvas picCanvas(skPicCanvas);
Matt Sarettea70d222017-03-29 16:25:10 -040077 picCanvas.drawBitmap(*adobeBitmap, 0, 0, nullptr);
78 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
79
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040080 // Playback to a software sRGB canvas. The result should be fully red.
Matt Sarettea70d222017-03-29 16:25:10 -040081 canvas.asSkCanvas()->drawPicture(picture);
82 ASSERT_EQ(0xFF0000FF, *skBitmap.getAddr32(0, 0));
83}
Matt Sarett44dc2702017-04-13 09:33:18 -040084
85TEST(SkiaCanvas, captureCanvasState) {
86 // Create a software canvas.
87 SkImageInfo info = SkImageInfo::Make(1, 1, kN32_SkColorType, kOpaque_SkAlphaType);
88 sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(info);
89 SkBitmap skBitmap;
90 bitmap->getSkBitmap(&skBitmap);
91 skBitmap.eraseColor(0);
92 SkiaCanvas canvas(skBitmap);
93
94 // Translate, then capture and verify the CanvasState.
95 canvas.translate(1.0f, 1.0f);
96 SkCanvasState* state = canvas.captureCanvasState();
97 ASSERT_NE(state, nullptr);
98 std::unique_ptr<SkCanvas> newCanvas = SkCanvasStateUtils::MakeFromCanvasState(state);
99 ASSERT_NE(newCanvas.get(), nullptr);
100 newCanvas->translate(-1.0f, -1.0f);
101 ASSERT_TRUE(newCanvas->getTotalMatrix().isIdentity());
102 SkCanvasStateUtils::ReleaseCanvasState(state);
103
104 // Create a picture canvas.
105 SkPictureRecorder recorder;
106 SkCanvas* skPicCanvas = recorder.beginRecording(1, 1, NULL, 0);
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400107 SkiaCanvas picCanvas(skPicCanvas);
Matt Sarett44dc2702017-04-13 09:33:18 -0400108 state = picCanvas.captureCanvasState();
109
110 // Verify that we cannot get the CanvasState.
111 ASSERT_EQ(state, nullptr);
112}