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