srcmode.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2012 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/SkFont.h"
  12. #include "include/core/SkImageInfo.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkPath.h"
  15. #include "include/core/SkPoint.h"
  16. #include "include/core/SkRect.h"
  17. #include "include/core/SkRefCnt.h"
  18. #include "include/core/SkScalar.h"
  19. #include "include/core/SkShader.h"
  20. #include "include/core/SkSize.h"
  21. #include "include/core/SkString.h"
  22. #include "include/core/SkSurface.h"
  23. #include "include/core/SkTileMode.h"
  24. #include "include/core/SkTypeface.h"
  25. #include "include/core/SkTypes.h"
  26. #include "include/effects/SkGradientShader.h"
  27. #include "tools/ToolUtils.h"
  28. #define W SkIntToScalar(80)
  29. #define H SkIntToScalar(60)
  30. typedef void (*PaintProc)(SkPaint*);
  31. static void identity_paintproc(SkPaint* paint) {
  32. paint->setShader(nullptr);
  33. }
  34. static void gradient_paintproc(SkPaint* paint) {
  35. const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
  36. const SkPoint pts[] = { { 0, 0 }, { W, H } };
  37. paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
  38. SkTileMode::kClamp));
  39. }
  40. typedef void (*Proc)(SkCanvas*, const SkPaint&, const SkFont&);
  41. static void draw_hair(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
  42. SkPaint p(paint);
  43. p.setStrokeWidth(0);
  44. canvas->drawLine(0, 0, W, H, p);
  45. }
  46. static void draw_thick(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
  47. SkPaint p(paint);
  48. p.setStrokeWidth(H/5);
  49. canvas->drawLine(0, 0, W, H, p);
  50. }
  51. static void draw_rect(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
  52. canvas->drawRect(SkRect::MakeWH(W, H), paint);
  53. }
  54. static void draw_oval(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
  55. canvas->drawOval(SkRect::MakeWH(W, H), paint);
  56. }
  57. static void draw_text(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
  58. canvas->drawString("Hamburge", 0, H*2/3, font, paint);
  59. }
  60. class SrcModeGM : public skiagm::GM {
  61. SkPath fPath;
  62. void onOnceBeforeDraw() override { this->setBGColor(SK_ColorBLACK); }
  63. SkString onShortName() override { return SkString("srcmode"); }
  64. SkISize onISize() override { return {640, 760}; }
  65. void drawContent(SkCanvas* canvas) {
  66. canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
  67. SkPaint paint;
  68. SkFont font(ToolUtils::create_portable_typeface(), H / 4);
  69. paint.setColor(0x80F60000);
  70. const Proc procs[] = {
  71. draw_hair, draw_thick, draw_rect, draw_oval, draw_text
  72. };
  73. const SkBlendMode modes[] = {
  74. SkBlendMode::kSrcOver, SkBlendMode::kSrc, SkBlendMode::kClear
  75. };
  76. const PaintProc paintProcs[] = {
  77. identity_paintproc, gradient_paintproc
  78. };
  79. for (int aa = 0; aa <= 1; ++aa) {
  80. paint.setAntiAlias(SkToBool(aa));
  81. font.setEdging(SkToBool(aa) ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
  82. canvas->save();
  83. for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) {
  84. paintProcs[i](&paint);
  85. for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) {
  86. paint.setBlendMode(modes[x]);
  87. canvas->save();
  88. for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
  89. procs[y](canvas, paint, font);
  90. canvas->translate(0, H * 5 / 4);
  91. }
  92. canvas->restore();
  93. canvas->translate(W * 5 / 4, 0);
  94. }
  95. }
  96. canvas->restore();
  97. canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
  98. }
  99. }
  100. static sk_sp<SkSurface> compat_surface(SkCanvas* canvas, const SkISize& size, bool skipGPU) {
  101. SkImageInfo info = SkImageInfo::MakeN32Premul(size);
  102. bool callNewSurface = true;
  103. if (canvas->getGrContext() && skipGPU) {
  104. callNewSurface = false;
  105. }
  106. sk_sp<SkSurface> surface = callNewSurface ? canvas->makeSurface(info) : nullptr;
  107. if (nullptr == surface) {
  108. // picture canvas will return null, so fall-back to raster
  109. surface = SkSurface::MakeRaster(info);
  110. }
  111. return surface;
  112. }
  113. void onDraw(SkCanvas* canvas) override {
  114. auto surf(compat_surface(canvas, this->getISize(), this->isCanvasDeferred()));
  115. surf->getCanvas()->drawColor(SK_ColorWHITE);
  116. this->drawContent(surf->getCanvas());
  117. surf->draw(canvas, 0, 0, nullptr);
  118. }
  119. };
  120. ///////////////////////////////////////////////////////////////////////////////
  121. DEF_GM(return new SrcModeGM;)