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=06aeb3cf63ffccf7b49fe556e5def351
5 REG_FIDDLE(Image_MakeBackendTextureFromSkImage, 256, 64, false, 0) {
create_gpu_image(GrRecordingContext * rContext)6 static sk_sp<SkImage> create_gpu_image(GrRecordingContext* rContext) {
7 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
8 auto surface(SkSurface::MakeRenderTarget(rContext, skgpu::Budgeted::kNo, info));
9 SkCanvas* canvas = surface->getCanvas();
10 canvas->clear(SK_ColorWHITE);
11 SkPaint paint;
12 paint.setColor(SK_ColorBLACK);
13 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
14 return surface->makeImageSnapshot();
15 }
16
draw(SkCanvas * canvas)17 void draw(SkCanvas* canvas) {
18 auto dContext = GrAsDirectContext(canvas->recordingContext());
19 if (!dContext) {
20 return;
21 }
22 sk_sp<SkImage> backEndImage = create_gpu_image(dContext);
23 canvas->drawImage(backEndImage, 0, 0);
24 GrBackendTexture texture;
25 SkImage::BackendTextureReleaseProc proc;
26 if (!SkImage::MakeBackendTextureFromSkImage(dContext, std::move(backEndImage),
27 &texture, &proc)) {
28 return;
29 }
30 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(dContext, texture, kTopLeft_GrSurfaceOrigin,
31 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
32 canvas->drawImage(i2, 30, 30);
33 }
34 } // END FIDDLE
35