Branch
Hash :
5fd368aa
Author :
Date :
2025-07-30T14:45:55
Replace Surface::getWidth/Height() with getSize() Most places in the code use both `getWidth/Height()` methods at the same time. On some back-ends it is more optimal to have single method. For example, `WindowSurfaceWGL` first uses `GetClientRect()` Win32 API inside each method to then calculate width and hight in each method, causing repeated `GetClientRect()` call. The only place where only one of the methods may be used is from `SurfaceImpl::getUserSize()` when one of the parameters is `nullptr`, which is only possible from `QuerySurfaceAttrib()` function. This method is rewritten to use new `getSize()` method and then use whatever dimensions is requested by the caller. This may cause `QuerySurfaceAttrib()` inefficiency on some back-ends that get width and height of the surface differently (`SurfaceEGL` for example). In such cases `getUserSize()` is overridden to avoid returning a dimension which is not required. After this change all places in the code that previously used both `getWidth/Height()` methods (like `Surface::getAttachmentSize()`) will be more efficient. The `QuerySurfaceAttrib()`, on the other hand, will have slightly more overhead on back-ends where base `SurfaceImpl::getUserSize()` method is used, which only happens on back-ends with trivial `getSize()` implementation. Bug: angleproject:397848903 Change-Id: I4a22701a1e62ee4e7d07e77cac5b388050d98e4d Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6802440 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Igor Nazarov <i.nazarov@samsung.com>
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
//
// Copyright 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// WindowSurfaceGLX.cpp: GLX implementation of egl::Surface for windows
#include "common/debug.h"
#include "libANGLE/renderer/gl/glx/DisplayGLX.h"
#include "libANGLE/renderer/gl/glx/FunctionsGLX.h"
#include "libANGLE/renderer/gl/glx/WindowSurfaceGLX.h"
namespace rx
{
static int IgnoreX11Errors(Display *, XErrorEvent *)
{
return 0;
}
WindowSurfaceGLX::WindowSurfaceGLX(const egl::SurfaceState &state,
const FunctionsGLX &glx,
DisplayGLX *glxDisplay,
Window window,
Display *display,
glx::FBConfig fbConfig)
: SurfaceGLX(state),
mParent(window),
mWindow(0),
mDisplay(display),
mUseChildWindow(false),
mParentWidth(0),
mParentHeight(0),
mGLX(glx),
mGLXDisplay(glxDisplay),
mFBConfig(fbConfig),
mGLXWindow(0)
{}
WindowSurfaceGLX::~WindowSurfaceGLX()
{
if (mGLXWindow)
{
mGLX.destroyWindow(mGLXWindow);
}
if (mUseChildWindow && mWindow)
{
// When destroying the window, it may happen that the window has already been
// destroyed by the application (this happens in Chromium). There is no way to
// atomically check that a window exists and to destroy it so instead we call
// XDestroyWindow, ignoring any errors.
auto oldErrorHandler = XSetErrorHandler(IgnoreX11Errors);
XDestroyWindow(mDisplay, mWindow);
XSync(mDisplay, False);
XSetErrorHandler(oldErrorHandler);
}
mGLXDisplay->syncXCommands(true);
}
egl::Error WindowSurfaceGLX::initialize(const egl::Display *display)
{
mUseChildWindow = !mGLXDisplay->isWindowVisualIdSpecified();
XVisualInfo *visualInfo = nullptr;
Colormap colormap = 0;
if (!mUseChildWindow)
{
XWindowAttributes windowAttributes;
XGetWindowAttributes(mDisplay, mParent, &windowAttributes);
unsigned long visualId = windowAttributes.visual->visualid;
// If the window's visual ID is different from the one provided by
// ANGLE_X11_VISUAL_ID, fallback to using a child window.
if (!mGLXDisplay->isMatchingWindowVisualId(visualId))
{
mUseChildWindow = true;
}
}
if (mUseChildWindow)
{
// The visual of the X window, GLX window and GLX context must match,
// however we received a user-created window that can have any visual
// and wouldn't work with our GLX context. To work in all cases, we
// create a child window with the right visual that covers all of its
// parent.
visualInfo = mGLX.getVisualFromFBConfig(mFBConfig);
if (!visualInfo)
{
return egl::Error(EGL_BAD_NATIVE_WINDOW,
"Failed to get the XVisualInfo for the child window.");
}
Visual *visual = visualInfo->visual;
if (!getWindowDimensions(mParent, &mParentWidth, &mParentHeight))
{
return egl::Error(EGL_BAD_NATIVE_WINDOW,
"Failed to get the parent window's dimensions.");
}
// The depth, colormap and visual must match otherwise we get a X error
// so we specify the colormap attribute. Also we do not want the window
// to be taken into account for input so we specify the event and
// do-not-propagate masks to 0 (the defaults). Finally we specify the
// border pixel attribute so that we can use a different visual depth
// than our parent (seems like X uses that as a condition to render
// the subwindow in a different buffer)
XSetWindowAttributes attributes;
unsigned long attributeMask = CWColormap | CWBorderPixel;
colormap = XCreateColormap(mDisplay, mParent, visual, AllocNone);
if (!colormap)
{
XFree(visualInfo);
return egl::Error(EGL_BAD_NATIVE_WINDOW,
"Failed to create the Colormap for the child window.");
}
attributes.colormap = colormap;
attributes.border_pixel = 0;
// TODO(cwallez) set up our own error handler to see if the call failed
mWindow = XCreateWindow(mDisplay, mParent, 0, 0, mParentWidth, mParentHeight, 0,
visualInfo->depth, InputOutput, visual, attributeMask, &attributes);
}
mGLXWindow = mGLX.createWindow(mFBConfig, (mUseChildWindow ? mWindow : mParent), nullptr);
if (mUseChildWindow)
{
XMapWindow(mDisplay, mWindow);
}
XFlush(mDisplay);
if (mUseChildWindow)
{
XFree(visualInfo);
XFreeColormap(mDisplay, colormap);
}
mGLXDisplay->syncXCommands(true);
return egl::NoError();
}
egl::Error WindowSurfaceGLX::makeCurrent(const gl::Context *context)
{
return egl::NoError();
}
egl::Error WindowSurfaceGLX::swap(const gl::Context *context, SurfaceSwapFeedback *feedback)
{
// We need to swap before resizing as some drivers clobber the back buffer
// when the window is resized.
mGLXDisplay->setSwapInterval(mGLXWindow, &mSwapControl);
mGLX.swapBuffers(mGLXWindow);
if (mUseChildWindow)
{
egl::Error error = checkForResize();
if (error.isError())
{
return error;
}
}
return egl::NoError();
}
egl::Error WindowSurfaceGLX::postSubBuffer(const gl::Context *context,
EGLint x,
EGLint y,
EGLint width,
EGLint height)
{
UNIMPLEMENTED();
return egl::NoError();
}
egl::Error WindowSurfaceGLX::querySurfacePointerANGLE(EGLint attribute, void **value)
{
UNIMPLEMENTED();
return egl::NoError();
}
egl::Error WindowSurfaceGLX::bindTexImage(const gl::Context *context,
gl::Texture *texture,
EGLint buffer)
{
UNIMPLEMENTED();
return egl::NoError();
}
egl::Error WindowSurfaceGLX::releaseTexImage(const gl::Context *context, EGLint buffer)
{
UNIMPLEMENTED();
return egl::NoError();
}
void WindowSurfaceGLX::setSwapInterval(const egl::Display *display, EGLint interval)
{
mSwapControl.targetSwapInterval = interval;
}
gl::Extents WindowSurfaceGLX::getSize() const
{
if (mUseChildWindow)
{
// If there's a child window, the size of the window is always the same as the cached
// size of its parent.
return gl::Extents(mParentWidth, mParentHeight, 1);
}
else
{
unsigned int parentWidth, parentHeight;
if (!getWindowDimensions(mParent, &parentWidth, &parentHeight))
{
return gl::Extents(mParentWidth, mParentHeight, 1);
}
return gl::Extents(parentWidth, parentHeight, 1);
}
}
EGLint WindowSurfaceGLX::isPostSubBufferSupported() const
{
UNIMPLEMENTED();
return EGL_FALSE;
}
EGLint WindowSurfaceGLX::getSwapBehavior() const
{
return EGL_BUFFER_DESTROYED;
}
egl::Error WindowSurfaceGLX::checkForResize()
{
// TODO(cwallez) set up our own error handler to see if the call failed
unsigned int newParentWidth, newParentHeight;
if (!getWindowDimensions(mParent, &newParentWidth, &newParentHeight))
{
return egl::Error(EGL_BAD_CURRENT_SURFACE,
"Failed to retrieve the size of the parent window.");
}
if (mParentWidth != newParentWidth || mParentHeight != newParentHeight)
{
mParentWidth = newParentWidth;
mParentHeight = newParentHeight;
mGLX.waitGL();
XResizeWindow(mDisplay, mWindow, mParentWidth, mParentHeight);
mGLX.waitX();
XSync(mDisplay, False);
}
return egl::NoError();
}
glx::Drawable WindowSurfaceGLX::getDrawable() const
{
return mGLXWindow;
}
bool WindowSurfaceGLX::getWindowDimensions(Window window,
unsigned int *width,
unsigned int *height) const
{
Window root;
int x, y;
unsigned int border, depth;
return XGetGeometry(mDisplay, window, &root, &x, &y, width, height, &border, &depth) != 0;
}
egl::Error WindowSurfaceGLX::getSyncValues(EGLuint64KHR *ust, EGLuint64KHR *msc, EGLuint64KHR *sbc)
{
if (!mGLX.getSyncValuesOML(mGLXWindow, reinterpret_cast<int64_t *>(ust),
reinterpret_cast<int64_t *>(msc), reinterpret_cast<int64_t *>(sbc)))
{
return egl::Error(EGL_BAD_SURFACE, "glXGetSyncValuesOML failed.");
}
return egl::NoError();
}
egl::Error WindowSurfaceGLX::getMscRate(EGLint *numerator, EGLint *denominator)
{
if (!mGLX.getMscRateOML(mGLXWindow, reinterpret_cast<int32_t *>(numerator),
reinterpret_cast<int32_t *>(denominator)))
{
return egl::Error(EGL_BAD_SURFACE, "glXGetMscRateOML failed.");
}
if (mGLXDisplay->getRenderer()->getFeatures().clampMscRate.enabled)
{
// Clamp any refresh rate under 2Hz to 30Hz
if (*numerator < *denominator * 2)
{
*numerator = 30;
*denominator = 1;
}
}
return egl::NoError();
}
} // namespace rx