xref: /aosp_15_r20/external/skia/tools/gpu/ProxyUtils.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2018 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 #include "tools/gpu/ProxyUtils.h"
9 
10 #include "include/core/SkColor.h"
11 #include "include/gpu/ganesh/GrBackendSurface.h"
12 #include "include/gpu/ganesh/GrDirectContext.h"
13 #include "include/private/gpu/ganesh/GrImageContext.h"
14 #include "src/gpu/ganesh/GrDirectContextPriv.h"
15 #include "src/gpu/ganesh/GrDrawingManager.h"
16 #include "src/gpu/ganesh/GrGpu.h"
17 #include "src/gpu/ganesh/GrImageContextPriv.h"
18 #include "src/gpu/ganesh/GrPixmap.h"
19 #include "src/gpu/ganesh/GrProgramInfo.h"
20 #include "src/gpu/ganesh/GrProxyProvider.h"
21 #include "src/gpu/ganesh/SkGr.h"
22 #include "src/gpu/ganesh/SurfaceContext.h"
23 #include "src/gpu/ganesh/image/GrImageUtils.h"
24 #include "src/image/SkImage_Base.h"
25 
26 #if defined(SK_GANESH)
27 #include "src/gpu/ganesh/ops/GrSimpleMeshDrawOpHelper.h"
28 #endif
29 
30 namespace sk_gpu_test {
31 
GetTextureImageProxy(SkImage * image,GrRecordingContext * rContext)32 GrTextureProxy* GetTextureImageProxy(SkImage* image, GrRecordingContext* rContext) {
33     if (!as_IB(image)->isGaneshBacked() || as_IB(image)->isYUVA()) {
34         return nullptr;
35     }
36     if (!rContext) {
37         // If the image is backed by a recording context we'll use that.
38         GrImageContext* iContext = as_IB(image)->context();
39         SkASSERT(iContext);
40         rContext = iContext->priv().asRecordingContext();
41         if (!rContext) {
42             return nullptr;
43         }
44     }
45     auto [view, ct] = skgpu::ganesh::AsView(rContext, image, skgpu::Mipmapped::kNo);
46     if (!view) {
47         // With the above checks we expect this to succeed unless there is a context mismatch.
48         SkASSERT(!image->isValid(rContext));
49         return nullptr;
50     }
51     GrSurfaceProxy* proxy = view.proxy();
52     SkASSERT(proxy->asTextureProxy());
53     return proxy->asTextureProxy();
54 }
55 
MakeTextureProxyViewFromData(GrDirectContext * dContext,GrRenderable renderable,GrSurfaceOrigin origin,GrCPixmap pixmap)56 GrSurfaceProxyView MakeTextureProxyViewFromData(GrDirectContext* dContext,
57                                                 GrRenderable renderable,
58                                                 GrSurfaceOrigin origin,
59                                                 GrCPixmap pixmap) {
60     if (dContext->abandoned()) {
61         return {};
62     }
63 
64     const GrCaps* caps = dContext->priv().caps();
65 
66     const GrBackendFormat format = caps->getDefaultBackendFormat(pixmap.colorType(), renderable);
67     if (!format.isValid()) {
68         return {};
69     }
70     skgpu::Swizzle swizzle = caps->getReadSwizzle(format, pixmap.colorType());
71 
72     sk_sp<GrTextureProxy> proxy;
73     proxy = dContext->priv().proxyProvider()->createProxy(format,
74                                                           pixmap.dimensions(),
75                                                           renderable,
76                                                           /*sample count*/ 1,
77                                                           skgpu::Mipmapped::kNo,
78                                                           SkBackingFit::kExact,
79                                                           skgpu::Budgeted::kYes,
80                                                           GrProtected::kNo,
81                                                           /*label=*/"TextureProxyViewFromData");
82     if (!proxy) {
83         return {};
84     }
85     GrSurfaceProxyView view(proxy, origin, swizzle);
86     auto sContext = dContext->priv().makeSC(std::move(view), pixmap.colorInfo());
87     if (!sContext) {
88         return {};
89     }
90     if (!sContext->writePixels(dContext, pixmap, {0, 0})) {
91         return {};
92     }
93     return sContext->readSurfaceView();
94 }
95 
96 #if defined(SK_GANESH)
CreateProgramInfo(const GrCaps * caps,SkArenaAlloc * arena,const GrSurfaceProxyView & writeView,bool usesMSAASurface,GrAppliedClip && appliedClip,const GrDstProxyView & dstProxyView,GrGeometryProcessor * geomProc,SkBlendMode blendMode,GrPrimitiveType primitiveType,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp,GrPipeline::InputFlags flags,const GrUserStencilSettings * stencilSettings)97 GrProgramInfo* CreateProgramInfo(const GrCaps* caps,
98                                  SkArenaAlloc* arena,
99                                  const GrSurfaceProxyView& writeView,
100                                  bool usesMSAASurface,
101                                  GrAppliedClip&& appliedClip,
102                                  const GrDstProxyView& dstProxyView,
103                                  GrGeometryProcessor* geomProc,
104                                  SkBlendMode blendMode,
105                                  GrPrimitiveType primitiveType,
106                                  GrXferBarrierFlags renderPassXferBarriers,
107                                  GrLoadOp colorLoadOp,
108                                  GrPipeline::InputFlags flags,
109                                  const GrUserStencilSettings* stencilSettings) {
110 
111     GrProcessorSet processors = GrProcessorSet(blendMode);
112 
113     SkPMColor4f analysisColor = { 0, 0, 0, 1 }; // opaque black
114 
115     SkDEBUGCODE(auto analysis =) processors.finalize(analysisColor,
116                                                      GrProcessorAnalysisCoverage::kSingleChannel,
117                                                      &appliedClip, stencilSettings, *caps,
118                                                      GrClampType::kAuto, &analysisColor);
119     SkASSERT(!analysis.requiresDstTexture());
120 
121     return GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView, usesMSAASurface,
122                                                        std::move(appliedClip), dstProxyView,
123                                                        geomProc, std::move(processors),
124                                                        primitiveType, renderPassXferBarriers,
125                                                        colorLoadOp, flags, stencilSettings);
126 }
127 #endif // defined(SK_GANESH)
128 
129 }  // namespace sk_gpu_test
130