reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrYUVProvider.h" |
Kevin Lubick | f58e49f | 2019-04-08 12:14:21 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkRefCnt.h" |
| 11 | #include "include/core/SkYUVAIndex.h" |
| 12 | #include "include/private/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/core/SkAutoMalloc.h" |
| 14 | #include "src/core/SkCachedData.h" |
| 15 | #include "src/core/SkResourceCache.h" |
| 16 | #include "src/core/SkYUVPlanesCache.h" |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrBitmapTextureMaker.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrCaps.h" |
| 19 | #include "src/gpu/GrClip.h" |
| 20 | #include "src/gpu/GrColorSpaceXform.h" |
| 21 | #include "src/gpu/GrProxyProvider.h" |
| 22 | #include "src/gpu/GrRecordingContextPriv.h" |
| 23 | #include "src/gpu/GrRenderTargetContext.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 24 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/effects/GrYUVtoRGBEffect.h" |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 26 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 27 | sk_sp<SkCachedData> GrYUVProvider::getPlanes(SkYUVASizeInfo* size, |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 28 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount], |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 29 | SkYUVColorSpace* colorSpace, |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 30 | const void* constPlanes[SkYUVASizeInfo::kMaxCount]) { |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 31 | sk_sp<SkCachedData> data; |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 32 | SkYUVPlanesCache::Info yuvInfo; |
| 33 | data.reset(SkYUVPlanesCache::FindAndRef(this->onGetID(), &yuvInfo)); |
| 34 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 35 | void* planes[SkYUVASizeInfo::kMaxCount]; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 36 | |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 37 | if (data.get()) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 38 | planes[0] = (void*)data->data(); // we should always have at least one plane |
| 39 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 40 | for (int i = 1; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 41 | if (!yuvInfo.fSizeInfo.fWidthBytes[i]) { |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 42 | SkASSERT(!yuvInfo.fSizeInfo.fWidthBytes[i] && |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 43 | !yuvInfo.fSizeInfo.fSizes[i].fHeight); |
| 44 | planes[i] = nullptr; |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | planes[i] = (uint8_t*)planes[i-1] + (yuvInfo.fSizeInfo.fWidthBytes[i-1] * |
| 49 | yuvInfo.fSizeInfo.fSizes[i-1].fHeight); |
| 50 | } |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 51 | } else { |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 52 | // Fetch yuv plane sizes for memory allocation. |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 53 | if (!this->onQueryYUVA8(&yuvInfo.fSizeInfo, yuvInfo.fYUVAIndices, &yuvInfo.fColorSpace)) { |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 54 | return nullptr; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 57 | // Allocate the memory for YUVA |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 58 | size_t totalSize(0); |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 59 | for (int i = 0; i < SkYUVASizeInfo::kMaxCount; i++) { |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 60 | SkASSERT((yuvInfo.fSizeInfo.fWidthBytes[i] && yuvInfo.fSizeInfo.fSizes[i].fHeight) || |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 61 | (!yuvInfo.fSizeInfo.fWidthBytes[i] && !yuvInfo.fSizeInfo.fSizes[i].fHeight)); |
| 62 | |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 63 | totalSize += yuvInfo.fSizeInfo.fWidthBytes[i] * yuvInfo.fSizeInfo.fSizes[i].fHeight; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 64 | } |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 65 | |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 66 | data.reset(SkResourceCache::NewCachedData(totalSize)); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 67 | |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 68 | planes[0] = data->writable_data(); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 69 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 70 | for (int i = 1; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 71 | if (!yuvInfo.fSizeInfo.fWidthBytes[i]) { |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 72 | SkASSERT(!yuvInfo.fSizeInfo.fWidthBytes[i] && |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 73 | !yuvInfo.fSizeInfo.fSizes[i].fHeight); |
| 74 | planes[i] = nullptr; |
| 75 | continue; |
| 76 | } |
| 77 | |
| 78 | planes[i] = (uint8_t*)planes[i-1] + (yuvInfo.fSizeInfo.fWidthBytes[i-1] * |
| 79 | yuvInfo.fSizeInfo.fSizes[i-1].fHeight); |
| 80 | } |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 81 | |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 82 | // Get the YUV planes. |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 83 | if (!this->onGetYUVA8Planes(yuvInfo.fSizeInfo, yuvInfo.fYUVAIndices, planes)) { |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 84 | return nullptr; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 87 | // Decoding is done, cache the resulting YUV planes |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 88 | SkYUVPlanesCache::Add(this->onGetID(), data.get(), &yuvInfo); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 89 | } |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 90 | |
| 91 | *size = yuvInfo.fSizeInfo; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 92 | memcpy(yuvaIndices, yuvInfo.fYUVAIndices, sizeof(yuvInfo.fYUVAIndices)); |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 93 | *colorSpace = yuvInfo.fColorSpace; |
| 94 | constPlanes[0] = planes[0]; |
| 95 | constPlanes[1] = planes[1]; |
| 96 | constPlanes[2] = planes[2]; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 97 | constPlanes[3] = planes[3]; |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 98 | return data; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 101 | void GrYUVProvider::YUVGen_DataReleaseProc(void*, void* data) { |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 102 | SkCachedData* cachedData = static_cast<SkCachedData*>(data); |
| 103 | SkASSERT(cachedData); |
| 104 | cachedData->unref(); |
| 105 | } |
| 106 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 107 | GrSurfaceProxyView GrYUVProvider::refAsTextureProxyView(GrRecordingContext* ctx, |
| 108 | const GrSurfaceDesc& desc, |
| 109 | GrColorType colorType, |
| 110 | SkColorSpace* srcColorSpace, |
| 111 | SkColorSpace* dstColorSpace) { |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 112 | SkYUVASizeInfo yuvSizeInfo; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 113 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount]; |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 114 | SkYUVColorSpace yuvColorSpace; |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 115 | const void* planes[SkYUVASizeInfo::kMaxCount]; |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 116 | |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 117 | sk_sp<SkCachedData> dataStorage = this->getPlanes(&yuvSizeInfo, yuvaIndices, |
| 118 | &yuvColorSpace, planes); |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 119 | if (!dataStorage) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 120 | return {}; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 123 | sk_sp<GrTextureProxy> yuvTextureProxies[SkYUVASizeInfo::kMaxCount]; |
| 124 | for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 125 | if (yuvSizeInfo.fSizes[i].isEmpty()) { |
| 126 | SkASSERT(!yuvSizeInfo.fWidthBytes[i]); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 127 | continue; |
| 128 | } |
| 129 | |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 130 | int componentWidth = yuvSizeInfo.fSizes[i].fWidth; |
| 131 | int componentHeight = yuvSizeInfo.fSizes[i].fHeight; |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 132 | // If the sizes of the components are not all the same we choose to create exact-match |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 133 | // textures for the smaller ones rather than add a texture domain to the draw. |
| 134 | // TODO: revisit this decision to improve texture reuse? |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 135 | SkBackingFit fit = |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 136 | (componentWidth != yuvSizeInfo.fSizes[0].fWidth) || |
| 137 | (componentHeight != yuvSizeInfo.fSizes[0].fHeight) |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 138 | ? SkBackingFit::kExact : SkBackingFit::kApprox; |
| 139 | |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 140 | SkImageInfo imageInfo = SkImageInfo::MakeA8(componentWidth, componentHeight); |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 141 | SkCachedData* dataStoragePtr = dataStorage.get(); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 142 | // We grab a ref to cached yuv data. When the SkBitmap we create below goes away it will |
| 143 | // call the YUVGen_DataReleaseProc which will release this ref. |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 144 | // DDL TODO: Currently we end up creating a lazy proxy that will hold onto a ref to the |
| 145 | // SkImage in its lambda. This means that we'll keep the ref on the YUV data around for the |
| 146 | // life time of the proxy and not just upload. For non-DDL draws we should look into |
| 147 | // releasing this SkImage after uploads (by deleting the lambda after instantiation). |
| 148 | dataStoragePtr->ref(); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 149 | SkBitmap bitmap; |
| 150 | SkAssertResult(bitmap.installPixels(imageInfo, const_cast<void*>(planes[i]), |
| 151 | yuvSizeInfo.fWidthBytes[i], |
| 152 | YUVGen_DataReleaseProc, dataStoragePtr)); |
| 153 | bitmap.setImmutable(); |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 154 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 155 | GrBitmapTextureMaker maker(ctx, bitmap, GrBitmapTextureMaker::Cached::kNo, fit); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 156 | auto [view, grCT] = maker.refTextureProxyView(GrMipMapped::kNo); |
| 157 | yuvTextureProxies[i] = view.asTextureProxyRef(); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 158 | |
Greg Daniel | 8ec605e | 2020-01-08 11:14:15 -0500 | [diff] [blame] | 159 | if (!yuvTextureProxies[i]) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 160 | return {}; |
Greg Daniel | 8ec605e | 2020-01-08 11:14:15 -0500 | [diff] [blame] | 161 | } |
Robert Phillips | c1b6066 | 2018-06-26 10:20:08 -0400 | [diff] [blame] | 162 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 163 | SkASSERT(yuvTextureProxies[i]->dimensions() == yuvSizeInfo.fSizes[i]); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 166 | // TODO: investigate preallocating mip maps here |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 167 | auto renderTargetContext = GrRenderTargetContext::Make( |
| 168 | ctx, colorType, nullptr, SkBackingFit::kExact, {desc.fWidth, desc.fHeight}, 1, |
| 169 | GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 170 | if (!renderTargetContext) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 171 | return {}; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 174 | GrPaint paint; |
Brian Salomon | ca6b2f4 | 2020-01-24 11:31:21 -0500 | [diff] [blame] | 175 | const auto& caps = *ctx->priv().caps(); |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 176 | auto yuvToRgbProcessor = GrYUVtoRGBEffect::Make(yuvTextureProxies, yuvaIndices, yuvColorSpace, |
Brian Salomon | ca6b2f4 | 2020-01-24 11:31:21 -0500 | [diff] [blame] | 177 | GrSamplerState::Filter::kNearest, caps); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 178 | paint.addColorFragmentProcessor(std::move(yuvToRgbProcessor)); |
brianosman | 717abfd | 2016-05-11 11:43:35 -0700 | [diff] [blame] | 179 | |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 180 | // If the caller expects the pixels in a different color space than the one from the image, |
| 181 | // apply a color conversion to do this. |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 182 | std::unique_ptr<GrFragmentProcessor> colorConversionProcessor = |
Brian Osman | 15f0f29 | 2018-10-01 14:14:46 -0400 | [diff] [blame] | 183 | GrColorSpaceXformEffect::Make(srcColorSpace, kOpaque_SkAlphaType, |
| 184 | dstColorSpace, kOpaque_SkAlphaType); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 185 | if (colorConversionProcessor) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 186 | paint.addColorFragmentProcessor(std::move(colorConversionProcessor)); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 187 | } |
| 188 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 189 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 190 | const SkRect r = SkRect::MakeIWH(yuvSizeInfo.fSizes[0].fWidth, |
| 191 | yuvSizeInfo.fSizes[0].fHeight); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 192 | |
Brian Osman | c337a63 | 2018-11-30 10:39:32 -0500 | [diff] [blame] | 193 | SkMatrix m = SkEncodedOriginToMatrix(yuvSizeInfo.fOrigin, r.width(), r.height()); |
| 194 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, m, r); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 195 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 196 | SkASSERT(renderTargetContext->asTextureProxy()); |
| 197 | return renderTargetContext->readSurfaceView(); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 198 | } |