Hash :
cfb430c8
Author :
Date :
2025-02-10T13:19:05
Remove angle::ErrorStream helper Most uses of the helper either use just the code or a fixed string, which compiles to a few instructions. Using this helper adds 200+ bytes of assembly to each use, due to the unneeded instantiation of ostringstream which allocates a buffer etc. The combined effect of this CL on an Android perf build is ~12KB (0.2%) reduction in size. The cases where the message is actually formatted are converted to an explicit use of ostringstream. Removing the helper so that the new code is explicit about the intent to use ostringstream, or an alternative way to format the message. Discovered accidentally while looking into size reduction due to __builtin_unreachable() Semi-automated code change, risk of copy-paste mistakes should be minimal. Bug: angleproject:394129077 Change-Id: I47c2642d750d31416b08a1cfa435d5463c294e35 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6250078 Commit-Queue: Roman Lavrov <romanl@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Charlie Lao <cclao@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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
//
// Copyright 2021 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.
//
// ImageMtl.cpp:
// Implements the class methods for ImageMtl.
//
#include "libANGLE/renderer/metal/ImageMtl.h"
#include "common/debug.h"
#include "libANGLE/Context.h"
#include "libANGLE/Display.h"
#include "libANGLE/renderer/metal/ContextMtl.h"
#include "libANGLE/renderer/metal/DisplayMtl.h"
#include "libANGLE/renderer/metal/RenderBufferMtl.h"
#include "libANGLE/renderer/metal/TextureMtl.h"
namespace rx
{
namespace
{
angle::FormatID intendedFormatForMTLTexture(id<MTLTexture> texture,
const egl::AttributeMap &attribs)
{
angle::FormatID angleFormatId = mtl::Format::MetalToAngleFormatID(texture.pixelFormat);
if (angleFormatId == angle::FormatID::NONE)
{
return angle::FormatID::NONE;
}
const angle::Format *textureAngleFormat = &angle::Format::Get(angleFormatId);
ASSERT(textureAngleFormat);
GLenum sizedInternalFormat = textureAngleFormat->glInternalFormat;
if (attribs.contains(EGL_TEXTURE_INTERNAL_FORMAT_ANGLE))
{
const GLenum internalFormat =
static_cast<GLenum>(attribs.get(EGL_TEXTURE_INTERNAL_FORMAT_ANGLE));
GLenum type = gl::GetSizedInternalFormatInfo(sizedInternalFormat).type;
const auto format = gl::Format(internalFormat, type);
if (!format.valid())
{
return angle::FormatID::NONE;
}
sizedInternalFormat = format.info->sizedInternalFormat;
}
return angle::Format::InternalFormatToID(sizedInternalFormat);
}
} // anonymous namespace
// TextureImageSiblingMtl implementation
TextureImageSiblingMtl::TextureImageSiblingMtl(EGLClientBuffer buffer,
const egl::AttributeMap &attribs)
: mBuffer(buffer), mAttribs(attribs), mGLFormat(GL_NONE)
{}
TextureImageSiblingMtl::~TextureImageSiblingMtl() {}
// Static
egl::Error TextureImageSiblingMtl::ValidateClientBuffer(const DisplayMtl *display,
EGLClientBuffer buffer,
const egl::AttributeMap &attribs)
{
id<MTLTexture> texture = (__bridge id<MTLTexture>)(buffer);
if (!texture || texture.device != display->getMetalDevice())
{
return egl::Error(EGL_BAD_ATTRIBUTE);
}
if (texture.textureType != MTLTextureType2D && texture.textureType != MTLTextureTypeCube &&
texture.textureType != MTLTextureType2DArray)
{
return egl::Error(EGL_BAD_ATTRIBUTE);
}
angle::FormatID angleFormatId = intendedFormatForMTLTexture(texture, attribs);
const mtl::Format &format = display->getPixelFormat(angleFormatId);
if (!format.valid())
{
return egl::Error(EGL_BAD_ATTRIBUTE, "Unrecognized format");
}
if (format.metalFormat != texture.pixelFormat)
{
return egl::Error(EGL_BAD_ATTRIBUTE, "Incompatible format");
}
unsigned textureArraySlice =
static_cast<unsigned>(attribs.getAsInt(EGL_METAL_TEXTURE_ARRAY_SLICE_ANGLE, 0));
if (texture.textureType != MTLTextureType2DArray && textureArraySlice > 0)
{
return egl::Error(EGL_BAD_ATTRIBUTE,
"Invalid texture type for non-zero texture array slice");
}
if (textureArraySlice >= texture.arrayLength)
{
std::ostringstream err;
err << "Invalid texture array slice: " << textureArraySlice;
return egl::Error(EGL_BAD_ATTRIBUTE, err.str());
}
return egl::NoError();
}
egl::Error TextureImageSiblingMtl::initialize(const egl::Display *display)
{
DisplayMtl *displayMtl = mtl::GetImpl(display);
if (initImpl(displayMtl) != angle::Result::Continue)
{
return egl::Error(EGL_BAD_PARAMETER);
}
return egl::NoError();
}
angle::Result TextureImageSiblingMtl::initImpl(DisplayMtl *displayMtl)
{
mNativeTexture = mtl::Texture::MakeFromMetal((__bridge id<MTLTexture>)(mBuffer));
if (mNativeTexture->textureType() == MTLTextureType2DArray)
{
mtl::TextureRef baseTexture = std::move(mNativeTexture);
unsigned textureArraySlice =
static_cast<unsigned>(mAttribs.getAsInt(EGL_METAL_TEXTURE_ARRAY_SLICE_ANGLE, 0));
mNativeTexture =
baseTexture->createSliceMipView(textureArraySlice, mtl::kZeroNativeMipLevel);
}
angle::FormatID angleFormatId = intendedFormatForMTLTexture(mNativeTexture->get(), mAttribs);
mFormat = displayMtl->getPixelFormat(angleFormatId);
if (mNativeTexture)
{
size_t resourceSize = EstimateTextureSizeInBytes(
mFormat, mNativeTexture->widthAt0(), mNativeTexture->heightAt0(),
mNativeTexture->depthAt0(), mNativeTexture->samples(), mNativeTexture->mipmapLevels());
mNativeTexture->setEstimatedByteSize(resourceSize);
}
mGLFormat = gl::Format(mFormat.intendedAngleFormat().glInternalFormat);
mRenderable = mFormat.getCaps().depthRenderable || mFormat.getCaps().colorRenderable;
// Some formats are not filterable but renderable such as integer formats. In this case, treat
// them as texturable as well.
mTextureable = mFormat.getCaps().filterable || mRenderable;
return angle::Result::Continue;
}
void TextureImageSiblingMtl::onDestroy(const egl::Display *display)
{
mNativeTexture = nullptr;
}
gl::Format TextureImageSiblingMtl::getFormat() const
{
return mGLFormat;
}
bool TextureImageSiblingMtl::isRenderable(const gl::Context *context) const
{
return mRenderable;
}
bool TextureImageSiblingMtl::isTexturable(const gl::Context *context) const
{
return mTextureable;
}
gl::Extents TextureImageSiblingMtl::getSize() const
{
return mNativeTexture ? mNativeTexture->sizeAt0() : gl::Extents(0, 0, 0);
}
size_t TextureImageSiblingMtl::getSamples() const
{
uint32_t samples = mNativeTexture ? mNativeTexture->samples() : 0;
return samples > 1 ? samples : 0;
}
bool TextureImageSiblingMtl::isYUV() const
{
// NOTE(hqle): not supporting YUV image yet.
return false;
}
bool TextureImageSiblingMtl::hasProtectedContent() const
{
return false;
}
// ImageMtl implementation
ImageMtl::ImageMtl(const egl::ImageState &state, const gl::Context *context) : ImageImpl(state) {}
ImageMtl::~ImageMtl() {}
void ImageMtl::onDestroy(const egl::Display *display)
{
mNativeTexture = nullptr;
}
egl::Error ImageMtl::initialize(const egl::Display *display)
{
if (mState.target == EGL_METAL_TEXTURE_ANGLE)
{
const TextureImageSiblingMtl *externalImageSibling =
GetImplAs<TextureImageSiblingMtl>(GetAs<egl::ExternalImageSibling>(mState.source));
mNativeTexture = externalImageSibling->getTexture();
switch (mNativeTexture->textureType())
{
case MTLTextureType2D:
case MTLTextureType2DArray:
mImageTextureType = gl::TextureType::_2D;
break;
case MTLTextureTypeCube:
mImageTextureType = gl::TextureType::CubeMap;
break;
default:
UNREACHABLE();
}
mImageLevel = 0;
mImageLayer = 0;
}
else
{
UNREACHABLE();
return egl::Error(EGL_BAD_ACCESS);
}
return egl::NoError();
}
angle::Result ImageMtl::orphan(const gl::Context *context, egl::ImageSibling *sibling)
{
if (sibling == mState.source)
{
mNativeTexture = nullptr;
}
return angle::Result::Continue;
}
} // namespace rx