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

Remove the sampling options from the DrawArgs class. #165

Merged
merged 1 commit into from
Feb 28, 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
8 changes: 4 additions & 4 deletions include/tgfx/core/ColorFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class ColorFilter : public Filter {

protected:
std::unique_ptr<FragmentProcessor> onFilterImage(std::shared_ptr<Image> source,
const DrawArgs& args,
const tgfx::Matrix* localMatrix,
TileMode tileModeX,
TileMode tileModeY) const override;
const DrawArgs& args, TileMode tileModeX,
TileMode tileModeY,
const SamplingOptions& sampling,
const tgfx::Matrix* localMatrix) const override;

private:
virtual std::unique_ptr<FragmentProcessor> asFragmentProcessor() const = 0;
Expand Down
8 changes: 4 additions & 4 deletions include/tgfx/core/Filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class Filter {
* The returned processor is in the coordinate space of the source image.
*/
virtual std::unique_ptr<FragmentProcessor> onFilterImage(std::shared_ptr<Image> source,
const DrawArgs& args,
const Matrix* localMatrix,
TileMode tileModeX,
TileMode tileModeY) const = 0;
const DrawArgs& args, TileMode tileModeX,
TileMode tileModeY,
const SamplingOptions& sampling,
const Matrix* localMatrix) const = 0;

