blob: 8a1f36d6d6621225f00449255139ab30437e320d [file] [log] [blame]
joshualittcbbc9df2016-02-24 05:49:55 -08001/*
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.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
joshualittcbbc9df2016-02-24 05:49:55 -080010
11#include "SkCanvas.h"
12#include "SkTextBlob.h"
13
14namespace skiagm {
15class TextBlobBlockReordering : public GM {
16public:
17 // This gm tests that textblobs translate properly when their draw order is different from their
18 // flush order
19 TextBlobBlockReordering() { }
20
21protected:
22 void onOnceBeforeDraw() override {
23 SkTextBlobBuilder builder;
24
25 // make textblob
26 // Large text is used to trigger atlas eviction
27 SkPaint paint;
28 paint.setTextSize(56);
29 const char* text = "AB";
30 sk_tool_utils::set_portable_typeface(&paint);
31
32 SkRect bounds;
33 paint.measureText(text, strlen(text), &bounds);
34
35 SkScalar yOffset = bounds.height();
36 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset - 30);
37
38 // build
fmalita37283c22016-09-13 10:00:23 -070039 fBlob = builder.make();
joshualittcbbc9df2016-02-24 05:49:55 -080040 }
41
42 SkString onShortName() override {
43 return SkString("textblobblockreordering");
44 }
45
46 SkISize onISize() override {
47 return SkISize::Make(kWidth, kHeight);
48 }
49
Brian Salomon09d994e2016-12-21 11:14:46 -050050 // 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.
joshualittcbbc9df2016-02-24 05:49:55 -080053 void onDraw(SkCanvas* canvas) override {
54 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
55
56 SkPaint paint;
57 canvas->translate(10, 40);
58
59 SkRect bounds = fBlob->bounds();
60 const int yDelta = SkScalarFloorToInt(bounds.height()) + 20;
61 const int xDelta = SkScalarFloorToInt(bounds.width());
halcanary9d524f22016-03-29 09:03:52 -070062
joshualittcbbc9df2016-02-24 05:49:55 -080063 canvas->drawTextBlob(fBlob, 0, 0, paint);
halcanary9d524f22016-03-29 09:03:52 -070064
joshualittcbbc9df2016-02-24 05:49:55 -080065 canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta));
66
Brian Salomon09d994e2016-12-21 11:14:46 -050067 // Draw a rect where the text should be, and then twiddle the xfermode so we don't combine.
joshualittcbbc9df2016-02-24 05:49:55 -080068 SkPaint redPaint;
69 redPaint.setColor(SK_ColorRED);
70 canvas->drawRect(bounds, redPaint);
71 SkPaint srcInPaint(paint);
reed374772b2016-10-05 17:33:02 -070072 srcInPaint.setBlendMode(SkBlendMode::kSrcIn);
joshualittcbbc9df2016-02-24 05:49:55 -080073 canvas->drawTextBlob(fBlob, 0, 0, srcInPaint);
74
75 canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta));
76 canvas->drawTextBlob(fBlob, 0, 0, paint);
77 }
78
79private:
fmalita37283c22016-09-13 10:00:23 -070080 sk_sp<SkTextBlob> fBlob;
joshualittcbbc9df2016-02-24 05:49:55 -080081
mtkleindbfd7ab2016-09-01 11:24:54 -070082 static constexpr int kWidth = 275;
83 static constexpr int kHeight = 200;
joshualittcbbc9df2016-02-24 05:49:55 -080084
85 typedef GM INHERITED;
86};
halcanary9d524f22016-03-29 09:03:52 -070087
joshualittcbbc9df2016-02-24 05:49:55 -080088//////////////////////////////////////////////////////////////////////////////
89
90DEF_GM(return new TextBlobBlockReordering;)
91}