3.10. sample_gpu_2d Instructions

sample_gpu_2d implements the following functions by calling the 2D GPU Interface:

  1. Rotation

  2. Rectangle Fill

  3. Alpha Blending

  4. Color Space Conversion

  5. Bit Block Transfer

  6. Cropping

  7. Stitching

  8. Scaling (Up and Down)

  9. Multi-source Alpha Blending

  10. External Memory to GPU Memory Conversion

Next, each function will be described in detail.

Note:

  • Performance testing code has been added to each sample. The script performance_test.sh can measure performance data for the corresponding interfaces in all samples.
    You can modify the resolution of interest in performance_test.sh before testing.

[Case copy                          ] [Input 1*1920*1080] [Output 1920*1080] [TestTimes   100] [TotalConsume 344412    us] [AverageConsume 3444   us] [FPS   290.3]
[Case format_convert                ] [Input 1*1920*1080] [Output 1920*1080] [TestTimes   100] [TotalConsume 313243    us] [AverageConsume 3132   us] [FPS   319.2]
[Case multi_source_alphablend       ] [Input 4*1920*1080] [Output 1920*1080] [TestTimes   100] [TotalConsume 541909    us] [AverageConsume 5419   us] [FPS   184.5]
[Case resize                        ] [Input 1*1920*1080] [Output 3840*2160] [TestTimes   100] [TotalConsume 1287105   us] [AverageConsume 12871  us] [FPS    77.7]
[Case resize                        ] [Input 1*1920*1080] [Output  960*540 ] [TestTimes   100] [TotalConsume 81144     us] [AverageConsume 811    us] [FPS  1232.4]
[Case stitch                        ] [Input 4*1920*1080] [Output 1920*1080] [TestTimes   100] [TotalConsume 351719    us] [AverageConsume 3517   us] [FPS   284.3]
[Case stitch                        ] [Input 2*1920*1080] [Output 1920*540 ] [TestTimes   100] [TotalConsume 176619    us] [AverageConsume 1766   us] [FPS   566.2]
[Case alphablend                    ] [Input 1*1920*1080] [Output 1920*1080] [TestTimes   100] [TotalConsume 412188    us] [AverageConsume 4121   us] [FPS   242.6]
[Case crop                          ] [Input 1*3840*2160] [Output 1920*1080] [TestTimes   100] [TotalConsume 314460    us] [AverageConsume 3144   us] [FPS   318.0]
[Case rectangle_fill                ] [Input 1*1920*1080] [Output 1920*1080] [TestTimes   100] [TotalConsume 298156    us] [AverageConsume 2981   us] [FPS   335.4]
[Case rotation                      ] [Input 1*1920*1080] [Output 1920*1080] [TestTimes   100] [TotalConsume 609109    us] [AverageConsume 6091   us] [FPS   164.2]
  • Function description: Most samples use the function n2d_util_load_buffer_from_raw_file. This function is used to load image files without data headers. It does not mean loading the raw images output by the Camera Sensor.

3.10.1. sample_rotation

3.10.1.1. Function Overview

Function Description: sample_rotation performs a 90-degree clockwise rotation on an image.

Software Architecture

framework_all

Code Location and Directory Structure

  • Code Location: app/samples/platform_samples/sample_gpu_2d

  • Directory Structure:

sample_gpu_2d/
├── resource
│   ├── R5G6B5_640x640.bmp
│   ├── RGBA8888_640x480.bmp
│   ├── scenery01.bmp
│   ├── scenery02.bmp
│   ├── scenery03.bmp
│   └── scenery04.bmp
└── sample_rotation
    ├── Makefile
    └── rotation.c

API Workflow Explanation

The core steps for using the GPU 2D API are:

  1. Prepare source and destination buffers. The buffer structures are defined using n2d_buffer_t.

  2. Execute blit operations: Send commands to the command buffer.

  3. Execute commit operation: Submit pending commands in the command buffer to the GPU hardware and block until processing is complete.

The example program mainly has two branches:

  1. Performance test

  2. Sample program

Overall Workflow

app_common_complex

Performance Test Workflow

performance

Sample Workflow

sample_simple

3.10.1.2. Compilation and Deployment

Compilation

  • Enter the sample_rotation directory and run make to compile.

  • The output binary is rotation, located in the sample_rotation source directory.

  • For detailed compilation methods, refer to the Build Instructions section.

Program Deployment

  1. Upload the directories sample_rotation and resource to the development board’s /userdata directory.

  2. Enter the sample_rotation directory and run chmod +x rotation to grant execution permissions.

3.10.1.3. Execution

How to Run the Program

Run the program: ./rotation.
The program reads the file R5G6B5_640x640.bmp from the resource directory, rotates it, and saves the result to the current directory as R5G6B5_640x640_rotated.bmp.

Program Parameter Options

