• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/GrDirectContext.h"
19 #include "tools/ToolUtils.h"
20 
21 #include <string.h>
22 
23 // This tests that we correctly regenerate textblobs after freeing all gpu resources crbug/491350
24 namespace skiagm {
25 class TextBlobUseAfterGpuFree : public GM {
26 public:
TextBlobUseAfterGpuFree()27     TextBlobUseAfterGpuFree() { }
28 
29 protected:
onShortName()30     SkString onShortName() override {
31         return SkString("textblobuseaftergpufree");
32     }
33 
onISize()34     SkISize onISize() override {
35         return SkISize::Make(kWidth, kHeight);
36     }
37 
onDraw(SkCanvas * canvas)38     void onDraw(SkCanvas* canvas) override {
39         auto dContext = GrAsDirectContext(canvas->recordingContext());
40 
41         const char text[] = "Hamburgefons";
42 
43         SkFont font(ToolUtils::create_portable_typeface(), 20);
44         auto blob = SkTextBlob::MakeFromText(text, strlen(text), font);
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);
51         canvas->drawTextBlob(blob, 20, 60, SkPaint());
52 
53         // This text should look fine
54         if (dContext) {
55             dContext->freeGpuResources();
56         }
57         canvas->drawTextBlob(blob, 20, 160, SkPaint());
58     }
59 
60 private:
61     inline static constexpr int kWidth = 200;
62     inline static constexpr int kHeight = 200;
63 
64     using INHERITED = GM;
65 };
66 
67 //////////////////////////////////////////////////////////////////////////////
68 
69 DEF_GM(return new TextBlobUseAfterGpuFree;)
70 }  // namespace skiagm
71