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

Rename Image::makeScaled() to Image::makeRasterized() and allow rasterization with a scale factor of 1.0 #392

Merged
merged 5 commits into from
Dec 20, 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
30 changes: 18 additions & 12 deletions include/tgfx/core/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,24 @@ class Image {
std::shared_ptr<Image> makeOriented(Orientation orientation) const;

/**
* Returns an Image scaled by the given factor. The scaled Image will have its own GPU cache at
* the new resolution and will not have mipmaps, even if the original Image does. You can call
* makeMipmapped() on the scaled Image to enable mipmaps. If the scale is greater than 1.0, it
* may result in blurring. Note that calling makeScaled() on an already scaled Image may return
* its internal non-scaled Image if the multiplied scale is 1.0.
* @param scale The factor to scale the Image by.
* @param sampling The sampling options to use when scaling the Image.
* @return The scaled Image. If the scale is 1.0, the original Image is returned. If the scale is
* less than 0, nullptr is returned.
* Returns a rasterized Image scaled by the specified rasterizationScale. A rasterized Image can
* be cached as an independent GPU resource for repeated drawing. By default, an Image directly
* backed by an ImageBuffer, an ImageGenerator, a GPU texture, or a Picture is rasterized. Other
* images aren’t rasterized unless implicitly created by this method. For example, if you create
* a subset Image from a rasterized Image, the subset Image doesn’t create its own GPU cache but
* uses the full resolution cache created by the original Image. If you want the subset Image to
* create its own GPU cache, call makeRasterized() on the subset Image. The returned Image always
* has the same mipmap state as the original Image.
* @param rasterizationScale The factor to scale the Image by when rasterizing. The default value
* is 1.0, indicating that the Image should be rasterized at its current size. If the value is
* greater than 1.0, it may result in blurring.
* @param sampling The sampling options to apply when rasterizing the Image if the
* rasterizationScale is not 1.0.
* @return If the Image is already rasterized and the rasterizationScale is 1.0, the original
* Image is returned. If the rasterizationScale is less than zero, nullptr is returned.
*/
virtual std::shared_ptr<Image> makeScaled(float scale,
const SamplingOptions& sampling = {}) const;
virtual std::shared_ptr<Image> makeRasterized(float rasterizationScale = 1.0f,
const SamplingOptions& sampling = {}) const;

/**
* Returns a filtered Image with the specified filter. The filter has the potential to alter the
Expand Down Expand Up @@ -320,7 +326,7 @@ class Image {
friend class RuntimeImageFilter;
friend class TransformImage;
friend class RGBAAAImage;
friend class ScaleImage;
friend class RasterizedImage;
friend class ImageShader;
};
} // namespace tgfx
2 changes: 1 addition & 1 deletion src/core/filters/DropShadowImageFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Rect DropShadowImageFilter::onFilterBounds(const Rect& srcRect) const {
std::unique_ptr<FragmentProcessor> DropShadowImageFilter::asFragmentProcessor(
std::shared_ptr<Image> source, const FPArgs& args, const SamplingOptions& sampling,
const Matrix* uvMatrix) const {
source = source->makeTextureImage(args.context);
source = source->makeRasterized();
std::unique_ptr<FragmentProcessor> shadowProcessor;
auto shadowMatrix = Matrix::MakeTrans(-dx, -dy);
if (uvMatrix != nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/filters/InnerShadowImageFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ InnerShadowImageFilter::InnerShadowImageFilter(float dx, float dy, float blurrin
std::unique_ptr<FragmentProcessor> InnerShadowImageFilter::asFragmentProcessor(
std::shared_ptr<Image> source, const FPArgs& args, const SamplingOptions& sampling,
const Matrix* uvMatrix) const {
source = source->makeTextureImage(args.context);
source = source->makeRasterized();
// get inverted shadow mask
auto shadowMatrix = Matrix::MakeTrans(-dx, -dy);
if (uvMatrix != nullptr) {
Expand Down
12 changes: 8 additions & 4 deletions src/core/images/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "core/images/FilterImage.h"
#include "core/images/OrientImage.h"
#include "core/images/RGBAAAImage.h"
#include "core/images/ScaleImage.h"
#include "core/images/RasterizedImage.h"
#include "core/images/SubsetImage.h"
#include "core/images/TextureImage.h"
#include "core/utils/Profiling.h"
Expand Down Expand Up @@ -195,9 +195,13 @@ std::shared_ptr<Image> Image::makeSubset(const Rect& subset) const {
return onMakeSubset(rect);
}

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

std::shared_ptr<Image> Image::onMakeSubset(const Rect& subset) const {
Expand Down
12 changes: 12 additions & 0 deletions src/core/images/MipmapImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ MipmapImage::MipmapImage(UniqueKey uniqueKey, std::shared_ptr<ResourceImage> sou
: ResourceImage(std::move(uniqueKey)), source(std::move(source)) {
}

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

std::shared_ptr<Image> MipmapImage::onMakeDecoded(Context* context, bool) const {
TRACE_EVENT;
auto newSource = std::static_pointer_cast<ResourceImage>(source->onMakeDecoded(context, false));
Expand Down
3 changes: 3 additions & 0 deletions src/core/images/MipmapImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class MipmapImage : public ResourceImage {
return true;
}

std::shared_ptr<Image> makeRasterized(float rasterizationScale = 1.0f,
const SamplingOptions& sampling = {}) const override;

protected:
std::shared_ptr<Image> onMakeDecoded(Context* context, bool tryHardware) const override;

Expand Down
2 changes: 1 addition & 1 deletion src/core/images/OrientImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "OrientImage.h"
#include "core/images/ScaleImage.h"
#include "core/images/RasterizedImage.h"
#include "core/images/SubsetImage.h"
#include "core/utils/AddressOf.h"
#include "gpu/ops/DrawOp.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "ScaleImage.h"
#include "RasterizedImage.h"
#include "core/images/SubsetImage.h"
#include "gpu/OpContext.h"
#include "gpu/ops/DrawOp.h"
Expand All @@ -26,68 +26,64 @@ static int GetSize(int size, float scale) {
return static_cast<int>(roundf(static_cast<float>(size) * scale));
}

std::shared_ptr<Image> ScaleImage::MakeFrom(std::shared_ptr<Image> source, float scale,
const SamplingOptions& sampling) {
std::shared_ptr<Image> RasterizedImage::MakeFrom(std::shared_ptr<Image> source,
float rasterizationScale,
const SamplingOptions& sampling) {
TRACE_EVENT;
if (source == nullptr) {
if (source == nullptr || rasterizationScale <= 0) {
return nullptr;
}
auto sourceWidth = source->width();
auto sourceHeight = source->height();
auto width = GetSize(sourceWidth, scale);
auto height = GetSize(sourceHeight, scale);
auto width = GetSize(sourceWidth, rasterizationScale);
auto height = GetSize(sourceHeight, rasterizationScale);
if (width <= 0 || height <= 0) {
return nullptr;
}
if (width == sourceWidth && height == sourceHeight) {
return source;
}
auto result = std::shared_ptr<ScaleImage>(
new ScaleImage(UniqueKey::Make(), std::move(source), scale, sampling));
auto result = std::shared_ptr<RasterizedImage>(
new RasterizedImage(UniqueKey::Make(), std::move(source), rasterizationScale, sampling));
result->weakThis = result;
return result;
}

ScaleImage::ScaleImage(UniqueKey uniqueKey, std::shared_ptr<Image> source, float scale,
const SamplingOptions& sampling)
: OffscreenImage(std::move(uniqueKey)), source(std::move(source)), scale(scale),
sampling((sampling)) {
RasterizedImage::RasterizedImage(UniqueKey uniqueKey, std::shared_ptr<Image> source,
float rasterizationScale, const SamplingOptions& sampling)
: OffscreenImage(std::move(uniqueKey)), source(std::move(source)),
rasterizationScale(rasterizationScale), sampling((sampling)) {
}

int ScaleImage::width() const {
return GetSize(source->width(), scale);
int RasterizedImage::width() const {
return GetSize(source->width(), rasterizationScale);
}

int ScaleImage::height() const {
return GetSize(source->height(), scale);
int RasterizedImage::height() const {
return GetSize(source->height(), rasterizationScale);
}

std::shared_ptr<Image> ScaleImage::makeScaled(float newScale,
const SamplingOptions& sampling) const {
TRACE_EVENT;
return MakeFrom(source, scale * newScale, sampling);
std::shared_ptr<Image> RasterizedImage::makeRasterized(float newScale,
const SamplingOptions& sampling) const {
return MakeFrom(source, rasterizationScale * newScale, sampling);
}

std::shared_ptr<Image> ScaleImage::onMakeDecoded(Context* context, bool) const {
TRACE_EVENT;
std::shared_ptr<Image> RasterizedImage::onMakeDecoded(Context* context, bool) const {
// There is no need to pass tryHardware (disabled) to the source image, as our texture proxy is
// not locked from the source image.
auto newSource = source->onMakeDecoded(context);
if (newSource == nullptr) {
return nullptr;
}
auto newImage =
std::shared_ptr<ScaleImage>(new ScaleImage(uniqueKey, std::move(newSource), scale, sampling));
auto newImage = std::shared_ptr<RasterizedImage>(
new RasterizedImage(uniqueKey, std::move(newSource), rasterizationScale, sampling));
newImage->weakThis = newImage;
return newImage;
}

bool ScaleImage::onDraw(std::shared_ptr<RenderTargetProxy> renderTarget,
uint32_t renderFlags) const {
bool RasterizedImage::onDraw(std::shared_ptr<RenderTargetProxy> renderTarget,
uint32_t renderFlags) const {
auto sourceWidth = source->width();
auto sourceHeight = source->height();
auto scaledWidth = GetSize(sourceWidth, scale);
auto scaledHeight = GetSize(sourceHeight, scale);
auto scaledWidth = GetSize(sourceWidth, rasterizationScale);
auto scaledHeight = GetSize(sourceHeight, rasterizationScale);
auto uvScaleX = static_cast<float>(sourceWidth) / static_cast<float>(scaledWidth);
auto uvScaleY = static_cast<float>(sourceHeight) / static_cast<float>(scaledHeight);
Matrix uvMatrix = Matrix::MakeScale(uvScaleX, uvScaleY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@

namespace tgfx {
/**
* ScaleImage is an image that scales another image.
* RasterizedImage is an image that rasterizes another image with a scale and sampling options.
*/
class ScaleImage : public OffscreenImage {
class RasterizedImage : public OffscreenImage {
public:
static std::shared_ptr<Image> MakeFrom(std::shared_ptr<Image> source, float scale,
/**
* Note that this method always returns a non-mipmapped image.
*/
static std::shared_ptr<Image> MakeFrom(std::shared_ptr<Image> source, float rasterizationScale,
const SamplingOptions& sampling);

int width() const override;
Expand All @@ -41,7 +44,8 @@ class ScaleImage : public OffscreenImage {
return source->isFullyDecoded();
}

std::shared_ptr<Image> makeScaled(float scale, const SamplingOptions& sampling) const override;
std::shared_ptr<Image> makeRasterized(float rasterizationScale = 1.0f,
const SamplingOptions& sampling = {}) const override;

protected:
std::shared_ptr<Image> onMakeDecoded(Context* context, bool tryHardware) const override;
Expand All @@ -50,10 +54,10 @@ class ScaleImage : public OffscreenImage {

private:
std::shared_ptr<Image> source = nullptr;
float scale = 1.0f;
float rasterizationScale = 1.0f;
SamplingOptions sampling = {};

ScaleImage(UniqueKey uniqueKey, std::shared_ptr<Image> source, float scale,
const SamplingOptions& sampling);
RasterizedImage(UniqueKey uniqueKey, std::shared_ptr<Image> source, float rasterizationScale,
const SamplingOptions& sampling);
};
} // namespace tgfx
8 changes: 8 additions & 0 deletions src/core/images/ResourceImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ namespace tgfx {
ResourceImage::ResourceImage(UniqueKey uniqueKey) : uniqueKey(std::move(uniqueKey)) {
}

std::shared_ptr<Image> ResourceImage::makeRasterized(float rasterizationScale,
const SamplingOptions& sampling) const {
if (rasterizationScale == 1.0f) {
return weakThis.lock();
}
return Image::makeRasterized(rasterizationScale, sampling);
}

std::shared_ptr<TextureProxy> ResourceImage::lockTextureProxy(const TPArgs& args) const {
TRACE_EVENT;
auto newArgs = args;
Expand Down
3 changes: 3 additions & 0 deletions src/core/images/ResourceImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class ResourceImage : public Image {
public:
explicit ResourceImage(UniqueKey uniqueKey);

std::shared_ptr<Image> makeRasterized(float rasterizationScale = 1.0f,
const SamplingOptions& sampling = {}) const override;

protected:
UniqueKey uniqueKey = {};

Expand Down
12 changes: 6 additions & 6 deletions test/baseline/version.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
"mipmap_nearest": "d010fb8",
"mipmap_none": "d010fb8",
"path": "6fd4617",
"rasterized": "5a971dd",
"rasterized_mipmap": "5a971dd",
"rasterized_scale_up": "5a971dd",
"saveLayer": "3888c19",
"scaleImage": "2028a1b",
"scaleImage_mipmap": "2028a1b",
"scaleImage_scale_up": "2028a1b",
"shape": "bc64712",
"text_shape": "b062b9a",
"tile_mode_normal": "8cb853c",
Expand All @@ -70,13 +70,13 @@
"ComposeImageFilter2": "2028a1b",
"ImageFilterShader": "2028a1b",
"ModeColorFilter": "c2b0b18",
"RuntimeEffect": "6f4a968",
"RuntimeEffect": "d93c573",
"blur": "4d6ab20",
"dropShadow": "a30de8b",
"greyColorMatrix": "a30de8b",
"identityMatrix": "a30de8b",
"innerShadow": "01cce38",
"shaderMaskFilter": "ded3c91"
"shaderMaskFilter": "d93c573"
},
"GlyphFaceTest": {
"GlyphFaceSimple": "4032de2",
Expand All @@ -96,7 +96,7 @@
"draw_text": "b062b9a",
"dropShadow": "43cd416",
"filterClip": "43cd416",
"filterTest": "43cd416",
"filterTest": "44166b7",
"getBounds": "3888c19",
"getLayersUnderPoint": "b062b9a",
"greyColorMatrix": "a1605b2",
Expand Down
Loading
Loading