friend class FilterImage;
};
Expand Down
7 changes: 3 additions & 4 deletions include/tgfx/core/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,9 @@ class Image {
virtual std::shared_ptr<Image> onMakeRGBAAA(int displayWidth, int displayHeight, int alphaStartX,
int alphaStartY) const;

virtual std::unique_ptr<FragmentProcessor> asFragmentProcessor(const DrawArgs& args,
const Matrix* localMatrix,
TileMode tileModeX,
TileMode tileModeY) const = 0;
virtual std::unique_ptr<FragmentProcessor> asFragmentProcessor(
const DrawArgs& args, TileMode tileModeX, TileMode tileModeY, const SamplingOptions& sampling,
const Matrix* localMatrix) const = 0;

friend class FragmentProcessor;
friend class TransformImage;
Expand Down
7 changes: 4 additions & 3 deletions include/tgfx/core/MaskFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ class MaskFilter : public Filter {

protected:
std::unique_ptr<FragmentProcessor> onFilterImage(std::shared_ptr<Image> source,
const DrawArgs& args, const Matrix* localMatrix,
TileMode tileModeX,
TileMode tileModeY) const override;
const DrawArgs& args, TileMode tileModeX,
TileMode tileModeY,
const SamplingOptions& sampling,
const Matrix* localMatrix) const override;

private:
virtual std::unique_ptr<FragmentProcessor> asFragmentProcessor(
Expand Down
22 changes: 7 additions & 15 deletions src/core/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,7 @@ void Canvas::drawPath(const Path& path, const Paint& paint) {
if (drawAsClear(fillPath, paint)) {
return;
}
auto inputColor = paint.getColor().premultiply();
DrawArgs args(getContext(), surface->options()->renderFlags(), inputColor, localBounds,
state->matrix);
DrawArgs args(surface, paint, localBounds, state->matrix);
auto drawOp = MakeSimplePathOp(fillPath, args);
if (drawOp != nullptr) {
addDrawOp(std::move(drawOp), args, paint);
Expand All @@ -437,7 +435,7 @@ void Canvas::drawPath(const Path& path, const Paint& paint) {
}

void Canvas::drawImage(std::shared_ptr<Image> image, float left, float top, const Paint* paint) {
drawImage(image, Matrix::MakeTrans(left, top), paint);
drawImage(std::move(image), Matrix::MakeTrans(left, top), paint);
}

void Canvas::drawImage(std::shared_ptr<Image> image, const Matrix& matrix, const Paint* paint) {
Expand Down Expand Up @@ -482,10 +480,8 @@ void Canvas::drawImage(std::shared_ptr<Image> image, SamplingOptions sampling, c
if (realPaint.getShader() != nullptr && !image->isAlphaOnly()) {
realPaint.setShader(nullptr);
}
auto inputColor = realPaint.getColor().premultiply();
DrawArgs args(getContext(), surface->options()->renderFlags(), inputColor, localBounds,
state->matrix, sampling);
auto processor = FragmentProcessor::Make(std::move(image), args);
DrawArgs args(surface, realPaint, localBounds, state->matrix);
auto processor = FragmentProcessor::Make(std::move(image), args, sampling);
if (processor == nullptr) {
return;
}
Expand All @@ -511,9 +507,7 @@ void Canvas::drawMask(const Rect& deviceBounds, std::shared_ptr<TextureProxy> te
static_cast<float>(textureProxy->height()) / deviceBounds.height());
auto oldMatrix = state->matrix;
resetMatrix();
auto inputColor = paint.getColor().premultiply();
DrawArgs args(getContext(), surface->options()->renderFlags(), inputColor, deviceBounds,
Matrix::I());
DrawArgs args(surface, paint, deviceBounds, Matrix::I());
auto op = FillRectOp::Make(args.color, args.drawRect, args.viewMatrix, &localMatrix);
auto maskProcessor = FragmentProcessor::MulInputByChildAlpha(
TextureEffect::Make(std::move(textureProxy), SamplingOptions(), &maskLocalMatrix));
Expand Down Expand Up @@ -644,11 +638,9 @@ void Canvas::drawAtlas(std::shared_ptr<Image> atlas, const Matrix matrix[], cons
return;
}
auto realPaint = CleanPaintForDrawImage(paint);
auto inputColor = realPaint.getColor().premultiply();
DrawArgs args(getContext(), surface->options()->renderFlags(), inputColor, drawRect,
state->matrix, sampling);
DrawArgs args(surface, realPaint, drawRect, state->matrix);
for (auto& rectOp : ops) {
auto processor = FragmentProcessor::Make(atlas, args);
auto processor = FragmentProcessor::Make(atlas, args, sampling);
if (colors) {
processor = FragmentProcessor::MulInputByChildAlpha(std::move(processor));
}
Expand Down
16 changes: 6 additions & 10 deletions src/filters/BlurImageFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,9 @@ Rect BlurImageFilter::onFilterBounds(const Rect& srcRect) const {
return srcRect.makeOutset(blurOffset.x * mul, blurOffset.y * mul);
}

std::unique_ptr<FragmentProcessor> BlurImageFilter::onFilterImage(std::shared_ptr<Image> source,
const DrawArgs& args,
const Matrix* localMatrix,
TileMode tileModeX,
TileMode tileModeY) const {
std::unique_ptr<FragmentProcessor> BlurImageFilter::onFilterImage(
std::shared_ptr<Image> source, const DrawArgs& args, TileMode tileModeX, TileMode tileModeY,
const SamplingOptions& sampling, const Matrix* localMatrix) const {
auto inputBounds = Rect::MakeWH(source->width(), source->height());
auto clipBounds = args.drawRect;
if (localMatrix) {
Expand All @@ -119,12 +117,10 @@ std::unique_ptr<FragmentProcessor> BlurImageFilter::onFilterImage(std::shared_pt
if (!applyCropRect(inputBounds, &dstBounds, &clipBounds)) {
return nullptr;
}
DrawArgs imageArgs = args;
imageArgs.sampling = {};
auto processor = FragmentProcessor::Make(source, imageArgs, nullptr, tileMode, tileMode);
auto processor = FragmentProcessor::Make(source, args, tileMode, tileMode, {});
auto imageBounds = dstBounds;
std::vector<std::shared_ptr<RenderTargetProxy>> renderTargets = {};
auto mipmapped = source->hasMipmaps() && args.sampling.mipmapMode != MipmapMode::None;
auto mipmapped = source->hasMipmaps() && sampling.mipmapMode != MipmapMode::None;
auto lastRenderTarget = RenderTargetProxy::Make(
args.context, static_cast<int>(imageBounds.width()), static_cast<int>(imageBounds.height()),
PixelFormat::RGBA_8888, 1, mipmapped);
Expand Down Expand Up @@ -158,6 +154,6 @@ std::unique_ptr<FragmentProcessor> BlurImageFilter::onFilterImage(std::shared_pt
matrix.preConcat(*localMatrix);
}
return TiledTextureEffect::Make(lastRenderTarget->getTextureProxy(), tileModeX, tileModeY,
args.sampling, &matrix);
sampling, &matrix);
}
} // namespace tgfx
7 changes: 4 additions & 3 deletions src/filters/BlurImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ class BlurImageFilter : public ImageFilter {
Rect onFilterBounds(const Rect& srcRect) const override;

std::unique_ptr<FragmentProcessor> onFilterImage(std::shared_ptr<Image> source,
const DrawArgs& args, const Matrix* localMatrix,
TileMode tileModeX,
TileMode tileModeY) const override;
const DrawArgs& args, TileMode tileModeX,
TileMode tileModeY,
const SamplingOptions& sampling,
const Matrix* localMatrix) const override;

void draw(std::shared_ptr<RenderTargetProxy> renderTarget,
std::unique_ptr<FragmentProcessor> imageProcessor, const Rect& imageBounds,
Expand Down
10 changes: 4 additions & 6 deletions src/filters/ColorFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
#include "gpu/ops/DrawOp.h"

namespace tgfx {
std::unique_ptr<FragmentProcessor> ColorFilter::onFilterImage(std::shared_ptr<Image> source,
const DrawArgs& args,
const tgfx::Matrix* localMatrix,
TileMode tileModeX,
TileMode tileModeY) const {
std::unique_ptr<FragmentProcessor> ColorFilter::onFilterImage(
std::shared_ptr<Image> source, const DrawArgs& args, TileMode tileModeX, TileMode tileModeY,
const SamplingOptions& sampling, const tgfx::Matrix* localMatrix) const {
auto imageProcessor =
FragmentProcessor::Make(std::move(source), args, localMatrix, tileModeX, tileModeY);
FragmentProcessor::Make(std::move(source), args, tileModeX, tileModeY, sampling, localMatrix);
if (imageProcessor == nullptr) {
return nullptr;
}
Expand Down
31 changes: 15 additions & 16 deletions src/filters/DropShadowImageFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ Rect DropShadowImageFilter::onFilterBounds(const Rect& srcRect) const {
}

std::unique_ptr<FragmentProcessor> DropShadowImageFilter::onFilterImage(
std::shared_ptr<Image> source, const tgfx::DrawArgs& args, const Matrix* localMatrix,
TileMode tileModeX, TileMode tileModeY) const {
std::shared_ptr<Image> source, const DrawArgs& args, TileMode tileModeX, TileMode tileModeY,
const SamplingOptions& sampling, const Matrix* localMatrix) const {
auto inputBounds = Rect::MakeWH(source->width(), source->height());
auto clipBounds = args.drawRect;
if (localMatrix) {
Expand All @@ -68,18 +68,16 @@ std::unique_ptr<FragmentProcessor> DropShadowImageFilter::onFilterImage(
}
if (dstBounds.contains(clipBounds) ||
(tileModeX == TileMode::Decal && tileModeY == TileMode::Decal)) {
return getFragmentProcessor(std::move(source), args, localMatrix);
return getFragmentProcessor(std::move(source), args, sampling, localMatrix);
}
auto mipmapped = source->hasMipmaps() && args.sampling.mipmapMode != MipmapMode::None;
auto mipmapped = source->hasMipmaps() && sampling.mipmapMode != MipmapMode::None;
auto renderTarget = RenderTargetProxy::Make(args.context, static_cast<int>(dstBounds.width()),
static_cast<int>(dstBounds.height()),
PixelFormat::RGBA_8888, 1, mipmapped);
if (renderTarget == nullptr) {
return nullptr;
}
DrawArgs shadowArgs = args;
shadowArgs.sampling = {};
auto processor = getFragmentProcessor(std::move(source), shadowArgs);
auto processor = getFragmentProcessor(std::move(source), args, {});
if (processor == nullptr) {
return nullptr;
}
Expand All @@ -90,23 +88,24 @@ std::unique_ptr<FragmentProcessor> DropShadowImageFilter::onFilterImage(
if (localMatrix != nullptr) {
matrix.preConcat(*localMatrix);
}
return TiledTextureEffect::Make(renderTarget->getTextureProxy(), tileModeX, tileModeY,
args.sampling, &matrix);
return TiledTextureEffect::Make(renderTarget->getTextureProxy(), tileModeX, tileModeY, sampling,
&matrix);
}

std::unique_ptr<FragmentProcessor> DropShadowImageFilter::getFragmentProcessor(
std::shared_ptr<Image> source, const DrawArgs& args, const Matrix* localMatrix) const {
std::shared_ptr<Image> source, const DrawArgs& args, const SamplingOptions& sampling,
const Matrix* localMatrix) const {
std::unique_ptr<FragmentProcessor> shadowProcessor;
auto shadowMatrix = Matrix::MakeTrans(-dx, -dy);
if (localMatrix != nullptr) {
shadowMatrix.preConcat(*localMatrix);
}
if (blurFilter != nullptr) {
shadowProcessor =
blurFilter->onFilterImage(source, args, &shadowMatrix, TileMode::Decal, TileMode::Decal);
shadowProcessor = blurFilter->onFilterImage(source, args, TileMode::Decal, TileMode::Decal,
sampling, &shadowMatrix);
} else {
shadowProcessor =
FragmentProcessor::Make(source, args, &shadowMatrix, TileMode::Decal, TileMode::Decal);
shadowProcessor = FragmentProcessor::Make(source, args, TileMode::Decal, TileMode::Decal,
sampling, &shadowMatrix);
}
if (shadowProcessor == nullptr) {
return nullptr;
Expand All @@ -117,8 +116,8 @@ std::unique_ptr<FragmentProcessor> DropShadowImageFilter::getFragmentProcessor(
if (shadowOnly) {
return colorShadowProcessor;
}
auto imageProcessor = FragmentProcessor::Make(std::move(source), args, localMatrix,
TileMode::Decal, TileMode::Decal);
auto imageProcessor = FragmentProcessor::Make(std::move(source), args, TileMode::Decal,
TileMode::Decal, sampling, localMatrix);
return XfermodeFragmentProcessor::MakeFromTwoProcessors(
std::move(imageProcessor), std::move(colorShadowProcessor), BlendMode::SrcOver);
}
Expand Down
9 changes: 5 additions & 4 deletions src/filters/DropShadowImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ class DropShadowImageFilter : public ImageFilter {
Rect onFilterBounds(const Rect& srcRect) const override;

std::unique_ptr<FragmentProcessor> onFilterImage(std::shared_ptr<Image> source,
const DrawArgs& args, const Matrix* localMatrix,
TileMode tileModeX,
TileMode tileModeY) const override;
const DrawArgs& args, TileMode tileModeX,
TileMode tileModeY,
const SamplingOptions& sampling,
const Matrix* localMatrix) const override;

std::unique_ptr<FragmentProcessor> getFragmentProcessor(
std::shared_ptr<Image> source, const DrawArgs& args,
std::shared_ptr<Image> source, const DrawArgs& args, const SamplingOptions& sampling,
const Matrix* localMatrix = nullptr) const;
};
} // namespace tgfx
8 changes: 4 additions & 4 deletions src/filters/MaskFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
namespace tgfx {
std::unique_ptr<FragmentProcessor> MaskFilter::onFilterImage(std::shared_ptr<Image> source,
const DrawArgs& args,
const Matrix* localMatrix,
TileMode tileModeX,
TileMode tileModeY) const {
TileMode tileModeX, TileMode tileModeY,
const SamplingOptions& sampling,
const Matrix* localMatrix) const {
auto imageProcessor =
FragmentProcessor::Make(std::move(source), args, localMatrix, tileModeX, tileModeY);
FragmentProcessor::Make(std::move(source), args, tileModeX, tileModeY, sampling, localMatrix);
if (imageProcessor == nullptr) {
return nullptr;
}
Expand Down
13 changes: 9 additions & 4 deletions src/gpu/DrawArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,27 @@
#pragma once

#include "tgfx/core/SamplingOptions.h"
#include "tgfx/gpu/Context.h"
#include "tgfx/gpu/Surface.h"

namespace tgfx {
class DrawArgs {
public:
DrawArgs(Context* context, uint32_t renderFlags, const Color& color, const Rect& drawRect,
const Matrix& viewMatrix = Matrix::I(), const SamplingOptions& sampling = {})
const Matrix& viewMatrix = Matrix::I())
: context(context), renderFlags(renderFlags), color(color), drawRect(drawRect),
viewMatrix(viewMatrix), sampling(sampling) {
viewMatrix(viewMatrix) {
}

DrawArgs(Surface* surface, const Paint& paint, const Rect& drawRect,
const Matrix& viewMatrix = Matrix::I())
: context(surface->getContext()), renderFlags(surface->options()->renderFlags()),
color(paint.getColor().premultiply()), drawRect(drawRect), viewMatrix(viewMatrix) {
}

Context* context = nullptr;
uint32_t renderFlags = 0;
Color color = Color::White();
Rect drawRect = Rect::MakeEmpty();
Matrix viewMatrix = Matrix::I();
SamplingOptions sampling = {};
};
} // namespace tgfx
17 changes: 14 additions & 3 deletions src/gpu/processors/FragmentProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,23 @@
namespace tgfx {
std::unique_ptr<FragmentProcessor> FragmentProcessor::Make(std::shared_ptr<Image> image,
const DrawArgs& args,
const Matrix* localMatrix,
TileMode tileModeX, TileMode tileModeY) {
const SamplingOptions& sampling,
const Matrix* localMatrix) {
if (image == nullptr) {
return nullptr;
}
return image->asFragmentProcessor(args, TileMode::Clamp, TileMode::Clamp, sampling, localMatrix);
}

std::unique_ptr<FragmentProcessor> FragmentProcessor::Make(std::shared_ptr<Image> image,
const tgfx::DrawArgs& args,
TileMode tileModeX, TileMode tileModeY,
const SamplingOptions& sampling,
const Matrix* localMatrix) {
if (image == nullptr) {
return nullptr;
}
return image->asFragmentProcessor(args, localMatrix, tileModeX, tileModeY);
return image->asFragmentProcessor(args, tileModeX, tileModeY, sampling, localMatrix);
}

std::unique_ptr<FragmentProcessor> FragmentProcessor::Make(std::shared_ptr<Shader> shader,
Expand Down
Loading
Loading