render_surface_filters.cc 12 KB

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