SkRecordOpts.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright 2014 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. #include "src/core/SkRecordOpts.h"
  8. #include "include/private/SkTDArray.h"
  9. #include "src/core/SkCanvasPriv.h"
  10. #include "src/core/SkRecordPattern.h"
  11. #include "src/core/SkRecords.h"
  12. using namespace SkRecords;
  13. // Most of the optimizations in this file are pattern-based. These are all defined as structs with:
  14. // - a Match typedef
  15. // - a bool onMatch(SkRceord*, Match*, int begin, int end) method,
  16. // which returns true if it made changes and false if not.
  17. // Run a pattern-based optimization once across the SkRecord, returning true if it made any changes.
  18. // It looks for spans which match Pass::Match, and when found calls onMatch() with that pattern,
  19. // record, and [begin,end) span of the commands that matched.
  20. template <typename Pass>
  21. static bool apply(Pass* pass, SkRecord* record) {
  22. typename Pass::Match match;
  23. bool changed = false;
  24. int begin, end = 0;
  25. while (match.search(record, &begin, &end)) {
  26. changed |= pass->onMatch(record, &match, begin, end);
  27. }
  28. return changed;
  29. }
  30. ///////////////////////////////////////////////////////////////////////////////////////////////////
  31. static void multiple_set_matrices(SkRecord* record) {
  32. struct {
  33. typedef Pattern<Is<SetMatrix>,
  34. Greedy<Is<NoOp>>,
  35. Is<SetMatrix> >
  36. Match;
  37. bool onMatch(SkRecord* record, Match* pattern, int begin, int end) {
  38. record->replace<NoOp>(begin); // first SetMatrix
  39. return true;
  40. }
  41. } pass;
  42. while (apply(&pass, record));
  43. }
  44. ///////////////////////////////////////////////////////////////////////////////////////////////////
  45. #if 0 // experimental, but needs knowledge of previous matrix to operate correctly
  46. static void apply_matrix_to_draw_params(SkRecord* record) {
  47. struct {
  48. typedef Pattern<Is<SetMatrix>,
  49. Greedy<Is<NoOp>>,
  50. Is<SetMatrix> >
  51. Pattern;
  52. bool onMatch(SkRecord* record, Pattern* pattern, int begin, int end) {
  53. record->replace<NoOp>(begin); // first SetMatrix
  54. return true;
  55. }
  56. } pass;
  57. // No need to loop, as we never "open up" opportunities for more of this type of optimization.
  58. apply(&pass, record);
  59. }
  60. #endif
  61. ///////////////////////////////////////////////////////////////////////////////////////////////////
  62. // Turns the logical NoOp Save and Restore in Save-Draw*-Restore patterns into actual NoOps.
  63. struct SaveOnlyDrawsRestoreNooper {
  64. typedef Pattern<Is<Save>,
  65. Greedy<Or<Is<NoOp>, IsDraw>>,
  66. Is<Restore>>
  67. Match;
  68. bool onMatch(SkRecord* record, Match*, int begin, int end) {
  69. record->replace<NoOp>(begin); // Save
  70. record->replace<NoOp>(end-1); // Restore
  71. return true;
  72. }
  73. };
  74. static bool fold_opacity_layer_color_to_paint(const SkPaint* layerPaint,
  75. bool isSaveLayer,
  76. SkPaint* paint) {
  77. // We assume layerPaint is always from a saveLayer. If isSaveLayer is
  78. // true, we assume paint is too.
  79. // The alpha folding can proceed if the filter layer paint does not have properties which cause
  80. // the resulting filter layer to be "blended" in complex ways to the parent layer. For example,
  81. // looper drawing unmodulated filter layer twice and then modulating the result produces
  82. // different image to drawing modulated filter layer twice.
  83. // TODO: most likely the looper and only some xfer modes are the hard constraints
  84. if (!paint->isSrcOver()
  85. #ifdef SK_SUPPORT_LEGACY_DRAWLOOPER
  86. || paint->getLooper()
  87. #endif
  88. ) {
  89. return false;
  90. }
  91. if (!isSaveLayer && paint->getImageFilter()) {
  92. // For normal draws, the paint color is used as one input for the color for the draw. Image
  93. // filter will operate on the result, and thus we can not change the input.
  94. // For layer saves, the image filter is applied to the layer contents. The layer is then
  95. // modulated with the paint color, so it's fine to proceed with the fold for saveLayer
  96. // paints with image filters.
  97. return false;
  98. }
  99. if (paint->getColorFilter()) {
  100. // Filter input depends on the paint color.
  101. // Here we could filter the color if we knew the draw is going to be uniform color. This
  102. // should be detectable as drawPath/drawRect/.. without a shader being uniform, while
  103. // drawBitmap/drawSprite or a shader being non-uniform. However, current matchers don't
  104. // give the type out easily, so just do not optimize that at the moment.
  105. return false;
  106. }
  107. if (layerPaint) {
  108. const uint32_t layerColor = layerPaint->getColor();
  109. // The layer paint color must have only alpha component.
  110. if (SK_ColorTRANSPARENT != SkColorSetA(layerColor, SK_AlphaTRANSPARENT)) {
  111. return false;
  112. }
  113. // The layer paint can not have any effects.
  114. if (layerPaint->getPathEffect() ||
  115. layerPaint->getShader() ||
  116. !layerPaint->isSrcOver() ||
  117. layerPaint->getMaskFilter() ||
  118. layerPaint->getColorFilter() ||
  119. #ifdef SK_SUPPORT_LEGACY_DRAWLOOPER
  120. layerPaint->getLooper() ||
  121. #endif
  122. layerPaint->getImageFilter()) {
  123. return false;
  124. }
  125. paint->setAlpha(SkMulDiv255Round(paint->getAlpha(), SkColorGetA(layerColor)));
  126. }
  127. return true;
  128. }
  129. // Turns logical no-op Save-[non-drawing command]*-Restore patterns into actual no-ops.
  130. struct SaveNoDrawsRestoreNooper {
  131. // Greedy matches greedily, so we also have to exclude Save and Restore.
  132. // Nested SaveLayers need to be excluded, or we'll match their Restore!
  133. typedef Pattern<Is<Save>,
  134. Greedy<Not<Or<Is<Save>,
  135. Is<SaveLayer>,
  136. Is<Restore>,
  137. IsDraw>>>,
  138. Is<Restore>>
  139. Match;
  140. bool onMatch(SkRecord* record, Match*, int begin, int end) {
  141. // The entire span between Save and Restore (inclusively) does nothing.
  142. for (int i = begin; i < end; i++) {
  143. record->replace<NoOp>(i);
  144. }
  145. return true;
  146. }
  147. };
  148. void SkRecordNoopSaveRestores(SkRecord* record) {
  149. SaveOnlyDrawsRestoreNooper onlyDraws;
  150. SaveNoDrawsRestoreNooper noDraws;
  151. // Run until they stop changing things.
  152. while (apply(&onlyDraws, record) || apply(&noDraws, record));
  153. }
  154. #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
  155. static bool effectively_srcover(const SkPaint* paint) {
  156. if (!paint || paint->isSrcOver()) {
  157. return true;
  158. }
  159. // src-mode with opaque and no effects (which might change opaqueness) is ok too.
  160. return !paint->getShader() && !paint->getColorFilter() && !paint->getImageFilter() &&
  161. 0xFF == paint->getAlpha() && paint->getBlendMode() == SkBlendMode::kSrc;
  162. }
  163. // For some SaveLayer-[drawing command]-Restore patterns, merge the SaveLayer's alpha into the
  164. // draw, and no-op the SaveLayer and Restore.
  165. struct SaveLayerDrawRestoreNooper {
  166. typedef Pattern<Is<SaveLayer>, IsDraw, Is<Restore>> Match;
  167. bool onMatch(SkRecord* record, Match* match, int begin, int end) {
  168. if (match->first<SaveLayer>()->backdrop || match->first<SaveLayer>()->clipMask) {
  169. // can't throw away the layer if we have a backdrop or clip mask
  170. return false;
  171. }
  172. if (match->first<SaveLayer>()->saveLayerFlags &
  173. SkCanvasPriv::kDontClipToLayer_SaveLayerFlag) {
  174. // can't throw away the layer if set
  175. return false;
  176. }
  177. // A SaveLayer's bounds field is just a hint, so we should be free to ignore it.
  178. SkPaint* layerPaint = match->first<SaveLayer>()->paint;
  179. SkPaint* drawPaint = match->second<SkPaint>();
  180. if (nullptr == layerPaint && effectively_srcover(drawPaint)) {
  181. // There wasn't really any point to this SaveLayer at all.
  182. return KillSaveLayerAndRestore(record, begin);
  183. }
  184. if (drawPaint == nullptr) {
  185. // We can just give the draw the SaveLayer's paint.
  186. // TODO(mtklein): figure out how to do this clearly
  187. return false;
  188. }
  189. if (!fold_opacity_layer_color_to_paint(layerPaint, false /*isSaveLayer*/, drawPaint)) {
  190. return false;
  191. }
  192. return KillSaveLayerAndRestore(record, begin);
  193. }
  194. static bool KillSaveLayerAndRestore(SkRecord* record, int saveLayerIndex) {
  195. record->replace<NoOp>(saveLayerIndex); // SaveLayer
  196. record->replace<NoOp>(saveLayerIndex+2); // Restore
  197. return true;
  198. }
  199. };
  200. void SkRecordNoopSaveLayerDrawRestores(SkRecord* record) {
  201. SaveLayerDrawRestoreNooper pass;
  202. apply(&pass, record);
  203. }
  204. #endif
  205. /* For SVG generated:
  206. SaveLayer (non-opaque, typically for CSS opacity)
  207. Save
  208. ClipRect
  209. SaveLayer (typically for SVG filter)
  210. Restore
  211. Restore
  212. Restore
  213. */
  214. struct SvgOpacityAndFilterLayerMergePass {
  215. typedef Pattern<Is<SaveLayer>, Is<Save>, Is<ClipRect>, Is<SaveLayer>,
  216. Is<Restore>, Is<Restore>, Is<Restore>> Match;
  217. bool onMatch(SkRecord* record, Match* match, int begin, int end) {
  218. if (match->first<SaveLayer>()->backdrop) {
  219. // can't throw away the layer if we have a backdrop
  220. return false;
  221. }
  222. SkPaint* opacityPaint = match->first<SaveLayer>()->paint;
  223. if (nullptr == opacityPaint) {
  224. // There wasn't really any point to this SaveLayer at all.
  225. return KillSaveLayerAndRestore(record, begin);
  226. }
  227. // This layer typically contains a filter, but this should work for layers with for other
  228. // purposes too.
  229. SkPaint* filterLayerPaint = match->fourth<SaveLayer>()->paint;
  230. if (filterLayerPaint == nullptr) {
  231. // We can just give the inner SaveLayer the paint of the outer SaveLayer.
  232. // TODO(mtklein): figure out how to do this clearly
  233. return false;
  234. }
  235. if (!fold_opacity_layer_color_to_paint(opacityPaint, true /*isSaveLayer*/,
  236. filterLayerPaint)) {
  237. return false;
  238. }
  239. return KillSaveLayerAndRestore(record, begin);
  240. }
  241. static bool KillSaveLayerAndRestore(SkRecord* record, int saveLayerIndex) {
  242. record->replace<NoOp>(saveLayerIndex); // SaveLayer
  243. record->replace<NoOp>(saveLayerIndex + 6); // Restore
  244. return true;
  245. }
  246. };
  247. void SkRecordMergeSvgOpacityAndFilterLayers(SkRecord* record) {
  248. SvgOpacityAndFilterLayerMergePass pass;
  249. apply(&pass, record);
  250. }
  251. ///////////////////////////////////////////////////////////////////////////////////////////////////
  252. void SkRecordOptimize(SkRecord* record) {
  253. // This might be useful as a first pass in the future if we want to weed
  254. // out junk for other optimization passes. Right now, nothing needs it,
  255. // and the bounding box hierarchy will do the work of skipping no-op
  256. // Save-NoDraw-Restore sequences better than we can here.
  257. // As there is a known problem with this peephole and drawAnnotation, disable this.
  258. // If we want to enable this we must first fix this bug:
  259. // https://bugs.chromium.org/p/skia/issues/detail?id=5548
  260. // SkRecordNoopSaveRestores(record);
  261. // Turn off this optimization completely for Android framework
  262. // because it makes the following Android CTS test fail:
  263. // android.uirendering.cts.testclasses.LayerTests#testSaveLayerClippedWithAlpha
  264. #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
  265. SkRecordNoopSaveLayerDrawRestores(record);
  266. #endif
  267. SkRecordMergeSvgOpacityAndFilterLayers(record);
  268. record->defrag();
  269. }
  270. void SkRecordOptimize2(SkRecord* record) {
  271. multiple_set_matrices(record);
  272. SkRecordNoopSaveRestores(record);
  273. // See why we turn this off in SkRecordOptimize above.
  274. #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
  275. SkRecordNoopSaveLayerDrawRestores(record);
  276. #endif
  277. SkRecordMergeSvgOpacityAndFilterLayers(record);
  278. record->defrag();
  279. }