本页内容

C

硬件加速混合

要实现硬件加速,请继承PlatformInterface::DrawingEngine 类,并重写平台能够加速的函数。

class ExampleDrawingEngine : public PlatformInterface::DrawingEngine
{
public:
    void blendRect(PlatformInterface::DrawingDevice *drawingDevice,
                   const PlatformInterface::Rect &rect,
                   PlatformInterface::Rgba32 color,
                   BlendMode blendMode) QUL_DECL_OVERRIDE;

    void blendRoundedRect(PlatformInterface::DrawingDevice *drawingDevice,
                          const PlatformInterface::Rect &rect,
                          const PlatformInterface::Rect &clipRect,
                          PlatformInterface::Rgba32 color,
                          int radius,
                          BlendMode blendMode) QUL_DECL_OVERRIDE;

    void blendImage(PlatformInterface::DrawingDevice *drawingDevice,
                    const PlatformInterface::Point &pos,
                    const PlatformInterface::Texture &source,
                    const PlatformInterface::Rect &sourceRect,
                    int sourceOpacity,
                    BlendMode blendMode) QUL_DECL_OVERRIDE;

    void blendAlphaMap(PlatformInterface::DrawingDevice *drawingDevice,
                       const PlatformInterface::Point &pos,
                       const PlatformInterface::Texture &source,
                       const PlatformInterface::Rect &sourceRect,
                       PlatformInterface::Rgba32 color,
                       BlendMode blendMode) QUL_DECL_OVERRIDE;

    void blendTransformedImage(PlatformInterface::DrawingDevice *drawingDevice,
                               const PlatformInterface::Transform &transform,
                               const PlatformInterface::RectF &destinationRect,
                               const PlatformInterface::Texture &source,
                               const PlatformInterface::RectF &sourceRect,
                               const PlatformInterface::Rect &clipRect,
                               int sourceOpacity,
                               BlendMode blendMode);
    void blendTransformedAlphaMap(PlatformInterface::DrawingDevice *drawingDevice,
                                  const PlatformInterface::Transform &transform,
                                  const PlatformInterface::RectF &destinationRect,
                                  const PlatformInterface::Texture &source,
                                  const PlatformInterface::RectF &sourceRect,
                                  const PlatformInterface::Rect &clipRect,
                                  PlatformInterface::Rgba32 color,
                                  BlendMode blendMode);

    void synchronizeForCpuAccess(PlatformInterface::DrawingDevice *drawingDevice,
                                 const PlatformInterface::Rect &rect) QUL_DECL_OVERRIDE;

    PlatformInterface::DrawingEngine::Path *allocatePath(const PlatformInterface::PathData *pathData,
                                                         PlatformInterface::PathFillRule fillRule) QUL_DECL_OVERRIDE;

    void setStrokeProperties(PlatformInterface::DrawingEngine::Path *path,
                             const PlatformInterface::StrokeProperties &strokeProperties) QUL_DECL_OVERRIDE;

    void blendPath(PlatformInterface::DrawingDevice *drawingDevice,
                   PlatformInterface::DrawingEngine::Path *path,
                   const PlatformInterface::Transform &transform,
                   const PlatformInterface::Rect &clipRect,
                   const PlatformInterface::Brush *fillBrush,
                   const PlatformInterface::Brush *strokeBrush,
                   int sourceOpacity,
                   PlatformInterface::DrawingEngine::BlendMode blendMode) QUL_DECL_OVERRIDE;

    PlatformInterface::RenderHints supportedRenderHints() const QUL_DECL_OVERRIDE;
};

如果未重写DrawingEngine::blendRect 、DrawingEngine::blendRoundedRect 、DrawingEngine::blendImage 、DrawingEngine::blendAlphaMap 、DrawingEngine::blendTransformedImage 或DrawingEngine::blendTransformedAlphaMap 中的任何函数,则将使用默认实现。 它在调用DrawingDevice::fallbackDrawingEngine 上的相应回退实现之前,会先调用synchronizeForCpuAccess()。如果平台能够对某些混合函数进行部分加速(例如仅针对给定的混合模式或不透明度参数),则可以自行使用fallbackDrawingEngine 作为特定参数集的回退方案。

警告:若 在使用fallbackDrawingEngine 时发生崩溃,可能是因为您在initializePlatform()中未调用PlatformInterface::initializeArgb32CpuFallbackDrawingEngine()或类似方法。

注意:如果 blendImage 函数通过默认实现或使用fallbackDrawingEngine 采用软件渲染,则源图像被假定为 ARGB32_Premultiplied 格式。若要启用 blendImage 对其他格式的混合,请将QUL_PLATFORM_DEFAULT_RESOURCE_ALPHA_OPTIONS设置为Always 。

