2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
12 #include "SkTextBlob.h"
15 class TextBlobBlockReordering : public GM {
17 // This gm tests that textblobs translate properly when their draw order is different from their
19 TextBlobBlockReordering() { }
22 void onOnceBeforeDraw() override {
23 SkTextBlobBuilder builder;
26 // Large text is used to trigger atlas eviction
28 paint.setTextSize(56);
29 const char* text = "AB";
30 sk_tool_utils::set_portable_typeface(&paint);
33 paint.measureText(text, strlen(text), &bounds);
35 SkScalar yOffset = bounds.height();
36 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset - 30);
39 fBlob = builder.make();
42 SkString onShortName() override {
43 return SkString("textblobblockreordering");
46 SkISize onISize() override {
47 return SkISize::Make(kWidth, kHeight);
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_tool_utils::color_to_565(SK_ColorGRAY));
57 canvas->translate(10, 40);
59 SkRect bounds = fBlob->bounds();
60 const int yDelta = SkScalarFloorToInt(bounds.height()) + 20;
61 const int xDelta = SkScalarFloorToInt(bounds.width());
63 canvas->drawTextBlob(fBlob, 0, 0, paint);
65 canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta));
67 // Draw a rect where the text should be, and then twiddle the xfermode so we don't combine.
69 redPaint.setColor(SK_ColorRED);
70 canvas->drawRect(bounds, redPaint);
71 SkPaint srcInPaint(paint);
72 srcInPaint.setBlendMode(SkBlendMode::kSrcIn);
73 canvas->drawTextBlob(fBlob, 0, 0, srcInPaint);
75 canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta));
76 canvas->drawTextBlob(fBlob, 0, 0, paint);
80 sk_sp<SkTextBlob> fBlob;
82 static constexpr int kWidth = 275;
83 static constexpr int kHeight = 200;
88 //////////////////////////////////////////////////////////////////////////////
90 DEF_GM(return new TextBlobBlockReordering;)