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