SkiaCanvasTests.cpp revision 792fb3252f6460acfd82c4e5d7536ca6b787a0e0
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 <SkPicture.h>
23#include <SkPictureRecorder.h>
24
25using namespace android;
26using namespace android::uirenderer;
27
28/**
29 * Verify that we get the same culling bounds for text for (1) drawing glyphs
30 * directly to a Canvas or (2) going through a SkPicture as an intermediate step.
31 */
32OPENGL_PIPELINE_TEST(SkiaCanvasProxy, drawGlyphsViaPicture) {
33    auto dl = TestUtils::createDisplayList<RecordingCanvas>(200, 200, [](RecordingCanvas& canvas) {
34        // setup test variables
35        SkPaint paint;
36        paint.setAntiAlias(true);
37        paint.setTextSize(20);
38        paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
39        static const char* text = "testing text bounds";
40
41        // draw text directly into Recording canvas
42        TestUtils::drawUtf8ToCanvas(&canvas, text, paint, 25, 25);
43
44        // record the same text draw into a SkPicture and replay it into a Recording canvas
45        SkPictureRecorder recorder;
46        SkCanvas* skCanvas = recorder.beginRecording(200, 200, NULL, 0);
47        std::unique_ptr<Canvas> pictCanvas(Canvas::create_canvas(skCanvas));
48        TestUtils::drawUtf8ToCanvas(pictCanvas.get(), text, paint, 25, 25);
49        sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
50
51        canvas.asSkCanvas()->drawPicture(picture);
52    });
53
54    // verify that the text bounds and matrices match
55    ASSERT_EQ(2U, dl->getOps().size());
56    auto directOp = dl->getOps()[0];
57    auto pictureOp = dl->getOps()[1];
58    ASSERT_EQ(RecordedOpId::TextOp, directOp->opId);
59    EXPECT_EQ(directOp->opId, pictureOp->opId);
60    EXPECT_EQ(directOp->unmappedBounds, pictureOp->unmappedBounds);
61    EXPECT_EQ(directOp->localMatrix, pictureOp->localMatrix);
62}
63
64TEST(SkiaCanvas, drawShadowLayer) {
65    auto surface = SkSurface::MakeRasterN32Premul(10, 10);
66    SkiaCanvas canvas(surface->getCanvas());
67
68    // clear to white
69    canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrc);
70
71    SkPaint paint;
72    // it is transparent to ensure that we still draw the rect since it has a looper
73    paint.setColor(SK_ColorTRANSPARENT);
74    // this is how view's shadow layers are implemented
75    paint.setLooper(SkBlurDrawLooper::Make(0xF0000000, 6.0f, 0, 10));
76    canvas.drawRect(3, 3, 7, 7, paint);
77
78    ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorWHITE);
79    ASSERT_NE(TestUtils::getColor(surface, 5, 5), SK_ColorWHITE);
80}
81