blob: f0b95e6e290324e5fa52995cafd79c768d889419 [file] [log] [blame]
Hal Canary87515122019-03-15 14:22:51 -04001// Copyright 2019 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Mike Kleinc0bd9f92019-04-23 12:05:21 -05003#include "tools/fiddle/examples.h"
Hal Canary87515122019-03-15 14:22:51 -04004// HASH=d3aec071998f871809f515e58abb1b0e
Hal Canarya7181e7c2019-03-18 16:06:34 -04005REG_FIDDLE(Surface_MakeFromBackendTexture, 256, 256, false, 3) {
Hal Canary87515122019-03-15 14:22:51 -04006void draw(SkCanvas* canvas) {
Robert Phillipsd1ce4cb2020-07-10 16:20:53 -04007 SkFont font(nullptr, 32);
Hal Canary87515122019-03-15 14:22:51 -04008 SkPaint paint;
Robert Phillipsd1ce4cb2020-07-10 16:20:53 -04009
10 GrRecordingContext* context = canvas->recordingContext();
Hal Canary87515122019-03-15 14:22:51 -040011 if (!context) {
Robert Phillipsd1ce4cb2020-07-10 16:20:53 -040012 canvas->drawString("GPU only!", 20, 40, font, paint);
Hal Canary87515122019-03-15 14:22:51 -040013 return;
14 }
Robert Phillipsd1ce4cb2020-07-10 16:20:53 -040015 GrDirectContext* direct = context->asDirectContext();
16 if (!direct) {
17 canvas->drawString("Direct Context only!", 20, 40, font, paint);
18 return;
19 }
20
21 sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendTexture(direct,
22 backEndTexture,
23 kTopLeft_GrSurfaceOrigin,
24 0,
25 kRGBA_8888_SkColorType,
26 nullptr,
27 nullptr,
28 nullptr);
Hal Canary87515122019-03-15 14:22:51 -040029 auto surfaceCanvas = gpuSurface->getCanvas();
Robert Phillipsd1ce4cb2020-07-10 16:20:53 -040030 surfaceCanvas->drawString("GPU rocks!", 20, 40, font, paint);
Hal Canary87515122019-03-15 14:22:51 -040031 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
32 canvas->drawImage(image, 0, 0);
33}
34} // END FIDDLE