Skip to content

Commit

Permalink
Fix video decoder on Web env. (#117)
Browse files Browse the repository at this point in the history
* Fix video decoder on Web env.

* Remove unused variable.

* Fix some tgfx bindings on Web env.
  • Loading branch information
zenoslin authored Feb 17, 2022
1 parent 153abc9 commit 2394ba9
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 52 deletions.
28 changes: 28 additions & 0 deletions src/platform/web/DecodingPolicy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making libpag available.
//
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "pag/pag.h"

namespace pag {
void PAGVideoDecoder::SetSoftwareToHardwareEnabled(bool) {
}

bool SoftwareToHardwareEnabled() {
return false;
}
} // namespace pag
13 changes: 5 additions & 8 deletions src/rendering/caches/RenderCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "rendering/caches/ImageContentCache.h"
#include "rendering/caches/LayerCache.h"
#include "rendering/renderers/FilterRenderer.h"
#include "video/VideoDecoder.h"

namespace pag {
// 300M设置的大一些用于兜底,通常在大于20M时就开始随时清理。
Expand Down Expand Up @@ -167,10 +166,9 @@ void RenderCache::prepareFrame() {
for (auto& item : layerDistances) {
for (auto pagLayer : item.second) {
if (pagLayer->layerType() == LayerType::PreCompose) {
auto policy =
VideoDecoder::SoftwareToHardwareEnabled() && item.first < MIN_HARDWARE_PREPARE_TIME
? DecodingPolicy::SoftwareToHardware
: DecodingPolicy::Hardware;
auto policy = SoftwareToHardwareEnabled() && item.first < MIN_HARDWARE_PREPARE_TIME
? DecodingPolicy::SoftwareToHardware
: DecodingPolicy::Hardware;
preparePreComposeLayer(static_cast<PreComposeLayer*>(pagLayer->layer), policy);
} else if (pagLayer->layerType() == LayerType::Image) {
prepareImageLayer(static_cast<PAGImageLayer*>(pagLayer));
Expand Down Expand Up @@ -423,9 +421,8 @@ std::shared_ptr<SequenceReader> RenderCache::getSequenceReader(Sequence* sequenc
if (reader == nullptr) {
auto file = stage->getSequenceFile(sequence);
reader = MakeSequenceReader(file, sequence,
VideoDecoder::SoftwareToHardwareEnabled()
? DecodingPolicy::SoftwareToHardware
: DecodingPolicy::Hardware);
SoftwareToHardwareEnabled() ? DecodingPolicy::SoftwareToHardware
: DecodingPolicy::Hardware);
if (reader && !staticComposition) {
// 完全静态的序列帧不用缓存。
sequenceCaches[compositionID] = reader;
Expand Down
9 changes: 4 additions & 5 deletions src/rendering/renderers/CompositionRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "rendering/graphics/Picture.h"
#include "rendering/graphics/Recorder.h"
#include "rendering/readers/BitmapSequenceReader.h"
#include "video/VideoDecoder.h"

namespace pag {
class SequenceProxy : public TextureProxy {
Expand All @@ -37,10 +36,10 @@ class SequenceProxy : public TextureProxy {
}

void prepare(RenderCache* cache) const override {
static_cast<RenderCache*>(cache)->prepareSequenceReader(
sequence, frame,
VideoDecoder::SoftwareToHardwareEnabled() ? DecodingPolicy::SoftwareToHardware
: DecodingPolicy::Hardware);
static_cast<RenderCache*>(cache)->prepareSequenceReader(sequence, frame,
SoftwareToHardwareEnabled()
? DecodingPolicy::SoftwareToHardware
: DecodingPolicy::Hardware);
}

std::shared_ptr<tgfx::Texture> getTexture(RenderCache* cache) const override {
Expand Down
33 changes: 33 additions & 0 deletions src/video/DecodingPolicy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making libpag available.
//
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "DecodingPolicy.h"
#include <atomic>
#include "pag/pag.h"

namespace pag {
static std::atomic_bool softwareToHardwareEnabled = true;

void PAGVideoDecoder::SetSoftwareToHardwareEnabled(bool value) {
softwareToHardwareEnabled = value;
}

bool SoftwareToHardwareEnabled() {
return softwareToHardwareEnabled;
}
} // namespace pag
7 changes: 7 additions & 0 deletions src/video/DecodingPolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ enum class DecodingPolicy {
*/
SoftwareToHardware
};

/**
* If true, VideoDecoder uses a software decoder first, but initializes a hardware on
* async thread, and then switches to the hardware decoder when it is initialized.
* The default value is true, which will improve the performance of first frame rendering.
*/
bool SoftwareToHardwareEnabled();
} // namespace pag
9 changes: 0 additions & 9 deletions src/video/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,13 @@

namespace pag {
static std::atomic<SoftwareDecoderFactory*> softwareDecoderFactory = {nullptr};
static std::atomic_bool softwareToHardwareEnabled = true;
static std::atomic_int maxHardwareDecoderCount = {65535};
static std::atomic_int globalGPUDecoderCount = {0};

void PAGVideoDecoder::SetMaxHardwareDecoderCount(int count) {
maxHardwareDecoderCount = count;
}

void PAGVideoDecoder::SetSoftwareToHardwareEnabled(bool value) {
softwareToHardwareEnabled = value;
}

void PAGVideoDecoder::RegisterSoftwareDecoderFactory(SoftwareDecoderFactory* decoderFactory) {
softwareDecoderFactory = decoderFactory;
}
Expand All @@ -46,10 +41,6 @@ bool VideoDecoder::HasHardwareDecoder() {
return Platform::Current()->hasHardwareDecoder();
}

bool VideoDecoder::SoftwareToHardwareEnabled() {
return softwareToHardwareEnabled;
}

int VideoDecoder::GetMaxHardwareDecoderCount() {
return maxHardwareDecoderCount;
}
Expand Down
7 changes: 0 additions & 7 deletions src/video/VideoDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,6 @@ class VideoDecoder {
*/
static int GetMaxHardwareDecoderCount();

/**
* If true, VideoDecoder uses a software decoder first, but initializes a hardware on
* async thread, and then switches to the hardware decoder when it is initialized.
* The default value is true, which will improve the performance of first frame rendering.
*/
static bool SoftwareToHardwareEnabled();

/**
* Creates a new video decoder by specified type. Returns a hardware video decoder if useHardware
* is true, otherwise, returns a software video decoder.
Expand Down
46 changes: 23 additions & 23 deletions web/src/pag_wasm_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,13 @@ EMSCRIPTEN_BINDINGS(pag) {
.property("rowBytes", &tgfx::ImageInfo::rowBytes)
.property("colorType", &tgfx::ImageInfo::colorType);

class_<Matrix>("Matrix")
.property("a", &Matrix::getScaleX)
.property("b", &Matrix::getSkewY)
.property("c", &Matrix::getSkewX)
.property("d", &Matrix::getScaleY)
.property("tx", &Matrix::getTranslateX)
.property("ty", &Matrix::getTranslateY);
class_<tgfx::Matrix>("Matrix")
.property("a", &tgfx::Matrix::getScaleX)
.property("b", &tgfx::Matrix::getSkewY)
.property("c", &tgfx::Matrix::getSkewX)
.property("d", &tgfx::Matrix::getScaleY)
.property("tx", &tgfx::Matrix::getTranslateX)
.property("ty", &tgfx::Matrix::getTranslateY);

class_<TextDocument>("TextDocument")
.smart_ptr<std::shared_ptr<TextDocument>>("TextDocument")
Expand Down Expand Up @@ -238,18 +238,18 @@ EMSCRIPTEN_BINDINGS(pag) {
.field("xHeight", &tgfx::FontMetrics::xHeight)
.field("capHeight", &tgfx::FontMetrics::capHeight);

value_object<Rect>("Rect")
.field("left", &Rect::left)
.field("top", &Rect::top)
.field("right", &Rect::right)
.field("bottom", &Rect::bottom);
value_object<tgfx::Rect>("Rect")
.field("left", &tgfx::Rect::left)
.field("top", &tgfx::Rect::top)
.field("right", &tgfx::Rect::right)
.field("bottom", &tgfx::Rect::bottom);

value_object<Point>("Point").field("x", &Point::x).field("y", &Point::y);
value_object<tgfx::Point>("Point").field("x", &tgfx::Point::x).field("y", &tgfx::Point::y);

value_object<Color>("Color")
.field("red", &Color::red)
.field("green", &Color::green)
.field("blue", &Color::blue);
value_object<tgfx::Color>("Color")
.field("red", &tgfx::Color::red)
.field("green", &tgfx::Color::green)
.field("blue", &tgfx::Color::blue);

value_object<Marker>("Marker")
.field("startTime", &Marker::startTime)
Expand All @@ -262,11 +262,11 @@ EMSCRIPTEN_BINDINGS(pag) {
.value("INVERSE_WINDING", tgfx::PathFillType::InverseWinding)
.value("INVERSE_EVEN_ODD", tgfx::PathFillType::InverseEvenOdd);

enum_<ColorType>("ColorType")
.value("Unknown", ColorType::Unknown)
.value("ALPHA_8", ColorType::ALPHA_8)
.value("RGBA_8888", ColorType::RGBA_8888)
.value("BGRA_8888", ColorType::BGRA_8888);
enum_<tgfx::ColorType>("ColorType")
.value("Unknown", tgfx::ColorType::Unknown)
.value("ALPHA_8", tgfx::ColorType::ALPHA_8)
.value("RGBA_8888", tgfx::ColorType::RGBA_8888)
.value("BGRA_8888", tgfx::ColorType::BGRA_8888);

enum_<LayerType>("LayerType")
.value("Unknown", LayerType::Unknown)
Expand All @@ -289,7 +289,7 @@ EMSCRIPTEN_BINDINGS(pag) {

register_vector<std::shared_ptr<PAGLayer>>("VectorPAGLayer");
register_vector<std::string>("VectorString");
register_vector<Point>("VectorPoint");
register_vector<tgfx::Point>("VectorPoint");
register_vector<const Marker*>("VectorMarker");

function("_SetFallbackFontNames", optional_override([](std::vector<std::string> fontNames) {
Expand Down

0 comments on commit 2394ba9

Please sign in to comment.