Debian Codegraph @

src/gpu/graphite/surface/Surface_AndroidFactories.cpp

raw
1 /*
2 * Copyright 2023 Google LLC
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 __ANDROID_API__ >= 26
11
12 #include "include/android/AHardwareBufferUtils.h"
13 #include "include/android/graphite/SurfaceAndroid.h"
14 #include "include/core/SkColorSpace.h"
15 #include "include/gpu/graphite/BackendTexture.h"
16 #include "include/gpu/graphite/Recorder.h"
17 #include "include/gpu/graphite/Surface.h"
18 #include "src/gpu/graphite/Caps.h"
19 #include "src/gpu/graphite/RecorderPriv.h"
20 #include "src/gpu/graphite/TextureFormat.h"
21 #include "src/gpu/graphite/TextureInfoPriv.h"
22
23 #include <android/hardware_buffer.h>
24
25 using namespace skgpu::graphite;
26
27 namespace SkSurfaces {
28
29 sk_sp<SkSurface> WrapAndroidHardwareBuffer(Recorder* recorder,
30 AHardwareBuffer* hardwareBuffer,
31 sk_sp<SkColorSpace> colorSpace,
32 const SkSurfaceProps* surfaceProps,
33 BufferReleaseProc releaseP,
34 ReleaseContext releaseC,
35 bool fromWindow) {
36 if (!recorder || !hardwareBuffer) {
37 return nullptr;
38 }
39
40 AHardwareBuffer_Desc bufferDesc;
41 AHardwareBuffer_describe(hardwareBuffer, &bufferDesc);
42
43 // AHB formats that mask out the alpha channel do not make sense to render into. This is
44 // consistent with ::RenderTarget(SkColorType) not allowing types like kRGB_888x to be an
45 // SkSurface. However, these AHB formats may get imported into Vulkan/GL as a backend format
46 // that does not mask the alpha channel, e.g. AHB_FORMAT_R8G8B8X8_UNORM maps to
47 // VK_FORMAT_R8G8AB8A8_UNORM (https://developer.android.com/ndk/reference/group/a-hardware-buffer#group___a_hardware_buffer_1gga94397844440c1c27b5b454d56ba5ff38af4f001b0b3e8aea568b533f682418e7f)
48 //
49 // We also require them to be usable as color output and sampled images.
50 if (!SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT) ||
51 !SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE) ||
52 bufferDesc.format == AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM) {
53 if (releaseP) {
54 releaseP(releaseC);
55 }
56 return nullptr;
57 }
58
59 const bool isProtectedContent =
60 SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
61
62 #if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
63 fromWindow = false; // ignored for non-framework builds
64 #endif
65
66 const SkISize dims = SkISize::Make(bufferDesc.width, bufferDesc.height);
67 BackendTexture backendTexture = recorder->createBackendTexture(hardwareBuffer,
68 /* isRenderable= */ true,
69 isProtectedContent,
70 dims,
71 fromWindow);
72 if (!backendTexture.isValid()) {
73 if (releaseP) {
74 releaseP(releaseC);
75 }
76 return nullptr;
77 }
78
79 // Will call 'releaseP' if SkSurface creation fails.
80 return SkSurfaces::WrapBackendTexture(recorder,
81 backendTexture,
82 std::move(colorSpace),
83 surfaceProps,
84 releaseP,
85 releaseC);
86 }
87
88 } // namespace SkSurfaces
89
90 #endif // __ANDROID_API__ >= 26