1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
/*
* Copyright (C) 2019 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "WebKitProtocolHandler.h"
#include "BuildRevision.h"
#include "WebKitError.h"
#include "WebKitVersion.h"
#include "WebKitWebView.h"
#include <WebCore/FloatRect.h>
#include <WebCore/GLContext.h>
#include <WebCore/IntRect.h>
#include <WebCore/PlatformDisplay.h>
#include <WebCore/PlatformScreen.h>
#include <epoxy/gl.h>
#include <gio/gio.h>
#include <wtf/URL.h>
#include <wtf/WorkQueue.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/GUniquePtr.h>
#if OS(UNIX)
#include <sys/utsname.h>
#endif
#if USE(CAIRO)
#include <cairo.h>
#endif
#if PLATFORM(GTK)
#if USE(EGL)
#include "AcceleratedBackingStoreDMABuf.h"
#include "DMABufRendererBufferMode.h"
#include <WebCore/PlatformDisplaySurfaceless.h>
#endif
#include <gtk/gtk.h>
#if PLATFORM(WAYLAND)
#include <wpe/wpe.h>
#include <wpe/fdo.h>
#endif
#if PLATFORM(X11)
#include <WebCore/PlatformDisplayX11.h>
#endif
#if USE(GBM)
#include <WebCore/PlatformDisplayGBM.h>
#endif
#endif
#if USE(EGL)
#include <epoxy/egl.h>
#endif
#if USE(GSTREAMER)
#include <gst/gst.h>
#endif
namespace WebKit {
using namespace WebCore;
WebKitProtocolHandler::WebKitProtocolHandler(WebKitWebContext* context)
{
webkit_web_context_register_uri_scheme(context, "webkit", [](WebKitURISchemeRequest* request, gpointer userData) {
static_cast<WebKitProtocolHandler*>(userData)->handleRequest(request);
}, this, nullptr);
auto* manager = webkit_web_context_get_security_manager(context);
webkit_security_manager_register_uri_scheme_as_display_isolated(manager, "webkit");
webkit_security_manager_register_uri_scheme_as_local(manager, "webkit");
}
void WebKitProtocolHandler::handleRequest(WebKitURISchemeRequest* request)
{
URL requestURL = URL(String::fromLatin1(webkit_uri_scheme_request_get_uri(request)));
if (requestURL.host() == "gpu"_s) {
handleGPU(request);
return;
}
GUniquePtr<GError> error(g_error_new_literal(WEBKIT_POLICY_ERROR, WEBKIT_POLICY_ERROR_CANNOT_SHOW_URI, "Not found"));
webkit_uri_scheme_request_finish_error(request, error.get());
}
static inline const char* webkitPortName()
{
#if PLATFORM(GTK)
return "WebKitGTK";
#elif PLATFORM(WPE)
return "WPE WebKit";
#endif
RELEASE_ASSERT_NOT_REACHED();
}
static const char* hardwareAccelerationPolicy(WebKitURISchemeRequest* request)
{
#if PLATFORM(WPE)
return "always";
#elif PLATFORM(GTK)
auto* webView = webkit_uri_scheme_request_get_web_view(request);
ASSERT(webView);
switch (webkit_settings_get_hardware_acceleration_policy(webkit_web_view_get_settings(webView))) {
case WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER:
return "never";
case WEBKIT_HARDWARE_ACCELERATION_POLICY_ALWAYS:
return "always";
#if !USE(GTK4)
case WEBKIT_HARDWARE_ACCELERATION_POLICY_ON_DEMAND:
return "on demand";
#endif
}
#endif
RELEASE_ASSERT_NOT_REACHED();
}
#if ENABLE(WEBGL)
static bool webGLEnabled(WebKitURISchemeRequest* request)
{
auto* webView = webkit_uri_scheme_request_get_web_view(request);
ASSERT(webView);
return webkit_settings_get_enable_webgl(webkit_web_view_get_settings(webView));
}
#endif
static bool uiProcessContextIsEGL()
{
#if USE(EGL)
#if PLATFORM(GTK)
return !!PlatformDisplay::sharedDisplay().gtkEGLDisplay();
#else
return true;
#endif
#else
return false;
#endif
}
static const char* openGLAPI()
{
if (epoxy_is_desktop_gl())
return "OpenGL (libepoxy)";
return "OpenGL ES 2 (libepoxy)";
}
#if PLATFORM(GTK)
static String dmabufRendererWithSupportedBuffers()
{
StringBuilder buffers;
buffers.append("DMABuf (Supported buffers: "_s);
#if USE(EGL)
auto mode = AcceleratedBackingStoreDMABuf::rendererBufferMode();
if (mode.contains(DMABufRendererBufferMode::Hardware))
buffers.append("Hardware"_s);
if (mode.contains(DMABufRendererBufferMode::SharedMemory)) {
if (mode.contains(DMABufRendererBufferMode::Hardware))
buffers.append(", ");
buffers.append("Shared Memory"_s);
}
#endif
buffers.append(')');
return buffers.toString();
}
#endif
void WebKitProtocolHandler::handleGPU(WebKitURISchemeRequest* request)
{
GString* html = g_string_new(
"<html><head><title>GPU information</title>"
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
"<style>"
" h1 { color: #babdb6; text-shadow: 0 1px 0 white; margin-bottom: 0; }"
" html { font-family: -webkit-system-font; font-size: 11pt; color: #2e3436; padding: 20px 20px 0 20px; background-color: #f6f6f4; "
" background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #eeeeec), color-stop(1, #f6f6f4));"
" background-size: 100% 5em; background-repeat: no-repeat; }"
" table { width: 100%; border-collapse: collapse; }"
" table, td { border: 1px solid #d3d7cf; border-left: none; border-right: none; }"
" p { margin-bottom: 30px; }"
" td { padding: 15px; }"
" td.data { width: 200px; }"
" .titlename { font-weight: bold; }"
"</style>");
StringBuilder tablesBuilder;
auto startTable = [&](auto header) {
tablesBuilder.append("<h1>"_s, header, "</h1><table>"_s);
};
auto addTableRow = [&](auto& jsonObject, auto key, auto&& value) {
tablesBuilder.append("<tbody><tr><td><div class=\"titlename\">"_s, key, "</div></td><td>"_s, value, "</td></tr></tbody>"_s);
jsonObject->setString(key, value);
};
auto stopTable = [&] {
tablesBuilder.append("</table>"_s);
};
#if USE(EGL)
auto addEGLInfo = [&](auto& jsonObject) {
addTableRow(jsonObject, "GL_RENDERER"_s, String::fromUTF8(reinterpret_cast<const char*>(glGetString(GL_RENDERER))));
addTableRow(jsonObject, "GL_VENDOR"_s, String::fromUTF8(reinterpret_cast<const char*>(glGetString(GL_VENDOR))));
addTableRow(jsonObject, "GL_VERSION"_s, String::fromUTF8(reinterpret_cast<const char*>(glGetString(GL_VERSION))));
addTableRow(jsonObject, "GL_SHADING_LANGUAGE_VERSION"_s, String::fromUTF8(reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION))));
switch (eglQueryAPI()) {
case EGL_OPENGL_ES_API:
addTableRow(jsonObject, "GL_EXTENSIONS"_s, String::fromUTF8(reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS))));
break;
case EGL_OPENGL_API: {
StringBuilder extensionsBuilder;
GLint numExtensions = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
for (GLint i = 0; i < numExtensions; ++i) {
if (i)
extensionsBuilder.append(' ');
extensionsBuilder.append(reinterpret_cast<const char*>(glGetStringi(GL_EXTENSIONS, i)));
}
addTableRow(jsonObject, "GL_EXTENSIONS"_s, extensionsBuilder.toString());
break;
}
}
auto eglDisplay = eglGetCurrentDisplay();
addTableRow(jsonObject, "EGL_VERSION"_s, String::fromUTF8(eglQueryString(eglDisplay, EGL_VERSION)));
addTableRow(jsonObject, "EGL_VENDOR"_s, String::fromUTF8(eglQueryString(eglDisplay, EGL_VENDOR)));
addTableRow(jsonObject, "EGL_EXTENSIONS"_s, makeString(eglQueryString(nullptr, EGL_EXTENSIONS), ' ', eglQueryString(eglDisplay, EGL_EXTENSIONS)));
};
#endif
auto jsonObject = JSON::Object::create();
startTable("Version Information"_s);
auto versionObject = JSON::Object::create();
addTableRow(versionObject, "WebKit version"_s, makeString(webkitPortName(), ' ', WEBKIT_MAJOR_VERSION, '.', WEBKIT_MINOR_VERSION, '.', WEBKIT_MICRO_VERSION, " ("_s, BUILD_REVISION, ')'));
#if OS(UNIX)
struct utsname osName;
uname(&osName);
addTableRow(versionObject, "Operating system"_s, makeString(osName.sysname, ' ', osName.release, ' ', osName.version, ' ', osName.machine));
#endif
const char* desktopName = g_getenv("XDG_CURRENT_DESKTOP");
addTableRow(versionObject, "Desktop"_s, (desktopName && *desktopName) ? String::fromUTF8(desktopName) : "Unknown"_s);
#if USE(CAIRO)
addTableRow(versionObject, "Cairo version"_s, makeString(CAIRO_VERSION_STRING, " (build) "_s, cairo_version_string(), " (runtime)"_s));
#endif
#if USE(GSTREAMER)
GUniquePtr<char> gstVersion(gst_version_string());
addTableRow(versionObject, "GStreamer version"_s, makeString(GST_VERSION_MAJOR, '.', GST_VERSION_MINOR, '.', GST_VERSION_MICRO, " (build) "_s, gstVersion.get(), " (runtime)"_s));
#endif
#if PLATFORM(GTK)
addTableRow(versionObject, "GTK version"_s, makeString(GTK_MAJOR_VERSION, '.', GTK_MINOR_VERSION, '.', GTK_MICRO_VERSION, " (build) "_s, gtk_get_major_version(), '.', gtk_get_minor_version(), '.', gtk_get_micro_version(), " (runtime)"_s));
#if USE(EGL)
bool usingDMABufRenderer = AcceleratedBackingStoreDMABuf::checkRequirements();
#else
bool usingDMABufRenderer = false;
#endif
#if PLATFORM(WAYLAND)
if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::Wayland && !usingDMABufRenderer) {
addTableRow(versionObject, "WPE version"_s, makeString(WPE_MAJOR_VERSION, '.', WPE_MINOR_VERSION, '.', WPE_MICRO_VERSION, " (build) "_s, wpe_get_major_version(), '.', wpe_get_minor_version(), '.', wpe_get_micro_version(), " (runtime)"_s));
#if WPE_FDO_CHECK_VERSION(1, 6, 1)
addTableRow(versionObject, "WPEBackend-fdo version"_s, makeString(WPE_FDO_MAJOR_VERSION, '.', WPE_FDO_MINOR_VERSION, '.', WPE_FDO_MICRO_VERSION, " (build) "_s, wpe_fdo_get_major_version(), '.', wpe_fdo_get_minor_version(), '.', wpe_fdo_get_micro_version(), " (runtime)"_s));
#endif
}
#endif
#endif
#if PLATFORM(WPE)
addTableRow(versionObject, "WPE version"_s, makeString(WPE_MAJOR_VERSION, '.', WPE_MINOR_VERSION, '.', WPE_MICRO_VERSION, " (build) "_s, wpe_get_major_version(), '.', wpe_get_minor_version(), '.', wpe_get_micro_version(), " (runtime)"_s));
addTableRow(versionObject, "WPE backend"_s, String::fromUTF8(wpe_loader_get_loaded_implementation_library_name()));
#endif
stopTable();
jsonObject->setObject("Version Information"_s, WTFMove(versionObject));
auto displayObject = JSON::Object::create();
startTable("Display Information"_s);
#if PLATFORM(GTK)
StringBuilder typeStringBuilder;
#if PLATFORM(WAYLAND)
if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::Wayland)
typeStringBuilder.append("Wayland"_s);
#endif
#if PLATFORM(X11)
if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::X11)
typeStringBuilder.append("X11"_s);
#endif
addTableRow(displayObject, "Type"_s, !typeStringBuilder.isEmpty() ? typeStringBuilder.toString() : "Unknown"_s);
#endif // PLATFORM(GTK)
const char* policy = hardwareAccelerationPolicy(request);
auto rect = IntRect(screenRect(nullptr));
addTableRow(displayObject, "Screen geometry"_s, makeString(rect.x(), ',', rect.y(), ' ', rect.width(), 'x', rect.height()));
rect = IntRect(screenAvailableRect(nullptr));
addTableRow(displayObject, "Screen work area"_s, makeString(rect.x(), ',', rect.y(), ' ', rect.width(), 'x', rect.height()));
addTableRow(displayObject, "Depth"_s, String::number(screenDepth(nullptr)));
addTableRow(displayObject, "Bits per color component"_s, String::number(screenDepthPerComponent(nullptr)));
addTableRow(displayObject, "DPI"_s, String::number(screenDPI()));
#if USE(GBM)
if (strcmp(policy, "never")) {
auto deviceFile = PlatformDisplay::sharedDisplay().drmDeviceFile();
if (!deviceFile.isEmpty())
addTableRow(displayObject, "DRM Device"_s, deviceFile);
auto renderNode = PlatformDisplay::sharedDisplay().drmRenderNodeFile();
if (!renderNode.isEmpty())
addTableRow(displayObject, "DRM Render Node"_s, renderNode);
}
#endif
stopTable();
jsonObject->setObject("Display Information"_s, WTFMove(displayObject));
auto hardwareAccelerationObject = JSON::Object::create();
startTable("Hardware Acceleration Information"_s);
addTableRow(hardwareAccelerationObject, "Policy"_s, String::fromUTF8(policy));
#if ENABLE(WEBGL)
addTableRow(hardwareAccelerationObject, "WebGL enabled"_s, webGLEnabled(request) ? "Yes"_s : "No"_s);
#endif
std::unique_ptr<PlatformDisplay> renderDisplay;
if (strcmp(policy, "never")) {
addTableRow(jsonObject, "API"_s, String::fromUTF8(openGLAPI()));
#if PLATFORM(WAYLAND)
if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::Wayland)
addTableRow(hardwareAccelerationObject, "Renderer"_s, usingDMABufRenderer ? dmabufRendererWithSupportedBuffers() : "WPE"_s);
#endif
#if PLATFORM(X11)
if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::X11)
addTableRow(hardwareAccelerationObject, "Renderer"_s, usingDMABufRenderer ? dmabufRendererWithSupportedBuffers() : "XWindow"_s);
#endif
addTableRow(hardwareAccelerationObject, "Native interface"_s, uiProcessContextIsEGL() ? "EGL"_s : "None"_s);
#if USE(EGL)
if (uiProcessContextIsEGL() && eglGetCurrentContext() != EGL_NO_CONTEXT)
addEGLInfo(hardwareAccelerationObject);
#endif // USE(EGL)
}
stopTable();
jsonObject->setObject("Hardware Acceleration Information"_s, WTFMove(hardwareAccelerationObject));
#if USE(EGL) && PLATFORM(GTK)
if (strcmp(policy, "never")) {
std::unique_ptr<PlatformDisplay> platformDisplay;
if (usingDMABufRenderer) {
#if USE(GBM)
const char* disableGBM = getenv("WEBKIT_DMABUF_RENDERER_DISABLE_GBM");
if (!disableGBM || !strcmp(disableGBM, "0")) {
if (auto* device = PlatformDisplay::sharedDisplay().gbmDevice())
platformDisplay = PlatformDisplayGBM::create(device);
}
#endif
if (!platformDisplay)
platformDisplay = PlatformDisplaySurfaceless::create();
}
if (platformDisplay || !uiProcessContextIsEGL()) {
auto hardwareAccelerationObject = JSON::Object::create();
startTable("Hardware Acceleration Information (Render Process)"_s);
if (platformDisplay)
addTableRow(hardwareAccelerationObject, "Platform"_s, String::fromUTF8(platformDisplay->type() == PlatformDisplay::Type::Surfaceless ? "Surfaceless"_s : "GBM"_s));
if (uiProcessContextIsEGL()) {
GLContext::ScopedGLContext glContext(GLContext::createOffscreen(platformDisplay ? *platformDisplay : PlatformDisplay::sharedDisplay()));
addEGLInfo(hardwareAccelerationObject);
} else {
// Create the context in a different thread to ensure it doesn't affect any current context in the main thread.
WorkQueue::create("GPU handler EGL context")->dispatchSync([&] {
auto glContext = GLContext::createOffscreen(platformDisplay ? *platformDisplay : PlatformDisplay::sharedDisplay());
glContext->makeContextCurrent();
addEGLInfo(hardwareAccelerationObject);
});
}
stopTable();
jsonObject->setObject("Hardware Acceleration Information (Render process)"_s, WTFMove(hardwareAccelerationObject));
if (platformDisplay) {
// Clear the contexts used by the display before it's destroyed.
platformDisplay->clearSharingGLContext();
}
}
}
#endif
auto infoAsString = jsonObject->toJSONString();
g_string_append_printf(html, "<script>function copyAsJSON() { "
"var textArea = document.createElement('textarea');"
"textArea.value = JSON.stringify(%s, null, 4);"
"document.body.appendChild(textArea);"
"textArea.focus();"
"textArea.select();"
"document.execCommand('copy');"
"document.body.removeChild(textArea);"
"}</script>", infoAsString.utf8().data());
g_string_append_printf(html, "<script>function sendToConsole() { "
"console.log(JSON.stringify(%s, null, 4));"
"}</script>", infoAsString.utf8().data());
g_string_append(html, "</head><body>");
#if PLATFORM(GTK)
g_string_append(html, "<button onclick=\"copyAsJSON()\">Copy to clipboard</button>");
#else
// WPE doesn't seem to pass clipboard data yet.
g_string_append(html, "<button onclick=\"sendToConsole()\">Send to JS console</button>");
#endif
g_string_append_printf(html, "%s</body></html>", tablesBuilder.toString().utf8().data());
gsize streamLength = html->len;
GRefPtr<GInputStream> stream = adoptGRef(g_memory_input_stream_new_from_data(g_string_free(html, FALSE), streamLength, g_free));
webkit_uri_scheme_request_finish(request, stream.get(), streamLength, "text/html");
}
} // namespace WebKit
|