./rotation -h
Usage: rotation [OPTIONS]
Options:
  -m <mode>              Specify running mode (0: sample, 1: performance test)
  -c <image_width>       Specify image width (columns)
  -r <image_height>      Specify image height (rows)
  -i <iteration_number>  Specify number of iterations
  -h <help>              Show this help message
     For Example, performance test: ./rotation -m 1 -c 1920 -r 1080 -i 1000
     For Example, sample          : ./rotation (not need input param)

Examples:

  • ./rotation -m 1 -c 1920 -r 1080 -i 1000: Performance test mode, specifying resolution 1920×1080, runs 1000 times, and prints average execution time and FPS.

  • ./rotation: Rotates the input image 90 degrees clockwise.

Execution Results

Command:

./rotation

Log Output:

./rotation
Start !!!
Save file to [./R5G6B5_640x640_rotated.bmp].
Stop !!!

Result Description:
After reading the input file, the program rotates it 90 degrees clockwise and saves the result. The effect is as follows:

Input File:

rotation-input

Output File:

rotation-output

3.10.2. sample_rectangle_fill

3.10.2.1. Function Overview

Function Description: sample_rectangle_fill performs rectangle filling on an image.

Software Architecture

framework_all

Code Location and Directory Structure

  • Code Location: app/samples/platform_samples/sample_gpu_2d

  • Directory Structure:

sample_gpu_2d/
├── resource
│   ├── R5G6B5_640x640.bmp
│   ├── RGBA8888_640x480.bmp
│   ├── scenery01.bmp
│   ├── scenery02.bmp
│   ├── scenery03.bmp
│   └── scenery04.bmp
└── sample_rectangle_fill
    ├── Makefile
    └── rectangle_fill.c

API Workflow Explanation

The core steps for using the GPU 2D API are:

  1. Prepare source and destination buffers. The buffer structures are defined using n2d_buffer_t.

  2. Execute blit operations: Send commands to the command buffer.

  3. Execute commit operation: Submit pending commands in the command buffer to the GPU hardware and block until processing is complete.

The example program mainly has two branches:

  1. Performance test

  2. Sample program

Overall Workflow

app_common_simple

Performance Test Workflow

performance

Sample Workflow

rectangle_fill_sample

3.10.2.2. Compilation and Deployment

Compilation

  • Enter the sample_rectangle_fill directory and run make to compile.

  • The output binary is rectangle_fill, located in the sample_rectangle_fill source directory.

  • For detailed compilation methods, refer to the Build Instructions section.

Program Deployment

  1. Upload the directories sample_rectangle_fill and resource to the development board’s /userdata directory.

  2. Enter the sample_rectangle_fill directory and run chmod +x rectangle_fill to grant execution permissions.

3.10.2.3. Execution

How to Run the Program

Run the program: ./rectangle_fill. The program reads the file R5G6B5_640x640.bmp from the resource directory, fills rectangles, and saves the result to the current directory as R5G6B5_640x640_rectangle_fill.bmp.

Program Parameter Options

Usage: rectangle_fill [OPTIONS]
Options:
  -m <mode>              Specify running mode (0: sample, 1: performance test)
  -c <image_width>       Specify image width (columns)
  -r <image_height>      Specify image height (rows)
  -i <iteration_number>  Specify number of iterations
  -h <help>              Show this help message
     For Example, performance test: ./rectangle_fill -m 1 -c 1920 -r 1080 -i 1000
     For Example, sample          : ./rectangle_fill (not need input param)

Examples:

  • ./rectangle_fill -m 1 -c 1920 -r 1080 -i 1000: Performance test mode, specifying resolution 1920×1080, runs 1000 times, and prints average execution time and FPS.

  • ./rectangle_fill: Performs rectangle fill on the input image.

Execution Results

Command: ./rectangle_fill

Log Output:

./rectangle_fill
Start !!!
Save file to [./R5G6B5_640x640_rectangle_fill.bmp].
Stop !!!

Result Description:
After reading the input file, the program performs the following operations:

  1. Adds an opaque blue rectangle at the top-left corner.

  2. Adds a semi-transparent green rectangle at the top-right corner.

  3. Adds a transparent green rectangle at the bottom-left corner.

  4. Adds an opaque green rectangle at the bottom-right corner.

Input File:

rectangle_fill-input

Output File:

rectangle_fill-output

3.10.3. sample_alphablend

3.10.3.1. Function Overview

Function Description: sample_alphablend performs alpha blending on images.

Software Architecture

framework_all

Code Location and Directory Structure

  • Code Location: app/samples/platform_samples/sample_gpu_2d

  • Directory Structure:

sample_gpu_2d/
├── resource
│   ├── R5G6B5_640x640.bmp
│   ├── RGBA8888_640x480.bmp
│   ├── scenery01.bmp
│   ├── scenery02.bmp
│   ├── scenery03.bmp
│   └── scenery04.bmp
└── sample_alphablend
    ├── alphablend.c
    └── Makefile