blend 函数的实际实现可能会因硬件加速 API 而存在显著差异。为了便于您实现这些函数,此处使用了一个模拟的硬件加速 API 进行演示。例如,DrawingEngine::blendRect 的实现可能如下所示:

void ExampleDrawingEngine::blendRect(PlatformInterface::DrawingDevice *drawingDevice,
                                     const PlatformInterface::Rect &rect,
                                     PlatformInterface::Rgba32 color,
                                     BlendMode blendMode)
{
    // Implement rectangle blending here

    // If only blitting is supported by the hardware, this is how to use the
    // fallback drawing engine for the blending path.
    if (color.alpha() != 255 && blendMode != BlendMode_SourceOver) {
        synchronizeForCpuAccess(drawingDevice, rect);
        drawingDevice->fallbackDrawingEngine()->blendRect(drawingDevice, rect, color, blendMode);
        return;
    }

    // HW_SetColor(1.0f, 1.0f, 1.0f, sourceOpacity * (1 / 256.0f));

    // HW_BlitRect(toHwRect(rect));
}

此示例代码假设硬件加速 API 不支持矩形混合,目的是演示如何利用备用绘制引擎来完成此任务。

否则,将设置颜色并发出绘制矩形的调用。

DrawingEngine::blendRoundedRect 在对具有圆角的矩形进行混合时会调用该函数。如果提供的裁剪矩形小于待混合的矩形,平台实现还必须根据该裁剪矩形进行裁剪。具体实现示例请参见:

void ExampleDrawingEngine::blendRoundedRect(PlatformInterface::DrawingDevice *drawingDevice,
                                            const PlatformInterface::Rect &rect,
                                            const PlatformInterface::Rect &clipRect,
                                            PlatformInterface::Rgba32 color,
                                            int radius,
                                            BlendMode blendMode)
{
    // Implement rectangle blending here

    // HW_SetColor(1.0f, 1.0f, 1.0f, sourceOpacity * (1 / 256.0f));
    // HW_SetClip(clipRect.x(), clipRect.y(), clipRect.width(), clipRect.height());

    // HW_BlitRoundRect(toHwRect(rect), radius);

    // HW_SetClip(0, 0, screen->width(), screen->height());
}

同样地,在混合图像和透明度贴图时,你还需要以某种方式将纹理布局和数据信息告知硬件加速 API。该示例假设存在一个负责处理此事的 `bindTexture ` 函数。该函数必须由平台移植版本根据特定硬件加速 API 的需求进行实现。

对于blendImage 函数,它包含一个源矩形、一个目标点,以及范围在0到256(含)之间的源不透明度。其实现可能如下所示:

void ExampleDrawingEngine::blendImage(PlatformInterface::DrawingDevice *drawingDevice,
                                      const PlatformInterface::Point &pos,
                                      const PlatformInterface::Texture &source,
                                      const PlatformInterface::Rect &sourceRect,
                                      int sourceOpacity,
                                      BlendMode blendMode)
{
    // Fall back to default CPU drawing engine for pixel formats that can't be
    // blended with hardware acceleration
    if (source.format() == Qul::PixelFormat_RGB332 || source.format() == PixelFormat_RLE_ARGB32
        || source.format() == PixelFormat_RLE_ARGB32_Premultiplied || source.format() == PixelFormat_RLE_RGB32
        || source.format() == PixelFormat_RLE_RGB888) {
        DrawingEngine::blendImage(drawingDevice, pos, source, sourceRect, sourceOpacity, blendMode);
        return;
    }

    // Implement image blending here

    // HW_SetBlendMode(toHwBlendMode(blendMode));

    // bindTexture(source);

    // HW_SetColor(1.0f, 1.0f, 1.0f, sourceOpacity * (1 / 256.0f));

    // if (drawingDevice->testRenderHint(PlatformInterface::RenderHint::...)) {
    // Take the render hint into account in this blending operation
    // HW_Set...()
    // }

    // const Rect destinationRect(pos, sourceRect.size());
    // HW_BlendTexture(toHwRect(sourceRect), toHwRect(destinationRect));
}

接下来是blendAlphaMap ,它与上述功能非常相似,主要区别在于纹理的像素格式为PixelFormat_Alpha1 或PixelFormat_Alpha8 ,这意味着每个像素分别使用1位或1字节来表示不透明度。对于每个像素,其不透明度值必须乘以给定的color ,然后根据blendMode 进行混合或复制。

void ExampleDrawingEngine::blendAlphaMap(PlatformInterface::DrawingDevice *drawingDevice,
                                         const PlatformInterface::Point &pos,
                                         const PlatformInterface::Texture &source,
                                         const PlatformInterface::Rect &sourceRect,
                                         PlatformInterface::Rgba32 color,
                                         BlendMode blendMode)
{
    // Implement alpha map blending here

    // HW_SetBlendMode(toHwBlendMode(blendMode));

    // bindTexture(source);

    // const float inv = 1 / 255.0f;
    // HW_SetColor(color.red() * inv, color.green() * inv, color.blue() * inv, color.alpha() * inv);

    // const PlatformInterface::Rect destinationRect(pos, sourceRect.size());
    // HW_BlendTexture(toHwRect(sourceRect), toHwRect(destinationRect));
}

