textblobblockreordering.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2016 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/SkFontTypes.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkRefCnt.h"
  16. #include "include/core/SkScalar.h"
  17. #include "include/core/SkSize.h"
  18. #include "include/core/SkString.h"
  19. #include "include/core/SkTextBlob.h"
  20. #include "include/core/SkTypeface.h"
  21. #include "tools/ToolUtils.h"
  22. #include <string.h>
  23. namespace skiagm {
  24. class TextBlobBlockReordering : public GM {
  25. public:
  26. // This gm tests that textblobs translate properly when their draw order is different from their
  27. // flush order
  28. TextBlobBlockReordering() { }
  29. protected:
  30. void onOnceBeforeDraw() override {
  31. SkTextBlobBuilder builder;
  32. // make textblob
  33. // Large text is used to trigger atlas eviction
  34. SkFont font(ToolUtils::create_portable_typeface(), 56);
  35. font.setEdging(SkFont::Edging::kAlias);
  36. const char* text = "AB";
  37. SkRect bounds;
  38. font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
  39. SkScalar yOffset = bounds.height();
  40. ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 30);
  41. // build
  42. fBlob = builder.make();
  43. }
  44. SkString onShortName() override {
  45. return SkString("textblobblockreordering");
  46. }
  47. SkISize onISize() override {
  48. return SkISize::Make(kWidth, kHeight);
  49. }
  50. // This draws the same text blob 3 times. The second draw used a different xfer mode so its
  51. // GrDrawOp doesn't get combined with the first and third. Ultimately, they will be flushed in
  52. // the order first, third, and then second.
  53. void onDraw(SkCanvas* canvas) override {
  54. canvas->drawColor(SK_ColorGRAY);
  55. SkPaint paint;
  56. canvas->translate(10, 40);
  57. SkRect bounds = fBlob->bounds();
  58. const int yDelta = SkScalarFloorToInt(bounds.height()) + 20;
  59. const int xDelta = SkScalarFloorToInt(bounds.width());
  60. canvas->drawTextBlob(fBlob, 0, 0, paint);
  61. canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta));
  62. // Draw a rect where the text should be, and then twiddle the xfermode so we don't combine.
  63. SkPaint redPaint;
  64. redPaint.setColor(SK_ColorRED);
  65. canvas->drawRect(bounds, redPaint);
  66. SkPaint srcInPaint(paint);
  67. srcInPaint.setBlendMode(SkBlendMode::kSrcIn);
  68. canvas->drawTextBlob(fBlob, 0, 0, srcInPaint);
  69. canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta));
  70. canvas->drawTextBlob(fBlob, 0, 0, paint);
  71. }
  72. private:
  73. sk_sp<SkTextBlob> fBlob;
  74. static constexpr int kWidth = 275;
  75. static constexpr int kHeight = 200;
  76. typedef GM INHERITED;
  77. };
  78. //////////////////////////////////////////////////////////////////////////////
  79. DEF_GM(return new TextBlobBlockReordering;)
  80. }