xref: /aosp_15_r20/external/skia/docs/examples/Image_refEncodedData.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 REG_FIDDLE(Image_refEncodedData, 256, 256, false, 3) {
draw(SkCanvas * canvas)5 void draw(SkCanvas* canvas) {
6     auto dContext = GrAsDirectContext(canvas->recordingContext());
7     if (!dContext) {
8         return;
9     }
10 
11     struct {
12         const char* name;
13         sk_sp<SkImage> image;
14     } tests[] = {{"image", image},
15                  {"bitmap", source.asImage()},
16                  {"texture",
17                   SkImages::BorrowTextureFrom(dContext,
18                                               backEndTexture,
19                                               kTopLeft_GrSurfaceOrigin,
20                                               kRGBA_8888_SkColorType,
21                                               kOpaque_SkAlphaType,
22                                               nullptr)}};
23     SkString string;
24     SkPaint paint;
25     SkFont font = SkFont(fontMgr->matchFamilyStyle(nullptr, {}));
26 
27     for (const auto& test : tests ) {
28         if (!test.image) {
29             string.printf("no %s", test.name);
30         } else {
31             string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
32         }
33         canvas->drawString(string, 10, 20, font, paint);
34         canvas->translate(0, 20);
35     }
36 }
37 }  // END FIDDLE
38