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=afc62f38aebc56af8e425297ec67dd37
5 REG_FIDDLE(Image_isValid, 256, 256, false, 5) {
draw(SkCanvas * canvas)6 void draw(SkCanvas* canvas) {
7 auto dContext = GrAsDirectContext(canvas->recordingContext());
8 if (!dContext) {
9 return;
10 }
11
12 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
13 if (nullptr == image) {
14 return;
15 }
16 SkFont font;
17 SkPaint paint;
18 paint.setAntiAlias(true);
19 canvas->drawImage(image, 0, 0);
20 canvas->drawString(label, image->width() / 2, image->height() / 4, font, paint);
21 if (dContext) {
22 const char* msg = image->isValid(dContext) ? "is valid on GPU"
23 : "not valid on GPU";
24 canvas->drawString(msg, 20, image->height() * 5 / 8, font, paint);
25 }
26
27 const char* msg = image->isValid(nullptr) ? "is valid on CPU" : "not valid on CPU";
28
29 canvas->drawString(msg, 20, image->height() * 7 / 8, font, paint);
30 };
31 sk_sp<SkImage> bitmapImage(source.asImage());
32 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(dContext, backEndTexture,
33 kTopLeft_GrSurfaceOrigin,
34 kRGBA_8888_SkColorType,
35 kOpaque_SkAlphaType, nullptr));
36 drawImage(image, "image");
37 canvas->translate(image->width(), 0);
38 drawImage(bitmapImage, "source");
39 canvas->translate(-image->width(), image->height());
40 drawImage(textureImage, "backEndTexture");
41 }
42 } // END FIDDLE
43