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] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVS
izeInfo::kY] * |
44 planes[2] = (uint8_t*)planes[1] + yuvInfo->fSizeInMemory[1]; | 44 yuvInfo->fSizeInfo.fSizes[SkYUVSizeIn
fo::kY].fHeight); |
| 45 planes[2] = (uint8_t*)planes[1] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVS
izeInfo::kU] * |
| 46 yuvInfo->fSizeInfo.fSizes[SkYUVSizeIn
fo::kU].fHeight); |
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 for (int i = 0; i < 3; i++) { |
55 yuvInfo->fRowBytes[i] = yuvInfo->fSize[i].fWidth; // we assume snug
fit: rb == width | 56 totalSize += yuvInfo->fSizeInfo.fWidthBytes[i] * yuvInfo->fSizeInfo.
fSizes[i].fHeight; |
56 yuvInfo->fSizeInMemory[i] = yuvInfo->fRowBytes[i] * yuvInfo->fSize[i
].fHeight; | |
57 totalSize += yuvInfo->fSizeInMemory[i]; | |
58 } | 57 } |
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] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVS
izeInfo::kY] * |
67 planes[2] = (uint8_t*)planes[1] + yuvInfo->fSizeInMemory[1]; | 66 yuvInfo->fSizeInfo.fSizes[SkYUVSizeIn
fo::kY].fHeight); |
| 67 planes[2] = (uint8_t*)planes[1] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVS
izeInfo::kU] * |
| 68 yuvInfo->fSizeInfo.fSizes[SkYUVSizeIn
fo::kU].fHeight); |
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 yuvDesc; |
92 yuvDesc.fConfig = kAlpha_8_GrPixelConfig; | 92 yuvDesc.fConfig = kAlpha_8_GrPixelConfig; |
93 SkAutoTUnref<GrTexture> yuvTextures[3]; | 93 SkAutoTUnref<GrTexture> yuvTextures[3]; |
94 for (int i = 0; i < 3; ++i) { | 94 for (int i = 0; i < 3; i++) { |
95 yuvDesc.fWidth = yuvInfo.fSize[i].fWidth; | 95 yuvDesc.fWidth = yuvInfo.fSizeInfo.fSizes[i].fWidth; |
96 yuvDesc.fHeight = yuvInfo.fSize[i].fHeight; | 96 yuvDesc.fHeight = yuvInfo.fSizeInfo.fSizes[i].fHeight; |
97 // TODO: why do we need this check? | 97 // TODO: why do we need this check? |
98 bool needsExactTexture = (yuvDesc.fWidth != yuvInfo.fSize[0].fWidth) || | 98 bool needsExactTexture = |
99 (yuvDesc.fHeight != yuvInfo.fSize[0].fHeight); | 99 (yuvDesc.fWidth != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].
fWidth) || |
| 100 (yuvDesc.fHeight != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].
fHeight); |
100 if (needsExactTexture) { | 101 if (needsExactTexture) { |
101 yuvTextures[i].reset(ctx->textureProvider()->createTexture(yuvDesc,
SkBudgeted::kYes)); | 102 yuvTextures[i].reset(ctx->textureProvider()->createTexture(yuvDesc,
SkBudgeted::kYes)); |
102 } else { | 103 } else { |
103 yuvTextures[i].reset(ctx->textureProvider()->createApproxTexture(yuv
Desc)); | 104 yuvTextures[i].reset(ctx->textureProvider()->createApproxTexture(yuv
Desc)); |
104 } | 105 } |
105 if (!yuvTextures[i] || | 106 if (!yuvTextures[i] || |
106 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight, | 107 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
yuvDesc.fConfig, |
107 yuvDesc.fConfig, planes[i], yuvInfo.fRo
wBytes[i])) { | 108 planes[i], yuvInfo.fSizeInfo.fWidthByte
s[i])) { |
108 return nullptr; | 109 return nullptr; |
109 } | 110 } |
110 } | 111 } |
111 | 112 |
112 GrSurfaceDesc rtDesc = desc; | 113 GrSurfaceDesc rtDesc = desc; |
113 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag; | 114 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag; |
114 | 115 |
115 SkAutoTUnref<GrTexture> result(ctx->textureProvider()->createTexture(rtDesc,
SkBudgeted::kYes, | 116 SkAutoTUnref<GrTexture> result(ctx->textureProvider()->createTexture(rtDesc,
SkBudgeted::kYes, |
116 nullptr
, 0)); | 117 nullptr
, 0)); |
117 if (!result) { | 118 if (!result) { |
118 return nullptr; | 119 return nullptr; |
119 } | 120 } |
120 | 121 |
121 GrRenderTarget* renderTarget = result->asRenderTarget(); | 122 GrRenderTarget* renderTarget = result->asRenderTarget(); |
122 SkASSERT(renderTarget); | 123 SkASSERT(renderTarget); |
123 | 124 |
124 GrPaint paint; | 125 GrPaint paint; |
125 SkAutoTUnref<const GrFragmentProcessor> yuvToRgbProcessor( | 126 SkAutoTUnref<const GrFragmentProcessor> yuvToRgbProcessor( |
126 GrYUVEffect::CreateYUVToRGB(yuvTextures[
0], | 127 GrYUVEffect::CreateYUVToRGB(yuvTextures[
0], |
127 yuvTextures[
1], | 128 yuvTextures[
1], |
128 yuvTextures[
2], | 129 yuvTextures[
2], |
129 yuvInfo.fSiz
e, | 130 yuvInfo.fSiz
eInfo.fSizes, |
130 yuvInfo.fCol
orSpace)); | 131 yuvInfo.fCol
orSpace)); |
131 paint.addColorFragmentProcessor(yuvToRgbProcessor); | 132 paint.addColorFragmentProcessor(yuvToRgbProcessor); |
132 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); | 133 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
133 const SkRect r = SkRect::MakeIWH(yuvInfo.fSize[0].fWidth, yuvInfo.fSize[0].f
Height); | 134 const SkRect r = SkRect::MakeIWH(yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY]
.fWidth, |
| 135 yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight); |
134 | 136 |
135 SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext(renderTarget)); | 137 SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext(renderTarget)); |
136 if (!drawContext) { | 138 if (!drawContext) { |
137 return nullptr; | 139 return nullptr; |
138 } | 140 } |
139 | 141 |
140 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), r); | 142 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), r); |
141 | 143 |
142 return result.detach(); | 144 return result.detach(); |
143 } | 145 } |
144 | 146 |
OLD | NEW |