1 /*
2  * Copyright (C) 2018 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 
19 #include "WebViewFunctorManager.h"
20 #include "private/hwui/WebViewFunctor.h"
21 #include "renderthread/RenderProxy.h"
22 #include "tests/common/TestUtils.h"
23 
24 #include <unordered_map>
25 
26 using namespace android;
27 using namespace android::uirenderer;
28 
TEST(WebViewFunctor,createDestroyGLES)29 TEST(WebViewFunctor, createDestroyGLES) {
30     int functor = WebViewFunctor_create(
31             nullptr, TestUtils::createMockFunctor(RenderMode::OpenGL_ES), RenderMode::OpenGL_ES);
32     ASSERT_NE(-1, functor);
33     WebViewFunctor_release(functor);
34     TestUtils::runOnRenderThreadUnmanaged([](renderthread::RenderThread&) {
35         // Empty, don't care
36     });
37     auto& counts = TestUtils::countsForFunctor(functor);
38     // We never initialized, so contextDestroyed == 0
39     EXPECT_EQ(0, counts.contextDestroyed);
40     EXPECT_EQ(1, counts.destroyed);
41 }
42 
TEST(WebViewFunctor,createSyncHandleGLES)43 TEST(WebViewFunctor, createSyncHandleGLES) {
44     int functor = WebViewFunctor_create(
45             nullptr, TestUtils::createMockFunctor(RenderMode::OpenGL_ES), RenderMode::OpenGL_ES);
46     ASSERT_NE(-1, functor);
47     auto handle = WebViewFunctorManager::instance().handleFor(functor);
48     ASSERT_TRUE(handle);
49     WebViewFunctor_release(functor);
50     EXPECT_FALSE(WebViewFunctorManager::instance().handleFor(functor));
51     TestUtils::runOnRenderThreadUnmanaged([](renderthread::RenderThread&) {
52         // fence
53     });
54     auto& counts = TestUtils::countsForFunctor(functor);
55     EXPECT_EQ(0, counts.sync);
56     EXPECT_EQ(0, counts.contextDestroyed);
57     EXPECT_EQ(0, counts.destroyed);
58 
59     TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
60         WebViewSyncData syncData;
61         handle->sync(syncData);
62     });
63 
64     EXPECT_EQ(1, counts.sync);
65 
66     TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
67         WebViewSyncData syncData;
68         handle->sync(syncData);
69     });
70 
71     EXPECT_EQ(2, counts.sync);
72 
73     handle.clear();
74 
75     TestUtils::runOnRenderThreadUnmanaged([](renderthread::RenderThread&) {
76         // fence
77     });
78 
79     EXPECT_EQ(2, counts.sync);
80     EXPECT_EQ(0, counts.contextDestroyed);
81     EXPECT_EQ(1, counts.destroyed);
82 }
83 
TEST(WebViewFunctor,createSyncDrawGLES)84 TEST(WebViewFunctor, createSyncDrawGLES) {
85     int functor = WebViewFunctor_create(
86             nullptr, TestUtils::createMockFunctor(RenderMode::OpenGL_ES), RenderMode::OpenGL_ES);
87     ASSERT_NE(-1, functor);
88     auto handle = WebViewFunctorManager::instance().handleFor(functor);
89     ASSERT_TRUE(handle);
90     WebViewFunctor_release(functor);
91     auto& counts = TestUtils::countsForFunctor(functor);
92     for (int i = 0; i < 5; i++) {
93         TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
94             WebViewSyncData syncData;
95             handle->sync(syncData);
96             DrawGlInfo drawInfo;
97             handle->drawGl(drawInfo);
98             handle->drawGl(drawInfo);
99         });
100     }
101     handle.clear();
102     TestUtils::runOnRenderThreadUnmanaged([](renderthread::RenderThread&) {
103         // fence
104     });
105     EXPECT_EQ(5, counts.sync);
106     EXPECT_EQ(10, counts.glesDraw);
107     EXPECT_EQ(1, counts.contextDestroyed);
108     EXPECT_EQ(1, counts.destroyed);
109 }
110 
TEST(WebViewFunctor,contextDestroyed)111 TEST(WebViewFunctor, contextDestroyed) {
112     int functor = WebViewFunctor_create(
113             nullptr, TestUtils::createMockFunctor(RenderMode::OpenGL_ES), RenderMode::OpenGL_ES);
114     ASSERT_NE(-1, functor);
115     auto handle = WebViewFunctorManager::instance().handleFor(functor);
116     ASSERT_TRUE(handle);
117     WebViewFunctor_release(functor);
118     auto& counts = TestUtils::countsForFunctor(functor);
119     TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
120         WebViewSyncData syncData;
121         handle->sync(syncData);
122         DrawGlInfo drawInfo;
123         handle->drawGl(drawInfo);
124     });
125     EXPECT_EQ(1, counts.sync);
126     EXPECT_EQ(1, counts.glesDraw);
127     EXPECT_EQ(0, counts.contextDestroyed);
128     EXPECT_EQ(0, counts.destroyed);
129     TestUtils::runOnRenderThreadUnmanaged([](auto& rt) {
130         rt.destroyRenderingContext();
131     });
132     EXPECT_EQ(1, counts.sync);
133     EXPECT_EQ(1, counts.glesDraw);
134     EXPECT_EQ(1, counts.contextDestroyed);
135     EXPECT_EQ(0, counts.destroyed);
136     TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
137         WebViewSyncData syncData;
138         handle->sync(syncData);
139         DrawGlInfo drawInfo;
140         handle->drawGl(drawInfo);
141     });
142     EXPECT_EQ(2, counts.sync);
143     EXPECT_EQ(2, counts.glesDraw);
144     EXPECT_EQ(1, counts.contextDestroyed);
145     EXPECT_EQ(0, counts.destroyed);
146     handle.clear();
147     TestUtils::runOnRenderThreadUnmanaged([](renderthread::RenderThread&) {
148         // fence
149     });
150     EXPECT_EQ(2, counts.sync);
151     EXPECT_EQ(2, counts.glesDraw);
152     EXPECT_EQ(2, counts.contextDestroyed);
153     EXPECT_EQ(1, counts.destroyed);
154 }
155