Branch
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 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
//
// 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.
//
// WindowSurfaceCGL.cpp: CGL implementation of egl::Surface for windows
#ifdef UNSAFE_BUFFERS_BUILD
# pragma allow_unsafe_buffers
#endif
#import "libANGLE/renderer/gl/cgl/WindowSurfaceCGL.h"
#import <Cocoa/Cocoa.h>
#import <OpenGL/OpenGL.h>
#import <QuartzCore/QuartzCore.h>
#import "common/debug.h"
#import "common/gl/cgl/FunctionsCGL.h"
#import "libANGLE/Context.h"
#import "libANGLE/renderer/gl/FramebufferGL.h"
#import "libANGLE/renderer/gl/RendererGL.h"
#import "libANGLE/renderer/gl/StateManagerGL.h"
#import "libANGLE/renderer/gl/cgl/DisplayCGL.h"
#ifdef ANGLE_OUTSIDE_WEBKIT
# define SWAP_CGL_LAYER_NAME ANGLESwapCGLLayer
#else
# define SWAP_CGL_LAYER_NAME WebSwapCGLLayer
#endif
@interface SWAP_CGL_LAYER_NAME : CAOpenGLLayer {
CGLContextObj mDisplayContext;
bool initialized;
rx::SharedSwapState *mSwapState;
const rx::FunctionsGL *mFunctions;
GLuint mReadFramebuffer;
}
- (id)initWithSharedState:(rx::SharedSwapState *)swapState
withContext:(CGLContextObj)displayContext
withFunctions:(const rx::FunctionsGL *)functions;
@end
@implementation SWAP_CGL_LAYER_NAME
- (id)initWithSharedState:(rx::SharedSwapState *)swapState
withContext:(CGLContextObj)displayContext
withFunctions:(const rx::FunctionsGL *)functions
{
self = [super init];
if (self != nil)
{
self.asynchronous = YES;
mDisplayContext = displayContext;
initialized = false;
mSwapState = swapState;
mFunctions = functions;
[self setFrame:CGRectMake(0, 0, mSwapState->textures[0].width,
mSwapState->textures[0].height)];
}
return self;
}
- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask
{
CGLPixelFormatAttribute attribs[] = {
kCGLPFADisplayMask, static_cast<CGLPixelFormatAttribute>(mask), kCGLPFAOpenGLProfile,
static_cast<CGLPixelFormatAttribute>(kCGLOGLPVersion_3_2_Core),
static_cast<CGLPixelFormatAttribute>(0)};
CGLPixelFormatObj pixelFormat = nullptr;
GLint numFormats = 0;
CGLChoosePixelFormat(attribs, &pixelFormat, &numFormats);
return pixelFormat;
}
- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
{
CGLContextObj context = nullptr;
CGLCreateContext(pixelFormat, mDisplayContext, &context);
return context;
}
- (BOOL)canDrawInCGLContext:(CGLContextObj)glContext
pixelFormat:(CGLPixelFormatObj)pixelFormat
forLayerTime:(CFTimeInterval)timeInterval
displayTime:(const CVTimeStamp *)timeStamp
{
BOOL result = NO;
pthread_mutex_lock(&mSwapState->mutex);
{
if (mSwapState->lastRendered->swapId > mSwapState->beingPresented->swapId)
{
std::swap(mSwapState->lastRendered, mSwapState->beingPresented);
result = YES;
}
}
pthread_mutex_unlock(&mSwapState->mutex);
return result;
}
- (void)drawInCGLContext:(CGLContextObj)glContext
pixelFormat:(CGLPixelFormatObj)pixelFormat
forLayerTime:(CFTimeInterval)timeInterval
displayTime:(const CVTimeStamp *)timeStamp
{
CGLSetCurrentContext(glContext);
if (!initialized)
{
initialized = true;
mFunctions->genFramebuffers(1, &mReadFramebuffer);
}
const auto &texture = *mSwapState->beingPresented;
if ([self frame].size.width != texture.width || [self frame].size.height != texture.height)
{
[self setFrame:CGRectMake(0, 0, texture.width, texture.height)];
// Without this, the OSX compositor / window system doesn't see the resize.
[self setNeedsDisplay];
}
// TODO(cwallez) support 2.1 contexts too that don't have blitFramebuffer nor the
// GL_DRAW_FRAMEBUFFER_BINDING query
GLint drawFBO;
mFunctions->getIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &drawFBO);
mFunctions->bindFramebuffer(GL_FRAMEBUFFER, mReadFramebuffer);
mFunctions->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
texture.texture, 0);
mFunctions->bindFramebuffer(GL_READ_FRAMEBUFFER, mReadFramebuffer);
mFunctions->bindFramebuffer(GL_DRAW_FRAMEBUFFER, drawFBO);
mFunctions->blitFramebuffer(0, 0, texture.width, texture.height, 0, 0, texture.width,
texture.height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
// Call the super method to flush the context
[super drawInCGLContext:glContext
pixelFormat:pixelFormat
forLayerTime:timeInterval
displayTime:timeStamp];
}
@end
namespace rx
{
WindowSurfaceCGL::WindowSurfaceCGL(const egl::SurfaceState &state,
RendererGL *renderer,
EGLNativeWindowType layer,
CGLContextObj context)
: SurfaceGL(state),
mSwapLayer(nil),
mCurrentSwapId(0),
mLayer((__bridge CALayer *)layer),
mContext(context),
mFunctions(renderer->getFunctions()),
mStateManager(renderer->getStateManager()),
mDSRenderbuffer(0),
mFramebufferID(0)
{
pthread_mutex_init(&mSwapState.mutex, nullptr);
}
WindowSurfaceCGL::~WindowSurfaceCGL()
{
EnsureCGLContextIsCurrent ensureContextCurrent(mContext);
pthread_mutex_destroy(&mSwapState.mutex);
if (mFramebufferID != 0)
{
mStateManager->deleteFramebuffer(mFramebufferID);
mFramebufferID = 0;
}
if (mDSRenderbuffer != 0)
{
mStateManager->deleteRenderbuffer(mDSRenderbuffer);
mDSRenderbuffer = 0;
}
if (mSwapLayer != nil)
{
[mSwapLayer removeFromSuperlayer];
[mSwapLayer release];
mSwapLayer = nil;
}
for (size_t i = 0; i < ArraySize(mSwapState.textures); ++i)
{
if (mSwapState.textures[i].texture != 0)
{
mStateManager->deleteTexture(mSwapState.textures[i].texture);
mSwapState.textures[i].texture = 0;
}
}
}
egl::Error WindowSurfaceCGL::initialize(const egl::Display *display)
{
EnsureCGLContextIsCurrent ensureContextCurrent(mContext);
gl::Extents size = getSize();
unsigned width = size.width;
unsigned height = size.height;
for (size_t i = 0; i < ArraySize(mSwapState.textures); ++i)
{
mFunctions->genTextures(1, &mSwapState.textures[i].texture);
mStateManager->bindTexture(gl::TextureType::_2D, mSwapState.textures[i].texture);
mFunctions->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, nullptr);
mSwapState.textures[i].width = width;
mSwapState.textures[i].height = height;
mSwapState.textures[i].swapId = 0;
}
mSwapState.beingRendered = &mSwapState.textures[0];
mSwapState.lastRendered = &mSwapState.textures[1];
mSwapState.beingPresented = &mSwapState.textures[2];
mSwapLayer = [[SWAP_CGL_LAYER_NAME alloc] initWithSharedState:&mSwapState
withContext:mContext
withFunctions:mFunctions];
[mLayer addSublayer:mSwapLayer];
[mSwapLayer setContentsScale:[mLayer contentsScale]];
mFunctions->genRenderbuffers(1, &mDSRenderbuffer);
mStateManager->bindRenderbuffer(GL_RENDERBUFFER, mDSRenderbuffer);
mFunctions->renderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
return egl::Error(EGL_SUCCESS);
}
egl::Error WindowSurfaceCGL::makeCurrent(const gl::Context *context)
{
return egl::Error(EGL_SUCCESS);
}
egl::Error WindowSurfaceCGL::swap(const gl::Context *context, SurfaceSwapFeedback *feedback)
{
const FunctionsGL *functions = GetFunctionsGL(context);
StateManagerGL *stateManager = GetStateManagerGL(context);
functions->flush();
mSwapState.beingRendered->swapId = ++mCurrentSwapId;
pthread_mutex_lock(&mSwapState.mutex);
{
std::swap(mSwapState.beingRendered, mSwapState.lastRendered);
}
pthread_mutex_unlock(&mSwapState.mutex);
gl::Extents size = getSize();
unsigned width = size.width;
unsigned height = size.height;
auto &texture = *mSwapState.beingRendered;
if (texture.width != width || texture.height != height)
{
stateManager->bindTexture(gl::TextureType::_2D, texture.texture);
functions->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, nullptr);
stateManager->bindRenderbuffer(GL_RENDERBUFFER, mDSRenderbuffer);
functions->renderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
texture.width = width;
texture.height = height;
}
ASSERT(mFramebufferID ==
GetImplAs<FramebufferGL>(context->getFramebuffer({0}))->getFramebufferID());
stateManager->bindFramebuffer(GL_FRAMEBUFFER, mFramebufferID);
functions->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
mSwapState.beingRendered->texture, 0);
return egl::Error(EGL_SUCCESS);
}
egl::Error WindowSurfaceCGL::postSubBuffer(const gl::Context *context,
EGLint x,
EGLint y,
EGLint width,
EGLint height)
{
UNIMPLEMENTED();
return egl::Error(EGL_SUCCESS);
}
egl::Error WindowSurfaceCGL::querySurfacePointerANGLE(EGLint attribute, void **value)
{
UNIMPLEMENTED();
return egl::Error(EGL_SUCCESS);
}
egl::Error WindowSurfaceCGL::bindTexImage(const gl::Context *context,
gl::Texture *texture,
EGLint buffer)
{
UNIMPLEMENTED();
return egl::Error(EGL_SUCCESS);
}
egl::Error WindowSurfaceCGL::releaseTexImage(const gl::Context *context, EGLint buffer)
{
UNIMPLEMENTED();
return egl::Error(EGL_SUCCESS);
}
void WindowSurfaceCGL::setSwapInterval(const egl::Display *display, EGLint interval)
{
// TODO(cwallez) investigate implementing swap intervals other than 0
}
gl::Extents WindowSurfaceCGL::getSize() const
{
EGLint width, height;
egl::Error error = WindowSurfaceCGL::getUserSize(nullptr, &width, &height);
ASSERT(!error.isError());
return gl::Extents(width, height, 1);
}
egl::Error WindowSurfaceCGL::getUserSize(const egl::Display *display,
EGLint *width,
EGLint *height) const
{
CGRect frame = [mLayer frame];
CGFloat scale = [mLayer contentsScale];
if (width != nullptr)
{
*width = static_cast<EGLint>(CGRectGetWidth(frame) * scale);
}
if (height != nullptr)
{
*height = static_cast<EGLint>(CGRectGetHeight(frame) * scale);
}
return egl::NoError();
}
EGLint WindowSurfaceCGL::isPostSubBufferSupported() const
{
UNIMPLEMENTED();
return EGL_FALSE;
}
EGLint WindowSurfaceCGL::getSwapBehavior() const
{
return EGL_BUFFER_DESTROYED;
}
egl::Error WindowSurfaceCGL::attachToFramebuffer(const gl::Context *context,
gl::Framebuffer *framebuffer)
{
FramebufferGL *framebufferGL = GetImplAs<FramebufferGL>(framebuffer);
ASSERT(framebufferGL->getFramebufferID() == 0);
if (mFramebufferID == 0)
{
GLuint framebufferID = 0;
mFunctions->genFramebuffers(1, &framebufferID);
mStateManager->bindFramebuffer(GL_FRAMEBUFFER, framebufferID);
mFunctions->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
mSwapState.beingRendered->texture, 0);
mFunctions->framebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, mDSRenderbuffer);
mFramebufferID = framebufferID;
}
framebufferGL->setFramebufferID(mFramebufferID);
return egl::NoError();
}
egl::Error WindowSurfaceCGL::detachFromFramebuffer(const gl::Context *context,
gl::Framebuffer *framebuffer)
{
FramebufferGL *framebufferGL = GetImplAs<FramebufferGL>(framebuffer);
ASSERT(framebufferGL->getFramebufferID() == mFramebufferID);
framebufferGL->setFramebufferID(0);
return egl::NoError();
}
} // namespace rx