xref: /aosp_15_r20/external/skia/gm/textblobuseaftergpufree.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/gm.h"
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"
18 #include "include/gpu/ganesh/GrDirectContext.h"
19 #include "tools/ToolUtils.h"
20 #include "tools/fonts/FontToolUtils.h"
21 
22 #include <string.h>
23 
24 // This tests that we correctly regenerate textblobs after freeing all gpu resources crbug/491350
25 namespace skiagm {
26 class TextBlobUseAfterGpuFree : public GM {
27 public:
TextBlobUseAfterGpuFree()28     TextBlobUseAfterGpuFree() { }
29 
30 protected:
getName() const31     SkString getName() const override { return SkString("textblobuseaftergpufree"); }
32 
getISize()33     SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
34 
onDraw(SkCanvas * canvas)35     void onDraw(SkCanvas* canvas) override {
36         auto dContext = GrAsDirectContext(canvas->recordingContext());
37 
38         const char text[] = "Hamburgefons";
39 
40         SkFont font(ToolUtils::DefaultPortableTypeface(), 20);
41         auto blob = SkTextBlob::MakeFromText(text, strlen(text), font);
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);
48         canvas->drawTextBlob(blob, 20, 60, SkPaint());
49 
50         // This text should look fine
51         if (dContext) {
52             dContext->freeGpuResources();
53         }
54         canvas->drawTextBlob(blob, 20, 160, SkPaint());
55     }
56 
57 private:
58     inline static constexpr int kWidth = 200;
59     inline static constexpr int kHeight = 200;
60 
61     using INHERITED = GM;
62 };
63 
64 //////////////////////////////////////////////////////////////////////////////
65 
66 DEF_GM(return new TextBlobUseAfterGpuFree;)
67 }  // namespace skiagm
68