接下来是图像与Alpha贴图的变换混合。如果平台支持对此进行加速,blendTransformedImage 的实现可能如下所示:

void ExampleDrawingEngine::blendTransformedImage(PlatformInterface::DrawingDevice *drawingDevice,
                                                 const PlatformInterface::Transform &transform,
                                                 const PlatformInterface::RectF &destinationRect,
                                                 const PlatformInterface::Texture &source,
                                                 const PlatformInterface::RectF &sourceRect,
                                                 const PlatformInterface::Rect &clipRect,
                                                 int sourceOpacity,
                                                 BlendMode blendMode)
{
    // Fall back to default CPU drawing engine for pixel formats that can't be
    // blended with hardware acceleration
    if (source.format() == Qul::PixelFormat_RGB332 || source.format() == PixelFormat_RLE_ARGB32
        || source.format() == PixelFormat_RLE_ARGB32_Premultiplied || source.format() == PixelFormat_RLE_RGB32
        || source.format() == PixelFormat_RLE_RGB888) {
        DrawingEngine::blendTransformedImage(drawingDevice,
                                             transform,
                                             destinationRect,
                                             source,
                                             sourceRect,
                                             clipRect,
                                             sourceOpacity,
                                             blendMode);
        return;
    }

    // Implement transformed image blending here

    // float matrix[16];
    // toHwMatrix(transform, &matrix);

    // HW_SetTransformMatrix(matrix);
    // HW_SetClip(clipRect.x(), clipRect.y(), clipRect.width(), clipRect.height());

    // HW_SetBlendMode(toHwBlendMode(blendMode));

    // bindTexture(source);

    // HW_SetColor(1.0f, 1.0f, 1.0f, sourceOpacity * (1 / 256.0f));

    // const Qul::PlatformInterface::RenderHints &renderHints = drawingDevice->renderHints();

    // if(renderHints.testFlag(PlatformInterface::RenderHint::...) {
    // Take the render hint into account in this blending operation
    // HW_Set...()
    // }

    // HW_BlendTexture(toHwRect(sourceRect), toHwRect(destinationRect));

    // HW_SetClip(0, 0, screen->width(), screen->height());
    // HW_SetTransformIdentity();
}

除了设置裁剪矩形外,你还需要将给定的变换转换为硬件加速 API 可接受的矩阵表示形式。如果使用 4x4 矩阵,转换过程可能类似于示例toHwMatrix 所示:

static void toHwMatrix(const PlatformInterface::Transform &transform, float *matrix)
{
    matrix[0] = transform.m11();
    matrix[1] = transform.m12();
    matrix[2] = 0;
    matrix[3] = 0;
    matrix[4] = transform.m21();
    matrix[5] = transform.m22();
    matrix[6] = 0;
    matrix[7] = 0;
    matrix[8] = 0;
    matrix[9] = 0;
    matrix[10] = 1;
    matrix[11] = 0;
    matrix[12] = transform.dx();
    matrix[13] = transform.dy();
    matrix[14] = 0;
    matrix[15] = 1;
}

blendTransformedAlphaMap 示例代码与blendTransformedImage 非常相似,唯一的区别在于它考虑了颜色因素——正如上文blendAlphaMap 示例代码中所示。有关blendTransformedAlphaMap 示例代码,请参阅platform/boards/qt/example-baremetal/platform_context.cpp 文件。

同步 CPU 访问

由于硬件混合通常以异步方式进行,如果发生任何基于 CPU 的帧缓冲区读写操作,Qt Quick Ultralite 内核会调用DrawingEngine::synchronizeForCpuAccess 。该函数需要与硬件加速混合单元进行同步,以确保每个待处理的混合命令都已完全写入帧缓冲区。 以下是使用我们的模拟硬件加速 API 时可能的实现方式:

void ExampleDrawingEngine::synchronizeForCpuAccess(PlatformInterface::DrawingDevice *drawingDevice,
                                                   const PlatformInterface::Rect &rect)
{
    // HW_SyncFramebufferForCpuAccess();

    framebufferAccessedByCpu = true;

    unsigned char *backBuffer = framebuffer[backBufferIndex];
    for (int i = 0; i < rect.height(); ++i) {
        unsigned char *pixels = backBuffer + (ScreenWidth * (rect.y() + i) + rect.x()) * BytesPerPixel;
        // CleanInvalidateDCache_by_Addr(pixels, rect.width() * BytesPerPixel);
    }
}

在某些硬件上,可能还需要清空相关区域的数据缓存。这样可以确保混合单元执行的异步写入操作能被 CPU 完全感知。

