shadowutils.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright 2017 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 "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkMatrix.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkPath.h"
  13. #include "include/core/SkPoint.h"
  14. #include "include/core/SkPoint3.h"
  15. #include "include/core/SkRRect.h"
  16. #include "include/core/SkRect.h"
  17. #include "include/core/SkScalar.h"
  18. #include "include/core/SkTypes.h"
  19. #include "include/private/SkShadowFlags.h"
  20. #include "include/private/SkTArray.h"
  21. #include "include/private/SkTDArray.h"
  22. #include "include/utils/SkShadowUtils.h"
  23. #include <initializer_list>
  24. void draw_shadow(SkCanvas* canvas, const SkPath& path, SkScalar height, SkColor color,
  25. SkPoint3 lightPos, SkScalar lightR, bool isAmbient, uint32_t flags) {
  26. SkScalar ambientAlpha = isAmbient ? .5f : 0.f;
  27. SkScalar spotAlpha = isAmbient ? 0.f : .5f;
  28. SkColor ambientColor = SkColorSetARGB(ambientAlpha*SkColorGetA(color), SkColorGetR(color),
  29. SkColorGetG(color), SkColorGetB(color));
  30. SkColor spotColor = SkColorSetARGB(spotAlpha*SkColorGetA(color), SkColorGetR(color),
  31. SkColorGetG(color), SkColorGetB(color));
  32. SkShadowUtils::DrawShadow(canvas, path, SkPoint3{ 0, 0, height}, lightPos, lightR,
  33. ambientColor, spotColor, flags);
  34. }
  35. static constexpr int kW = 800;
  36. static constexpr int kH = 960;
  37. enum ShadowMode {
  38. kDebugColorNoOccluders,
  39. kDebugColorOccluders,
  40. kGrayscale
  41. };
  42. void draw_paths(SkCanvas* canvas, ShadowMode mode) {
  43. SkTArray<SkPath> paths;
  44. paths.push_back().addRoundRect(SkRect::MakeWH(50, 50), 10, 10);
  45. SkRRect oddRRect;
  46. oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16);
  47. paths.push_back().addRRect(oddRRect);
  48. paths.push_back().addRect(SkRect::MakeWH(50, 50));
  49. paths.push_back().addCircle(25, 25, 25);
  50. paths.push_back().cubicTo(100, 50, 20, 100, 0, 0);
  51. paths.push_back().addOval(SkRect::MakeWH(20, 60));
  52. // star
  53. SkTArray<SkPath> concavePaths;
  54. concavePaths.push_back().moveTo(0.0f, -33.3333f);
  55. concavePaths.back().lineTo(9.62f, -16.6667f);
  56. concavePaths.back().lineTo(28.867f, -16.6667f);
  57. concavePaths.back().lineTo(19.24f, 0.0f);
  58. concavePaths.back().lineTo(28.867f, 16.6667f);
  59. concavePaths.back().lineTo(9.62f, 16.6667f);
  60. concavePaths.back().lineTo(0.0f, 33.3333f);
  61. concavePaths.back().lineTo(-9.62f, 16.6667f);
  62. concavePaths.back().lineTo(-28.867f, 16.6667f);
  63. concavePaths.back().lineTo(-19.24f, 0.0f);
  64. concavePaths.back().lineTo(-28.867f, -16.6667f);
  65. concavePaths.back().lineTo(-9.62f, -16.6667f);
  66. concavePaths.back().close();
  67. // dumbbell
  68. concavePaths.push_back().moveTo(50, 0);
  69. concavePaths.back().cubicTo(100, 25, 60, 50, 50, 0);
  70. concavePaths.back().cubicTo(0, -25, 40, -50, 50, 0);
  71. static constexpr SkScalar kPad = 15.f;
  72. static constexpr SkScalar kLightR = 100.f;
  73. static constexpr SkScalar kHeight = 50.f;
  74. // transform light position relative to canvas to handle tiling
  75. SkPoint lightXY = canvas->getTotalMatrix().mapXY(250, 400);
  76. SkPoint3 lightPos = { lightXY.fX, lightXY.fY, 500 };
  77. canvas->translate(3 * kPad, 3 * kPad);
  78. canvas->save();
  79. SkScalar x = 0;
  80. SkScalar dy = 0;
  81. SkTDArray<SkMatrix> matrices;
  82. matrices.push()->reset();
  83. SkMatrix* m = matrices.push();
  84. m->setRotate(33.f, 25.f, 25.f);
  85. m->postScale(1.2f, 0.8f, 25.f, 25.f);
  86. for (auto& m : matrices) {
  87. for (int flags : { kNone_ShadowFlag, kTransparentOccluder_ShadowFlag }) {
  88. int pathCounter = 0;
  89. for (const auto& path : paths) {
  90. SkRect postMBounds = path.getBounds();
  91. m.mapRect(&postMBounds);
  92. SkScalar w = postMBounds.width() + kHeight;
  93. SkScalar dx = w + kPad;
  94. if (x + dx > kW - 3 * kPad) {
  95. canvas->restore();
  96. canvas->translate(0, dy);
  97. canvas->save();
  98. x = 0;
  99. dy = 0;
  100. }
  101. canvas->save();
  102. canvas->concat(m);
  103. // flip a couple of paths to test 180° rotation
  104. if (kTransparentOccluder_ShadowFlag == flags && 0 == pathCounter % 3) {
  105. canvas->save();
  106. canvas->rotate(180, 25, 25);
  107. }
  108. if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) {
  109. draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR,
  110. true, flags);
  111. draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR,
  112. false, flags);
  113. } else if (kGrayscale == mode) {
  114. SkColor ambientColor = SkColorSetARGB(0.1f * 255, 0, 0, 0);
  115. SkColor spotColor = SkColorSetARGB(0.25f * 255, 0, 0, 0);
  116. SkShadowUtils::DrawShadow(canvas, path, SkPoint3{0, 0, kHeight}, lightPos,
  117. kLightR, ambientColor, spotColor, flags);
  118. }
  119. SkPaint paint;
  120. paint.setAntiAlias(true);
  121. if (kDebugColorNoOccluders == mode) {
  122. // Draw the path outline in green on top of the ambient and spot shadows.
  123. if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
  124. paint.setColor(SK_ColorCYAN);
  125. } else {
  126. paint.setColor(SK_ColorGREEN);
  127. }
  128. paint.setStyle(SkPaint::kStroke_Style);
  129. paint.setStrokeWidth(0);
  130. } else {
  131. paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE);
  132. if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
  133. paint.setAlphaf(0.5f);
  134. }
  135. paint.setStyle(SkPaint::kFill_Style);
  136. }
  137. canvas->drawPath(path, paint);
  138. if (kTransparentOccluder_ShadowFlag == flags && 0 == pathCounter % 3) {
  139. canvas->restore();
  140. }
  141. canvas->restore();
  142. canvas->translate(dx, 0);
  143. x += dx;
  144. dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
  145. ++pathCounter;
  146. }
  147. }
  148. }
  149. // concave paths
  150. canvas->restore();
  151. canvas->translate(kPad, dy);
  152. canvas->save();
  153. x = kPad;
  154. dy = 0;
  155. for (auto& m : matrices) {
  156. // for the concave paths we are not clipping, so transparent and opaque are the same
  157. for (const auto& path : concavePaths) {
  158. SkRect postMBounds = path.getBounds();
  159. m.mapRect(&postMBounds);
  160. SkScalar w = postMBounds.width() + kHeight;
  161. SkScalar dx = w + kPad;
  162. canvas->save();
  163. canvas->concat(m);
  164. if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) {
  165. draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR,
  166. true, kNone_ShadowFlag);
  167. draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR,
  168. false, kNone_ShadowFlag);
  169. } else if (kGrayscale == mode) {
  170. SkColor ambientColor = SkColorSetARGB(0.1f * 255, 0, 0, 0);
  171. SkColor spotColor = SkColorSetARGB(0.25f * 255, 0, 0, 0);
  172. SkShadowUtils::DrawShadow(canvas, path, SkPoint3{ 0, 0, kHeight }, lightPos,
  173. kLightR, ambientColor, spotColor, kNone_ShadowFlag);
  174. }
  175. SkPaint paint;
  176. paint.setAntiAlias(true);
  177. if (kDebugColorNoOccluders == mode) {
  178. // Draw the path outline in green on top of the ambient and spot shadows.
  179. paint.setColor(SK_ColorGREEN);
  180. paint.setStyle(SkPaint::kStroke_Style);
  181. paint.setStrokeWidth(0);
  182. } else {
  183. paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE);
  184. paint.setStyle(SkPaint::kFill_Style);
  185. }
  186. canvas->drawPath(path, paint);
  187. canvas->restore();
  188. canvas->translate(dx, 0);
  189. x += dx;
  190. dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
  191. }
  192. }
  193. // Show where the light is in x,y as a circle (specified in device space).
  194. SkMatrix invCanvasM = canvas->getTotalMatrix();
  195. if (invCanvasM.invert(&invCanvasM)) {
  196. canvas->save();
  197. canvas->concat(invCanvasM);
  198. SkPaint paint;
  199. paint.setColor(SK_ColorBLACK);
  200. paint.setAntiAlias(true);
  201. canvas->drawCircle(lightPos.fX, lightPos.fY, kLightR / 10.f, paint);
  202. canvas->restore();
  203. }
  204. }
  205. DEF_SIMPLE_GM(shadow_utils, canvas, kW, kH) {
  206. draw_paths(canvas, kDebugColorNoOccluders);
  207. }
  208. DEF_SIMPLE_GM(shadow_utils_occl, canvas, kW, kH) {
  209. draw_paths(canvas, kDebugColorOccluders);
  210. }
  211. DEF_SIMPLE_GM(shadow_utils_gray, canvas, kW, kH) {
  212. draw_paths(canvas, kGrayscale);
  213. }