joshualitt | 7a9c45c | 2015-05-26 12:32:23 -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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 10 | |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkSurface.h" |
| 13 | #include "SkTextBlob.h" |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 14 | #include "GrContext.h" |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 15 | |
| 16 | // This tests that we correctly regenerate textblobs after freeing all gpu resources crbug/491350 |
| 17 | namespace skiagm { |
| 18 | class TextBlobUseAfterGpuFree : public GM { |
| 19 | public: |
| 20 | TextBlobUseAfterGpuFree() { } |
| 21 | |
| 22 | protected: |
| 23 | SkString onShortName() override { |
| 24 | return SkString("textblobuseaftergpufree"); |
| 25 | } |
| 26 | |
| 27 | SkISize onISize() override { |
| 28 | return SkISize::Make(kWidth, kHeight); |
| 29 | } |
| 30 | |
| 31 | void onDraw(SkCanvas* canvas) override { |
| 32 | // This GM exists to test a specific feature of the GPU backend. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 33 | if (nullptr == canvas->getGrContext()) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 34 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 35 | return; |
| 36 | } |
| 37 | |
| 38 | const char text[] = "Hamburgefons"; |
| 39 | |
| 40 | SkPaint paint; |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 41 | sk_tool_utils::set_portable_typeface(&paint); |
caryclark | 85693c1 | 2015-07-20 10:48:01 -0700 | [diff] [blame] | 42 | paint.setAntiAlias(true); |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 43 | paint.setTextSize(20); |
| 44 | |
| 45 | SkTextBlobBuilder builder; |
| 46 | |
| 47 | sk_tool_utils::add_to_text_blob(&builder, text, paint, 10, 10); |
| 48 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 49 | sk_sp<SkTextBlob> blob(builder.make()); |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 50 | |
| 51 | // draw textblob |
| 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); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 56 | canvas->drawTextBlob(blob, 10, 50, paint); |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 57 | |
| 58 | // This text should look fine |
| 59 | canvas->getGrContext()->freeGpuResources(); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 60 | canvas->drawTextBlob(blob, 10, 150, paint); |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 64 | static constexpr int kWidth = 200; |
| 65 | static constexpr int kHeight = 200; |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 66 | |
| 67 | typedef GM INHERITED; |
| 68 | }; |
| 69 | |
| 70 | ////////////////////////////////////////////////////////////////////////////// |
| 71 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 72 | DEF_GM(return new TextBlobUseAfterGpuFree;) |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 73 | } |