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" |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 20 | #include "tools/fonts/FontToolUtils.h" |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 21 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 22 | #include <string.h> |
| 23 | |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 24 | // This tests that we correctly regenerate textblobs after freeing all gpu resources crbug/491350 |
| 25 | namespace skiagm { |
Robert Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 26 | class TextBlobUseAfterGpuFree : public GM { |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 27 | public: |
| 28 | TextBlobUseAfterGpuFree() { } |
| 29 | |
| 30 | protected: |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 31 | SkString getName() const override { return SkString("textblobuseaftergpufree"); } |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 32 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 33 | SkISize getISize() override { return SkISize::Make(kWidth, kHeight); } |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 34 | |
Robert Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 35 | void onDraw(SkCanvas* canvas) override { |
| 36 | auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 37 | |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 38 | const char text[] = "Hamburgefons"; |
| 39 | |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 40 | SkFont font(ToolUtils::DefaultPortableTypeface(), 20); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 41 | auto blob = SkTextBlob::MakeFromText(text, strlen(text), font); |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 42 | |
| 43 | // draw textblob |
| 44 | SkRect rect = SkRect::MakeLTRB(0.f, 0.f, SkIntToScalar(kWidth), kHeight / 2.f); |
| 45 | SkPaint rectPaint; |
| 46 | rectPaint.setColor(0xffffffff); |
| 47 | canvas->drawRect(rect, rectPaint); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 48 | canvas->drawTextBlob(blob, 20, 60, SkPaint()); |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 49 | |
| 50 | // This text should look fine |
Robert Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 51 | if (dContext) { |
| 52 | dContext->freeGpuResources(); |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 53 | } |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 54 | canvas->drawTextBlob(blob, 20, 160, SkPaint()); |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private: |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 58 | inline static constexpr int kWidth = 200; |
| 59 | inline static constexpr int kHeight = 200; |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 60 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 61 | using INHERITED = GM; |
joshualitt | 7a9c45c | 2015-05-26 12:32:23 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | ////////////////////////////////////////////////////////////////////////////// |
| 65 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 66 | DEF_GM(return new TextBlobUseAfterGpuFree;) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 67 | } // namespace skiagm |