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 
29 #define ASSUME_GLES()                                                      \
30     if (WebViewFunctor_queryPlatformRenderMode() != RenderMode::OpenGL_ES) \
31     GTEST_SKIP() << "Not in GLES, skipping test"
32 
TEST(WebViewFunctor,createDestroyGLES)33 TEST(WebViewFunctor, createDestroyGLES) {
34     ASSUME_GLES();
35     int functor = WebViewFunctor_create(
36             nullptr, TestUtils::createMockFunctorCallbacks(RenderMode::OpenGL_ES),
37             RenderMode::OpenGL_ES);
38     ASSERT_NE(-1, functor);
39     WebViewFunctor_release(functor);
40     TestUtils::runOnRenderThreadUnmanaged([](renderthread::RenderThread&) {
41         // Empty, don't care
42     });
43     auto& counts = TestUtils::countsForFunctor(functor);
44     // We never initialized, so contextDestroyed == 0
45     EXPECT_EQ(0, counts.contextDestroyed);
46     EXPECT_EQ(1, counts.destroyed);
47 }
48 
TEST(WebViewFunctor,createSyncHandleGLES)49 TEST(WebViewFunctor, createSyncHandleGLES) {
50     ASSUME_GLES();
51     int functor = WebViewFunctor_create(
52             nullptr, TestUtils::createMockFunctorCallbacks(RenderMode::OpenGL_ES),
53             RenderMode::OpenGL_ES);
54     ASSERT_NE(-1, functor);
55     auto handle = WebViewFunctorManager::instance().handleFor(functor);
56     ASSERT_TRUE(handle);
57     WebViewFunctor_release(functor);
58     EXPECT_FALSE(WebViewFunctorManager::instance().handleFor(functor));
59     TestUtils::runOnRenderThreadUnmanaged([](renderthread::RenderThread&) {
60         // fence
61     });
62     auto& counts = TestUtils::countsForFunctor(functor);
63     EXPECT_EQ(0, counts.sync);
64     EXPECT_EQ(0, counts.contextDestroyed);
65     EXPECT_EQ(0, counts.destroyed);
66 
67     TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
68         WebViewSyncData syncData;
69         handle->sync(syncData);
70     });
71 
72     EXPECT_EQ(1, counts.sync);
73 
74     TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
75         WebViewSyncData syncData;
76         handle->sync(syncData);
77     });
78 
79     EXPECT_EQ(2, counts.sync);
80 
81     handle.clear();
82 
83     TestUtils::runOnRenderThreadUnmanaged([](renderthread::RenderThread&) {
84         // fence
85     });
86 
87     EXPECT_EQ(2, counts.sync);
88     EXPECT_EQ(0, counts.contextDestroyed);
89     EXPECT_EQ(1, counts.destroyed);
90 }
91 
TEST(WebViewFunctor,createSyncDrawGLES)92 TEST(WebViewFunctor, createSyncDrawGLES) {
93     ASSUME_GLES();
94     int functor = WebViewFunctor_create(
95             nullptr, TestUtils::createMockFunctorCallbacks(RenderMode::OpenGL_ES),
96             RenderMode::OpenGL_ES);
97     ASSERT_NE(-1, functor);
98     auto handle = WebViewFunctorManager::instance().handleFor(functor);
99     ASSERT_TRUE(handle);
100     WebViewFunctor_release(functor);
101     auto& counts = TestUtils::countsForFunctor(functor);
102     for (int i = 0; i < 5; i++) {
103         TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
104             WebViewSyncData syncData;
105             handle->sync(syncData);
106             DrawGlInfo drawInfo;
107             handle->drawGl(drawInfo);
108             handle->drawGl(drawInfo);
109         });
110     }
111     handle.clear();
112     TestUtils::runOnRenderThreadUnmanaged([](renderthread::RenderThread&) {
113         // fence
114     });
115     EXPECT_EQ(5, counts.sync);
116     EXPECT_EQ(10, counts.glesDraw);
117     EXPECT_EQ(1, counts.contextDestroyed);
118     EXPECT_EQ(1, counts.destroyed);
119 }
120 
TEST(WebViewFunctor,contextDestroyedGLES)121 TEST(WebViewFunctor, contextDestroyedGLES) {
122     ASSUME_GLES();
123     int functor = WebViewFunctor_create(
124             nullptr, TestUtils::createMockFunctorCallbacks(RenderMode::OpenGL_ES),
125             RenderMode::OpenGL_ES);
126     ASSERT_NE(-1, functor);
127     auto handle = WebViewFunctorManager::instance().handleFor(functor);
128     ASSERT_TRUE(handle);
129     WebViewFunctor_release(functor);
130     auto& counts = TestUtils::countsForFunctor(functor);
131     TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
132         WebViewSyncData syncData;
133         handle->sync(syncData);
134         DrawGlInfo drawInfo;
135         handle->drawGl(drawInfo);
136     });
137     EXPECT_EQ(1, counts.sync);
138     EXPECT_EQ(1, counts.glesDraw);
139     EXPECT_EQ(0, counts.contextDestroyed);
140     EXPECT_EQ(0, counts.destroyed);
141     TestUtils::runOnRenderThreadUnmanaged([](auto& rt) {
142         rt.destroyRenderingContext();
143     });
144     EXPECT_EQ(1, counts.sync);
145     EXPECT_EQ(1, counts.glesDraw);
146     EXPECT_EQ(1, counts.contextDestroyed);
147     EXPECT_EQ(0, counts.destroyed);
148     TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
149         WebViewSyncData syncData;
150         handle->sync(syncData);
151         DrawGlInfo drawInfo;
152         handle->drawGl(drawInfo);
153     });
154     EXPECT_EQ(2, counts.sync);
155     EXPECT_EQ(2, counts.glesDraw);
156     EXPECT_EQ(1, counts.contextDestroyed);
157     EXPECT_EQ(0, counts.destroyed);
158     handle.clear();
159     TestUtils::runOnRenderThreadUnmanaged([](renderthread::RenderThread&) {
160         // fence
161     });
162     EXPECT_EQ(2, counts.sync);
163     EXPECT_EQ(2, counts.glesDraw);
164     EXPECT_EQ(2, counts.contextDestroyed);
165     EXPECT_EQ(1, counts.destroyed);
166 }
167