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 class GrRenderTargetContext;
24 
25 // This tests that we correctly regenerate textblobs after freeing all gpu resources crbug/491350
26 namespace skiagm {
27 class TextBlobUseAfterGpuFree : public GpuGM {
28 public:
TextBlobUseAfterGpuFree()29     TextBlobUseAfterGpuFree() { }
30 
31 protected:
onShortName()32     SkString onShortName() override {
33         return SkString("textblobuseaftergpufree");
34     }
35 
onISize()36     SkISize onISize() override {
37         return SkISize::Make(kWidth, kHeight);
38     }
39 
onDraw(GrRecordingContext * context,GrRenderTargetContext *,SkCanvas * canvas)40     void onDraw(GrRecordingContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
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 (auto direct = context->asDirectContext()) {
55             direct->freeGpuResources();
56         }
57         canvas->drawTextBlob(blob, 20, 160, SkPaint());
58     }
59 
60 private:
61     static constexpr int kWidth = 200;
62     static constexpr int kHeight = 200;
63 
64     using INHERITED = GM;
65 };
66 
67 //////////////////////////////////////////////////////////////////////////////
68 
69 DEF_GM(return new TextBlobUseAfterGpuFree;)
70 }  // namespace skiagm
71