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