Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrContext.h" | 8 #include "GrContext.h" |
| 9 #include "GrDrawContext.h" | 9 #include "GrDrawContext.h" |
| 10 #include "GrYUVProvider.h" | 10 #include "GrYUVProvider.h" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 } | 33 } |
| 34 | 34 |
| 35 bool YUVScoper::init(GrYUVProvider* provider, SkYUVPlanesCache::Info* yuvInfo, v oid* planes[3], | 35 bool YUVScoper::init(GrYUVProvider* provider, SkYUVPlanesCache::Info* yuvInfo, v oid* planes[3], |
| 36 bool useCache) { | 36 bool useCache) { |
| 37 if (useCache) { | 37 if (useCache) { |
| 38 fCachedData.reset(SkYUVPlanesCache::FindAndRef(provider->onGetID(), yuvI nfo)); | 38 fCachedData.reset(SkYUVPlanesCache::FindAndRef(provider->onGetID(), yuvI nfo)); |
| 39 } | 39 } |
| 40 | 40 |
| 41 if (fCachedData.get()) { | 41 if (fCachedData.get()) { |
| 42 planes[0] = (void*)fCachedData->data(); | 42 planes[0] = (void*)fCachedData->data(); |
| 43 planes[1] = (uint8_t*)planes[0] + yuvInfo->fSizeInMemory[0]; | 43 planes[1] = (uint8_t*)planes[0] + |
| 44 planes[2] = (uint8_t*)planes[1] + yuvInfo->fSizeInMemory[1]; | 44 (yuvInfo->fSizeInfo.fYWidthBytes * yuvInfo->fSizeInfo.fYSize.fHe ight); |
| 45 planes[2] = (uint8_t*)planes[1] + | |
| 46 (yuvInfo->fSizeInfo.fUWidthBytes * yuvInfo->fSizeInfo.fUSize.fHe ight); | |
| 45 } else { | 47 } else { |
| 46 // Fetch yuv plane sizes for memory allocation. Here, width and height c an be | 48 // Fetch yuv plane sizes for memory allocation. |
| 47 // rounded up to JPEG block size and be larger than the image's width an d height. | 49 if (!provider->onQueryYUV8(&yuvInfo->fSizeInfo, &yuvInfo->fColorSpace)) { |
| 48 if (!provider->onGetYUVSizes(yuvInfo->fSize)) { | |
| 49 return false; | 50 return false; |
| 50 } | 51 } |
| 51 | 52 |
| 52 // Allocate the memory for YUV | 53 // Allocate the memory for YUV |
| 53 size_t totalSize(0); | 54 size_t totalSize(0); |
| 54 for (int i = 0; i < GrYUVProvider::kPlaneCount; ++i) { | 55 totalSize += yuvInfo->fSizeInfo.fYWidthBytes * yuvInfo->fSizeInfo.fYSize .fHeight; |
| 55 yuvInfo->fRowBytes[i] = yuvInfo->fSize[i].fWidth; // we assume snug fit: rb == width | 56 totalSize += yuvInfo->fSizeInfo.fUWidthBytes * yuvInfo->fSizeInfo.fUSize .fHeight; |
| 56 yuvInfo->fSizeInMemory[i] = yuvInfo->fRowBytes[i] * yuvInfo->fSize[i ].fHeight; | 57 totalSize += yuvInfo->fSizeInfo.fVWidthBytes * yuvInfo->fSizeInfo.fVSize .fHeight; |
| 57 totalSize += yuvInfo->fSizeInMemory[i]; | |
| 58 } | |
| 59 if (useCache) { | 58 if (useCache) { |
| 60 fCachedData.reset(SkResourceCache::NewCachedData(totalSize)); | 59 fCachedData.reset(SkResourceCache::NewCachedData(totalSize)); |
| 61 planes[0] = fCachedData->writable_data(); | 60 planes[0] = fCachedData->writable_data(); |
| 62 } else { | 61 } else { |
| 63 fStorage.reset(totalSize); | 62 fStorage.reset(totalSize); |
| 64 planes[0] = fStorage.get(); | 63 planes[0] = fStorage.get(); |
| 65 } | 64 } |
| 66 planes[1] = (uint8_t*)planes[0] + yuvInfo->fSizeInMemory[0]; | 65 planes[1] = (uint8_t*)planes[0] + |
| 67 planes[2] = (uint8_t*)planes[1] + yuvInfo->fSizeInMemory[1]; | 66 (yuvInfo->fSizeInfo.fYWidthBytes * yuvInfo->fSizeInfo.fYSize.fHe ight); |
| 67 planes[2] = (uint8_t*)planes[1] + | |
| 68 (yuvInfo->fSizeInfo.fUWidthBytes * yuvInfo->fSizeInfo.fUSize.fHe ight); | |
| 68 | 69 |
| 69 // Get the YUV planes and update plane sizes to actual image size | 70 // Get the YUV planes. |
| 70 if (!provider->onGetYUVPlanes(yuvInfo->fSize, planes, yuvInfo->fRowBytes , | 71 if (!provider->onGetYUV8Planes(yuvInfo->fSizeInfo, planes)) { |
| 71 &yuvInfo->fColorSpace)) { | |
| 72 return false; | 72 return false; |
| 73 } | 73 } |
| 74 | 74 |
| 75 if (useCache) { | 75 if (useCache) { |
| 76 // Decoding is done, cache the resulting YUV planes | 76 // Decoding is done, cache the resulting YUV planes |
| 77 SkYUVPlanesCache::Add(provider->onGetID(), fCachedData, yuvInfo); | 77 SkYUVPlanesCache::Add(provider->onGetID(), fCachedData, yuvInfo); |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 return true; | 80 return true; |
| 81 } | 81 } |
| 82 | 82 |
| 83 GrTexture* GrYUVProvider::refAsTexture(GrContext* ctx, const GrSurfaceDesc& desc , bool useCache) { | 83 GrTexture* GrYUVProvider::refAsTexture(GrContext* ctx, const GrSurfaceDesc& desc , bool useCache) { |
| 84 SkYUVPlanesCache::Info yuvInfo; | 84 SkYUVPlanesCache::Info yuvInfo; |
| 85 void* planes[3]; | 85 void* planes[3]; |
| 86 YUVScoper scoper; | 86 YUVScoper scoper; |
| 87 if (!scoper.init(this, &yuvInfo, planes, useCache)) { | 87 if (!scoper.init(this, &yuvInfo, planes, useCache)) { |
| 88 return nullptr; | 88 return nullptr; |
| 89 } | 89 } |
| 90 | 90 |
| 91 GrSurfaceDesc yuvDesc; | 91 GrSurfaceDesc yDesc, uDesc, vDesc; |
| 92 yuvDesc.fConfig = kAlpha_8_GrPixelConfig; | 92 yDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 93 SkAutoTUnref<GrTexture> yuvTextures[3]; | 93 uDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 94 for (int i = 0; i < 3; ++i) { | 94 vDesc.fConfig = kAlpha_8_GrPixelConfig; |
|
msarett
2016/02/18 23:00:02
The fact that I needed to unroll this loop makes a
| |
| 95 yuvDesc.fWidth = yuvInfo.fSize[i].fWidth; | 95 SkAutoTUnref<GrTexture> yuvTextures[GrYUVProvider::kPlaneCount]; |
| 96 yuvDesc.fHeight = yuvInfo.fSize[i].fHeight; | 96 |
| 97 // TODO: why do we need this check? | 97 yDesc.fWidth = yuvInfo.fSizeInfo.fYSize.fWidth; |
| 98 bool needsExactTexture = (yuvDesc.fWidth != yuvInfo.fSize[0].fWidth) || | 98 yDesc.fHeight = yuvInfo.fSizeInfo.fYSize.fHeight; |
| 99 (yuvDesc.fHeight != yuvInfo.fSize[0].fHeight); | 99 yuvTextures[GrYUVProvider::kY_Index].reset(ctx->textureProvider()->createTex ture(yDesc, true)); |
| 100 if (needsExactTexture) { | 100 if (!yuvTextures[GrYUVProvider::kY_Index] || |
| 101 yuvTextures[i].reset(ctx->textureProvider()->createTexture(yuvDesc, true)); | 101 !yuvTextures[GrYUVProvider::kY_Index]->writePixels(0, 0, yDesc.fWidth, y Desc.fHeight, |
| 102 } else { | 102 yDesc.fConfig, planes[GrYUVProvider::kY_Index], yuvInfo.fSizeInf o.fYWidthBytes)) { |
| 103 yuvTextures[i].reset(ctx->textureProvider()->createApproxTexture(yuv Desc)); | 103 return nullptr; |
| 104 } | 104 } |
| 105 if (!yuvTextures[i] || | 105 |
| 106 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight, | 106 uDesc.fWidth = yuvInfo.fSizeInfo.fUSize.fWidth; |
| 107 yuvDesc.fConfig, planes[i], yuvInfo.fRo wBytes[i])) { | 107 uDesc.fHeight = yuvInfo.fSizeInfo.fUSize.fHeight; |
| 108 return nullptr; | 108 bool needsExactTexture = (uDesc.fWidth != yDesc.fWidth) || |
| 109 } | 109 (uDesc.fHeight != yDesc.fHeight); |
| 110 if (needsExactTexture) { | |
| 111 yuvTextures[GrYUVProvider::kU_Index].reset( | |
| 112 ctx->textureProvider()->createTexture(uDesc, true)); | |
| 113 } else { | |
| 114 yuvTextures[GrYUVProvider::kU_Index].reset( | |
| 115 ctx->textureProvider()->createApproxTexture(uDesc)); | |
| 116 } | |
| 117 if (!yuvTextures[GrYUVProvider::kU_Index] || | |
| 118 !yuvTextures[GrYUVProvider::kU_Index]->writePixels(0, 0, uDesc.fWidth, u Desc.fHeight, | |
| 119 uDesc.fConfig, planes[GrYUVProvider::kU_Index], yuvInfo.fSizeInf o.fUWidthBytes)) { | |
| 120 return nullptr; | |
| 121 } | |
| 122 | |
| 123 vDesc.fWidth = yuvInfo.fSizeInfo.fVSize.fWidth; | |
| 124 vDesc.fHeight = yuvInfo.fSizeInfo.fVSize.fHeight; | |
| 125 needsExactTexture = (vDesc.fWidth != vDesc.fWidth) || | |
| 126 (vDesc.fHeight != vDesc.fHeight); | |
| 127 if (needsExactTexture) { | |
| 128 yuvTextures[GrYUVProvider::kV_Index].reset( | |
| 129 ctx->textureProvider()->createTexture(vDesc, true)); | |
| 130 } else { | |
| 131 yuvTextures[GrYUVProvider::kV_Index].reset( | |
| 132 ctx->textureProvider()->createApproxTexture(vDesc)); | |
| 133 } | |
| 134 if (!yuvTextures[GrYUVProvider::kV_Index] || | |
| 135 !yuvTextures[GrYUVProvider::kV_Index]->writePixels(0, 0, vDesc.fWidth, v Desc.fHeight, | |
| 136 vDesc.fConfig, planes[GrYUVProvider::kV_Index], yuvInfo.fSizeInf o.fVWidthBytes)) { | |
| 137 return nullptr; | |
| 110 } | 138 } |
| 111 | 139 |
| 112 GrSurfaceDesc rtDesc = desc; | 140 GrSurfaceDesc rtDesc = desc; |
| 113 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag; | 141 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag; |
| 114 | 142 |
| 115 SkAutoTUnref<GrTexture> result(ctx->textureProvider()->createTexture(rtDesc, true, nullptr, 0)); | 143 SkAutoTUnref<GrTexture> result(ctx->textureProvider()->createTexture(rtDesc, true, nullptr, 0)); |
| 116 if (!result) { | 144 if (!result) { |
| 117 return nullptr; | 145 return nullptr; |
| 118 } | 146 } |
| 119 | 147 |
| 120 GrRenderTarget* renderTarget = result->asRenderTarget(); | 148 GrRenderTarget* renderTarget = result->asRenderTarget(); |
| 121 SkASSERT(renderTarget); | 149 SkASSERT(renderTarget); |
| 122 | 150 |
| 123 GrPaint paint; | 151 GrPaint paint; |
| 124 SkAutoTUnref<const GrFragmentProcessor> yuvToRgbProcessor( | 152 SkAutoTUnref<const GrFragmentProcessor> yuvToRgbProcessor( |
| 125 GrYUVEffect::CreateYUVToRGB(yuvTextures[ 0], | 153 GrYUVEffect::CreateYUVToRGB(yuvTextures[ 0], |
| 126 yuvTextures[ 1], | 154 yuvTextures[ 1], |
| 127 yuvTextures[ 2], | 155 yuvTextures[ 2], |
| 128 yuvInfo.fSiz e, | 156 &yuvInfo.fSi zeInfo.fYSize, |
|
msarett
2016/02/18 23:00:02
Nobody look here. This line isn't hacky at all. :
| |
| 129 yuvInfo.fCol orSpace)); | 157 yuvInfo.fCol orSpace)); |
| 130 paint.addColorFragmentProcessor(yuvToRgbProcessor); | 158 paint.addColorFragmentProcessor(yuvToRgbProcessor); |
| 131 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); | 159 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
| 132 const SkRect r = SkRect::MakeIWH(yuvInfo.fSize[0].fWidth, yuvInfo.fSize[0].f Height); | 160 const SkRect r = SkRect::MakeIWH(yuvInfo.fSizeInfo.fYSize.fWidth, |
| 161 yuvInfo.fSizeInfo.fYSize.fHeight); | |
| 133 | 162 |
| 134 SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext(renderTarget)); | 163 SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext(renderTarget)); |
| 135 if (!drawContext) { | 164 if (!drawContext) { |
| 136 return nullptr; | 165 return nullptr; |
| 137 } | 166 } |
| 138 | 167 |
| 139 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), r); | 168 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), r); |
| 140 | 169 |
| 141 return result.detach(); | 170 return result.detach(); |
| 142 } | 171 } |
| 143 | 172 |
| OLD | NEW |