Skip to content

Commit

Permalink
Replace tgfx::Shape with the built-in cache mechanism of the Path cla…
Browse files Browse the repository at this point in the history
…ss. (#2158)
  • Loading branch information
domchen committed Feb 25, 2024
1 parent 7581de8 commit c1ff50e
Show file tree
Hide file tree
Showing 8 changed files with 2,320 additions and 2,404 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{
"url": "${PAG_GROUP}/tgfx.git",
"commit": "a67f1975c50fbcdc5c69886fb5c48a746803e098",
"commit": "d71b28197abb09993e857c038c32a90008b6d1ac",
"dir": "third_party/tgfx"
},
{
Expand Down
20 changes: 1 addition & 19 deletions src/base/utils/MatrixUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,9 @@ float GetMaxScaleFactor(const tgfx::Matrix& matrix) {
}

tgfx::Point GetScaleFactor(const tgfx::Matrix& matrix, float contentScale, bool inverted) {
tgfx::Point scale = {};
auto a = matrix.get(0);
auto c = matrix.get(1);
auto b = matrix.get(3);
auto d = matrix.get(4);
float determinant = a * d - b * c;
if (a == 1 && b == 0) {
scale.x = 1;
} else {
auto result = sqrtf(a * a + b * b);
scale.x = determinant < 0 ? -result : result;
}
if (c == 0 && d == 1) {
scale.y = 1;
} else {
auto result = sqrtf(c * c + d * d);
scale.y = determinant < 0 ? -result : result;
}
auto scale = matrix.getAxisScales();
scale.x *= contentScale;
scale.y *= contentScale;

if (inverted) {
scale.x = scale.x == 0 ? 0 : 1 / scale.x;
scale.y = scale.y == 0 ? 0 : 1 / scale.y;
Expand Down
45 changes: 0 additions & 45 deletions src/rendering/caches/RenderCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ void RenderCache::attachToContext(tgfx::Context* current, bool forDrawing) {
clearSequenceCache(assetID);
clearFilterCache(assetID);
removeTextAtlas(assetID);
shapeCaches.erase(assetID);
}
}

Expand Down Expand Up @@ -278,50 +277,6 @@ Snapshot* RenderCache::getSnapshot(const Picture* picture) {
return snapshot;
}

std::shared_ptr<tgfx::Shape> RenderCache::getShape(ID assetID, const tgfx::Path& path) {
usedAssets.insert(assetID);
auto scaleFactor = stage->getAssetMaxScale(assetID);
auto shape = findShape(assetID, path);
if (shape && fabsf(shape->resolutionScale() - scaleFactor) > SCALE_FACTOR_PRECISION) {
removeShape(assetID, path);
shape = nullptr;
}
if (shape != nullptr) {
return shape;
}
shape = tgfx::Shape::MakeFromFill(path, scaleFactor);
if (shape != nullptr) {
shapeCaches[assetID][path] = shape;
}
return shape;
}

std::shared_ptr<tgfx::Shape> RenderCache::findShape(ID assetID, const tgfx::Path& path) {
auto result = shapeCaches.find(assetID);
if (result != shapeCaches.end()) {
auto iter = result->second.find(path);
if (iter != result->second.end()) {
return iter->second;
}
}
return nullptr;
}

void RenderCache::removeShape(ID assetID, const tgfx::Path& path) {
auto shapeMap = shapeCaches.find(assetID);
if (shapeMap == shapeCaches.end()) {
return;
}
auto shape = shapeMap->second.find(path);
if (shape == shapeMap->second.end()) {
return;
}
shapeMap->second.erase(shape);
if (shapeMap->second.empty()) {
shapeCaches.erase(assetID);
}
}

void RenderCache::removeSnapshot(ID assetID) {
auto snapshot = snapshotCaches.find(assetID);
if (snapshot == snapshotCaches.end()) {
Expand Down
13 changes: 2 additions & 11 deletions src/rendering/caches/RenderCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
#include "tgfx/gpu/Device.h"

namespace pag {
using ShapeMap = std::unordered_map<tgfx::Path, std::shared_ptr<tgfx::Shape>, PathHasher>;

class RenderCache : public Performance {
public:
explicit RenderCache(PAGStage* stage);
Expand Down Expand Up @@ -130,13 +128,11 @@ class RenderCache : public Performance {
*/
void removeSnapshot(ID assetID);

std::shared_ptr<tgfx::Shape> getShape(ID assetID, const tgfx::Path& path);

TextAtlas* getTextAtlas(const TextBlock* textBlock);

/**
* Prepares an image for next getAssetImage() call, which may schedule an asynchronous decoding
* task immediately.
* Prepares an image for the next getAssetImage() call, which may schedule an asynchronous
* decoding task immediately.
*/
void prepareAssetImage(ID assetID, const ImageProxy* proxy);

Expand Down Expand Up @@ -193,7 +189,6 @@ class RenderCache : public Performance {
std::list<Snapshot*> snapshotLRU = {};
std::unordered_map<Snapshot*, std::list<Snapshot*>::iterator> snapshotPositions = {};
std::unordered_map<ID, TextAtlas*> textAtlases = {};
std::unordered_map<ID, ShapeMap> shapeCaches = {};
std::unordered_map<ID, std::shared_ptr<tgfx::Image>> assetImages = {};
std::unordered_map<ID, std::shared_ptr<tgfx::Image>> decodedAssetImages = {};
std::unordered_map<ID, std::vector<SequenceImageQueue*>> sequenceCaches = {};
Expand Down Expand Up @@ -231,10 +226,6 @@ class RenderCache : public Performance {
void removeTextAtlas(ID assetID);
TextAtlas* getTextAtlas(ID assetID) const;

// shape caches:
std::shared_ptr<tgfx::Shape> findShape(ID assetID, const tgfx::Path& path);
void removeShape(ID assetID, const tgfx::Path& path);

void preparePreComposeLayer(PreComposeLayer* layer);
void prepareImageLayer(PAGImageLayer* layer);
void prepareNextFrame();
Expand Down
17 changes: 3 additions & 14 deletions src/rendering/graphics/Shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "tgfx/core/Canvas.h"
#include "tgfx/core/Mask.h"
#include "tgfx/core/Shader.h"
#include "tgfx/core/Shape.h"
#include "tgfx/gpu/Surface.h"

namespace pag {
Expand Down Expand Up @@ -65,19 +64,9 @@ bool Shape::getPath(tgfx::Path* result) const {
void Shape::prepare(RenderCache*) const {
}

void Shape::draw(tgfx::Canvas* canvas, RenderCache* renderCache) const {
void Shape::draw(tgfx::Canvas* canvas, RenderCache*) const {
tgfx::Paint paint;
auto shape = renderCache->getShape(assetID, path);
if (shape == nullptr) {
paint.setShader(shader);
canvas->drawPath(path, paint);
return;
}
auto matrix = tgfx::Matrix::MakeScale(shape->resolutionScale());
paint.setShader(shader->makeWithMatrix(matrix));
auto oldMatrix = canvas->getMatrix();
canvas->concat(tgfx::Matrix::MakeScale(1 / shape->resolutionScale()));
canvas->drawShape(shape, paint);
canvas->setMatrix(oldMatrix);
paint.setShader(shader);
canvas->drawPath(path, paint);
}
} // namespace pag
1 change: 0 additions & 1 deletion src/rendering/graphics/Snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "pag/types.h"
#include "tgfx/core/Image.h"
#include "tgfx/core/Matrix.h"
#include "tgfx/core/Shape.h"

namespace pag {
class RenderCache;
Expand Down
Loading

0 comments on commit c1ff50e

Please sign in to comment.