GPU 预处理单 buffer 随帧切换 Created-by: Phooyau Commit-by: Phooyau Merged-by: openharmony_ci Description: **Description:** GPU 预处理单 buffer 随帧切换 **Issue number:** https://gitcode.com/openharmony/graphic_graphic_2d/issues/25672 **Test & Result:** OK **CodeCheck:** <table> <thead> <tr> <th>类型</th> <th>自检项</th> <th>自检结果</th> </tr> </thead> <tbody> <tr> <td rowspan="2"><strong>多线程</strong></td> <td>在类的成员变量中定义了vector/map/list等容器类型,且在多个成员函数中有操作时,需要加锁保护</td> <td>自检结果:OK</td> </tr> <tr> <td>定义全局变量,在多个函数中都有操作时,需要加锁保护</td> <td>自检结果:OK</td> </tr> <tr> <td rowspan="4"><strong>内存操作</strong></td> <td>调用外部接口时,确认是否对返回值做了判空判断,尤其外部接口返回了nullptr的情况,避免进程崩溃</td> <td>自检结果:OK</td> </tr> <tr> <td>内存操作优先使用安全函数,并检查其返回值</td> <td>自检结果:OK</td> </tr> <tr> <td>注意每个异常退出流程,是否都已经将资源释放(推荐使用RAII)</td> <td>自检结果:OK</td> </tr> <tr> <td>隐式内存分配场景:realpath、ReadParcelable序列化、cJSON相关函数时等,需主动释放或使用智能指针</td> <td>自检结果:OK</td> </tr> <tr> <td rowspan="3"><strong>外部输入</strong></td> <td>所有外部输入均不可信,需判断外部输入是否直接作为内存分配的大小,数组下标、循环条件、SQL查询等</td> <td>自检结果:OK</td> </tr> <tr> <td>注意外部字符串数据有无尾0</td> <td>自检结果:OK</td> </tr> <tr> <td>外部输入的路径不可信,需使用realpath做标准化处理,并判断路径的合法性</td> <td>自检结果:OK</td> </tr> <tr> <td><strong>敏感信息</strong></td> <td>注意日志中打印敏感信息需匿名化</td> <td>自检结果:OK</td> </tr> <tr> <td><strong>数学运算</strong></td> <td>代码中是否混合了加减乘除等运算,需检查是否可能导致整数溢出或符号翻转</td> <td>自检结果:OK</td> </tr> <tr> <td><strong>初始化</strong></td> <td>类成员、局部变量使用前需初始化</td> <td>自检结果:OK</td> </tr> <tr> <td><strong>权限管理</strong></td> <td>作为系统服务对外提供了接口(或RSCmd),是否做了权限保护和校验,只允许申请了权限的应用访问</td> <td>自检结果:OK</td> </tr> </tbody> </table> ### L0新增用例自检结果 - [ ] 是,有新增L0用例,且完成自检 - [ ] 否 See merge request: openharmony/graphic_graphic_surface!1400
Surface
Introduction
A surface is used to manage and transfer the shared memory of graphics and media. Specific use cases include graphics display and composition, and media playback and recording.
A surface transfers data across processes through control structures such as Inter-Process Communication (IPC) handles (with copies), and transfers graphics and media data (zero-copy) through shared memory. The following figure shows the position where a surface works in the system architecture. The elements in green blocks are surface buffers.
Figure 1 Position of a surface (known as Screen buffer in the following figure) in the system architecture

Directory Structure
/foundation/graphic/graphic_surface
├── surface # Framework code
│ ├── include # Header
│ ├── src # Source Code
│ └── test # Test code
│ ├── fuzztest # Fuzzing
│ └── unittest # Unit testing
├── interfaces # APIs
│ ├── innerkits # APIs between modules
│ └── kits # External APIs
├── buffer_handle # widget storage
└── scoped_bytrace # widget storage
Compilation and Building
# Generate the surface.so file in the out directory of the product folder through GN compilation.
hb build graphic_surface
Description
Process Description
Take the interaction between Window Manager Service (WMS) and UI as an example. UI works as a producer, and WMS works as a consumer.
The producer obtains a buffer from the free queue, draws the UI content into the buffer, and places the buffer in the dirty queue.
The consumer obtains the buffer from the dirty queue, synthesizes graphics data, and places the buffer in the free queue again.
Figure 2 Surface rotation process

- A shared memory is used for data transfer, and the process of creating a surface of the first time should be responsible for task of managing the shared memory. If the process is abnormal and is not recycled, severe memory leakage occurs. Therefore, pay special attention to the process.
- Surfaces are generally used for cross-process transfers of large memory blocks (such as display data) in graphics or media. Especially, when continuous physical memory is used, the transfer rate can be greatly improved. It is not recommended that surfaces be used in small-memory transfer scenarios. Otherwise, memory fragmentation may occur, which affects the performance in typical scenarios.
Usage
For details, see lite WMS.