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->fSizeInfo.fWidthBytes[SkYUVS
izeInfo::kY] * | 43 planes[1] = (uint8_t*)planes[0] + yuvInfo->fSizeInMemory[0]; |
44 yuvInfo->fSizeInfo.fSizes[SkYUVSizeIn
fo::kY].fHeight); | 44 planes[2] = (uint8_t*)planes[1] + yuvInfo->fSizeInMemory[1]; |
45 planes[2] = (uint8_t*)planes[1] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVS
izeInfo::kU] * | |
46 yuvInfo->fSizeInfo.fSizes[SkYUVSizeIn
fo::kU].fHeight); | |
47 } else { | 45 } else { |
48 // Fetch yuv plane sizes for memory allocation. | 46 // Fetch yuv plane sizes for memory allocation. Here, width and height c
an be |
49 if (!provider->onQueryYUV8(&yuvInfo->fSizeInfo, &yuvInfo->fColorSpace))
{ | 47 // rounded up to JPEG block size and be larger than the image's width an
d height. |
| 48 if (!provider->onGetYUVSizes(yuvInfo->fSize)) { |
50 return false; | 49 return false; |
51 } | 50 } |
52 | 51 |
53 // Allocate the memory for YUV | 52 // Allocate the memory for YUV |
54 size_t totalSize(0); | 53 size_t totalSize(0); |
55 for (int i = 0; i < 3; i++) { | 54 for (int i = 0; i < GrYUVProvider::kPlaneCount; ++i) { |
56 totalSize += yuvInfo->fSizeInfo.fWidthBytes[i] * yuvInfo->fSizeInfo.
fSizes[i].fHeight; | 55 yuvInfo->fRowBytes[i] = yuvInfo->fSize[i].fWidth; // we assume snug
fit: rb == width |
| 56 yuvInfo->fSizeInMemory[i] = yuvInfo->fRowBytes[i] * yuvInfo->fSize[i
].fHeight; |
| 57 totalSize += yuvInfo->fSizeInMemory[i]; |
57 } | 58 } |
58 if (useCache) { | 59 if (useCache) { |
59 fCachedData.reset(SkResourceCache::NewCachedData(totalSize)); | 60 fCachedData.reset(SkResourceCache::NewCachedData(totalSize)); |
60 planes[0] = fCachedData->writable_data(); | 61 planes[0] = fCachedData->writable_data(); |
61 } else { | 62 } else { |
62 fStorage.reset(totalSize); | 63 fStorage.reset(totalSize); |
63 planes[0] = fStorage.get(); | 64 planes[0] = fStorage.get(); |
64 } | 65 } |
65 planes[1] = (uint8_t*)planes[0] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVS
izeInfo::kY] * | 66 planes[1] = (uint8_t*)planes[0] + yuvInfo->fSizeInMemory[0]; |
66 yuvInfo->fSizeInfo.fSizes[SkYUVSizeIn
fo::kY].fHeight); | 67 planes[2] = (uint8_t*)planes[1] + yuvInfo->fSizeInMemory[1]; |
67 planes[2] = (uint8_t*)planes[1] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVS
izeInfo::kU] * | |
68 yuvInfo->fSizeInfo.fSizes[SkYUVSizeIn
fo::kU].fHeight); | |
69 | 68 |
70 // Get the YUV planes. | 69 // Get the YUV planes and update plane sizes to actual image size |
71 if (!provider->onGetYUV8Planes(yuvInfo->fSizeInfo, planes)) { | 70 if (!provider->onGetYUVPlanes(yuvInfo->fSize, planes, yuvInfo->fRowBytes
, |
| 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.fSizeInfo.fSizes[i].fWidth; | 95 yuvDesc.fWidth = yuvInfo.fSize[i].fWidth; |
96 yuvDesc.fHeight = yuvInfo.fSizeInfo.fSizes[i].fHeight; | 96 yuvDesc.fHeight = yuvInfo.fSize[i].fHeight; |
97 // TODO: why do we need this check? | 97 // TODO: why do we need this check? |
98 bool needsExactTexture = | 98 bool needsExactTexture = (yuvDesc.fWidth != yuvInfo.fSize[0].fWidth) || |
99 (yuvDesc.fWidth != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].
fWidth) || | 99 (yuvDesc.fHeight != yuvInfo.fSize[0].fHeight); |
100 (yuvDesc.fHeight != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].
fHeight); | |
101 if (needsExactTexture) { | 100 if (needsExactTexture) { |
102 yuvTextures[i].reset(ctx->textureProvider()->createTexture(yuvDesc,
SkBudgeted::kYes)); | 101 yuvTextures[i].reset(ctx->textureProvider()->createTexture(yuvDesc,
SkBudgeted::kYes)); |
103 } else { | 102 } else { |
104 yuvTextures[i].reset(ctx->textureProvider()->createApproxTexture(yuv
Desc)); | 103 yuvTextures[i].reset(ctx->textureProvider()->createApproxTexture(yuv
Desc)); |
105 } | 104 } |
106 if (!yuvTextures[i] || | 105 if (!yuvTextures[i] || |
107 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
yuvDesc.fConfig, | 106 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight, |
108 planes[i], yuvInfo.fSizeInfo.fWidthByte
s[i])) { | 107 yuvDesc.fConfig, planes[i], yuvInfo.fRo
wBytes[i])) { |
109 return nullptr; | 108 return nullptr; |
110 } | 109 } |
111 } | 110 } |
112 | 111 |
113 GrSurfaceDesc rtDesc = desc; | 112 GrSurfaceDesc rtDesc = desc; |
114 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag; | 113 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag; |
115 | 114 |
116 SkAutoTUnref<GrTexture> result(ctx->textureProvider()->createTexture(rtDesc,
SkBudgeted::kYes, | 115 SkAutoTUnref<GrTexture> result(ctx->textureProvider()->createTexture(rtDesc,
SkBudgeted::kYes, |
117 nullptr
, 0)); | 116 nullptr
, 0)); |
118 if (!result) { | 117 if (!result) { |
119 return nullptr; | 118 return nullptr; |
120 } | 119 } |
121 | 120 |
122 GrRenderTarget* renderTarget = result->asRenderTarget(); | 121 GrRenderTarget* renderTarget = result->asRenderTarget(); |
123 SkASSERT(renderTarget); | 122 SkASSERT(renderTarget); |
124 | 123 |
125 GrPaint paint; | 124 GrPaint paint; |
126 SkAutoTUnref<const GrFragmentProcessor> yuvToRgbProcessor( | 125 SkAutoTUnref<const GrFragmentProcessor> yuvToRgbProcessor( |
127 GrYUVEffect::CreateYUVToRGB(yuvTextures[
0], | 126 GrYUVEffect::CreateYUVToRGB(yuvTextures[
0], |
128 yuvTextures[
1], | 127 yuvTextures[
1], |
129 yuvTextures[
2], | 128 yuvTextures[
2], |
130 yuvInfo.fSiz
eInfo.fSizes, | 129 yuvInfo.fSiz
e, |
131 yuvInfo.fCol
orSpace)); | 130 yuvInfo.fCol
orSpace)); |
132 paint.addColorFragmentProcessor(yuvToRgbProcessor); | 131 paint.addColorFragmentProcessor(yuvToRgbProcessor); |
133 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); | 132 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
134 const SkRect r = SkRect::MakeIWH(yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY]
.fWidth, | 133 const SkRect r = SkRect::MakeIWH(yuvInfo.fSize[0].fWidth, yuvInfo.fSize[0].f
Height); |
135 yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight); | |
136 | 134 |
137 SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext(renderTarget)); | 135 SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext(renderTarget)); |
138 if (!drawContext) { | 136 if (!drawContext) { |
139 return nullptr; | 137 return nullptr; |
140 } | 138 } |
141 | 139 |
142 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), r); | 140 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), r); |
143 | 141 |
144 return result.detach(); | 142 return result.detach(); |
145 } | 143 } |
146 | 144 |
OLD | NEW |