blob: 756eaa15c4df9283113e656bfc300458a7b6e857 [file] [log] [blame]
Brian Salomon58389b92018-03-07 13:01:25 -05001/*
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
Brian Salomondd4087d2020-12-23 20:36:44 -05008#include "tools/gpu/ProxyUtils.h"
9
Robert Phillips34cea002019-11-21 16:02:34 -050010#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrBackendSurface.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040012#include "include/gpu/GrDirectContext.h"
Brian Salomone6662542021-02-23 10:45:39 -050013#include "include/private/GrImageContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -040014#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrDrawingManager.h"
16#include "src/gpu/GrGpu.h"
Brian Salomone6662542021-02-23 10:45:39 -050017#include "src/gpu/GrImageContextPriv.h"
Brian Salomondd4087d2020-12-23 20:36:44 -050018#include "src/gpu/GrPixmap.h"
Robert Phillips34cea002019-11-21 16:02:34 -050019#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrProxyProvider.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040021#include "src/gpu/GrSurfaceContext.h"
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040022#include "src/gpu/SkGr.h"
Robert Phillipsac6156c2020-02-28 16:02:40 -050023#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Brian Salomone6662542021-02-23 10:45:39 -050024#include "src/image/SkImage_Base.h"
Brian Salomon58389b92018-03-07 13:01:25 -050025
26namespace sk_gpu_test {
27
Brian Salomone6662542021-02-23 10:45:39 -050028GrTextureProxy* GetTextureImageProxy(SkImage* image, GrRecordingContext* rContext) {
29 if (!image->isTextureBacked() || as_IB(image)->isYUVA()) {
30 return nullptr;
31 }
32 if (!rContext) {
33 // If the image is backed by a recording context we'll use that.
34 GrImageContext* iContext = as_IB(image)->context();
35 SkASSERT(iContext);
36 rContext = iContext->priv().asRecordingContext();
37 if (!rContext) {
38 return nullptr;
39 }
40 }
41 auto [view, ct] = as_IB(image)->asView(rContext, GrMipmapped::kNo);
42 if (!view) {
43 // With the above checks we expect this to succeed unless there is a context mismatch.
44 SkASSERT(!image->isValid(rContext));
45 return nullptr;
46 }
47 GrSurfaceProxy* proxy = view.proxy();
48 SkASSERT(proxy->asTextureProxy());
49 return proxy->asTextureProxy();
50}
51
Brian Salomon652124c2020-08-12 14:06:50 -040052GrSurfaceProxyView MakeTextureProxyViewFromData(GrDirectContext* dContext,
53 GrRenderable renderable,
54 GrSurfaceOrigin origin,
Brian Salomon5392c942021-03-30 16:14:37 -040055 GrCPixmap pixmap) {
Adlai Hollerc95b5892020-08-11 12:02:22 -040056 if (dContext->abandoned()) {
Brian Salomon652124c2020-08-12 14:06:50 -040057 return {};
Robert Phillips44333c52020-06-30 13:28:00 -040058 }
59
Adlai Hollerc95b5892020-08-11 12:02:22 -040060 const GrCaps* caps = dContext->priv().caps();
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040061
Brian Salomondd4087d2020-12-23 20:36:44 -050062 const GrBackendFormat format = caps->getDefaultBackendFormat(pixmap.colorType(), renderable);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040063 if (!format.isValid()) {
Brian Salomon652124c2020-08-12 14:06:50 -040064 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040065 }
Brian Salomondd4087d2020-12-23 20:36:44 -050066 GrSwizzle swizzle = caps->getReadSwizzle(format, pixmap.colorType());
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040067
Brian Salomon02bd2952018-03-07 15:20:21 -050068 sk_sp<GrTextureProxy> proxy;
Brian Salomondd4087d2020-12-23 20:36:44 -050069 proxy = dContext->priv().proxyProvider()->createProxy(format,
70 pixmap.dimensions(),
71 renderable,
72 /*sample count*/ 1,
73 GrMipmapped::kNo,
74 SkBackingFit::kExact,
75 SkBudgeted::kYes,
Adlai Hollerc95b5892020-08-11 12:02:22 -040076 GrProtected::kNo);
Brian Salomon4eda7102019-10-21 15:04:52 -040077 if (!proxy) {
Brian Salomon652124c2020-08-12 14:06:50 -040078 return {};
Brian Salomon58389b92018-03-07 13:01:25 -050079 }
Greg Daniel3912a4b2020-01-14 09:56:04 -050080 GrSurfaceProxyView view(proxy, origin, swizzle);
Brian Salomondd4087d2020-12-23 20:36:44 -050081 auto sContext = GrSurfaceContext::Make(dContext, std::move(view), pixmap.colorInfo());
Brian Salomon58389b92018-03-07 13:01:25 -050082 if (!sContext) {
Brian Salomon652124c2020-08-12 14:06:50 -040083 return {};
Brian Salomon58389b92018-03-07 13:01:25 -050084 }
Brian Salomondd4087d2020-12-23 20:36:44 -050085 if (!sContext->writePixels(dContext, pixmap, {0, 0})) {
Brian Salomon652124c2020-08-12 14:06:50 -040086 return {};
Brian Salomon58389b92018-03-07 13:01:25 -050087 }
Brian Salomon652124c2020-08-12 14:06:50 -040088 return sContext->readSurfaceView();
Brian Salomon58389b92018-03-07 13:01:25 -050089}
90
Robert Phillips34cea002019-11-21 16:02:34 -050091GrProgramInfo* CreateProgramInfo(const GrCaps* caps,
92 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -050093 const GrSurfaceProxyView& writeView,
Robert Phillips34cea002019-11-21 16:02:34 -050094 GrAppliedClip&& appliedClip,
95 const GrXferProcessor::DstProxyView& dstProxyView,
96 GrGeometryProcessor* geomProc,
97 SkBlendMode blendMode,
98 GrPrimitiveType primitiveType,
Greg Danield358cbe2020-09-11 09:33:54 -040099 GrXferBarrierFlags renderPassXferBarriers,
Greg Daniel42dbca52020-11-20 10:22:43 -0500100 GrLoadOp colorLoadOp,
Robert Phillips34cea002019-11-21 16:02:34 -0500101 GrPipeline::InputFlags flags,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500102 const GrUserStencilSettings* stencilSettings) {
Robert Phillips34cea002019-11-21 16:02:34 -0500103
104 GrProcessorSet processors = GrProcessorSet(blendMode);
105
106 SkPMColor4f analysisColor = { 0, 0, 0, 1 }; // opaque black
107
108 SkDEBUGCODE(auto analysis =) processors.finalize(analysisColor,
Greg Danielf6d60d32020-01-08 13:39:16 -0500109 GrProcessorAnalysisCoverage::kSingleChannel,
Chris Dalton57ab06c2021-04-22 12:57:28 -0600110 &appliedClip, stencilSettings, *caps,
111 GrClampType::kAuto, &analysisColor);
Robert Phillips34cea002019-11-21 16:02:34 -0500112 SkASSERT(!analysis.requiresDstTexture());
113
Brian Salomon8afde5f2020-04-01 16:22:00 -0400114 return GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500115 std::move(appliedClip), dstProxyView,
116 geomProc, std::move(processors),
Greg Daniel42dbca52020-11-20 10:22:43 -0500117 primitiveType, renderPassXferBarriers,
118 colorLoadOp, flags, stencilSettings);
Robert Phillips34cea002019-11-21 16:02:34 -0500119}
120
121
Brian Salomon58389b92018-03-07 13:01:25 -0500122} // namespace sk_gpu_test