144b49f070aafe8ad44efae87341121cce49ff11cJohn Reck/*
244b49f070aafe8ad44efae87341121cce49ff11cJohn Reck * Copyright (C) 2016 The Android Open Source Project
344b49f070aafe8ad44efae87341121cce49ff11cJohn Reck *
444b49f070aafe8ad44efae87341121cce49ff11cJohn Reck * Licensed under the Apache License, Version 2.0 (the "License");
544b49f070aafe8ad44efae87341121cce49ff11cJohn Reck * you may not use this file except in compliance with the License.
644b49f070aafe8ad44efae87341121cce49ff11cJohn Reck * You may obtain a copy of the License at
744b49f070aafe8ad44efae87341121cce49ff11cJohn Reck *
844b49f070aafe8ad44efae87341121cce49ff11cJohn Reck *      http://www.apache.org/licenses/LICENSE-2.0
944b49f070aafe8ad44efae87341121cce49ff11cJohn Reck *
1044b49f070aafe8ad44efae87341121cce49ff11cJohn Reck * Unless required by applicable law or agreed to in writing, software
1144b49f070aafe8ad44efae87341121cce49ff11cJohn Reck * distributed under the License is distributed on an "AS IS" BASIS,
1244b49f070aafe8ad44efae87341121cce49ff11cJohn Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1344b49f070aafe8ad44efae87341121cce49ff11cJohn Reck * See the License for the specific language governing permissions and
1444b49f070aafe8ad44efae87341121cce49ff11cJohn Reck * limitations under the License.
1544b49f070aafe8ad44efae87341121cce49ff11cJohn Reck */
1644b49f070aafe8ad44efae87341121cce49ff11cJohn Reck
1744b49f070aafe8ad44efae87341121cce49ff11cJohn Reck#include <gtest/gtest.h>
1844b49f070aafe8ad44efae87341121cce49ff11cJohn Reck
1944b49f070aafe8ad44efae87341121cce49ff11cJohn Reck#include "RenderNode.h"
2044b49f070aafe8ad44efae87341121cce49ff11cJohn Reck#include "TreeInfo.h"
2144b49f070aafe8ad44efae87341121cce49ff11cJohn Reck#include "tests/common/TestUtils.h"
2244b49f070aafe8ad44efae87341121cce49ff11cJohn Reck#include "utils/Color.h"
2344b49f070aafe8ad44efae87341121cce49ff11cJohn Reck
2444b49f070aafe8ad44efae87341121cce49ff11cJohn Reckusing namespace android;
2544b49f070aafe8ad44efae87341121cce49ff11cJohn Reckusing namespace android::uirenderer;
2644b49f070aafe8ad44efae87341121cce49ff11cJohn Reck
2744b49f070aafe8ad44efae87341121cce49ff11cJohn ReckTEST(RenderNode, hasParents) {
2844b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    auto child = TestUtils::createNode(0, 0, 200, 400,
2944b49f070aafe8ad44efae87341121cce49ff11cJohn Reck            [](RenderProperties& props, TestCanvas& canvas) {
3044b49f070aafe8ad44efae87341121cce49ff11cJohn Reck        canvas.drawColor(Color::Red_500, SkXfermode::kSrcOver_Mode);
3144b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    });
3244b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    auto parent = TestUtils::createNode(0, 0, 200, 400,
3344b49f070aafe8ad44efae87341121cce49ff11cJohn Reck            [&child](RenderProperties& props, TestCanvas& canvas) {
3444b49f070aafe8ad44efae87341121cce49ff11cJohn Reck        canvas.drawRenderNode(child.get());
3544b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    });
3644b49f070aafe8ad44efae87341121cce49ff11cJohn Reck
3744b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    TestUtils::syncHierarchyPropertiesAndDisplayList(parent);
3844b49f070aafe8ad44efae87341121cce49ff11cJohn Reck
3944b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    EXPECT_TRUE(child->hasParents()) << "Child node has no parent";
4044b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    EXPECT_FALSE(parent->hasParents()) << "Root node shouldn't have any parents";
4144b49f070aafe8ad44efae87341121cce49ff11cJohn Reck
4244b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    TestUtils::recordNode(*parent, [](TestCanvas& canvas) {
4344b49f070aafe8ad44efae87341121cce49ff11cJohn Reck        canvas.drawColor(Color::Amber_500, SkXfermode::kSrcOver_Mode);
4444b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    });
4544b49f070aafe8ad44efae87341121cce49ff11cJohn Reck
4644b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    EXPECT_TRUE(child->hasParents()) << "Child should still have a parent";
4744b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    EXPECT_FALSE(parent->hasParents()) << "Root node shouldn't have any parents";
4844b49f070aafe8ad44efae87341121cce49ff11cJohn Reck
4944b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    TestUtils::syncHierarchyPropertiesAndDisplayList(parent);
5044b49f070aafe8ad44efae87341121cce49ff11cJohn Reck
5144b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    EXPECT_FALSE(child->hasParents()) << "Child should be removed";
5244b49f070aafe8ad44efae87341121cce49ff11cJohn Reck    EXPECT_FALSE(parent->hasParents()) << "Root node shouldn't have any parents";
5344b49f070aafe8ad44efae87341121cce49ff11cJohn Reck}
54cd1c3eba69d044b551cededad75474038f919890John Reck
55cd1c3eba69d044b551cededad75474038f919890John ReckTEST(RenderNode, releasedCallback) {
56cd1c3eba69d044b551cededad75474038f919890John Reck    class DecRefOnReleased : public GlFunctorLifecycleListener {
57cd1c3eba69d044b551cededad75474038f919890John Reck    public:
58cd1c3eba69d044b551cededad75474038f919890John Reck        DecRefOnReleased(int* refcnt) : mRefCnt(refcnt) {}
59cd1c3eba69d044b551cededad75474038f919890John Reck        void onGlFunctorReleased(Functor* functor) override {
60cd1c3eba69d044b551cededad75474038f919890John Reck            *mRefCnt -= 1;
61cd1c3eba69d044b551cededad75474038f919890John Reck        }
62cd1c3eba69d044b551cededad75474038f919890John Reck    private:
63cd1c3eba69d044b551cededad75474038f919890John Reck        int* mRefCnt;
64cd1c3eba69d044b551cededad75474038f919890John Reck    };
65cd1c3eba69d044b551cededad75474038f919890John Reck
66cd1c3eba69d044b551cededad75474038f919890John Reck    int refcnt = 0;
67cd1c3eba69d044b551cededad75474038f919890John Reck    sp<DecRefOnReleased> listener(new DecRefOnReleased(&refcnt));
68cd1c3eba69d044b551cededad75474038f919890John Reck    Functor noopFunctor;
69cd1c3eba69d044b551cededad75474038f919890John Reck
70cd1c3eba69d044b551cededad75474038f919890John Reck    auto node = TestUtils::createNode(0, 0, 200, 400,
71cd1c3eba69d044b551cededad75474038f919890John Reck            [&](RenderProperties& props, TestCanvas& canvas) {
72cd1c3eba69d044b551cededad75474038f919890John Reck        refcnt++;
73cd1c3eba69d044b551cededad75474038f919890John Reck        canvas.callDrawGLFunction(&noopFunctor, listener.get());
74cd1c3eba69d044b551cededad75474038f919890John Reck    });
75cd1c3eba69d044b551cededad75474038f919890John Reck    TestUtils::syncHierarchyPropertiesAndDisplayList(node);
76cd1c3eba69d044b551cededad75474038f919890John Reck    EXPECT_EQ(1, refcnt);
77cd1c3eba69d044b551cededad75474038f919890John Reck
78cd1c3eba69d044b551cededad75474038f919890John Reck    TestUtils::recordNode(*node, [&](TestCanvas& canvas) {
79cd1c3eba69d044b551cededad75474038f919890John Reck        refcnt++;
80cd1c3eba69d044b551cededad75474038f919890John Reck        canvas.callDrawGLFunction(&noopFunctor, listener.get());
81cd1c3eba69d044b551cededad75474038f919890John Reck    });
82cd1c3eba69d044b551cededad75474038f919890John Reck    EXPECT_EQ(2, refcnt);
83cd1c3eba69d044b551cededad75474038f919890John Reck
84cd1c3eba69d044b551cededad75474038f919890John Reck    TestUtils::syncHierarchyPropertiesAndDisplayList(node);
85cd1c3eba69d044b551cededad75474038f919890John Reck    EXPECT_EQ(1, refcnt);
86cd1c3eba69d044b551cededad75474038f919890John Reck
87cd1c3eba69d044b551cededad75474038f919890John Reck    TestUtils::recordNode(*node, [](TestCanvas& canvas) {});
88cd1c3eba69d044b551cededad75474038f919890John Reck    EXPECT_EQ(1, refcnt);
89cd1c3eba69d044b551cededad75474038f919890John Reck    TestUtils::syncHierarchyPropertiesAndDisplayList(node);
90cd1c3eba69d044b551cededad75474038f919890John Reck    EXPECT_EQ(0, refcnt);
91cd1c3eba69d044b551cededad75474038f919890John Reck}
92