blob: 845bea292cee20cfd5c677a1d756f470141f4581 [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"
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 Salomon7e67dca2020-07-21 09:27:25 -040049GrSurfaceProxyView GrYUVAImageTextureMaker::refOriginalTextureProxyView(GrMipmapped mipMapped) {
50 if (mipMapped == GrMipmapped::kYes) {
Greg Daniel37c127f2020-02-05 10:37:27 -050051 return fImage->refMippedView(this->context());
Jim Van Verth803a5022018-11-05 15:55:53 -050052 } else {
Greg Daniel37c127f2020-02-05 10:37:27 -050053 if (const GrSurfaceProxyView* view = fImage->view(this->context())) {
54 return *view;
55 } else {
56 return {};
57 }
Jim Van Verth7da46762018-11-05 14:24:23 -050058 }
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040059}
60
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040061std::unique_ptr<GrFragmentProcessor> GrYUVAImageTextureMaker::createFragmentProcessor(
Brian Salomon777e1462020-02-28 21:10:31 -050062 const SkMatrix& textureMatrix,
Brian Salomon0ea33072020-07-14 10:43:42 -040063 const SkRect* subset,
64 const SkRect* domain,
65 GrSamplerState samplerState) {
Brian Salomon5c4c61e2020-03-25 14:26:01 -040066 // Check whether it's already been flattened.
67 if (fImage->fRGBView.proxy()) {
Brian Salomon0ea33072020-07-14 10:43:42 -040068 return this->INHERITED::createFragmentProcessor(textureMatrix, subset, domain,
69 samplerState);
Jim Van Verth30e0d7f2018-11-02 13:36:42 -040070 }
71
Jim Van Verthf542cab2018-11-07 12:08:21 -050072 // Check to see if the client has given us pre-mipped textures or we can generate them
Brian Salomona3b02f52020-07-15 16:02:01 -040073 // If not, fall back to linear filtering. Also fall back to linear filtering when a domain is
74 // requested.
Brian Salomon0ea33072020-07-14 10:43:42 -040075 if (samplerState.filter() == GrSamplerState::Filter::kMipMap &&
76 (subset || !fImage->setupMipmapsForPlanes(this->context()))) {
Brian Salomona3b02f52020-07-15 16:02:01 -040077 samplerState.setFilterMode(GrSamplerState::Filter::kLinear);
Michael Ludwiga6a84002019-04-12 15:03:02 -040078 }
79
Brian Salomonca6b2f42020-01-24 11:31:21 -050080 const auto& caps = *fImage->context()->priv().caps();
Greg Danielc7672092020-02-06 14:32:54 -050081 auto fp = GrYUVtoRGBEffect::Make(fImage->fViews, fImage->fYUVAIndices, fImage->fYUVColorSpace,
Brian Salomon0ea33072020-07-14 10:43:42 -040082 samplerState, caps, textureMatrix, subset, domain);
83 if (fImage->fFromColorSpace) {
84 fp = GrColorSpaceXformEffect::Make(std::move(fp), fImage->fFromColorSpace.get(),
85 fImage->alphaType(), fImage->colorSpace(),
86 kPremul_SkAlphaType);
Brian Salomon5c4c61e2020-03-25 14:26:01 -040087 }
Brian Salomon0ea33072020-07-14 10:43:42 -040088 return fp;
89}
90
91std::unique_ptr<GrFragmentProcessor> GrYUVAImageTextureMaker::createBicubicFragmentProcessor(
92 const SkMatrix& textureMatrix,
93 const SkRect* subset,
94 const SkRect* domain,
95 GrSamplerState::WrapMode wrapX,
96 GrSamplerState::WrapMode wrapY) {
97 const auto& caps = *fImage->context()->priv().caps();
98 GrSamplerState samplerState(wrapX, wrapY, GrSamplerState::Filter::kNearest);
99 auto fp = GrYUVtoRGBEffect::Make(fImage->fViews, fImage->fYUVAIndices, fImage->fYUVColorSpace,
100 samplerState, caps, SkMatrix::I(), subset, domain);
101 fp = GrBicubicEffect::Make(std::move(fp),
102 fImage->alphaType(),
103 textureMatrix,
104 GrBicubicEffect::Kernel::kMitchell,
105 GrBicubicEffect::Direction::kXY);
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400106 if (fImage->fFromColorSpace) {
John Stiles3e627622020-06-29 12:47:04 -0400107 fp = GrColorSpaceXformEffect::Make(std::move(fp),
108 fImage->fFromColorSpace.get(), fImage->alphaType(),
109 fImage->colorSpace(), kPremul_SkAlphaType);
Brian Osmane9560492019-02-05 17:00:03 -0500110 }
111 return fp;
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400112}