xref: /third_party/skia/gm/lcdoverlap.cpp (revision cb93a386)
1/*
2 * Copyright 2015 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/gm.h"
9#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/SkPaint.h"
14#include "include/core/SkRect.h"
15#include "include/core/SkRefCnt.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTextBlob.h"
20#include "include/core/SkTypeface.h"
21#include "include/core/SkTypes.h"
22#include "tools/ToolUtils.h"
23
24namespace skiagm {
25
26constexpr int kWidth = 750;
27constexpr int kHeight = 750;
28
29class LcdOverlapGM : public skiagm::GM {
30public:
31    LcdOverlapGM() {
32        const int kPointSize = 25;
33        fTextHeight = SkIntToScalar(kPointSize);
34    }
35
36protected:
37    SkString onShortName() override {
38        return SkString("lcdoverlap");
39    }
40
41    void onOnceBeforeDraw() override {
42        // build text blob
43        SkTextBlobBuilder builder;
44
45        SkFont      font(ToolUtils::create_portable_typeface(), 32);
46        const char* text = "able was I ere I saw elba";
47        font.setSubpixel(true);
48        font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
49        ToolUtils::add_to_text_blob(&builder, text, font, 0, 0);
50        fBlob = builder.make();
51    }
52
53    SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
54
55    void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkBlendMode mode,
56                      SkBlendMode mode2) {
57        const SkColor colors[] {
58                SK_ColorRED,
59                SK_ColorGREEN,
60                SK_ColorBLUE,
61                SK_ColorYELLOW,
62                SK_ColorCYAN,
63                SK_ColorMAGENTA,
64        };
65
66        for (size_t i = 0; i < SK_ARRAY_COUNT(colors); i++) {
67            canvas->save();
68            canvas->translate(x, y);
69            canvas->rotate(360.0f / SK_ARRAY_COUNT(colors) * i);
70            canvas->translate(-fBlob->bounds().width() / 2.0f - fBlob->bounds().left() + 0.5f, 0);
71
72            SkPaint textPaint;
73            textPaint.setColor(colors[i]);
74            textPaint.setBlendMode(i % 2 == 0 ? mode : mode2);
75            canvas->drawTextBlob(fBlob, 0, 0, textPaint);
76            canvas->restore();
77        }
78    }
79
80    void onDraw(SkCanvas* canvas) override {
81        SkScalar offsetX = kWidth / 4.0f;
82        SkScalar offsetY = kHeight / 4.0f;
83        drawTestCase(canvas, offsetX, offsetY,  SkBlendMode::kSrc, SkBlendMode::kSrc);
84        drawTestCase(canvas, 3 * offsetX, offsetY,  SkBlendMode::kSrcOver, SkBlendMode::kSrcOver);
85        drawTestCase(canvas, offsetX, 3 * offsetY,  SkBlendMode::kHardLight,
86                     SkBlendMode::kLuminosity);
87        drawTestCase(canvas, 3 * offsetX, 3 * offsetY,  SkBlendMode::kSrcOver, SkBlendMode::kSrc);
88    }
89
90private:
91    SkScalar fTextHeight;
92    sk_sp<SkTextBlob> fBlob;
93    using INHERITED = skiagm::GM;
94};
95
96//////////////////////////////////////////////////////////////////////////////
97
98DEF_GM( return new LcdOverlapGM; )
99}  // namespace skiagm
100