Skip to content

Commit

Permalink
[TextureMapper] Pass size to BitmapTexture constructor
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=264157

Reviewed by Fujii Hironori.

This way we can create a BitmapTexture without having to call reset
right after the creation to set the size and flags (that are passed but
ignored in the constructor).

* Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp:
(WebCore::MediaPlayerPrivateGStreamer::pushTextureToCompositor):
* Source/WebCore/platform/graphics/nicosia/NicosiaImageBufferPipe.cpp:
(Nicosia::NicosiaImageBufferPipeSource::handle):
* Source/WebCore/platform/graphics/texmap/BitmapTexture.cpp:
(WebCore::BitmapTexture::BitmapTexture):
(WebCore::BitmapTexture::reset):
(WebCore::BitmapTexture::updateContents):
(WebCore::BitmapTexture::initializeStencil):
(WebCore::BitmapTexture::initializeDepthBuffer):
(WebCore::BitmapTexture::clearIfNeeded):
(WebCore::BitmapTexture::bindAsSurface):
(WebCore::BitmapTexture::~BitmapTexture):
(WebCore::BitmapTexture::copyFromExternalTexture):
(WebCore::BitmapTexture::didReset): Deleted.
(WebCore::BitmapTexture::isValid const): Deleted.
(WebCore::BitmapTexture::size const): Deleted.
* Source/WebCore/platform/graphics/texmap/BitmapTexture.h:
* Source/WebCore/platform/graphics/texmap/BitmapTexturePool.cpp:
(WebCore::BitmapTexturePool::acquireTexture):
* Source/WebCore/platform/graphics/texmap/TextureMapper.cpp:
(WebCore::TextureMapper::acquireTextureFromPool):
(WebCore::TextureMapper::drawTexture):
(WebCore::TextureMapper::drawTextureCopy):
(WebCore::TextureMapper::drawBlurred):
(WebCore::TextureMapper::applyBlurFilter):
(WebCore::TextureMapper::applyDropShadowFilter):
(WebCore::TextureMapper::applySinglePassFilter):
* Source/WebCore/platform/graphics/texmap/TextureMapperPlatformLayerBuffer.cpp:
(WebCore::TextureMapperPlatformLayerBuffer::clone):
* Source/WebCore/platform/graphics/texmap/TextureMapperTile.cpp:
(WebCore::TextureMapperTile::updateContents):

Canonical link: https://commits.webkit.org/270227@main
  • Loading branch information
