1cb93a386Sopenharmony_ci// Copyright 2019 Google LLC. 2cb93a386Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. 3cb93a386Sopenharmony_ci#include "tools/fiddle/examples.h" 4cb93a386Sopenharmony_ci// HASH=06aeb3cf63ffccf7b49fe556e5def351 5cb93a386Sopenharmony_ciREG_FIDDLE(Image_MakeBackendTextureFromSkImage, 256, 64, false, 0) { 6cb93a386Sopenharmony_cistatic sk_sp<SkImage> create_gpu_image(GrRecordingContext* rContext) { 7cb93a386Sopenharmony_ci const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); 8cb93a386Sopenharmony_ci auto surface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)); 9cb93a386Sopenharmony_ci SkCanvas* canvas = surface->getCanvas(); 10cb93a386Sopenharmony_ci canvas->clear(SK_ColorWHITE); 11cb93a386Sopenharmony_ci SkPaint paint; 12cb93a386Sopenharmony_ci paint.setColor(SK_ColorBLACK); 13cb93a386Sopenharmony_ci canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint); 14cb93a386Sopenharmony_ci return surface->makeImageSnapshot(); 15cb93a386Sopenharmony_ci} 16cb93a386Sopenharmony_ci 17cb93a386Sopenharmony_civoid draw(SkCanvas* canvas) { 18cb93a386Sopenharmony_ci auto dContext = GrAsDirectContext(canvas->recordingContext()); 19cb93a386Sopenharmony_ci if (!dContext) { 20cb93a386Sopenharmony_ci return; 21cb93a386Sopenharmony_ci } 22cb93a386Sopenharmony_ci sk_sp<SkImage> backEndImage = create_gpu_image(dContext); 23cb93a386Sopenharmony_ci canvas->drawImage(backEndImage, 0, 0); 24cb93a386Sopenharmony_ci GrBackendTexture texture; 25cb93a386Sopenharmony_ci SkImage::BackendTextureReleaseProc proc; 26cb93a386Sopenharmony_ci if (!SkImage::MakeBackendTextureFromSkImage(dContext, std::move(backEndImage), 27cb93a386Sopenharmony_ci &texture, &proc)) { 28cb93a386Sopenharmony_ci return; 29cb93a386Sopenharmony_ci } 30cb93a386Sopenharmony_ci sk_sp<SkImage> i2 = SkImage::MakeFromTexture(dContext, texture, kTopLeft_GrSurfaceOrigin, 31cb93a386Sopenharmony_ci kN32_SkColorType, kOpaque_SkAlphaType, nullptr); 32cb93a386Sopenharmony_ci canvas->drawImage(i2, 30, 30); 33cb93a386Sopenharmony_ci} 34cb93a386Sopenharmony_ci} // END FIDDLE 35