xref: /third_party/skia/docs/examples/Image_MakeBackendTextureFromSkImage.cpp (revision cb93a386)
  • Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
  • only in /third_party/skia/docs/examples/
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
5REG_FIDDLE(Image_MakeBackendTextureFromSkImage, 256, 64, false, 0) {
6static sk_sp<SkImage> create_gpu_image(GrRecordingContext* rContext) {
7    const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
8    auto surface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::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
17void 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

Indexes created Thu Nov 07 10:32:03 CST 2024