Other

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

深度剖析 Tencent/libpag

项目背景与痛点

PAG (Portable Animated Graphics) 文件是一个跨平台的动画格式,广泛应用于广告、游戏和应用程序的动态效果表现。Tencent/libpag 项目旨在提供一个官方的渲染库,可以在多个平台上原生渲染 PAG 格式文件,尤其是来自 Adobe After Effects 的动画。这个项目解决了在不同平台上统一处理复杂动画内容的痛点,它允许开发者跨平台部署高性能的动画支持。

核心技术揭秘

libpag 通过一系列高效的渲染算法和步骤实现了高性能的动画渲染效果。在内部,它利用了多线程、缓存机制和高效的图形引擎来加速渲染过程。

架构分析

graph TD A[用户请求加载PAG文件] --> B[文件解析为内部数据结构] B --> C{"设置渲染环境"} C -->|"成功"| D[执行渲染] D --> E[更新显示] C -->|"失败"| F[记录错误]

在项目的源代码中,可以看到大量设计模式的运用,如工厂模式用于创建不同类型的动画效果,以及单例模式保证渲染上下文的全局唯一性。数据流方面,动画渲染遵循一种预处理-渲染-输出的流程,其中预处理包括了文件解析、参数配置和资源加载等多个环节。

功能亮点与差异

相比其他同类产品,Tencent/libpag 的独特之处在于其高效的跨平台支持和灵活的自定义能力。它能够自适应多种平台的特点,包括 Android、iOS 以及桌面操作系统。同时,开发团队提供了丰富的 API 接口,使得开发者可以方便地对渲染过程进行优化和扩展。

应用场景与落地建议

在生产环境中使用 libpag,需要注意资源的管理和渲染的并发控制。对于大型项目,合理安排渲染任务的优先级,确保资源可以被有效利用。此外,针对不同的设备和操作系统优化渲染参数是非常重要的。它适用于需要跨平台高性能动画支持的应用场景,如视频广告平台、互动游戏和多媒体应用。

综合评价

Tencent/libpag 是一款高性能、跨平台的 PAG 文件渲染库。它很好地解决了不同平台上的动画一致性问题,并提供了丰富的定制扩展能力。项目优化细致、功能全面,对于需要在多种设备上实现一致动画效果的开发者来说是一个非常棒的选择。同时,它也具备一定的复杂性,对开发者的技术要求较高,需要熟练的动画和图形渲染知识以便灵活运用。

简要分析

热度分
12542
价值分
4165
活跃状态
活跃

语言占比

AppleScript
Batchfile
C
C++
CMake
CSS
Dockerfile
HTML
Inno Setup
Java
JavaScript
Linker Script
Objective-C
Objective-C++
PowerShell
Python
QML
Rez
Shell
TypeScript

Release

README

PAG Logo

license PRs Welcome codecov autotest build GitHub release (latest SemVer)

Cloud Studio

English | 简体中文 | Homepage

Introduction

libpag is a real-time rendering library for PAG (Portable Animated Graphics) files, capable of rendering both vector-based and raster-based animations across various platforms, including iOS, Android, OpenHarmony, macOS, Windows, Linux, and the Web.

PAG is an open-source file format designed for recording animations. You can create and export PAG files from Adobe After Effects using the PAGExporter plugin and preview them in the PAGViewer app, both available on macOS and Windows.

PAG is widely used in major Tencent apps like WeChat, Mobile QQ, Honor of Kings, Tencent Video, and QQ Music, as well as in thousands of third-party apps, reaching hundreds of millions of users.

Advantages

  • Highly efficient file format
intro_1

Benefiting from its highly efficient binary format, PAG files decode 10 times faster than JSON files and are about 50% smaller in file size for the same animations. Designers can also easily include beautiful animations with bitmaps or audiovisual media in a single file without needing additional attachments.

  • All AE features supported
intro_2

While other solutions may only support exporting limited vector-based AE features, PAG combines vector-based and raster-based exporting techniques to support all AE animations in a single file. This means third-party plugin effects in AE can also be exported.

  • Measurable performance
intro_4

PAG provides a monitoring panel in PAGViewer that displays normalized performance data for PAG files, making it easy for designers to review and optimize performance without needing developers. With numerous automatic optimization techniques from the PAGExporter plugin, you can create animations with impressive visual effects and excellent performance more efficiently.

PAGViewer includes a monitoring panel

  • Runtime editable animations
intro_5

With the flexible editing APIs from the PAG SDK, developers can easily modify the layer structure of a single PAG file, combine multiple PAG files into one composition, or replace text and images with all pre-designed animation effects applied at runtime. This significantly reduces the coding work required for features like video templates.

System Requirements

  • iOS 15.0+
  • Android 5.0+
  • HarmonyOS Next 5.0.0(12)+
  • macOS 12.0+
  • Windows 7.0+
  • Chrome 69.0+ (Web)
  • Safari 15.0+ (Web)

Getting Started

We currently only publish precompiled libraries for iOS, Android, macOS, Web, and OpenHarmony. You can build libraries of other platforms from the source code. The latest releases can be downloaded from here.

