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