patch.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "gm/gm.h"
  8. #include "include/core/SkBlendMode.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkImage.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkPath.h"
  14. #include "include/core/SkPoint.h"
  15. #include "include/core/SkRefCnt.h"
  16. #include "include/core/SkScalar.h"
  17. #include "include/core/SkShader.h"
  18. #include "include/core/SkTileMode.h"
  19. #include "include/core/SkTypes.h"
  20. #include "include/effects/SkGradientShader.h"
  21. #include "src/utils/SkPatchUtils.h"
  22. #include "tools/Resources.h"
  23. static sk_sp<SkShader> make_shader() {
  24. const SkColor colors[] = {
  25. SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, SK_ColorMAGENTA, SK_ColorBLUE,
  26. SK_ColorYELLOW,
  27. };
  28. const SkPoint pts[] = { { 100.f / 4.f, 0.f }, { 3.f * 100.f / 4.f, 100.f } };
  29. return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
  30. SkTileMode::kMirror);
  31. }
  32. static void draw_control_points(SkCanvas* canvas, const SkPoint cubics[12]) {
  33. //draw control points
  34. SkPaint paint;
  35. SkPoint bottom[SkPatchUtils::kNumPtsCubic];
  36. SkPatchUtils::GetBottomCubic(cubics, bottom);
  37. SkPoint top[SkPatchUtils::kNumPtsCubic];
  38. SkPatchUtils::GetTopCubic(cubics, top);
  39. SkPoint left[SkPatchUtils::kNumPtsCubic];
  40. SkPatchUtils::GetLeftCubic(cubics, left);
  41. SkPoint right[SkPatchUtils::kNumPtsCubic];
  42. SkPatchUtils::GetRightCubic(cubics, right);
  43. paint.setColor(SK_ColorBLACK);
  44. paint.setStrokeWidth(0.5f);
  45. SkPoint corners[4] = { bottom[0], bottom[3], top[0], top[3] };
  46. canvas->drawPoints(SkCanvas::kLines_PointMode, 4, bottom, paint);
  47. canvas->drawPoints(SkCanvas::kLines_PointMode, 2, bottom + 1, paint);
  48. canvas->drawPoints(SkCanvas::kLines_PointMode, 4, top, paint);
  49. canvas->drawPoints(SkCanvas::kLines_PointMode, 4, left, paint);
  50. canvas->drawPoints(SkCanvas::kLines_PointMode, 4, right, paint);
  51. canvas->drawPoints(SkCanvas::kLines_PointMode, 2, top + 1, paint);
  52. canvas->drawPoints(SkCanvas::kLines_PointMode, 2, left + 1, paint);
  53. canvas->drawPoints(SkCanvas::kLines_PointMode, 2, right + 1, paint);
  54. paint.setStrokeWidth(2);
  55. paint.setColor(SK_ColorRED);
  56. canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, corners, paint);
  57. paint.setColor(SK_ColorBLUE);
  58. canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, bottom + 1, paint);
  59. paint.setColor(SK_ColorCYAN);
  60. canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, top + 1, paint);
  61. paint.setColor(SK_ColorYELLOW);
  62. canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, left + 1, paint);
  63. paint.setColor(SK_ColorGREEN);
  64. canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, right + 1, paint);
  65. }
  66. // The order of the colors and points is clockwise starting at upper-left corner.
  67. const SkPoint gCubics[SkPatchUtils::kNumCtrlPts] = {
  68. //top points
  69. {100,100},{150,50},{250,150}, {300,100},
  70. //right points
  71. {250, 150},{350,250},
  72. //bottom points
  73. {300,300},{250,250},{150,350},{100,300},
  74. //left points
  75. {50,250},{150,150}
  76. };
  77. const SkPoint gTexCoords[SkPatchUtils::kNumCorners] = {
  78. {0.0f, 0.0f}, {100.0f, 0.0f}, {100.0f,100.0f}, {0.0f, 100.0f}
  79. };
  80. static void dopatch(SkCanvas* canvas, const SkColor colors[], sk_sp<SkImage> img = nullptr) {
  81. SkPaint paint;
  82. const SkBlendMode modes[] = {
  83. SkBlendMode::kSrc,
  84. SkBlendMode::kDst,
  85. SkBlendMode::kModulate,
  86. };
  87. SkPoint texStorage[4];
  88. const SkPoint* tex = gTexCoords;
  89. sk_sp<SkShader> shader;
  90. if (img) {
  91. SkScalar w = img->width();
  92. SkScalar h = img->height();
  93. shader = img->makeShader();
  94. texStorage[0].set(0, 0);
  95. texStorage[1].set(w, 0);
  96. texStorage[2].set(w, h);
  97. texStorage[3].set(0, h);
  98. tex = texStorage;
  99. } else {
  100. shader = make_shader();
  101. }
  102. canvas->save();
  103. for (int y = 0; y < 3; y++) {
  104. for (int x = 0; x < 4; x++) {
  105. canvas->save();
  106. canvas->translate(x * 350.0f, y * 350.0f);
  107. switch (x) {
  108. case 0:
  109. canvas->drawPatch(gCubics, nullptr, nullptr, modes[y], paint);
  110. break;
  111. case 1:
  112. canvas->drawPatch(gCubics, colors, nullptr, modes[y], paint);
  113. break;
  114. case 2:
  115. paint.setShader(shader);
  116. canvas->drawPatch(gCubics, nullptr, tex, modes[y], paint);
  117. paint.setShader(nullptr);
  118. break;
  119. case 3:
  120. paint.setShader(shader);
  121. canvas->drawPatch(gCubics, colors, tex, modes[y], paint);
  122. paint.setShader(nullptr);
  123. break;
  124. default:
  125. break;
  126. }
  127. draw_control_points(canvas, gCubics);
  128. canvas->restore();
  129. }
  130. }
  131. canvas->restore();
  132. }
  133. DEF_SIMPLE_GM(patch_primitive, canvas, 1500, 1100) {
  134. const SkColor colors[SkPatchUtils::kNumCorners] = {
  135. SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
  136. };
  137. dopatch(canvas, colors);
  138. }
  139. DEF_SIMPLE_GM(patch_image, canvas, 1500, 1100) {
  140. const SkColor colors[SkPatchUtils::kNumCorners] = {
  141. SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN
  142. };
  143. dopatch(canvas, colors, GetResourceAsImage("images/mandrill_128.png"));
  144. }
  145. DEF_SIMPLE_GM(patch_alpha, canvas, 1500, 1100) {
  146. const SkColor colors[SkPatchUtils::kNumCorners] = {
  147. SK_ColorRED, 0x0000FF00, SK_ColorBLUE, 0x00FF00FF,
  148. };
  149. dopatch(canvas, colors);
  150. }
  151. // These two should look the same (one patch, one simple path)
  152. DEF_SIMPLE_GM(patch_alpha_test, canvas, 550, 250) {
  153. canvas->translate(-75, -75);
  154. const SkColor colors[SkPatchUtils::kNumCorners] = {
  155. 0x80FF0000, 0x80FF0000, 0x80FF0000, 0x80FF0000,
  156. };
  157. SkPaint paint;
  158. canvas->drawPatch(gCubics, colors, nullptr, SkBlendMode::kModulate, paint);
  159. canvas->translate(300, 0);
  160. SkPath path;
  161. path.moveTo(gCubics[0]);
  162. path.cubicTo(gCubics[ 1], gCubics[ 2], gCubics[ 3]);
  163. path.cubicTo(gCubics[ 4], gCubics[ 5], gCubics[ 6]);
  164. path.cubicTo(gCubics[ 7], gCubics[ 8], gCubics[ 9]);
  165. path.cubicTo(gCubics[10], gCubics[11], gCubics[ 0]);
  166. paint.setColor(colors[0]);
  167. canvas->drawPath(path, paint);
  168. }