diff --git a/src/rendering/graphics/Modifier.cpp b/src/rendering/graphics/Modifier.cpp index 72c1a84568..2e47441d04 100644 --- a/src/rendering/graphics/Modifier.cpp +++ b/src/rendering/graphics/Modifier.cpp @@ -200,8 +200,11 @@ bool BlendModifier::hitTest(RenderCache*, float, float) const { void BlendModifier::applyToGraphic(Canvas* canvas, RenderCache* cache, std::shared_ptr graphic) const { canvas->save(); - canvas->concatAlpha(alpha); - canvas->concatBlendMode(ToBlend(blendMode)); + auto newAlpha = OpacityConcat(canvas->getAlpha(), alpha); + canvas->setAlpha(newAlpha); + if (blendMode != BlendMode::Normal) { + canvas->setBlendMode(ToBlend(blendMode)); + } graphic->draw(canvas, cache); canvas->restore(); } diff --git a/tgfx/include/gpu/Canvas.h b/tgfx/include/gpu/Canvas.h index f4a2e25144..629029a560 100644 --- a/tgfx/include/gpu/Canvas.h +++ b/tgfx/include/gpu/Canvas.h @@ -95,14 +95,24 @@ class Canvas { void concat(const Matrix& matrix); /** - * Replaces the global alpha with specified alpha premultiplied with existing alpha. + * Returns the current global alpha. */ - void concatAlpha(Opacity alpha); + Opacity getAlpha() const; /** - * Replaces the global blend mode with specified blend mode if it is not BlendMode::Normal. + * Replaces the global alpha with specified newAlpha. */ - void concatBlendMode(Blend blendMode); + void setAlpha(Opacity newAlpha); + + /** + * Returns the current global blend mode. + */ + Blend getBlendMode() const; + + /** + * Replaces the global blend mode with specified new blend mode. + */ + void setBlendMode(Blend blendMode); /** * Returns the current total clip. diff --git a/tgfx/src/gpu/Canvas.cpp b/tgfx/src/gpu/Canvas.cpp index 26a89c38d7..fbfe8afd2e 100644 --- a/tgfx/src/gpu/Canvas.cpp +++ b/tgfx/src/gpu/Canvas.cpp @@ -61,14 +61,19 @@ void Canvas::concat(const Matrix& matrix) { onSetMatrix(globalPaint.matrix); } -void Canvas::concatAlpha(Opacity alpha) { - globalPaint.alpha = OpacityConcat(globalPaint.alpha, alpha); +Opacity Canvas::getAlpha() const { + return globalPaint.alpha; } -void Canvas::concatBlendMode(Blend blendMode) { - if (blendMode == Blend::SrcOver) { - return; - } +void Canvas::setAlpha(Opacity newAlpha) { + globalPaint.alpha = newAlpha; +} + +Blend Canvas::getBlendMode() const { + return globalPaint.blendMode; +} + +void Canvas::setBlendMode(Blend blendMode) { globalPaint.blendMode = blendMode; } diff --git a/tgfx/src/gpu/opengl/GLCanvas.cpp b/tgfx/src/gpu/opengl/GLCanvas.cpp index 14464d4bd3..2ba26ef32d 100644 --- a/tgfx/src/gpu/opengl/GLCanvas.cpp +++ b/tgfx/src/gpu/opengl/GLCanvas.cpp @@ -260,7 +260,7 @@ void GLCanvas::drawColorGlyphs(const GlyphID glyphIDs[], const Point positions[] glyphMatrix.postTranslate(position.x, position.y); save(); concat(glyphMatrix); - concatAlpha(paint.getAlpha()); + globalPaint.alpha = OpacityConcat(globalPaint.alpha, paint.getAlpha()); auto texture = glyphBuffer->makeTexture(getContext()); drawTexture(texture.get(), nullptr, false); restore();