Hash :
25390156
Author :
Date :
2025-08-21T00:13:19
Suppress unsafe buffers on a file-by-file basis in src/ [1 of N] In this CL, we suppress many files but stop short of actually enabling the warning by not removing the line from the unsafe_buffers_paths.txt file. That will happen in a follow-on CL, along with resolving any stragglers missed here. This is mostly a manual change so as to familiarize myself with the kinds of issues faced by the Angle codebase when applying buffer safety warnings. -- Re-generate affected hashes. -- Clang-format applied to all changed files. -- Add a few missing .reserve() calls to vectors as noticed. -- Fix some mismatches between file names and header comments. -- Be more consistent with header comment format (blank lines and trailing //-only lines when a filename comment adjoins license boilerplate). Bug: b/436880895 Change-Id: I3bde5cc2059acbe8345057289214f1a26f1c34aa Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6869022 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
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
//
// Copyright 2018 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.
//
// EGLDirectCompositionTest.cpp:
// Tests pertaining to DirectComposition and WindowsUIComposition.
#ifdef UNSAFE_BUFFERS_BUILD
# pragma allow_unsafe_buffers
#endif
#ifdef ANGLE_ENABLE_D3D11_COMPOSITOR_NATIVE_WINDOW
# include <d3d11.h>
# include "test_utils/ANGLETest.h"
# include <DispatcherQueue.h>
# include <VersionHelpers.h>
# include <Windows.Foundation.h>
# include <windows.ui.composition.desktop.h>
# include <windows.ui.composition.h>
# include <windows.ui.composition.interop.h>
# include <wrl.h>
# include <memory>
# include "libANGLE/renderer/d3d/d3d11/converged/CompositorNativeWindow11.h"
# include "util/OSWindow.h"
# include "util/com_utils.h"
# include "util/test_utils.h"
using namespace angle;
using namespace ABI::Windows::System;
using namespace ABI::Windows::UI::Composition;
using namespace ABI::Windows::UI::Composition::Desktop;
using namespace ABI::Windows::Foundation;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
const int WINDOWWIDTH = 200, WINDOWHEIGHT = 200;
class EGLDirectCompositionTest : public ANGLETest<>
{
protected:
EGLDirectCompositionTest() : mOSWindow(nullptr) {}
void testSetUp() override
{
if (!mRoHelper.SupportedWindowsRelease())
{
return;
}
// Create an OS Window
mOSWindow = OSWindow::New();
mOSWindow->initialize("EGLDirectCompositionTest", WINDOWWIDTH, WINDOWHEIGHT);
auto nativeWindow = mOSWindow->getNativeWindow();
setWindowVisible(mOSWindow, true);
// Create DispatcherQueue for window to process compositor callbacks
CreateDispatcherQueue(mDispatcherController);
HSTRING act;
HSTRING_HEADER header;
auto hr = mRoHelper.GetStringReference(RuntimeClass_Windows_UI_Composition_Compositor, &act,
&header);
ASSERT_TRUE(SUCCEEDED(hr));
void *fac = nullptr;
hr = mRoHelper.GetActivationFactory(act, __uuidof(IActivationFactory), &fac);
ASSERT_TRUE(SUCCEEDED(hr));
ComPtr<IActivationFactory> compositorFactory;
compositorFactory.Attach((IActivationFactory *)fac);
hr = compositorFactory->ActivateInstance(&mCompositor);
ASSERT_TRUE(SUCCEEDED(hr));
// Create a DesktopWindowTarget against native window (HWND)
CreateDesktopWindowTarget(mCompositor, static_cast<HWND>(nativeWindow), mDesktopTarget);
ASSERT_TRUE(SUCCEEDED(mCompositor->CreateSpriteVisual(mAngleHost.GetAddressOf())));
ComPtr<IVisual> angleVis;
ASSERT_TRUE(SUCCEEDED(mAngleHost.As(&angleVis)));
ASSERT_TRUE(SUCCEEDED(angleVis->put_Size(
{static_cast<FLOAT>(WINDOWWIDTH), static_cast<FLOAT>(WINDOWHEIGHT)})));
ASSERT_TRUE(SUCCEEDED(angleVis->put_Offset({0, 0, 0})));
ComPtr<ICompositionTarget> compTarget;
ASSERT_TRUE(SUCCEEDED(mDesktopTarget.As(&compTarget)));
ASSERT_TRUE(SUCCEEDED(compTarget->put_Root(angleVis.Get())));
Init();
}
void CreateDispatcherQueue(ComPtr<IDispatcherQueueController> &controller)
{
DispatcherQueueOptions options{sizeof(DispatcherQueueOptions), DQTYPE_THREAD_CURRENT,
DQTAT_COM_STA};
auto hr = mRoHelper.CreateDispatcherQueueController(options, controller.GetAddressOf());
ASSERT_TRUE(SUCCEEDED(hr));
}
void CreateDesktopWindowTarget(ComPtr<ICompositor> const &compositor,
const HWND window,
ComPtr<IDesktopWindowTarget> &target)
{
namespace abi = ABI::Windows::UI::Composition::Desktop;
ComPtr<ICompositorDesktopInterop> interop;
ASSERT_TRUE(SUCCEEDED(compositor.As(&interop)));
ASSERT_TRUE(SUCCEEDED(interop->CreateDesktopWindowTarget(
window, true, reinterpret_cast<abi::IDesktopWindowTarget **>(target.GetAddressOf()))));
}
void Init()
{
if (!mRoHelper.SupportedWindowsRelease())
{
return;
}
DPI_AWARENESS_CONTEXT
WINAPI
SetThreadDpiAwarenessContext(_In_ DPI_AWARENESS_CONTEXT dpiContext);
auto userModule = LoadLibraryA("user32.dll");
if (userModule == nullptr)
{
return;
}
auto temp = GetProcAddress(userModule, "SetThreadDpiAwarenessContext");
mFpSetThreadDpiAwarenessContext = reinterpret_cast<_SetThreadDpiAwarenessContext *>(temp);
const EGLint configAttributes[] = {
EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 8, EGL_STENCIL_SIZE, 8, EGL_NONE};
const EGLAttrib defaultDisplayAttributes[] = {
EGL_PLATFORM_ANGLE_TYPE_ANGLE,
EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
EGL_NONE,
};
PFNEGLGETPLATFORMDISPLAYPROC eglGetPlatformDisplay =
reinterpret_cast<PFNEGLGETPLATFORMDISPLAYPROC>(
eglGetProcAddress("eglGetPlatformDisplay"));
ASSERT_TRUE(eglGetPlatformDisplay != nullptr);
mEglDisplay =
eglGetPlatformDisplay(GetEglPlatform(), reinterpret_cast<void *>(EGL_DEFAULT_DISPLAY),
defaultDisplayAttributes);
ASSERT_TRUE(mEglDisplay != EGL_NO_DISPLAY);
ASSERT_EGL_TRUE(eglInitialize(mEglDisplay, nullptr, nullptr));
EGLint nConfigs = 0;
ASSERT_EGL_TRUE(eglGetConfigs(mEglDisplay, nullptr, 0, &nConfigs));
ASSERT_TRUE(nConfigs != 0);
ASSERT_EGL_TRUE(eglChooseConfig(mEglDisplay, configAttributes, &mEglConfig, 1, &nConfigs));
}
void CreateSurface(ComPtr<ABI::Windows::UI::Composition::ISpriteVisual> visual,
EGLSurface &surface)
{
auto displayExtensions = eglQueryString(mEglDisplay, EGL_EXTENSIONS);
// Check that the EGL_ANGLE_windows_ui_composition display extension is available
ASSERT_TRUE(strstr(displayExtensions, "EGL_ANGLE_windows_ui_composition") != nullptr);
const EGLint contextAttributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
// Use a spritevisual as the nativewindowtype
surface =
eglCreateWindowSurface(mEglDisplay, mEglConfig,
static_cast<EGLNativeWindowType>((void *)visual.Get()), nullptr);
ASSERT_TRUE(surface != EGL_NO_SURFACE);
mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, contextAttributes);
ASSERT_TRUE(mEglContext != EGL_NO_CONTEXT);
ASSERT_TRUE(eglMakeCurrent(mEglDisplay, surface, surface, mEglContext) != EGL_FALSE);
}
void testTearDown() override
{
if (!mRoHelper.SupportedWindowsRelease())
{
return;
}
if (mEglDisplay != EGL_NO_DISPLAY)
{
ASSERT_EGL_TRUE(eglTerminate(mEglDisplay));
mEglDisplay = EGL_NO_DISPLAY;
}
OSWindow::Delete(&mOSWindow);
}
OSWindow *mOSWindow;
ComPtr<ICompositor> mCompositor;
ComPtr<IDispatcherQueueController> mDispatcherController;
ComPtr<ICompositionColorBrush> mColorBrush;
ComPtr<IDesktopWindowTarget> mDesktopTarget;
ComPtr<ISpriteVisual> mAngleHost;
EGLDisplay mEglDisplay;
EGLContext mEglContext;
EGLConfig mEglConfig;
rx::RoHelper mRoHelper;
using _SetThreadDpiAwarenessContext =
DPI_AWARENESS_CONTEXT WINAPI(DPI_AWARENESS_CONTEXT dpiContext);
_SetThreadDpiAwarenessContext *mFpSetThreadDpiAwarenessContext;
};
// This tests that a surface created using a SpriteVisual as container has the expected dimensions
// which should match the dimensions of the SpriteVisual passed in
TEST_P(EGLDirectCompositionTest, SurfaceSizeFromSpriteSize)
{
// Only attempt this test when on Windows 10 1803+
ANGLE_SKIP_TEST_IF(!mRoHelper.SupportedWindowsRelease());
EGLSurface s{nullptr};
CreateSurface(mAngleHost, s);
EGLint surfacewidth = 0, surfaceheight = 0;
eglQuerySurface(mEglDisplay, s, EGL_WIDTH, &surfacewidth);
eglQuerySurface(mEglDisplay, s, EGL_HEIGHT, &surfaceheight);
ComPtr<IVisual> angleVis;
ASSERT_TRUE(SUCCEEDED(mAngleHost.As(&angleVis)));
ABI::Windows::Foundation::Numerics::Vector2 visualsize{0, 0};
ASSERT_TRUE(SUCCEEDED(angleVis->get_Size(&visualsize)));
ASSERT_TRUE(surfacewidth == static_cast<int>(visualsize.X));
ASSERT_TRUE(surfaceheight == static_cast<int>(visualsize.Y));
ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) !=
EGL_FALSE);
ASSERT_EGL_TRUE(eglDestroySurface(mEglDisplay, s));
ASSERT_EGL_TRUE(eglDestroyContext(mEglDisplay, mEglContext));
mEglContext = EGL_NO_CONTEXT;
}
// This tests that a WindowSurface can be created using a SpriteVisual as the containing window
// and that pixels can be successfully rendered into the resulting WindowSurface
TEST_P(EGLDirectCompositionTest, RenderSolidColor)
{
// Only attempt this test when on Windows 10 1803+
ANGLE_SKIP_TEST_IF(!mRoHelper.SupportedWindowsRelease());
EGLSurface s{nullptr};
CreateSurface(mAngleHost, s);
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glViewport(0, 0, WINDOWWIDTH, WINDOWHEIGHT);
glClear(GL_COLOR_BUFFER_BIT);
ASSERT_EGL_TRUE(eglSwapBuffers(mEglDisplay, s));
// ensure user/DWM have a chance to paint the window and kick it to the top of the desktop
// zorder before we attempt to sample
angle::Sleep(200);
mOSWindow->messageLoop();
uint8_t *pixelBuffer = static_cast<uint8_t *>(malloc(WINDOWWIDTH * WINDOWHEIGHT * 4));
ZeroMemory(pixelBuffer, WINDOWWIDTH * WINDOWHEIGHT * 4);
// In order to accurately capture a bitmap, we need to temporarily shift into per-monitor DPI
// mode in order to get the window offset from desktop correct
auto previous = mFpSetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
bool success = mOSWindow->takeScreenshot(pixelBuffer);
mFpSetThreadDpiAwarenessContext(previous);
ASSERT_EGL_TRUE(success);
ASSERT_EGL_TRUE(pixelBuffer[(50 * 50 * 4)] == 255);
ASSERT_EGL_TRUE(pixelBuffer[(50 * 50 * 4) + 1] == 0);
ASSERT_EGL_TRUE(pixelBuffer[(50 * 50 * 4) + 2] == 0);
ASSERT_EGL_TRUE(pixelBuffer[(50 * 50 * 4) + 3] == 255);
ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) !=
EGL_FALSE);
ASSERT_EGL_TRUE(eglDestroySurface(mEglDisplay, s));
ASSERT_EGL_TRUE(eglDestroyContext(mEglDisplay, mEglContext));
mEglContext = EGL_NO_CONTEXT;
}
ANGLE_INSTANTIATE_TEST(EGLDirectCompositionTest, WithNoFixture(ES2_D3D11()));
#endif // ANGLE_ENABLE_D3D11_COMPOSITOR_NATIVE_WINDOW