Skip to content

Commit 3a6874b

Browse files
authored
Use Nearest as the texture filtering mode to avoid rendering blur when the image is not scaled. (#2930)
1 parent 89a59f4 commit 3a6874b

File tree

5 files changed

+228
-211
lines changed

5 files changed

+228
-211
lines changed

src/rendering/graphics/Canvas.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@
2121
#include "tgfx/core/Surface.h"
2222

2323
namespace pag {
24+
tgfx::SamplingOptions GetSamplingOptions(const tgfx::Matrix& matrix, tgfx::Image* image) {
25+
if (image == nullptr) {
26+
return {};
27+
}
28+
auto mipmapMode = image->hasMipmaps() ? tgfx::MipmapMode::Linear : tgfx::MipmapMode::None;
29+
tgfx::SamplingOptions samplingOptions(tgfx::FilterMode::Linear, mipmapMode);
30+
if (matrix.isTranslate()) {
31+
float tx = matrix.getTranslateX();
32+
float ty = matrix.getTranslateY();
33+
if (std::floor(tx) == tx && std::floor(ty) == ty) {
34+
samplingOptions.filterMode = tgfx::FilterMode::Nearest;
35+
}
36+
}
37+
return samplingOptions;
38+
}
39+
2440
struct CanvasState {
2541
float alpha = 1.0f;
2642
tgfx::BlendMode blendMode = tgfx::BlendMode::SrcOver;

src/rendering/graphics/Canvas.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020

2121
#include "tgfx/core/Canvas.h"
2222
#include "tgfx/core/RenderFlags.h"
23+
#include "tgfx/core/SamplingOptions.h"
2324

2425
namespace pag {
2526
class RenderCache;
2627
struct CanvasState;
2728

29+
tgfx::SamplingOptions GetSamplingOptions(const tgfx::Matrix& matrix, tgfx::Image* image);
30+
2831
class Canvas {
2932
public:
3033
Canvas(tgfx::Surface* surface, RenderCache* cache);
@@ -145,21 +148,10 @@ class Canvas {
145148
canvas->drawPath(path, createPaint(paint));
146149
}
147150

148-
void drawImage(std::shared_ptr<tgfx::Image> image, float left, float top,
149-
const tgfx::Paint* paint = nullptr) {
150-
auto realPaint = createPaint(paint);
151-
canvas->drawImage(std::move(image), left, top, &realPaint);
152-
}
153-
154-
void drawImage(std::shared_ptr<tgfx::Image> image, const tgfx::Matrix& matrix,
155-
const tgfx::Paint* paint = nullptr) {
156-
auto realPaint = createPaint(paint);
157-
canvas->drawImage(std::move(image), matrix, &realPaint);
158-
}
159-
160151
void drawImage(std::shared_ptr<tgfx::Image> image, const tgfx::Paint* paint = nullptr) {
161152
auto realPaint = createPaint(paint);
162-
canvas->drawImage(std::move(image), &realPaint);
153+
auto samplingOptions = GetSamplingOptions(canvas->getMatrix(), image.get());
154+
canvas->drawImage(std::move(image), samplingOptions, &realPaint);
163155
}
164156

165157
void drawImage(std::shared_ptr<tgfx::Image> image, tgfx::SamplingOptions sampling,

src/rendering/graphics/Picture.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ class ImageProxyPicture : public Picture {
9494
if (!(renderFlags & tgfx::RenderFlags::DisableCache)) {
9595
auto snapshot = cache->getSnapshot(this);
9696
if (snapshot) {
97-
canvas->drawImage(snapshot->getImage(), snapshot->getMatrix());
97+
auto canvasMatrix = canvas->getMatrix();
98+
canvas->concat(snapshot->getMatrix());
99+
canvas->drawImage(snapshot->getImage());
100+
canvas->setMatrix(canvasMatrix);
98101
return;
99102
}
100103
}
@@ -178,7 +181,10 @@ class SnapshotPicture : public Picture {
178181
graphic->draw(canvas);
179182
return;
180183
}
181-
canvas->drawImage(snapshot->getImage(), snapshot->getMatrix());
184+
auto canvasMatrix = canvas->getMatrix();
185+
canvas->concat(snapshot->getMatrix());
186+
canvas->drawImage(snapshot->getImage());
187+
canvas->setMatrix(canvasMatrix);
182188
}
183189

184190
protected:

src/rendering/renderers/FilterRenderer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,10 @@ void FilterRenderer::DrawWithFilter(Canvas* parentCanvas, const FilterModifier*
415415
} else if (input == output) {
416416
parentCanvas->drawPicture(sourcePicture);
417417
} else {
418-
parentCanvas->drawImage(output, totalOffset.x, totalOffset.y);
418+
auto canvasMatrix = parentCanvas->getMatrix();
419+
parentCanvas->translate(totalOffset.x, totalOffset.y);
420+
parentCanvas->drawImage(output);
421+
parentCanvas->setMatrix(canvasMatrix);
419422
}
420423
parentCanvas->restore();
421424
}

0 commit comments

Comments
 (0)