API Workflow Explanation

The core steps for using the GPU 2D API are:

  1. Prepare source and destination buffers. The buffer structures are defined using n2d_buffer_t.

  2. Execute blit operations: Send commands to the command buffer.

  3. Execute commit operation: Submit pending commands in the command buffer to the GPU hardware and block until processing is complete.

The example program mainly has two branches:

  1. Performance test

  2. Sample program

Overall Workflow

app_common_simple

Performance Test Workflow

performance

Sample Workflow

alpha_sample

3.10.3.2. Compilation and Deployment

Compilation

  • Enter the sample_alphablend directory and run make to compile.

  • The output binary is alphablend, located in the sample_alphablend source directory.

  • For detailed compilation methods, refer to the Build Instructions section.

Program Deployment

  1. Upload the directory sample_alphablend to the development board’s /userdata directory.

  2. Enter the sample_alphablend directory and run chmod +x alphablend to grant execution permissions.

3.10.3.3. Execution

How to Run the Program

Run the program:

./alphablend

Program Parameter Options

Usage: alphablend [OPTIONS]
Options:
  -m <mode>              Specify running mode (0: sample, 1: performance test)
  -c <image_width>       Specify image width (columns)
  -r <image_height>      Specify image height (rows)
  -i <iteration_number>  Specify number of iterations
  -h <help>              Show this help message
     For Example, performance test: ./alphablend -m 1 -c 1920 -r 1080 -i 1000
     For Example, sample          : ./alphablend (not need input param)

Examples:

  • ./alphablend -m 1 -c 1920 -r 1080 -i 1000: Performance test mode, specifying resolution 1920×1080, runs 1000 times, and prints average execution time and FPS.

  • ./alphablend: Runs an example of alpha blending.

Execution Results

Command: ./alphablend

Log Output:

./alphablend
Start !!!
Stop !!!

Result Description:

  1. First, create two buffers of size 640×480 in N2D_BGRA8888 format.

  2. Draw a blue rectangle in one buffer and a red rectangle in the other. The rectangles overlap (to show blending effect).

  3. Iterate through all blending modes, varying the transparency in each mode.

  4. Save the results of different blending modes and transparency combinations to files (filenames are generated by combining blending mode and transparency parameters).

src_over Mode

src_over-0 src_over-1 src_over-2 src_over-3

src_over-4 src_over-5 src_over-6 src_over-7

src_in Mode

src_in-0 src_in-1 src_in-2 src_in-3

src_in-4 src_in-5 src_in-6 src_in-7

dst_over Mode

dst_over-0 dst_over-1 dst_over-2 dst_over-3

dst_over-4 dst_over-5 dst_over-6 dst_over-7

dst_in Mode

dst_in-0 dst_in-1 dst_in-2 dst_in-3

dst_in-4 dst_in-5 dst_in-6 dst_in-7

subtract Mode

subtract-0 subtract-1 subtract-2 subtract-3

subtract-4 subtract-5 subtract-6 subtract-7

additive Mode

additive-0 additive-1 additive-2 additive-3

additive-4 additive-5 additive-6 additive-7

3.10.4. sample_format_convert

3.10.4.1. Function Overview

Function Description: sample_format_convert performs color space conversion on images.

Software Architecture

framework_all

Code Location and Directory Structure

  • Code Location: app/samples/platform_samples/sample_gpu_2d

  • Directory Structure:

sample_gpu_2d/
├── resource
│   ├── R5G6B5_640x640.bmp
│   ├── RGBA8888_640x480.bmp
│   ├── bit_filter_1920_1080.bmp
│   ├── nv12_1920x1080.yuv
│   ├── scenery01.bmp
│   ├── scenery02.bmp
│   ├── scenery03.bmp
│   └── scenery04.bmp
└── sample_format_convert
    ├── format_convert.c
    └── Makefile

API Workflow Explanation

The core steps for using the GPU 2D API are:

  1. Prepare source and destination buffers. The buffer structures are defined using n2d_buffer_t.

  2. Execute blit operations: Send commands to the command buffer.

  3. Execute commit operation: Submit pending commands in the command buffer to the GPU hardware and block until processing is complete.

The example program mainly has two branches:

  1. Performance test

  2. Sample program

Overall Workflow

app_common_simple

Performance Test Workflow

performance

Sample Workflow

format_convert_sample

3.10.4.2. Compilation and Deployment

Compilation

  • Enter the sample_format_convert directory and run make to compile.

  • The output binary is format_convert located in the sample_format_convert source directory.

  • For detailed compilation methods, refer to the Compilation Method section.

