blob: 5a807c573d1d53e833ac4261d25a4474c9c0b978 [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"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040023#include "tools/fonts/FontToolUtils.h"
joshualittcbbc9df2016-02-24 05:49:55 -080024
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include <string.h>
joshualittcbbc9df2016-02-24 05:49:55 -080026
27namespace skiagm {
28class TextBlobBlockReordering : public GM {
29public:
30 // This gm tests that textblobs translate properly when their draw order is different from their
31 // flush order
32 TextBlobBlockReordering() { }
33
34protected:
35 void onOnceBeforeDraw() override {
36 SkTextBlobBuilder builder;
37
38 // make textblob
39 // Large text is used to trigger atlas eviction
Kevin Lubicke836c3a2023-10-20 06:55:35 -040040 SkFont font(ToolUtils::DefaultPortableTypeface(), 56);
Mike Reed28bd8822018-12-22 22:29:45 -050041 font.setEdging(SkFont::Edging::kAlias);
joshualittcbbc9df2016-02-24 05:49:55 -080042 const char* text = "AB";
joshualittcbbc9df2016-02-24 05:49:55 -080043
44 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040045 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualittcbbc9df2016-02-24 05:49:55 -080046
47 SkScalar yOffset = bounds.height();
Mike Kleinea3f0142019-03-20 11:12:10 -050048 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset - 30);
joshualittcbbc9df2016-02-24 05:49:55 -080049
50 // build
fmalita37283c22016-09-13 10:00:23 -070051 fBlob = builder.make();
joshualittcbbc9df2016-02-24 05:49:55 -080052 }
53
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000054 SkString getName() const override { return SkString("textblobblockreordering"); }
joshualittcbbc9df2016-02-24 05:49:55 -080055
Leandro Lovisolo8f023882023-08-15 21:13:52 +000056 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
joshualittcbbc9df2016-02-24 05:49:55 -080057
Brian Salomon09d994e2016-12-21 11:14:46 -050058 // This draws the same text blob 3 times. The second draw used a different xfer mode so its
59 // GrDrawOp doesn't get combined with the first and third. Ultimately, they will be flushed in
60 // the order first, third, and then second.
joshualittcbbc9df2016-02-24 05:49:55 -080061 void onDraw(SkCanvas* canvas) override {
Mike Kleind46dce32018-08-16 10:17:03 -040062 canvas->drawColor(SK_ColorGRAY);
joshualittcbbc9df2016-02-24 05:49:55 -080063
64 SkPaint paint;
65 canvas->translate(10, 40);
66
67 SkRect bounds = fBlob->bounds();
68 const int yDelta = SkScalarFloorToInt(bounds.height()) + 20;
69 const int xDelta = SkScalarFloorToInt(bounds.width());
halcanary9d524f22016-03-29 09:03:52 -070070
joshualittcbbc9df2016-02-24 05:49:55 -080071 canvas->drawTextBlob(fBlob, 0, 0, paint);
halcanary9d524f22016-03-29 09:03:52 -070072
joshualittcbbc9df2016-02-24 05:49:55 -080073 canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta));
74
Brian Salomon09d994e2016-12-21 11:14:46 -050075 // Draw a rect where the text should be, and then twiddle the xfermode so we don't combine.
joshualittcbbc9df2016-02-24 05:49:55 -080076 SkPaint redPaint;
77 redPaint.setColor(SK_ColorRED);
78 canvas->drawRect(bounds, redPaint);
79 SkPaint srcInPaint(paint);
reed374772b2016-10-05 17:33:02 -070080 srcInPaint.setBlendMode(SkBlendMode::kSrcIn);
joshualittcbbc9df2016-02-24 05:49:55 -080081 canvas->drawTextBlob(fBlob, 0, 0, srcInPaint);
82
83 canvas->translate(SkIntToScalar(xDelta), SkIntToScalar(yDelta));
84 canvas->drawTextBlob(fBlob, 0, 0, paint);
85 }
86
87private:
fmalita37283c22016-09-13 10:00:23 -070088 sk_sp<SkTextBlob> fBlob;
joshualittcbbc9df2016-02-24 05:49:55 -080089
Brian Salomon9fa47cc2021-10-08 18:48:26 -040090 inline static constexpr int kWidth = 275;
91 inline static constexpr int kHeight = 200;
joshualittcbbc9df2016-02-24 05:49:55 -080092
John Stiles7571f9e2020-09-02 22:42:33 -040093 using INHERITED = GM;
joshualittcbbc9df2016-02-24 05:49:55 -080094};
halcanary9d524f22016-03-29 09:03:52 -070095
joshualittcbbc9df2016-02-24 05:49:55 -080096//////////////////////////////////////////////////////////////////////////////
97
98DEF_GM(return new TextBlobBlockReordering;)
John Stilesa6841be2020-08-06 14:11:56 -040099} // namespace skiagm