blob: f7b3c4f24c683967a9749c102370a0d7d2e59b47 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkFontTypes.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
20#include "include/core/SkTextBlob.h"
21#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "tools/ToolUtils.h"
joshualittcbbc9df2016-02-24 05:49:55 -080023
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include <string.h>
joshualittcbbc9df2016-02-24 05:49:55 -080025
26namespace skiagm {
27class TextBlobBlockReordering : public GM {
28public:
29 // This gm tests that textblobs translate properly when their draw order is different from their
30 // flush order
31 TextBlobBlockReordering() { }
32
33protected:
34 void onOnceBeforeDraw() override {
35 SkTextBlobBuilder builder;
36
37 // make textblob
38 // Large text is used to trigger atlas eviction
Mike Kleinea3f0142019-03-20 11:12:10 -050039 SkFont font(ToolUtils::create_portable_typeface(), 56);
Mike Reed28bd8822018-12-22 22:29:45 -050040 font.setEdging(SkFont::Edging::kAlias);
joshualittcbbc9df2016-02-24 05:49:55 -080041 const char* text = "AB";
joshualittcbbc9df2016-02-24 05:49:55 -080042
43 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040044 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualittcbbc9df2016-02-24 05:49:55 -080045
46 SkScalar yOffset = bounds.height();
Mike Kleinea3f0142019-03-20 11:12:10 -050047 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 30);
joshualittcbbc9df2016-02-24 05:49:55 -080048
49 // build
fmalita37283c22016-09-13 10:00:23 -070050 fBlob = builder.make();
joshualittcbbc9df2016-02-24 05:49:55 -080051 }
52
53 SkString onShortName() override {
54 return SkString("textblobblockreordering");
55 }
56
57 SkISize onISize() override {
58 return SkISize::Make(kWidth, kHeight);
59 }
60
Brian Salomon09d994e2016-12-21 11:14:46 -050061 // This draws the same text blob 3 times. The second draw used a different xfer mode so its
62 // GrDrawOp doesn't get combined with the first and third. Ultimately, they will be flushed in
63 // the order first, third, and then second.
joshualittcbbc9df2016-02-24 05:49:55 -080064 void onDraw(SkCanvas* canvas) override {
Mike Kleind46dce32018-08-16 10:17:03 -040065 canvas->drawColor(SK_ColorGRAY);
joshualittcbbc9df2016-02-24 05:49:55 -080066
67 SkPaint paint;
68 canvas->translate(10, 40);
69
70 SkRect bounds = fBlob->bounds();
71 const int yDelta = SkScalarFloorToInt(bounds.height()) + 20;
72 const int xDelta = SkScalarFloorToInt(bounds.width());
halcanary9d524f22016-03-29 09:03:52 -070073
joshualittcbbc9df2016-02-24 05:49:55 -080074 canvas->drawTextBlob(fBlob, 0, 0, paint);
halcanary9d524f22016-03-29 09:03:52 -070075
joshualittcbbc9df2016-02-24 05:49:55 -080076 canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta));
77
Brian Salomon09d994e2016-12-21 11:14:46 -050078 // Draw a rect where the text should be, and then twiddle the xfermode so we don't combine.
joshualittcbbc9df2016-02-24 05:49:55 -080079 SkPaint redPaint;
80 redPaint.setColor(SK_ColorRED);
81 canvas->drawRect(bounds, redPaint);
82 SkPaint srcInPaint(paint);
reed374772b2016-10-05 17:33:02 -070083 srcInPaint.setBlendMode(SkBlendMode::kSrcIn);
joshualittcbbc9df2016-02-24 05:49:55 -080084 canvas->drawTextBlob(fBlob, 0, 0, srcInPaint);
85
86 canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta));
87 canvas->drawTextBlob(fBlob, 0, 0, paint);
88 }
89
90private:
fmalita37283c22016-09-13 10:00:23 -070091 sk_sp<SkTextBlob> fBlob;
joshualittcbbc9df2016-02-24 05:49:55 -080092
mtkleindbfd7ab2016-09-01 11:24:54 -070093 static constexpr int kWidth = 275;
94 static constexpr int kHeight = 200;
joshualittcbbc9df2016-02-24 05:49:55 -080095
96 typedef GM INHERITED;
97};
halcanary9d524f22016-03-29 09:03:52 -070098
joshualittcbbc9df2016-02-24 05:49:55 -080099//////////////////////////////////////////////////////////////////////////////
100
101DEF_GM(return new TextBlobBlockReordering;)
102}