1 /*
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
19 #include <hwui/Paint.h>
20 #include <SkCanvasStateUtils.h>
21 #include <SkPicture.h>
22 #include <SkPictureRecorder.h>
23 #include <gtest/gtest.h>
24
25 using namespace android;
26 using namespace android::uirenderer;
27
TEST(SkiaCanvas,drawShadowLayer)28 TEST(SkiaCanvas, drawShadowLayer) {
29 auto surface = SkSurface::MakeRasterN32Premul(10, 10);
30 SkiaCanvas canvas(surface->getCanvas());
31
32 // clear to white
33 canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrc);
34
35 Paint 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(BlurDrawLooper::Make({0, 0, 0, 240.0f / 255}, nullptr, 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 }
45
TEST(SkiaCanvas,colorSpaceXform)46 TEST(SkiaCanvas, colorSpaceXform) {
47 sk_sp<SkColorSpace> adobe = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
48 SkNamedGamut::kAdobeRGB);
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);
54 *adobeSkBitmap.getAddr32(0, 0) = 0xFF0000F0; // Opaque, almost fully-red
55
56 SkImageInfo info = adobeInfo.makeColorSpace(SkColorSpace::MakeSRGB());
57 sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(info);
58 SkBitmap skBitmap;
59 bitmap->getSkBitmap(&skBitmap);
60
61 // Create a software sRGB canvas.
62 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.
65 ASSERT_EQ(0xFF0000FF, *skBitmap.getAddr32(0, 0));
66
67 // 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
73 // Test picture recording.
74 SkPictureRecorder recorder;
75 SkCanvas* skPicCanvas = recorder.beginRecording(1, 1);
76 SkiaCanvas picCanvas(skPicCanvas);
77 picCanvas.drawBitmap(*adobeBitmap, 0, 0, nullptr);
78 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
79
80 // Playback to a software sRGB canvas. The result should be fully red.
81 canvas.drawPicture(*picture);
82 ASSERT_EQ(0xFF0000FF, *skBitmap.getAddr32(0, 0));
83 }
84
TEST(SkiaCanvas,captureCanvasState)85 TEST(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);
107 SkiaCanvas picCanvas(skPicCanvas);
108 state = picCanvas.captureCanvasState();
109
110 // Verify that we cannot get the CanvasState.
111 ASSERT_EQ(state, nullptr);
112 }
113