是否应该在渲染调用之间缓存QPainter?

时间:2017-01-03 06:44:57

标签: qt

查看QT代码,似乎QPainter正试图通过QWidget::sharedPainter()重用现有的QPainter实例(参见QPainterPrivate :: attachPainterPrivate)。出于某种原因,虽然此sharedPainter()仅在单个渲染过程中设置,然后再次重置为null。

QPainter和相关QPaintEngine的预期使用寿命是多少?是应该在每个应用程序生命周期创建一次还是每次渲染调用创建一次?

QPaintEngine :: begin()怎么样?是应该为每个渲染过程调用还是每个引擎创建一次?

查看来自QGLPaintEngineEx2的代码,特别是QPaintEngine :: begin()可能包含一些代价高昂的初始化:

#if !defined(QT_OPENGL_ES_2)
    QGLExtensionMatcher extensions; // <- calls glGetString for the extension strings
    d->hasCompatibilityExtension = extensions.match("GL_ARB_compatibility");

    bool success = qt_resolve_version_2_0_functions(d->ctx)
                   && qt_resolve_buffer_extensions(d->ctx)
                   && (!QGLFramebufferObject::hasOpenGLFramebufferObjects()
                       || qt_resolve_framebufferobject_extensions(d->ctx));
    Q_ASSERT(success);
    Q_UNUSED(success);
#endif

    d->shaderManager = new QGLEngineShaderManager(d->ctx); // Dirties the shader manager

    glDisable(GL_STENCIL_TEST);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_SCISSOR_TEST);

#if !defined(QT_OPENGL_ES_2)
    glDisable(GL_MULTISAMPLE);
#endif

    d->glyphCacheType = QFontEngineGlyphCache::Raster_A8;

#if !defined(QT_OPENGL_ES_2)
#if defined(Q_WS_WIN)
    if (qt_cleartype_enabled
        && (fontSmoothingApproximately(1.0) || fontSmoothingApproximately(2.1)))
#endif
#if defined(Q_WS_MAC)
    if (qt_applefontsmoothing_enabled)
#endif
        d->glyphCacheType = QFontEngineGlyphCache::Raster_RGBMask;
#endif

#if defined(QT_OPENGL_ES_2)
    // OpenGL ES can't switch MSAA off, so if the gl paint device is
    // multisampled, it's always multisampled.
    d->multisamplingAlwaysEnabled = d->device->format().sampleBuffers();
#else
    d->multisamplingAlwaysEnabled = false;
#endif

    return true;
}

1 个答案:

答案 0 :(得分:2)

应在使用点创建QPainter。你永远不应该缓存它们,或让它们坚持超越油漆事件! Qt这是一个实现细节。