carlosgcampos committed Nov 4, 2023
1 parent 3d73290 commit 0cf3ba9
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3264,8 +3264,7 @@ void MediaPlayerPrivateGStreamer::pushTextureToCompositor()
} else {
layerBuffer = proxy.getAvailableBuffer(frameHolder->size(), GL_DONT_CARE);
if (UNLIKELY(!layerBuffer)) {
auto texture = BitmapTexture::create();
texture->reset(frameHolder->size(), frameHolder->hasAlphaChannel() ? BitmapTexture::SupportsAlpha : BitmapTexture::NoFlag);
auto texture = BitmapTexture::create(frameHolder->size(), frameHolder->hasAlphaChannel() ? BitmapTexture::SupportsAlpha : BitmapTexture::NoFlag);
layerBuffer = makeUnique<TextureMapperPlatformLayerBuffer>(WTFMove(texture));
}
frameHolder->updateTexture(layerBuffer->texture());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ void NicosiaImageBufferPipeSource::handle(ImageBuffer& buffer)
if (!proxy.isActive())
return;

auto texture = BitmapTexture::create();

RefPtr<BitmapTexture> texture;
{
Locker locker { m_imageBufferLock };

Expand All @@ -84,8 +83,7 @@ void NicosiaImageBufferPipeSource::handle(ImageBuffer& buffer)
return;

auto size = nativeImage->size();

texture->reset(size, nativeImage->hasAlpha() ? BitmapTexture::SupportsAlpha : BitmapTexture::NoFlag);
texture = BitmapTexture::create(size, nativeImage->hasAlpha() ? BitmapTexture::SupportsAlpha : BitmapTexture::NoFlag);
#if USE(CAIRO)
auto* surface = nativeImage->platformImage().get();
auto* imageData = cairo_image_surface_get_data(surface);
Expand Down
70 changes: 29 additions & 41 deletions Source/WebCore/platform/graphics/texmap/BitmapTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,40 +47,39 @@

#if OS(DARWIN)
#define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
static const GLenum s_pixelDataType = GL_UNSIGNED_INT_8_8_8_8_REV;
#else
static const GLenum s_pixelDataType = GL_UNSIGNED_BYTE;
#endif

namespace WebCore {

BitmapTexture::BitmapTexture(const Flags, GLint internalFormat)
: m_format(GL_RGBA)
{
if (internalFormat != GL_DONT_CARE) {
m_internalFormat = internalFormat;
return;
}

m_internalFormat = GL_RGBA;
}

void BitmapTexture::didReset()
BitmapTexture::BitmapTexture(const IntSize& size, const Flags flags, GLint internalFormat)
: m_flags(flags)
, m_size(size)
, m_internalFormat(internalFormat == GL_DONT_CARE ? GL_RGBA : internalFormat)
, m_format(GL_RGBA)
{
if (!m_id)
glGenTextures(1, &m_id);

m_shouldClear = true;
m_colorConvertFlags = { };
m_filterInfo = FilterInfo();
if (m_textureSize == contentSize())
return;

m_textureSize = contentSize();
glGenTextures(1, &m_id);
glBindTexture(GL_TEXTURE_2D, m_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, m_internalFormat, m_size.width(), m_size.height(), 0, m_format, s_pixelDataType, nullptr);
}

glTexImage2D(GL_TEXTURE_2D, 0, m_internalFormat, m_textureSize.width(), m_textureSize.height(), 0, m_format, m_type, 0);
void BitmapTexture::reset(const IntSize& size, const Flags flags)
{
m_flags = flags;
m_shouldClear = true;
m_colorConvertFlags = { };
m_filterInfo = FilterInfo();
if (m_size != size) {
m_size = size;
glBindTexture(GL_TEXTURE_2D, m_id);
glTexImage2D(GL_TEXTURE_2D, 0, m_internalFormat, m_size.width(), m_size.height(), 0, m_format, s_pixelDataType, nullptr);
}
}

void BitmapTexture::updateContents(const void* srcData, const IntRect& targetRect, const IntPoint& sourceOffset, int bytesPerLine)
Expand Down Expand Up @@ -129,7 +128,7 @@ void BitmapTexture::updateContents(const void* srcData, const IntRect& targetRec
glPixelStorei(GL_UNPACK_SKIP_PIXELS, adjustedSourceOffset.x());
}

glTexSubImage2D(GL_TEXTURE_2D, 0, targetRect.x(), targetRect.y(), targetRect.width(), targetRect.height(), m_format, m_type, data);
glTexSubImage2D(GL_TEXTURE_2D, 0, targetRect.x(), targetRect.y(), targetRect.width(), targetRect.height(), m_format, s_pixelDataType, data);

if (supportsUnpackSubimage) {
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
Expand Down Expand Up @@ -210,7 +209,7 @@ void BitmapTexture::initializeStencil()

glGenRenderbuffers(1, &m_rbo);
glBindRenderbuffer(GL_RENDERBUFFER, m_rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, m_textureSize.width(), m_textureSize.height());
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, m_size.width(), m_size.height());
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_rbo);
glClearStencil(0);
Expand All @@ -231,7 +230,7 @@ void BitmapTexture::initializeDepthBuffer()

glGenRenderbuffers(1, &m_depthBufferObject);
glBindRenderbuffer(GL_RENDERBUFFER, m_depthBufferObject);
glRenderbufferStorage(GL_RENDERBUFFER, format, m_textureSize.width(), m_textureSize.height());
glRenderbufferStorage(GL_RENDERBUFFER, format, m_size.width(), m_size.height());
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthBufferObject);
}
Expand All @@ -241,7 +240,7 @@ void BitmapTexture::clearIfNeeded()
if (!m_shouldClear)
return;

