textblobcolortrans.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkFont.h"
  11. #include "include/core/SkFontTypes.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkSize.h"
  17. #include "include/core/SkString.h"
  18. #include "include/core/SkTextBlob.h"
  19. #include "include/core/SkTypeface.h"
  20. #include "include/core/SkTypes.h"
  21. #include "tools/ToolUtils.h"
  22. #include <string.h>
  23. namespace skiagm {
  24. class TextBlobColorTrans : public GM {
  25. public:
  26. // This gm tests that textblobs can be translated and have their colors regenerated
  27. // correctly. With smaller atlas sizes, it can also trigger regeneration of texture coords on
  28. // the GPU backend
  29. TextBlobColorTrans() { }
  30. protected:
  31. void onOnceBeforeDraw() override {
  32. SkTextBlobBuilder builder;
  33. // make textblob
  34. // Large text is used to trigger atlas eviction
  35. SkFont font(ToolUtils::create_portable_typeface(), 256);
  36. font.setEdging(SkFont::Edging::kAlias);
  37. const char* text = "AB";
  38. SkRect bounds;
  39. font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
  40. SkScalar yOffset = bounds.height();
  41. ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 30);
  42. // A8
  43. font.setSize(28);
  44. text = "The quick brown fox jumps over the lazy dog.";
  45. font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
  46. ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 8);
  47. // build
  48. fBlob = builder.make();
  49. }
  50. SkString onShortName() override {
  51. return SkString("textblobcolortrans");
  52. }
  53. SkISize onISize() override {
  54. return SkISize::Make(kWidth, kHeight);
  55. }
  56. void onDraw(SkCanvas* canvas) override {
  57. canvas->drawColor(SK_ColorGRAY);
  58. SkPaint paint;
  59. canvas->translate(10, 40);
  60. SkRect bounds = fBlob->bounds();
  61. // Colors were chosen to map to pairs of canonical colors. The GPU Backend will cache A8
  62. // Texture Blobs based on the canonical color they map to. Canonical colors are used to
  63. // create masks. For A8 there are 8 of them.
  64. SkColor colors[] = {SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorYELLOW, SK_ColorWHITE};
  65. size_t count = SK_ARRAY_COUNT(colors);
  66. size_t colorIndex = 0;
  67. for (int y = 0; y + SkScalarFloorToInt(bounds.height()) < kHeight;
  68. y += SkScalarFloorToInt(bounds.height())) {
  69. paint.setColor(colors[colorIndex++ % count]);
  70. canvas->save();
  71. canvas->translate(0, SkIntToScalar(y));
  72. canvas->drawTextBlob(fBlob, 0, 0, paint);
  73. canvas->restore();
  74. }
  75. }
  76. private:
  77. sk_sp<SkTextBlob> fBlob;
  78. static constexpr int kWidth = 675;
  79. static constexpr int kHeight = 1600;
  80. typedef GM INHERITED;
  81. };
  82. //////////////////////////////////////////////////////////////////////////////
  83. DEF_GM(return new TextBlobColorTrans;)
  84. }