1 // 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.
3 #include "tools/fiddle/examples.h"
4 // HASH=d093aad721261f421c4bef4a296aab48
5 REG_FIDDLE(Image_getBackendTexture, 256, 256, false, 3) {
draw(SkCanvas * canvas)6 void draw(SkCanvas* canvas) {
7     SkFont font;
8     SkPaint paint;
9 
10     GrRecordingContext* context = canvas->recordingContext();
11     if (!context) {
12         canvas->drawString("GPU only!", 20, 40, font, paint);
13         return;
14     }
15     GrDirectContext* direct = context->asDirectContext();
16     if (!direct) {
17         canvas->drawString("Direct context only!", 20, 40, font, paint);
18         return;
19     }
20 
21     sk_sp<SkImage> imageFromBackend = SkImage::MakeFromAdoptedTexture(direct,
22                                                                       backEndTexture,
23                                                                       kBottomLeft_GrSurfaceOrigin,
24                                                                       kRGBA_8888_SkColorType,
25                                                                       kOpaque_SkAlphaType);
26     GrBackendTexture textureFromImage = imageFromBackend->getBackendTexture(false);
27     if (!textureFromImage.isValid()) {
28         return;
29     }
30     sk_sp<SkImage> imageFromTexture = SkImage::MakeFromAdoptedTexture(direct,
31                                                                       textureFromImage,
32                                                                       kTopLeft_GrSurfaceOrigin,
33                                                                       kRGBA_8888_SkColorType,
34                                                                       kOpaque_SkAlphaType);
35     canvas->drawImage(imageFromTexture, 0, 0);
36     canvas->drawImage(imageFromBackend, 128, 128);
37 }
38 }  // END FIDDLE
39