Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | #if SK_SUPPORT_ATLAS_TEXT |
| 11 | |
| 12 | #include "SkAtlasTextContext.h" |
| 13 | #include "SkAtlasTextFont.h" |
| 14 | #include "SkAtlasTextTarget.h" |
| 15 | #include "SkBitmap.h" |
| 16 | #include "SkCanvas.h" |
| 17 | #include "SkTypeface.h" |
| 18 | #include "gpu/TestContext.h" |
| 19 | #include "gpu/atlastext/GLTestAtlasTextRenderer.h" |
| 20 | #include "gpu/atlastext/TestAtlasTextRenderer.h" |
| 21 | #include "sk_tool_utils.h" |
| 22 | |
| 23 | // GM that draws text using the Atlas Text interface offscreen and then blits that to the canvas. |
| 24 | |
| 25 | static SkScalar draw_string(SkAtlasTextTarget* target, const SkString& text, SkScalar x, SkScalar y, |
| 26 | uint32_t color, sk_sp<SkTypeface> typeface, float size) { |
| 27 | auto font = SkAtlasTextFont::Make(std::move(typeface), size); |
| 28 | target->drawText(text.c_str(), text.size(), x, y, color, *font); |
| 29 | SkPaint paint; |
| 30 | paint.setTextSize(size); |
| 31 | return x + paint.measureText(text.c_str(), text.size()); |
| 32 | } |
| 33 | |
| 34 | class AtlasTextGM : public skiagm::GM { |
| 35 | public: |
| 36 | AtlasTextGM() = default; |
| 37 | |
| 38 | protected: |
| 39 | SkString onShortName() override { return SkString("atlastext"); } |
| 40 | |
| 41 | SkISize onISize() override { return SkISize::Make(kSize, kSize); } |
| 42 | |
| 43 | void onOnceBeforeDraw() override { |
| 44 | fRenderer = sk_gpu_test::MakeGLTestAtlasTextRenderer(); |
| 45 | if (!fRenderer) { |
| 46 | return; |
| 47 | } |
| 48 | fContext = SkAtlasTextContext::Make(fRenderer); |
| 49 | auto targetHandle = fRenderer->makeTargetHandle(kSize, kSize); |
| 50 | fTarget = SkAtlasTextTarget::Make(fContext, kSize, kSize, targetHandle); |
| 51 | |
| 52 | fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic()); |
| 53 | fTypefaces[1] = |
| 54 | sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Italic()); |
| 55 | fTypefaces[2] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Normal()); |
| 56 | fTypefaces[3] = |
| 57 | sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Normal()); |
| 58 | fTypefaces[4] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Bold()); |
| 59 | fTypefaces[5] = sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Bold()); |
| 60 | } |
| 61 | |
| 62 | void onDraw(SkCanvas* canvas) override { |
| 63 | if (!fRenderer) { |
| 64 | canvas->clear(SK_ColorRED); |
| 65 | return; |
| 66 | } |
Brian Salomon | 40dc8a7 | 2017-11-20 11:01:54 -0500 | [diff] [blame^] | 67 | fRenderer->clearTarget(fTarget->handle(), 0xFF808080); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 68 | auto bmp = this->drawText(); |
| 69 | SkPaint paint; |
| 70 | paint.setBlendMode(SkBlendMode::kSrc); |
| 71 | canvas->drawBitmap(bmp, 0, 0); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | SkBitmap drawText() { |
| 76 | static const int kSizes[] = {8, 13, 18, 23, 30}; |
| 77 | |
| 78 | static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), |
| 79 | SkString("abcdefghijklmnopqrstuvwxyz"), |
| 80 | SkString("0123456789"), |
| 81 | SkString("!@#$%^&*()<>[]{}")}; |
| 82 | SkScalar x = 0; |
| 83 | SkScalar y = 10; |
| 84 | |
| 85 | SkRandom random; |
| 86 | do { |
| 87 | for (auto s : kSizes) { |
| 88 | auto size = 2 * s; |
| 89 | for (const auto& typeface : fTypefaces) { |
| 90 | for (const auto& text : kTexts) { |
| 91 | uint32_t color = random.nextU(); |
| 92 | x = size + draw_string(fTarget.get(), text, x, y, color, typeface, size); |
| 93 | x = SkScalarCeilToScalar(x); |
| 94 | if (x + 100 > kSize) { |
| 95 | x = 0; |
| 96 | y += SkScalarCeilToScalar(size + 3); |
| 97 | if (y > kSize) { |
| 98 | fTarget->flush(); |
| 99 | return fRenderer->readTargetHandle(fTarget->handle()); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } while (true); |
| 106 | } |
| 107 | |
| 108 | static constexpr int kSize = 1280; |
| 109 | |
| 110 | sk_sp<SkTypeface> fTypefaces[6]; |
| 111 | sk_sp<sk_gpu_test::TestAtlasTextRenderer> fRenderer; |
| 112 | std::unique_ptr<SkAtlasTextTarget> fTarget; |
| 113 | sk_sp<SkAtlasTextContext> fContext; |
| 114 | |
| 115 | typedef GM INHERITED; |
| 116 | }; |
| 117 | |
| 118 | constexpr int AtlasTextGM::kSize; |
| 119 | |
| 120 | ////////////////////////////////////////////////////////////////////////////// |
| 121 | |
| 122 | DEF_GM(return new AtlasTextGM;) |
| 123 | |
| 124 | #endif |