Program Deployment

  1. Upload the directories sample_format_convert and resource to the development board’s /userdata directory.

  2. Enter the sample_format_convert directory and run chmod +x format_convert to grant execution permissions.

3.10.4.3. Execution

How to Run the Program

Run the program: ./format_convert

Program Parameter Options

Usage: format_convert [OPTIONS]
Options:
  -m <mode>              Specify running mode (0: sample, 1: performance test)
  -c <image_width>       Specify image width (columns)
  -r <image_height>      Specify image height (rows)
  -i <iteration_number>  Specify number of iterations
  -h <help>              Show this help message
     For Example, performance test: ./format_convert -m 1 -c 1920 -r 1080 -i 1000
     For Example, sample          : ./format_convert (not need input param)

Examples:

  • ./format_convert -m 1 -c 1920 -r 1080 -i 1000: Performance test mode, specifying resolution 1920×1080, runs 1000 times, and prints average execution time and FPS.

  • ./format_convert: Runs an example of color space conversion.

Execution ResultsExecute command:

./format_convert

Running log:

./format_convert
Start !!!
Stop !!!

Effect description:

  1. The program reads the file nv12_1920x1080.yuv from the resource directory and performs color space conversion among different formats.

  2. First, the input image is converted into one of the following formats:

  3. Then, it is converted back to the input file’s original format (NV12) and saved as a file.

n2d_buffer_format_t yuv_format[] =
{
    N2D_YUYV,
    N2D_UYVY,
    N2D_YV12,
    N2D_I420,
    N2D_NV12,
    N2D_NV21,
    N2D_NV16,
    N2D_NV61,
    N2D_P010_MSB,
    N2D_P010_LSB,
    N2D_I010
};

Input file:

format-input

Output file:

format-input

Note:In yuview software, when previewing nv12 format pictures, the format selection is as shown in the figure:
framework_all

3.10.5. sample_copy

3.10.5.1. Function Overview

Function description: sample_copy implements image memory copy functionality.

Software Architecture Diagram

framework_all

Code Location and Directory Structure

  • Code location: app/samples/platform_samples/sample_gpu_2d

  • Directory structure

sample_gpu_2d/
├── resource
│   ├── R5G6B5_640x640.bmp
│   ├── RGBA8888_640x480.bmp
│   ├── bit_filter_1920_1080.bmp
│   ├── nv12_1920x1080.yuv
│   ├── scenery01.bmp
│   ├── scenery02.bmp
│   ├── scenery03.bmp
│   └── scenery04.bmp
└── sample_copy
    ├── copy.c
    └── Makefile

API Workflow Description

The core steps for using GPU 2D API are:

  1. Prepare source and destination buffers. The buffer structures are defined using the n2d_buffer_t structure.

  2. Perform blit operations: send execution commands into the command buffer

  3. Perform commit operation: submit pending instructions in the command buffer to GPU hardware and block until GPU processing completes

The example program mainly has two branches:

  1. Performance test

  2. Sample program

Overall Flow

app_common_simple

Performance Test Flow

performance

Sample Flow

sample_normal

3.10.5.2. Compilation and Deployment

Compilation

  • Enter the sample_copy directory and run make to compile.

  • The output binary is copy located in the sample_copy source directory.

  • For detailed compilation methods, refer to the Compilation Method section.

Program Deployment

  1. Upload the directories sample_copy and resource to the development board’s /userdata directory.

  2. Enter the sample_copy directory and run chmod +x copy to grant executable permissions to the program.

3.10.5.3. Running

How to Run the Program

Execute the program: ./copy

Program Parameter Options

Usage: copy [OPTIONS]
Options:
  -m <mode>              Specify running mode (0: sample, 1: performance test)
  -c <image_width>       Specify image width (columns)
  -r <image_height>      Specify image height (rows)
  -i <iteration_number>  Specify number of iterations
  -h <help>              Show this help message
     For Example, performance test: ./copy -m 1 -c 1920 -r 1080 -i 1000
     For Example, sample          : ./copy (not need input param)

Examples:

  • ./copy -m 1 -c 1920 -r 1080 -i 1000: Performance test mode, specifying resolution 1920*1080, run 1000 times, finally print average execution time and frame rate.

  • ./copy: Run the memory copy sample.

Running Effect

Execute command: ./copy Running log:

Run Sample[copy](sample mode ignores input parameters).

Start !!!
copy input file ../resource/nv12_1920x1080.yuv [1920*1080] ==> output file ./copy_sample_1920_1080.yuv [1920*1080]
Stop !!!

Effect description:

  1. The program reads the file nv12_1920x1080.yuv from the resource directory and copies it.

  2. The copied image is saved to the file: copy_sample_1920_1080.yuv

Input file:

format-input

Output file:

format-input

3.10.6. sample_crop

3.10.6.1. Function Overview

Function description: sample_crop implements image cropping functionality.

Software Architecture Diagram

