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