| 1 |
/* |
| 2 |
* Copyright 2018 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/effects/SkShaderMaskFilterImpl.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkBlendMode.h" |
| 12 |
#include "include/core/SkCanvas.h" |
| 13 |
#include "include/core/SkFlattenable.h" |
| 14 |
#include "include/core/SkImageFilter.h" |
| 15 |
#include "include/core/SkMaskFilter.h" |
| 16 |
#include "include/core/SkPaint.h" |
| 17 |
#include "include/core/SkPoint.h" |
| 18 |
#include "include/core/SkRect.h" |
| 19 |
#include "include/core/SkRefCnt.h" |
| 20 |
#include "include/core/SkScalar.h" |
| 21 |
#include "include/core/SkShader.h" |
| 22 |
#include "include/effects/SkImageFilters.h" |
| 23 |
#include "include/effects/SkShaderMaskFilter.h" |
| 24 |
#include "src/core/SkMask.h" |
| 25 |
#include "src/core/SkReadBuffer.h" |
| 26 |
#include "src/core/SkWriteBuffer.h" |
| 27 |
|
| 28 |
#include <cstdint> |
| 29 |
#include <cstring> |
| 30 |
#include <utility> |
| 31 |
|
| 32 |
class SkMatrix; |
| 33 |
|
| 34 |
sk_sp<SkFlattenable> SkShaderMaskFilterImpl::CreateProc(SkReadBuffer& buffer) { |
| 35 |
return SkShaderMaskFilter::Make(buffer.readShader()); |
| 36 |
} |
| 37 |
|
| 38 |
void SkShaderMaskFilterImpl::flatten(SkWriteBuffer& buffer) const { |
| 39 |
buffer.writeFlattenable(fShader.get()); |
| 40 |
} |
| 41 |
|
| 42 |
static void rect_memcpy(void* dst, size_t dstRB, const void* src, size_t srcRB, |
| 43 |
size_t copyBytes, int rows) { |
| 44 |
for (int i = 0; i < rows; ++i) { |
| 45 |
memcpy(dst, src, copyBytes); |
| 46 |
dst = (char*)dst + dstRB; |
| 47 |
src = (const char*)src + srcRB; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
bool SkShaderMaskFilterImpl::filterMask(SkMaskBuilder* dst, const SkMask& src, const SkMatrix& ctm, |
| 52 |
SkIPoint* margin) const { |
| 53 |
if (src.fFormat != SkMask::kA8_Format) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
if (margin) { |
| 58 |
margin->set(0, 0); |
| 59 |
} |
| 60 |
dst->bounds() = src.fBounds; |
| 61 |
dst->rowBytes() = src.fBounds.width(); // need alignment? |
| 62 |
dst->format() = SkMask::kA8_Format; |
| 63 |
|
| 64 |
if (src.fImage == nullptr) { |
| 65 |
dst->image() = nullptr; |
| 66 |
return true; |
| 67 |
} |
| 68 |
size_t size = dst->computeImageSize(); |
| 69 |
if (0 == size) { |
| 70 |
return false; // too big to allocate, abort |
| 71 |
} |
| 72 |
|
| 73 |
// Allocate and initialize dst image with a copy of the src image |
| 74 |
dst->image() = SkMaskBuilder::AllocImage(size); |
| 75 |
rect_memcpy(dst->image(), dst->fRowBytes, src.fImage, src.fRowBytes, |
| 76 |
src.fBounds.width() * sizeof(uint8_t), src.fBounds.height()); |
| 77 |
|
| 78 |
// Now we have a dst-mask, just need to setup a canvas and draw into it |
| 79 |
SkBitmap bitmap; |
| 80 |
if (!bitmap.installPixels(SkImageInfo::MakeA8(dst->fBounds.width(), dst->fBounds.height()), |
| 81 |
dst->image(), |
| 82 |
dst->fRowBytes)) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
SkPaint paint; |
| 87 |
paint.setShader(fShader); |
| 88 |
// this blendmode is the trick: we only draw the shader where the mask is |
| 89 |
paint.setBlendMode(SkBlendMode::kSrcIn); |
| 90 |
|
| 91 |
SkCanvas canvas(bitmap); |
| 92 |
canvas.translate(-SkIntToScalar(dst->fBounds.fLeft), -SkIntToScalar(dst->fBounds.fTop)); |
| 93 |
canvas.concat(ctm); |
| 94 |
canvas.drawPaint(paint); |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
std::pair<sk_sp<SkImageFilter>, bool> SkShaderMaskFilterImpl::asImageFilter(const SkMatrix&, |
| 99 |
const SkPaint&) const { |
| 100 |
sk_sp<SkImageFilter> filter = SkImageFilters::Shader(fShader); |
| 101 |
return {SkImageFilters::Blend(SkBlendMode::kDstIn, std::move(filter), nullptr), false}; |
| 102 |
} |
| 103 |
|
| 104 |
sk_sp<SkMaskFilter> SkShaderMaskFilter::Make(sk_sp<SkShader> shader) { |
| 105 |
return shader ? sk_sp<SkMaskFilter>(new SkShaderMaskFilterImpl(std::move(shader))) : nullptr; |
| 106 |
} |
| 107 |
|
| 108 |
void SkShaderMaskFilter::RegisterFlattenables() { |
| 109 |
SK_REGISTER_FLATTENABLE(SkShaderMaskFilterImpl); |
| 110 |
// Previous name |
| 111 |
SkFlattenable::Register("SkShaderMF", SkShaderMaskFilterImpl::CreateProc); |
| 112 |
} |