1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "gm.h"
9 #include "sk_tool_utils.h"
10 
11 #include "SkCanvas.h"
12 #include "SkSurface.h"
13 #include "SkTextBlob.h"
14 
15 // This tests that we don't try to reuse textblobs from the GPU textblob cache across pixel geometry
16 // changes when we have LCD.  crbug/486744
17 namespace skiagm {
18 class TextBlobGeometryChange : public GM {
19 public:
20     TextBlobGeometryChange() { }
21 
22 protected:
23     SkString onShortName() override {
24         return SkString("textblobgeometrychange");
25     }
26 
27     SkISize onISize() override {
28         return SkISize::Make(kWidth, kHeight);
29     }
30 
31     void onDraw(SkCanvas* canvas) override {
32         const char text[] = "Hamburgefons";
33 
34         SkPaint paint;
35         sk_tool_utils::set_portable_typeface(&paint);
36         paint.setTextSize(20);
37         paint.setAntiAlias(true);
38         paint.setLCDRenderText(true);
39 
40         SkTextBlobBuilder builder;
41 
42         sk_tool_utils::add_to_text_blob(&builder, text, paint, 10, 10);
43 
44         sk_sp<SkTextBlob> blob(builder.make());
45 
46         SkImageInfo info = SkImageInfo::MakeN32Premul(200, 200);
47         SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
48         auto surface = sk_tool_utils::makeSurface(canvas, info, &props);
49         SkCanvas* c = surface->getCanvas();
50 
51         // LCD text on white background
52         SkRect rect = SkRect::MakeLTRB(0.f, 0.f, SkIntToScalar(kWidth), kHeight / 2.f);
53         SkPaint rectPaint;
54         rectPaint.setColor(0xffffffff);
55         canvas->drawRect(rect, rectPaint);
56         canvas->drawTextBlob(blob, 10, 50, paint);
57 
58         // This should not look garbled since we should disable LCD text in this case
59         // (i.e., unknown pixel geometry)
60         c->clear(0x00ffffff);
61         c->drawTextBlob(blob, 10, 150, paint);
62         surface->draw(canvas, 0, 0, nullptr);
63     }
64 
65 private:
66     static constexpr int kWidth = 200;
67     static constexpr int kHeight = 200;
68 
69     typedef GM INHERITED;
70 };
71 
72 //////////////////////////////////////////////////////////////////////////////
73 
74 DEF_GM(return new TextBlobGeometryChange;)
75 }
76