| 1 | // Copyright (C) 2019 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qsgrhilayer_p.h" |
| 5 | |
| 6 | #include <private/qqmlglobal_p.h> |
| 7 | #include <private/qsgrenderer_p.h> |
| 8 | |
| 9 | QSGRhiLayer::QSGRhiLayer(QSGRenderContext *context) |
| 10 | : QSGLayer(*(new QSGTexturePrivate(this))) |
| 11 | , m_mipmap(false) |
| 12 | , m_live(true) |
| 13 | , m_recursive(false) |
| 14 | , m_dirtyTexture(true) |
| 15 | , m_multisampling(false) |
| 16 | , m_grab(false) |
| 17 | , m_mirrorHorizontal(false) |
| 18 | , m_mirrorVertical(true) |
| 19 | { |
| 20 | m_context = static_cast<QSGDefaultRenderContext *>(context); |
| 21 | m_rhi = m_context->rhi(); |
| 22 | Q_ASSERT(m_rhi); |
| 23 | } |
| 24 | |
| 25 | QSGRhiLayer::~QSGRhiLayer() |
| 26 | { |
| 27 | invalidated(); |
| 28 | } |
| 29 | |
| 30 | void QSGRhiLayer::invalidated() |
| 31 | { |
| 32 | releaseResources(); |
| 33 | |
| 34 | delete m_prevTexture; |
| 35 | m_prevTexture = nullptr; |
| 36 | |
| 37 | delete m_renderer; |
| 38 | m_renderer = nullptr; |
| 39 | } |
| 40 | |
| 41 | qint64 QSGRhiLayer::comparisonKey() const |
| 42 | { |
| 43 | return qint64(m_texture); |
| 44 | } |
| 45 | |
| 46 | bool QSGRhiLayer::hasAlphaChannel() const |
| 47 | { |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | bool QSGRhiLayer::hasMipmaps() const |
| 52 | { |
| 53 | return m_mipmap; |
| 54 | } |
| 55 | |
| 56 | QRhiTexture *QSGRhiLayer::rhiTexture() const |
| 57 | { |
| 58 | return m_prevTexture ? m_prevTexture : m_texture; |
| 59 | } |
| 60 | |
| 61 | void QSGRhiLayer::commitTextureOperations(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates) |
| 62 | { |
| 63 | Q_UNUSED(rhi); |
| 64 | Q_UNUSED(resourceUpdates); |
| 65 | } |
| 66 | |
| 67 | bool QSGRhiLayer::updateTexture() |
| 68 | { |
| 69 | // called during the preprocess phase, outside of frame rendering -> good |
| 70 | |
| 71 | bool doGrab = (m_live || m_grab) && m_dirtyTexture; |
| 72 | if (doGrab) |
| 73 | grab(); |
| 74 | |
| 75 | if (m_grab) |
| 76 | emit scheduledUpdateCompleted(); |
| 77 | |
| 78 | m_grab = false; |
| 79 | return doGrab; |
| 80 | } |
| 81 | |
| 82 | void QSGRhiLayer::setHasMipmaps(bool mipmap) |
| 83 | { |
| 84 | if (mipmap == m_mipmap) |
| 85 | return; |
| 86 | |
| 87 | m_mipmap = mipmap; |
| 88 | if (m_mipmap && m_texture) |
| 89 | markDirtyTexture(); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | void QSGRhiLayer::setItem(QSGNode *item) |
| 94 | { |
| 95 | if (item == m_item) |
| 96 | return; |
| 97 | |
| 98 | m_item = item; |
| 99 | |
| 100 | if (m_live && !m_item) |
| 101 | releaseResources(); |
| 102 | |
| 103 | markDirtyTexture(); |
| 104 | } |
| 105 | |
| 106 | void QSGRhiLayer::setRect(const QRectF &logicalRect) |
| 107 | { |
| 108 | if (logicalRect == m_logicalRect) |
| 109 | return; |
| 110 | |
| 111 | m_logicalRect = logicalRect; |
| 112 | markDirtyTexture(); |
| 113 | } |
| 114 | |
| 115 | void QSGRhiLayer::setSize(const QSize &pixelSize) |
| 116 | { |
| 117 | if (pixelSize == m_pixelSize) |
| 118 | return; |
| 119 | |
| 120 | const int textureSizeMax = m_rhi->resourceLimit(limit: QRhi::TextureSizeMax); |
| 121 | m_pixelSize = pixelSize.boundedTo(otherSize: QSize(textureSizeMax, textureSizeMax)); |
| 122 | |
| 123 | if (Q_UNLIKELY(m_pixelSize != pixelSize)) { |
| 124 | qWarning(msg: "QSGRhiLayer: Unsupported size requested: [%d, %d]. " |
| 125 | "Maximum texture size: %d" , |
| 126 | pixelSize.width(), |
| 127 | pixelSize.height(), |
| 128 | textureSizeMax); |
| 129 | } |
| 130 | |
| 131 | if (m_live && m_pixelSize.isNull()) |
| 132 | releaseResources(); |
| 133 | |
| 134 | markDirtyTexture(); |
| 135 | } |
| 136 | |
| 137 | void QSGRhiLayer::setFormat(Format format) |
| 138 | { |
| 139 | QRhiTexture::Format rhiFormat = QRhiTexture::RGBA8; |
| 140 | switch (format) { |
| 141 | case RGBA16F: |
| 142 | rhiFormat = QRhiTexture::RGBA16F; |
| 143 | break; |
| 144 | case RGBA32F: |
| 145 | rhiFormat = QRhiTexture::RGBA32F; |
| 146 | break; |
| 147 | default: |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | if (rhiFormat == m_format) |
| 152 | return; |
| 153 | |
| 154 | if (m_rhi->isTextureFormatSupported(format: rhiFormat)) { |
| 155 | m_format = rhiFormat; |
| 156 | markDirtyTexture(); |
| 157 | } else { |
| 158 | qWarning(msg: "QSGRhiLayer: Attempted to set unsupported texture format %d" , int(rhiFormat)); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void QSGRhiLayer::setLive(bool live) |
| 163 | { |
| 164 | if (live == m_live) |
| 165 | return; |
| 166 | |
| 167 | m_live = live; |
| 168 | |
| 169 | if (m_live && (!m_item || m_pixelSize.isNull())) |
| 170 | releaseResources(); |
| 171 | |
| 172 | markDirtyTexture(); |
| 173 | } |
| 174 | |
| 175 | void QSGRhiLayer::scheduleUpdate() |
| 176 | { |
| 177 | if (m_grab) |
| 178 | return; |
| 179 | |
| 180 | m_grab = true; |
| 181 | if (m_dirtyTexture) |
| 182 | emit updateRequested(); |
| 183 | } |
| 184 | |
| 185 | void QSGRhiLayer::setRecursive(bool recursive) |
| 186 | { |
| 187 | m_recursive = recursive; |
| 188 | } |
| 189 | |
| 190 | void QSGRhiLayer::setMirrorHorizontal(bool mirror) |
| 191 | { |
| 192 | m_mirrorHorizontal = mirror; |
| 193 | } |
| 194 | |
| 195 | void QSGRhiLayer::setMirrorVertical(bool mirror) |
| 196 | { |
| 197 | m_mirrorVertical = mirror; |
| 198 | } |
| 199 | |
| 200 | void QSGRhiLayer::markDirtyTexture() |
| 201 | { |
| 202 | m_dirtyTexture = true; |
| 203 | if (m_live || m_grab) |
| 204 | emit updateRequested(); |
| 205 | } |
| 206 | |
| 207 | void QSGRhiLayer::releaseResources() |
| 208 | { |
| 209 | delete m_rt; |
| 210 | m_rt = nullptr; |
| 211 | |
| 212 | delete m_rtRp; |
| 213 | m_rtRp = nullptr; |
| 214 | |
| 215 | m_ds.clear(); |
| 216 | |
| 217 | delete m_msaaColorBuffer; |
| 218 | m_msaaColorBuffer = nullptr; |
| 219 | |
| 220 | if (m_prevTexture != m_texture) |
| 221 | delete m_texture; |
| 222 | |
| 223 | m_texture = nullptr; |
| 224 | |
| 225 | delete m_secondaryTexture; |
| 226 | m_secondaryTexture = nullptr; |
| 227 | } |
| 228 | |
| 229 | void QSGRhiLayer::clearMainTexture() |
| 230 | { |
| 231 | std::unique_ptr<QRhiTextureRenderTarget> tempRt(m_rhi->newTextureRenderTarget(desc: { m_texture })); |
| 232 | std::unique_ptr<QRhiRenderPassDescriptor> tempRp(tempRt->newCompatibleRenderPassDescriptor()); |
| 233 | tempRt->setRenderPassDescriptor(tempRp.get()); |
| 234 | if (tempRt->create()) { |
| 235 | m_context->currentFrameCommandBuffer()->beginPass(rt: tempRt.get(), colorClearValue: Qt::transparent, depthStencilClearValue: { 1.0f, 0 }); |
| 236 | m_context->currentFrameCommandBuffer()->endPass(); |
| 237 | } else { |
| 238 | qWarning(msg: "Failed to clear layer main texture in recursive mode" ); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | void QSGRhiLayer::grab() |
| 243 | { |
| 244 | if (!m_item || m_pixelSize.isEmpty()) { |
| 245 | releaseResources(); |
| 246 | m_dirtyTexture = false; |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | int effectiveSamples = m_samples; |
| 251 | // if no layer.samples was provided use the window's msaa setting |
| 252 | if (effectiveSamples <= 1) |
| 253 | effectiveSamples = m_context->msaaSampleCount(); |
| 254 | |
| 255 | const bool needsNewRt = !m_rt || m_rt->pixelSize() != m_pixelSize || (m_recursive && !m_secondaryTexture) || (m_texture && m_texture->format() != m_format); |
| 256 | const bool mipmapSettingChanged = m_texture && m_texture->flags().testFlag(flag: QRhiTexture::MipMapped) != m_mipmap; |
| 257 | const bool msaaSettingChanged = (effectiveSamples > 1 && !m_msaaColorBuffer) || (effectiveSamples <= 1 && m_msaaColorBuffer); |
| 258 | |
| 259 | if (needsNewRt ||mipmapSettingChanged || msaaSettingChanged) { |
| 260 | if (effectiveSamples <= 1) { |
| 261 | m_multisampling = false; |
| 262 | } else { |
| 263 | m_multisampling = m_rhi->isFeatureSupported(feature: QRhi::MultisampleRenderBuffer); |
| 264 | if (!m_multisampling) |
| 265 | qWarning(msg: "Layer requested %d samples but multisample renderbuffers are not supported" , effectiveSamples); |
| 266 | } |
| 267 | |
| 268 | QRhiTexture::Flags textureFlags = QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource; |
| 269 | if (m_mipmap) |
| 270 | textureFlags |= QRhiTexture::MipMapped | QRhiTexture::UsedWithGenerateMips; |
| 271 | |
| 272 | // Not the same as m_context->useDepthBufferFor2D(), only the env.var |
| 273 | // is to be checked here. Consider a layer with a non-offscreen View3D |
| 274 | // in it. That still needs a depth buffer, even when the 2D content |
| 275 | // renders without relying on it (i.e. RenderMode2DNoDepthBuffer does |
| 276 | // not imply not having a depth/stencil attachment for the render |
| 277 | // target! The env.var serves as a hard switch, on the other hand, and |
| 278 | // that will likely break 3D for instance but that's fine) |
| 279 | static bool depthBufferEnabled = qEnvironmentVariableIsEmpty(varName: "QSG_NO_DEPTH_BUFFER" ); |
| 280 | |
| 281 | if (m_recursive && m_texture) { |
| 282 | if (m_prevTexture != m_texture) |
| 283 | delete m_prevTexture; |
| 284 | m_prevTexture = m_texture; |
| 285 | } |
| 286 | |
| 287 | releaseResources(); |
| 288 | |
| 289 | if (m_multisampling) { |
| 290 | m_msaaColorBuffer = m_rhi->newRenderBuffer(type: QRhiRenderBuffer::Color, pixelSize: m_pixelSize, sampleCount: effectiveSamples); |
| 291 | if (!m_msaaColorBuffer->create()) { |
| 292 | qWarning(msg: "Failed to build multisample color buffer for layer of size %dx%d, sample count %d" , |
| 293 | m_pixelSize.width(), m_pixelSize.height(), effectiveSamples); |
| 294 | releaseResources(); |
| 295 | return; |
| 296 | } |
| 297 | m_texture = m_rhi->newTexture(format: m_format, pixelSize: m_pixelSize, sampleCount: 1, flags: textureFlags); |
| 298 | if (!m_texture->create()) { |
| 299 | qWarning(msg: "Failed to build texture for layer of size %dx%d" , m_pixelSize.width(), m_pixelSize.height()); |
| 300 | releaseResources(); |
| 301 | return; |
| 302 | } |
| 303 | if (depthBufferEnabled) { |
| 304 | m_ds = m_context->getDepthStencilBuffer(size: m_pixelSize, sampleCount: effectiveSamples); |
| 305 | if (!m_ds) { |
| 306 | releaseResources(); |
| 307 | return; |
| 308 | } |
| 309 | } |
| 310 | QRhiTextureRenderTargetDescription desc; |
| 311 | QRhiColorAttachment color0(m_msaaColorBuffer); |
| 312 | if (m_recursive) { |
| 313 | m_secondaryTexture = m_rhi->newTexture(format: m_format, pixelSize: m_pixelSize, sampleCount: 1, flags: textureFlags); |
| 314 | if (!m_secondaryTexture->create()) { |
| 315 | qWarning(msg: "Failed to build secondary texture for layer of size %dx%d" , m_pixelSize.width(), m_pixelSize.height()); |
| 316 | releaseResources(); |
| 317 | return; |
| 318 | } |
| 319 | color0.setResolveTexture(m_secondaryTexture); |
| 320 | if (!m_prevTexture) |
| 321 | clearMainTexture(); |
| 322 | } else { |
| 323 | color0.setResolveTexture(m_texture); |
| 324 | } |
| 325 | desc.setColorAttachments({ color0 }); |
| 326 | if (depthBufferEnabled) |
| 327 | desc.setDepthStencilBuffer(m_ds->ds); |
| 328 | m_rt = m_rhi->newTextureRenderTarget(desc); |
| 329 | m_rtRp = m_rt->newCompatibleRenderPassDescriptor(); |
| 330 | if (!m_rtRp) { |
| 331 | qWarning(msg: "Failed to build render pass descriptor for layer" ); |
| 332 | releaseResources(); |
| 333 | return; |
| 334 | } |
| 335 | m_rt->setRenderPassDescriptor(m_rtRp); |
| 336 | if (!m_rt->create()) { |
| 337 | qWarning(msg: "Failed to build texture render target for layer" ); |
| 338 | releaseResources(); |
| 339 | return; |
| 340 | } |
| 341 | } else { |
| 342 | m_texture = m_rhi->newTexture(format: m_format, pixelSize: m_pixelSize, sampleCount: 1, flags: textureFlags); |
| 343 | if (!m_texture->create()) { |
| 344 | qWarning(msg: "Failed to build texture for layer of size %dx%d" , m_pixelSize.width(), m_pixelSize.height()); |
| 345 | releaseResources(); |
| 346 | return; |
| 347 | } |
| 348 | if (depthBufferEnabled) { |
| 349 | m_ds = m_context->getDepthStencilBuffer(size: m_pixelSize, sampleCount: 1); |
| 350 | if (!m_ds) { |
| 351 | releaseResources(); |
| 352 | return; |
| 353 | } |
| 354 | } |
| 355 | QRhiColorAttachment color0(m_texture); |
| 356 | if (m_recursive) { |
| 357 | // Here rt is associated with m_secondaryTexture instead of m_texture. |
| 358 | // We will issue a copy to m_texture afterwards. |
| 359 | m_secondaryTexture = m_rhi->newTexture(format: m_format, pixelSize: m_pixelSize, sampleCount: 1, flags: textureFlags); |
| 360 | if (!m_secondaryTexture->create()) { |
| 361 | qWarning(msg: "Failed to build texture for layer of size %dx%d" , m_pixelSize.width(), m_pixelSize.height()); |
| 362 | releaseResources(); |
| 363 | return; |
| 364 | } |
| 365 | color0.setTexture(m_secondaryTexture); |
| 366 | if (!m_prevTexture) |
| 367 | clearMainTexture(); |
| 368 | } |
| 369 | QRhiTextureRenderTargetDescription desc({ color0 }); |
| 370 | if (depthBufferEnabled) |
| 371 | desc.setDepthStencilBuffer(m_ds->ds); |
| 372 | m_rt = m_rhi->newTextureRenderTarget(desc); |
| 373 | m_rtRp = m_rt->newCompatibleRenderPassDescriptor(); |
| 374 | if (!m_rtRp) { |
| 375 | qWarning(msg: "Failed to build render pass descriptor for layer" ); |
| 376 | releaseResources(); |
| 377 | return; |
| 378 | } |
| 379 | m_rt->setRenderPassDescriptor(m_rtRp); |
| 380 | if (!m_rt->create()) { |
| 381 | qWarning(msg: "Failed to build texture render target for layer" ); |
| 382 | releaseResources(); |
| 383 | return; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | QSGNode *root = m_item; |
| 389 | while (root->firstChild() && root->type() != QSGNode::RootNodeType) |
| 390 | root = root->firstChild(); |
| 391 | if (root->type() != QSGNode::RootNodeType) |
| 392 | return; |
| 393 | |
| 394 | if (!m_renderer) { |
| 395 | const bool useDepth = m_context->useDepthBufferFor2D(); |
| 396 | const QSGRendererInterface::RenderMode renderMode = useDepth ? QSGRendererInterface::RenderMode2D |
| 397 | : QSGRendererInterface::RenderMode2DNoDepthBuffer; |
| 398 | m_renderer = m_context->createRenderer(renderMode); |
| 399 | connect(sender: m_renderer, SIGNAL(sceneGraphChanged()), receiver: this, SLOT(markDirtyTexture())); |
| 400 | } |
| 401 | m_renderer->setRootNode(static_cast<QSGRootNode *>(root)); |
| 402 | root->markDirty(bits: QSGNode::DirtyForceUpdate); // Force matrix, clip and opacity update. |
| 403 | m_renderer->nodeChanged(node: root, state: QSGNode::DirtyForceUpdate); // Force render list update. |
| 404 | |
| 405 | // This must not be moved. The flag must be reset only after the |
| 406 | // nodeChanged otherwise we end up with constantly updating even when the |
| 407 | // layer contents do not change. |
| 408 | m_dirtyTexture = false; |
| 409 | |
| 410 | m_renderer->setDevicePixelRatio(m_dpr); |
| 411 | m_renderer->setDeviceRect(m_pixelSize); |
| 412 | m_renderer->setViewportRect(m_pixelSize); |
| 413 | |
| 414 | QRectF mirrored; // in logical coordinates (no dpr) since this gets passed to setProjectionMatrixToRect() |
| 415 | |
| 416 | // In the unlikely event of back/front face culling used by a custom |
| 417 | // material or effect in the layer, the default front face setting may be |
| 418 | // wrong. Rather, it needs to invert based on what the vertex shader does, |
| 419 | // and so on the rect (and so matrix) generated here. |
| 420 | bool frontFaceSwap = false; |
| 421 | |
| 422 | if (m_rhi->isYUpInFramebuffer()) { // basically OpenGL |
| 423 | mirrored = QRectF(m_mirrorHorizontal ? m_logicalRect.right() : m_logicalRect.left(), |
| 424 | m_mirrorVertical ? m_logicalRect.bottom() : m_logicalRect.top(), |
| 425 | m_mirrorHorizontal ? -m_logicalRect.width() : m_logicalRect.width(), |
| 426 | m_mirrorVertical ? -m_logicalRect.height() : m_logicalRect.height()); |
| 427 | if (m_mirrorHorizontal) |
| 428 | frontFaceSwap = !frontFaceSwap; |
| 429 | if (m_mirrorVertical) |
| 430 | frontFaceSwap = !frontFaceSwap; |
| 431 | } else { // APIs other than OpenGL |
| 432 | mirrored = QRectF(m_mirrorHorizontal ? m_logicalRect.right() : m_logicalRect.left(), |
| 433 | m_mirrorVertical ? m_logicalRect.top() : m_logicalRect.bottom(), |
| 434 | m_mirrorHorizontal ? -m_logicalRect.width() : m_logicalRect.width(), |
| 435 | m_mirrorVertical ? m_logicalRect.height() : -m_logicalRect.height()); |
| 436 | if (m_mirrorHorizontal) |
| 437 | frontFaceSwap = !frontFaceSwap; |
| 438 | if (!m_mirrorVertical) |
| 439 | frontFaceSwap = !frontFaceSwap; |
| 440 | } |
| 441 | |
| 442 | QSGAbstractRenderer::MatrixTransformFlags matrixFlags; |
| 443 | if (!m_rhi->isYUpInNDC()) |
| 444 | matrixFlags |= QSGAbstractRenderer::MatrixTransformFlipY; |
| 445 | |
| 446 | m_renderer->setProjectionMatrixToRect(rect: mirrored, flags: matrixFlags); |
| 447 | m_renderer->setInvertFrontFace(frontFaceSwap); |
| 448 | m_renderer->setClearColor(Qt::transparent); |
| 449 | m_renderer->setRenderTarget({ m_rt, m_rtRp, m_context->currentFrameCommandBuffer() }); |
| 450 | |
| 451 | QRhiResourceUpdateBatch *resourceUpdates = nullptr; |
| 452 | |
| 453 | // render with our own "sub-renderer" (this will just add a render pass to the command buffer) |
| 454 | if (m_recursive) { |
| 455 | m_context->renderNextFrame(renderer: m_renderer); |
| 456 | if (!resourceUpdates) |
| 457 | resourceUpdates = m_rhi->nextResourceUpdateBatch(); |
| 458 | resourceUpdates->copyTexture(dst: m_texture, src: m_secondaryTexture); |
| 459 | if (m_prevTexture) { |
| 460 | delete m_prevTexture; |
| 461 | m_prevTexture = nullptr; |
| 462 | } |
| 463 | } else { |
| 464 | m_context->renderNextFrame(renderer: m_renderer); |
| 465 | } |
| 466 | |
| 467 | if (m_mipmap) { |
| 468 | if (!resourceUpdates) |
| 469 | resourceUpdates = m_rhi->nextResourceUpdateBatch(); |
| 470 | // going to be expensive - if done every frame - but the user asked for it... |
| 471 | resourceUpdates->generateMips(tex: m_texture); |
| 472 | } |
| 473 | |
| 474 | // Do not defer committing the resource updates to the main pass - with |
| 475 | // multiple layers there can be dependencies, so the texture should be |
| 476 | // usable once we return. |
| 477 | m_context->currentFrameCommandBuffer()->resourceUpdate(resourceUpdates); |
| 478 | |
| 479 | root->markDirty(bits: QSGNode::DirtyForceUpdate); // Force matrix, clip, opacity and render list update. |
| 480 | |
| 481 | if (m_recursive) |
| 482 | markDirtyTexture(); // Continuously update if 'live' and 'recursive'. |
| 483 | } |
| 484 | |
| 485 | QImage QSGRhiLayer::toImage() const |
| 486 | { |
| 487 | if (!m_texture) |
| 488 | return QImage(); |
| 489 | |
| 490 | QRhiCommandBuffer *cb = m_context->currentFrameCommandBuffer(); |
| 491 | QRhiResourceUpdateBatch *resourceUpdates = m_rhi->nextResourceUpdateBatch(); |
| 492 | QRhiReadbackResult result; |
| 493 | QRhiReadbackDescription readbackDesc(m_texture); |
| 494 | resourceUpdates->readBackTexture(rb: readbackDesc, result: &result); |
| 495 | |
| 496 | cb->resourceUpdate(resourceUpdates); |
| 497 | |
| 498 | // Inefficient but what can you do. We need the results right away. This is |
| 499 | // not something that occurs in a normal rendering process anyway. (used by |
| 500 | // QQuickItem's grabToImage). |
| 501 | m_rhi->finish(); |
| 502 | |
| 503 | if (result.data.isEmpty()) { |
| 504 | qWarning(msg: "Layer grab failed" ); |
| 505 | return QImage(); |
| 506 | } |
| 507 | |
| 508 | // There is little room for negotiation here, the texture is one of the formats from setFormat. |
| 509 | // Also, Quick is always premultiplied alpha. |
| 510 | QImage::Format imageFormat = QImage::Format_RGBA8888_Premultiplied; |
| 511 | if (m_format == QRhiTexture::RGBA16F) |
| 512 | imageFormat = QImage::Format_RGBA16FPx4_Premultiplied; |
| 513 | else if (m_format == QRhiTexture::RGBA32F) |
| 514 | imageFormat = QImage::Format_RGBA32FPx4_Premultiplied; |
| 515 | |
| 516 | const uchar *p = reinterpret_cast<const uchar *>(result.data.constData()); |
| 517 | return QImage(p, result.pixelSize.width(), result.pixelSize.height(), imageFormat).flipped(); |
| 518 | } |
| 519 | |
| 520 | QRectF QSGRhiLayer::normalizedTextureSubRect() const |
| 521 | { |
| 522 | return QRectF(m_mirrorHorizontal ? 1 : 0, |
| 523 | m_mirrorVertical ? 0 : 1, |
| 524 | m_mirrorHorizontal ? -1 : 1, |
| 525 | m_mirrorVertical ? 1 : -1); |
| 526 | } |
| 527 | |
| 528 | #include "moc_qsgrhilayer_p.cpp" |
| 529 | |