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