blob: ead5dfea0057810b7884eaf73cefd609be3db420 [file] [log] [blame]
Brian Osman3b66ab62016-11-28 09:26:31 -05001/*
2 * Copyright 2016 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrImageTextureMaker.h"
Brian Salomone7499c72019-06-24 12:12:36 -04009
10#include "src/gpu/GrColorSpaceXform.h"
Greg Daniel82c6b102020-01-21 10:33:22 -050011#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/SkGr.h"
13#include "src/gpu/effects/GrYUVtoRGBEffect.h"
14#include "src/image/SkImage_GpuYUVA.h"
15#include "src/image/SkImage_Lazy.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050016
Greg Daniel82c6b102020-01-21 10:33:22 -050017static GrImageInfo get_image_info(GrRecordingContext* context, const SkImage* client) {
18 SkASSERT(client->isLazyGenerated());
19 const SkImage_Lazy* lazyImage = static_cast<const SkImage_Lazy*>(client);
20
21 GrColorType ct = lazyImage->colorTypeOfLockTextureProxy(context->priv().caps());
22
23 return {ct, client->alphaType(), client->refColorSpace(), client->dimensions()};
24}
25
Robert Phillips9338c602019-02-19 12:52:29 -050026GrImageTextureMaker::GrImageTextureMaker(GrRecordingContext* context, const SkImage* client,
Michael Ludwigddeed372019-02-20 16:50:10 -050027 SkImage::CachingHint chint, bool useDecal)
Greg Daniel82c6b102020-01-21 10:33:22 -050028 : INHERITED(context, get_image_info(context, client), useDecal)
Brian Osmanbd659552018-09-11 10:03:19 -040029 , fImage(static_cast<const SkImage_Lazy*>(client))
Brian Osmandf7e0752017-04-26 16:20:28 -040030 , fCachingHint(chint) {
Brian Osmanbd659552018-09-11 10:03:19 -040031 SkASSERT(client->isLazyGenerated());
Brian Salomon1a217eb2019-10-24 10:50:36 -040032 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(), SkIRect::MakeSize(this->dimensions()));
Brian Osman3b66ab62016-11-28 09:26:31 -050033}
34
Robert Phillips0c984a02017-03-16 07:51:56 -040035sk_sp<GrTextureProxy> GrImageTextureMaker::refOriginalTextureProxy(bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -040036 AllowedTexGenType onlyIfFast) {
Brian Osmanbd659552018-09-11 10:03:19 -040037 return fImage->lockTextureProxy(this->context(), fOriginalKey, fCachingHint,
Brian Osmane7fd8c32018-10-19 13:30:39 -040038 willBeMipped, onlyIfFast);
Robert Phillips0c984a02017-03-16 07:51:56 -040039}
40
Brian Osmanb3f38302018-09-07 15:24:44 -040041void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
Brian Osman3b66ab62016-11-28 09:26:31 -050042 if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
Brian Osman10494e32018-09-10 12:45:18 -040043 GrUniqueKey cacheKey;
Brian Osmanbd659552018-09-11 10:03:19 -040044 fImage->makeCacheKeyFromOrigKey(fOriginalKey, &cacheKey);
Brian Osman10494e32018-09-10 12:45:18 -040045 MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
Brian Osman3b66ab62016-11-28 09:26:31 -050046 }
47}
48
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040049
50/////////////////////////////////////////////////////////////////////////////////////////////////
51
Michael Ludwigddeed372019-02-20 16:50:10 -050052GrYUVAImageTextureMaker::GrYUVAImageTextureMaker(GrContext* context, const SkImage* client,
53 bool useDecal)
Brian Salomon1a217eb2019-10-24 10:50:36 -040054 : INHERITED(context, client->imageInfo(), useDecal)
Brian Salomone7499c72019-06-24 12:12:36 -040055 , fImage(static_cast<const SkImage_GpuYUVA*>(client)) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040056 SkASSERT(as_IB(client)->isYUVA());
Brian Salomon1a217eb2019-10-24 10:50:36 -040057 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(), SkIRect::MakeSize(this->dimensions()));
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040058}
59
60sk_sp<GrTextureProxy> GrYUVAImageTextureMaker::refOriginalTextureProxy(bool willBeMipped,
61 AllowedTexGenType onlyIfFast) {
Jim Van Verth7da46762018-11-05 14:24:23 -050062 if (AllowedTexGenType::kCheap == onlyIfFast) {
63 return nullptr;
64 }
65
Jim Van Verth803a5022018-11-05 15:55:53 -050066 if (willBeMipped) {
Robert Phillips6603a172019-03-05 12:35:44 -050067 return fImage->asMippedTextureProxyRef(this->context());
Jim Van Verth803a5022018-11-05 15:55:53 -050068 } else {
Robert Phillips6603a172019-03-05 12:35:44 -050069 return fImage->asTextureProxyRef(this->context());
Jim Van Verth7da46762018-11-05 14:24:23 -050070 }
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040071}
72
73void GrYUVAImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
74 // TODO: Do we ever want to disable caching?
75 if (fOriginalKey.isValid()) {
76 GrUniqueKey cacheKey;
77 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
78 GrUniqueKey::Builder builder(&cacheKey, fOriginalKey, kDomain, 0, "Image");
79 MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
80 }
81}
82
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040083std::unique_ptr<GrFragmentProcessor> GrYUVAImageTextureMaker::createFragmentProcessor(
84 const SkMatrix& textureMatrix,
85 const SkRect& constraintRect,
86 FilterConstraint filterConstraint,
87 bool coordsLimitedToConstraintRect,
88 const GrSamplerState::Filter* filterOrNullForBicubic) {
89
Brian Salomonad8efda2019-05-10 09:22:49 -040090 // Check simple cases to see if we need to fall back to flattening the image (or whether it's
91 // already been flattened.)
Greg Daniel1a372c32019-12-17 13:53:38 -050092 if (!filterOrNullForBicubic || this->domainNeedsDecal() || fImage->fRGBView.proxy()) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040093 return this->INHERITED::createFragmentProcessor(textureMatrix, constraintRect,
94 filterConstraint,
95 coordsLimitedToConstraintRect,
96 filterOrNullForBicubic);
97 }
98
Jim Van Verthf542cab2018-11-07 12:08:21 -050099 // Check to see if the client has given us pre-mipped textures or we can generate them
Michael Ludwiga6a84002019-04-12 15:03:02 -0400100 // If not, fall back to bilerp. Also fall back to bilerp when a domain is requested
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400101 GrSamplerState::Filter filter = *filterOrNullForBicubic;
Robert Phillips8defcc12019-03-05 15:58:59 -0500102 if (GrSamplerState::Filter::kMipMap == filter &&
Michael Ludwiga6a84002019-04-12 15:03:02 -0400103 (filterConstraint == GrTextureProducer::kYes_FilterConstraint ||
104 !fImage->setupMipmapsForPlanes(this->context()))) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400105 filter = GrSamplerState::Filter::kBilerp;
106 }
107
Michael Ludwiga6a84002019-04-12 15:03:02 -0400108 // Cannot rely on GrTextureProducer's domain infrastructure since we need to calculate domain's
109 // per plane, which may be different, so respect the filterConstraint without any additional
110 // analysis.
111 const SkRect* domain = nullptr;
112 if (filterConstraint == GrTextureProducer::kYes_FilterConstraint) {
113 domain = &constraintRect;
114 }
115
Brian Osmane9560492019-02-05 17:00:03 -0500116 auto fp = GrYUVtoRGBEffect::Make(fImage->fProxies, fImage->fYUVAIndices,
Michael Ludwiga6a84002019-04-12 15:03:02 -0400117 fImage->fYUVColorSpace, filter, textureMatrix, domain);
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400118 if (fImage->fFromColorSpace) {
119 fp = GrColorSpaceXformEffect::Make(std::move(fp), fImage->fFromColorSpace.get(),
120 fImage->alphaType(), fImage->colorSpace());
Brian Osmane9560492019-02-05 17:00:03 -0500121 }
122 return fp;
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400123}