Hash :
f507fe05
Author :
Date :
2023-04-06T12:27:00
Rename PLATFORM_IOS to PLATFORM_IOS_FAMILY The current define ANGLE_PLATFORM_IOS actually means "iOS or tvOS or WatchOS or MacCatalyst". The current define ANGLE_PLATFORM_WATCHOS means nothing. The current define ANGLE_PLATFORM_APPLETV means nothing. Replace PLATFORM_IOS and its uses with PLATFORM_IOS_FAMILY, so that then PLATFORM_IOS can be reintroduced and others can be fixed. Replace PLATFORM_IOS_SIMULATOR and its uses with PLATFORM_IOS_FAMILY_SIMULATOR for consistency. Use consistent `#if X` notation instead of `#if defined(X)`. Bug: angleproject:8121 Change-Id: Ibe668c2ae9bb801d15e036fcf1dfd53f22c30787 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4404161 Reviewed-by: Dan Glastonbury <djg@apple.com> Reviewed-by: Kenneth Russell <kbr@chromium.org> Commit-Queue: Kimmo Kinnunen <kkinnunen@apple.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 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
//
// Copyright 2020 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.
//
// IOSurfaceSurfaceEAGL.mm: an implementation of PBuffers created from IOSurfaces using
// EGL_ANGLE_iosurface_client_buffer
#import "libANGLE/renderer/gl/eagl/IOSurfaceSurfaceEAGL.h"
#import <OpenGLES/EAGL.h>
#import <OpenGLES/EAGLDrawable.h>
#import <OpenGLES/EAGLIOSurface.h>
#import "common/debug.h"
#import "common/platform.h"
#import "libANGLE/AttributeMap.h"
#import "libANGLE/renderer/gl/BlitGL.h"
#import "libANGLE/renderer/gl/FramebufferGL.h"
#import "libANGLE/renderer/gl/FunctionsGL.h"
#import "libANGLE/renderer/gl/RendererGL.h"
#import "libANGLE/renderer/gl/StateManagerGL.h"
#import "libANGLE/renderer/gl/TextureGL.h"
#import "libANGLE/renderer/gl/eagl/DisplayEAGL.h"
namespace rx
{
namespace
{
struct IOSurfaceFormatInfo
{
GLenum internalFormat;
GLenum type;
GLenum nativeInternalFormat;
GLenum nativeFormat;
GLenum nativeType;
};
// clang-format off
static const IOSurfaceFormatInfo kIOSurfaceFormats[] = {
{GL_RED, GL_UNSIGNED_BYTE, GL_R8, GL_RED, GL_UNSIGNED_BYTE },
{GL_R16UI, GL_UNSIGNED_SHORT, GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT},
{GL_RG, GL_UNSIGNED_BYTE, GL_RG8, GL_RG, GL_UNSIGNED_BYTE },
{GL_RGB, GL_UNSIGNED_BYTE, GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE },
{GL_BGRA_EXT, GL_UNSIGNED_BYTE, GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE },
{GL_RGBA, GL_HALF_FLOAT, GL_RGBA, GL_RGBA, GL_HALF_FLOAT },
};
// clang-format on
int FindIOSurfaceFormatIndex(GLenum internalFormat, GLenum type)
{
for (int i = 0; i < static_cast<int>(ArraySize(kIOSurfaceFormats)); ++i)
{
const auto &formatInfo = kIOSurfaceFormats[i];
if (formatInfo.internalFormat == internalFormat && formatInfo.type == type)
{
return i;
}
}
return -1;
}
} // anonymous namespace
IOSurfaceSurfaceEAGL::IOSurfaceSurfaceEAGL(const egl::SurfaceState &state,
RendererGL *renderer,
EAGLContextObj cglContext,
EGLClientBuffer buffer,
const egl::AttributeMap &attribs)
: SurfaceGL(state),
mFunctions(renderer->getFunctions()),
mStateManager(renderer->getStateManager()),
mEAGLContext(cglContext),
mIOSurface(nullptr),
mFramebufferID(0),
mTextureID(0),
mWidth(0),
mHeight(0),
mPlane(0),
mFormatIndex(-1),
mRowStrideInPixels(0),
mAlphaInitialized(false)
{
// Keep reference to the IOSurface so it doesn't get deleted while the pbuffer exists.
mIOSurface = reinterpret_cast<IOSurfaceRef>(buffer);
CFRetain(mIOSurface);
// Extract attribs useful for the call to EAGLTexImageIOSurface2D
mWidth = static_cast<int>(attribs.get(EGL_WIDTH));
mHeight = static_cast<int>(attribs.get(EGL_HEIGHT));
mPlane = static_cast<int>(attribs.get(EGL_IOSURFACE_PLANE_ANGLE));
// Hopefully the number of bytes per row is always an integer number of pixels. We use
// glReadPixels to fill the IOSurface in the simulator and it can only support strides that are
// an integer number of pixels.
ASSERT(IOSurfaceGetBytesPerRowOfPlane(mIOSurface, mPlane) %
IOSurfaceGetBytesPerElementOfPlane(mIOSurface, mPlane) ==
0);
mRowStrideInPixels = static_cast<int>(IOSurfaceGetBytesPerRowOfPlane(mIOSurface, mPlane) /
IOSurfaceGetBytesPerElementOfPlane(mIOSurface, mPlane));
EGLAttrib internalFormat = attribs.get(EGL_TEXTURE_INTERNAL_FORMAT_ANGLE);
EGLAttrib type = attribs.get(EGL_TEXTURE_TYPE_ANGLE);
mFormatIndex =
FindIOSurfaceFormatIndex(static_cast<GLenum>(internalFormat), static_cast<GLenum>(type));
ASSERT(mFormatIndex >= 0);
mAlphaInitialized = !hasEmulatedAlphaChannel();
#if ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR
ANGLE_UNUSED_VARIABLE(mEAGLContext);
mBoundTextureID = 0;
EGLAttrib usageHint =
attribs.get(EGL_IOSURFACE_USAGE_HINT_ANGLE,
EGL_IOSURFACE_READ_HINT_ANGLE | EGL_IOSURFACE_WRITE_HINT_ANGLE);
mUploadFromIOSurface = ((usageHint & EGL_IOSURFACE_READ_HINT_ANGLE) != 0);
mReadbackToIOSurface = ((usageHint & EGL_IOSURFACE_WRITE_HINT_ANGLE) != 0);
#endif
}
IOSurfaceSurfaceEAGL::~IOSurfaceSurfaceEAGL()
{
if (!mFramebufferID)
{
mStateManager->deleteFramebuffer(mFramebufferID);
mFramebufferID = 0;
mStateManager->deleteTexture(mTextureID);
mTextureID = 0;
}
if (mIOSurface != nullptr)
{
CFRelease(mIOSurface);
mIOSurface = nullptr;
}
}
egl::Error IOSurfaceSurfaceEAGL::initialize(const egl::Display *display)
{
return egl::NoError();
}
egl::Error IOSurfaceSurfaceEAGL::makeCurrent(const gl::Context *context)
{
return egl::NoError();
}
egl::Error IOSurfaceSurfaceEAGL::unMakeCurrent(const gl::Context *context)
{
GetFunctionsGL(context)->flush();
return egl::NoError();
}
egl::Error IOSurfaceSurfaceEAGL::swap(const gl::Context *context)
{
return egl::NoError();
}
egl::Error IOSurfaceSurfaceEAGL::postSubBuffer(const gl::Context *context,
EGLint x,
EGLint y,
EGLint width,
EGLint height)
{
UNREACHABLE();
return egl::NoError();
}
egl::Error IOSurfaceSurfaceEAGL::querySurfacePointerANGLE(EGLint attribute, void **value)
{
UNREACHABLE();
return egl::NoError();
}
egl::Error IOSurfaceSurfaceEAGL::bindTexImage(const gl::Context *context,
gl::Texture *texture,
EGLint)
{
StateManagerGL *stateManager = GetStateManagerGL(context);
const TextureGL *textureGL = GetImplAs<TextureGL>(texture);
GLuint textureID = textureGL->getTextureID();
stateManager->bindTexture(gl::TextureType::_2D, textureID);
const auto &format = kIOSurfaceFormats[mFormatIndex];
#if !ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR
if (![mEAGLContext texImageIOSurface:mIOSurface
target:GL_TEXTURE_2D
internalFormat:format.nativeInternalFormat
width:mWidth
height:mHeight
format:format.nativeFormat
type:format.nativeType
plane:mPlane])
{
return egl::EglContextLost() << "EAGLContext texImageIOSurface2D failed.";
}
if (IsError(initializeAlphaChannel(context, textureID)))
{
return egl::EglContextLost() << "Failed to initialize IOSurface alpha channel.";
}
#else // !ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR
const FunctionsGL *functions = GetFunctionsGL(context);
IOSurfaceLock(mIOSurface, getIOSurfaceLockOptions(), nullptr);
void *textureData = nullptr;
if (mUploadFromIOSurface)
{
// TODO(kbr): possibly more state to be set here, including setting any
// pixel unpack buffer to 0 when using ES 3.0 contexts.
gl::PixelUnpackState defaultUnpackState;
if (IsError(stateManager->setPixelUnpackState(context, defaultUnpackState)))
{
return egl::EglBadState() << "Failed to set pixel unpack state.";
}
textureData = IOSurfaceGetBaseAddress(mIOSurface);
}
// TODO(kbr): consider trying to optimize away texture reallocations by
// keeping track of which textures have already been allocated.
functions->texImage2D(GL_TEXTURE_2D, 0, format.nativeInternalFormat, mWidth, mHeight, 0,
format.nativeFormat, format.nativeType, textureData);
mBoundTextureID = textureID;
#endif // !ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR
return egl::NoError();
}
egl::Error IOSurfaceSurfaceEAGL::releaseTexImage(const gl::Context *context, EGLint buffer)
{
const FunctionsGL *functions = GetFunctionsGL(context);
#if !ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR
functions->flush();
#else // !ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR
if (mReadbackToIOSurface)
{
StateManagerGL *stateManager = GetStateManagerGL(context);
GLuint tempFBO = 0;
functions->genFramebuffers(1, &tempFBO);
stateManager->bindFramebuffer(GL_FRAMEBUFFER, tempFBO);
functions->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
mBoundTextureID, 0);
gl::PixelPackState state;
state.rowLength = mRowStrideInPixels;
state.alignment = 1;
if (IsError(stateManager->setPixelPackState(context, state)))
{
return egl::EglBadState() << "Failed to set pixel pack state.";
}
// TODO(kbr): possibly more state to be set here, including setting any
// pixel pack buffer to 0 when using ES 3.0 contexts.
const auto &format = kIOSurfaceFormats[mFormatIndex];
functions->readPixels(0, 0, mWidth, mHeight, format.nativeFormat, format.nativeType,
IOSurfaceGetBaseAddress(mIOSurface));
}
IOSurfaceUnlock(mIOSurface, getIOSurfaceLockOptions(), nullptr);
#endif // !ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR
return egl::NoError();
}
void IOSurfaceSurfaceEAGL::setSwapInterval(EGLint interval)
{
UNREACHABLE();
}
EGLint IOSurfaceSurfaceEAGL::getWidth() const
{
return mWidth;
}
EGLint IOSurfaceSurfaceEAGL::getHeight() const
{
return mHeight;
}
EGLint IOSurfaceSurfaceEAGL::isPostSubBufferSupported() const
{
UNREACHABLE();
return EGL_FALSE;
}
EGLint IOSurfaceSurfaceEAGL::getSwapBehavior() const
{
// N/A because you can't MakeCurrent an IOSurface, return any valid value.
return EGL_BUFFER_PRESERVED;
}
// static
bool IOSurfaceSurfaceEAGL::validateAttributes(EGLClientBuffer buffer,
const egl::AttributeMap &attribs)
{
IOSurfaceRef ioSurface = reinterpret_cast<IOSurfaceRef>(buffer);
// The plane must exist for this IOSurface. IOSurfaceGetPlaneCount can return 0 for non-planar
// ioSurfaces but we will treat non-planar like it is a single plane.
size_t surfacePlaneCount = std::max(size_t(1), IOSurfaceGetPlaneCount(ioSurface));
EGLAttrib plane = attribs.get(EGL_IOSURFACE_PLANE_ANGLE);
if (plane < 0 || static_cast<size_t>(plane) >= surfacePlaneCount)
{
return false;
}
// The width height specified must be at least (1, 1) and at most the plane size
EGLAttrib width = attribs.get(EGL_WIDTH);
EGLAttrib height = attribs.get(EGL_HEIGHT);
if (width <= 0 || static_cast<size_t>(width) > IOSurfaceGetWidthOfPlane(ioSurface, plane) ||
height <= 0 || static_cast<size_t>(height) > IOSurfaceGetHeightOfPlane(ioSurface, plane))
{
return false;
}
// Find this IOSurface format
EGLAttrib internalFormat = attribs.get(EGL_TEXTURE_INTERNAL_FORMAT_ANGLE);
EGLAttrib type = attribs.get(EGL_TEXTURE_TYPE_ANGLE);
int formatIndex =
FindIOSurfaceFormatIndex(static_cast<GLenum>(internalFormat), static_cast<GLenum>(type));
if (formatIndex < 0)
{
return false;
}
// FIXME: Check that the format matches this IOSurface plane for pixel formats that we know of.
// We could map IOSurfaceGetPixelFormat to expected type plane and format type.
// However, the caller might supply us non-public pixel format, which makes exhaustive checks
// problematic.
return true;
}
angle::Result IOSurfaceSurfaceEAGL::initializeAlphaChannel(const gl::Context *context,
GLuint texture)
{
if (mAlphaInitialized)
{
return angle::Result::Continue;
}
BlitGL *blitter = GetBlitGL(context);
ANGLE_TRY(blitter->clearRenderableTextureAlphaToOne(context, texture,
gl::TextureTarget::Rectangle, 0));
mAlphaInitialized = true;
return angle::Result::Continue;
}
bool IOSurfaceSurfaceEAGL::hasEmulatedAlphaChannel() const
{
const auto &format = kIOSurfaceFormats[mFormatIndex];
return format.internalFormat == GL_RGB;
}
egl::Error IOSurfaceSurfaceEAGL::attachToFramebuffer(const gl::Context *context,
gl::Framebuffer *framebuffer)
{
FramebufferGL *framebufferGL = GetImplAs<FramebufferGL>(framebuffer);
ASSERT(framebufferGL->getFramebufferID() == 0);
if (mFramebufferID == 0)
{
GLuint textureID = 0;
mFunctions->genTextures(1, &textureID);
mStateManager->bindTexture(gl::TextureType::_2D, textureID);
#if !ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR
const auto &format = kIOSurfaceFormats[mFormatIndex];
if (![mEAGLContext texImageIOSurface:mIOSurface
target:GL_TEXTURE_2D
internalFormat:format.nativeInternalFormat
width:mWidth
height:mHeight
format:format.nativeFormat
type:format.nativeType
plane:mPlane])
{
return egl::EglContextLost() << "[EAGLContext texImageIOSurface] failed";
}
#else // !ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR
ERR() << "IOSurfaces with OpenGL ES not supported on iOS Simulator";
#endif // !ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR
if (IsError(initializeAlphaChannel(context, textureID)))
{
return egl::EglContextLost() << "Failed to initialize IOSurface alpha channel.";
}
GLuint framebufferID = 0;
mFunctions->genFramebuffers(1, &framebufferID);
mStateManager->bindFramebuffer(GL_FRAMEBUFFER, framebufferID);
mStateManager->bindTexture(gl::TextureType::_2D, textureID);
mFunctions->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
textureID, 0);
mFramebufferID = framebufferID;
mTextureID = textureID;
}
framebufferGL->setFramebufferID(mFramebufferID);
framebufferGL->setHasEmulatedAlphaAttachment(hasEmulatedAlphaChannel());
return egl::NoError();
}
egl::Error IOSurfaceSurfaceEAGL::detachFromFramebuffer(const gl::Context *context,
gl::Framebuffer *framebuffer)
{
FramebufferGL *framebufferGL = GetImplAs<FramebufferGL>(framebuffer);
ASSERT(framebufferGL->getFramebufferID() == mFramebufferID);
framebufferGL->setFramebufferID(0);
return egl::NoError();
}
#if ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR
IOSurfaceLockOptions IOSurfaceSurfaceEAGL::getIOSurfaceLockOptions() const
{
IOSurfaceLockOptions options = 0;
if (!mReadbackToIOSurface)
{
options |= kIOSurfaceLockReadOnly;
}
return options;
}
#endif // ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR
} // namespace rx