pictureimagegenerator.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright 2015 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/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkColorSpace.h"
  12. #include "include/core/SkFont.h"
  13. #include "include/core/SkFontTypes.h"
  14. #include "include/core/SkImage.h"
  15. #include "include/core/SkImageGenerator.h"
  16. #include "include/core/SkImageInfo.h"
  17. #include "include/core/SkMatrix.h"
  18. #include "include/core/SkPaint.h"
  19. #include "include/core/SkPath.h"
  20. #include "include/core/SkPicture.h"
  21. #include "include/core/SkPictureRecorder.h"
  22. #include "include/core/SkPoint.h"
  23. #include "include/core/SkRect.h"
  24. #include "include/core/SkRefCnt.h"
  25. #include "include/core/SkScalar.h"
  26. #include "include/core/SkShader.h"
  27. #include "include/core/SkSize.h"
  28. #include "include/core/SkString.h"
  29. #include "include/core/SkTileMode.h"
  30. #include "include/core/SkTypeface.h"
  31. #include "include/core/SkTypes.h"
  32. #include "include/effects/SkGradientShader.h"
  33. #include "include/pathops/SkPathOps.h"
  34. #include "include/utils/SkTextUtils.h"
  35. #include "tools/ToolUtils.h"
  36. #include <string.h>
  37. #include <memory>
  38. static void draw_vector_logo(SkCanvas* canvas, const SkRect& viewBox) {
  39. constexpr char kSkiaStr[] = "SKIA";
  40. constexpr SkScalar kGradientPad = .1f;
  41. constexpr SkScalar kVerticalSpacing = 0.25f;
  42. constexpr SkScalar kAccentScale = 1.20f;
  43. SkPaint paint;
  44. paint.setAntiAlias(true);
  45. SkFont font(ToolUtils::create_portable_typeface());
  46. font.setSubpixel(true);
  47. font.setEmbolden(true);
  48. SkPath path;
  49. SkRect iBox, skiBox, skiaBox;
  50. SkTextUtils::GetPath("SKI", 3, SkTextEncoding::kUTF8, 0, 0, font, &path);
  51. TightBounds(path, &skiBox);
  52. SkTextUtils::GetPath("I", 1, SkTextEncoding::kUTF8, 0, 0, font, &path);
  53. TightBounds(path, &iBox);
  54. iBox.offsetTo(skiBox.fRight - iBox.width(), iBox.fTop);
  55. const size_t textLen = strlen(kSkiaStr);
  56. SkTextUtils::GetPath(kSkiaStr, textLen, SkTextEncoding::kUTF8, 0, 0, font, &path);
  57. TightBounds(path, &skiaBox);
  58. skiaBox.outset(0, 2 * iBox.width() * (kVerticalSpacing + 1));
  59. const SkScalar accentSize = iBox.width() * kAccentScale;
  60. const SkScalar underlineY = iBox.bottom() +
  61. (kVerticalSpacing + SkScalarSqrt(3) / 2) * accentSize;
  62. SkMatrix m;
  63. m.setRectToRect(skiaBox, viewBox, SkMatrix::kFill_ScaleToFit);
  64. SkAutoCanvasRestore acr(canvas, true);
  65. canvas->concat(m);
  66. canvas->drawCircle(iBox.centerX(),
  67. iBox.y() - (0.5f + kVerticalSpacing) * accentSize,
  68. accentSize / 2,
  69. paint);
  70. path.reset();
  71. path.moveTo(iBox.centerX() - accentSize / 2, iBox.bottom() + kVerticalSpacing * accentSize);
  72. path.rLineTo(accentSize, 0);
  73. path.lineTo(iBox.centerX(), underlineY);
  74. canvas->drawPath(path, paint);
  75. SkRect underlineRect = SkRect::MakeLTRB(iBox.centerX() - iBox.width() * accentSize * 3,
  76. underlineY,
  77. iBox.centerX(),
  78. underlineY + accentSize / 10);
  79. const SkPoint pts1[] = { SkPoint::Make(underlineRect.x(), 0),
  80. SkPoint::Make(iBox.centerX(), 0) };
  81. const SkScalar pos1[] = { 0, 0.75f };
  82. const SkColor colors1[] = { SK_ColorTRANSPARENT, SK_ColorBLACK };
  83. SkASSERT(SK_ARRAY_COUNT(pos1) == SK_ARRAY_COUNT(colors1));
  84. paint.setShader(SkGradientShader::MakeLinear(pts1, colors1, pos1, SK_ARRAY_COUNT(pos1),
  85. SkTileMode::kClamp));
  86. canvas->drawRect(underlineRect, paint);
  87. const SkPoint pts2[] = { SkPoint::Make(iBox.x() - iBox.width() * kGradientPad, 0),
  88. SkPoint::Make(iBox.right() + iBox.width() * kGradientPad, 0) };
  89. const SkScalar pos2[] = { 0, .01f, 1.0f/3, 1.0f/3, 2.0f/3, 2.0f/3, .99f, 1 };
  90. const SkColor colors2[] = {
  91. SK_ColorBLACK,
  92. 0xffca5139,
  93. 0xffca5139,
  94. 0xff8dbd53,
  95. 0xff8dbd53,
  96. 0xff5460a5,
  97. 0xff5460a5,
  98. SK_ColorBLACK
  99. };
  100. SkASSERT(SK_ARRAY_COUNT(pos2) == SK_ARRAY_COUNT(colors2));
  101. paint.setShader(SkGradientShader::MakeLinear(pts2, colors2, pos2, SK_ARRAY_COUNT(pos2),
  102. SkTileMode::kClamp));
  103. canvas->drawSimpleText(kSkiaStr, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
  104. }
  105. // This GM exercises SkPictureImageGenerator features
  106. // (in particular its matrix vs. bounds semantics).
  107. class PictureGeneratorGM : public skiagm::GM {
  108. protected:
  109. SkString onShortName() override {
  110. return SkString("pictureimagegenerator");
  111. }
  112. SkISize onISize() override {
  113. return SkISize::Make(1160, 860);
  114. }
  115. void onOnceBeforeDraw() override {
  116. const SkRect rect = SkRect::MakeWH(kPictureWidth, kPictureHeight);
  117. SkPictureRecorder recorder;
  118. SkCanvas* canvas = recorder.beginRecording(rect);
  119. draw_vector_logo(canvas, rect);
  120. fPicture = recorder.finishRecordingAsPicture();
  121. }
  122. void onDraw(SkCanvas* canvas) override {
  123. const struct {
  124. SkISize size;
  125. SkScalar scaleX, scaleY;
  126. SkScalar opacity;
  127. } configs[] = {
  128. { SkISize::Make(200, 100), 1, 1, 1 },
  129. { SkISize::Make(200, 200), 1, 1, 1 },
  130. { SkISize::Make(200, 200), 1, 2, 1 },
  131. { SkISize::Make(400, 200), 2, 2, 1 },
  132. { SkISize::Make(200, 100), 1, 1, 0.9f },
  133. { SkISize::Make(200, 200), 1, 1, 0.75f },
  134. { SkISize::Make(200, 200), 1, 2, 0.5f },
  135. { SkISize::Make(400, 200), 2, 2, 0.25f },
  136. { SkISize::Make(200, 200), 0.5f, 1, 1 },
  137. { SkISize::Make(200, 200), 1, 0.5f, 1 },
  138. { SkISize::Make(200, 200), 0.5f, 0.5f, 1 },
  139. { SkISize::Make(200, 200), 2, 2, 1 },
  140. { SkISize::Make(200, 100), -1, 1, 1 },
  141. { SkISize::Make(200, 100), 1, -1, 1 },
  142. { SkISize::Make(200, 100), -1, -1, 1 },
  143. { SkISize::Make(200, 100), -1, -1, 0.5f },
  144. };
  145. auto srgbColorSpace = SkColorSpace::MakeSRGB();
  146. const unsigned kDrawsPerRow = 4;
  147. const SkScalar kDrawSize = 250;
  148. for (size_t i = 0; i < SK_ARRAY_COUNT(configs); ++i) {
  149. SkPaint p;
  150. p.setAlphaf(configs[i].opacity);
  151. SkMatrix m = SkMatrix::MakeScale(configs[i].scaleX, configs[i].scaleY);
  152. if (configs[i].scaleX < 0) {
  153. m.postTranslate(SkIntToScalar(configs[i].size.width()), 0);
  154. }
  155. if (configs[i].scaleY < 0) {
  156. m.postTranslate(0, SkIntToScalar(configs[i].size.height()));
  157. }
  158. std::unique_ptr<SkImageGenerator> gen =
  159. SkImageGenerator::MakeFromPicture(configs[i].size, fPicture, &m,
  160. p.getAlpha() != 255 ? &p : nullptr,
  161. SkImage::BitDepth::kU8, srgbColorSpace);
  162. SkImageInfo bmInfo = gen->getInfo().makeColorSpace(canvas->imageInfo().refColorSpace());
  163. SkBitmap bm;
  164. bm.allocPixels(bmInfo);
  165. SkAssertResult(gen->getPixels(bm.info(), bm.getPixels(), bm.rowBytes()));
  166. const SkScalar x = kDrawSize * (i % kDrawsPerRow);
  167. const SkScalar y = kDrawSize * (i / kDrawsPerRow);
  168. p.setColor(0xfff0f0f0);
  169. p.setAlphaf(1.0f);
  170. canvas->drawRect(SkRect::MakeXYWH(x, y,
  171. SkIntToScalar(bm.width()),
  172. SkIntToScalar(bm.height())), p);
  173. canvas->drawBitmap(bm, x, y);
  174. }
  175. }
  176. private:
  177. sk_sp<SkPicture> fPicture;
  178. const SkScalar kPictureWidth = 200;
  179. const SkScalar kPictureHeight = 100;
  180. typedef skiagm::GM INHERITED;
  181. };
  182. DEF_GM(return new PictureGeneratorGM;)