C
将图形显示在屏幕上
本章介绍如何实现平台接口,将像素渲染到屏幕上。它描述了如何为单个屏幕实现基本支持,该屏幕不包含硬件层且不支持硬件加速。有关该主题的详细信息,请参阅“实现硬件层支持”。
概述
Qt Quick Ultralite 核心会将 QML 场景转换为一系列渲染命令,并将它们合成到帧缓冲区中。 提供该帧缓冲区并将其呈现到屏幕上,是平台库的职责。如果目标硬件支持硬件加速图形处理,平台库还会为此提供相应的渲染命令实现。
实现基本图形功能
Qt Quick Ultralite 核心通过 `PlatformContext ` 实例与平台硬件进行通信。要在屏幕上显示内容,平台必须实现上下文中与图形相关的部分。首先需要实现的方法是 `PlatformContext::availableScreens`。
注意: 示例实现 假设仅存在一个屏幕,且没有额外的层。
static PlatformInterface::Screen platformScreens[] = { PlatformInterface::Screen(PlatformInterface::Size(ScreenWidth, ScreenHeight), ScreenColorFormat)}; Qul::PlatformInterface::Screen *ExamplePlatform::availableScreens(size_t *screenCount) const { *screenCount = sizeof(platformScreens) / sizeof(PlatformInterface::Screen); return platformScreens; }
PlatformContext::availableScreens 返回设备上可用屏幕的数组。 返回的PlatformInterface::Screen 实例提供了屏幕标识符、其像素尺寸及其颜色格式。它还会告知Qt Quick Ultralite核心该屏幕是否支持调整大小,这实际上意味着平台能否支持多种分辨率或帧缓冲区尺寸。在此示例中,您依赖于Screen 构造函数的默认行为,即不支持调整大小。
如果您的设备支持多屏幕,您应在构造函数中为每个屏幕提供一个名称作为标识符。
接下来,实现 `PlatformContext::initializeDisplay ` 方法,该方法会在应用程序需要使用的每个屏幕上被调用一次。如果屏幕支持调整大小,它将根据根 QML 项的大小自动调整自身大小。基于屏幕大小,平台库现在可以初始化显示硬件。
void ExamplePlatform::initializeDisplay(const PlatformInterface::Screen *screen) { // const int screenIndex = screen - platformScreens; // initLcd(screenIndex, screen->size().width(), screen->size().height()); // initTouchInput(screenIndex); }
你还需要提供用于渲染的帧缓冲区。如果屏幕尺寸固定,可以像这样静态分配它们:
// Assuming we use 32bpp framebuffers static const int BytesPerPixel = 4; unsigned char framebuffer[2][BytesPerPixel * ScreenWidth * ScreenHeight] __attribute__((section(".framebufferSection"))); static int backBufferIndex = 0;
通常会使用两个帧缓冲区以获得更好的性能,并在显示硬件需要时直接从内存中反复刷新。其中一个是后缓冲区,Qt Quick Ultralite 核心会将下一帧渲染到该缓冲区中;另一个是前缓冲区,其内容将显示在屏幕上。 若要报告正在使用此类缓冲机制,请从PlatformContext::frameBufferingType 函数中返回FlippedDoubleBuffering 。变量backBufferIndex 用于跟踪当前哪个帧缓冲区被用作后缓冲区。
FrameBufferingType ExamplePlatform::frameBufferingType(const PlatformInterface::LayerEngine::ItemLayer *) const { return FlippedDoubleBuffering; }
Qt Quick Ultralite 内核会根据PlatformContext::frameBufferingType 返回的值来判断,是否可以仅对屏幕中发生变化的部分进行部分渲染更新,而不是每帧都重绘整个帧缓冲区。
注意:某些 自定义架构可能完全不使用帧缓冲区,例如使用命令缓冲区来及时更新显示。在这种情况下,必须将Platform::OtherBuffering 返回,以告知Qt Quick Ultralite 内核,每帧都必须进行全屏重绘。
某些平台可能连容纳单个完整帧缓冲区所需的内存都不够。使用部分帧缓冲区可以显著降低应用程序的内存需求,但对于复杂的应用程序,可能会导致性能下降并产生潜在的画面撕裂现象。 在这种情况下,必须返回Platform::PartialBuffering ,以通知Qt Quick Ultralite内核应将部分更新拆分以适应部分帧缓冲区的大小。有关更多信息,请参阅《实现部分帧缓冲支持》。
除非选择了部分缓冲,否则Qt Quick Ultralite内核会在每个帧中,每当屏幕需要视觉更新时,分别调用一次PlatformContext::beginFrame 和PlatformContext::endFrame 。
beginFrame 返回一个PlatformInterface::DrawingDevice 实例,其中包含:
- 像素格式以及屏幕或图层尺寸,
- 指向待渲染帧缓冲区的指针,
- 每行字节数,
- 以及用于向缓冲区渲染的绘图引擎。
当仅使用Qt Quick Ultralite中内置的默认软件渲染(无任何硬件加速)时,可直接使用普通的PlatformInterface::DrawingEngine 。否则,必须实现一个继承自PlatformInterface::DrawingEngine 的自定义绘图引擎子类。相关内容将在后文详细说明。
PlatformInterface::DrawingDevice *ExamplePlatform::beginFrame(const PlatformInterface::LayerEngine::ItemLayer *layer, const PlatformInterface::Rect &rect, int refreshInterval) { static Qul::PlatformInterface::DrawingEngine drawingEngine; requestedRefreshInterval = refreshInterval; // Wait until the back buffer is free, i.e. no longer held by the display waitForBufferFlip(); // A pointer to the back buffer uchar *bits = framebuffer[backBufferIndex]; static PlatformInterface::DrawingDevice buffer(Qul::PixelFormat_RGB32, PlatformInterface::Size(ScreenWidth, ScreenHeight), bits, ScreenWidth * BytesPerPixel, // Bytes per line &drawingEngine); buffer.setBits(bits); return &buffer; }
waitForBufferFlip 调用该方法是为了确保显示控制器已释放后缓冲区(即上一帧的前缓冲区),且该缓冲区已准备好接受渲染。其具体实现方法将在后文关于presentFrame 的章节中进行说明。
PlatformContext::endFrame 在完成特定屏幕的帧渲染后调用此函数。对于多屏幕或多图层的情况,某些平台可能需要在此处刷新硬件加速混合单元的命令缓冲区,但在大多数情况下可以留空。
void ExamplePlatform::endFrame(const PlatformInterface::LayerEngine::ItemLayer *layer) {}
完成某个屏幕的渲染后,Qt Quick Ultralite 内核会调用PlatformContext::presentFrame 函数。此时,平台应将刚渲染好的缓冲区显示在屏幕上。 该示例采用双缓冲技术,因此将后缓冲区的指针传递给显示设备,并交换前缓冲区和后缓冲区,以便下一帧渲染到前一帧的前缓冲区中。
FrameStatistics ExamplePlatform::presentFrame(const PlatformInterface::Screen *screen, const PlatformInterface::Rect &rect) { waitForRefreshInterval(); // Update the framebuffer address // LCD_SetBufferAddr(framebuffer[backBufferIndex]); // Now the front and back buffers are swapped if (backBufferIndex == 0) backBufferIndex = 1; else backBufferIndex = 0; return FrameStatistics(); }
presentFrame 函数调用waitForRefreshInterval ,以便在收到Qt Quick Ultralite内核的请求时降低刷新率。这需要记录自上次调用presentFrame 以来发生的刷新次数,并等待该计数达到请求的刷新间隔。
static void waitForRefreshInterval() { if (refreshCount < requestedRefreshInterval) { uint64_t startTime = getPlatformInstance()->currentTimestamp(); while (refreshCount < requestedRefreshInterval) { // The device may yield or go into sleep mode } idleTimeWaitingForDisplay += getPlatformInstance()->currentTimestamp() - startTime; } refreshCount = 0; }
这假设还存在一个用于跟踪屏幕刷新次数的中断处理程序:
static volatile int refreshCount = 1; volatile unsigned int currentFrame = 0; // Note: This line incrementing the refreshCount will need to be moved to the // actual interrupt handler for the display available on the target platform. It // needs to be called once per vertical refresh of the display, to keep track of // how many refreshes have happened between calls to presentFrame in order to // support custom refresh intervals. On some implementations this can be done // using a so called "line event". void LCD_RefreshInterruptHandler() { ++refreshCount; // currentFrame is only needed if the layer backend is used ++currentFrame; }
最后,presentFrame 可指示显示控制器从后缓冲区(back buffer)开始刷新,而非当前持有的前缓冲区(front buffer)。该操作以异步方式进行,因为显示控制器可能仍在扫描前缓冲区以进行显示。 因此,必须将waitingForBufferFlip 设置为true,并使用中断处理程序来接收显示控制器的通知,以便在旧的前缓冲区被释放且可作为新的后缓冲区用于绘制时及时响应:
static volatile bool waitingForBufferFlip = false; static uint32_t idleTimeWaitingForDisplay = 0; static void waitForBufferFlip() { // Has there already been a buffer flip? if (!waitingForBufferFlip) return; const uint64_t startTime = getPlatformInstance()->currentTimestamp(); while (waitingForBufferFlip) { // The device may yield or go into sleep mode } idleTimeWaitingForDisplay = getPlatformInstance()->currentTimestamp() - startTime; } // Note: This line clearing waitingForBufferFlip will need to be moved to the // actual interrupt handler for the display available on the target platform. // It's needed to inform about when the buffer address used to scan out pixels // to the display has been updated, making the buffer free in order to start // drawing the next frame. void LCD_BufferFlipInterruptHandler() { waitingForBufferFlip = false; }
PlatformContext::presentFrame 应直接返回一个默认构造的FrameStatistics 值。
初始化图形引擎
在进行任何图形渲染之前,必须先初始化图形渲染引擎的内部组件。请将此初始化调用添加到前几章中实现的 `Initializing platform` 函数中。
根据帧缓冲区的颜色深度,应调用您将要使用的颜色格式的各个函数:
- Qul::PlatformInterface::initializeRgb332CpuFallbackDrawingEngine()
- Qul::PlatformInterface::initializeRgb16CpuFallbackDrawingEngine()
- Qul::PlatformInterface::initializeArgb4444CpuFallbackDrawingEngine()
- Qul::PlatformInterface::initializeRgb888CpuFallbackDrawingEngine()
- Qul::PlatformInterface::initializeBgr888CpuFallbackDrawingEngine()
- Qul::PlatformInterface::initializeRgb32CpuFallbackDrawingEngine()
- Qul::PlatformInterface::initializeArgb32CpuFallbackDrawingEngine()
这将初始化Qt Quick Ultralite内核中的基于CPU的备用绘制引擎。除非平台本身提供了完整的虚拟DrawingEngine API(且不依赖于默认实现),或者显式使用了DrawingEngine::fallbackDrawingEngine() ,否则此操作是必要的。
void ExamplePlatform::initializePlatform() { Qul::PlatformInterface::initializeRgb32CpuFallbackDrawingEngine(); ...
构建并烧录到设备后,应会显示类似于下图的动画。
至此,您已完成移植指南的第二阶段,并可提交当前状态。下一阶段将处理来自屏幕的触摸输入。
在某些 Qt 许可证下可用。
了解更多信息。