xref: /aosp_15_r20/external/skia/tools/fiddle/draw.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 // This is an example of the translation unit that needs to be
9 // assembled by the fiddler program to compile into a fiddle: an
10 // implementation of the GetDrawOptions() and draw() functions.
11 
12 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
13 #include "tools/fiddle/fiddle_main.h"
14 #include "tools/gpu/ManagedBackendTexture.h"
15 
GetDrawOptions()16 DrawOptions GetDrawOptions() {
17     // path *should* be absolute.
18     static const char path[] = "resources/images/color_wheel.png";
19     return DrawOptions(256,
20                        256,
21                        true,
22                        true,
23                        true,
24                        true,
25                        true,
26                        false,
27                        false,
28                        path,
29                        skgpu::Mipmapped::kYes,
30                        64,
31                        64,
32                        0,
33                        skgpu::Mipmapped::kYes);
34 }
draw(SkCanvas * canvas)35 void draw(SkCanvas* canvas) {
36     canvas->clear(SK_ColorWHITE);
37     SkMatrix matrix;
38     matrix.setScale(0.75f, 0.75f);
39     matrix.preRotate(frame * 30.0f * duration); // If an animation, rotate at 30 deg/s.
40     SkPaint paint;
41     paint.setShader(image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
42                                       SkSamplingOptions(), matrix));
43     canvas->drawPaint(paint);
44     SkDebugf("This is text output: %d", 2);
45 
46     if (auto dContext = GrAsDirectContext(canvas->recordingContext())) {
47         sk_sp<SkImage> tmp = SkImages::BorrowTextureFrom(dContext,
48                                                          backEndTexture,
49                                                          kTopLeft_GrSurfaceOrigin,
50                                                          kRGBA_8888_SkColorType,
51                                                          kOpaque_SkAlphaType,
52                                                          nullptr);
53 
54         constexpr int kSampleCnt = 0;
55         sk_sp<SkSurface> tmp2 = SkSurfaces::WrapBackendTexture(dContext,
56                                                                backEndTextureRenderTarget,
57                                                                kTopLeft_GrSurfaceOrigin,
58                                                                kSampleCnt,
59                                                                kRGBA_8888_SkColorType,
60                                                                nullptr,
61                                                                nullptr);
62 
63         // Note: this surface should only be renderable (i.e., not textureable)
64         sk_sp<SkSurface> tmp3 = SkSurfaces::WrapBackendRenderTarget(dContext,
65                                                                     backEndRenderTarget,
66                                                                     kTopLeft_GrSurfaceOrigin,
67                                                                     kRGBA_8888_SkColorType,
68                                                                     nullptr,
69                                                                     nullptr);
70     }
71 }
72