Home | History | Annotate | Download | only in unit
      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 <gtest/gtest.h>
     18 #include <VectorDrawable.h>
     19 
     20 #include "AnimationContext.h"
     21 #include "DamageAccumulator.h"
     22 #include "IContextFactory.h"
     23 #include "pipeline/skia/SkiaDisplayList.h"
     24 #include "pipeline/skia/SkiaPipeline.h"
     25 #include "pipeline/skia/SkiaRecordingCanvas.h"
     26 #include "renderthread/CanvasContext.h"
     27 #include "tests/common/TestUtils.h"
     28 #include "SkiaCanvas.h"
     29 #include <SkSurface_Base.h>
     30 #include <SkLiteRecorder.h>
     31 #include <SkClipStack.h>
     32 #include "FatalTestCanvas.h"
     33 #include <string.h>
     34 
     35 using namespace android;
     36 using namespace android::uirenderer;
     37 using namespace android::uirenderer::renderthread;
     38 using namespace android::uirenderer::skiapipeline;
     39 
     40 namespace {
     41 
     42 static void testProperty(std::function<void(RenderProperties&)> propSetupCallback,
     43         std::function<void(const SkCanvas&)> opValidateCallback) {
     44     static const int CANVAS_WIDTH = 100;
     45     static const int CANVAS_HEIGHT = 100;
     46     class PropertyTestCanvas : public TestCanvasBase {
     47     public:
     48         PropertyTestCanvas(std::function<void(const SkCanvas&)> callback)
     49                 : TestCanvasBase(CANVAS_WIDTH, CANVAS_HEIGHT), mCallback(callback) {}
     50         void onDrawRect(const SkRect& rect, const SkPaint& paint) override {
     51             EXPECT_EQ(mDrawCounter++, 0);
     52             mCallback(*this);
     53         }
     54         void onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle style) {
     55             SkCanvas::onClipRRect(rrect, op, style);
     56         }
     57         std::function<void(const SkCanvas&)> mCallback;
     58     };
     59 
     60     auto node = TestUtils::createSkiaNode(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT,
     61             [propSetupCallback](RenderProperties& props, SkiaRecordingCanvas& canvas) {
     62         propSetupCallback(props);
     63         SkPaint paint;
     64         paint.setColor(SK_ColorWHITE);
     65         canvas.drawRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, paint);
     66     });
     67 
     68     PropertyTestCanvas canvas(opValidateCallback);
     69     RenderNodeDrawable drawable(node.get(), &canvas, true);
     70     canvas.drawDrawable(&drawable);
     71     EXPECT_EQ(1, canvas.mDrawCounter);
     72 }
     73 
     74 }
     75 
     76 TEST(RenderNodeDrawable, renderPropClipping) {
     77     testProperty([](RenderProperties& properties) {
     78         properties.setClipToBounds(true);
     79         properties.setClipBounds(android::uirenderer::Rect(10, 20, 300, 400));
     80     }, [](const SkCanvas& canvas) {
     81         EXPECT_EQ(SkRect::MakeLTRB(10, 20, 100, 100), TestUtils::getClipBounds(&canvas))
     82                 << "Clip rect should be intersection of node bounds and clip bounds";
     83     });
     84 }
     85 
     86 TEST(RenderNodeDrawable, renderPropRevealClip) {
     87     testProperty([](RenderProperties& properties) {
     88         properties.mutableRevealClip().set(true, 50, 50, 25);
     89     }, [](const SkCanvas& canvas) {
     90         EXPECT_EQ(SkRect::MakeLTRB(25, 25, 75, 75), TestUtils::getClipBounds(&canvas));
     91     });
     92 }
     93 
     94 TEST(RenderNodeDrawable, renderPropOutlineClip) {
     95     testProperty([](RenderProperties& properties) {
     96         properties.mutableOutline().setShouldClip(true);
     97         properties.mutableOutline().setRoundRect(10, 20, 30, 40, 5.0f, 0.5f);
     98     }, [](const SkCanvas& canvas) {
     99         EXPECT_EQ(SkRect::MakeLTRB(10, 20, 30, 40), TestUtils::getClipBounds(&canvas));
    100     });
    101 }
    102 
    103 TEST(RenderNodeDrawable, renderPropTransform) {
    104     testProperty([](RenderProperties& properties) {
    105         properties.setLeftTopRightBottom(10, 10, 110, 110);
    106 
    107         SkMatrix staticMatrix = SkMatrix::MakeScale(1.2f, 1.2f);
    108         properties.setStaticMatrix(&staticMatrix);
    109 
    110         // ignored, since static overrides animation
    111         SkMatrix animationMatrix = SkMatrix::MakeTrans(15, 15);
    112         properties.setAnimationMatrix(&animationMatrix);
    113 
    114         properties.setTranslationX(10);
    115         properties.setTranslationY(20);
    116         properties.setScaleX(0.5f);
    117         properties.setScaleY(0.7f);
    118     }, [](const SkCanvas& canvas) {
    119         Matrix4 matrix;
    120         matrix.loadTranslate(10, 10, 0); // left, top
    121         matrix.scale(1.2f, 1.2f, 1); // static matrix
    122         // ignore animation matrix, since static overrides it
    123 
    124         // translation xy
    125         matrix.translate(10, 20);
    126 
    127         // scale xy (from default pivot - center)
    128         matrix.translate(50, 50);
    129         matrix.scale(0.5f, 0.7f, 1);
    130         matrix.translate(-50, -50);
    131         Matrix4 actual(canvas.getTotalMatrix());
    132         EXPECT_MATRIX_APPROX_EQ(matrix, actual)
    133                 << "Op draw matrix must match expected combination of transformation properties";
    134     });
    135 }
    136