xref: /aosp_15_r20/external/skia/tools/GpuToolUtils.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 Google LLC
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 #ifndef GpuToolUtils_DEFINED
9 #define GpuToolUtils_DEFINED
10 
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkRefCnt.h"
14 
15 #if defined(SK_GANESH)
16 #include "include/gpu/ganesh/GrDirectContext.h"
17 #include "include/gpu/ganesh/GrRecordingContext.h"
18 #include "include/gpu/ganesh/SkImageGanesh.h"
19 #include "src/gpu/ganesh/GrCaps.h"
20 #include "src/gpu/ganesh/GrDirectContextPriv.h"
21 #endif
22 
23 #if defined(SK_GRAPHITE)
24 #include "include/gpu/graphite/Image.h"
25 #include "include/gpu/graphite/Recorder.h"
26 #endif
27 
28 namespace ToolUtils {
29 
30 // We do not put this in a .cpp file because otherwise the defines from the client
31 // (e.g. dm) won't be seen if this is compiled into a "core" tools library.
MakeTextureImage(SkCanvas * canvas,sk_sp<SkImage> orig)32 inline sk_sp<SkImage> MakeTextureImage(SkCanvas* canvas, sk_sp<SkImage> orig) {
33     if (!orig) {
34         return nullptr;
35     }
36 
37 #if defined(SK_GANESH)
38     if (canvas->recordingContext() && canvas->recordingContext()->asDirectContext()) {
39         GrDirectContext* dContext = canvas->recordingContext()->asDirectContext();
40         const GrCaps* caps = dContext->priv().caps();
41 
42         if (orig->width() >= caps->maxTextureSize() || orig->height() >= caps->maxTextureSize()) {
43             // Ganesh is able to tile large SkImage draws. Always forcing SkImages to be uploaded
44             // prevents this feature from being tested by our tools. For now, leave excessively
45             // large SkImages as bitmaps.
46             return orig;
47         }
48 
49         return SkImages::TextureFromImage(dContext, orig);
50     }
51 #endif
52 #if defined(SK_GRAPHITE)
53     if (canvas->recorder()) {
54         return SkImages::TextureFromImage(canvas->recorder(), orig, {/*fMipmapped=*/false});
55     }
56 #endif
57     return orig;
58 }
59 }  // namespace ToolUtils
60 
61 #endif
62