iOS Integration

You can download the framework from the release page or add libpag to your project using CocoaPods. To add the pod to your Podfile, include:

pod 'libpag'

And then run:

pod install

After installing the CocoaPod, import libpag into your project with:

#import <libpag/xxx.h>

Android Integration

You can download the AAR from the release page or add libpag to your project using Maven:

Edit the build.gradle file in the root of your project and add mavenCentral() to the repositories section:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

Add libpag to app/build.gradle (4.0.5.10 should be replaced with the latest release version):

dependencies {
    implementation 'com.tencent.tav:libpag:4.0.5.10'
}

Add the following rule to your proguard rules to prevent incorrect obfuscation:

  -keep class org.libpag.** {*;}
  -keep class androidx.exifinterface.** {*;}

Finally, run gradle sync and build your project.

OpenHarmony Integration

You can download the HAR file from the release page, or add libpag to your project using OHPM:

ohpm install @tencent/libpag

Alternatively, you can add it to your project manually. Add the following lines to oh-package.json5 in your app module.

"dependencies": {
"@tencent/libpag": "^1.0.1",
}

Then run:

ohpm install

Web Integration

Copy the following code into an HTML file and open it in your browser:

<canvas class="canvas" id="pag"></canvas>
<script src="https://unpkg.com/libpag@latest/lib/libpag.min.js"></script>
<script>
  window.libpag.PAGInit().then((PAG) => {
    const url = 'https://pag.io/file/like.pag';
    fetch(url)
      .then((response) => response.blob())
      .then(async (blob) => {
        const file = new window.File([blob], url.replace(/(.*\/)*([^.]+)/i, '$2'));
        // Do Something.
      });
  });
</script>

More information:Web SDK Guide

Example

Explore these projects to learn how to use the libpag APIs:

Documentation

You can find additional documentation on pag.io

Development

We recommend using the CLion IDE on macOS for development.

Branch Management

  • The main branch is our active development branch, containing the latest features and bug fixes.
  • The release/ branches are our stable milestone branches, fully tested. We periodically create a release/{version} branch from the main branch. After a release/{version} branch is created, only high-priority fixes are checked into it.

Note: This repository only contains the latest code since PAG 4.0. For legacy PAG 3.0 versions, you can download the precompiled libraries from here.

Build Prerequisites

  • Xcode 13.0+
  • GCC 9.0+
  • Visual Studio 2019+
  • NodeJS 14.14.0+
  • Ninja 1.9.0+
  • CMake 3.13.0+
  • QT 6.2.0+
  • NDK 28+ (28.0.13004108 recommended)
  • Emscripten 3.1.58+

Dependency Management

libpag uses the depsync tool to manage third-party dependencies.

For macOS platform:

Run the script located in the root directory of the project:

./sync_deps.sh

This script will automatically install the necessary tools and sync all third-party repositories.

For other platforms:

First, ensure you have the latest version of Node.js installed (you may need to restart your computer afterward). Then, run the following command to install the depsync tool:

npm install -g depsync

Then, run depsync in the root directory of the project.

depsync

You might need to enter your Git account and password during synchronization. Ensure you have enabled the git-credential-store so that CMakeList.txt can automatically trigger synchronization next time.

Build

After synchronization, you can open the project with CLion and build the PAG library.

For macOS:

No additional CLion configuration is needed.

For Windows:

Follow these steps to configure CLion correctly:

  • Ensure you have installed the [Desktop development with C++] and [Universal Windows Platform development] components for VS2019.
  • Open the File->Settings panel, go to Build, Execution, Deployment->Toolchains, and set the toolchain to Visual Studio with amd64 (Recommended) or x86 architecture.

Note: If you encounter issues during the CMake build, update to the latest version of the CMake command-line tool and try again.

Support Us

If you find libpag helpful, please give us a Star. We truly appreciate your support :)

Star History Chart

License

libpag is licensed under the Apache Version 2.0 License

The copyright notice pertaining to the Tencent code in this repo was previously in the name of “THL A29 Limited”. That entity has now been de-registered. You should treat all previously distributed copies of the code as if the copyright notice was in the name of “Tencent”.

Privacy Policy

Please refer to the PAG SDK Personal Information Processing Rules when using the libpag SDK.

Data Collection

  • Binaries built from this repository collect no data. They contain no network requests or usage reporting, and the bundled privacy manifest (PrivacyInfo.xcprivacy) declares no data collection.
  • Official prebuilt releases distributed through pag.io and CocoaPods contain an additional anonymous usage statistics module on iOS and Android, which reports the following data to the Tencent Beacon analytics service (otheve.beacon.qq.com), at most once per SDK version per month: host app name and bundle identifier, operating system version and device model, GPU version and vendor, and the libpag SDK version. No user IDs, advertising identifiers, or other personal data are collected, and the data is used solely for SDK usage statistics. The privacy manifest bundled in the official releases declares this data collection accordingly.

Contribution

If you have any ideas or suggestions to improve libpag, feel free to submit an issue or a pull request. Before doing so, please read our Contributing Guide.

评论

暂无评论