• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
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 "include/core/SkColorPriv.h"
9 #include "include/core/SkString.h"
10 #include "include/effects/SkBlurMaskFilter.h"
11 #include "include/private/SkTPin.h"
12 #include "src/core/SkBlurMask.h"
13 #include "src/core/SkReadBuffer.h"
14 #include "src/core/SkWriteBuffer.h"
15 #include "src/effects/SkEmbossMask.h"
16 #include "src/effects/SkEmbossMaskFilter.h"
17 
normalize3(SkScalar dst[3],const SkScalar src[3])18 static void normalize3(SkScalar dst[3], const SkScalar src[3]) {
19     SkScalar mag = SkScalarSquare(src[0]) + SkScalarSquare(src[1]) + SkScalarSquare(src[2]);
20     SkScalar scale = SkScalarInvert(SkScalarSqrt(mag));
21 
22     for (int i = 0; i < 3; i++) {
23         dst[i] = src[i] * scale;
24     }
25 }
26 
Make(SkScalar blurSigma,const Light & light)27 sk_sp<SkMaskFilter> SkEmbossMaskFilter::Make(SkScalar blurSigma, const Light& light) {
28     if (!SkScalarIsFinite(blurSigma) || blurSigma <= 0) {
29         return nullptr;
30     }
31 
32     Light newLight = light;
33     normalize3(newLight.fDirection, light.fDirection);
34     if (!SkScalarsAreFinite(newLight.fDirection, 3)) {
35         return nullptr;
36     }
37 
38     return sk_sp<SkMaskFilter>(new SkEmbossMaskFilter(blurSigma, newLight));
39 }
40 
41 #ifdef SK_SUPPORT_LEGACY_EMBOSSMASKFILTER
MakeEmboss(SkScalar blurSigma,const SkScalar direction[3],SkScalar ambient,SkScalar specular)42 sk_sp<SkMaskFilter> SkBlurMaskFilter::MakeEmboss(SkScalar blurSigma, const SkScalar direction[3],
43                                                  SkScalar ambient, SkScalar specular) {
44     if (direction == nullptr) {
45         return nullptr;
46     }
47 
48     SkEmbossMaskFilter::Light   light;
49 
50     memcpy(light.fDirection, direction, sizeof(light.fDirection));
51     // ambient should be 0...1 as a scalar
52     light.fAmbient = SkUnitScalarClampToByte(ambient);
53     // specular should be 0..15.99 as a scalar
54     static const SkScalar kSpecularMultiplier = SkIntToScalar(255) / 16;
55     light.fSpecular = static_cast<U8CPU>(SkTPin(specular, 0.0f, 16.0f) * kSpecularMultiplier + 0.5);
56 
57     return SkEmbossMaskFilter::Make(blurSigma, light);
58 }
59 #endif
60 
61 ///////////////////////////////////////////////////////////////////////////////
62 
SkEmbossMaskFilter(SkScalar blurSigma,const Light & light)63 SkEmbossMaskFilter::SkEmbossMaskFilter(SkScalar blurSigma, const Light& light)
64     : fLight(light), fBlurSigma(blurSigma)
65 {
66     SkASSERT(fBlurSigma > 0);
67     SkASSERT(SkScalarsAreFinite(fLight.fDirection, 3));
68 }
69 
getFormat() const70 SkMask::Format SkEmbossMaskFilter::getFormat() const {
71     return SkMask::k3D_Format;
72 }
73 
filterMask(SkMask * dst,const SkMask & src,const SkMatrix & matrix,SkIPoint * margin) const74 bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src,
75                                     const SkMatrix& matrix, SkIPoint* margin) const {
76     if (src.fFormat != SkMask::kA8_Format) {
77         return false;
78     }
79 
80     SkScalar sigma = matrix.mapRadius(fBlurSigma);
81 
82     if (!SkBlurMask::BoxBlur(dst, src, sigma, kInner_SkBlurStyle)) {
83         return false;
84     }
85 
86     dst->fFormat = SkMask::k3D_Format;
87     if (margin) {
88         margin->set(SkScalarCeilToInt(3*sigma), SkScalarCeilToInt(3*sigma));
89     }
90 
91     if (src.fImage == nullptr) {
92         return true;
93     }
94 
95     // create a larger buffer for the other two channels (should force fBlur to do this for us)
96 
97     {
98         uint8_t* alphaPlane = dst->fImage;
99         size_t totalSize = dst->computeTotalImageSize();
100         if (totalSize == 0) {
101             return false;  // too big to allocate, abort
102         }
103         size_t planeSize = dst->computeImageSize();
104         SkASSERT(planeSize != 0);  // if totalSize didn't overflow, this can't either
105         dst->fImage = SkMask::AllocImage(totalSize);
106         memcpy(dst->fImage, alphaPlane, planeSize);
107         SkMask::FreeImage(alphaPlane);
108     }
109 
110     // run the light direction through the matrix...
111     Light   light = fLight;
112     matrix.mapVectors((SkVector*)(void*)light.fDirection,
113                       (SkVector*)(void*)fLight.fDirection, 1);
114 
115     // now restore the length of the XY component
116     // cast to SkVector so we can call setLength (this double cast silences alias warnings)
117     SkVector* vec = (SkVector*)(void*)light.fDirection;
118     vec->setLength(light.fDirection[0],
119                    light.fDirection[1],
120                    SkPoint::Length(fLight.fDirection[0], fLight.fDirection[1]));
121 
122     SkEmbossMask::Emboss(dst, light);
123 
124     // restore original alpha
125     memcpy(dst->fImage, src.fImage, src.computeImageSize());
126 
127     return true;
128 }
129 
CreateProc(SkReadBuffer & buffer)130 sk_sp<SkFlattenable> SkEmbossMaskFilter::CreateProc(SkReadBuffer& buffer) {
131     Light light;
132     if (buffer.readByteArray(&light, sizeof(Light))) {
133         light.fPad = 0; // for the font-cache lookup to be clean
134         const SkScalar sigma = buffer.readScalar();
135         return Make(sigma, light);
136     }
137     return nullptr;
138 }
139 
flatten(SkWriteBuffer & buffer) const140 void SkEmbossMaskFilter::flatten(SkWriteBuffer& buffer) const {
141     Light tmpLight = fLight;
142     tmpLight.fPad = 0;    // for the font-cache lookup to be clean
143     buffer.writeByteArray(&tmpLight, sizeof(tmpLight));
144     buffer.writeScalar(fBlurSigma);
145 }
146