• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/GrImageContext.h"
14 #include "src/gpu/GrDirectContextPriv.h"
15 #include "src/gpu/GrDrawingManager.h"
16 #include "src/gpu/GrGpu.h"
17 #include "src/gpu/GrImageContextPriv.h"
18 #include "src/gpu/GrPixmap.h"
19 #include "src/gpu/GrProgramInfo.h"
20 #include "src/gpu/GrProxyProvider.h"
21 #include "src/gpu/SkGr.h"
22 #include "src/gpu/SurfaceContext.h"
23 #include "src/image/SkImage_Base.h"
24 
25 #if SK_GPU_V1
26 #include "src/gpu/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 (!image->isTextureBacked() || 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     GrSwizzle 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                                                           SkBudgeted::kYes,
79                                                           GrProtected::kNo);
80     if (!proxy) {
81         return {};
82     }
83     GrSurfaceProxyView view(proxy, origin, swizzle);
84     auto sContext = dContext->priv().makeSC(std::move(view), pixmap.colorInfo());
85     if (!sContext) {
86         return {};
87     }
88     if (!sContext->writePixels(dContext, pixmap, {0, 0})) {
89         return {};
90     }
91     return sContext->readSurfaceView();
92 }
93 
94 #if SK_GPU_V1
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)95 GrProgramInfo* CreateProgramInfo(const GrCaps* caps,
96                                  SkArenaAlloc* arena,
97                                  const GrSurfaceProxyView& writeView,
98                                  bool usesMSAASurface,
99                                  GrAppliedClip&& appliedClip,
100                                  const GrDstProxyView& dstProxyView,
101                                  GrGeometryProcessor* geomProc,
102                                  SkBlendMode blendMode,
103                                  GrPrimitiveType primitiveType,
104                                  GrXferBarrierFlags renderPassXferBarriers,
105                                  GrLoadOp colorLoadOp,
106                                  GrPipeline::InputFlags flags,
107                                  const GrUserStencilSettings* stencilSettings) {
108 
109     GrProcessorSet processors = GrProcessorSet(blendMode);
110 
111     SkPMColor4f analysisColor = { 0, 0, 0, 1 }; // opaque black
112 
113     SkDEBUGCODE(auto analysis =) processors.finalize(analysisColor,
114                                                      GrProcessorAnalysisCoverage::kSingleChannel,
115                                                      &appliedClip, stencilSettings, *caps,
116                                                      GrClampType::kAuto, &analysisColor);
117     SkASSERT(!analysis.requiresDstTexture());
118 
119     return GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView, usesMSAASurface,
120                                                        std::move(appliedClip), dstProxyView,
121                                                        geomProc, std::move(processors),
122                                                        primitiveType, renderPassXferBarriers,
123                                                        colorLoadOp, flags, stencilSettings);
124 }
125 #endif // SK_GPU_V1
126 
127 }  // namespace sk_gpu_test
128