blob: 70b97eadc0a76c9b66c870540c1c9052704bb55c [file] [log] [blame]
joshualitt7a9c45c2015-05-26 12:32:23 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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 Phillipsb7bfbc22020-07-01 12:55:01 -040018#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040020#include "tools/fonts/FontToolUtils.h"
joshualitt7a9c45c2015-05-26 12:32:23 -070021
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include <string.h>
23
joshualitt7a9c45c2015-05-26 12:32:23 -070024// This tests that we correctly regenerate textblobs after freeing all gpu resources crbug/491350
25namespace skiagm {
Robert Phillipsedcd4312021-06-03 10:14:16 -040026class TextBlobUseAfterGpuFree : public GM {
joshualitt7a9c45c2015-05-26 12:32:23 -070027public:
28 TextBlobUseAfterGpuFree() { }
29
30protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000031 SkString getName() const override { return SkString("textblobuseaftergpufree"); }
joshualitt7a9c45c2015-05-26 12:32:23 -070032
Leandro Lovisolo8f023882023-08-15 21:13:52 +000033 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
joshualitt7a9c45c2015-05-26 12:32:23 -070034
Robert Phillipsedcd4312021-06-03 10:14:16 -040035 void onDraw(SkCanvas* canvas) override {
36 auto dContext = GrAsDirectContext(canvas->recordingContext());
37
joshualitt7a9c45c2015-05-26 12:32:23 -070038 const char text[] = "Hamburgefons";
39
Kevin Lubicke836c3a2023-10-20 06:55:35 -040040 SkFont font(ToolUtils::DefaultPortableTypeface(), 20);
Mike Reed28bd8822018-12-22 22:29:45 -050041 auto blob = SkTextBlob::MakeFromText(text, strlen(text), font);
joshualitt7a9c45c2015-05-26 12:32:23 -070042
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 Reed28bd8822018-12-22 22:29:45 -050048 canvas->drawTextBlob(blob, 20, 60, SkPaint());
joshualitt7a9c45c2015-05-26 12:32:23 -070049
50 // This text should look fine
Robert Phillipsedcd4312021-06-03 10:14:16 -040051 if (dContext) {
52 dContext->freeGpuResources();
Robert Phillips95c250c2020-06-29 15:36:12 -040053 }
Mike Reed28bd8822018-12-22 22:29:45 -050054 canvas->drawTextBlob(blob, 20, 160, SkPaint());
joshualitt7a9c45c2015-05-26 12:32:23 -070055 }
56
57private:
Brian Salomon9fa47cc2021-10-08 18:48:26 -040058 inline static constexpr int kWidth = 200;
59 inline static constexpr int kHeight = 200;
joshualitt7a9c45c2015-05-26 12:32:23 -070060
John Stiles7571f9e2020-09-02 22:42:33 -040061 using INHERITED = GM;
joshualitt7a9c45c2015-05-26 12:32:23 -070062};
63
64//////////////////////////////////////////////////////////////////////////////
65
halcanary385fe4d2015-08-26 13:07:48 -070066DEF_GM(return new TextBlobUseAfterGpuFree;)
John Stilesa6841be2020-08-06 14:11:56 -040067} // namespace skiagm