• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
20 #include <RecordingCanvas.h>
21 #include <SkBlurDrawLooper.h>
22 #include <SkCanvasStateUtils.h>
23 #include <SkPicture.h>
24 #include <SkPictureRecorder.h>
25 
26 using namespace android;
27 using namespace android::uirenderer;
28 
29 /**
30  * Verify that we get the same culling bounds for text for (1) drawing glyphs
31  * directly to a Canvas or (2) going through a SkPicture as an intermediate step.
32  */
OPENGL_PIPELINE_TEST(SkiaCanvasProxy,drawGlyphsViaPicture)33 OPENGL_PIPELINE_TEST(SkiaCanvasProxy, drawGlyphsViaPicture) {
34     auto dl = TestUtils::createDisplayList<RecordingCanvas>(200, 200, [](RecordingCanvas& canvas) {
35         // setup test variables
36         SkPaint paint;
37         paint.setAntiAlias(true);
38         paint.setTextSize(20);
39         paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
40         static const char* text = "testing text bounds";
41 
42         // draw text directly into Recording canvas
43         TestUtils::drawUtf8ToCanvas(&canvas, text, paint, 25, 25);
44 
45         // record the same text draw into a SkPicture and replay it into a Recording canvas
46         SkPictureRecorder recorder;
47         SkCanvas* skCanvas = recorder.beginRecording(200, 200, NULL, 0);
48         std::unique_ptr<Canvas> pictCanvas(Canvas::create_canvas(skCanvas,
49                 Canvas::XformToSRGB::kDefer));
50         TestUtils::drawUtf8ToCanvas(pictCanvas.get(), text, paint, 25, 25);
51         sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
52 
53         canvas.asSkCanvas()->drawPicture(picture);
54     });
55 
56     // verify that the text bounds and matrices match
57     ASSERT_EQ(2U, dl->getOps().size());
58     auto directOp = dl->getOps()[0];
59     auto pictureOp = dl->getOps()[1];
60     ASSERT_EQ(RecordedOpId::TextOp, directOp->opId);
61     EXPECT_EQ(directOp->opId, pictureOp->opId);
62     EXPECT_EQ(directOp->unmappedBounds, pictureOp->unmappedBounds);
63     EXPECT_EQ(directOp->localMatrix, pictureOp->localMatrix);
64 }
65 
TEST(SkiaCanvas,drawShadowLayer)66 TEST(SkiaCanvas, drawShadowLayer) {
67     auto surface = SkSurface::MakeRasterN32Premul(10, 10);
68     SkiaCanvas canvas(surface->getCanvas(), Canvas::XformToSRGB::kDefer);
69 
70     // clear to white
71     canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrc);
72 
73     SkPaint paint;
74     // it is transparent to ensure that we still draw the rect since it has a looper
75     paint.setColor(SK_ColorTRANSPARENT);
76     // this is how view's shadow layers are implemented
77     paint.setLooper(SkBlurDrawLooper::Make(0xF0000000, 6.0f, 0, 10));
78     canvas.drawRect(3, 3, 7, 7, paint);
79 
80     ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorWHITE);
81     ASSERT_NE(TestUtils::getColor(surface, 5, 5), SK_ColorWHITE);
82 }
83 
TEST(SkiaCanvas,colorSpaceXform)84 TEST(SkiaCanvas, colorSpaceXform) {
85     sk_sp<SkColorSpace> adobe = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
86                                                       SkColorSpace::kAdobeRGB_Gamut);
87 
88     SkImageInfo adobeInfo = SkImageInfo::Make(1, 1, kN32_SkColorType, kOpaque_SkAlphaType, adobe);
89     sk_sp<Bitmap> adobeBitmap = Bitmap::allocateHeapBitmap(adobeInfo);
90     SkBitmap adobeSkBitmap;
91     adobeBitmap->getSkBitmap(&adobeSkBitmap);
92     *adobeSkBitmap.getAddr32(0, 0) = 0xFF0000F0; // Opaque, almost fully-red
93 
94     SkImageInfo info = adobeInfo.makeColorSpace(nullptr);
95     sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(info);
96     SkBitmap skBitmap;
97     bitmap->getSkBitmap(&skBitmap);
98 
99     // Create a software canvas.
100     SkiaCanvas canvas(skBitmap);
101     canvas.drawBitmap(*adobeBitmap, 0, 0, nullptr);
102     // The result should be fully red, since we convert to sRGB at draw time.
103     ASSERT_EQ(0xFF0000FF, *skBitmap.getAddr32(0, 0));
104 
105     // Create a software canvas with an Adobe color space.
106     SkiaCanvas adobeSkCanvas(adobeSkBitmap);
107     adobeSkCanvas.drawBitmap(*bitmap, 0, 0, nullptr);
108     // The result should be less than fully red, since we convert to Adobe RGB at draw time.
109     ASSERT_EQ(0xFF0000DC, *adobeSkBitmap.getAddr32(0, 0));
110 
111     // Now try in kDefer mode.  This is a little strange given that, in practice, all software
112     // canvases are kImmediate.
113     SkCanvas skCanvas(skBitmap);
114     SkiaCanvas deferCanvas(&skCanvas, Canvas::XformToSRGB::kDefer);
115     deferCanvas.drawBitmap(*adobeBitmap, 0, 0, nullptr);
116     // The result should be as before, since we deferred the conversion to sRGB.
117     ASSERT_EQ(0xFF0000DC, *skBitmap.getAddr32(0, 0));
118 
119     // Test picture recording.  We will kDefer the xform at recording time, but handle it when
120     // we playback to the software canvas.
121     SkPictureRecorder recorder;
122     SkCanvas* skPicCanvas = recorder.beginRecording(1, 1, NULL, 0);
123     SkiaCanvas picCanvas(skPicCanvas, Canvas::XformToSRGB::kDefer);
124     picCanvas.drawBitmap(*adobeBitmap, 0, 0, nullptr);
125     sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
126 
127     // Playback to a deferred canvas.  The result should be as before.
128     deferCanvas.asSkCanvas()->drawPicture(picture);
129     ASSERT_EQ(0xFF0000DC, *skBitmap.getAddr32(0, 0));
130 
131     // Playback to an immediate canvas.  The result should be fully red.
132     canvas.asSkCanvas()->drawPicture(picture);
133     ASSERT_EQ(0xFF0000FF, *skBitmap.getAddr32(0, 0));
134 }
135 
TEST(SkiaCanvas,captureCanvasState)136 TEST(SkiaCanvas, captureCanvasState) {
137     // Create a software canvas.
138     SkImageInfo info = SkImageInfo::Make(1, 1, kN32_SkColorType, kOpaque_SkAlphaType);
139     sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(info);
140     SkBitmap skBitmap;
141     bitmap->getSkBitmap(&skBitmap);
142     skBitmap.eraseColor(0);
143     SkiaCanvas canvas(skBitmap);
144 
145     // Translate, then capture and verify the CanvasState.
146     canvas.translate(1.0f, 1.0f);
147     SkCanvasState* state = canvas.captureCanvasState();
148     ASSERT_NE(state, nullptr);
149     std::unique_ptr<SkCanvas> newCanvas = SkCanvasStateUtils::MakeFromCanvasState(state);
150     ASSERT_NE(newCanvas.get(), nullptr);
151     newCanvas->translate(-1.0f, -1.0f);
152     ASSERT_TRUE(newCanvas->getTotalMatrix().isIdentity());
153     SkCanvasStateUtils::ReleaseCanvasState(state);
154 
155     // Create a picture canvas.
156     SkPictureRecorder recorder;
157     SkCanvas* skPicCanvas = recorder.beginRecording(1, 1, NULL, 0);
158     SkiaCanvas picCanvas(skPicCanvas, Canvas::XformToSRGB::kDefer);
159     state = picCanvas.captureCanvasState();
160 
161     // Verify that we cannot get the CanvasState.
162     ASSERT_EQ(state, nullptr);
163 }
164