OLD | NEW |
---|---|
(Empty) | |
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 | |
8 #include "GrContext.h" | |
9 #include "GrDrawContext.h" | |
10 #include "GrYUVProvider.h" | |
11 #include "effects/GrYUVtoRGBEffect.h" | |
12 | |
13 #include "SkCachedData.h" | |
14 #include "SkRefCnt.h" | |
15 #include "SkResourceCache.h" | |
16 #include "SkYUVPlanesCache.h" | |
17 | |
18 namespace { | |
robertphillips
2015/09/08 15:04:47
// comment ?
reed1
2015/09/08 15:22:52
Done.
| |
19 class YUVScoper { | |
20 public: | |
21 bool init(GrYUVProvider*, SkYUVPlanesCache::Info*, void* planes[3], bool use Cache); | |
22 | |
23 private: | |
24 SkAutoTUnref<SkCachedData> fCachedData; | |
25 SkAutoMalloc fStorage; | |
26 }; | |
27 } | |
28 | |
29 bool YUVScoper::init(GrYUVProvider* provider, SkYUVPlanesCache::Info* yuvInfo, v oid* planes[3], | |
30 bool useCache) { | |
31 if (useCache) { | |
32 fCachedData.reset(SkYUVPlanesCache::FindAndRef(provider->onGetID(), yuvI nfo)); | |
33 } | |
34 | |
35 if (fCachedData.get()) { | |
36 planes[0] = (void*)fCachedData->data(); | |
37 planes[1] = (uint8_t*)planes[0] + yuvInfo->fSizeInMemory[0]; | |
38 planes[2] = (uint8_t*)planes[1] + yuvInfo->fSizeInMemory[1]; | |
39 } else { | |
40 // Fetch yuv plane sizes for memory allocation. Here, width and height c an be | |
41 // rounded up to JPEG block size and be larger than the image's width an d height. | |
42 if (!provider->onGetYUVSizes(yuvInfo->fSize)) { | |
43 return false; | |
44 } | |
45 | |
46 // Allocate the memory for YUV | |
47 size_t totalSize(0); | |
48 for (int i = 0; i < GrYUVProvider::kPlaneCount; ++i) { | |
49 yuvInfo->fRowBytes[i] = yuvInfo->fSize[i].fWidth; // we assume snug fit: rb == width | |
50 yuvInfo->fSizeInMemory[i] = yuvInfo->fRowBytes[i] * yuvInfo->fSize[i ].fHeight; | |
51 totalSize += yuvInfo->fSizeInMemory[i]; | |
52 } | |
53 if (useCache) { | |
54 fCachedData.reset(SkResourceCache::NewCachedData(totalSize)); | |
55 planes[0] = fCachedData->writable_data(); | |
56 } else { | |
57 fStorage.reset(totalSize); | |
58 planes[0] = fStorage.get(); | |
59 } | |
60 planes[1] = (uint8_t*)planes[0] + yuvInfo->fSizeInMemory[0]; | |
61 planes[2] = (uint8_t*)planes[1] + yuvInfo->fSizeInMemory[1]; | |
62 | |
63 // Get the YUV planes and update plane sizes to actual image size | |
64 if (!provider->onGetYUVPlanes(yuvInfo->fSize, planes, yuvInfo->fRowBytes , | |
65 &yuvInfo->fColorSpace)) { | |
66 return false; | |
67 } | |
68 | |
69 if (useCache) { | |
70 // Decoding is done, cache the resulting YUV planes | |
71 SkYUVPlanesCache::Add(provider->onGetID(), fCachedData, yuvInfo); | |
72 } | |
73 } | |
74 return true; | |
75 } | |
76 | |
77 GrTexture* GrYUVProvider::refAsTexture(GrContext* ctx, const GrSurfaceDesc& desc , bool useCache) { | |
78 SkYUVPlanesCache::Info yuvInfo; | |
79 void* planes[3]; | |
80 YUVScoper scoper; | |
81 if (!scoper.init(this, &yuvInfo, planes, useCache)) { | |
82 return nullptr; | |
83 } | |
84 | |
85 GrSurfaceDesc yuvDesc; | |
86 yuvDesc.fConfig = kAlpha_8_GrPixelConfig; | |
87 SkAutoTUnref<GrTexture> yuvTextures[3]; | |
88 for (int i = 0; i < 3; ++i) { | |
89 yuvDesc.fWidth = yuvInfo.fSize[i].fWidth; | |
90 yuvDesc.fHeight = yuvInfo.fSize[i].fHeight; | |
91 // TODO: why do we need this check? | |
92 bool needsExactTexture = (yuvDesc.fWidth != yuvInfo.fSize[0].fWidth) || | |
93 (yuvDesc.fHeight != yuvInfo.fSize[0].fHeight); | |
94 if (needsExactTexture) { | |
95 yuvTextures[i].reset(ctx->textureProvider()->createTexture(yuvDesc, true)); | |
96 } else { | |
97 yuvTextures[i].reset(ctx->textureProvider()->createApproxTexture(yuv Desc)); | |
98 } | |
99 if (!yuvTextures[i] || | |
100 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight, | |
101 yuvDesc.fConfig, planes[i], yuvInfo.fRo wBytes[i])) { | |
102 return nullptr; | |
103 } | |
104 } | |
105 | |
106 GrSurfaceDesc rtDesc = desc; | |
107 rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag; | |
108 | |
109 SkAutoTUnref<GrTexture> result(ctx->textureProvider()->createTexture(rtDesc, true, nullptr, 0)); | |
110 if (!result) { | |
111 return nullptr; | |
112 } | |
113 | |
114 GrRenderTarget* renderTarget = result->asRenderTarget(); | |
115 SkASSERT(renderTarget); | |
116 | |
117 GrPaint paint; | |
robertphillips
2015/09/08 15:04:47
put "SkAutoTUnref<GrFragmentProcessor> yuvToRgbPro
reed1
2015/09/08 15:22:52
Done.
| |
118 SkAutoTUnref<GrFragmentProcessor> | |
119 yuvToRgbProcessor(GrYUVtoRGBEffect::Create(paint.getProcessorDataManager(), yuvTextures[0], | |
120 yuvTextures[1], yuvTextures[2], | |
121 yuvInfo.fSize, yuvInfo.fColorSpac e)); | |
122 paint.addColorFragmentProcessor(yuvToRgbProcessor); | |
123 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth), | |
124 SkIntToScalar(yuvInfo.fSize[0].fHeight)); | |
125 | |
126 SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext()); | |
127 if (!drawContext) { | |
128 return nullptr; | |
129 } | |
130 | |
131 drawContext->drawRect(renderTarget, GrClip::WideOpen(), paint, SkMatrix::I() , r); | |
132 | |
133 return result.detach(); | |
134 } | |
135 | |
OLD | NEW |