blob: 1121380b5eaa3a1a21a4572dd0e533f959a9af1e [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
Mike Reed28bd8822018-12-22 22:29:45 -050027 SkFont font(sk_tool_utils::create_portable_typeface(), 56);
28 font.setEdging(SkFont::Edging::kAlias);
joshualittcbbc9df2016-02-24 05:49:55 -080029 const char* text = "AB";
joshualittcbbc9df2016-02-24 05:49:55 -080030
31 SkRect bounds;
Mike Reed28bd8822018-12-22 22:29:45 -050032 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
joshualittcbbc9df2016-02-24 05:49:55 -080033
34 SkScalar yOffset = bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050035 sk_tool_utils::add_to_text_blob(&builder, text, font, 0, yOffset - 30);
joshualittcbbc9df2016-02-24 05:49:55 -080036
37 // build
fmalita37283c22016-09-13 10:00:23 -070038 fBlob = builder.make();
joshualittcbbc9df2016-02-24 05:49:55 -080039 }
40
41 SkString onShortName() override {
42 return SkString("textblobblockreordering");
43 }
44
45 SkISize onISize() override {
46 return SkISize::Make(kWidth, kHeight);
47 }
48
Brian Salomon09d994e2016-12-21 11:14:46 -050049 // This draws the same text blob 3 times. The second draw used a different xfer mode so its
50 // GrDrawOp doesn't get combined with the first and third. Ultimately, they will be flushed in
51 // the order first, third, and then second.
joshualittcbbc9df2016-02-24 05:49:55 -080052 void onDraw(SkCanvas* canvas) override {
Mike Kleind46dce32018-08-16 10:17:03 -040053 canvas->drawColor(SK_ColorGRAY);
joshualittcbbc9df2016-02-24 05:49:55 -080054
55 SkPaint paint;
56 canvas->translate(10, 40);
57
58 SkRect bounds = fBlob->bounds();
59 const int yDelta = SkScalarFloorToInt(bounds.height()) + 20;
60 const int xDelta = SkScalarFloorToInt(bounds.width());
halcanary9d524f22016-03-29 09:03:52 -070061
joshualittcbbc9df2016-02-24 05:49:55 -080062 canvas->drawTextBlob(fBlob, 0, 0, paint);
halcanary9d524f22016-03-29 09:03:52 -070063
joshualittcbbc9df2016-02-24 05:49:55 -080064 canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta));
65
Brian Salomon09d994e2016-12-21 11:14:46 -050066 // Draw a rect where the text should be, and then twiddle the xfermode so we don't combine.
joshualittcbbc9df2016-02-24 05:49:55 -080067 SkPaint redPaint;
68 redPaint.setColor(SK_ColorRED);
69 canvas->drawRect(bounds, redPaint);
70 SkPaint srcInPaint(paint);
reed374772b2016-10-05 17:33:02 -070071 srcInPaint.setBlendMode(SkBlendMode::kSrcIn);
joshualittcbbc9df2016-02-24 05:49:55 -080072 canvas->drawTextBlob(fBlob, 0, 0, srcInPaint);
73
74 canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta));
75 canvas->drawTextBlob(fBlob, 0, 0, paint);
76 }
77
78private:
fmalita37283c22016-09-13 10:00:23 -070079 sk_sp<SkTextBlob> fBlob;
joshualittcbbc9df2016-02-24 05:49:55 -080080
mtkleindbfd7ab2016-09-01 11:24:54 -070081 static constexpr int kWidth = 275;
82 static constexpr int kHeight = 200;
joshualittcbbc9df2016-02-24 05:49:55 -080083
84 typedef GM INHERITED;
85};
halcanary9d524f22016-03-29 09:03:52 -070086
joshualittcbbc9df2016-02-24 05:49:55 -080087//////////////////////////////////////////////////////////////////////////////
88
89DEF_GM(return new TextBlobBlockReordering;)
90}