m_clipStack.reset(IntRect(IntPoint::zero(), m_textureSize), ClipStack::YAxisMode::Default);
m_clipStack.reset(IntRect(IntPoint::zero(), m_size), ClipStack::YAxisMode::Default);
m_clipStack.applyIfNeeded();
glClearColor(0, 0, 0, 0);
glClearStencil(0);
Expand All @@ -267,7 +266,7 @@ void BitmapTexture::bindAsSurface()
glBindTexture(GL_TEXTURE_2D, 0);
createFboIfNeeded();
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glViewport(0, 0, m_textureSize.width(), m_textureSize.height());
glViewport(0, 0, m_size.width(), m_size.height());
if (flags() & DepthBuffer)
glEnable(GL_DEPTH_TEST);
else
Expand All @@ -278,8 +277,7 @@ void BitmapTexture::bindAsSurface()

BitmapTexture::~BitmapTexture()
{
if (m_id)
glDeleteTextures(1, &m_id);
glDeleteTextures(1, &m_id);

if (m_fbo)
glDeleteFramebuffers(1, &m_fbo);
Expand All @@ -293,16 +291,6 @@ BitmapTexture::~BitmapTexture()
glDeleteRenderbuffers(1, &m_depthBufferObject);
}

bool BitmapTexture::isValid() const
{
return m_id;
}

IntSize BitmapTexture::size() const
{
return m_textureSize;
}

void BitmapTexture::copyFromExternalTexture(GLuint sourceTextureID)
{
GLint boundTexture = 0;
Expand All @@ -322,7 +310,7 @@ void BitmapTexture::copyFromExternalTexture(GLuint sourceTextureID)

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, id());
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_textureSize.width(), m_textureSize.height());
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_size.width(), m_size.height());

glBindTexture(GL_TEXTURE_2D, boundTexture);
glBindFramebuffer(GL_FRAMEBUFFER, boundFramebuffer);
Expand Down
30 changes: 6 additions & 24 deletions Source/WebCore/platform/graphics/texmap/BitmapTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,14 @@ class BitmapTexture final : public RefCounted<BitmapTexture> {

typedef unsigned Flags;

static Ref<BitmapTexture> create(const Flags flags = NoFlag, GLint internalFormat = GL_DONT_CARE)
static Ref<BitmapTexture> create(const IntSize& size, const Flags flags = NoFlag, GLint internalFormat = GL_DONT_CARE)
{
return adoptRef(*new BitmapTexture(flags, internalFormat));
return adoptRef(*new BitmapTexture(size, flags, internalFormat));
}

WEBCORE_EXPORT ~BitmapTexture();

IntSize size() const;
bool isValid() const;
const IntSize& size() const { return m_size; };
Flags flags() const { return m_flags; }
GLint internalFormat() const { return m_internalFormat; }
bool isOpaque() const { return !(m_flags & SupportsAlpha); }
Expand All @@ -77,22 +76,13 @@ class BitmapTexture final : public RefCounted<BitmapTexture> {
void initializeStencil();
void initializeDepthBuffer();
uint32_t id() const { return m_id; }
uint32_t textureTarget() const { return GL_TEXTURE_2D; }
IntSize textureSize() const { return m_textureSize; }

void updateContents(NativeImage*, const IntRect&, const IntPoint& offset);
void updateContents(GraphicsLayer*, const IntRect& target, const IntPoint& offset, float scale = 1);
void updateContents(const void*, const IntRect& target, const IntPoint& offset, int bytesPerLine);

void reset(const IntSize& size, Flags flags = 0)
{
m_flags = flags;
m_contentSize = size;
didReset();
}
void didReset();
void reset(const IntSize&, Flags = NoFlag);

const IntSize& contentSize() const { return m_contentSize; }
int numberOfBytes() const { return size().width() * size().height() * 32 >> 3; }

RefPtr<BitmapTexture> applyFilters(TextureMapper&, const FilterOperations&, bool defersLastFilterPass);
Expand All @@ -113,15 +103,14 @@ class BitmapTexture final : public RefCounted<BitmapTexture> {
OptionSet<TextureMapperFlags> colorConvertFlags() const { return m_colorConvertFlags; }

private:
BitmapTexture(const Flags, GLint internalFormat);
BitmapTexture(const IntSize&, const Flags, GLint internalFormat);

void clearIfNeeded();
void createFboIfNeeded();

Flags m_flags { 0 };
IntSize m_contentSize;
IntSize m_size;
GLuint m_id { 0 };
IntSize m_textureSize;
GLuint m_fbo { 0 };
#if !USE(TEXMAP_DEPTH_STENCIL_BUFFER)
GLuint m_rbo { 0 };
Expand All @@ -133,13 +122,6 @@ class BitmapTexture final : public RefCounted<BitmapTexture> {
FilterInfo m_filterInfo;
GLint m_internalFormat { 0 };
GLenum m_format { 0 };
GLenum m_type {
#if OS(DARWIN)
GL_UNSIGNED_INT_8_8_8_8_REV
#else
GL_UNSIGNED_BYTE
#endif
};
};

} // namespace WebCore
5 changes: 3 additions & 2 deletions Source/WebCore/platform/graphics/texmap/BitmapTexturePool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ RefPtr<BitmapTexture> BitmapTexturePool::acquireTexture(const IntSize& size, con
});

if (selectedEntry == m_textures.end()) {
m_textures.append(Entry(BitmapTexture::create(flags)));
m_textures.append(Entry(BitmapTexture::create(size, flags)));
selectedEntry = &m_textures.last();
}
} else
selectedEntry->m_texture->reset(size, flags);

