Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 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 | |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 8 | #include "tools/gpu/ProxyUtils.h" |
| 9 | |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrBackendSurface.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrDirectContext.h" |
Brian Salomon | e666254 | 2021-02-23 10:45:39 -0500 | [diff] [blame] | 13 | #include "include/private/GrImageContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrDrawingManager.h" |
| 16 | #include "src/gpu/GrGpu.h" |
Brian Salomon | e666254 | 2021-02-23 10:45:39 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrImageContextPriv.h" |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrPixmap.h" |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrProxyProvider.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrSurfaceContext.h" |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 22 | #include "src/gpu/SkGr.h" |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 23 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Brian Salomon | e666254 | 2021-02-23 10:45:39 -0500 | [diff] [blame] | 24 | #include "src/image/SkImage_Base.h" |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 25 | |
| 26 | namespace sk_gpu_test { |
| 27 | |
Brian Salomon | e666254 | 2021-02-23 10:45:39 -0500 | [diff] [blame] | 28 | GrTextureProxy* 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 Salomon | 652124c | 2020-08-12 14:06:50 -0400 | [diff] [blame] | 52 | GrSurfaceProxyView MakeTextureProxyViewFromData(GrDirectContext* dContext, |
| 53 | GrRenderable renderable, |
| 54 | GrSurfaceOrigin origin, |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 55 | GrCPixmap pixmap) { |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 56 | if (dContext->abandoned()) { |
Brian Salomon | 652124c | 2020-08-12 14:06:50 -0400 | [diff] [blame] | 57 | return {}; |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 60 | const GrCaps* caps = dContext->priv().caps(); |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 61 | |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 62 | const GrBackendFormat format = caps->getDefaultBackendFormat(pixmap.colorType(), renderable); |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 63 | if (!format.isValid()) { |
Brian Salomon | 652124c | 2020-08-12 14:06:50 -0400 | [diff] [blame] | 64 | return {}; |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 65 | } |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 66 | GrSwizzle swizzle = caps->getReadSwizzle(format, pixmap.colorType()); |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 67 | |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 68 | sk_sp<GrTextureProxy> proxy; |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 69 | 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 Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 76 | GrProtected::kNo); |
Brian Salomon | 4eda710 | 2019-10-21 15:04:52 -0400 | [diff] [blame] | 77 | if (!proxy) { |
Brian Salomon | 652124c | 2020-08-12 14:06:50 -0400 | [diff] [blame] | 78 | return {}; |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 79 | } |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 80 | GrSurfaceProxyView view(proxy, origin, swizzle); |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 81 | auto sContext = GrSurfaceContext::Make(dContext, std::move(view), pixmap.colorInfo()); |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 82 | if (!sContext) { |
Brian Salomon | 652124c | 2020-08-12 14:06:50 -0400 | [diff] [blame] | 83 | return {}; |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 84 | } |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 85 | if (!sContext->writePixels(dContext, pixmap, {0, 0})) { |
Brian Salomon | 652124c | 2020-08-12 14:06:50 -0400 | [diff] [blame] | 86 | return {}; |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 87 | } |
Brian Salomon | 652124c | 2020-08-12 14:06:50 -0400 | [diff] [blame] | 88 | return sContext->readSurfaceView(); |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 91 | GrProgramInfo* CreateProgramInfo(const GrCaps* caps, |
| 92 | SkArenaAlloc* arena, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 93 | const GrSurfaceProxyView& writeView, |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 94 | GrAppliedClip&& appliedClip, |
| 95 | const GrXferProcessor::DstProxyView& dstProxyView, |
| 96 | GrGeometryProcessor* geomProc, |
| 97 | SkBlendMode blendMode, |
| 98 | GrPrimitiveType primitiveType, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 99 | GrXferBarrierFlags renderPassXferBarriers, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 100 | GrLoadOp colorLoadOp, |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 101 | GrPipeline::InputFlags flags, |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 102 | const GrUserStencilSettings* stencilSettings) { |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 103 | |
| 104 | GrProcessorSet processors = GrProcessorSet(blendMode); |
| 105 | |
| 106 | SkPMColor4f analysisColor = { 0, 0, 0, 1 }; // opaque black |
| 107 | |
| 108 | SkDEBUGCODE(auto analysis =) processors.finalize(analysisColor, |
Greg Daniel | f6d60d3 | 2020-01-08 13:39:16 -0500 | [diff] [blame] | 109 | GrProcessorAnalysisCoverage::kSingleChannel, |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 110 | &appliedClip, stencilSettings, *caps, |
| 111 | GrClampType::kAuto, &analysisColor); |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 112 | SkASSERT(!analysis.requiresDstTexture()); |
| 113 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 114 | return GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView, |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 115 | std::move(appliedClip), dstProxyView, |
| 116 | geomProc, std::move(processors), |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 117 | primitiveType, renderPassXferBarriers, |
| 118 | colorLoadOp, flags, stencilSettings); |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 122 | } // namespace sk_gpu_test |