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