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

Add a isOpaque() method to the FillStyle class. #280

Merged
merged 3 commits into from
Oct 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
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ endif ()

if (IOS)
option(TGFX_USE_WEBP_DECODE "Enable embedded WEBP decoding support" ON)
elseif(WEB)
elseif (WEB)
option(TGFX_USE_WEBP_DECODE "Enable embedded WEBP decoding support" ON)
if (EMSCRIPTEN_PTHREADS)
option(TGFX_USE_PNG_DECODE "Enable embedded PNG decoding support" ON)
option(TGFX_USE_JPEG_DECODE "Enable embedded JPEG decoding support" ON)
ENDIF()
ENDIF ()
elseif (NOT ANDROID AND NOT OHOS)
option(TGFX_USE_PNG_DECODE "Enable embedded PNG decoding support" ON)
option(TGFX_USE_PNG_ENCODE "Enable embedded PNG encoding support" ON)
Expand Down
87 changes: 87 additions & 0 deletions src/core/FillStyle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "FillStyle.h"
#include "gpu/Blend.h"

namespace tgfx {
enum class OpacityType {
// The opacity is unknown
Unknown,
// The src color is known to be opaque (alpha == 255)
Opaque,
// The src color is known to be fully transparent (color == 0)
TransparentBlack,
// The src alpha is known to be fully transparent (alpha == 0)
TransparentAlpha,
};

static OpacityType GetOpacityType(const Color& color, const Shader* shader) {
auto alpha = color.alpha;
if (alpha == 1.0f && (!shader || shader->isOpaque())) {
return OpacityType::Opaque;
}
if (alpha == 0.0f) {
if (shader || color.red != 0.0f || color.green != 0.0f || color.blue != 0.0f) {
return OpacityType::TransparentAlpha;
}
return OpacityType::TransparentBlack;
}
return OpacityType::Unknown;
}

static bool BlendModeIsOpaque(BlendMode mode, OpacityType srcColorOpacity) {
BlendInfo blendInfo = {};
if (!BlendModeAsCoeff(mode, &blendInfo)) {
return false;
}
switch (blendInfo.srcBlend) {
case BlendModeCoeff::DA:
case BlendModeCoeff::DC:
case BlendModeCoeff::IDA:
case BlendModeCoeff::IDC:
return false;
default:
break;
}
switch (blendInfo.dstBlend) {
case BlendModeCoeff::Zero:
return true;
case BlendModeCoeff::ISA:
return srcColorOpacity == OpacityType::Opaque;
case BlendModeCoeff::SA:
return srcColorOpacity == OpacityType::TransparentBlack ||
srcColorOpacity == OpacityType::TransparentAlpha;
case BlendModeCoeff::SC:
return srcColorOpacity == OpacityType::TransparentBlack;
default:
return false;
}
}

bool FillStyle::isOpaque() const {
if (maskFilter) {
return false;
}
if (colorFilter && !colorFilter->isAlphaUnchanged()) {
return false;
}
return BlendModeIsOpaque(blendMode, GetOpacityType(color, shader.get()));
}

} // namespace tgfx
5 changes: 5 additions & 0 deletions src/core/FillStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,10 @@ class FillStyle {
* The blend mode used to combine the fill with the destination pixels.
*/
BlendMode blendMode = BlendMode::SrcOver;

/**
* Returns true if the FillStyle is guaranteed to produce only opaque colors.
*/
bool isOpaque() const;
};
} // namespace tgfx
15 changes: 1 addition & 14 deletions src/core/images/PictureImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,9 @@ static std::shared_ptr<Image> GetEquivalentImage(const Record* record, int width

static bool CheckStyleAndClip(const FillStyle& style, const Path& clip, int width, int height,
const Matrix* matrix) {
if (style.colorFilter || style.maskFilter) {
if (!style.isOpaque()) {
return false;
}
switch (style.blendMode) {
case BlendMode::Clear:
case BlendMode::Dst:
case BlendMode::SrcIn:
case BlendMode::SrcATop:
case BlendMode::DstOver:
case BlendMode::DstIn:
case BlendMode::DstOut:
case BlendMode::DstATop:
return false;
default:
break;
}
if (clip.isEmpty() && clip.isInverseFillType()) {
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/gpu/Blend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
namespace tgfx {
bool BlendModeAsCoeff(BlendMode mode, BlendInfo* blendInfo) {
// clang-format off
static constexpr std::pair<BlendModeCoeff, BlendModeCoeff> kCoeffs[] = {
static constexpr std::pair<BlendModeCoeff, BlendModeCoeff> Coeffs[] = {
// For Porter-Duff blend functions, color = src * src coeff + dst * dst coeff
// src coeff dst coeff blend func
// ------------------- -------------------- ----------
Expand All @@ -48,8 +48,8 @@ bool BlendModeAsCoeff(BlendMode mode, BlendInfo* blendInfo) {
return false;
}
if (blendInfo != nullptr) {
blendInfo->srcBlend = kCoeffs[static_cast<int>(mode)].first;
blendInfo->dstBlend = kCoeffs[static_cast<int>(mode)].second;
blendInfo->srcBlend = Coeffs[static_cast<int>(mode)].first;
blendInfo->dstBlend = Coeffs[static_cast<int>(mode)].second;
}
return true;
}
Expand Down
67 changes: 2 additions & 65 deletions src/gpu/RenderContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,60 +45,6 @@ static constexpr int AA_TESSELLATOR_BUFFER_SIZE_FACTOR = 170;
*/
static constexpr float BOUNDS_TOLERANCE = 1e-3f;

enum class SrcColorOpacity {
Unknown,
// The src color is known to be opaque (alpha == 255)
Opaque,
// The src color is known to be fully transparent (color == 0)
TransparentBlack,
// The src alpha is known to be fully transparent (alpha == 0)
TransparentAlpha,
};

static SrcColorOpacity GetFillColorOpacity(const FillStyle& style) {
auto alpha = style.color.alpha;
const auto& color = style.color;
if (alpha == 1.0f && (!style.shader || style.shader->isOpaque())) {
return SrcColorOpacity::Opaque;
} else if (alpha == 0.0f) {
if (style.shader || color.red != 0.0f || color.green != 0.0f || color.blue != 0.0f) {
return SrcColorOpacity::TransparentAlpha;
} else {
return SrcColorOpacity::TransparentBlack;
}
}
return SrcColorOpacity::Unknown;
}

static bool BlendModeIsOpaque(BlendMode mode, SrcColorOpacity opacityType) {
BlendInfo blendInfo = {};
if (!BlendModeAsCoeff(mode, &blendInfo)) {
return false;
}
switch (blendInfo.srcBlend) {
case BlendModeCoeff::DA:
case BlendModeCoeff::DC:
case BlendModeCoeff::IDA:
case BlendModeCoeff::IDC:
return false;
default:
break;
}
switch (blendInfo.dstBlend) {
case BlendModeCoeff::Zero:
return true;
case BlendModeCoeff::ISA:
return opacityType == SrcColorOpacity::Opaque;
case BlendModeCoeff::SA:
return opacityType == SrcColorOpacity::TransparentBlack ||
opacityType == SrcColorOpacity::TransparentAlpha;
case BlendModeCoeff::SC:
return opacityType == SrcColorOpacity::TransparentBlack;
default:
return false;
}
}

RenderContext::RenderContext(std::shared_ptr<RenderTargetProxy> renderTargetProxy,
uint32_t renderFlags)
: renderFlags(renderFlags) {
Expand Down Expand Up @@ -159,13 +105,10 @@ static bool HasColorOnly(const FillStyle& style) {
}

bool RenderContext::drawAsClear(const Rect& rect, const MCState& state, const FillStyle& style) {
if (!HasColorOnly(style) || !state.matrix.rectStaysRect()) {
if (!HasColorOnly(style) || !style.isOpaque() || !state.matrix.rectStaysRect()) {
return false;
}
auto color = style.color;
if (!BlendModeIsOpaque(style.blendMode, GetFillColorOpacity(style))) {
return false;
}
auto bounds = rect;
state.matrix.mapRect(&bounds);
auto [clipRect, useScissor] = getClipRect(state.clip, &bounds);
Expand Down Expand Up @@ -566,13 +509,7 @@ bool RenderContext::wouldOverwriteEntireRT(const Rect& localBounds, const MCStat
if (!deviceRect.contains(rtRect)) {
return false;
}
if (style.maskFilter) {
return false;
}
if (style.colorFilter && style.colorFilter->isAlphaUnchanged()) {
return false;
}
return BlendModeIsOpaque(style.blendMode, GetFillColorOpacity(style));
return style.isOpaque();
}

void RenderContext::replaceRenderTarget(std::shared_ptr<RenderTargetProxy> newRenderTargetProxy) {
Expand Down
4 changes: 2 additions & 2 deletions test/baseline/version.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"NothingToDraw": "d010fb8",
"Picture": "7fd9cd9",
"PictureImage": "8a2ac4d",
"PictureImage_Path": "418393d",
"PictureImage_Text": "418393d",
"PictureImage_Path": "2212c4e",
"PictureImage_Text": "2212c4e",
"blendMode": "43cd416",
"drawImage": "8cb853c",
"filter_mode_linear": "d010fb8",
Expand Down
6 changes: 6 additions & 0 deletions test/src/CanvasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ TGFX_TEST(CanvasTest, Picture) {
EXPECT_TRUE(Baseline::Compare(surface, "CanvasTest/PictureImage"));

canvas = recorder.beginRecording();
paint.reset();
canvas->drawSimpleText("Hello TGFX~", 0, 0, font, paint);
auto textRecord = recorder.finishRecordingAsPicture();
bounds = textRecord->getBounds();
Expand All @@ -951,6 +952,11 @@ TGFX_TEST(CanvasTest, Picture) {
EXPECT_TRUE(Baseline::Compare(surface, "CanvasTest/PictureImage_Text"));

canvas = recorder.beginRecording();
path.reset();
path.addRect(Rect::MakeXYWH(0, 0, 100, 100));
matrix.reset();
matrix.postRotate(30, 50, 50);
path.transform(matrix);
canvas->drawPath(path, paint);
auto patRecord = recorder.finishRecordingAsPicture();
bounds = patRecord->getBounds();
Expand Down