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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrColorSpaceXform.h" |
| 20 | #include "src/gpu/GrProxyProvider.h" |
| 21 | #include "src/gpu/GrRecordingContextPriv.h" |
| 22 | #include "src/gpu/GrRenderTargetContext.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 23 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/effects/GrYUVtoRGBEffect.h" |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 25 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 26 | sk_sp<SkCachedData> GrYUVProvider::getPlanes(SkYUVASizeInfo* size, |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 27 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount], |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 28 | SkYUVColorSpace* colorSpace, |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 29 | const void* constPlanes[SkYUVASizeInfo::kMaxCount]) { |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 30 | sk_sp<SkCachedData> data; |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 31 | SkYUVPlanesCache::Info yuvInfo; |
| 32 | data.reset(SkYUVPlanesCache::FindAndRef(this->onGetID(), &yuvInfo)); |
| 33 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 34 | void* planes[SkYUVASizeInfo::kMaxCount]; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 35 | |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 36 | if (data.get()) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 37 | planes[0] = (void*)data->data(); // we should always have at least one plane |
| 38 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 39 | for (int i = 1; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 40 | if (!yuvInfo.fSizeInfo.fWidthBytes[i]) { |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 41 | SkASSERT(!yuvInfo.fSizeInfo.fWidthBytes[i] && |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 42 | !yuvInfo.fSizeInfo.fSizes[i].fHeight); |
| 43 | planes[i] = nullptr; |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | planes[i] = (uint8_t*)planes[i-1] + (yuvInfo.fSizeInfo.fWidthBytes[i-1] * |
| 48 | yuvInfo.fSizeInfo.fSizes[i-1].fHeight); |
| 49 | } |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 50 | } else { |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 51 | // Fetch yuv plane sizes for memory allocation. |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 52 | if (!this->onQueryYUVA8(&yuvInfo.fSizeInfo, yuvInfo.fYUVAIndices, &yuvInfo.fColorSpace)) { |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 53 | return nullptr; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 56 | // Allocate the memory for YUVA |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 57 | size_t totalSize(0); |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 58 | for (int i = 0; i < SkYUVASizeInfo::kMaxCount; i++) { |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 59 | SkASSERT((yuvInfo.fSizeInfo.fWidthBytes[i] && yuvInfo.fSizeInfo.fSizes[i].fHeight) || |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 60 | (!yuvInfo.fSizeInfo.fWidthBytes[i] && !yuvInfo.fSizeInfo.fSizes[i].fHeight)); |
| 61 | |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 62 | totalSize += yuvInfo.fSizeInfo.fWidthBytes[i] * yuvInfo.fSizeInfo.fSizes[i].fHeight; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 63 | } |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 64 | |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 65 | data.reset(SkResourceCache::NewCachedData(totalSize)); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 66 | |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 67 | planes[0] = data->writable_data(); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 68 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 69 | for (int i = 1; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 70 | if (!yuvInfo.fSizeInfo.fWidthBytes[i]) { |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 71 | SkASSERT(!yuvInfo.fSizeInfo.fWidthBytes[i] && |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 72 | !yuvInfo.fSizeInfo.fSizes[i].fHeight); |
| 73 | planes[i] = nullptr; |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | planes[i] = (uint8_t*)planes[i-1] + (yuvInfo.fSizeInfo.fWidthBytes[i-1] * |
| 78 | yuvInfo.fSizeInfo.fSizes[i-1].fHeight); |
| 79 | } |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 80 | |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 81 | // Get the YUV planes. |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 82 | if (!this->onGetYUVA8Planes(yuvInfo.fSizeInfo, yuvInfo.fYUVAIndices, planes)) { |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 83 | return nullptr; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 86 | // Decoding is done, cache the resulting YUV planes |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 87 | SkYUVPlanesCache::Add(this->onGetID(), data.get(), &yuvInfo); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 88 | } |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 89 | |
| 90 | *size = yuvInfo.fSizeInfo; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 91 | memcpy(yuvaIndices, yuvInfo.fYUVAIndices, sizeof(yuvInfo.fYUVAIndices)); |
Robert Phillips | b4a8eac | 2018-09-21 08:26:33 -0400 | [diff] [blame] | 92 | *colorSpace = yuvInfo.fColorSpace; |
| 93 | constPlanes[0] = planes[0]; |
| 94 | constPlanes[1] = planes[1]; |
| 95 | constPlanes[2] = planes[2]; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 96 | constPlanes[3] = planes[3]; |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 97 | return data; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 100 | void GrYUVProvider::YUVGen_DataReleaseProc(void*, void* data) { |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 101 | SkCachedData* cachedData = static_cast<SkCachedData*>(data); |
| 102 | SkASSERT(cachedData); |
| 103 | cachedData->unref(); |
| 104 | } |
| 105 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 106 | GrSurfaceProxyView GrYUVProvider::refAsTextureProxyView(GrRecordingContext* ctx, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 107 | SkISize dimensions, |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 108 | GrColorType colorType, |
| 109 | SkColorSpace* srcColorSpace, |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 110 | SkColorSpace* dstColorSpace, |
| 111 | SkBudgeted budgeted) { |
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 | |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 123 | GrSurfaceProxyView yuvViews[SkYUVASizeInfo::kMaxCount]; |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 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 | |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 155 | GrBitmapTextureMaker maker(ctx, bitmap, fit); |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 156 | yuvViews[i] = maker.view(GrMipMapped::kNo); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 157 | |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 158 | if (!yuvViews[i]) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 159 | return {}; |
Greg Daniel | 8ec605e | 2020-01-08 11:14:15 -0500 | [diff] [blame] | 160 | } |
Robert Phillips | c1b6066 | 2018-06-26 10:20:08 -0400 | [diff] [blame] | 161 | |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 162 | SkASSERT(yuvViews[i].proxy()->dimensions() == yuvSizeInfo.fSizes[i]); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 165 | // TODO: investigate preallocating mip maps here |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 166 | auto renderTargetContext = GrRenderTargetContext::Make( |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 167 | ctx, colorType, nullptr, SkBackingFit::kExact, dimensions, 1, GrMipMapped::kNo, |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 168 | GrProtected::kNo, kTopLeft_GrSurfaceOrigin, budgeted); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 169 | if (!renderTargetContext) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 170 | return {}; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 173 | GrPaint paint; |
Brian Salomon | ca6b2f4 | 2020-01-24 11:31:21 -0500 | [diff] [blame] | 174 | const auto& caps = *ctx->priv().caps(); |
Greg Daniel | c767209 | 2020-02-06 14:32:54 -0500 | [diff] [blame] | 175 | auto yuvToRgbProcessor = GrYUVtoRGBEffect::Make(yuvViews, yuvaIndices, yuvColorSpace, |
Brian Salomon | ca6b2f4 | 2020-01-24 11:31:21 -0500 | [diff] [blame] | 176 | GrSamplerState::Filter::kNearest, caps); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 177 | paint.addColorFragmentProcessor(std::move(yuvToRgbProcessor)); |
brianosman | 717abfd | 2016-05-11 11:43:35 -0700 | [diff] [blame] | 178 | |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 179 | // If the caller expects the pixels in a different color space than the one from the image, |
| 180 | // apply a color conversion to do this. |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 181 | std::unique_ptr<GrFragmentProcessor> colorConversionProcessor = |
Brian Osman | 15f0f29 | 2018-10-01 14:14:46 -0400 | [diff] [blame] | 182 | GrColorSpaceXformEffect::Make(srcColorSpace, kOpaque_SkAlphaType, |
| 183 | dstColorSpace, kOpaque_SkAlphaType); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 184 | if (colorConversionProcessor) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 185 | paint.addColorFragmentProcessor(std::move(colorConversionProcessor)); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 186 | } |
| 187 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 188 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 189 | const SkRect r = SkRect::MakeIWH(yuvSizeInfo.fSizes[0].fWidth, |
| 190 | yuvSizeInfo.fSizes[0].fHeight); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 191 | |
Brian Osman | c337a63 | 2018-11-30 10:39:32 -0500 | [diff] [blame] | 192 | SkMatrix m = SkEncodedOriginToMatrix(yuvSizeInfo.fOrigin, r.width(), r.height()); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 193 | renderTargetContext->drawRect(nullptr, std::move(paint), GrAA::kNo, m, r); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 194 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 195 | SkASSERT(renderTargetContext->asTextureProxy()); |
| 196 | return renderTargetContext->readSurfaceView(); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 197 | } |