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 | #define GL_GLEXT_PROTOTYPES |
12 | #define EGL_EGLEXT_PROTOTYPES |
13 | |
14 | |
15 | #include "src/gpu/GrAHardwareBufferImageGenerator.h" |
16 | |
17 | #include <android/hardware_buffer.h> |
18 | |
19 | #include "include/gpu/GrBackendSurface.h" |
20 | #include "include/gpu/GrDirectContext.h" |
21 | #include "include/gpu/GrRecordingContext.h" |
22 | #include "include/gpu/gl/GrGLTypes.h" |
23 | #include "src/core/SkMessageBus.h" |
24 | #include "src/gpu/GrAHardwareBufferUtils.h" |
25 | #include "src/gpu/GrContextPriv.h" |
26 | #include "src/gpu/GrProxyProvider.h" |
27 | #include "src/gpu/GrRecordingContextPriv.h" |
28 | #include "src/gpu/GrResourceCache.h" |
29 | #include "src/gpu/GrResourceProvider.h" |
30 | #include "src/gpu/GrResourceProviderPriv.h" |
31 | #include "src/gpu/GrTexture.h" |
32 | #include "src/gpu/GrTextureProxy.h" |
33 | #include "src/gpu/SkGr.h" |
34 | #include "src/gpu/gl/GrGLDefines.h" |
35 | |
36 | #include <EGL/egl.h> |
37 | #include <EGL/eglext.h> |
38 | #include <GLES/gl.h> |
39 | #include <GLES/glext.h> |
40 | |
41 | #ifdef SK_VULKAN |
42 | #include "include/gpu/vk/GrVkExtensions.h" |
43 | #include "src/gpu/vk/GrVkGpu.h" |
44 | #endif |
45 | |
46 | #define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content" |
47 | #define EGL_PROTECTED_CONTENT_EXT 0x32C0 |
48 | |
49 | std::unique_ptr<SkImageGenerator> GrAHardwareBufferImageGenerator::Make( |
50 | AHardwareBuffer* graphicBuffer, SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace, |
51 | GrSurfaceOrigin surfaceOrigin) { |
52 | AHardwareBuffer_Desc bufferDesc; |
53 | AHardwareBuffer_describe(graphicBuffer, &bufferDesc); |
54 | |
55 | SkColorType colorType = |
56 | GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format); |
57 | SkImageInfo info = SkImageInfo::Make(bufferDesc.width, bufferDesc.height, colorType, |
58 | alphaType, std::move(colorSpace)); |
59 | |
60 | bool createProtectedImage = 0 != (bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT); |
61 | return std::unique_ptr<SkImageGenerator>(new GrAHardwareBufferImageGenerator( |
62 | info, graphicBuffer, alphaType, createProtectedImage, |
63 | bufferDesc.format, surfaceOrigin)); |
64 | } |
65 | |
66 | GrAHardwareBufferImageGenerator::GrAHardwareBufferImageGenerator(const SkImageInfo& info, |
67 | AHardwareBuffer* hardwareBuffer, SkAlphaType alphaType, bool isProtectedContent, |
68 | uint32_t bufferFormat, GrSurfaceOrigin surfaceOrigin) |
69 | : INHERITED(info) |
70 | , fHardwareBuffer(hardwareBuffer) |
71 | , fBufferFormat(bufferFormat) |
72 | , fIsProtectedContent(isProtectedContent) |
73 | , fSurfaceOrigin(surfaceOrigin) { |
74 | AHardwareBuffer_acquire(fHardwareBuffer); |
75 | } |
76 | |
77 | GrAHardwareBufferImageGenerator::~GrAHardwareBufferImageGenerator() { |
78 | AHardwareBuffer_release(fHardwareBuffer); |
79 | } |
80 | |
81 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
82 | |
83 | GrSurfaceProxyView GrAHardwareBufferImageGenerator::makeView(GrRecordingContext* context) { |
84 | if (context->abandoned()) { |
85 | return {}; |
86 | } |
87 | |
88 | auto direct = context->asDirectContext(); |
89 | if (!direct) { |
90 | return {}; |
91 | } |
92 | |
93 | GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(direct, |
94 | fHardwareBuffer, |
95 | fBufferFormat, |
96 | false); |
97 | |
98 | GrColorType grColorType = SkColorTypeToGrColorType(this->getInfo().colorType()); |
99 | |
100 | int width = this->getInfo().width(); |
101 | int height = this->getInfo().height(); |
102 | |
103 | GrTextureType textureType = GrTextureType::k2D; |
104 | if (context->backend() == GrBackendApi::kOpenGL) { |
105 | textureType = GrTextureType::kExternal; |
106 | } else if (context->backend() == GrBackendApi::kVulkan) { |
107 | VkFormat format; |
108 | SkAssertResult(backendFormat.asVkFormat(&format)); |
109 | if (format == VK_FORMAT_UNDEFINED) { |
110 | textureType = GrTextureType::kExternal; |
111 | } |
112 | } |
113 | |
114 | auto proxyProvider = context->priv().proxyProvider(); |
115 | |
116 | AHardwareBuffer* hardwareBuffer = fHardwareBuffer; |
117 | AHardwareBuffer_acquire(hardwareBuffer); |
118 | |
119 | class AutoAHBRelease { |
120 | public: |
121 | AutoAHBRelease(AHardwareBuffer* ahb) : fAhb(ahb) {} |
122 | // std::function() must be CopyConstructible, but ours should never actually be copied. |
123 | AutoAHBRelease(const AutoAHBRelease&) { SkASSERT(0); } |
124 | AutoAHBRelease(AutoAHBRelease&& that) : fAhb(that.fAhb) { that.fAhb = nullptr; } |
125 | ~AutoAHBRelease() { fAhb ? AHardwareBuffer_release(fAhb) : void(); } |
126 | |
127 | AutoAHBRelease& operator=(AutoAHBRelease&& that) { |
128 | fAhb = std::exchange(that.fAhb, nullptr); |
129 | return *this; |
130 | } |
131 | AutoAHBRelease& operator=(const AutoAHBRelease&) = delete; |
132 | |
133 | AHardwareBuffer* get() const { return fAhb; } |
134 | |
135 | private: |
136 | AHardwareBuffer* fAhb; |
137 | }; |
138 | |
139 | sk_sp<GrTextureProxy> texProxy = proxyProvider->createLazyProxy( |
140 | [direct, buffer = AutoAHBRelease(hardwareBuffer)]( |
141 | GrResourceProvider* resourceProvider, |
142 | const GrSurfaceProxy::LazySurfaceDesc& desc) |
143 | -> GrSurfaceProxy::LazyCallbackResult { |
144 | GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr; |
145 | GrAHardwareBufferUtils::UpdateImageProc updateImageProc = nullptr; |
146 | GrAHardwareBufferUtils::TexImageCtx texImageCtx = nullptr; |
147 | |
148 | bool isProtected = desc.fProtected == GrProtected::kYes; |
149 | GrBackendTexture backendTex = |
150 | GrAHardwareBufferUtils::MakeBackendTexture(direct, |
151 | buffer.get(), |
152 | desc.fDimensions.width(), |
153 | desc.fDimensions.height(), |
154 | &deleteImageProc, |
155 | &updateImageProc, |
156 | &texImageCtx, |
157 | isProtected, |
158 | desc.fFormat, |
159 | false); |
160 | if (!backendTex.isValid()) { |
161 | return {}; |
162 | } |
163 | SkASSERT(deleteImageProc && texImageCtx); |
164 | |
165 | // We make this texture cacheable to avoid recreating a GrTexture every time this |
166 | // is invoked. We know the owning SkIamge will send an invalidation message when the |
167 | // image is destroyed, so the texture will be removed at that time. |
168 | sk_sp<GrTexture> tex = resourceProvider->wrapBackendTexture( |
169 | backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes, kRead_GrIOType); |
170 | if (!tex) { |
171 | deleteImageProc(texImageCtx); |
172 | return {}; |
173 | } |
174 | |
175 | if (deleteImageProc) { |
176 | tex->setRelease(deleteImageProc, texImageCtx); |
177 | } |
178 | |
179 | return tex; |
180 | }, |
181 | backendFormat, {width, height}, GrRenderable::kNo, 1, GrMipmapped::kNo, |
182 | GrMipmapStatus::kNotAllocated, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, |
183 | SkBudgeted::kNo, GrProtected(fIsProtectedContent), GrSurfaceProxy::UseAllocator::kYes); |
184 | |
185 | GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(backendFormat, grColorType); |
186 | |
187 | return GrSurfaceProxyView(std::move(texProxy), fSurfaceOrigin, readSwizzle); |
188 | } |
189 | |
190 | GrSurfaceProxyView GrAHardwareBufferImageGenerator::onGenerateTexture( |
191 | GrRecordingContext* context, |
192 | const SkImageInfo& info, |
193 | const SkIPoint& origin, |
194 | GrMipmapped mipMapped, |
195 | GrImageTexGenPolicy texGenPolicy) { |
196 | GrSurfaceProxyView texProxyView = this->makeView(context); |
197 | if (!texProxyView.proxy()) { |
198 | return {}; |
199 | } |
200 | SkASSERT(texProxyView.asTextureProxy()); |
201 | |
202 | if (texGenPolicy == GrImageTexGenPolicy::kDraw && origin.isZero() && |
203 | info.dimensions() == this->getInfo().dimensions() && mipMapped == GrMipmapped::kNo) { |
204 | // If the caller wants the full non-MIP mapped texture we're done. |
205 | return texProxyView; |
206 | } |
207 | // Otherwise, make a copy for the requested subset and/or MIP maps. |
208 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height()); |
209 | |
210 | SkBudgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted |
211 | ? SkBudgeted::kNo |
212 | : SkBudgeted::kYes; |
213 | |
214 | return GrSurfaceProxyView::Copy(context, std::move(texProxyView), mipMapped, subset, |
215 | SkBackingFit::kExact, budgeted); |
216 | } |
217 | |
218 | bool GrAHardwareBufferImageGenerator::onIsValid(GrRecordingContext* context) const { |
219 | if (nullptr == context) { |
220 | return false; //CPU backend is not supported, because hardware buffer can be swizzled |
221 | } |
222 | return GrBackendApi::kOpenGL == context->backend() || |
223 | GrBackendApi::kVulkan == context->backend(); |
224 | } |
225 | |
226 | #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK |
227 | |