OSDN Git Service

test: move generic toString to i965_streamable.h
[android-x86/hardware-intel-common-vaapi.git] / test / i965_test_fixture.cpp
1 /*
2  * Copyright (C) 2016 Intel Corporation. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include "i965_test_fixture.h"
26
27 #include <fcntl.h> // for O_RDWR
28 #include <unistd.h> // for close()
29 #include <va/va_drm.h>
30
31 I965TestFixture::I965TestFixture()
32     : ::testing::Test::Test()
33     , m_handle(-1)
34     , m_vaDisplay(NULL)
35 {
36     setenv("LIBVA_DRIVERS_PATH", TEST_VA_DRIVERS_PATH, 1);
37     setenv("LIBVA_DRIVER_NAME", "i965", 1);
38 }
39
40 I965TestFixture::~I965TestFixture()
41 {
42     if (m_handle >= 0)
43         close(m_handle);
44     m_handle = -1;
45     m_vaDisplay = NULL;
46 }
47
48 const std::string I965TestFixture::getFullTestName() const
49 {
50     const ::testing::TestInfo * const info =
51         ::testing::UnitTest::GetInstance()->current_test_info();
52     return std::string(info->test_case_name())
53         + std::string(".")
54         + std::string(info->name());
55 }
56
57 void I965TestFixture::initialize()
58 {
59     ASSERT_FALSE(NULL == (VADisplay)*this);
60
61     int major, minor;
62     ASSERT_STATUS(vaInitialize(*this, &major, &minor));
63
64     EXPECT_EQ(VA_MAJOR_VERSION, major);
65     EXPECT_EQ(VA_MINOR_VERSION, minor);
66
67     VADriverContextP context(*this);
68     ASSERT_PTR(context);
69
70     const std::string vendor(context->str_vendor);
71
72     ::testing::Test::RecordProperty("driver_vendor", vendor);
73     ::testing::Test::RecordProperty("vaapi_version", VA_VERSION_S);
74 }
75
76 void I965TestFixture::terminate()
77 {
78     if (m_vaDisplay)
79         EXPECT_STATUS(vaTerminate(m_vaDisplay));
80 }
81
82 I965TestFixture::operator VADisplay()
83 {
84     if (m_vaDisplay)
85         return m_vaDisplay;
86
87     m_handle = open("/dev/dri/renderD128", O_RDWR);
88     if (m_handle < 0)
89         m_handle = open("/dev/dri/card0", O_RDWR);
90
91     m_vaDisplay = vaGetDisplayDRM(m_handle);
92     if (!m_vaDisplay && m_handle >= 0) {
93         close(m_handle);
94         m_handle = -1;
95     }
96
97     return m_vaDisplay;
98 }
99
100 Surfaces I965TestFixture::createSurfaces(int w, int h, int format, size_t count,
101     const SurfaceAttribs& attributes)
102 {
103     Surfaces surfaces(count, VA_INVALID_ID);
104     if (attributes.empty()) {
105         EXPECT_STATUS(
106             i965_CreateSurfaces(
107                 *this, w, h, format, surfaces.size(), surfaces.data()));
108     } else {
109         VADriverContextP ctx(*this);
110         EXPECT_PTR(ctx);
111         if (ctx)
112             EXPECT_STATUS(
113                 ctx->vtable->vaCreateSurfaces2(
114                     *this, format, w, h, surfaces.data(), surfaces.size(),
115                     const_cast<VASurfaceAttrib*>(attributes.data()),
116                     attributes.size()));
117     }
118
119     for (size_t i(0); i < count; ++i) {
120         EXPECT_ID(surfaces[i]);
121     }
122
123     return surfaces;
124 }
125
126 void I965TestFixture::destroySurfaces(Surfaces& surfaces)
127 {
128     EXPECT_STATUS(
129         i965_DestroySurfaces(*this, surfaces.data(), surfaces.size()));
130 }
131
132 VAConfigID I965TestFixture::createConfig(
133     VAProfile profile, VAEntrypoint entrypoint, ConfigAttribs& attribs)
134 {
135     VAConfigID id = VA_INVALID_ID;
136     EXPECT_STATUS(
137         i965_CreateConfig(
138             *this, profile, entrypoint, attribs.data(), attribs.size(), &id));
139     EXPECT_ID(id);
140
141     return id;
142 }
143
144 void I965TestFixture::destroyConfig(VAConfigID id)
145 {
146     EXPECT_STATUS(i965_DestroyConfig(*this, id));
147 }
148
149 VAContextID I965TestFixture::createContext(
150     VAConfigID config, int w, int h, int flags, Surfaces& targets)
151 {
152     VAContextID id = VA_INVALID_ID;
153     EXPECT_STATUS(
154         i965_CreateContext(
155             *this, config, w, h, flags, targets.data(), targets.size(), &id));
156     EXPECT_ID(id);
157
158     return id;
159 }
160
161 void I965TestFixture::destroyContext(VAContextID id)
162 {
163     EXPECT_STATUS(i965_DestroyContext(*this, id));
164 }
165
166 VABufferID I965TestFixture::createBuffer(
167     VAContextID context, VABufferType type,
168     unsigned size, unsigned num, const void *data)
169 {
170     VABufferID id;
171     EXPECT_STATUS(
172         i965_CreateBuffer(*this, context, type, size, num, (void*)data, &id));
173     EXPECT_ID(id);
174
175     return id;
176 }
177
178 void I965TestFixture::beginPicture(VAContextID context, VASurfaceID target)
179 {
180     EXPECT_STATUS(
181         i965_BeginPicture(*this, context, target));
182 }
183
184 void I965TestFixture::renderPicture(
185     VAContextID context, VABufferID *bufs, int num_bufs)
186 {
187     EXPECT_STATUS(
188         i965_RenderPicture(*this, context, bufs, num_bufs));
189 }
190
191 void I965TestFixture::endPicture(VAContextID context)
192 {
193     EXPECT_STATUS(
194         i965_EndPicture(*this, context));
195 }
196
197 void I965TestFixture::destroyBuffer(VABufferID id)
198 {
199     EXPECT_STATUS(
200         i965_DestroyBuffer(*this, id));
201 }
202
203 void I965TestFixture::deriveImage(VASurfaceID surface, VAImage &image)
204 {
205     EXPECT_STATUS(
206         i965_DeriveImage(*this, surface, &image));
207 }
208
209 void I965TestFixture::destroyImage(VAImage &image)
210 {
211     EXPECT_STATUS(
212         i965_DestroyImage(*this, image.image_id));
213 }
214
215 void I965TestFixture::syncSurface(VASurfaceID surface)
216 {
217     EXPECT_STATUS(
218         i965_SyncSurface(*this, surface));
219 }
220
221 class I965TestFixtureTest
222     : public I965TestFixture
223 {
224 protected:
225     virtual void SetUp() { } // override I965TestFixture::SetUp
226     virtual void TearDown() { } // override I965TestFixture::TearDown
227 };
228
229 TEST_F(I965TestFixtureTest, Logic)
230 {
231     VADisplayContextP dispCtx(*this);
232     VADriverContextP drvCtx(*this);
233     struct i965_driver_data* i965(*this);
234     VADisplay display(*this);
235
236     EXPECT_PTR(display);
237     EXPECT_PTR(dispCtx);
238     EXPECT_PTR(drvCtx);
239     EXPECT_TRUE(NULL == i965);
240     EXPECT_TRUE(NULL == drvCtx->handle);
241
242     ASSERT_NO_FATAL_FAILURE(initialize());
243
244     i965 = *this;
245     EXPECT_PTR(i965);
246     EXPECT_PTR(drvCtx->handle);
247
248     terminate();
249 }