Debian Codegraph @

src/gpu/ganesh/GrAHardwareBufferImageGenerator.cpp

raw
1 /*
2 * Copyright 2017 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 "include/core/SkTypes.h"
9
10 #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
11
12 #include "src/gpu/ganesh/GrAHardwareBufferImageGenerator.h"
13
14 #include "include/android/AHardwareBufferUtils.h"
15 #include "include/android/GrAHardwareBufferUtils.h"
16 #include "include/core/SkColorSpace.h"
17 #include "include/gpu/ganesh/GrBackendSurface.h"
18 #include "include/gpu/ganesh/GrDirectContext.h"
19 #include "include/gpu/ganesh/GrRecordingContext.h"
20 #include "include/gpu/ganesh/gl/GrGLTypes.h"
21 #include "src/core/SkMessageBus.h"
22 #include "src/gpu/ganesh/GrCaps.h"
23 #include "src/gpu/ganesh/GrDirectContextPriv.h"
24 #include "src/gpu/ganesh/GrProxyProvider.h"
25 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
26 #include "src/gpu/ganesh/GrResourceCache.h"
27 #include "src/gpu/ganesh/GrResourceProvider.h"
28 #include "src/gpu/ganesh/GrResourceProviderPriv.h"
29 #include "src/gpu/ganesh/GrTexture.h"
30 #include "src/gpu/ganesh/GrTextureProxy.h"
31 #include "src/gpu/ganesh/SkGaneshRecorder.h"
32 #include "src/gpu/ganesh/SkGr.h"
33
34 #include <android/hardware_buffer.h>
35
36 std::unique_ptr<GrAHardwareBufferImageGenerator> GrAHardwareBufferImageGenerator::Make(
37 AHardwareBuffer* graphicBuffer, SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace,
38 GrSurfaceOrigin surfaceOrigin) {
39 AHardwareBuffer_Desc bufferDesc;
40 AHardwareBuffer_describe(graphicBuffer, &bufferDesc);
41
42 SkColorType colorType =
43 AHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format);
44 SkImageInfo info = SkImageInfo::Make(bufferDesc.width, bufferDesc.height, colorType,
45 alphaType, std::move(colorSpace));
46
47 bool createProtectedImage = 0 != (bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
48 return std::unique_ptr<GrAHardwareBufferImageGenerator>(new GrAHardwareBufferImageGenerator(
49 info, graphicBuffer, alphaType, createProtectedImage,
50 bufferDesc.format, surfaceOrigin));
51 }
52
53 GrAHardwareBufferImageGenerator::GrAHardwareBufferImageGenerator(const SkImageInfo& info,
54 AHardwareBuffer* hardwareBuffer, SkAlphaType alphaType, bool isProtectedContent,
55 uint32_t bufferFormat, GrSurfaceOrigin surfaceOrigin)
56 : GrTextureGenerator(info)
57 , fHardwareBuffer(hardwareBuffer)
58 , fBufferFormat(bufferFormat)
59 , fIsProtectedContent(isProtectedContent)
60 , fSurfaceOrigin(surfaceOrigin) {
61 AHardwareBuffer_acquire(fHardwareBuffer);
62 }
63
64 GrAHardwareBufferImageGenerator::~GrAHardwareBufferImageGenerator() {
65 AHardwareBuffer_release(fHardwareBuffer);
66 }
67
68 ///////////////////////////////////////////////////////////////////////////////////////////////////
69
70 GrSurfaceProxyView GrAHardwareBufferImageGenerator::makeView(GrRecordingContext* context) {
71 if (context->abandoned()) {
72 return {};
73 }
74
75 auto direct = context->asDirectContext();
76 if (!direct) {
77 return {};
78 }
79
80 GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(direct,
81 fHardwareBuffer,
82 fBufferFormat,
83 false);
84
85 GrColorType grColorType = SkColorTypeToGrColorType(this->getInfo().colorType());
86
87 int width = this->getInfo().width();
88 int height = this->getInfo().height();
89
90 auto proxyProvider = context->priv().proxyProvider();
91
92 AHardwareBuffer* hardwareBuffer = fHardwareBuffer;
93 AHardwareBuffer_acquire(hardwareBuffer);
94
95 class AutoAHBRelease {
96 public:
97 AutoAHBRelease(AHardwareBuffer* ahb) : fAhb(ahb) {}
98 // std::function() must be CopyConstructible, but ours should never actually be copied.
99 AutoAHBRelease(const AutoAHBRelease&) { SkASSERT(0); }
100 AutoAHBRelease(AutoAHBRelease&& that) : fAhb(that.fAhb) { that.fAhb = nullptr; }
101 ~AutoAHBRelease() { fAhb ? AHardwareBuffer_release(fAhb) : void(); }
102
103 AutoAHBRelease& operator=(AutoAHBRelease&& that) {
104 fAhb = std::exchange(that.fAhb, nullptr);
105 return *this;
106 }
107 AutoAHBRelease& operator=(const AutoAHBRelease&) = delete;
108
109 AHardwareBuffer* get() const { return fAhb; }
110
111 private:
112 AHardwareBuffer* fAhb;
113 };
114
115 sk_sp<GrTextureProxy> texProxy = proxyProvider->createLazyProxy(
116 [direct, buffer = AutoAHBRelease(hardwareBuffer)](
117 GrResourceProvider* resourceProvider,
118 const GrSurfaceProxy::LazySurfaceDesc& desc)
119 -> GrSurfaceProxy::LazyCallbackResult {
120 GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr;
121 GrAHardwareBufferUtils::UpdateImageProc updateImageProc = nullptr;
122 GrAHardwareBufferUtils::TexImageCtx texImageCtx = nullptr;
123
124 bool isProtected = desc.fProtected == GrProtected::kYes;
125 GrBackendTexture backendTex =
126 GrAHardwareBufferUtils::MakeBackendTexture(direct,
127 buffer.get(),
128 desc.fDimensions.width(),
129 desc.fDimensions.height(),
130 &deleteImageProc,
131 &updateImageProc,
132 &texImageCtx,
133 isProtected,
134 desc.fFormat,
135 false);
136 if (!backendTex.isValid()) {
137 return {};
138 }
139 SkASSERT(deleteImageProc && texImageCtx);
140
141 // We make this texture cacheable to avoid recreating a GrTexture every time this
142 // is invoked. We know the owning SkImage will send an invalidation message when the
143 // image is destroyed, so the texture will be removed at that time. Note that the
144 // proxy will be keyed in GrProxyProvider but that cache just allows extant proxies
145 // to be reused. It does not retain them. After a flush the proxy will be deleted
146 // and a subsequent use of the image will recreate a new proxy around the GrTexture
147 // found in the GrResourceCache.
148 // This is the last use of GrWrapCacheable::kYes so if we actually cached the proxy
149 // we could remove wrapped GrGpuResource caching.
150 sk_sp<GrTexture> tex = resourceProvider->wrapBackendTexture(
151 backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes, kRead_GrIOType);
152 if (!tex) {
153 deleteImageProc(texImageCtx);
154 return {};
155 }
156
157 if (deleteImageProc) {
158 tex->setRelease(deleteImageProc, texImageCtx);
159 }
160
161 return tex;
162 },
163 backendFormat,
164 {width, height},
165 skgpu::Mipmapped::kNo,
166 GrMipmapStatus::kNotAllocated,
167 GrInternalSurfaceFlags::kReadOnly,
168 SkBackingFit::kExact,
169 skgpu::Budgeted::kNo,
170 GrProtected(fIsProtectedContent),
171 GrSurfaceProxy::UseAllocator::kYes,
172 "AHardwareBufferImageGenerator_MakeView");
173
174 skgpu::Swizzle readSwizzle = context->priv().caps()->getReadSwizzle(backendFormat, grColorType);
175
176 return GrSurfaceProxyView(std::move(texProxy), fSurfaceOrigin, readSwizzle);
177 }
178
179 GrSurfaceProxyView GrAHardwareBufferImageGenerator::onGenerateTexture(
180 GrRecordingContext* context,
181 const SkImageInfo& info,
182 skgpu::Mipmapped mipmapped,
183 GrImageTexGenPolicy texGenPolicy) {
184 GrSurfaceProxyView texProxyView = this->makeView(context);
185 if (!texProxyView.proxy()) {
186 return {};
187 }
188 SkASSERT(texProxyView.asTextureProxy());
189
190 if (texGenPolicy == GrImageTexGenPolicy::kDraw && mipmapped == skgpu::Mipmapped::kNo) {
191 // If we have the correct mip support, we're done
192 return texProxyView;
193 }
194
195 // Otherwise, make a copy for the requested MIP map setting.
196 skgpu::Budgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
197 ? skgpu::Budgeted::kNo
198 : skgpu::Budgeted::kYes;
199
200 return GrSurfaceProxyView::Copy(context,
201 std::move(texProxyView),
202 mipmapped,
203 SkIRect::MakeWH(info.width(), info.height()),
204 SkBackingFit::kExact,
205 budgeted,
206 /*label=*/"AHardwareBufferImageGenerator_GenerateTexture");
207 }
208
209 bool GrAHardwareBufferImageGenerator::onIsValid(SkRecorder* recorder) const {
210 if (!recorder) {
211 return false;
212 }
213 if (recorder->type() != SkRecorder::Type::kGanesh) {
214 return false;
215 }
216 auto ctx = static_cast<SkGaneshRecorder*>(recorder)->recordingContext();
217 SkASSERT(ctx);
218 return GrBackendApi::kOpenGL == ctx->backend() ||
219 GrBackendApi::kVulkan == ctx->backend();
220 }
221
222 #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK