Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 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. |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 3 | #include "tools/fiddle/examples.h" |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 4 | // HASH=06aeb3cf63ffccf7b49fe556e5def351 |
Hal Canary | a7181e7c | 2019-03-18 16:06:34 -0400 | [diff] [blame] | 5 | REG_FIDDLE(Image_MakeBackendTextureFromSkImage, 256, 64, false, 0) { |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 6 | static sk_sp<SkImage> create_gpu_image(GrRecordingContext* rContext) { |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 7 | const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 8 | auto surface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)); |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 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 | |
| 17 | void draw(SkCanvas* canvas) { |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 18 | auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 19 | if (!dContext) { |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 20 | return; |
| 21 | } |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 22 | sk_sp<SkImage> backEndImage = create_gpu_image(dContext); |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 23 | canvas->drawImage(backEndImage, 0, 0); |
| 24 | GrBackendTexture texture; |
| 25 | SkImage::BackendTextureReleaseProc proc; |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 26 | if (!SkImage::MakeBackendTextureFromSkImage(dContext, std::move(backEndImage), |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 27 | &texture, &proc)) { |
| 28 | return; |
| 29 | } |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 30 | sk_sp<SkImage> i2 = SkImage::MakeFromTexture(dContext, texture, kTopLeft_GrSurfaceOrigin, |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 31 | kN32_SkColorType, kOpaque_SkAlphaType, nullptr); |
| 32 | canvas->drawImage(i2, 30, 30); |
| 33 | } |
| 34 | } // END FIDDLE |