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 "include/core/SkColor.h"
9 #include "include/gpu/GrBackendSurface.h"
10 #include "src/gpu/GrContextPriv.h"
11 #include "src/gpu/GrDrawingManager.h"
12 #include "src/gpu/GrGpu.h"
13 #include "src/gpu/GrImageInfo.h"
14 #include "src/gpu/GrProgramInfo.h"
15 #include "src/gpu/GrProxyProvider.h"
16 #include "src/gpu/SkGr.h"
17 #include "tools/gpu/ProxyUtils.h"
18
19 namespace sk_gpu_test {
20
MakeTextureProxyFromData(GrContext * context,GrRenderable renderable,GrSurfaceOrigin origin,const GrImageInfo & imageInfo,const void * data,size_t rowBytes)21 sk_sp<GrTextureProxy> MakeTextureProxyFromData(GrContext* context,
22 GrRenderable renderable,
23 GrSurfaceOrigin origin,
24 const GrImageInfo& imageInfo,
25 const void* data,
26 size_t rowBytes) {
27 if (context->priv().abandoned()) {
28 return nullptr;
29 }
30
31 const GrCaps* caps = context->priv().caps();
32
33 const GrBackendFormat format = caps->getDefaultBackendFormat(imageInfo.colorType(), renderable);
34 if (!format.isValid()) {
35 return nullptr;
36 }
37 GrSwizzle swizzle = caps->getReadSwizzle(format, imageInfo.colorType());
38
39 sk_sp<GrTextureProxy> proxy;
40 proxy = context->priv().proxyProvider()->createProxy(
41 format, imageInfo.dimensions(), swizzle, renderable, 1, GrMipMapped::kNo,
42 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
43 if (!proxy) {
44 return nullptr;
45 }
46 GrSurfaceProxyView view(proxy, origin, swizzle);
47 auto sContext = GrSurfaceContext::Make(context, std::move(view), imageInfo.colorType(),
48 imageInfo.alphaType(), imageInfo.refColorSpace());
49 if (!sContext) {
50 return nullptr;
51 }
52 if (!sContext->writePixels(imageInfo, data, rowBytes, {0, 0}, context)) {
53 return nullptr;
54 }
55 return proxy;
56 }
57
CreateProgramInfo(const GrCaps * caps,SkArenaAlloc * arena,const GrSurfaceProxyView * dstView,GrAppliedClip && appliedClip,const GrXferProcessor::DstProxyView & dstProxyView,GrGeometryProcessor * geomProc,SkBlendMode blendMode,GrPrimitiveType primitiveType,GrPipeline::InputFlags flags,const GrUserStencilSettings * stencil)58 GrProgramInfo* CreateProgramInfo(const GrCaps* caps,
59 SkArenaAlloc* arena,
60 const GrSurfaceProxyView* dstView,
61 GrAppliedClip&& appliedClip,
62 const GrXferProcessor::DstProxyView& dstProxyView,
63 GrGeometryProcessor* geomProc,
64 SkBlendMode blendMode,
65 GrPrimitiveType primitiveType,
66 GrPipeline::InputFlags flags,
67 const GrUserStencilSettings* stencil) {
68
69 GrPipeline::InitArgs initArgs;
70 initArgs.fInputFlags = flags;
71 initArgs.fUserStencil = stencil;
72 initArgs.fCaps = caps;
73 initArgs.fDstProxyView = dstProxyView;
74 initArgs.fOutputSwizzle = dstView->swizzle();
75
76 GrPipeline::FixedDynamicState* fixedDynamicState = nullptr;
77
78 if (appliedClip.scissorState().enabled()) {
79 fixedDynamicState = arena->make<GrPipeline::FixedDynamicState>(
80 appliedClip.scissorState().rect());
81 }
82
83 GrProcessorSet processors = GrProcessorSet(blendMode);
84
85 SkPMColor4f analysisColor = { 0, 0, 0, 1 }; // opaque black
86
87 SkDEBUGCODE(auto analysis =) processors.finalize(analysisColor,
88 GrProcessorAnalysisCoverage::kSingleChannel,
89 &appliedClip, stencil, false,
90 *caps, GrClampType::kAuto, &analysisColor);
91 SkASSERT(!analysis.requiresDstTexture());
92
93 GrPipeline* pipeline = arena->make<GrPipeline>(initArgs,
94 std::move(processors),
95 std::move(appliedClip));
96
97 GrRenderTargetProxy* dstProxy = dstView->asRenderTargetProxy();
98 return arena->make<GrProgramInfo>(dstProxy->numSamples(),
99 dstProxy->numStencilSamples(),
100 dstProxy->backendFormat(),
101 dstView->origin(),
102 pipeline,
103 geomProc,
104 fixedDynamicState,
105 nullptr, 0,
106 primitiveType);
107 }
108
109
110 } // namespace sk_gpu_test
111