blob: 9378b9cc7c89c310b48e2fa7531dbc5254a1ce3a [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/SkGr.h"
12#include "src/gpu/effects/GrYUVtoRGBEffect.h"
13#include "src/image/SkImage_GpuYUVA.h"
14#include "src/image/SkImage_Lazy.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050015
Brian Salomone7499c72019-06-24 12:12:36 -040016static GrColorSpaceInfo make_info(const SkImage*& image) {
Brian Salomond6287472019-06-24 15:50:07 -040017 return GrColorSpaceInfo(SkColorTypeToGrColorType(image->colorType()),
18 image->alphaType(),
Brian Salomonbd3d8d32019-07-02 09:16:28 -040019 image->refColorSpace());
Brian Salomone7499c72019-06-24 12:12:36 -040020}
21
Robert Phillips9338c602019-02-19 12:52:29 -050022GrImageTextureMaker::GrImageTextureMaker(GrRecordingContext* context, const SkImage* client,
Michael Ludwigddeed372019-02-20 16:50:10 -050023 SkImage::CachingHint chint, bool useDecal)
Brian Salomone7499c72019-06-24 12:12:36 -040024 : INHERITED(context, client->width(), client->height(), make_info(client), useDecal)
Brian Osmanbd659552018-09-11 10:03:19 -040025 , fImage(static_cast<const SkImage_Lazy*>(client))
Brian Osmandf7e0752017-04-26 16:20:28 -040026 , fCachingHint(chint) {
Brian Osmanbd659552018-09-11 10:03:19 -040027 SkASSERT(client->isLazyGenerated());
Brian Osmandf7e0752017-04-26 16:20:28 -040028 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
29 SkIRect::MakeWH(this->width(), this->height()));
Brian Osman3b66ab62016-11-28 09:26:31 -050030}
31
Robert Phillips0c984a02017-03-16 07:51:56 -040032sk_sp<GrTextureProxy> GrImageTextureMaker::refOriginalTextureProxy(bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -040033 AllowedTexGenType onlyIfFast) {
Brian Osmanbd659552018-09-11 10:03:19 -040034 return fImage->lockTextureProxy(this->context(), fOriginalKey, fCachingHint,
Brian Osmane7fd8c32018-10-19 13:30:39 -040035 willBeMipped, onlyIfFast);
Robert Phillips0c984a02017-03-16 07:51:56 -040036}
37
Brian Osmanb3f38302018-09-07 15:24:44 -040038void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
Brian Osman3b66ab62016-11-28 09:26:31 -050039 if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
Brian Osman10494e32018-09-10 12:45:18 -040040 GrUniqueKey cacheKey;
Brian Osmanbd659552018-09-11 10:03:19 -040041 fImage->makeCacheKeyFromOrigKey(fOriginalKey, &cacheKey);
Brian Osman10494e32018-09-10 12:45:18 -040042 MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
Brian Osman3b66ab62016-11-28 09:26:31 -050043 }
44}
45
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040046
47/////////////////////////////////////////////////////////////////////////////////////////////////
48
Michael Ludwigddeed372019-02-20 16:50:10 -050049GrYUVAImageTextureMaker::GrYUVAImageTextureMaker(GrContext* context, const SkImage* client,
50 bool useDecal)
Brian Salomone7499c72019-06-24 12:12:36 -040051 : INHERITED(context, client->width(), client->height(), make_info(client), useDecal)
52 , fImage(static_cast<const SkImage_GpuYUVA*>(client)) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040053 SkASSERT(as_IB(client)->isYUVA());
54 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
55 SkIRect::MakeWH(this->width(), this->height()));
56}
57
58sk_sp<GrTextureProxy> GrYUVAImageTextureMaker::refOriginalTextureProxy(bool willBeMipped,
59 AllowedTexGenType onlyIfFast) {
Jim Van Verth7da46762018-11-05 14:24:23 -050060 if (AllowedTexGenType::kCheap == onlyIfFast) {
61 return nullptr;
62 }
63
Jim Van Verth803a5022018-11-05 15:55:53 -050064 if (willBeMipped) {
Robert Phillips6603a172019-03-05 12:35:44 -050065 return fImage->asMippedTextureProxyRef(this->context());
Jim Van Verth803a5022018-11-05 15:55:53 -050066 } else {
Robert Phillips6603a172019-03-05 12:35:44 -050067 return fImage->asTextureProxyRef(this->context());
Jim Van Verth7da46762018-11-05 14:24:23 -050068 }
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040069}
70
71void GrYUVAImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
72 // TODO: Do we ever want to disable caching?
73 if (fOriginalKey.isValid()) {
74 GrUniqueKey cacheKey;
75 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
76 GrUniqueKey::Builder builder(&cacheKey, fOriginalKey, kDomain, 0, "Image");
77 MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
78 }
79}
80
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040081std::unique_ptr<GrFragmentProcessor> GrYUVAImageTextureMaker::createFragmentProcessor(
82 const SkMatrix& textureMatrix,
83 const SkRect& constraintRect,
84 FilterConstraint filterConstraint,
85 bool coordsLimitedToConstraintRect,
86 const GrSamplerState::Filter* filterOrNullForBicubic) {
87
Brian Salomonad8efda2019-05-10 09:22:49 -040088 // Check simple cases to see if we need to fall back to flattening the image (or whether it's
89 // already been flattened.)
90 if (!filterOrNullForBicubic || this->domainNeedsDecal() || fImage->fRGBProxy) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040091 return this->INHERITED::createFragmentProcessor(textureMatrix, constraintRect,
92 filterConstraint,
93 coordsLimitedToConstraintRect,
94 filterOrNullForBicubic);
95 }
96
Jim Van Verthf542cab2018-11-07 12:08:21 -050097 // Check to see if the client has given us pre-mipped textures or we can generate them
Michael Ludwiga6a84002019-04-12 15:03:02 -040098 // If not, fall back to bilerp. Also fall back to bilerp when a domain is requested
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040099 GrSamplerState::Filter filter = *filterOrNullForBicubic;
Robert Phillips8defcc12019-03-05 15:58:59 -0500100 if (GrSamplerState::Filter::kMipMap == filter &&
Michael Ludwiga6a84002019-04-12 15:03:02 -0400101 (filterConstraint == GrTextureProducer::kYes_FilterConstraint ||
102 !fImage->setupMipmapsForPlanes(this->context()))) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400103 filter = GrSamplerState::Filter::kBilerp;
104 }
105
Michael Ludwiga6a84002019-04-12 15:03:02 -0400106 // Cannot rely on GrTextureProducer's domain infrastructure since we need to calculate domain's
107 // per plane, which may be different, so respect the filterConstraint without any additional
108 // analysis.
109 const SkRect* domain = nullptr;
110 if (filterConstraint == GrTextureProducer::kYes_FilterConstraint) {
111 domain = &constraintRect;
112 }
113
Brian Osmane9560492019-02-05 17:00:03 -0500114 auto fp = GrYUVtoRGBEffect::Make(fImage->fProxies, fImage->fYUVAIndices,
Michael Ludwiga6a84002019-04-12 15:03:02 -0400115 fImage->fYUVColorSpace, filter, textureMatrix, domain);
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400116 if (fImage->fFromColorSpace) {
117 fp = GrColorSpaceXformEffect::Make(std::move(fp), fImage->fFromColorSpace.get(),
118 fImage->alphaType(), fImage->colorSpace());
Brian Osmane9560492019-02-05 17:00:03 -0500119 }
120 return fp;
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400121}