SampleTiling.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright 2011 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 "include/core/SkBitmap.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColorFilter.h"
  10. #include "include/core/SkColorPriv.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkPath.h"
  13. #include "include/core/SkPicture.h"
  14. #include "include/core/SkPictureRecorder.h"
  15. #include "include/core/SkRegion.h"
  16. #include "include/core/SkShader.h"
  17. #include "include/core/SkTypeface.h"
  18. #include "include/utils/SkTextUtils.h"
  19. #include "samplecode/Sample.h"
  20. #include "src/utils/SkUTF.h"
  21. #ifdef SK_SUPPORT_LEGACY_DRAWLOOPER
  22. // effects
  23. #include "include/effects/SkBlurDrawLooper.h"
  24. #include "include/effects/SkGradientShader.h"
  25. #include "src/core/SkBlurMask.h"
  26. static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
  27. bm->allocPixels(SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType));
  28. bm->eraseColor(SK_ColorTRANSPARENT);
  29. SkCanvas canvas(*bm);
  30. SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h) } };
  31. SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
  32. SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
  33. SkPaint paint;
  34. paint.setDither(true);
  35. paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
  36. SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
  37. canvas.drawPaint(paint);
  38. }
  39. static void setup(SkPaint* paint, const SkBitmap& bm, bool filter, SkTileMode tmx, SkTileMode tmy) {
  40. paint->setShader(bm.makeShader(tmx, tmy));
  41. paint->setFilterQuality(filter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
  42. }
  43. static const SkColorType gColorTypes[] = {
  44. kN32_SkColorType,
  45. kRGB_565_SkColorType,
  46. };
  47. static const int gWidth = 32;
  48. static const int gHeight = 32;
  49. class TilingView : public Sample {
  50. sk_sp<SkPicture> fTextPicture;
  51. sk_sp<SkDrawLooper> fLooper;
  52. public:
  53. TilingView()
  54. : fLooper(SkBlurDrawLooper::Make(0x88000000,
  55. SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
  56. SkIntToScalar(2), SkIntToScalar(2))) {
  57. for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
  58. makebm(&fTexture[i], gColorTypes[i], gWidth, gHeight);
  59. }
  60. }
  61. virtual ~TilingView() {
  62. }
  63. SkBitmap fTexture[SK_ARRAY_COUNT(gColorTypes)];
  64. protected:
  65. virtual SkString name() { return SkString("Tiling"); }
  66. virtual void onDrawContent(SkCanvas* canvas) {
  67. SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };
  68. static const char* gConfigNames[] = { "8888", "565", "4444" };
  69. static const bool gFilters[] = { false, true };
  70. static const char* gFilterNames[] = { "point", "bilinear" };
  71. static const SkTileMode gModes[] = { SkTileMode::kClamp, SkTileMode::kRepeat, SkTileMode::kMirror };
  72. static const char* gModeNames[] = { "C", "R", "M" };
  73. SkScalar y = SkIntToScalar(24);
  74. SkScalar x = SkIntToScalar(10);
  75. SkPictureRecorder recorder;
  76. SkCanvas* textCanvas = nullptr;
  77. if (nullptr == fTextPicture) {
  78. textCanvas = recorder.beginRecording(1000, 1000, nullptr, 0);
  79. }
  80. if (textCanvas) {
  81. for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
  82. for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
  83. SkPaint p;
  84. SkString str;
  85. p.setDither(true);
  86. p.setLooper(fLooper);
  87. str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
  88. SkTextUtils::DrawString(textCanvas, str.c_str(), x + r.width()/2, y, SkFont(), p,
  89. SkTextUtils::kCenter_Align);
  90. x += r.width() * 4 / 3;
  91. }
  92. }
  93. }
  94. y += SkIntToScalar(16);
  95. for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypes); i++) {
  96. for (size_t j = 0; j < SK_ARRAY_COUNT(gFilters); j++) {
  97. x = SkIntToScalar(10);
  98. for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
  99. for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
  100. SkPaint paint;
  101. setup(&paint, fTexture[i], gFilters[j], gModes[kx], gModes[ky]);
  102. paint.setDither(true);
  103. canvas->save();
  104. canvas->translate(x, y);
  105. canvas->drawRect(r, paint);
  106. canvas->restore();
  107. x += r.width() * 4 / 3;
  108. }
  109. }
  110. if (textCanvas) {
  111. SkPaint p;
  112. p.setLooper(fLooper);
  113. textCanvas->drawString(
  114. SkStringPrintf("%s, %s", gConfigNames[i], gFilterNames[j]),
  115. x, y + r.height() * 2 / 3, SkFont(), p);
  116. }
  117. y += r.height() * 4 / 3;
  118. }
  119. }
  120. if (textCanvas) {
  121. SkASSERT(nullptr == fTextPicture);
  122. fTextPicture = recorder.finishRecordingAsPicture();
  123. }
  124. SkASSERT(fTextPicture);
  125. canvas->drawPicture(fTextPicture.get());
  126. }
  127. private:
  128. typedef Sample INHERITED;
  129. };
  130. //////////////////////////////////////////////////////////////////////////////
  131. DEF_SAMPLE( return new TilingView(); )
  132. #endif