blob: 0fb1cbee9c973e02d428ef3d39b7f280b7a4ed4c [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/GrColorSpaceXform.h"
9#include "src/gpu/GrImageTextureMaker.h"
10#include "src/gpu/SkGr.h"
11#include "src/gpu/effects/GrYUVtoRGBEffect.h"
12#include "src/image/SkImage_GpuYUVA.h"
13#include "src/image/SkImage_Lazy.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050014
Robert Phillips9338c602019-02-19 12:52:29 -050015GrImageTextureMaker::GrImageTextureMaker(GrRecordingContext* context, const SkImage* client,
Michael Ludwigddeed372019-02-20 16:50:10 -050016 SkImage::CachingHint chint, bool useDecal)
17 : INHERITED(context, client->width(), client->height(), client->isAlphaOnly(), useDecal)
Brian Osmanbd659552018-09-11 10:03:19 -040018 , fImage(static_cast<const SkImage_Lazy*>(client))
Brian Osmandf7e0752017-04-26 16:20:28 -040019 , fCachingHint(chint) {
Brian Osmanbd659552018-09-11 10:03:19 -040020 SkASSERT(client->isLazyGenerated());
Brian Osmandf7e0752017-04-26 16:20:28 -040021 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
22 SkIRect::MakeWH(this->width(), this->height()));
Brian Osman3b66ab62016-11-28 09:26:31 -050023}
24
Robert Phillips0c984a02017-03-16 07:51:56 -040025sk_sp<GrTextureProxy> GrImageTextureMaker::refOriginalTextureProxy(bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -040026 AllowedTexGenType onlyIfFast) {
Brian Osmanbd659552018-09-11 10:03:19 -040027 return fImage->lockTextureProxy(this->context(), fOriginalKey, fCachingHint,
Brian Osmane7fd8c32018-10-19 13:30:39 -040028 willBeMipped, onlyIfFast);
Robert Phillips0c984a02017-03-16 07:51:56 -040029}
30
Brian Osmanb3f38302018-09-07 15:24:44 -040031void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
Brian Osman3b66ab62016-11-28 09:26:31 -050032 if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
Brian Osman10494e32018-09-10 12:45:18 -040033 GrUniqueKey cacheKey;
Brian Osmanbd659552018-09-11 10:03:19 -040034 fImage->makeCacheKeyFromOrigKey(fOriginalKey, &cacheKey);
Brian Osman10494e32018-09-10 12:45:18 -040035 MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
Brian Osman3b66ab62016-11-28 09:26:31 -050036 }
37}
38
Brian Osman3b66ab62016-11-28 09:26:31 -050039SkAlphaType GrImageTextureMaker::alphaType() const {
Brian Osmanbd659552018-09-11 10:03:19 -040040 return fImage->alphaType();
Brian Osman3b66ab62016-11-28 09:26:31 -050041}
Brian Osman6064e1c2018-10-19 14:27:54 -040042SkColorSpace* GrImageTextureMaker::colorSpace() const {
43 return fImage->colorSpace();
Brian Osman3b66ab62016-11-28 09:26:31 -050044}
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040045
46/////////////////////////////////////////////////////////////////////////////////////////////////
47
Michael Ludwigddeed372019-02-20 16:50:10 -050048GrYUVAImageTextureMaker::GrYUVAImageTextureMaker(GrContext* context, const SkImage* client,
49 bool useDecal)
50 : INHERITED(context, client->width(), client->height(), client->isAlphaOnly(), useDecal)
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040051 , fImage(static_cast<const SkImage_GpuYUVA*>(client)) {
52 SkASSERT(as_IB(client)->isYUVA());
53 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
54 SkIRect::MakeWH(this->width(), this->height()));
55}
56
57sk_sp<GrTextureProxy> GrYUVAImageTextureMaker::refOriginalTextureProxy(bool willBeMipped,
58 AllowedTexGenType onlyIfFast) {
Jim Van Verth7da46762018-11-05 14:24:23 -050059 if (AllowedTexGenType::kCheap == onlyIfFast) {
60 return nullptr;
61 }
62
Jim Van Verth803a5022018-11-05 15:55:53 -050063 if (willBeMipped) {
Robert Phillips6603a172019-03-05 12:35:44 -050064 return fImage->asMippedTextureProxyRef(this->context());
Jim Van Verth803a5022018-11-05 15:55:53 -050065 } else {
Robert Phillips6603a172019-03-05 12:35:44 -050066 return fImage->asTextureProxyRef(this->context());
Jim Van Verth7da46762018-11-05 14:24:23 -050067 }
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040068}
69
70void GrYUVAImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
71 // TODO: Do we ever want to disable caching?
72 if (fOriginalKey.isValid()) {
73 GrUniqueKey cacheKey;
74 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
75 GrUniqueKey::Builder builder(&cacheKey, fOriginalKey, kDomain, 0, "Image");
76 MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
77 }
78}
79
80SkAlphaType GrYUVAImageTextureMaker::alphaType() const {
81 return fImage->alphaType();
82}
83SkColorSpace* GrYUVAImageTextureMaker::colorSpace() const {
84 return fImage->colorSpace();
85}
86
87std::unique_ptr<GrFragmentProcessor> GrYUVAImageTextureMaker::createFragmentProcessor(
88 const SkMatrix& textureMatrix,
89 const SkRect& constraintRect,
90 FilterConstraint filterConstraint,
91 bool coordsLimitedToConstraintRect,
92 const GrSamplerState::Filter* filterOrNullForBicubic) {
93
Brian Salomonad8efda2019-05-10 09:22:49 -040094 // Check simple cases to see if we need to fall back to flattening the image (or whether it's
95 // already been flattened.)
96 if (!filterOrNullForBicubic || this->domainNeedsDecal() || fImage->fRGBProxy) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040097 return this->INHERITED::createFragmentProcessor(textureMatrix, constraintRect,
98 filterConstraint,
99 coordsLimitedToConstraintRect,
100 filterOrNullForBicubic);
101 }
102
Jim Van Verthf542cab2018-11-07 12:08:21 -0500103 // Check to see if the client has given us pre-mipped textures or we can generate them
Michael Ludwiga6a84002019-04-12 15:03:02 -0400104 // If not, fall back to bilerp. Also fall back to bilerp when a domain is requested
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400105 GrSamplerState::Filter filter = *filterOrNullForBicubic;
Robert Phillips8defcc12019-03-05 15:58:59 -0500106 if (GrSamplerState::Filter::kMipMap == filter &&
Michael Ludwiga6a84002019-04-12 15:03:02 -0400107 (filterConstraint == GrTextureProducer::kYes_FilterConstraint ||
108 !fImage->setupMipmapsForPlanes(this->context()))) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400109 filter = GrSamplerState::Filter::kBilerp;
110 }
111
Michael Ludwiga6a84002019-04-12 15:03:02 -0400112 // Cannot rely on GrTextureProducer's domain infrastructure since we need to calculate domain's
113 // per plane, which may be different, so respect the filterConstraint without any additional
114 // analysis.
115 const SkRect* domain = nullptr;
116 if (filterConstraint == GrTextureProducer::kYes_FilterConstraint) {
117 domain = &constraintRect;
118 }
119
Brian Osmane9560492019-02-05 17:00:03 -0500120 auto fp = GrYUVtoRGBEffect::Make(fImage->fProxies, fImage->fYUVAIndices,
Michael Ludwiga6a84002019-04-12 15:03:02 -0400121 fImage->fYUVColorSpace, filter, textureMatrix, domain);
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400122 if (fImage->fFromColorSpace) {
123 fp = GrColorSpaceXformEffect::Make(std::move(fp), fImage->fFromColorSpace.get(),
124 fImage->alphaType(), fImage->colorSpace());
Brian Osmane9560492019-02-05 17:00:03 -0500125 }
126 return fp;
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400127}