framework_all

Code Location and Directory Structure

  • Code location: app/samples/platform_samples/sample_gpu_2d

  • Directory structure

sample_gpu_2d/
├── resource
│   ├── R5G6B5_640x640.bmp
│   ├── RGBA8888_640x480.bmp
│   ├── bit_filter_1920_1080.bmp
│   ├── nv12_1920x1080.yuv
│   ├── scenery01.bmp
│   ├── scenery02.bmp
│   ├── scenery03.bmp
│   └── scenery04.bmp
└── sample_crop
    ├── crop.c
    └── Makefile

API Workflow Description

The core steps for using GPU 2D API are:

  1. Prepare source and destination buffers. The buffer structures are defined using the n2d_buffer_t structure.

  2. Perform blit operations: send execution commands into the command buffer

  3. Perform commit operation: submit pending instructions in the command buffer to GPU hardware and block until GPU processing completes

The example program mainly has two branches:

  1. Performance test

  2. Sample program

Overall Flow

app_common_simple

Performance Test Flow

performance

Sample Flow

sample_normal

3.10.6.2. Compilation and Deployment

Compilation

  • Enter the sample_crop directory and run make to compile.

  • The output binary is crop located in the sample_crop source directory.

  • For detailed compilation methods, refer to the Compilation Method section.

Program Deployment

  1. Upload the directories sample_crop and resource to the development board’s /userdata directory.

  2. Enter the sample_crop directory and run chmod +x crop to grant executable permissions.

3.10.6.3. Running

How to Run the Program

Execute the program: ./crop

Program Parameter Options

Usage: crop [OPTIONS]
Options:
  -m <mode>              Specify running mode (0: sample, 1: performance test)
  -c <image_width>       Specify image width (columns)
  -r <image_height>      Specify image height (rows)
  -i <iteration_number>  Specify number of iterations
  -h <help>              Show this help message
     For Example, performance test: ./crop -m 1 -c 1920 -r 1080 -i 1000
     For Example, sample          : ./crop (not need input param)

Examples:

  • ./crop -m 1 -c 1920 -r 1080 -i 1000: Performance test mode, specifying resolution 1920*1080, run 1000 times, finally print average execution time and frame rate.

  • ./crop: Run the image cropping sample.

Running Effect

Execute command: ./crop Running log:

Run Sample[crop](sample mode ignores input parameters).

Start !!!
crop input file ../resource/nv12_1920x1080.yuv [1920*1080] ==> output file ./crop_sample_480_270.yuv [480*270]
Stop !!!

Effect description:

  1. The program reads the file nv12_1920x1080.yuv from the resource directory.

  2. Crops the input image according to the rectangle (0,0,480,270).

  3. Saves the cropped result to crop_sample_480_270.bmp.

Input file:

format-input

Output file:

format-input

3.10.7. sample_multi_source_alphablend

3.10.7.1. Function Overview

Function description: sample_multi_source_alphablend implements alpha blending of multiple input images simultaneously.

Software Architecture Diagram

framework_all

Code Location and Directory Structure

  • Code location: app/samples/platform_samples/sample_gpu_2d

  • Directory structure

sample_gpu_2d/
├── resource
│   ├── R5G6B5_640x640.bmp
│   ├── RGBA8888_640x480.bmp
│   ├── scenery01.bmp
│   ├── scenery02.bmp
│   ├── scenery03.bmp
│   └── scenery04.bmp
└── sample_multi_source_alphablend
    ├── multi_source_alphablend.c
    └── Makefile

API Workflow Description

The core steps for using GPU 2D API are:

  1. Prepare source and destination buffers. The buffer structures are defined using the n2d_buffer_t structure.

  2. Perform blit operations: send execution commands into the command buffer

  3. Perform commit operation: submit pending instructions in the command buffer to GPU hardware and block until GPU processing completes

The example program mainly has two branches:

  1. Performance test

  2. Sample program

Overall Flow

app_common_simple

Performance Test Flow

multi_source_blend_performance

Sample Program Flow

multi_source_blend

3.10.7.2. Compilation and Deployment

Compilation

  • Enter the sample_multi_source_alphablend directory and run make to compile.

  • The output binary is multi_source_alphablend located in the source directory.

  • For detailed compilation methods, refer to the Compilation Method section.

Program Deployment

  1. Upload the directories sample_multi_source_alphablend and resource to the development board’s /userdata directory.

  2. Enter the sample_multi_source_alphablend directory and run chmod +x multi_source_alphablend to grant executable permissions.

3.10.7.3. Running

How to Run the Program

Execute the program: ./multi_source_alphablend

Program Parameter Options

