• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/GrContext.h"
21 #include "include/gpu/GrTexture.h"
22 #include "include/gpu/gl/GrGLTypes.h"
23 #include "include/private/GrRecordingContext.h"
24 #include "src/core/SkExchange.h"
25 #include "src/core/SkMessageBus.h"
26 #include "src/gpu/GrAHardwareBufferUtils.h"
27 #include "src/gpu/GrContextPriv.h"
28 #include "src/gpu/GrProxyProvider.h"
29 #include "src/gpu/GrRecordingContextPriv.h"
30 #include "src/gpu/GrResourceCache.h"
31 #include "src/gpu/GrResourceProvider.h"
32 #include "src/gpu/GrResourceProviderPriv.h"
33 #include "src/gpu/GrTextureProxy.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 
Make(AHardwareBuffer * graphicBuffer,SkAlphaType alphaType,sk_sp<SkColorSpace> colorSpace,GrSurfaceOrigin surfaceOrigin)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 
GrAHardwareBufferImageGenerator(const SkImageInfo & info,AHardwareBuffer * hardwareBuffer,SkAlphaType alphaType,bool isProtectedContent,uint32_t bufferFormat,GrSurfaceOrigin surfaceOrigin)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 
~GrAHardwareBufferImageGenerator()77 GrAHardwareBufferImageGenerator::~GrAHardwareBufferImageGenerator() {
78     AHardwareBuffer_release(fHardwareBuffer);
79 }
80 
81 ///////////////////////////////////////////////////////////////////////////////////////////////////
82 
makeProxy(GrRecordingContext * context)83 sk_sp<GrTextureProxy> GrAHardwareBufferImageGenerator::makeProxy(GrRecordingContext* context) {
84     if (context->priv().abandoned()) {
85         return nullptr;
86     }
87 
88     auto direct = context->priv().asDirectContext();
89     if (!direct) {
90         return nullptr;
91     }
92 
93     GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(direct,
94                                                                              fHardwareBuffer,
95                                                                              fBufferFormat,
96                                                                              false);
97 
98     GrColorType grColorType = SkColorTypeToGrColorType(this->getInfo().colorType());
99     GrPixelConfig pixelConfig = context->priv().caps()->getConfigFromBackendFormat(backendFormat,
100                                                                                    grColorType);
101 
102     if (pixelConfig == kUnknown_GrPixelConfig) {
103         return nullptr;
104     }
105 
106     int width = this->getInfo().width();
107     int height = this->getInfo().height();
108 
109     GrSurfaceDesc desc;
110     desc.fWidth = width;
111     desc.fHeight = height;
112     desc.fConfig = pixelConfig;
113 
114     GrTextureType textureType = GrTextureType::k2D;
115     if (context->backend() == GrBackendApi::kOpenGL) {
116         textureType = GrTextureType::kExternal;
117     } else if (context->backend() == GrBackendApi::kVulkan) {
118         VkFormat format;
119         SkAssertResult(backendFormat.asVkFormat(&format));
120         if (format == VK_FORMAT_UNDEFINED) {
121             textureType = GrTextureType::kExternal;
122         }
123     }
124 
125     auto proxyProvider = context->priv().proxyProvider();
126 
127     AHardwareBuffer* hardwareBuffer = fHardwareBuffer;
128     AHardwareBuffer_acquire(hardwareBuffer);
129 
130     const bool isProtectedContent = fIsProtectedContent;
131 
132     class AutoAHBRelease {
133     public:
134         AutoAHBRelease(AHardwareBuffer* ahb) : fAhb(ahb) {}
135         // std::function() must be CopyConstructible, but ours should never actually be copied.
136         AutoAHBRelease(const AutoAHBRelease&) { SkASSERT(0); }
137         AutoAHBRelease(AutoAHBRelease&& that) : fAhb(that.fAhb) { that.fAhb = nullptr; }
138         ~AutoAHBRelease() { fAhb ? AHardwareBuffer_release(fAhb) : void(); }
139 
140         AutoAHBRelease& operator=(AutoAHBRelease&& that) {
141             fAhb = skstd::exchange(that.fAhb, nullptr);
142             return *this;
143         }
144         AutoAHBRelease& operator=(const AutoAHBRelease&) = delete;
145 
146         AHardwareBuffer* get() const { return fAhb; }
147 
148     private:
149         AHardwareBuffer* fAhb;
150     };
151 
152     sk_sp<GrTextureProxy> texProxy = proxyProvider->createLazyProxy(
153             [direct, buffer = AutoAHBRelease(hardwareBuffer), width, height,
154              isProtectedContent, backendFormat, grColorType](GrResourceProvider* resourceProvider)
155                     -> GrSurfaceProxy::LazyInstantiationResult {
156                 GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr;
157                 GrAHardwareBufferUtils::UpdateImageProc updateImageProc = nullptr;
158                 GrAHardwareBufferUtils::TexImageCtx texImageCtx = nullptr;
159 
160                 GrBackendTexture backendTex =
161                         GrAHardwareBufferUtils::MakeBackendTexture(direct, buffer.get(),
162                                                                    width, height,
163                                                                    &deleteImageProc,
164                                                                    &updateImageProc,
165                                                                    &texImageCtx,
166                                                                    isProtectedContent,
167                                                                    backendFormat,
168                                                                    false);
169                 if (!backendTex.isValid()) {
170                     return {};
171                 }
172                 SkASSERT(deleteImageProc && texImageCtx);
173 
174                 // We make this texture cacheable to avoid recreating a GrTexture every time this
175                 // is invoked. We know the owning SkIamge will send an invalidation message when the
176                 // image is destroyed, so the texture will be removed at that time.
177                 sk_sp<GrTexture> tex = resourceProvider->wrapBackendTexture(
178                         backendTex, grColorType, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes,
179                         kRead_GrIOType);
180                 if (!tex) {
181                     deleteImageProc(texImageCtx);
182                     return {};
183                 }
184 
185                 if (deleteImageProc) {
186                     tex->setRelease(deleteImageProc, texImageCtx);
187                 }
188 
189                 return tex;
190             },
191             backendFormat, desc, GrRenderable::kNo, 1, fSurfaceOrigin, GrMipMapped::kNo,
192             GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact,
193             SkBudgeted::kNo, GrProtected::kNo);
194 
195     return texProxy;
196 }
197 
onGenerateTexture(GrRecordingContext * context,const SkImageInfo & info,const SkIPoint & origin,bool willNeedMipMaps)198 sk_sp<GrTextureProxy> GrAHardwareBufferImageGenerator::onGenerateTexture(
199         GrRecordingContext* context, const SkImageInfo& info,
200         const SkIPoint& origin, bool willNeedMipMaps) {
201     sk_sp<GrTextureProxy> texProxy = this->makeProxy(context);
202     if (!texProxy) {
203         return nullptr;
204     }
205 
206     if (0 == origin.fX && 0 == origin.fY &&
207         info.width() == this->getInfo().width() && info.height() == this->getInfo().height()) {
208         // If the caller wants the full texture we're done. The caller will handle making a copy for
209         // mip maps if that is required.
210         return texProxy;
211     }
212     // Otherwise, make a copy for the requested subset.
213     SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
214 
215     GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo;
216 
217     return GrSurfaceProxy::Copy(context, texProxy.get(), mipMapped, subset, SkBackingFit::kExact,
218                                 SkBudgeted::kYes);
219 }
220 
onIsValid(GrContext * context) const221 bool GrAHardwareBufferImageGenerator::onIsValid(GrContext* context) const {
222     if (nullptr == context) {
223         return false; //CPU backend is not supported, because hardware buffer can be swizzled
224     }
225     return GrBackendApi::kOpenGL == context->backend() ||
226            GrBackendApi::kVulkan == context->backend();
227 }
228 
229 #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK
230