blob: f7d3b5cb1c318a19ebaac1557486d220983dd78b [file] [log] [blame]
joshualitt853336c2015-07-31 14:46:46 -07001/*
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.h"
9
10#include "Resources.h"
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkStream.h"
14#include "SkSurface.h"
15#include "SkTextBlob.h"
16#include "SkTypeface.h"
17#include "../src/fonts/SkRandomScalerContext.h"
18
19#if SK_SUPPORT_GPU
20
21#include "GrContext.h"
22
23namespace skiagm {
24class TextBlobRandomFont : public GM {
25public:
26 // This gm tests that textblobs can be translated and scaled with a font that returns random
27 // but deterministic masks
28 TextBlobRandomFont() { }
29
30protected:
31 void onOnceBeforeDraw() override {
32 SkTextBlobBuilder builder;
33
34 const char* text = "The quick brown fox jumps over the lazy dog.";
35
36 // make textbloben
37 SkPaint paint;
38 paint.setTextSize(32);
39 paint.setLCDRenderText(true);
40
41 // Setup our random scaler context
42 SkAutoTUnref<SkTypeface> orig(sk_tool_utils::create_portable_typeface("sans-serif",
43 SkTypeface::kBold));
44 if (NULL == orig) {
45 orig.reset(SkTypeface::RefDefault());
46 }
47 SkAutoTUnref<SkTypeface> random(SkNEW_ARGS(SkRandomTypeface, (orig, paint, false)));
48 paint.setTypeface(random);
49
50 SkRect bounds;
51 paint.measureText(text, strlen(text), &bounds);
52
53 SkScalar yOffset = bounds.height();
54 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0);
55
56 // A8
57 paint.setSubpixelText(false);
58 paint.setLCDRenderText(false);
59 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset - 32);
60
61 // build
62 fBlob.reset(builder.build());
63 }
64
65 SkString onShortName() override {
66 return SkString("textblobrandomfont");
67 }
68
69 SkISize onISize() override {
70 return SkISize::Make(kWidth, kHeight);
71 }
72
73 void onDraw(SkCanvas* canvas) override {
74 // This GM exists to test a specific feature of the GPU backend.
75 if (NULL == canvas->getGrContext()) {
76 this->drawGpuOnlyMessage(canvas);
77 return;
78 }
79
80 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
81
82 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
83 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
84 SkAutoTUnref<SkSurface> surface(canvas->newSurface(info, &props));
85 if (surface) {
86 SkPaint paint;
87 paint.setAntiAlias(true);
88
89 SkCanvas* c = surface->getCanvas();
90
91 int stride = SkScalarCeilToInt(fBlob->bounds().height() / 2) + 10;
92 int yOffset = stride;
93 for (int i = 0; i < 10; i++) {
94 // fiddle the canvas to force regen of textblobs
95 canvas->rotate(i % 2 ? 0.0f : -0.05f);
96 canvas->drawTextBlob(fBlob, 10.0f, SkIntToScalar(yOffset), paint);
97 yOffset += stride;
98
99 // This will draw as black boxes
100 c->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint);
101 surface->draw(canvas, 0, 0, nullptr);
102
103 // free gpu resources and verify
104 yOffset += stride;
105 canvas->getGrContext()->freeGpuResources();
106 canvas->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint);
107
108 yOffset += stride;
109 }
110
111 } else {
112 const char* text = "This test requires a surface";
113 size_t len = strlen(text);
114 SkPaint paint;
115 canvas->drawText(text, len, 10, 100, paint);
116 }
117 }
118
119private:
120 SkAutoTUnref<const SkTextBlob> fBlob;
121
122 static const int kWidth = 1000;
123 static const int kHeight = 1000;
124
125 typedef GM INHERITED;
126};
127
128//////////////////////////////////////////////////////////////////////////////
129
130DEF_GM( return SkNEW(TextBlobRandomFont); )
131}
132#endif