blob: 981f761b9e6dde39635ec67f29744fbd776e5757 [file] [log] [blame]
* Copyright 2018 Google Inc.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
#include "include/effects/SkShaderMaskFilter.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkFlattenable.h"
#include "include/core/SkMaskFilter.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkShader.h"
#include "src/core/SkMask.h"
#include "src/core/SkMaskFilterBase.h"
#include "src/core/SkReadBuffer.h"
#include "src/core/SkWriteBuffer.h"
#include "src/shaders/SkShaderBase.h"
#include <cstdint>
#include <cstring>
#include <memory>
#include <utility>
class SkMatrix;
#include "src/gpu/ganesh/GrFragmentProcessor.h"
struct GrFPArgs;
class SkShaderMF : public SkMaskFilterBase {
SkShaderMF(sk_sp<SkShader> shader) : fShader(std::move(shader)) {}
SkMask::Format getFormat() const override { return SkMask::kA8_Format; }
bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&,
SkIPoint* margin) const override;
void computeFastBounds(const SkRect& src, SkRect* dst) const override {
*dst = src;
bool asABlur(BlurRec*) const override { return false; }
std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(const GrFPArgs&) const override;
bool onHasFragmentProcessor() const override;
sk_sp<SkShader> fShader;
void flatten(SkWriteBuffer&) const override;
friend class SkShaderMaskFilter;
using INHERITED = SkMaskFilter;
sk_sp<SkFlattenable> SkShaderMF::CreateProc(SkReadBuffer& buffer) {
return SkShaderMaskFilter::Make(buffer.readShader());
void SkShaderMF::flatten(SkWriteBuffer& buffer) const {
static void rect_memcpy(void* dst, size_t dstRB, const void* src, size_t srcRB,
size_t copyBytes, int rows) {
for (int i = 0; i < rows; ++i) {
memcpy(dst, src, copyBytes);
dst = (char*)dst + dstRB;
src = (const char*)src + srcRB;
bool SkShaderMF::filterMask(SkMask* dst, const SkMask& src, const SkMatrix& ctm,
SkIPoint* margin) const {
if (src.fFormat != SkMask::kA8_Format) {
return false;
if (margin) {
margin->set(0, 0);
dst->fBounds = src.fBounds;
dst->fRowBytes = src.fBounds.width(); // need alignment?
dst->fFormat = SkMask::kA8_Format;
if (src.fImage == nullptr) {
dst->fImage = nullptr;
return true;
size_t size = dst->computeImageSize();
if (0 == size) {
return false; // too big to allocate, abort
// Allocate and initialize dst image with a copy of the src image
dst->fImage = SkMask::AllocImage(size);
rect_memcpy(dst->fImage, dst->fRowBytes, src.fImage, src.fRowBytes,
src.fBounds.width() * sizeof(uint8_t), src.fBounds.height());
// Now we have a dst-mask, just need to setup a canvas and draw into it
SkBitmap bitmap;
if (!bitmap.installMaskPixels(*dst)) {
return false;
SkPaint paint;
// this blendmode is the trick: we only draw the shader where the mask is
SkCanvas canvas(bitmap);
canvas.translate(-SkIntToScalar(dst->fBounds.fLeft), -SkIntToScalar(dst->fBounds.fTop));
return true;
std::unique_ptr<GrFragmentProcessor> SkShaderMF::onAsFragmentProcessor(const GrFPArgs& args) const {
return GrFragmentProcessor::MulInputByChildAlpha(as_SB(fShader)->asFragmentProcessor(args));
bool SkShaderMF::onHasFragmentProcessor() const {
return true;
sk_sp<SkMaskFilter> SkShaderMaskFilter::Make(sk_sp<SkShader> shader) {
return shader ? sk_sp<SkMaskFilter>(new SkShaderMF(std::move(shader))) : nullptr;
void SkShaderMaskFilter::RegisterFlattenables() { SK_REGISTER_FLATTENABLE(SkShaderMF); }