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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkFont.h" |
| 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkRect.h" |
| 13 | #include "include/core/SkScalar.h" |
| 14 | #include "include/core/SkSize.h" |
| 15 | #include "include/core/SkString.h" |
| 16 | #include "include/core/SkTextBlob.h" |
| 17 | #include "include/core/SkTypeface.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 18 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "tools/ToolUtils.h" |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 20 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #include <string.h> |
| 22 | |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 23 | class GrSurfaceDrawContext; |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 24 | |
| 25 | // This tests that we correctly regenerate textblobs after freeing all gpu resources crbug/491350 |
| 26 | namespace skiagm { |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 27 | class TextBlobUseAfterGpuFree : public GpuGM { |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 28 | public: |
| 29 | TextBlobUseAfterGpuFree() { } |
| 30 | |
| 31 | protected: |
| 32 | SkString onShortName() override { |
| 33 | return SkString("textblobuseaftergpufree"); |
| 34 | } |
| 35 | |
| 36 | SkISize onISize() override { |
| 37 | return SkISize::Make(kWidth, kHeight); |
| 38 | } |
| 39 | |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 40 | void onDraw(GrRecordingContext* context, GrSurfaceDrawContext*, SkCanvas* canvas) override { |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 41 | const char text[] = "Hamburgefons"; |
| 42 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 43 | SkFont font(ToolUtils::create_portable_typeface(), 20); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 44 | auto blob = SkTextBlob::MakeFromText(text, strlen(text), font); |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 45 | |
| 46 | // draw textblob |
| 47 | SkRect rect = SkRect::MakeLTRB(0.f, 0.f, SkIntToScalar(kWidth), kHeight / 2.f); |
| 48 | SkPaint rectPaint; |
| 49 | rectPaint.setColor(0xffffffff); |
| 50 | canvas->drawRect(rect, rectPaint); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 51 | canvas->drawTextBlob(blob, 20, 60, SkPaint()); |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 52 | |
| 53 | // This text should look fine |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 54 | if (auto direct = context->asDirectContext()) { |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 55 | direct->freeGpuResources(); |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 56 | } |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 57 | canvas->drawTextBlob(blob, 20, 160, SkPaint()); |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 61 | static constexpr int kWidth = 200; |
| 62 | static constexpr int kHeight = 200; |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 63 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 64 | using INHERITED = GM; |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | ////////////////////////////////////////////////////////////////////////////// |
| 68 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 69 | DEF_GM(return new TextBlobUseAfterGpuFree;) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 70 | } // namespace skiagm |