libkwineffects: make GLVertexBuffer::map safer

This ensures that in the event of a GPU reset, failure to map the vbo is
always handled without crashing
This commit is contained in:
Xaver Hugl 2023-05-14 23:19:35 +02:00
parent 3ea701743e
commit 3eb5f18861
9 changed files with 57 additions and 35 deletions

View file

@ -941,9 +941,7 @@ WindowQuadList WindowQuadList::makeRegularGrid(int xSubdivisions, int ySubdivisi
void RenderGeometry::copy(std::span<GLVertex2D> destination) void RenderGeometry::copy(std::span<GLVertex2D> destination)
{ {
Q_ASSERT(int(destination.size()) >= size()); Q_ASSERT(int(destination.size()) >= size());
for (std::size_t i = 0; i < destination.size(); ++i) { std::copy(cbegin(), cend(), destination.begin());
destination[i] = at(i);
}
} }
void RenderGeometry::appendWindowVertex(const WindowVertex &windowVertex, qreal deviceScale) void RenderGeometry::appendWindowVertex(const WindowVertex &windowVertex, qreal deviceScale)

View file

@ -19,6 +19,8 @@
#include <QSize> #include <QSize>
#include <QStack> #include <QStack>
#include <span>
/** @addtogroup kwineffects */ /** @addtogroup kwineffects */
/** @{ */ /** @{ */
@ -627,7 +629,15 @@ public:
* It is assumed that the GL_ARRAY_BUFFER_BINDING will not be changed while * It is assumed that the GL_ARRAY_BUFFER_BINDING will not be changed while
* the buffer object is mapped. * the buffer object is mapped.
*/ */
GLvoid *map(size_t size); template<typename T>
std::optional<std::span<T>> map(size_t count)
{
if (const auto m = map(sizeof(T) * count)) {
return std::span(reinterpret_cast<T *>(m), count);
} else {
return std::nullopt;
}
}
/** /**
* Flushes the mapped buffer range and unmaps the buffer. * Flushes the mapped buffer range and unmaps the buffer.
@ -740,6 +750,8 @@ public:
static const GLVertexAttrib GLVertex3DLayout[2]; static const GLVertexAttrib GLVertex3DLayout[2];
private: private:
GLvoid *map(size_t size);
const std::unique_ptr<GLVertexBufferPrivate> d; const std::unique_ptr<GLVertexBufferPrivate> d;
}; };

View file

@ -167,8 +167,11 @@ void OffscreenData::paint(const RenderTarget &renderTarget, const RenderViewport
} }
geometry.postProcessTextureCoordinates(m_texture->matrix(NormalizedCoordinates)); geometry.postProcessTextureCoordinates(m_texture->matrix(NormalizedCoordinates));
GLVertex2D *map = static_cast<GLVertex2D *>(vbo->map(geometry.count() * sizeof(GLVertex2D))); const auto map = vbo->map<GLVertex2D>(geometry.size());
geometry.copy(std::span(map, geometry.count())); if (!map) {
return;
}
geometry.copy(*map);
vbo->unmap(); vbo->unmap();
vbo->bindArrays(); vbo->bindArrays();

View file

@ -310,9 +310,9 @@ QRegion ContrastEffect::contrastRegion(const EffectWindow *w) const
return region; return region;
} }
void ContrastEffect::uploadRegion(QVector2D *&map, const QRegion &region, qreal scale) void ContrastEffect::uploadRegion(std::span<QVector2D> map, const QRegion &region, qreal scale)
{ {
Q_ASSERT(map); size_t index = 0;
for (const QRect &r : region) { for (const QRect &r : region) {
const auto deviceRect = scaledRect(r, scale); const auto deviceRect = scaledRect(r, scale);
const QVector2D topLeft = roundVector(QVector2D(deviceRect.x(), deviceRect.y())); const QVector2D topLeft = roundVector(QVector2D(deviceRect.x(), deviceRect.y()));
@ -321,14 +321,14 @@ void ContrastEffect::uploadRegion(QVector2D *&map, const QRegion &region, qreal
const QVector2D bottomRight = roundVector(QVector2D(deviceRect.x() + deviceRect.width(), deviceRect.y() + deviceRect.height())); const QVector2D bottomRight = roundVector(QVector2D(deviceRect.x() + deviceRect.width(), deviceRect.y() + deviceRect.height()));
// First triangle // First triangle
*(map++) = topRight; map[index++] = topRight;
*(map++) = topLeft; map[index++] = topLeft;
*(map++) = bottomLeft; map[index++] = bottomLeft;
// Second triangle // Second triangle
*(map++) = bottomLeft; map[index++] = bottomLeft;
*(map++) = bottomRight; map[index++] = bottomRight;
*(map++) = topRight; map[index++] = topRight;
} }
} }
@ -339,11 +339,11 @@ bool ContrastEffect::uploadGeometry(GLVertexBuffer *vbo, const QRegion &region,
return false; return false;
} }
QVector2D *map = (QVector2D *)vbo->map(vertexCount * sizeof(QVector2D)); const auto map = vbo->map<QVector2D>(vertexCount);
if (!map) { if (!map) {
return false; return false;
} }
uploadRegion(map, region, scale); uploadRegion(*map, region, scale);
vbo->unmap(); vbo->unmap();
const GLVertexAttrib layout[] = { const GLVertexAttrib layout[] = {

View file

@ -60,7 +60,7 @@ private:
bool shouldContrast(const EffectWindow *w, int mask, const WindowPaintData &data) const; bool shouldContrast(const EffectWindow *w, int mask, const WindowPaintData &data) const;
void updateContrastRegion(EffectWindow *w); void updateContrastRegion(EffectWindow *w);
void doContrast(const RenderTarget &renderTarget, const RenderViewport &viewport, EffectWindow *w, const QRegion &shape, const QRect &screen, const float opacity, const QMatrix4x4 &screenProjection); void doContrast(const RenderTarget &renderTarget, const RenderViewport &viewport, EffectWindow *w, const QRegion &shape, const QRect &screen, const float opacity, const QMatrix4x4 &screenProjection);
void uploadRegion(QVector2D *&map, const QRegion &region, qreal scale); void uploadRegion(std::span<QVector2D> map, const QRegion &region, qreal scale);
Q_REQUIRED_RESULT bool uploadGeometry(GLVertexBuffer *vbo, const QRegion &region, qreal scale); Q_REQUIRED_RESULT bool uploadGeometry(GLVertexBuffer *vbo, const QRegion &region, qreal scale);
private: private:

View file

@ -496,9 +496,8 @@ QRegion BlurEffect::blurRegion(const EffectWindow *w) const
return region; return region;
} }
void BlurEffect::uploadRegion(QVector2D *&map, const QRegion &region) void BlurEffect::uploadRegion(const std::span<QVector2D> map, size_t &index, const QRegion &region)
{ {
Q_ASSERT(map);
for (const QRect &r : region) { for (const QRect &r : region) {
const QVector2D topLeft(r.x(), r.y()); const QVector2D topLeft(r.x(), r.y());
const QVector2D topRight((r.x() + r.width()), r.y()); const QVector2D topRight((r.x() + r.width()), r.y());
@ -506,14 +505,14 @@ void BlurEffect::uploadRegion(QVector2D *&map, const QRegion &region)
const QVector2D bottomRight((r.x() + r.width()), (r.y() + r.height())); const QVector2D bottomRight((r.x() + r.width()), (r.y() + r.height()));
// First triangle // First triangle
*(map++) = topRight; map[index++] = topRight;
*(map++) = topLeft; map[index++] = topLeft;
*(map++) = bottomLeft; map[index++] = bottomLeft;
// Second triangle // Second triangle
*(map++) = bottomLeft; map[index++] = bottomLeft;
*(map++) = bottomRight; map[index++] = bottomRight;
*(map++) = topRight; map[index++] = topRight;
} }
} }
@ -524,13 +523,14 @@ bool BlurEffect::uploadGeometry(GLVertexBuffer *vbo, const QRegion &expandedBlur
return false; return false;
} }
QVector2D *map = (QVector2D *)vbo->map(vertexCount * sizeof(QVector2D)); auto map = vbo->map<QVector2D>(vertexCount);
if (!map) { if (!map) {
return false; return false;
} }
uploadRegion(map, expandedBlurRegion); size_t index = 0;
uploadRegion(map, blurRegion); uploadRegion(*map, index, expandedBlurRegion);
uploadRegion(*map, index, blurRegion);
vbo->unmap(); vbo->unmap();

View file

@ -84,7 +84,7 @@ private:
bool shouldBlur(const EffectWindow *w, int mask, const WindowPaintData &data) const; bool shouldBlur(const EffectWindow *w, int mask, const WindowPaintData &data) const;
void updateBlurRegion(EffectWindow *w); void updateBlurRegion(EffectWindow *w);
void doBlur(const RenderTarget &renderTarget, const RenderViewport &viewport, const QRegion &shape, const QRect &screen, const float opacity, bool isDock, QRect windowRect); void doBlur(const RenderTarget &renderTarget, const RenderViewport &viewport, const QRegion &shape, const QRect &screen, const float opacity, bool isDock, QRect windowRect);
void uploadRegion(QVector2D *&map, const QRegion &region); void uploadRegion(const std::span<QVector2D> map, size_t &index, const QRegion &region);
Q_REQUIRED_RESULT bool uploadGeometry(GLVertexBuffer *vbo, const QRegion &expandedBlurRegion, const QRegion &blurRegion); Q_REQUIRED_RESULT bool uploadGeometry(GLVertexBuffer *vbo, const QRegion &expandedBlurRegion, const QRegion &blurRegion);
void generateNoiseTexture(); void generateNoiseTexture();
void screenAdded(EffectScreen *screen); void screenAdded(EffectScreen *screen);

View file

@ -128,7 +128,11 @@ static GLVertexBuffer *texturedRectVbo(const QRectF &geometry, qreal scale)
}; };
vbo->setAttribLayout(attribs, 2, sizeof(GLVertex2D)); vbo->setAttribLayout(attribs, 2, sizeof(GLVertex2D));
auto map = static_cast<GLVertex2D *>(vbo->map(6 * sizeof(GLVertex2D))); const auto opt = vbo->map<GLVertex2D>(6);
if (!opt) {
return nullptr;
}
const auto map = *opt;
auto deviceGeometry = scaledRect(geometry, scale); auto deviceGeometry = scaledRect(geometry, scale);
@ -222,6 +226,11 @@ void ScreenTransformEffect::paintScreen(const RenderTarget &renderTarget, const
glClearColor(0, 0, 0, 0); glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
GLVertexBuffer *vbo = texturedRectVbo(lerp(it->m_oldGeometry, screenRect, blendFactor), scale);
if (!vbo) {
return;
}
ShaderManager *sm = ShaderManager::instance(); ShaderManager *sm = ShaderManager::instance();
sm->pushShader(m_shader.get()); sm->pushShader(m_shader.get());
m_shader->setUniform(m_modelViewProjectioMatrixLocation, modelViewProjectionMatrix); m_shader->setUniform(m_modelViewProjectioMatrixLocation, modelViewProjectionMatrix);
@ -229,7 +238,6 @@ void ScreenTransformEffect::paintScreen(const RenderTarget &renderTarget, const
m_shader->setUniform(m_currentTextureLocation, 0); m_shader->setUniform(m_currentTextureLocation, 0);
m_shader->setUniform(m_previousTextureLocation, 1); m_shader->setUniform(m_previousTextureLocation, 1);
GLVertexBuffer *vbo = texturedRectVbo(lerp(it->m_oldGeometry, screenRect, blendFactor), scale);
vbo->bindArrays(); vbo->bindArrays();
vbo->draw(GL_TRIANGLES, 0, 6); vbo->draw(GL_TRIANGLES, 0, 6);
vbo->unbindArrays(); vbo->unbindArrays();

View file

@ -281,8 +281,6 @@ void ItemRendererOpenGL::renderItem(const RenderTarget &renderTarget, const Rend
return; return;
} }
const size_t size = totalVertexCount * sizeof(GLVertex2D);
ShaderTraits shaderTraits = ShaderTrait::MapTexture | ShaderTrait::TransformColorspace; ShaderTraits shaderTraits = ShaderTrait::MapTexture | ShaderTrait::TransformColorspace;
if (data.brightness() != 1.0) { if (data.brightness() != 1.0) {
@ -296,7 +294,10 @@ void ItemRendererOpenGL::renderItem(const RenderTarget &renderTarget, const Rend
vbo->reset(); vbo->reset();
vbo->setAttribLayout(GLVertexBuffer::GLVertex2DLayout, 2, sizeof(GLVertex2D)); vbo->setAttribLayout(GLVertexBuffer::GLVertex2DLayout, 2, sizeof(GLVertex2D));
GLVertex2D *map = (GLVertex2D *)vbo->map(size); const auto map = vbo->map<GLVertex2D>(totalVertexCount);
if (!map) {
return;
}
for (int i = 0, v = 0; i < renderContext.renderNodes.count(); i++) { for (int i = 0, v = 0; i < renderContext.renderNodes.count(); i++) {
RenderNode &renderNode = renderContext.renderNodes[i]; RenderNode &renderNode = renderContext.renderNodes[i];
@ -313,7 +314,7 @@ void ItemRendererOpenGL::renderItem(const RenderTarget &renderTarget, const Rend
renderNode.geometry.postProcessTextureCoordinates(renderNode.texture->matrix(renderNode.coordinateType)); renderNode.geometry.postProcessTextureCoordinates(renderNode.texture->matrix(renderNode.coordinateType));
renderNode.geometry.copy(std::span(&map[v], renderNode.geometry.count())); renderNode.geometry.copy(map->subspan(v));
v += renderNode.geometry.count(); v += renderNode.geometry.count();
} }