scheduleReleaseUnusedTextures();
selectedEntry->markIsInUse();
Expand Down
27 changes: 11 additions & 16 deletions Source/WebCore/platform/graphics/texmap/TextureMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ TextureMapper::TextureMapper()

RefPtr<BitmapTexture> TextureMapper::acquireTextureFromPool(const IntSize& size, const BitmapTexture::Flags flags)
{
RefPtr<BitmapTexture> selectedTexture = m_texturePool->acquireTexture(size, flags);
selectedTexture->reset(size, flags);
return selectedTexture;
return m_texturePool->acquireTexture(size, flags);
}

#if USE(GRAPHICS_LAYER_WC)
Expand Down Expand Up @@ -454,9 +452,6 @@ static void prepareRoundedRectClip(TextureMapperShaderProgram& program, const fl

void TextureMapper::drawTexture(const BitmapTexture& texture, const FloatRect& targetRect, const TransformationMatrix& matrix, float opacity, AllEdgesExposed allEdgesExposed)
{
if (!texture.isValid())
return;

if (clipStack().isCurrentScissorBoxEmpty())
return;

Expand Down Expand Up @@ -839,19 +834,19 @@ void TextureMapper::drawTexturedQuadWithProgram(TextureMapperShaderProgram& prog
void TextureMapper::drawTextureCopy(const BitmapTexture& sourceTexture, const FloatRect& sourceRect, const FloatRect& targetRect)
{
Ref<TextureMapperShaderProgram> program = data().getShaderProgram({ TextureMapperShaderProgram::TextureCopy });
IntSize textureSize = sourceTexture.contentSize();
const auto& textureSize = sourceTexture.size();

glUseProgram(program->programID());

auto textureCopyMatrix = TransformationMatrix::identity;

textureCopyMatrix.scale3d(
double(sourceRect.width()) / sourceTexture.contentSize().width(),
double(sourceRect.height()) / sourceTexture.contentSize().height(),
double(sourceRect.width()) / textureSize.width(),
double(sourceRect.height()) / textureSize.height(),
1
).translate3d(
double(sourceRect.x()) / sourceTexture.contentSize().width(),
double(sourceRect.y()) / sourceTexture.contentSize().height(),
double(sourceRect.x()) / textureSize.width(),
double(sourceRect.y()) / textureSize.height(),
0
);

Expand All @@ -872,7 +867,7 @@ void TextureMapper::drawBlurred(const BitmapTexture& sourceTexture, const FloatR
alphaBlur ? TextureMapperShaderProgram::AlphaBlur : TextureMapperShaderProgram::BlurFilter,
});

IntSize textureSize = sourceTexture.contentSize();
const auto& textureSize = sourceTexture.size();

glUseProgram(program->programID());

Expand Down Expand Up @@ -911,7 +906,7 @@ void TextureMapper::drawBlurred(const BitmapTexture& sourceTexture, const FloatR

RefPtr<BitmapTexture> TextureMapper::applyBlurFilter(RefPtr<BitmapTexture> sourceTexture, const BlurFilterOperation& blurFilter)
{
IntSize textureSize = sourceTexture->contentSize();
const auto& textureSize = sourceTexture->size();
float radiusX = floatValueForLength(blurFilter.stdDeviation(), textureSize.width());
float radiusY = floatValueForLength(blurFilter.stdDeviation(), textureSize.height());

Expand Down Expand Up @@ -993,7 +988,7 @@ RefPtr<BitmapTexture> TextureMapper::applyBlurFilter(RefPtr<BitmapTexture> sourc

RefPtr<BitmapTexture> TextureMapper::applyDropShadowFilter(RefPtr<BitmapTexture> sourceTexture, const DropShadowFilterOperation& dropShadowFilter)
{
IntSize textureSize = sourceTexture->contentSize();
const auto& textureSize = sourceTexture->size();
RefPtr<BitmapTexture> resultTexture = acquireTextureFromPool(textureSize, BitmapTexture::SupportsAlpha);
RefPtr<BitmapTexture> contentTexture = acquireTextureFromPool(textureSize, BitmapTexture::SupportsAlpha);
IntSize currentSize = textureSize;
Expand Down Expand Up @@ -1121,7 +1116,7 @@ RefPtr<BitmapTexture> TextureMapper::applySinglePassFilter(RefPtr<BitmapTexture>
return sourceTexture;
}

RefPtr<BitmapTexture> resultTexture = acquireTextureFromPool(sourceTexture->contentSize(), BitmapTexture::SupportsAlpha);
RefPtr<BitmapTexture> resultTexture = acquireTextureFromPool(sourceTexture->size(), BitmapTexture::SupportsAlpha);

bindSurface(resultTexture.get());

Expand All @@ -1130,7 +1125,7 @@ RefPtr<BitmapTexture> TextureMapper::applySinglePassFilter(RefPtr<BitmapTexture>
Ref<TextureMapperShaderProgram> program = data().getShaderProgram(options);

prepareFilterProgram(program.get(), *filter);
FloatRect targetRect(FloatPoint::zero(), sourceTexture->contentSize());
FloatRect targetRect(FloatPoint::zero(), sourceTexture->size());
drawTexturedQuadWithProgram(program.get(), sourceTexture->id(), { }, targetRect, TransformationMatrix(), 1);

return resultTexture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ std::unique_ptr<TextureMapperPlatformLayerBuffer> TextureMapperPlatformLayerBuff
return nullptr;
}

auto clonedTexture = BitmapTexture::create(BitmapTexture::NoFlag, m_internalFormat);
clonedTexture->reset(m_size);
auto clonedTexture = BitmapTexture::create(m_size, BitmapTexture::NoFlag, m_internalFormat);
clonedTexture->copyFromExternalTexture(texture.id);
return makeUnique<TextureMapperPlatformLayerBuffer>(WTFMove(clonedTexture), m_extraFlags);
},
Expand Down
12 changes: 4 additions & 8 deletions Source/WebCore/platform/graphics/texmap/TextureMapperTile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ void TextureMapperTile::updateContents(Image* image, const IntRect& dirtyRect)

// Normalize targetRect to the texture's coordinates.
targetRect.move(-m_rect.x(), -m_rect.y());
if (!m_texture) {
m_texture = BitmapTexture::create();
m_texture->reset(targetRect.size(), image->currentFrameKnownToBeOpaque() ? 0 : BitmapTexture::SupportsAlpha);
}
if (!m_texture)
m_texture = BitmapTexture::create(targetRect.size(), image->currentFrameKnownToBeOpaque() ? 0 : BitmapTexture::SupportsAlpha);

auto nativeImage = image->nativeImageForCurrentFrame();
m_texture->updateContents(nativeImage.get(), targetRect, sourceOffset);
Expand All @@ -77,10 +75,8 @@ void TextureMapperTile::updateContents(GraphicsLayer* sourceLayer, const IntRect
// Normalize targetRect to the texture's coordinates.
targetRect.move(-m_rect.x(), -m_rect.y());

if (!m_texture) {
m_texture = BitmapTexture::create();
m_texture->reset(targetRect.size(), BitmapTexture::SupportsAlpha);
}
if (!m_texture)
m_texture = BitmapTexture::create(targetRect.size(), BitmapTexture::SupportsAlpha);

m_texture->updateContents(sourceLayer, targetRect, sourceOffset, scale);
}
Expand Down

0 comments on commit 0cf3ba9

Please sign in to comment.