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