Skip to content

Commit

Permalink
Bug 1828192 [Linux] Implement common glx/vaapi test routines in GfxIn…
Browse files Browse the repository at this point in the history
…foUtils.h r=emilio

Differential Revision: https://phabricator.services.mozilla.com/D175805
  • Loading branch information
stransky committed Apr 20, 2023
1 parent f5be0a3 commit 9c396d1
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
85 changes: 85 additions & 0 deletions widget/gtk/GfxInfoUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* vim: se cin sw=2 ts=2 et : */
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef WIDGET_GTK_GFXINFO_UTILS_h__
#define WIDGET_GTK_GFXINFO_UTILS_h__

// An alternative to mozilla::Unused for use in (a) C code and (b) code where
// linking with unused.o is difficult.
#define MOZ_UNUSED(expr) \
do { \
if (expr) { \
(void)0; \
} \
} while (0)

#define LOG_PIPE 2

static bool enable_logging = false;
static void log(const char* format, ...) {
if (!enable_logging) {
return;
}
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}

// C++ standard collides with C standard in that it doesn't allow casting void*
// to function pointer types. So the work-around is to convert first to size_t.
// http://www.trilithium.com/johan/2004/12/problem-with-dlsym/
template <typename func_ptr_type>
static func_ptr_type cast(void* ptr) {
return reinterpret_cast<func_ptr_type>(reinterpret_cast<size_t>(ptr));
}

#define BUFFER_SIZE_STEP 4000

static char* test_buf = nullptr;
static int test_bufsize = 0;
static int test_length = 0;

static void record_value(const char* format, ...) {
if (!test_buf || test_length + BUFFER_SIZE_STEP / 2 > test_bufsize) {
test_bufsize += BUFFER_SIZE_STEP;
test_buf = (char*)realloc(test_buf, test_bufsize);
}
int remaining = test_bufsize - test_length;

// Append the new values to the buffer, not to exceed the remaining space.
va_list args;
va_start(args, format);
int max_added = vsnprintf(test_buf + test_length, remaining, format, args);
va_end(args);

if (max_added >= remaining) {
test_length += remaining;
} else {
test_length += max_added;
}
}

static void record_error(const char* str) { record_value("ERROR\n%s\n", str); }

static void record_warning(const char* str) {
record_value("WARNING\n%s\n", str);
}

static void record_flush(int out_pipe) {
if (!test_buf) {
return;
}
MOZ_UNUSED(write(out_pipe, test_buf, test_length));
if (enable_logging) {
MOZ_UNUSED(write(LOG_PIPE, test_buf, test_length));
}
free(test_buf);
test_buf = nullptr;
}

#endif /* WIDGET_GTK_GFXINFO_h__ */
1 change: 1 addition & 0 deletions widget/gtk/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ EXPORTS += [
]

EXPORTS.mozilla += [
"GfxInfoUtils.h",
"GRefPtr.h",
"GUniquePtr.h",
"WidgetUtilsGtk.h",
Expand Down

0 comments on commit 9c396d1

Please sign in to comment.