Hash :
0690f5d3
Author :
Date :
2022-04-21T21:58:49
Revert "CGL, MTL: pbuffer for IOSurface fails for some formats" This reverts commit 5b84ad7973a3019b66848aabb2d2eef27c094545. Reason for revert: Breaks the build on the bots (see below) Example AutoRoll CL that has compilation pre-submit error: https://chromium-review.googlesource.com/c/chromium/src/+/3598558 Example bot results, showing compilation error: https://ci.chromium.org/ui/p/chromium/builders/try/ios-simulator/1143059/overview Original change's description: > CGL, MTL: pbuffer for IOSurface fails for some formats > > Some IOSurface SPI formats are compressed, and getting the > element size for those returns values that are not consistent > with the validation. Disable the validation > until a better detection of such formats are known. > > A workaround exists for EAGL. > Apply similar workaround for CGL and Metal. > > This hunk is in downtstream WebKit ANGLE and having it upstream would > help merging back and forth. > > Bug: angleproject:7175 > Change-Id: Ic97afd3b952fed236e7b7e1e8511a1dde9008647 > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3568380 > Reviewed-by: Geoff Lang <geofflang@chromium.org> > Reviewed-by: Kenneth Russell <kbr@chromium.org> > Commit-Queue: Kenneth Russell <kbr@chromium.org> Bug: angleproject:7175 Change-Id: I0c18bdb800e39d6930455dbc86931681b6df20b1 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3600148 Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> Reviewed-by: Ian Elliott <ianelliott@google.com> Commit-Queue: Ian Elliott <ianelliott@google.com> Auto-Submit: Ian Elliott <ianelliott@google.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
//
// Copyright 2019 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.
//
// IOSurfaceSurfaceMtl.mm:
// Implements the class methods for IOSurfaceSurfaceMtl.
//
#include "libANGLE/renderer/metal/IOSurfaceSurfaceMtl.h"
#include <TargetConditionals.h>
#include "libANGLE/Display.h"
#include "libANGLE/Surface.h"
#include "libANGLE/renderer/metal/ContextMtl.h"
#include "libANGLE/renderer/metal/DisplayMtl.h"
#include "libANGLE/renderer/metal/FrameBufferMtl.h"
#include "libANGLE/renderer/metal/mtl_format_utils.h"
#include "libANGLE/renderer/metal/mtl_utils.h"
// Compiler can turn on programmatical frame capture in release build by defining
// ANGLE_METAL_FRAME_CAPTURE flag.
#if defined(NDEBUG) && !defined(ANGLE_METAL_FRAME_CAPTURE)
# define ANGLE_METAL_FRAME_CAPTURE_ENABLED 0
#else
# define ANGLE_METAL_FRAME_CAPTURE_ENABLED ANGLE_WITH_MODERN_METAL_API
#endif
namespace rx
{
namespace
{
struct IOSurfaceFormatInfo
{
GLenum internalFormat;
GLenum type;
size_t componentBytes;
angle::FormatID nativeAngleFormatId;
};
// clang-format off
// NOTE(hqle): Support R16_UINT once GLES3 is complete.
constexpr std::array<IOSurfaceFormatInfo, 8> kIOSurfaceFormats = {{
{GL_RED, GL_UNSIGNED_BYTE, 1, angle::FormatID::R8_UNORM},
{GL_RED, GL_UNSIGNED_SHORT, 2, angle::FormatID::R16_UNORM},
{GL_RG, GL_UNSIGNED_BYTE, 2, angle::FormatID::R8G8_UNORM},
{GL_RG, GL_UNSIGNED_SHORT, 4, angle::FormatID::R16G16_UNORM},
{GL_RGB, GL_UNSIGNED_BYTE, 4, angle::FormatID::B8G8R8A8_UNORM},
{GL_BGRA_EXT, GL_UNSIGNED_BYTE, 4, angle::FormatID::B8G8R8A8_UNORM},
{GL_RGBA, GL_HALF_FLOAT, 8, angle::FormatID::R16G16B16A16_FLOAT},
{GL_RGB10_A2, GL_UNSIGNED_INT_2_10_10_10_REV, 4, angle::FormatID::B10G10R10A2_UNORM},
}};
// clang-format on
int FindIOSurfaceFormatIndex(GLenum internalFormat, GLenum type)
{
for (int i = 0; i < static_cast<int>(kIOSurfaceFormats.size()); ++i)
{
const auto &formatInfo = kIOSurfaceFormats[i];
if (formatInfo.internalFormat == internalFormat && formatInfo.type == type)
{
return i;
}
}
return -1;
}
} // anonymous namespace
// IOSurfaceSurfaceMtl implementation.
IOSurfaceSurfaceMtl::IOSurfaceSurfaceMtl(DisplayMtl *display,
const egl::SurfaceState &state,
EGLClientBuffer buffer,
const egl::AttributeMap &attribs)
: OffscreenSurfaceMtl(display, state, attribs), mIOSurface((__bridge IOSurfaceRef)(buffer))
{
CFRetain(mIOSurface);
mIOSurfacePlane = static_cast<int>(attribs.get(EGL_IOSURFACE_PLANE_ANGLE));
EGLAttrib internalFormat = attribs.get(EGL_TEXTURE_INTERNAL_FORMAT_ANGLE);
EGLAttrib type = attribs.get(EGL_TEXTURE_TYPE_ANGLE);
mIOSurfaceFormatIdx =
FindIOSurfaceFormatIndex(static_cast<GLenum>(internalFormat), static_cast<GLenum>(type));
ASSERT(mIOSurfaceFormatIdx >= 0);
mColorFormat =
display->getPixelFormat(kIOSurfaceFormats[mIOSurfaceFormatIdx].nativeAngleFormatId);
}
IOSurfaceSurfaceMtl::~IOSurfaceSurfaceMtl()
{
if (mIOSurface != nullptr)
{
CFRelease(mIOSurface);
mIOSurface = nullptr;
}
}
egl::Error IOSurfaceSurfaceMtl::bindTexImage(const gl::Context *context,
gl::Texture *texture,
EGLint buffer)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
StartFrameCapture(contextMtl);
// Initialize offscreen texture if needed:
ANGLE_TO_EGL_TRY(ensureColorTextureCreated(context));
return OffscreenSurfaceMtl::bindTexImage(context, texture, buffer);
}
egl::Error IOSurfaceSurfaceMtl::releaseTexImage(const gl::Context *context, EGLint buffer)
{
egl::Error re = OffscreenSurfaceMtl::releaseTexImage(context, buffer);
StopFrameCapture();
return re;
}
angle::Result IOSurfaceSurfaceMtl::getAttachmentRenderTarget(
const gl::Context *context,
GLenum binding,
const gl::ImageIndex &imageIndex,
GLsizei samples,
FramebufferAttachmentRenderTarget **rtOut)
{
// Initialize offscreen texture if needed:
ANGLE_TRY(ensureColorTextureCreated(context));
return OffscreenSurfaceMtl::getAttachmentRenderTarget(context, binding, imageIndex, samples,
rtOut);
}
angle::Result IOSurfaceSurfaceMtl::ensureColorTextureCreated(const gl::Context *context)
{
if (mColorTexture)
{
return angle::Result::Continue;
}
ContextMtl *contextMtl = mtl::GetImpl(context);
ANGLE_MTL_OBJC_SCOPE
{
auto texDesc =
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:mColorFormat.metalFormat
width:mSize.width
height:mSize.height
mipmapped:NO];
texDesc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
mColorTexture =
mtl::Texture::MakeFromMetal(contextMtl->getMetalDevice().newTextureWithDescriptor(
texDesc, mIOSurface, mIOSurfacePlane));
if (mColorTexture)
{
size_t resourceSize = EstimateTextureSizeInBytes(
mColorFormat, mColorTexture->widthAt0(), mColorTexture->heightAt0(),
mColorTexture->depthAt0(), mColorTexture->samples(), mColorTexture->mipmapLevels());
mColorTexture->setEstimatedByteSize(resourceSize);
}
}
mColorRenderTarget.set(mColorTexture, mtl::kZeroNativeMipLevel, 0, mColorFormat);
if (kIOSurfaceFormats[mIOSurfaceFormatIdx].internalFormat == GL_RGB)
{
// This format has emulated alpha channel. Initialize texture's alpha channel to 1.0.
const mtl::Format &rgbClearFormat =
contextMtl->getPixelFormat(angle::FormatID::R8G8B8_UNORM);
ANGLE_TRY(mtl::InitializeTextureContentsGPU(
context, mColorTexture, rgbClearFormat,
mtl::ImageNativeIndex::FromBaseZeroGLIndex(gl::ImageIndex::Make2D(0)),
MTLColorWriteMaskAlpha));
// Disable subsequent rendering to alpha channel.
mColorTexture->setColorWritableMask(MTLColorWriteMaskAll & (~MTLColorWriteMaskAlpha));
}
return angle::Result::Continue;
}
// static
bool IOSurfaceSurfaceMtl::ValidateAttributes(EGLClientBuffer buffer,
const egl::AttributeMap &attribs)
{
IOSurfaceRef ioSurface = (__bridge 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;
}
// Check that the format matches this IOSurface plane
if (IOSurfaceGetBytesPerElementOfPlane(ioSurface, plane) !=
kIOSurfaceFormats[formatIndex].componentBytes)
{
return false;
}
return true;
}
}