blob: b1658f84d1c9acfca773a3b595d942b707525f16 [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"
9
10#include "SkCanvas.h"
11#include "SkTextBlob.h"
12
13namespace skiagm {
14class TextBlobBlockReordering : public GM {
15public:
16 // This gm tests that textblobs translate properly when their draw order is different from their
17 // flush order
18 TextBlobBlockReordering() { }
19
20protected:
21 void onOnceBeforeDraw() override {
22 SkTextBlobBuilder builder;
23
24 // make textblob
25 // Large text is used to trigger atlas eviction
26 SkPaint paint;
27 paint.setTextSize(56);
28 const char* text = "AB";
29 sk_tool_utils::set_portable_typeface(&paint);
30
31 SkRect bounds;
32 paint.measureText(text, strlen(text), &bounds);
33
34 SkScalar yOffset = bounds.height();
35 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset - 30);
36
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 {
53 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
54
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}