此外,在某些平台上,synchronizeAfterCpuAccess 函数将非常有用。如果使用了任何CPU渲染回退机制,该函数会在允许显示控制器进行异步读取以访问内存之前,先使所有数据缓存失效:

static bool framebufferAccessedByCpu = false;

void synchronizeAfterCpuAccess(const PlatformInterface::Rect &rect)
{
    if (framebufferAccessedByCpu) {
        unsigned char *backBuffer = framebuffer[backBufferIndex];
        for (int i = 0; i < rect.height(); ++i) {
            unsigned char *pixels = backBuffer + (ScreenWidth * (rect.y() + i) + rect.x()) * BytesPerPixel;
            // CleanInvalidateDCache_by_Addr(pixels, rect.width() * BytesPerPixel);
        }
    }
}

此外,还需要实现另外两个函数,以确保Qt Quick Ultralite内核能够动态读写纹理数据。它们分别是PlatformContext::waitUntilAsyncReadFinished 和PlatformContext::flushCachesForAsyncRead 。

以下是它们的实现示例:

void ExamplePlatform::waitUntilAsyncReadFinished(const void * /*begin*/, const void * /*end*/)
{
    // HW_SyncFramebufferForCpuAccess();
}

void ExamplePlatform::flushCachesForAsyncRead(const void * /*addr*/, size_t /*length*/)
{
    // CleanInvalidateDCache_by_Addr(const_cast<void *>(addr), length);
}

waitUntilAsyncReadFinished 应确保在返回之前,从给定内存区域读取的任何异步操作均已完成。通常,该内存区域指代某些纹理数据,这些数据即将被覆盖。最简单的做法是与硬件加速的混合单元进行同步,以确保不再有待处理的纹理混合操作。

flushCachesForAsyncRead 确保 CPU 写入的任何更改均已刷新,以便后续的异步读取能获取正确且最新的内存数据。如果在给定硬件上需要此操作,则必须调用相关方法来无效化数据缓存。

以上内容涵盖了实现硬件加速混合所需的所有要素,以确保Qt Quick Ultralite用户界面在目标移植平台上流畅、高效地运行。

渲染提示

渲染提示用于告知绘图引擎在混合操作期间应应用哪些处理。例如,启用抗锯齿功能意味着引擎需要对边缘进行平滑处理。

要指定绘图引擎支持哪些渲染提示,应实现supportedRenderHints()) 方法。

PlatformInterface::RenderHints ExampleDrawingEngine::supportedRenderHints() const
{
    // Indicate the rendering hints that this DrawingEngine supports:
    // return PlatformInterface::RenderHints(PlatformInterface::RenderHint::.., ...);
    return PlatformInterface::RenderHints();
}

绘图引擎使用DrawingDevice::testRenderHint() 或DrawingDevice::renderHints() 查询渲染操作的有效提示。

void ExampleDrawingEngine::blendImage(PlatformInterface::DrawingDevice *drawingDevice,
                                      const PlatformInterface::Point &pos,
                                      const PlatformInterface::Texture &source,
                                      const PlatformInterface::Rect &sourceRect,
                                      int sourceOpacity,
                                      BlendMode blendMode)
{
    // Fall back to default CPU drawing engine for pixel formats that can't be
    // blended with hardware acceleration
    if (source.format() == Qul::PixelFormat_RGB332 || source.format() == PixelFormat_RLE_ARGB32
        || source.format() == PixelFormat_RLE_ARGB32_Premultiplied || source.format() == PixelFormat_RLE_RGB32
        || source.format() == PixelFormat_RLE_RGB888) {
        DrawingEngine::blendImage(drawingDevice, pos, source, sourceRect, sourceOpacity, blendMode);
        return;
    }

    // Implement image blending here

    // HW_SetBlendMode(toHwBlendMode(blendMode));

    // bindTexture(source);

    // HW_SetColor(1.0f, 1.0f, 1.0f, sourceOpacity * (1 / 256.0f));

    // if (drawingDevice->testRenderHint(PlatformInterface::RenderHint::...)) {
    // Take the render hint into account in this blending operation
    // HW_Set...()
    // }

    // const Rect destinationRect(pos, sourceRect.size());
    // HW_BlendTexture(toHwRect(sourceRect), toHwRect(destinationRect));
}

当 DrawingEngine 不支持某个渲染提示时,将使用DrawingDevice::fallbackDrawingEngine() 进行混合。

另请参阅 Qul::PlatformInterface::DrawingEngine::supportedRenderHints()、Qul::PlatformInterface::DrawingDevice::renderHints()、Qul::PlatformInterface::DrawingDevice::setRenderHint() 以及Qul::PlatformInterface::DrawingDevice::testRenderHint()。

在某些 Qt 许可证下可用。
了解更多。