joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 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 | |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkSurface.h" |
| 12 | #include "SkTextBlob.h" |
| 13 | |
| 14 | // This tests that we don't try to reuse textblobs from the GPU textblob cache across pixel geometry |
| 15 | // changes when we have LCD. crbug/486744 |
| 16 | namespace skiagm { |
| 17 | class TextBlobGeometryChange : public GM { |
| 18 | public: |
| 19 | TextBlobGeometryChange() { } |
| 20 | |
| 21 | protected: |
| 22 | SkString onShortName() override { |
| 23 | return SkString("textblobgeometrychange"); |
| 24 | } |
| 25 | |
| 26 | SkISize onISize() override { |
| 27 | return SkISize::Make(kWidth, kHeight); |
| 28 | } |
| 29 | |
| 30 | void onDraw(SkCanvas* canvas) override { |
| 31 | const char text[] = "Hamburgefons"; |
| 32 | |
| 33 | SkPaint paint; |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 34 | sk_tool_utils::set_portable_typeface(&paint); |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 35 | paint.setTextSize(20); |
| 36 | paint.setAntiAlias(true); |
| 37 | paint.setLCDRenderText(true); |
| 38 | |
| 39 | SkTextBlobBuilder builder; |
| 40 | |
| 41 | sk_tool_utils::add_to_text_blob(&builder, text, paint, 10, 10); |
| 42 | |
| 43 | SkAutoTUnref<const SkTextBlob> blob(builder.build()); |
| 44 | |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 45 | SkImageInfo info = SkImageInfo::MakeN32(200, 200, kPremul_SkAlphaType, |
| 46 | sk_ref_sp(canvas->imageInfo().colorSpace())); |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 47 | SkSurfaceProps canvasProps(SkSurfaceProps::kLegacyFontHost_InitType); |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 48 | uint32_t gammaCorrect = canvas->getProps(&canvasProps) |
| 49 | ? canvasProps.flags() & SkSurfaceProps::kGammaCorrect_Flag : 0; |
| 50 | SkSurfaceProps props(gammaCorrect, kUnknown_SkPixelGeometry); |
mtklein | 9e65f93 | 2016-04-06 13:57:38 -0700 | [diff] [blame] | 51 | auto surface = canvas->makeSurface(info, &props); |
| 52 | if (!surface) { |
| 53 | surface = SkSurface::MakeRaster(info, &props); |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 54 | } |
mtklein | 9e65f93 | 2016-04-06 13:57:38 -0700 | [diff] [blame] | 55 | SkCanvas* c = surface->getCanvas(); |
| 56 | |
| 57 | // LCD text on white background |
| 58 | SkRect rect = SkRect::MakeLTRB(0.f, 0.f, SkIntToScalar(kWidth), kHeight / 2.f); |
| 59 | SkPaint rectPaint; |
| 60 | rectPaint.setColor(0xffffffff); |
| 61 | canvas->drawRect(rect, rectPaint); |
| 62 | canvas->drawTextBlob(blob.get(), 10, 50, paint); |
| 63 | |
| 64 | // This should not look garbled since we should disable LCD text in this case |
| 65 | // (i.e., unknown pixel geometry) |
| 66 | c->clear(0x00ffffff); |
| 67 | c->drawTextBlob(blob.get(), 10, 150, paint); |
| 68 | surface->draw(canvas, 0, 0, nullptr); |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | private: |
| 72 | static const int kWidth = 200; |
| 73 | static const int kHeight = 200; |
| 74 | |
| 75 | typedef GM INHERITED; |
| 76 | }; |
| 77 | |
| 78 | ////////////////////////////////////////////////////////////////////////////// |
| 79 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 80 | DEF_GM(return new TextBlobGeometryChange;) |
joshualitt | e4cee1f | 2015-05-11 13:04:28 -0700 | [diff] [blame] | 81 | } |