Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the incorrect mipmap state of the Image returned by Image::makeRasterized(). #157

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/images/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ BackendTexture Image::getBackendTexture(Context*, ImageOrigin*) const {

std::shared_ptr<Image> Image::makeRasterized(float rasterizationScale,
SamplingOptions sampling) const {
return RasterImage::MakeFrom(weakThis.lock(), rasterizationScale, sampling);
auto rasterImage = RasterImage::MakeFrom(weakThis.lock(), rasterizationScale, sampling);
if (rasterImage != nullptr && hasMipmaps()) {
return rasterImage->makeMipmapped(true);
}
return rasterImage;
}

std::shared_ptr<Image> Image::makeTextureImage(Context* context) const {
Expand Down
14 changes: 10 additions & 4 deletions src/images/MipmapImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "MipmapImage.h"
#include "utils/Log.h"
#include "utils/UniqueID.h"

namespace tgfx {
static BytesKey MakeMipmapBytesKey() {
auto mipmapFlag = UniqueID::Next();
BytesKey bytesKey(1);
bytesKey.write(1);
bytesKey.write(mipmapFlag);
return bytesKey;
}

std::shared_ptr<Image> MipmapImage::MakeFrom(std::shared_ptr<ResourceImage> source) {
if (source == nullptr) {
return nullptr;
}
DEBUG_ASSERT(!source->hasMipmaps());
static const auto MipmapBytesKey = MakeMipmapBytesKey();
auto uniqueKey = UniqueKey::Combine(source->uniqueKey, MipmapBytesKey);
auto image =
Expand All @@ -44,12 +47,15 @@ MipmapImage::MipmapImage(UniqueKey uniqueKey, std::shared_ptr<ResourceImage> sou

std::shared_ptr<Image> MipmapImage::makeRasterized(float rasterizationScale,
SamplingOptions sampling) const {
if (rasterizationScale == 1.0f) {
return weakThis.lock();
}
auto newSource =
std::static_pointer_cast<ResourceImage>(source->makeRasterized(rasterizationScale, sampling));
if (newSource == source) {
return weakThis.lock();
if (newSource != nullptr) {
return newSource->makeMipmapped(true);
}
return MipmapImage::MakeFrom(std::move(newSource));
return newSource;
}

std::shared_ptr<Image> MipmapImage::onMakeDecoded(Context* context, bool) const {
Expand Down
9 changes: 6 additions & 3 deletions src/images/RasterImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
#include "tgfx/core/RenderFlags.h"

namespace tgfx {
std::shared_ptr<Image> RasterImage::MakeFrom(std::shared_ptr<Image> source,
float rasterizationScale, SamplingOptions sampling) {
std::shared_ptr<RasterImage> RasterImage::MakeFrom(std::shared_ptr<Image> source,
float rasterizationScale,
SamplingOptions sampling) {
if (source == nullptr || rasterizationScale <= 0) {
return nullptr;
}
auto hasMipmap = source->hasMipmaps();
auto needMipmap = sampling.mipmapMode != MipmapMode::None;
// The source image's mipmap state is controlled by sampling options, while the caller defines the
// mipmap state of the returned raster image.
if (hasMipmap != needMipmap) {
auto newSource = source->makeMipmapped(needMipmap);
if (newSource != nullptr) {
Expand All @@ -40,7 +43,7 @@ std::shared_ptr<Image> RasterImage::MakeFrom(std::shared_ptr<Image> source,
auto rasterImage = std::shared_ptr<RasterImage>(
new RasterImage(UniqueKey::Make(), std::move(source), rasterizationScale, sampling));
rasterImage->weakThis = rasterImage;
return hasMipmap ? rasterImage->makeMipmapped(true) : rasterImage;
return rasterImage;
}

RasterImage::RasterImage(UniqueKey uniqueKey, std::shared_ptr<Image> source,
Expand Down
9 changes: 6 additions & 3 deletions src/images/RasterImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
namespace tgfx {
class RasterImage : public ResourceImage {
public:
static std::shared_ptr<Image> MakeFrom(std::shared_ptr<Image> source,
float rasterizationScale = 1.0f,
SamplingOptions sampling = {});
/**
* Note that the returned Image is always non-mipmapped.
*/
static std::shared_ptr<RasterImage> MakeFrom(std::shared_ptr<Image> source,
float rasterizationScale = 1.0f,
SamplingOptions sampling = {});

int width() const override;

Expand Down
4 changes: 4 additions & 0 deletions test/src/CanvasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,11 @@ TGFX_TEST(CanvasTest, rasterized) {
texture = Resource::Find<Texture>(context, rasterImageUniqueKey);
EXPECT_TRUE(texture != nullptr);
canvas->clear();
rasterImage = rasterImage->makeMipmapped(false);
EXPECT_FALSE(rasterImage->hasMipmaps());
rasterImage = rasterImage->makeRasterized(2.0f, sampling);
EXPECT_FALSE(rasterImage->hasMipmaps());
rasterImage = rasterImage->makeMipmapped(true);
EXPECT_EQ(rasterImage->width(), 908);
EXPECT_EQ(rasterImage->height(), 1210);
canvas->drawImage(rasterImage, 100, 100);
Expand Down
Loading