1 /*
2 * Copyright 2019 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/android/GrAHardwareBufferUtils.h"
9
10 #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
11 #define GL_GLEXT_PROTOTYPES
12 #define EGL_EGLEXT_PROTOTYPES
13
14 #include <android/hardware_buffer.h>
15
16 #if !defined(SK_DISABLE_LEGACY_ANDROID_HW_UTILS)
17 #include "include/gpu/ganesh/GrDirectContext.h"
18 #endif
19
20 // TODO: remove this once Android is using the AHardwareBufferUtils version of
21 // GetSkColorTypeFromBufferFormat
22 #include "include/android/AHardwareBufferUtils.h"
23
24 namespace GrAHardwareBufferUtils {
25
GetSkColorTypeFromBufferFormat(uint32_t bufferFormat)26 SkColorType GetSkColorTypeFromBufferFormat(uint32_t bufferFormat) {
27 return AHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferFormat);
28 }
29
30 #if !defined(SK_DISABLE_LEGACY_ANDROID_HW_UTILS)
GetBackendFormat(GrDirectContext * dContext,AHardwareBuffer * hardwareBuffer,uint32_t bufferFormat,bool requireKnownFormat)31 GrBackendFormat GetBackendFormat(GrDirectContext* dContext, AHardwareBuffer* hardwareBuffer,
32 uint32_t bufferFormat, bool requireKnownFormat) {
33 GrBackendApi backend = dContext->backend();
34
35 if (backend == GrBackendApi::kOpenGL) {
36 #ifdef SK_GL
37 return GetGLBackendFormat(dContext, bufferFormat, requireKnownFormat);
38 #else // SK_GL
39 return GrBackendFormat();
40 #endif // SK_GL
41 } else if (backend == GrBackendApi::kVulkan) {
42 #ifdef SK_VULKAN
43 return GetVulkanBackendFormat(dContext, hardwareBuffer, bufferFormat, requireKnownFormat);
44 #else // SK_VULKAN
45 return GrBackendFormat();
46 #endif // SK_VULKAN
47 }
48 return GrBackendFormat();
49 }
50
MakeBackendTexture(GrDirectContext * dContext,AHardwareBuffer * hardwareBuffer,int width,int height,DeleteImageProc * deleteProc,UpdateImageProc * updateProc,TexImageCtx * imageCtx,bool isProtectedContent,const GrBackendFormat & backendFormat,bool isRenderable,bool fromAndroidWindow)51 GrBackendTexture MakeBackendTexture(GrDirectContext* dContext,
52 AHardwareBuffer* hardwareBuffer,
53 int width, int height,
54 DeleteImageProc* deleteProc,
55 UpdateImageProc* updateProc,
56 TexImageCtx* imageCtx,
57 bool isProtectedContent,
58 const GrBackendFormat& backendFormat,
59 bool isRenderable,
60 bool fromAndroidWindow) {
61 SkASSERT(dContext);
62 if (!dContext || dContext->abandoned()) {
63 return GrBackendTexture();
64 }
65
66 if (GrBackendApi::kOpenGL == dContext->backend()) {
67 #ifdef SK_GL
68 return MakeGLBackendTexture(dContext, hardwareBuffer, width, height, deleteProc,
69 updateProc, imageCtx, isProtectedContent, backendFormat,
70 isRenderable);
71 #else
72 return GrBackendTexture();
73 #endif // SK_GL
74 } else {
75 SkASSERT(GrBackendApi::kVulkan == dContext->backend());
76 #ifdef SK_VULKAN
77 return MakeVulkanBackendTexture(dContext, hardwareBuffer, width, height, deleteProc,
78 updateProc, imageCtx, isProtectedContent, backendFormat,
79 isRenderable, fromAndroidWindow);
80 #else
81 return GrBackendTexture();
82 #endif // SK_VULKAN
83 }
84 }
85 #endif
86
87 } // namespace GrAHardwareBufferUtils
88
89 #endif
90