Debian Codegraph @

src/gpu/ganesh/image/SkImage_GaneshYUVA.cpp

raw
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 "src/gpu/ganesh/image/SkImage_GaneshYUVA.h"
9
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkSurface.h"
14 #include "include/core/SkYUVAInfo.h"
15 #include "include/gpu/GpuTypes.h"
16 #include "include/gpu/ganesh/GrBackendSurface.h" // IWYU pragma: keep
17 #include "include/gpu/ganesh/GrDirectContext.h"
18 #include "include/gpu/ganesh/GrRecordingContext.h"
19 #include "include/gpu/ganesh/GrTypes.h"
20 #include "include/private/base/SkAssert.h"
21 #include "include/private/base/SkDebug.h"
22 #include "include/private/gpu/ganesh/GrImageContext.h"
23 #include "src/core/SkSamplingPriv.h"
24 #include "src/gpu/SkBackingFit.h"
25 #include "src/gpu/Swizzle.h"
26 #include "src/gpu/ganesh/GrCaps.h"
27 #include "src/gpu/ganesh/GrColorInfo.h"
28 #include "src/gpu/ganesh/GrColorSpaceXform.h"
29 #include "src/gpu/ganesh/GrDirectContextPriv.h"
30 #include "src/gpu/ganesh/GrFragmentProcessor.h"
31 #include "src/gpu/ganesh/GrImageContextPriv.h"
32 #include "src/gpu/ganesh/GrImageInfo.h"
33 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
34 #include "src/gpu/ganesh/GrSamplerState.h"
35 #include "src/gpu/ganesh/GrSurfaceProxy.h"
36 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
37 #include "src/gpu/ganesh/GrTextureProxy.h"
38 #include "src/gpu/ganesh/SkGr.h"
39 #include "src/gpu/ganesh/SurfaceDrawContext.h"
40 #include "src/gpu/ganesh/SurfaceFillContext.h"
41 #include "src/gpu/ganesh/effects/GrBicubicEffect.h"
42 #include "src/gpu/ganesh/effects/GrYUVtoRGBEffect.h"
43 #include "src/image/SkImage_Base.h"
44
45 #include <utility>
46
47 enum class SkTileMode;
48 struct SkRect;
49
50 SkImage_GaneshYUVA::SkImage_GaneshYUVA(sk_sp<GrImageContext> context,
51 uint32_t uniqueID,
52 GrYUVATextureProxies proxies,
53 sk_sp<SkColorSpace> imageColorSpace)
54 : INHERITED(std::move(context),
55 SkImageInfo::Make(proxies.yuvaInfo().dimensions(),
56 kAssumedColorType,
57 // If an alpha channel is present we always use kPremul. This
58 // is because, although the planar data is always un-premul,
59 // the final interleaved RGBA sample produced in the shader
60 // is premul (and similar if flattened via asView).
61 proxies.yuvaInfo().hasAlpha() ? kPremul_SkAlphaType
62 : kOpaque_SkAlphaType,
63 std::move(imageColorSpace)),
64 uniqueID)
65 , fYUVAProxies(std::move(proxies)) {
66 // The caller should have checked this, just verifying.
67 SkASSERT(fYUVAProxies.isValid());
68 }
69
70 // For onMakeColorTypeAndColorSpace() / onReinterpretColorSpace()
71 SkImage_GaneshYUVA::SkImage_GaneshYUVA(sk_sp<GrImageContext> context,
72 const SkImage_GaneshYUVA* image,
73 sk_sp<SkColorSpace> targetCS,
74 ColorSpaceMode csMode)
75 : INHERITED(std::move(context),
76 image->imageInfo().makeColorSpace(std::move(targetCS)),
77 kNeedNewImageUniqueID)
78 , fYUVAProxies(image->fYUVAProxies)
79 // If we're *reinterpreting* in a new color space, leave fFromColorSpace null.
80 // If we're *converting* to a new color space, it must be non-null, so turn null into sRGB.
81 , fFromColorSpace(csMode == ColorSpaceMode::kReinterpret
82 ? nullptr
83 : (image->colorSpace() ? image->refColorSpace()
84 : SkColorSpace::MakeSRGB())) {}
85
86 bool SkImage_GaneshYUVA::setupMipmapsForPlanes(GrRecordingContext* context) const {
87 if (!context || !fContext->priv().matches(context)) {
88 return false;
89 }
90 if (!context->priv().caps()->mipmapSupport()) {
91 // We succeed in this case by doing nothing.
92 return true;
93 }
94 int n = fYUVAProxies.yuvaInfo().numPlanes();
95 sk_sp<GrSurfaceProxy> newProxies[4];
96 for (int i = 0; i < n; ++i) {
97 auto* t = fYUVAProxies.proxy(i)->asTextureProxy();
98 if (t->mipmapped() == skgpu::Mipmapped::kNo && (t->width() > 1 || t->height() > 1)) {
99 auto newView = GrCopyBaseMipMapToView(context, fYUVAProxies.makeView(i));
100 if (!newView) {
101 return false;
102 }
103 SkASSERT(newView.swizzle() == fYUVAProxies.makeView(i).swizzle());
104 newProxies[i] = newView.detachProxy();
105 } else {
106 newProxies[i] = fYUVAProxies.refProxy(i);
107 }
108 }
109 fYUVAProxies =
110 GrYUVATextureProxies(fYUVAProxies.yuvaInfo(), newProxies, fYUVAProxies.textureOrigin());
111 SkASSERT(fYUVAProxies.isValid());
112 return true;
113 }
114
115 //////////////////////////////////////////////////////////////////////////////////////////////////
116
117 GrSemaphoresSubmitted SkImage_GaneshYUVA::flush(GrDirectContext* dContext,
118 const GrFlushInfo& info) const {
119 if (!fContext->priv().matches(dContext) || dContext->abandoned()) {
120 if (info.fSubmittedProc) {
121 info.fSubmittedProc(info.fSubmittedContext, false);
122 }
123 if (info.fFinishedProc) {
124 info.fFinishedProc(info.fFinishedContext);
125 }
126 return GrSemaphoresSubmitted::kNo;
127 }
128
129 GrSurfaceProxy* proxies[SkYUVAInfo::kMaxPlanes] = {};
130 size_t numProxies = fYUVAProxies.numPlanes();
131 for (size_t i = 0; i < numProxies; ++i) {
132 proxies[i] = fYUVAProxies.proxy(i);
133 }
134 return dContext->priv().flushSurfaces(
135 {proxies, numProxies}, SkSurfaces::BackendSurfaceAccess::kNoAccess, info);
136 }
137
138 bool SkImage_GaneshYUVA::onHasMipmaps() const {
139 return fYUVAProxies.mipmapped() == skgpu::Mipmapped::kYes;
140 }
141
142 bool SkImage_GaneshYUVA::onIsProtected() const {
143 skgpu::Protected isProtected = fYUVAProxies.proxy(0)->isProtected();
144
145 #if defined(SK_DEBUG)
146 for (int i = 1; i < fYUVAProxies.numPlanes(); ++i) {
147 SkASSERT(isProtected == fYUVAProxies.proxy(i)->isProtected());
148 }
149 #endif
150
151 return isProtected == skgpu::Protected::kYes;
152 }
153
154
155 size_t SkImage_GaneshYUVA::textureSize() const {
156 size_t size = 0;
157 for (int i = 0; i < fYUVAProxies.numPlanes(); ++i) {
158 size += fYUVAProxies.proxy(i)->gpuMemorySize();
159 }
160 return size;
161 }
162
163 sk_sp<SkImage> SkImage_GaneshYUVA::onMakeColorTypeAndColorSpace(
164 GrDirectContext* direct, SkColorType, sk_sp<SkColorSpace> targetCS) const {
165 // We explicitly ignore color type changes, for now.
166
167 // we may need a mutex here but for now we expect usage to be in a single thread
168 if (fOnMakeColorSpaceTarget &&
169 SkColorSpace::Equals(targetCS.get(), fOnMakeColorSpaceTarget.get())) {
170 return fOnMakeColorSpaceResult;
171 }
172 sk_sp<SkImage> result = sk_sp<SkImage>(
173 new SkImage_GaneshYUVA(sk_ref_sp(direct), this, targetCS, ColorSpaceMode::kConvert));
174 if (result) {
175 fOnMakeColorSpaceTarget = targetCS;
176 fOnMakeColorSpaceResult = result;
177 }
178 return result;
179 }
180
181 sk_sp<SkImage> SkImage_GaneshYUVA::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const {
182 return sk_sp<SkImage>(
183 new SkImage_GaneshYUVA(fContext, this, std::move(newCS), ColorSpaceMode::kReinterpret));
184 }
185
186 std::tuple<GrSurfaceProxyView, GrColorType> SkImage_GaneshYUVA::asView(GrRecordingContext* rContext,
187 skgpu::Mipmapped mipmapped,
188 GrImageTexGenPolicy,
189 GrRenderTargetProxy*) const {
190 if (!fContext->priv().matches(rContext)) {
191 return {};
192 }
193 auto sfc = rContext->priv().makeSFC(this->imageInfo(),
194 "Image_GpuYUVA_ReinterpretColorSpace",
195 SkBackingFit::kExact,
196 /*sample count*/ 1,
197 mipmapped,
198 GrProtected::kNo,
199 fYUVAProxies.textureOrigin(),
200 skgpu::Budgeted::kYes);
201 if (!sfc) {
202 return {};
203 }
204
205 const GrCaps& caps = *rContext->priv().caps();
206 auto fp = GrYUVtoRGBEffect::Make(fYUVAProxies, GrSamplerState::Filter::kNearest, caps);
207 if (fFromColorSpace) {
208 fp = GrColorSpaceXformEffect::Make(std::move(fp),
209 fFromColorSpace.get(),
210 this->alphaType(),
211 this->colorSpace(),
212 this->alphaType());
213 }
214 sfc->fillWithFP(std::move(fp));
215
216 return {sfc->readSurfaceView(), sfc->colorInfo().colorType()};
217 }
218
219 std::unique_ptr<GrFragmentProcessor> SkImage_GaneshYUVA::asFragmentProcessor(
220 skgpu::ganesh::SurfaceDrawContext* sdc,
221 SkSamplingOptions sampling,
222 const SkTileMode tileModes[2],
223 const SkMatrix& m,
224 const SkRect* subset,
225 const SkRect* domain) const {
226 GrRecordingContext* context = sdc->recordingContext();
227 if (!fContext->priv().matches(context)) {
228 return {};
229 }
230 // At least for now we do not attempt aniso filtering on YUVA images.
231 if (sampling.isAniso()) {
232 sampling =
233 SkSamplingPriv::AnisoFallback(fYUVAProxies.mipmapped() == skgpu::Mipmapped::kYes);
234 }
235
236 auto wmx = SkTileModeToWrapMode(tileModes[0]);
237 auto wmy = SkTileModeToWrapMode(tileModes[1]);
238 GrSamplerState sampler(wmx, wmy, sampling.filter, sampling.mipmap);
239 if (sampler.mipmapped() == skgpu::Mipmapped::kYes && !this->setupMipmapsForPlanes(context)) {
240 sampler = GrSamplerState(sampler.wrapModeX(),
241 sampler.wrapModeY(),
242 sampler.filter(),
243 GrSamplerState::MipmapMode::kNone);
244 }
245
246 const auto& yuvM = sampling.useCubic ? SkMatrix::I() : m;
247 auto fp = GrYUVtoRGBEffect::Make(
248 fYUVAProxies, sampler, *context->priv().caps(), yuvM, subset, domain);
249 if (sampling.useCubic) {
250 fp = GrBicubicEffect::Make(std::move(fp),
251 this->alphaType(),
252 m,
253 sampling.cubic,
254 GrBicubicEffect::Direction::kXY);
255 }
256 if (fFromColorSpace) {
257 fp = GrColorSpaceXformEffect::Make(std::move(fp),
258 fFromColorSpace.get(),
259 this->alphaType(),
260 this->colorSpace(),
261 this->alphaType());
262 }
263 return fp;
264 }