1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "third_party/blink/renderer/platform/graphics/draw_looper_builder.h"
32 
33 #include <memory>
34 #include "base/memory/scoped_refptr.h"
35 #include "third_party/blink/renderer/platform/geometry/float_size.h"
36 #include "third_party/blink/renderer/platform/graphics/color.h"
37 #include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h"
38 #include "third_party/skia/include/core/SkColor.h"
39 #include "third_party/skia/include/core/SkColorFilter.h"
40 #include "third_party/skia/include/core/SkDrawLooper.h"
41 #include "third_party/skia/include/core/SkMaskFilter.h"
42 #include "third_party/skia/include/core/SkPaint.h"
43 
44 namespace blink {
45 
46 DrawLooperBuilder::DrawLooperBuilder() = default;
47 
48 DrawLooperBuilder::~DrawLooperBuilder() = default;
49 
DetachDrawLooper()50 sk_sp<SkDrawLooper> DrawLooperBuilder::DetachDrawLooper() {
51   return sk_draw_looper_builder_.detach();
52 }
53 
AddUnmodifiedContent()54 void DrawLooperBuilder::AddUnmodifiedContent() {
55   SkLayerDrawLooper::LayerInfo info;
56   sk_draw_looper_builder_.addLayerOnTop(info);
57 }
58 
AddShadow(const FloatSize & offset,float blur,const Color & color,ShadowTransformMode shadow_transform_mode,ShadowAlphaMode shadow_alpha_mode)59 void DrawLooperBuilder::AddShadow(const FloatSize& offset,
60                                   float blur,
61                                   const Color& color,
62                                   ShadowTransformMode shadow_transform_mode,
63                                   ShadowAlphaMode shadow_alpha_mode) {
64   DCHECK_GE(blur, 0);
65 
66   // Detect when there's no effective shadow.
67   if (!color.Alpha())
68     return;
69 
70   SkColor sk_color = color.Rgb();
71 
72   SkLayerDrawLooper::LayerInfo info;
73 
74   switch (shadow_alpha_mode) {
75     case kShadowRespectsAlpha:
76       info.fColorMode = SkBlendMode::kDst;
77       break;
78     case kShadowIgnoresAlpha:
79       info.fColorMode = SkBlendMode::kSrc;
80       break;
81     default:
82       NOTREACHED();
83   }
84 
85   if (blur)
86     info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit;  // our blur
87   info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit;
88   info.fOffset.set(offset.Width(), offset.Height());
89   info.fPostTranslate = (shadow_transform_mode == kShadowIgnoresTransforms);
90 
91   SkPaint* paint = sk_draw_looper_builder_.addLayerOnTop(info);
92 
93   if (blur) {
94     const auto sigma = BlurRadiusToStdDev(blur);
95     const bool respectCTM = shadow_transform_mode != kShadowIgnoresTransforms;
96     paint->setMaskFilter(
97         SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
98   }
99 
100   paint->setColorFilter(SkColorFilters::Blend(sk_color, SkBlendMode::kSrcIn));
101 }
102 
103 }  // namespace blink
104