blob: 2b6d87fa28860f3afce658de2d07100c79a49937 [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"
Brian Salomonca6b2f42020-01-24 11:31:21 -050011#include "src/gpu/GrContextPriv.h"
Greg Daniel82c6b102020-01-21 10:33:22 -050012#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/SkGr.h"
14#include "src/gpu/effects/GrYUVtoRGBEffect.h"
15#include "src/image/SkImage_GpuYUVA.h"
16#include "src/image/SkImage_Lazy.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050017
Greg Daniel82c6b102020-01-21 10:33:22 -050018static GrImageInfo get_image_info(GrRecordingContext* context, const SkImage* client) {
19 SkASSERT(client->isLazyGenerated());
20 const SkImage_Lazy* lazyImage = static_cast<const SkImage_Lazy*>(client);
21
22 GrColorType ct = lazyImage->colorTypeOfLockTextureProxy(context->priv().caps());
23
24 return {ct, client->alphaType(), client->refColorSpace(), client->dimensions()};
25}
26
Robert Phillips9338c602019-02-19 12:52:29 -050027GrImageTextureMaker::GrImageTextureMaker(GrRecordingContext* context, const SkImage* client,
Michael Ludwigddeed372019-02-20 16:50:10 -050028 SkImage::CachingHint chint, bool useDecal)
Greg Daniel82c6b102020-01-21 10:33:22 -050029 : INHERITED(context, get_image_info(context, client), useDecal)
Brian Osmanbd659552018-09-11 10:03:19 -040030 , fImage(static_cast<const SkImage_Lazy*>(client))
Brian Osmandf7e0752017-04-26 16:20:28 -040031 , fCachingHint(chint) {
Brian Osmanbd659552018-09-11 10:03:19 -040032 SkASSERT(client->isLazyGenerated());
Brian Salomon1a217eb2019-10-24 10:50:36 -040033 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(), SkIRect::MakeSize(this->dimensions()));
Brian Osman3b66ab62016-11-28 09:26:31 -050034}
35
Greg Danielcc104db2020-02-03 14:17:08 -050036GrSurfaceProxyView GrImageTextureMaker::refOriginalTextureProxyView(bool willBeMipped,
37 AllowedTexGenType onlyIfFast) {
38 return fImage->lockTextureProxyView(this->context(), fOriginalKey, fCachingHint, willBeMipped,
39 onlyIfFast);
Robert Phillips0c984a02017-03-16 07:51:56 -040040}
41
Brian Osmanb3f38302018-09-07 15:24:44 -040042void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
Brian Osman3b66ab62016-11-28 09:26:31 -050043 if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
Brian Osman10494e32018-09-10 12:45:18 -040044 GrUniqueKey cacheKey;
Brian Osmanbd659552018-09-11 10:03:19 -040045 fImage->makeCacheKeyFromOrigKey(fOriginalKey, &cacheKey);
Brian Osman10494e32018-09-10 12:45:18 -040046 MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
Brian Osman3b66ab62016-11-28 09:26:31 -050047 }
48}
49
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040050
51/////////////////////////////////////////////////////////////////////////////////////////////////
52
Michael Ludwigddeed372019-02-20 16:50:10 -050053GrYUVAImageTextureMaker::GrYUVAImageTextureMaker(GrContext* context, const SkImage* client,
54 bool useDecal)
Brian Salomon1a217eb2019-10-24 10:50:36 -040055 : INHERITED(context, client->imageInfo(), useDecal)
Brian Salomone7499c72019-06-24 12:12:36 -040056 , fImage(static_cast<const SkImage_GpuYUVA*>(client)) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040057 SkASSERT(as_IB(client)->isYUVA());
Brian Salomon1a217eb2019-10-24 10:50:36 -040058 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(), SkIRect::MakeSize(this->dimensions()));
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040059}
60
Greg Danielcc104db2020-02-03 14:17:08 -050061GrSurfaceProxyView GrYUVAImageTextureMaker::refOriginalTextureProxyView(
62 bool willBeMipped, AllowedTexGenType onlyIfFast) {
Jim Van Verth7da46762018-11-05 14:24:23 -050063 if (AllowedTexGenType::kCheap == onlyIfFast) {
Greg Danielcc104db2020-02-03 14:17:08 -050064 return {};
Jim Van Verth7da46762018-11-05 14:24:23 -050065 }
66
Jim Van Verth803a5022018-11-05 15:55:53 -050067 if (willBeMipped) {
Greg Daniel37c127f2020-02-05 10:37:27 -050068 return fImage->refMippedView(this->context());
Jim Van Verth803a5022018-11-05 15:55:53 -050069 } else {
Greg Daniel37c127f2020-02-05 10:37:27 -050070 if (const GrSurfaceProxyView* view = fImage->view(this->context())) {
71 return *view;
72 } else {
73 return {};
74 }
Jim Van Verth7da46762018-11-05 14:24:23 -050075 }
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040076}
77
78void GrYUVAImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
79 // TODO: Do we ever want to disable caching?
80 if (fOriginalKey.isValid()) {
81 GrUniqueKey cacheKey;
82 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
83 GrUniqueKey::Builder builder(&cacheKey, fOriginalKey, kDomain, 0, "Image");
84 MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
85 }
86}
87
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040088std::unique_ptr<GrFragmentProcessor> GrYUVAImageTextureMaker::createFragmentProcessor(
89 const SkMatrix& textureMatrix,
90 const SkRect& constraintRect,
91 FilterConstraint filterConstraint,
92 bool coordsLimitedToConstraintRect,
93 const GrSamplerState::Filter* filterOrNullForBicubic) {
94
Brian Salomonad8efda2019-05-10 09:22:49 -040095 // Check simple cases to see if we need to fall back to flattening the image (or whether it's
96 // already been flattened.)
Greg Daniel1a372c32019-12-17 13:53:38 -050097 if (!filterOrNullForBicubic || this->domainNeedsDecal() || fImage->fRGBView.proxy()) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040098 return this->INHERITED::createFragmentProcessor(textureMatrix, constraintRect,
99 filterConstraint,
100 coordsLimitedToConstraintRect,
101 filterOrNullForBicubic);
102 }
103
Jim Van Verthf542cab2018-11-07 12:08:21 -0500104 // Check to see if the client has given us pre-mipped textures or we can generate them
Michael Ludwiga6a84002019-04-12 15:03:02 -0400105 // If not, fall back to bilerp. Also fall back to bilerp when a domain is requested
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400106 GrSamplerState::Filter filter = *filterOrNullForBicubic;
Robert Phillips8defcc12019-03-05 15:58:59 -0500107 if (GrSamplerState::Filter::kMipMap == filter &&
Michael Ludwiga6a84002019-04-12 15:03:02 -0400108 (filterConstraint == GrTextureProducer::kYes_FilterConstraint ||
109 !fImage->setupMipmapsForPlanes(this->context()))) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400110 filter = GrSamplerState::Filter::kBilerp;
111 }
112
Michael Ludwiga6a84002019-04-12 15:03:02 -0400113 // Cannot rely on GrTextureProducer's domain infrastructure since we need to calculate domain's
114 // per plane, which may be different, so respect the filterConstraint without any additional
115 // analysis.
116 const SkRect* domain = nullptr;
117 if (filterConstraint == GrTextureProducer::kYes_FilterConstraint) {
118 domain = &constraintRect;
119 }
120
Brian Salomonca6b2f42020-01-24 11:31:21 -0500121 const auto& caps = *fImage->context()->priv().caps();
122 auto fp = GrYUVtoRGBEffect::Make(fImage->fProxies, fImage->fYUVAIndices, fImage->fYUVColorSpace,
123 filter, caps, textureMatrix, domain);
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400124 if (fImage->fFromColorSpace) {
125 fp = GrColorSpaceXformEffect::Make(std::move(fp), fImage->fFromColorSpace.get(),
126 fImage->alphaType(), fImage->colorSpace());
Brian Osmane9560492019-02-05 17:00:03 -0500127 }
128 return fp;
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400129}