1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stddef.h>
6 #include <algorithm>
7 
8 #include "cc/paint/render_surface_filters.h"
9 
10 #include "base/numerics/math_constants.h"
11 #include "cc/paint/filter_operation.h"
12 #include "cc/paint/filter_operations.h"
13 #include "cc/paint/paint_filter.h"
14 #include "third_party/skia/include/core/SkColorFilter.h"
15 #include "third_party/skia/include/core/SkImageFilter.h"
16 #include "third_party/skia/include/core/SkRegion.h"
17 #include "third_party/skia/include/effects/SkAlphaThresholdFilter.h"
18 #include "third_party/skia/include/effects/SkColorFilterImageFilter.h"
19 #include "third_party/skia/include/effects/SkColorMatrixFilter.h"
20 #include "third_party/skia/include/effects/SkComposeImageFilter.h"
21 #include "third_party/skia/include/effects/SkDropShadowImageFilter.h"
22 #include "third_party/skia/include/effects/SkMagnifierImageFilter.h"
23 #include "ui/gfx/geometry/size_f.h"
24 #include "ui/gfx/skia_util.h"
25 
26 namespace cc {
27 
28 namespace {
29 
GetBrightnessMatrix(float amount,float matrix[20])30 void GetBrightnessMatrix(float amount, float matrix[20]) {
31   // Spec implementation
32   // (http://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#brightnessEquivalent)
33   // <feFunc[R|G|B] type="linear" slope="[amount]">
34   memset(matrix, 0, 20 * sizeof(float));
35   matrix[0] = matrix[6] = matrix[12] = amount;
36   matrix[18] = 1.f;
37 }
38 
GetSaturatingBrightnessMatrix(float amount,float matrix[20])39 void GetSaturatingBrightnessMatrix(float amount, float matrix[20]) {
40   // Legacy implementation used by internal clients.
41   // <feFunc[R|G|B] type="linear" intercept="[amount]"/>
42   memset(matrix, 0, 20 * sizeof(float));
43   matrix[0] = matrix[6] = matrix[12] = matrix[18] = 1.f;
44   matrix[4] = matrix[9] = matrix[14] = amount;
45 }
46 
GetContrastMatrix(float amount,float matrix[20])47 void GetContrastMatrix(float amount, float matrix[20]) {
48   memset(matrix, 0, 20 * sizeof(float));
49   matrix[0] = matrix[6] = matrix[12] = amount;
50   matrix[4] = matrix[9] = matrix[14] = (-0.5f * amount + 0.5f);
51   matrix[18] = 1.f;
52 }
53 
GetSaturateMatrix(float amount,float matrix[20])54 void GetSaturateMatrix(float amount, float matrix[20]) {
55   // Note, these values are computed to ensure MatrixNeedsClamping is false
56   // for amount in [0..1]
57   matrix[0] = 0.213f + 0.787f * amount;
58   matrix[1] = 0.715f - 0.715f * amount;
59   matrix[2] = 1.f - (matrix[0] + matrix[1]);
60   matrix[3] = matrix[4] = 0.f;
61   matrix[5] = 0.213f - 0.213f * amount;
62   matrix[6] = 0.715f + 0.285f * amount;
63   matrix[7] = 1.f - (matrix[5] + matrix[6]);
64   matrix[8] = matrix[9] = 0.f;
65   matrix[10] = 0.213f - 0.213f * amount;
66   matrix[11] = 0.715f - 0.715f * amount;
67   matrix[12] = 1.f - (matrix[10] + matrix[11]);
68   matrix[13] = matrix[14] = 0.f;
69   matrix[15] = matrix[16] = matrix[17] = matrix[19] = 0.f;
70   matrix[18] = 1.f;
71 }
72 
GetHueRotateMatrix(float hue,float matrix[20])73 void GetHueRotateMatrix(float hue, float matrix[20]) {
74   float cos_hue = cosf(hue * base::kPiFloat / 180.f);
75   float sin_hue = sinf(hue * base::kPiFloat / 180.f);
76   matrix[0] = 0.213f + cos_hue * 0.787f - sin_hue * 0.213f;
77   matrix[1] = 0.715f - cos_hue * 0.715f - sin_hue * 0.715f;
78   matrix[2] = 0.072f - cos_hue * 0.072f + sin_hue * 0.928f;
79   matrix[3] = matrix[4] = 0.f;
80   matrix[5] = 0.213f - cos_hue * 0.213f + sin_hue * 0.143f;
81   matrix[6] = 0.715f + cos_hue * 0.285f + sin_hue * 0.140f;
82   matrix[7] = 0.072f - cos_hue * 0.072f - sin_hue * 0.283f;
83   matrix[8] = matrix[9] = 0.f;
84   matrix[10] = 0.213f - cos_hue * 0.213f - sin_hue * 0.787f;
85   matrix[11] = 0.715f - cos_hue * 0.715f + sin_hue * 0.715f;
86   matrix[12] = 0.072f + cos_hue * 0.928f + sin_hue * 0.072f;
87   matrix[13] = matrix[14] = 0.f;
88   matrix[15] = matrix[16] = matrix[17] = 0.f;
89   matrix[18] = 1.f;
90   matrix[19] = 0.f;
91 }
92 
GetInvertMatrix(float amount,float matrix[20])93 void GetInvertMatrix(float amount, float matrix[20]) {
94   memset(matrix, 0, 20 * sizeof(float));
95   matrix[0] = matrix[6] = matrix[12] = 1.f - 2.f * amount;
96   matrix[4] = matrix[9] = matrix[14] = amount;
97   matrix[18] = 1.f;
98 }
99 
GetOpacityMatrix(float amount,float matrix[20])100 void GetOpacityMatrix(float amount, float matrix[20]) {
101   memset(matrix, 0, 20 * sizeof(float));
102   matrix[0] = matrix[6] = matrix[12] = 1.f;
103   matrix[18] = amount;
104 }
105 
GetGrayscaleMatrix(float amount,float matrix[20])106 void GetGrayscaleMatrix(float amount, float matrix[20]) {
107   // Note, these values are computed to ensure MatrixNeedsClamping is false
108   // for amount in [0..1]
109   matrix[0] = 0.2126f + 0.7874f * amount;
110   matrix[1] = 0.7152f - 0.7152f * amount;
111   matrix[2] = 1.f - (matrix[0] + matrix[1]);
112   matrix[3] = matrix[4] = 0.f;
113 
114   matrix[5] = 0.2126f - 0.2126f * amount;
115   matrix[6] = 0.7152f + 0.2848f * amount;
116   matrix[7] = 1.f - (matrix[5] + matrix[6]);
117   matrix[8] = matrix[9] = 0.f;
118 
119   matrix[10] = 0.2126f - 0.2126f * amount;
120   matrix[11] = 0.7152f - 0.7152f * amount;
121   matrix[12] = 1.f - (matrix[10] + matrix[11]);
122   matrix[13] = matrix[14] = 0.f;
123 
124   matrix[15] = matrix[16] = matrix[17] = matrix[19] = 0.f;
125   matrix[18] = 1.f;
126 }
127 
GetSepiaMatrix(float amount,float matrix[20])128 void GetSepiaMatrix(float amount, float matrix[20]) {
129   matrix[0] = 0.393f + 0.607f * amount;
130   matrix[1] = 0.769f - 0.769f * amount;
131   matrix[2] = 0.189f - 0.189f * amount;
132   matrix[3] = matrix[4] = 0.f;
133 
134   matrix[5] = 0.349f - 0.349f * amount;
135   matrix[6] = 0.686f + 0.314f * amount;
136   matrix[7] = 0.168f - 0.168f * amount;
137   matrix[8] = matrix[9] = 0.f;
138 
139   matrix[10] = 0.272f - 0.272f * amount;
140   matrix[11] = 0.534f - 0.534f * amount;
141   matrix[12] = 0.131f + 0.869f * amount;
142   matrix[13] = matrix[14] = 0.f;
143 
144   matrix[15] = matrix[16] = matrix[17] = matrix[19] = 0.f;
145   matrix[18] = 1.f;
146 }
147 
CreateMatrixImageFilter(const float matrix[20],sk_sp<PaintFilter> input)148 sk_sp<PaintFilter> CreateMatrixImageFilter(const float matrix[20],
149                                            sk_sp<PaintFilter> input) {
150   auto color_filter = SkColorFilters::Matrix(matrix);
151   if (!color_filter)
152     return nullptr;
153 
154   return sk_make_sp<ColorFilterPaintFilter>(std::move(color_filter),
155                                             std::move(input));
156 }
157 
158 }  // namespace
159 
BuildImageFilter(const FilterOperations & filters,const gfx::SizeF & size,const gfx::Vector2dF & offset)160 sk_sp<PaintFilter> RenderSurfaceFilters::BuildImageFilter(
161     const FilterOperations& filters,
162     const gfx::SizeF& size,
163     const gfx::Vector2dF& offset) {
164   sk_sp<PaintFilter> image_filter;
165   float matrix[20];
166   for (size_t i = 0; i < filters.size(); ++i) {
167     const FilterOperation& op = filters.at(i);
168     switch (op.type()) {
169       case FilterOperation::GRAYSCALE:
170         GetGrayscaleMatrix(1.f - op.amount(), matrix);
171         image_filter = CreateMatrixImageFilter(matrix, std::move(image_filter));
172         break;
173       case FilterOperation::SEPIA:
174         GetSepiaMatrix(1.f - op.amount(), matrix);
175         image_filter = CreateMatrixImageFilter(matrix, std::move(image_filter));
176         break;
177       case FilterOperation::SATURATE:
178         GetSaturateMatrix(op.amount(), matrix);
179         image_filter = CreateMatrixImageFilter(matrix, std::move(image_filter));
180         break;
181       case FilterOperation::HUE_ROTATE:
182         GetHueRotateMatrix(op.amount(), matrix);
183         image_filter = CreateMatrixImageFilter(matrix, std::move(image_filter));
184         break;
185       case FilterOperation::INVERT:
186         GetInvertMatrix(op.amount(), matrix);
187         image_filter = CreateMatrixImageFilter(matrix, std::move(image_filter));
188         break;
189       case FilterOperation::OPACITY:
190         GetOpacityMatrix(op.amount(), matrix);
191         image_filter = CreateMatrixImageFilter(matrix, std::move(image_filter));
192         break;
193       case FilterOperation::BRIGHTNESS:
194         GetBrightnessMatrix(op.amount(), matrix);
195         image_filter = CreateMatrixImageFilter(matrix, std::move(image_filter));
196         break;
197       case FilterOperation::CONTRAST:
198         GetContrastMatrix(op.amount(), matrix);
199         image_filter = CreateMatrixImageFilter(matrix, std::move(image_filter));
200         break;
201       case FilterOperation::BLUR:
202         image_filter = sk_make_sp<BlurPaintFilter>(op.amount(), op.amount(),
203                                                    op.blur_tile_mode(),
204                                                    std::move(image_filter));
205         break;
206       case FilterOperation::DROP_SHADOW:
207         image_filter = sk_make_sp<DropShadowPaintFilter>(
208             SkIntToScalar(op.drop_shadow_offset().x()),
209             SkIntToScalar(op.drop_shadow_offset().y()),
210             SkIntToScalar(op.amount()), SkIntToScalar(op.amount()),
211             op.drop_shadow_color(),
212             SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
213             std::move(image_filter));
214         break;
215       case FilterOperation::COLOR_MATRIX:
216         image_filter =
217             CreateMatrixImageFilter(op.matrix(), std::move(image_filter));
218         break;
219       case FilterOperation::ZOOM: {
220         // The center point, always the midpoint of the unclipped rectangle.
221         // When we go to either edge of the screen, the width/height will shrink
222         // at the same rate the offset changes. Use abs on the offset since we
223         // do not care about the offset direction.
224         gfx::Vector2dF center =
225             gfx::Vector2dF((size.width() + std::abs(offset.x())) / 2,
226                            (size.height() + std::abs(offset.y())) / 2);
227 
228         // The dimensions of the source content. This shrinks as the texture
229         // rectangle gets clipped.
230         gfx::Vector2d src_dimensions =
231             gfx::Vector2d((size.width() + std::abs(offset.x())) / op.amount(),
232                           (size.height() + std::abs(offset.y())) / op.amount());
233 
234         // When the magnifier goes to the left/top border of the screen, we need
235         // to adjust the x/y position of the rect. The rate the position gets
236         // updated currently only works properly for a 2x magnification.
237         DCHECK_EQ(op.amount(), 2.f);
238         gfx::Vector2dF center_offset = gfx::Vector2dF(0, 0);
239         if (offset.x() >= 0)
240           center_offset.set_x(-offset.x() / op.amount());
241         if (offset.y() <= 0)
242           center_offset.set_y(offset.y() / op.amount());
243 
244         sk_sp<PaintFilter> zoom_filter = sk_make_sp<MagnifierPaintFilter>(
245             SkRect::MakeXYWH(
246                 (center.x() - src_dimensions.x() / 2.f) + center_offset.x(),
247                 (center.y() - src_dimensions.y() / 2.f) + center_offset.y(),
248                 size.width() / op.amount(), size.height() / op.amount()),
249             op.zoom_inset(), nullptr);
250         if (image_filter) {
251           // TODO(ajuma): When there's a 1-input version of
252           // SkMagnifierImageFilter, use that to handle the input filter
253           // instead of using an SkComposeImageFilter.
254           image_filter = sk_make_sp<ComposePaintFilter>(
255               std::move(zoom_filter), std::move(image_filter));
256         } else {
257           image_filter = std::move(zoom_filter);
258         }
259         break;
260       }
261       case FilterOperation::SATURATING_BRIGHTNESS:
262         GetSaturatingBrightnessMatrix(op.amount(), matrix);
263         image_filter = CreateMatrixImageFilter(matrix, std::move(image_filter));
264         break;
265       case FilterOperation::REFERENCE: {
266         if (!op.image_filter())
267           break;
268 
269         sk_sp<SkColorFilter> cf;
270         bool has_input = false;
271         if (op.image_filter()->type() == PaintFilter::Type::kColorFilter &&
272             !op.image_filter()->crop_rect()) {
273           auto* color_paint_filter =
274               static_cast<ColorFilterPaintFilter*>(op.image_filter().get());
275           cf = color_paint_filter->color_filter();
276           has_input = !!color_paint_filter->input();
277         }
278 
279         if (cf && cf->asAColorMatrix(matrix) && !has_input) {
280           image_filter =
281               CreateMatrixImageFilter(matrix, std::move(image_filter));
282         } else if (image_filter) {
283           image_filter = sk_make_sp<ComposePaintFilter>(
284               op.image_filter(), std::move(image_filter));
285         } else {
286           image_filter = op.image_filter();
287         }
288         break;
289       }
290       case FilterOperation::ALPHA_THRESHOLD: {
291         SkRegion region;
292         for (const gfx::Rect& rect : op.shape())
293           region.op(gfx::RectToSkIRect(rect), SkRegion::kUnion_Op);
294         sk_sp<PaintFilter> alpha_filter = sk_make_sp<AlphaThresholdPaintFilter>(
295             region, op.amount(), op.outer_threshold(), nullptr);
296         if (image_filter) {
297           image_filter = sk_make_sp<ComposePaintFilter>(
298               std::move(alpha_filter), std::move(image_filter));
299         } else {
300           image_filter = std::move(alpha_filter);
301         }
302         break;
303       }
304     }
305   }
306   return image_filter;
307 }
308 
309 }  // namespace cc
310