Usage: multi_source_alphablend [OPTIONS]
Options:
  -m <mode>              Specify running mode (0: sample, 1: performance test)
  -c <image_width>       Specify image width (columns)
  -r <image_height>      Specify image height (rows)
  -i <iteration_number>  Specify number of iterations
  -h <help>              Show this help message
     For Example, performance test: ./multi_source_alphablend -m 1 -c 1920 -r 1080 -i 1000
     For Example, sample          : ./multi_source_alphablend (not need input param)

Examples:

  • ./multi_source_alphablend -m 1 -c 1920 -r 1080 -i 1000: Performance test mode, specifying resolution 1920*1080, run 1000 times, finally print average execution time and frame rate.

  • ./multi_source_alphablend: Run the multi-source alpha blending sample.

Running Effect

Execute command: ./multi_source_alphablend Running log:

Run Sample[multi_source_alphablend](sample mode ignores input parameters).

Start !!!
Multi-source alphablend input file scenery0x [640*480] ==> output file ./multi_source_alphablend_sample_640_480.bmp [640*480]
Stop !!!

Effect description:

  1. The program reads files scenery01.bmp, scenery02.bmp, scenery03.bmp, and scenery04.bmp from the resource directory.

  2. Each of the four images is cropped from different positions and then blended using the N2D_BLEND_SRC_OVER blending mode with transparency.

  3. The blended result is saved to the file multi_source_alphablend_sample_640_480.bmp.

Input files:

format-input

format-input

format-input

format-input

Output file:

format-input

3.10.8. sample_resize

3.10.8.1. Function Overview

Function description: sample_resize completes the image enlargement function and can specify different filter types.

Filter type Meaning Typical uses
N2D_FILTER_SYNC Synchronous filter, mostly refers to Sinc Filter or synchronous interpolation filter, which can theoretically maintain the frequency domain characteristics of the image and reduce aliasing. The calculation amount is large. High-quality scaling, image processing with extremely high requirements for clarity.
N2D_FILTER_BLUR Blur filter, performs smoothing or low-pass processing to soften the image, reduce details, and reduce noise. Noise reduction, background blur, thumbnail generation.
N2D_FILTER_BILINEAR Bilinear interpolation filtering, one of the most commonly used interpolation algorithms, fast speed and medium quality. Real-time scaling, video playback, GUI rendering.
N2D_FILTER_BICUBIC Bicubic interpolation filtering is smoother and more detailed than bilinear, but requires a large amount of calculation. High-quality image scaling, photo processing.

Software architecture description

framework_all

Code location and directory structure

  • Code location app/samples/platform_samples/sample_gpu_2d

  • Directory structure

sample_gpu_2d/
├── resource
│ ├── R5G6B5_640x640.bmp
│ ├── RGBA8888_640x480.bmp
│ ├── bit_filter_1920_1080.bmp
│ ├── nv12_1920x1080.yuv
│ ├── scenery01.bmp
│ ├── scenery02.bmp
│ ├── scenery03.bmp
│ └── scenery04.bmp
└── sample_resize
├── resize.c
    └── Makefile

API process description

The core steps for using the GPU 2D API are:

  1. Prepare the source buffer and destination buffer. The source buffer and destination buffer structures use the n2d_buffer_t structure definition.

  2. Perform blit operation: send the execution command to the instruction cache area

  3. Execute the commit operation: send the instructions to be executed in the instruction buffer area to the GPU hardware and block waiting for the GPU hardware processing to complete.

The sample program mainly has two branches:

  1. Performance testing

  2. Sample program

Overall process

app_common_simple

Performance testing process

performance

Sample program flow

sample_normal

3.10.8.2. Compile and deploy

Compile

  • Enter the sample_resize directory and execute make to compile

  • The output product is resize in the sample_resize source code directory

  • For detailed program compilation methods, please refer to the Compilation Method chapter

Program deployment

  1. Upload the directories sample_resize and resource to the /userdata directory of the development board

  2. Enter sample_resize and run the chmod +x resize command to give the program executable permissions

3.10.8.3. Run

How to run the program

Execution program: ./resize

Program parameter option description

Usage: resize [OPTIONS]
Options:
  -m <mode> Specify running mode(0:sample, 1:performance test)
  -c <image_width> Specify image width(column)
  -r <image_height> Specify image height(row)
  -i <iteration_number> Specify frames per second
  -h <help> Show this help message
     For Example, performance test: ./resize -m 1 -c 1920 -r 1080 -i 1000
     For Example, sample : ./resize (not need input param)

Example:

  • ./resize -m 1 -c 1920 -r 1080 -i 1000: Performance test mode, the specified resolution is 1920*1080, run 1000 times, the final average printing time and frame rate

  • ./resize: Example of performing image enlargement function

Running effect

Execute command: ./resize

Running log:

Run Sample[resize](sample mode ignore input param).

