Debian Codegraph @

src/gpu/ganesh/image/SkImage_GaneshBase.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_GaneshBase.h"
9
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPixmap.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkSurface.h"
19 #include "include/core/SkTypes.h"
20 #include "include/gpu/GpuTypes.h"
21 #include "include/gpu/ganesh/GrBackendSurface.h"
22 #include "include/gpu/ganesh/GrDirectContext.h"
23 #include "include/gpu/ganesh/GrRecordingContext.h"
24 #include "include/gpu/ganesh/GrTypes.h"
25 #include "include/gpu/ganesh/SkImageGanesh.h"
26 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
27 #include "include/private/chromium/GrPromiseImageTexture.h"
28 #include "include/private/chromium/SkImageChromium.h"
29 #include "include/private/gpu/ganesh/GrTypesPriv.h"
30 #include "src/core/SkBitmapCache.h"
31 #include "src/core/SkColorSpacePriv.h"
32 #include "src/core/SkImageFilterTypes.h"
33 #include "src/core/SkImageFilter_Base.h"
34 #include "src/core/SkImageInfoPriv.h"
35 #include "src/gpu/RefCntedCallback.h"
36 #include "src/gpu/SkBackingFit.h"
37 #include "src/gpu/ganesh/GrCaps.h"
38 #include "src/gpu/ganesh/GrColorInfo.h"
39 #include "src/gpu/ganesh/GrDirectContextPriv.h"
40 #include "src/gpu/ganesh/GrImageContextPriv.h"
41 #include "src/gpu/ganesh/GrProxyProvider.h"
42 #include "src/gpu/ganesh/GrResourceCache.h"
43 #include "src/gpu/ganesh/GrResourceProvider.h"
44 #include "src/gpu/ganesh/GrSurface.h"
45 #include "src/gpu/ganesh/GrSurfaceProxy.h"
46 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
47 #include "src/gpu/ganesh/GrTexture.h"
48 #include "src/gpu/ganesh/GrTextureProxy.h"
49 #include "src/gpu/ganesh/SkGaneshRecorder.h"
50 #include "src/gpu/ganesh/SurfaceContext.h"
51 #include "src/gpu/ganesh/image/GrImageUtils.h"
52 #include "src/gpu/ganesh/image/SkImage_Ganesh.h"
53 #include "src/image/SkImage_Base.h"
54
55 #include <functional>
56 #include <memory>
57 #include <utility>
58
59 class GrContextThreadSafeProxy;
60 class SkImageFilter;
61 struct SkIPoint;
62
63 SkImage_GaneshBase::SkImage_GaneshBase(sk_sp<GrImageContext> context,
64 SkImageInfo info,
65 uint32_t uniqueID)
66 : SkImage_Base(std::move(info), uniqueID), fContext(std::move(context)) {}
67
68 //////////////////////////////////////////////////////////////////////////////////////////////////
69
70 bool SkImage_GaneshBase::ValidateBackendTexture(const GrCaps* caps,
71 const GrBackendTexture& tex,
72 GrColorType grCT,
73 SkColorType ct,
74 SkAlphaType at,
75 sk_sp<SkColorSpace> cs) {
76 if (!tex.isValid()) {
77 return false;
78 }
79 SkColorInfo info(ct, at, cs);
80 if (!SkColorInfoIsValid(info)) {
81 return false;
82 }
83 GrBackendFormat backendFormat = tex.getBackendFormat();
84 if (!backendFormat.isValid()) {
85 return false;
86 }
87
88 return caps->areColorTypeAndFormatCompatible(grCT, backendFormat);
89 }
90
91 bool SkImage_GaneshBase::ValidateCompressedBackendTexture(const GrCaps* caps,
92 const GrBackendTexture& tex,
93 SkAlphaType at) {
94 if (!tex.isValid() || tex.width() <= 0 || tex.height() <= 0) {
95 return false;
96 }
97
98 if (tex.width() > caps->maxTextureSize() || tex.height() > caps->maxTextureSize()) {
99 return false;
100 }
101
102 if (at == kUnknown_SkAlphaType) {
103 return false;
104 }
105
106 GrBackendFormat backendFormat = tex.getBackendFormat();
107 if (!backendFormat.isValid()) {
108 return false;
109 }
110
111 if (!caps->isFormatCompressed(backendFormat)) {
112 return false;
113 }
114
115 return true;
116 }
117
118 //////////////////////////////////////////////////////////////////////////////////////////////////
119
120 bool SkImage_GaneshBase::getROPixels(GrDirectContext* dContext,
121 SkBitmap* dst,
122 CachingHint chint) const {
123 if (!fContext->priv().matches(dContext)) {
124 return false;
125 }
126
127 const auto desc = SkBitmapCacheDesc::Make(this);
128 if (SkBitmapCache::Find(desc, dst)) {
129 SkASSERT(dst->isImmutable());
130 SkASSERT(dst->getPixels());
131 return true;
132 }
133
134 SkBitmapCache::RecPtr rec = nullptr;
135 SkPixmap pmap;
136 if (kAllow_CachingHint == chint) {
137 rec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
138 if (!rec) {
139 return false;
140 }
141 } else {
142 if (!dst->tryAllocPixels(this->imageInfo()) || !dst->peekPixels(&pmap)) {
143 return false;
144 }
145 }
146
147 auto [view, ct] = skgpu::ganesh::AsView(dContext, this, skgpu::Mipmapped::kNo,
148 /*targetSurface=*/nullptr);
149 if (!view) {
150 return false;
151 }
152
153 GrColorInfo colorInfo(ct, this->alphaType(), this->refColorSpace());
154 auto sContext = dContext->priv().makeSC(std::move(view), std::move(colorInfo));
155 if (!sContext) {
156 return false;
157 }
158
159 if (!sContext->readPixels(dContext, pmap, {0, 0})) {
160 return false;
161 }
162
163 if (rec) {
164 SkBitmapCache::Add(std::move(rec), dst);
165 this->notifyAddedToRasterCache();
166 }
167 return true;
168 }
169
170 sk_sp<SkImage> SkImage_GaneshBase::onMakeSubset(SkRecorder* recorder,
171 const SkIRect& subset,
172 RequiredProperties) const {
173 auto gRecorder = AsGaneshRecorder(recorder);
174 if (!gRecorder) {
175 if (recorder) {
176 SkDEBUGFAIL("Wrong recorder type; need Ganesh Recorder made from direct context");
177 }
178 return nullptr;
179 }
180 auto direct = gRecorder->directContext();
181 if (!direct) {
182 SkDEBUGFAIL("Cannot take subset of Ganesh image w/o a directContext");
183 return nullptr;
184 }
185 if (!fContext->priv().matches(direct)) {
186 return nullptr;
187 }
188 auto [view, ct] = skgpu::ganesh::AsView(direct, this, skgpu::Mipmapped::kNo,
189 /*targetSurface=*/nullptr);
190 SkASSERT(view);
191 SkASSERT(ct == SkColorTypeToGrColorType(this->colorType()));
192
193 skgpu::Budgeted isBudgeted = view.proxy()->isBudgeted();
194 auto copyView = GrSurfaceProxyView::Copy(direct,
195 std::move(view),
196 skgpu::Mipmapped::kNo,
197 subset,
198 SkBackingFit::kExact,
199 isBudgeted,
200 /*label=*/"ImageGpuBase_MakeSubset");
201
202 if (!copyView) {
203 return nullptr;
204 }
205
206 return sk_make_sp<SkImage_Ganesh>(sk_ref_sp(direct),
207 kNeedNewImageUniqueID,
208 std::move(copyView),
209 this->imageInfo().colorInfo());
210 }
211
212 sk_sp<SkImage> SkImage_GaneshBase::makeColorTypeAndColorSpace(SkRecorder* recorder,
213 SkColorType targetColorType,
214 sk_sp<SkColorSpace> targetCS,
215 RequiredProperties) const {
216 auto gRecorder = AsGaneshRecorder(recorder);
217 if (!gRecorder) {
218 return nullptr;
219 }
220 GrDirectContext* dContext = gRecorder->directContext();
221 if (!dContext) {
222 return nullptr;
223 }
224
225 if (kUnknown_SkColorType == targetColorType || !targetCS) {
226 return nullptr;
227 }
228
229 auto myContext = this->context();
230 // This check is also performed in the subclass, but we do it here for the short-circuit below.
231 if (!myContext || !myContext->priv().matches(dContext)) {
232 return nullptr;
233 }
234
235 SkColorType colorType = this->colorType();
236 SkColorSpace* colorSpace = this->colorSpace();
237 if (!colorSpace) {
238 colorSpace = sk_srgb_singleton();
239 }
240 if (colorType == targetColorType &&
241 (SkColorSpace::Equals(colorSpace, targetCS.get()) || this->isAlphaOnly())) {
242 return sk_ref_sp(const_cast<SkImage_GaneshBase*>(this));
243 }
244
245 return this->onMakeColorTypeAndColorSpace(dContext, targetColorType, std::move(targetCS));
246 }
247
248 sk_sp<SkSurface> SkImage_GaneshBase::onMakeSurface(SkRecorder* recorder,
249 const SkImageInfo& info) const {
250 if (!recorder) {
251 // TODO(kjlubick) remove this after old SkImage::makeScaled(image info, sampling) API gone
252 if (auto ictx = this->context()) {
253 if (auto rctx = ictx->priv().asRecordingContext()) {
254 auto isBudgeted = skgpu::Budgeted::kNo; // Assuming we're a one-shot surface
255 return SkSurfaces::RenderTarget(rctx, isBudgeted, info);
256 }
257 }
258 return nullptr;
259 }
260 auto gRecorder = AsGaneshRecorder(recorder);
261 if (!gRecorder) {
262 return nullptr;
263 }
264 constexpr auto isBudgeted = skgpu::Budgeted::kNo; // Assuming we're a one-shot surface
265 return SkSurfaces::RenderTarget(gRecorder->recordingContext(), isBudgeted, info);
266 }
267
268 bool SkImage_GaneshBase::onReadPixels(GrDirectContext* dContext,
269 const SkImageInfo& dstInfo,
270 void* dstPixels,
271 size_t dstRB,
272 int srcX,
273 int srcY,
274 CachingHint) const {
275 if (!fContext->priv().matches(dContext) ||
276 !SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
277 return false;
278 }
279
280 auto [view, ct] = skgpu::ganesh::AsView(dContext, this, skgpu::Mipmapped::kNo,
281 /*targetSurface=*/nullptr);
282 SkASSERT(view);
283
284 GrColorInfo colorInfo(ct, this->alphaType(), this->refColorSpace());
285 auto sContext = dContext->priv().makeSC(std::move(view), colorInfo);
286 if (!sContext) {
287 return false;
288 }
289
290 return sContext->readPixels(dContext, {dstInfo, dstPixels, dstRB}, {srcX, srcY});
291 }
292
293 bool SkImage_GaneshBase::isValid(SkRecorder* recorder) const {
294 auto gRecorder = AsGaneshRecorder(recorder);
295 if (!gRecorder) {
296 return false;
297 }
298 auto context = gRecorder->recordingContext();
299 if (!context || context->abandoned()) {
300 return false;
301 }
302 if (fContext->priv().abandoned()) {
303 return false;
304 }
305 if (context && !fContext->priv().matches(context)) {
306 return false;
307 }
308 return true;
309 }
310
311 sk_sp<GrTextureProxy> SkImage_GaneshBase::MakePromiseImageLazyProxy(
312 GrContextThreadSafeProxy* tsp,
313 SkISize dimensions,
314 const GrBackendFormat& backendFormat,
315 skgpu::Mipmapped mipmapped,
316 SkImages::PromiseImageTextureFulfillProc fulfillProc,
317 sk_sp<skgpu::RefCntedCallback> releaseHelper) {
318 SkASSERT(tsp);
319 SkASSERT(!dimensions.isEmpty());
320 SkASSERT(releaseHelper);
321
322 if (!fulfillProc) {
323 return nullptr;
324 }
325
326 if (mipmapped == skgpu::Mipmapped::kYes &&
327 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
328 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
329 // well.
330 return nullptr;
331 }
332
333 /**
334 * This class is the lazy instantiation callback for promise images. It manages calling the
335 * client's Fulfill and Release procs. It attempts to reuse a GrTexture instance in
336 * cases where the client provides the same GrPromiseImageTexture as Fulfill results for
337 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
338 * the GrPromiseImageTexture.
339 *
340 * A key invalidation message is installed on the GrPromiseImageTexture so that the GrTexture
341 * is deleted once it can no longer be used to instantiate a proxy.
342 */
343 class PromiseLazyInstantiateCallback {
344 public:
345 PromiseLazyInstantiateCallback(SkImages::PromiseImageTextureFulfillProc fulfillProc,
346 sk_sp<skgpu::RefCntedCallback> releaseHelper)
347 : fFulfillProc(fulfillProc), fReleaseHelper(std::move(releaseHelper)) {}
348 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
349 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
350 // Because we get wrapped in std::function we must be copyable. But we should never
351 // be copied.
352 SkASSERT(false);
353 }
354 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
355 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
356 SkASSERT(false);
357 return *this;
358 }
359
360 ~PromiseLazyInstantiateCallback() {
361 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
362 if (fTexture) {
363 GrResourceCache::ReturnResourceFromThread(std::move(fTexture), fTextureContextID);
364 }
365 }
366
367 GrSurfaceProxy::LazyCallbackResult operator()(GrResourceProvider* resourceProvider,
368 const GrSurfaceProxy::LazySurfaceDesc&) {
369 // We use the unique key in a way that is unrelated to the SkImage-based key that the
370 // proxy may receive, hence kUnsynced.
371 static constexpr auto kKeySyncMode =
372 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
373
374 // In order to make the SkImage "thread safe" we rely on holding an extra ref to the
375 // texture in the callback and signalling the unref via a message to the resource cache.
376 // We need to extend the callback's lifetime to that of the proxy.
377 static constexpr auto kReleaseCallbackOnInstantiation = false;
378
379 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
380 // Fulfill once. So return our cached result.
381 if (fTexture) {
382 return {fTexture, kReleaseCallbackOnInstantiation, kKeySyncMode};
383 } else if (fFulfillProcFailed) {
384 // We've already called fulfill and it failed. Our contract says that we should only
385 // call each callback once.
386 return {};
387 }
388
389 SkImages::PromiseImageTextureContext textureContext = fReleaseHelper->context();
390 sk_sp<GrPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
391
392 if (!promiseTexture) {
393 fFulfillProcFailed = true;
394 return {};
395 }
396
397 const GrBackendTexture& backendTexture = promiseTexture->backendTexture();
398 if (!backendTexture.isValid()) {
399 return {};
400 }
401
402 fTexture = resourceProvider->wrapBackendTexture(
403 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRead_GrIOType);
404 if (!fTexture) {
405 return {};
406 }
407 fTexture->setRelease(fReleaseHelper);
408 auto dContext = fTexture->getContext();
409 fTextureContextID = dContext->directContextID();
410 return {fTexture, kReleaseCallbackOnInstantiation, kKeySyncMode};
411 }
412
413 private:
414 SkImages::PromiseImageTextureFulfillProc fFulfillProc;
415 sk_sp<skgpu::RefCntedCallback> fReleaseHelper;
416 sk_sp<GrTexture> fTexture;
417 GrDirectContext::DirectContextID fTextureContextID;
418 bool fFulfillProcFailed = false;
419 } callback(fulfillProc, std::move(releaseHelper));
420
421 return GrProxyProvider::CreatePromiseProxy(
422 tsp, std::move(callback), backendFormat, dimensions, mipmapped);
423 }
424
425 namespace SkImages {
426 sk_sp<SkImage> SubsetTextureFrom(GrDirectContext* context,
427 const SkImage* img,
428 const SkIRect& subset) {
429 if (context == nullptr || img == nullptr) {
430 return nullptr;
431 }
432 auto subsetImg = img->makeSubset(context->asRecorder(), subset, {});
433 return SkImages::TextureFromImage(context, subsetImg.get());
434 }
435
436 sk_sp<SkImage> MakeWithFilter(GrRecordingContext* rContext,
437 sk_sp<SkImage> src,
438 const SkImageFilter* filter,
439 const SkIRect& subset,
440 const SkIRect& clipBounds,
441 SkIRect* outSubset,
442 SkIPoint* offset) {
443 if (!rContext || !src || !filter) {
444 return nullptr;
445 }
446
447 GrSurfaceOrigin origin = kTopLeft_GrSurfaceOrigin;
448 if (as_IB(src)->isGaneshBacked()) {
449 SkImage_GaneshBase* base = static_cast<SkImage_GaneshBase*>(src.get());
450 origin = base->origin();
451 }
452
453 sk_sp<skif::Backend> backend =
454 skif::MakeGaneshBackend(sk_ref_sp(rContext), origin, {}, src->colorType());
455 return as_IFB(filter)->makeImageWithFilter(std::move(backend),
456 std::move(src),
457 subset,
458 clipBounds,
459 outSubset,
460 offset);
461 }
462
463 GrDirectContext* GetContext(const SkImage* src) {
464 if (!src || !as_IB(src)->isGaneshBacked()) {
465 return nullptr;
466 }
467 return as_IB(src)->directContext();
468 }
469
470
471 } // namespace SkImages