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.h"
9 #include "sk_tool_utils.h"
10 
11 #if SK_SUPPORT_GPU
12 
13 #include "SkCanvas.h"
14 #include "SkSurface.h"
15 #include "SkTextBlob.h"
16 #include "GrContext.h"
17 
18 // This tests that we correctly regenerate textblobs after freeing all gpu resources crbug/491350
19 namespace skiagm {
20 class TextBlobUseAfterGpuFree : public GM {
21 public:
TextBlobUseAfterGpuFree()22     TextBlobUseAfterGpuFree() { }
23 
24 protected:
onShortName()25     SkString onShortName() override {
26         return SkString("textblobuseaftergpufree");
27     }
28 
onISize()29     SkISize onISize() override {
30         return SkISize::Make(kWidth, kHeight);
31     }
32 
onDraw(SkCanvas * canvas)33     void onDraw(SkCanvas* canvas) override {
34         // This GM exists to test a specific feature of the GPU backend.
35         if (nullptr == canvas->getGrContext()) {
36             skiagm::GM::DrawGpuOnlyMessage(canvas);
37             return;
38         }
39 
40         const char text[] = "Hamburgefons";
41 
42         SkPaint paint;
43         sk_tool_utils::set_portable_typeface(&paint);
44         paint.setAntiAlias(true);
45         paint.setTextSize(20);
46 
47         SkTextBlobBuilder builder;
48 
49         sk_tool_utils::add_to_text_blob(&builder, text, paint, 10, 10);
50 
51         sk_sp<SkTextBlob> blob(builder.make());
52 
53         // draw textblob
54         SkRect rect = SkRect::MakeLTRB(0.f, 0.f, SkIntToScalar(kWidth), kHeight / 2.f);
55         SkPaint rectPaint;
56         rectPaint.setColor(0xffffffff);
57         canvas->drawRect(rect, rectPaint);
58         canvas->drawTextBlob(blob, 10, 50, paint);
59 
60         // This text should look fine
61         canvas->getGrContext()->freeGpuResources();
62         canvas->drawTextBlob(blob, 10, 150, paint);
63     }
64 
65 private:
66     static constexpr int kWidth = 200;
67     static constexpr int kHeight = 200;
68 
69     typedef GM INHERITED;
70 };
71 
72 //////////////////////////////////////////////////////////////////////////////
73 
74 DEF_GM(return new TextBlobUseAfterGpuFree;)
75 }
76 #endif
77