blob: c3745cce77a44059b6cf49d5ccc884053c69cb57 [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"
joshualitt7a9c45c2015-05-26 12:32:23 -070020
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include <string.h>
22
Brian Salomoneebe7352020-12-09 16:37:04 -050023class GrSurfaceDrawContext;
joshualitt7a9c45c2015-05-26 12:32:23 -070024
25// This tests that we correctly regenerate textblobs after freeing all gpu resources crbug/491350
26namespace skiagm {
Chris Dalton3a778372019-02-07 15:23:36 -070027class TextBlobUseAfterGpuFree : public GpuGM {
joshualitt7a9c45c2015-05-26 12:32:23 -070028public:
29 TextBlobUseAfterGpuFree() { }
30
31protected:
32 SkString onShortName() override {
33 return SkString("textblobuseaftergpufree");
34 }
35
36 SkISize onISize() override {
37 return SkISize::Make(kWidth, kHeight);
38 }
39
Brian Salomoneebe7352020-12-09 16:37:04 -050040 void onDraw(GrRecordingContext* context, GrSurfaceDrawContext*, SkCanvas* canvas) override {
joshualitt7a9c45c2015-05-26 12:32:23 -070041 const char text[] = "Hamburgefons";
42
Mike Kleinea3f0142019-03-20 11:12:10 -050043 SkFont font(ToolUtils::create_portable_typeface(), 20);
Mike Reed28bd8822018-12-22 22:29:45 -050044 auto blob = SkTextBlob::MakeFromText(text, strlen(text), font);
joshualitt7a9c45c2015-05-26 12:32:23 -070045
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);
Mike Reed28bd8822018-12-22 22:29:45 -050051 canvas->drawTextBlob(blob, 20, 60, SkPaint());
joshualitt7a9c45c2015-05-26 12:32:23 -070052
53 // This text should look fine
Robert Phillipsf8f45d92020-07-01 11:11:18 -040054 if (auto direct = context->asDirectContext()) {
Robert Phillips44333c52020-06-30 13:28:00 -040055 direct->freeGpuResources();
Robert Phillips95c250c2020-06-29 15:36:12 -040056 }
Mike Reed28bd8822018-12-22 22:29:45 -050057 canvas->drawTextBlob(blob, 20, 160, SkPaint());
joshualitt7a9c45c2015-05-26 12:32:23 -070058 }
59
60private:
mtkleindbfd7ab2016-09-01 11:24:54 -070061 static constexpr int kWidth = 200;
62 static constexpr int kHeight = 200;
joshualitt7a9c45c2015-05-26 12:32:23 -070063
John Stiles7571f9e2020-09-02 22:42:33 -040064 using INHERITED = GM;
joshualitt7a9c45c2015-05-26 12:32:23 -070065};
66
67//////////////////////////////////////////////////////////////////////////////
68
halcanary385fe4d2015-08-26 13:07:48 -070069DEF_GM(return new TextBlobUseAfterGpuFree;)
John Stilesa6841be2020-08-06 14:11:56 -040070} // namespace skiagm