blob: f09eb1fa113e1c41f664095875677497c49e630a [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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
joshualitt7a9c45c2015-05-26 12:32:23 -070010
joshualitt7a9c45c2015-05-26 12:32:23 -070011#include "SkCanvas.h"
12#include "SkSurface.h"
13#include "SkTextBlob.h"
bsalomon4ee6bd82015-05-27 13:23:23 -070014#include "GrContext.h"
joshualitt7a9c45c2015-05-26 12:32:23 -070015
16// This tests that we correctly regenerate textblobs after freeing all gpu resources crbug/491350
17namespace skiagm {
18class TextBlobUseAfterGpuFree : public GM {
19public:
20 TextBlobUseAfterGpuFree() { }
21
22protected:
23 SkString onShortName() override {
24 return SkString("textblobuseaftergpufree");
25 }
26
27 SkISize onISize() override {
28 return SkISize::Make(kWidth, kHeight);
29 }
30
31 void onDraw(SkCanvas* canvas) override {
32 // This GM exists to test a specific feature of the GPU backend.
halcanary96fcdcc2015-08-27 07:41:13 -070033 if (nullptr == canvas->getGrContext()) {
halcanary2a243382015-09-09 08:16:41 -070034 skiagm::GM::DrawGpuOnlyMessage(canvas);
joshualitt7a9c45c2015-05-26 12:32:23 -070035 return;
36 }
37
38 const char text[] = "Hamburgefons";
39
40 SkPaint paint;
caryclark1818acb2015-07-24 12:09:25 -070041 sk_tool_utils::set_portable_typeface(&paint);
caryclark85693c12015-07-20 10:48:01 -070042 paint.setAntiAlias(true);
joshualitt7a9c45c2015-05-26 12:32:23 -070043 paint.setTextSize(20);
44
45 SkTextBlobBuilder builder;
46
47 sk_tool_utils::add_to_text_blob(&builder, text, paint, 10, 10);
48
fmalita37283c22016-09-13 10:00:23 -070049 sk_sp<SkTextBlob> blob(builder.make());
joshualitt7a9c45c2015-05-26 12:32:23 -070050
51 // draw textblob
52 SkRect rect = SkRect::MakeLTRB(0.f, 0.f, SkIntToScalar(kWidth), kHeight / 2.f);
53 SkPaint rectPaint;
54 rectPaint.setColor(0xffffffff);
55 canvas->drawRect(rect, rectPaint);
fmalita37283c22016-09-13 10:00:23 -070056 canvas->drawTextBlob(blob, 10, 50, paint);
joshualitt7a9c45c2015-05-26 12:32:23 -070057
58 // This text should look fine
59 canvas->getGrContext()->freeGpuResources();
fmalita37283c22016-09-13 10:00:23 -070060 canvas->drawTextBlob(blob, 10, 150, paint);
joshualitt7a9c45c2015-05-26 12:32:23 -070061 }
62
63private:
mtkleindbfd7ab2016-09-01 11:24:54 -070064 static constexpr int kWidth = 200;
65 static constexpr int kHeight = 200;
joshualitt7a9c45c2015-05-26 12:32:23 -070066
67 typedef GM INHERITED;
68};
69
70//////////////////////////////////////////////////////////////////////////////
71
halcanary385fe4d2015-08-26 13:07:48 -070072DEF_GM(return new TextBlobUseAfterGpuFree;)
joshualitt7a9c45c2015-05-26 12:32:23 -070073}