1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <gtest/gtest.h>
16
17 #include "GfxStreamBackend.h"
18 #include "OSWindow.h"
19 #include "base/System.h"
20
21 class GfxStreamBackendTest : public ::testing::Test {
22 private:
sWriteFence(void * cookie,uint32_t fence)23 static void sWriteFence(void* cookie, uint32_t fence) {
24 uint32_t current = *(uint32_t*)cookie;
25 if (current < fence)
26 *(uint32_t*)(cookie) = fence;
27 }
28
29 protected:
30 uint32_t cookie;
31 static const bool useWindow;
32 struct virgl_renderer_callbacks callbacks;
33 static constexpr uint32_t width = 256;
34 static constexpr uint32_t height = 256;
35 static std::unique_ptr<OSWindow> window;
36
GfxStreamBackendTest()37 GfxStreamBackendTest()
38 : cookie(0),
39 callbacks({
40 0,
41 sWriteFence,
42 0,
43 0,
44 0,
45 }) {}
46
SetUpTestSuite()47 static void SetUpTestSuite() {
48 if (useWindow) {
49 window.reset(CreateOSWindow());
50 }
51 }
52
TearDownTestSuite()53 static void TearDownTestSuite() { window.reset(nullptr); }
54
SetUp()55 void SetUp() override {
56 android::base::setEnvironmentVariable("ANDROID_GFXSTREAM_EGL", "1");
57 if (useWindow) {
58 window->initialize("GfxStreamBackendTestWindow", width, height);
59 window->setVisible(true);
60 window->messageLoop();
61 }
62 }
63
TearDown()64 void TearDown() override {
65 if (useWindow) {
66 window->destroy();
67 }
68 gfxstream_backend_teardown();
69 }
70 };
71
72 std::unique_ptr<OSWindow> GfxStreamBackendTest::window = nullptr;
73
74 const bool GfxStreamBackendTest::useWindow =
75 android::base::getEnvironmentVariable("ANDROID_EMU_TEST_WITH_WINDOW") == "1";
76
TEST_F(GfxStreamBackendTest,Init)77 TEST_F(GfxStreamBackendTest, Init) {
78 gfxstream_backend_init(width, height, 0, &cookie,
79 GFXSTREAM_RENDERER_FLAGS_USE_SURFACELESS_BIT |
80 GFXSTREAM_RENDERER_FLAGS_NO_VK_BIT,
81 &callbacks);
82 }
83
TEST_F(GfxStreamBackendTest,InitOpenGLWindow)84 TEST_F(GfxStreamBackendTest, InitOpenGLWindow) {
85 if (!useWindow) {
86 return;
87 }
88 gfxstream_backend_init(width, height, 0, &cookie,
89 GFXSTREAM_RENDERER_FLAGS_NO_VK_BIT, &callbacks);
90 gfxstream_backend_setup_window(window->getFramebufferNativeWindow(), 0, 0,
91 width, height, width, height);
92 }
93
TEST_F(GfxStreamBackendTest,SimpleFlush)94 TEST_F(GfxStreamBackendTest, SimpleFlush) {
95 gfxstream_backend_init(width, height, 0, &cookie,
96 GFXSTREAM_RENDERER_FLAGS_USE_SURFACELESS_BIT |
97 GFXSTREAM_RENDERER_FLAGS_NO_VK_BIT,
98 &callbacks);
99
100 const uint32_t res_id = 8;
101 struct virgl_renderer_resource_create_args create_resource_args = {
102 .handle = res_id,
103 .target = 2, // PIPE_TEXTURE_2D
104 .format = VIRGL_FORMAT_R8G8B8A8_UNORM,
105 .bind = VIRGL_BIND_SAMPLER_VIEW | VIRGL_BIND_SCANOUT |
106 VIRGL_BIND_SHARED,
107 .width = width,
108 .height = height,
109 .depth = 1,
110 .array_size = 1,
111 .last_level = 0,
112 .nr_samples = 0,
113 .flags = 0,
114 };
115 EXPECT_EQ(
116 pipe_virgl_renderer_resource_create(&create_resource_args, NULL, 0),
117 0);
118 // R8G8B8A8 is used, so 4 bytes per pixel
119 auto fb = std::make_unique<uint32_t[]>(width * height);
120 EXPECT_NE(fb, nullptr);
121 stream_renderer_flush_resource_and_readback(res_id, 0, 0, width, height,
122 fb.get(), width * height);
123 }
124
125 // Tests compile and link only.
TEST_F(GfxStreamBackendTest,DISABLED_ApiCallLinkTest)126 TEST_F(GfxStreamBackendTest, DISABLED_ApiCallLinkTest) {
127 gfxstream_backend_init(width, height, 0, &cookie,
128 GFXSTREAM_RENDERER_FLAGS_USE_SURFACELESS_BIT |
129 GFXSTREAM_RENDERER_FLAGS_NO_VK_BIT,
130 &callbacks);
131
132 const uint32_t res_id = 8;
133 struct virgl_renderer_resource_create_args create_resource_args = {
134 .handle = res_id,
135 .target = 2, // PIPE_TEXTURE_2D
136 .format = VIRGL_FORMAT_R8G8B8A8_UNORM,
137 .bind = VIRGL_BIND_SAMPLER_VIEW | VIRGL_BIND_SCANOUT |
138 VIRGL_BIND_SHARED,
139 .width = width,
140 .height = height,
141 .depth = 1,
142 .array_size = 1,
143 .last_level = 0,
144 .nr_samples = 0,
145 .flags = 0,
146 };
147 EXPECT_EQ(
148 pipe_virgl_renderer_resource_create(&create_resource_args, NULL, 0),
149 0);
150 // R8G8B8A8 is used, so 4 bytes per pixel
151 auto fb = std::make_unique<uint32_t[]>(width * height);
152 EXPECT_NE(fb, nullptr);
153 stream_renderer_flush_resource_and_readback(res_id, 0, 0, width, height,
154 fb.get(), width * height);
155
156 virtio_goldfish_pipe_reset(0, 0);
157 pipe_virgl_renderer_init(0, 0, 0);
158 pipe_virgl_renderer_poll();
159 pipe_virgl_renderer_get_cursor_data(0, 0, 0);
160 pipe_virgl_renderer_resource_unref(0);
161 pipe_virgl_renderer_context_create(0, 0, 0);
162 pipe_virgl_renderer_context_destroy(0);
163 pipe_virgl_renderer_submit_cmd(0, 0, 0);
164 pipe_virgl_renderer_transfer_read_iov(0, 0, 0, 0, 0, 0, 0, 0, 0);
165 pipe_virgl_renderer_transfer_write_iov(0, 0, 0, 0, 0, 0, 0, 0, 0);
166
167 pipe_virgl_renderer_get_cap_set(0, 0, 0);
168 pipe_virgl_renderer_fill_caps(0, 0, 0);
169
170 pipe_virgl_renderer_resource_attach_iov(0, 0, 0);
171 pipe_virgl_renderer_resource_detach_iov(0, 0, 0);
172 pipe_virgl_renderer_create_fence(0, 0);
173 pipe_virgl_renderer_force_ctx_0();
174 pipe_virgl_renderer_ctx_attach_resource(0, 0);
175 pipe_virgl_renderer_ctx_detach_resource(0, 0);
176 pipe_virgl_renderer_resource_get_info(0, 0);
177 stream_renderer_resource_create_v2(0, 0);
178 stream_renderer_resource_get_hva(0);
179 stream_renderer_resource_get_hva_size(0);
180 stream_renderer_resource_set_hv_slot(0, 0);
181 stream_renderer_resource_get_hv_slot(0);
182 stream_renderer_resource_map(0, 0, 0);
183 stream_renderer_resource_unmap(0);
184 }
185