xref: /aosp_15_r20/frameworks/base/libs/hwui/tests/unit/WebViewFunctorManagerTests.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
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::copyCountsForFunctor(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::copyCountsForFunctor(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     counts = TestUtils::copyCountsForFunctor(functor);
73     EXPECT_EQ(1, counts.sync);
74 
75     TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
76         WebViewSyncData syncData;
77         handle->sync(syncData);
78     });
79 
80     counts = TestUtils::copyCountsForFunctor(functor);
81     EXPECT_EQ(2, counts.sync);
82 
83     handle.clear();
84 
85     TestUtils::runOnRenderThreadUnmanaged([](renderthread::RenderThread&) {
86         // fence
87     });
88 
89     counts = TestUtils::copyCountsForFunctor(functor);
90     EXPECT_EQ(2, counts.sync);
91     EXPECT_EQ(0, counts.contextDestroyed);
92     EXPECT_EQ(1, counts.destroyed);
93 }
94 
TEST(WebViewFunctor,createSyncDrawGLES)95 TEST(WebViewFunctor, createSyncDrawGLES) {
96     ASSUME_GLES();
97     int functor = WebViewFunctor_create(
98             nullptr, TestUtils::createMockFunctorCallbacks(RenderMode::OpenGL_ES),
99             RenderMode::OpenGL_ES);
100     ASSERT_NE(-1, functor);
101     auto handle = WebViewFunctorManager::instance().handleFor(functor);
102     ASSERT_TRUE(handle);
103     WebViewFunctor_release(functor);
104     for (int i = 0; i < 5; i++) {
105         TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
106             WebViewSyncData syncData;
107             handle->sync(syncData);
108             DrawGlInfo drawInfo;
109             handle->drawGl(drawInfo);
110             handle->drawGl(drawInfo);
111         });
112     }
113     handle.clear();
114     TestUtils::runOnRenderThreadUnmanaged([](renderthread::RenderThread&) {
115         // fence
116     });
117     auto counts = TestUtils::copyCountsForFunctor(functor);
118     EXPECT_EQ(5, counts.sync);
119     EXPECT_EQ(10, counts.glesDraw);
120     EXPECT_EQ(1, counts.contextDestroyed);
121     EXPECT_EQ(1, counts.destroyed);
122 }
123 
TEST(WebViewFunctor,contextDestroyedGLES)124 TEST(WebViewFunctor, contextDestroyedGLES) {
125     ASSUME_GLES();
126     int functor = WebViewFunctor_create(
127             nullptr, TestUtils::createMockFunctorCallbacks(RenderMode::OpenGL_ES),
128             RenderMode::OpenGL_ES);
129     ASSERT_NE(-1, functor);
130     auto handle = WebViewFunctorManager::instance().handleFor(functor);
131     ASSERT_TRUE(handle);
132     WebViewFunctor_release(functor);
133     TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
134         WebViewSyncData syncData;
135         handle->sync(syncData);
136         DrawGlInfo drawInfo;
137         handle->drawGl(drawInfo);
138     });
139     auto counts = TestUtils::copyCountsForFunctor(functor);
140     EXPECT_EQ(1, counts.sync);
141     EXPECT_EQ(1, counts.glesDraw);
142     EXPECT_EQ(0, counts.contextDestroyed);
143     EXPECT_EQ(0, counts.destroyed);
144     TestUtils::runOnRenderThreadUnmanaged([](auto& rt) {
145         rt.destroyRenderingContext();
146     });
147     counts = TestUtils::copyCountsForFunctor(functor);
148     EXPECT_EQ(1, counts.sync);
149     EXPECT_EQ(1, counts.glesDraw);
150     EXPECT_EQ(1, counts.contextDestroyed);
151     EXPECT_EQ(0, counts.destroyed);
152     TestUtils::runOnRenderThreadUnmanaged([&](auto&) {
153         WebViewSyncData syncData;
154         handle->sync(syncData);
155         DrawGlInfo drawInfo;
156         handle->drawGl(drawInfo);
157     });
158     counts = TestUtils::copyCountsForFunctor(functor);
159     EXPECT_EQ(2, counts.sync);
160     EXPECT_EQ(2, counts.glesDraw);
161     EXPECT_EQ(1, counts.contextDestroyed);
162     EXPECT_EQ(0, counts.destroyed);
163     handle.clear();
164     TestUtils::runOnRenderThreadUnmanaged([](renderthread::RenderThread&) {
165         // fence
166     });
167     counts = TestUtils::copyCountsForFunctor(functor);
168     EXPECT_EQ(2, counts.sync);
169     EXPECT_EQ(2, counts.glesDraw);
170     EXPECT_EQ(2, counts.contextDestroyed);
171     EXPECT_EQ(1, counts.destroyed);
172 }
173