Start!!!
Resize input file ../resource/nv12_1920x1080.yuv [1920*1080] ==> output file ./resize_sample_3840_2160_sync.yuv [3840*2160]
Resize input file ../resource/nv12_1920x1080.yuv [1920*1080] ==> output file ./resize_sample_3840_2160_blur.yuv [3840*2160]
Resize input file ../resource/nv12_1920x1080.yuv [1920*1080] ==> output file ./resize_sample_3840_2160_bilinear.yuv [3840*2160]
Resize input file ../resource/nv12_1920x1080.yuv [1920*1080] ==> output file ./resize_sample_3840_2160_bicubic.yuv [3840*2160]
Stop!!!

Effect description:

  1. The program reads the file nv12_1920x1080.yuv from the resource directory

  2. Use 4 filter algorithms respectively to enlarge the image twice horizontally and vertically.

  3. Save the enlarged image to the files resize_sample_3840_2160_sync.yuvresize_sample_3840_2160_blur.yuvresize_sample_3840_2160_bilinear.yuvresize_sample_3840_2160_bicubic.yuv

Input file:

format-input

3.10.9. sample_stitch

3.10.9.1. Function Overview

Function description: sample_stitch implements stitching four input images into a single output image.

Software Architecture Diagram

framework_all

Code Location and Directory Structure

  • Code location: app/samples/platform_samples/sample_gpu_2d

  • Directory structure

sample_gpu_2d/
├── resource
│   ├── R5G6B5_640x640.bmp
│   ├── RGBA8888_640x480.bmp
│   ├── bit_filter_1920_1080.bmp
│   ├── nv12_1920x1080.yuv
│   ├── scenery01.bmp
│   ├── scenery02.bmp
│   ├── scenery03.bmp
│   └── scenery04.bmp
└── sample_stitch
    ├── stitch.c
    └── Makefile

API Workflow Description

The core steps for using GPU 2D API are:

  1. Prepare source and destination buffers. The buffer structures are defined using the n2d_buffer_t structure.

  2. Perform blit operations: send execution commands into the command buffer

  3. Perform commit operation: submit pending instructions in the command buffer to GPU hardware and block until GPU processing completes

The example program mainly has two branches:

  1. Performance Test

  2. Sample Program

Overall Flow

app_common_simple

Performance Test Flow

stitch_performance

Sample Program Flow

stitch_sample

3.10.9.2. Compilation and Deployment

Compilation

  • Enter the sample_stitch directory and run make to compile

  • The output binary is stitch, located in the sample_stitch source directory

  • For detailed compilation methods, refer to the Compilation Method section

Program Deployment

  1. Upload the directories sample_stitch and resource to the development board’s /userdata directory

  2. Enter sample_stitch and run the command chmod +x stitch to grant executable permissions to the program

3.10.9.3. Run

Program Execution Method

Execute the program: ./stitch

Program Parameter Options Description

Usage: stitch [OPTIONS]
Options:
  -m <mode>              Specify running mode (0: sample, 1: performance test)
  -c <image_width>       Specify image width (column)
  -r <image_height>      Specify image height (row)
  -i <iteration_number>  Specify frames per second
  -h <help>              Show this help message
     For Example, performance test: ./stitch -m 1 -c 1920 -r 1080 -i 1000
     For Example, sample          : ./stitch (not need input param)

Examples:

  • ./stitch -m 1 -c 1920 -r 1080 -i 1000: Performance test mode, specifying resolution as 1920*1080, run 1000 iterations, finally print average processing time and frame rate

  • ./stitch: Run example that stitches 4 input images into 1 image

Execution Results

Execution command: Execution log:

Run Sample[stitch](sample mode ignore input param).

Start !!!
stitch input file pip0x [1920*1080] ==> output file ./stitch_sample_3840_2160.yuv [3840*2160]
Stop !!!

Result explanation:

  1. The program reads files nv12_1920x1080.yuvfour times from the resource directory

  2. The 4 input images are stitched together into one image in a 2x2 grid layout

  3. The stitched image is saved as stitch_sample_3840_2160.yuv

Input files:

format-input

Output file:

format-input

3.10.10. sample_create_n2d_buffer

3.10.10.1. Function Overview

Function Description: sample_create_n2d_buffer demonstrates converting memory allocated via the following two methods into the image buffer structure n2d_buffer_t, which is used by the 2D GPU API:

  1. Non-contiguous physical address: memory allocated using the malloc function

  2. Contiguous physical address: memory allocated via the Hbmem interface

Software Architecture Description

framework_create_n2d_buffer

Code Location and Directory Structure

  • Code location: app/samples/platform_samples/sample_gpu_2d

  • Directory structure

sample_gpu_2d/
├── resource
│   ├── R5G6B5_640x640.bmp
│   ├── RGBA8888_640x480.bmp
│   ├── scenery01.bmp
│   ├── scenery02.bmp
│   ├── scenery03.bmp
│   ├── scenery04.bmp
|   └── nv12_1920x1080.yuv
└── sample_create_n2d_buffer
    ├── create_n2d_buffer.c
    ├── create_n2d_buffer_wraper.c
    ├── create_n2d_buffer_wraper.h
    └── Makefile

