blob: 3974aca5ab0d9795e4a2585bb4ce7f6dc16b22c9 [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
49 // This draws the same text blob 3 times. The second draw used a different
50 // xfer mode so it doens't get batched with the first and third.
51 // ultimately thye iwll be flushed in the order first, third, and then second
52 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
66 // draw a rect where the text should be, and then twiddle the xfermode
67 // so we don't batch
68 SkPaint redPaint;
69 redPaint.setColor(SK_ColorRED);
70 canvas->drawRect(bounds, redPaint);
71 SkPaint srcInPaint(paint);
Mike Reed2cbcd122016-10-03 21:34:16 +000072 srcInPaint.setXfermodeMode(SkXfermode::kSrcIn_Mode);
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}