From 34888cfbdcd1769789e922ed81f6f997073120ba Mon Sep 17 00:00:00 2001 From: Tony Barbour Date: Mon, 2 Mar 2015 16:38:52 -0700 Subject: [PATCH] tests: Move environment object into sharable file --- tests/CMakeLists.txt | 1 + tests/blit_tests.cpp | 94 +--------------------------------- tests/test_environment.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++++ tests/test_environment.h | 31 +++++++++++ 4 files changed, 159 insertions(+), 92 deletions(-) create mode 100644 tests/test_environment.cpp create mode 100644 tests/test_environment.h diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 23dfc42..c8d2b1d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,6 +18,7 @@ SET(COMMON_CPP xglrenderframework.cpp xgltestbinding.cpp xgltestframework.cpp + test_environment.cpp ) # Expect libraries to be in either the build (release build) or dbuild (debug) directories diff --git a/tests/blit_tests.cpp b/tests/blit_tests.cpp index 258a85a..1149209 100644 --- a/tests/blit_tests.cpp +++ b/tests/blit_tests.cpp @@ -24,6 +24,7 @@ #include "test_common.h" #include "xgltestbinding.h" +#include "test_environment.h" #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) @@ -31,26 +32,6 @@ namespace xgl_testing { size_t get_format_size(XGL_FORMAT format); -class Environment : public ::testing::Environment { -public: - Environment(); - - bool parse_args(int argc, char **argv); - - virtual void SetUp(); - virtual void TearDown(); - - const std::vector &devices() { return devs_; } - Device &default_device() { return *(devs_[default_dev_]); } - -private: - XGL_APPLICATION_INFO app_; - int default_dev_; - XGL_INSTANCE inst; - - std::vector devs_; -}; - class ImageChecker { public: explicit ImageChecker(const XGL_IMAGE_CREATE_INFO &info, const std::vector ®ions) @@ -99,77 +80,6 @@ private: std::vector pattern_solid_; }; -Environment::Environment() : - default_dev_(0) -{ - app_.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO; - app_.pAppName = "xgl_testing"; - app_.appVersion = 1; - app_.pEngineName = "xgl_testing"; - app_.engineVersion = 1; - app_.apiVersion = XGL_API_VERSION; -} - -bool Environment::parse_args(int argc, char **argv) -{ - int i; - - for (i = 1; i < argc; i++) { -#define ARG(name) (strcmp(argv[i], name) == 0) -#define ARG_P(name) (i < argc - 1 && ARG(name)) - if (ARG_P("--gpu")) { - default_dev_ = atoi(argv[++i]); - } else { - break; - } -#undef ARG -#undef ARG_P - } - - if (i < argc) { - std::cout << - "invalid argument: " << argv[i] << "\n\n" << - "Usage: " << argv[0] << " \n\n" << - "Options:\n" - " --gpu Use GPU as the default GPU\n"; - - return false; - } - - return true; -} - -void Environment::SetUp() -{ - XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS]; - uint32_t count; - XGL_RESULT err; - - err = xglCreateInstance(&app_, NULL, &inst); - ASSERT_EQ(XGL_SUCCESS, err); - err = xglEnumerateGpus(inst, ARRAY_SIZE(gpus), &count, gpus); - ASSERT_EQ(XGL_SUCCESS, err); - ASSERT_GT(count, default_dev_); - - devs_.reserve(count); - for (uint32_t i = 0; i < count; i++) { - devs_.push_back(new Device(gpus[i])); - if (i == default_dev_) { - devs_[i]->init(); - ASSERT_NE(true, devs_[i]->graphics_queues().empty()); - } - } -} - -void Environment::TearDown() -{ - // destroy devices first - for (std::vector::iterator it = devs_.begin(); it != devs_.end(); it++) - delete *it; - devs_.clear(); - - xglDestroyInstance(inst); -} uint32_t ImageChecker::hash_salt_; @@ -582,7 +492,7 @@ namespace { #define DO(action) ASSERT_EQ(true, action); -xgl_testing::Environment *environment; +static xgl_testing::Environment *environment; class XglCmdBlitTest : public ::testing::Test { protected: diff --git a/tests/test_environment.cpp b/tests/test_environment.cpp new file mode 100644 index 0000000..b3f3a9e --- /dev/null +++ b/tests/test_environment.cpp @@ -0,0 +1,125 @@ +#include "test_common.h" +#include "xgltestbinding.h" +#include "test_environment.h" + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) + +namespace xgl_testing { + +Environment::Environment() : + default_dev_(0) +{ + app_.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO; + app_.pAppName = "xgl_testing"; + app_.appVersion = 1; + app_.pEngineName = "xgl_testing"; + app_.engineVersion = 1; + app_.apiVersion = XGL_API_VERSION; +} + +bool Environment::parse_args(int argc, char **argv) +{ + int i; + + for (i = 1; i < argc; i++) { +#define ARG(name) (strcmp(argv[i], name) == 0) +#define ARG_P(name) (i < argc - 1 && ARG(name)) + if (ARG_P("--gpu")) { + default_dev_ = atoi(argv[++i]); + } else { + break; + } +#undef ARG +#undef ARG_P + } + + if (i < argc) { + std::cout << + "invalid argument: " << argv[i] << "\n\n" << + "Usage: " << argv[0] << " \n\n" << + "Options:\n" + " --gpu Use GPU as the default GPU\n"; + + return false; + } + + return true; +} + +void Environment::SetUp() +{ + + uint32_t count; + XGL_RESULT err; + + err = xglCreateInstance(&app_, NULL, &inst); + ASSERT_EQ(XGL_SUCCESS, err); + err = xglEnumerateGpus(inst, ARRAY_SIZE(gpus), &count, gpus); + ASSERT_EQ(XGL_SUCCESS, err); + ASSERT_GT(count, default_dev_); + + devs_.reserve(count); + for (uint32_t i = 0; i < count; i++) { + devs_.push_back(new Device(gpus[i])); + if (i == default_dev_) { + devs_[i]->init(); + ASSERT_NE(true, devs_[i]->graphics_queues().empty()); + } + } +} + +void Environment::X11SetUp() +{ + + uint32_t count; + XGL_RESULT err; + const xcb_setup_t *setup; + xcb_screen_iterator_t iter; + int scr; + + err = xglCreateInstance(&app_, NULL, &inst); + ASSERT_EQ(XGL_SUCCESS, err); + err = xglEnumerateGpus(inst, ARRAY_SIZE(gpus), &count, gpus); + ASSERT_EQ(XGL_SUCCESS, err); + ASSERT_GT(count, default_dev_); + + m_connection = xcb_connect(NULL, &scr); + + setup = xcb_get_setup(m_connection); + iter = xcb_setup_roots_iterator(setup); + while (scr-- > 0) + xcb_screen_next(&iter); + + m_screen = iter.data; + + const XGL_WSI_X11_CONNECTION_INFO connection_info = { + .pConnection = m_connection, + .root = m_screen->root, + .provider = 0, + }; + + err = xglWsiX11AssociateConnection(gpus[0], &connection_info); + assert(!err); + + + devs_.reserve(count); + for (uint32_t i = 0; i < count; i++) { + devs_.push_back(new Device(gpus[i])); + if (i == default_dev_) { + devs_[i]->init(); + ASSERT_NE(true, devs_[i]->graphics_queues().empty()); + } + } +} + +void Environment::TearDown() +{ + // destroy devices first + for (std::vector::iterator it = devs_.begin(); it != devs_.end(); it++) + delete *it; + devs_.clear(); + + xglDestroyInstance(inst); + xcb_disconnect(m_connection); +} +} // xgl_testing namespace diff --git a/tests/test_environment.h b/tests/test_environment.h new file mode 100644 index 0000000..69d574e --- /dev/null +++ b/tests/test_environment.h @@ -0,0 +1,31 @@ +#ifndef TEST_ENVIRONMENT_H +#define TEST_ENVIRONMENT_H +#include "xgltestbinding.h" +#include + +namespace xgl_testing { +class Environment : public ::testing::Environment { +public: + Environment(); + + bool parse_args(int argc, char **argv); + + virtual void SetUp(); + virtual void X11SetUp(); + virtual void TearDown(); + xcb_connection_t *m_connection; + xcb_screen_t *m_screen; + + const std::vector &devices() { return devs_; } + Device &default_device() { return *(devs_[default_dev_]); } + XGL_PHYSICAL_GPU gpus[XGL_MAX_PHYSICAL_GPUS]; + +private: + XGL_APPLICATION_INFO app_; + int default_dev_; + XGL_INSTANCE inst; + + std::vector devs_; +}; +} +#endif // TEST_ENVIRONMENT_H -- 2.7.4