blob: 6c2e57a5dfcdf5fb13e75be78539384da6048248 [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"
Adlai Holler302e8fb2020-09-14 11:58:06 -040011#include "src/gpu/GrImageContextPriv.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"
Brian Salomon5c4c61e2020-03-25 14:26:01 -040014#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/effects/GrYUVtoRGBEffect.h"
16#include "src/image/SkImage_GpuYUVA.h"
17#include "src/image/SkImage_Lazy.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050018
Greg Daniel82c6b102020-01-21 10:33:22 -050019static GrImageInfo get_image_info(GrRecordingContext* context, const SkImage* client) {
20 SkASSERT(client->isLazyGenerated());
21 const SkImage_Lazy* lazyImage = static_cast<const SkImage_Lazy*>(client);
22
23 GrColorType ct = lazyImage->colorTypeOfLockTextureProxy(context->priv().caps());
24
25 return {ct, client->alphaType(), client->refColorSpace(), client->dimensions()};
26}
27
Brian Salomonbc074a62020-03-18 10:06:13 -040028GrImageTextureMaker::GrImageTextureMaker(GrRecordingContext* context,
29 const SkImage* client,
30 GrImageTexGenPolicy texGenPolicy)
Brian Salomon777e1462020-02-28 21:10:31 -050031 : INHERITED(context, get_image_info(context, client))
Brian Osmanbd659552018-09-11 10:03:19 -040032 , fImage(static_cast<const SkImage_Lazy*>(client))
Brian Salomonbc074a62020-03-18 10:06:13 -040033 , fTexGenPolicy(texGenPolicy) {
Brian Osmanbd659552018-09-11 10:03:19 -040034 SkASSERT(client->isLazyGenerated());
Brian Osman3b66ab62016-11-28 09:26:31 -050035}
36
Brian Salomon7e67dca2020-07-21 09:27:25 -040037GrSurfaceProxyView GrImageTextureMaker::refOriginalTextureProxyView(GrMipmapped mipMapped) {
Brian Salomonbc074a62020-03-18 10:06:13 -040038 return fImage->lockTextureProxyView(this->context(), fTexGenPolicy, mipMapped);
Robert Phillips0c984a02017-03-16 07:51:56 -040039}
40
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040041/////////////////////////////////////////////////////////////////////////////////////////////////
42
Brian Salomon0c243202020-06-29 14:29:25 -040043GrYUVAImageTextureMaker::GrYUVAImageTextureMaker(GrRecordingContext* context, const SkImage* client)
Brian Salomon777e1462020-02-28 21:10:31 -050044 : INHERITED(context, client->imageInfo())
Brian Salomone7499c72019-06-24 12:12:36 -040045 , fImage(static_cast<const SkImage_GpuYUVA*>(client)) {
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040046 SkASSERT(as_IB(client)->isYUVA());
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040047}
48
Brian Salomond0924f32021-02-03 10:15:31 -050049GrSurfaceProxyView GrYUVAImageTextureMaker::refOriginalTextureProxyView(GrMipmapped mipmapped) {
50 auto [view, ct] = fImage->asView(this->context(), mipmapped);
51 SkASSERT(ct == this->colorType());
52 return view;
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040053}
54
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040055std::unique_ptr<GrFragmentProcessor> GrYUVAImageTextureMaker::createFragmentProcessor(
Brian Salomon777e1462020-02-28 21:10:31 -050056 const SkMatrix& textureMatrix,
Brian Salomon0ea33072020-07-14 10:43:42 -040057 const SkRect* subset,
58 const SkRect* domain,
59 GrSamplerState samplerState) {
Brian Salomon5c4c61e2020-03-25 14:26:01 -040060 // Check whether it's already been flattened.
61 if (fImage->fRGBView.proxy()) {
Brian Salomon0ea33072020-07-14 10:43:42 -040062 return this->INHERITED::createFragmentProcessor(textureMatrix, subset, domain,
63 samplerState);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040064 }
65
Jim Van Verthf542cab2018-11-07 12:08:21 -050066 // Check to see if the client has given us pre-mipped textures or we can generate them
Brian Salomone69b9ef2020-07-22 11:18:06 -040067 // If not disable mip mapping. Also disable when a subset is requested.
68 if (samplerState.mipmapped() == GrMipmapped::kYes &&
Brian Salomon0ea33072020-07-14 10:43:42 -040069 (subset || !fImage->setupMipmapsForPlanes(this->context()))) {
Brian Salomone69b9ef2020-07-22 11:18:06 -040070 samplerState.setMipmapMode(GrSamplerState::MipmapMode::kNone);
Michael Ludwiga6a84002019-04-12 15:03:02 -040071 }
72
Brian Salomonca6b2f42020-01-24 11:31:21 -050073 const auto& caps = *fImage->context()->priv().caps();
Brian Salomon0857bef2021-01-13 15:54:04 -050074 auto fp = GrYUVtoRGBEffect::Make(fImage->fYUVAProxies,
75 samplerState,
76 caps,
77 textureMatrix,
78 subset,
79 domain);
Brian Salomon0ea33072020-07-14 10:43:42 -040080 if (fImage->fFromColorSpace) {
Brian Salomon0857bef2021-01-13 15:54:04 -050081 fp = GrColorSpaceXformEffect::Make(std::move(fp),
82 fImage->fFromColorSpace.get(), fImage->alphaType(),
83 fImage->colorSpace(), kPremul_SkAlphaType);
Brian Salomon5c4c61e2020-03-25 14:26:01 -040084 }
Brian Salomon0ea33072020-07-14 10:43:42 -040085 return fp;
86}
87
88std::unique_ptr<GrFragmentProcessor> GrYUVAImageTextureMaker::createBicubicFragmentProcessor(
89 const SkMatrix& textureMatrix,
90 const SkRect* subset,
91 const SkRect* domain,
92 GrSamplerState::WrapMode wrapX,
Mike Reed3867c702020-09-01 13:28:10 -040093 GrSamplerState::WrapMode wrapY,
94 SkImage::CubicResampler kernel) {
Brian Salomon0ea33072020-07-14 10:43:42 -040095 const auto& caps = *fImage->context()->priv().caps();
96 GrSamplerState samplerState(wrapX, wrapY, GrSamplerState::Filter::kNearest);
Brian Salomon0857bef2021-01-13 15:54:04 -050097 auto fp = GrYUVtoRGBEffect::Make(fImage->fYUVAProxies, samplerState, caps, SkMatrix::I(),
98 subset, domain);
Brian Salomon0ea33072020-07-14 10:43:42 -040099 fp = GrBicubicEffect::Make(std::move(fp),
100 fImage->alphaType(),
101 textureMatrix,
Brian Salomon0857bef2021-01-13 15:54:04 -0500102 kernel,
Brian Salomon0ea33072020-07-14 10:43:42 -0400103 GrBicubicEffect::Direction::kXY);
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400104 if (fImage->fFromColorSpace) {
John Stiles3e627622020-06-29 12:47:04 -0400105 fp = GrColorSpaceXformEffect::Make(std::move(fp),
106 fImage->fFromColorSpace.get(), fImage->alphaType(),
107 fImage->colorSpace(), kPremul_SkAlphaType);
Brian Osmane9560492019-02-05 17:00:03 -0500108 }
109 return fp;
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400110}