API Flow Description

The core steps for using the GPU 2D API are:

  1. Prepare source and destination buffers. The buffer structures are defined using the n2d_buffer_t structure

  2. Perform blit operations: send commands to the command buffer

  3. Perform commit operation: submit pending commands in the command buffer to GPU hardware and block until GPU processing completes

The sample program mainly has two branches:

  1. Performance test

  2. Sample program, which provides the following 3 examples:

    • create_n2d_buffer_and_copy_sample: Convert memory allocated via hbmem into the n2d_buffer_t structure used by the 2D GPU API

      • The essence of converting hbmem-allocated memory to n2d_buffer_t: encapsulating the memory descriptor in another form, without involving memory copy

    • create_n2d_buffer_from_normal_memory_sample: Convert memory allocated via malloc into the n2d_buffer_t structure used by the 2D GPU API. The conversion steps are as follows:

      • Allocate physically contiguous memory via the Hbmem interface

      • Copy memory allocated by malloc into the memory allocated by Hbmem

      • Convert the memory allocated by Hbmem into n2d_buffer_t

    • create_n2d_buffer_stitch: Convert hbmem-allocated memory into n2d_buffer_t and perform image stitching

Note: The sample program calls cache-related interfaces, as listed below. For details, please refer to: Hbmem

  1. hb_mem_flush_buf

  2. hb_mem_invalidate_buf

Overall Flow of Sample Program

create_n2d_buffer_sample

Performance Test Flow

create_n2d_buffer_performance

Flow of create_n2d_buffer_and_copy_sample Example

create_n2d_buffer_copy

Flow of create_n2d_buffer_from_normal_memory_sample Example

create_n2d_buffer_normal

Flow of create_n2d_buffer_stitch Example

create_n2d_buffer_stitch

3.10.10.2. Compilation and Deployment

Compilation

  • Enter the sample_create_n2d_buffer directory and run make to compile

  • The output binary is create_n2d_buffer, located in the sample_create_n2d_buffer source directory

  • For detailed compilation methods, refer to the Compilation Method section

Program Deployment

  1. Upload the directories sample_create_n2d_buffer and resource to the development board’s /userdata directory

  2. Enter sample_create_n2d_buffer and run the command chmod +x create_n2d_buffer to grant executable permissions to the program

3.10.10.3. Run

Program Execution Method

Execute the program: ./create_n2d_buffer

Program Parameter Options Description

Usage: create_n2d_buffer [OPTIONS]
Options:
  -m <mode>              Specify running mode (0: sample, 1: performance test)
  -c <image_width>       Specify image width (column)
  -r <image_height>      Specify image height (row)
  -i <iteration_number>  Specify frames per second
  -h <help>              Show this help message
     For Example, performance test: ./create_n2d_buffer -m 1 -c 1920 -r 1080 -i 1000
     For Example, sample          : ./create_n2d_buffer (not need input param)

Examples:

  • ./create_n2d_buffer -m 1 -c 1920 -r 1080 -i 1000: Performance test mode, specifying resolution as 1920*1080, run 1000 iterations, finally print average processing time and frame rate

  • ./create_n2d_buffer: Run example that converts external memory into n2d_buffer_t supported by the 2D GPU API

Execution Results

Execution command: ./create_n2d_buffer Execution log:


Run Sample[create_n2d_buffer](sample mode ignore input param).

Start !!!
Dump image to file(./create_n2d_buffer_copy_sample_hbn_1920_1080.yuv), size(3110400) succeeded
Dump image to file(./create_n2d_buffer_stitch_sample_hbn_1920_2160.yuv), size(6220800) succeeded
Dump image to file(./create_n2d_buffer_normal_memory_sample_hbn_1920_1080.yuv), size(3110400) succeeded
Stop !!!

Result explanation:

  1. The program reads the file nv12_1920x1080.yuv from the resource directory

  2. Performs three operations on the input image:

    a. Allocate physically contiguous memory using the Hbmem interface, convert it to n2d_buffer_t, use 2D GPU API to complete image copy, and save the result to file create_n2d_buffer_copy_sample_hbn_1920_1080.yuv

    b. Using Hbmem, the operation on a is enhanced by incorporating a 2D GPU interface to enable vertical and horizontal stitching functionality. The resulting output is saved to the file create_n2d_buffer_stitch_sample_hbn_1920_2160.yuv.

    c. Allocate non-contiguous physical memory using malloc, convert it to n2d_buffer_t, use 2D GPU API to complete image copy, and save the result to file create_n2d_buffer_normal_memory_sample_hbn_1920_1080.yuv