Flutter Engine
The Flutter Engine
Surface_AndroidFactories.cpp
Go to the documentation of this file.
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
9
10#if __ANDROID_API__ >= 26
11
18
20
21using namespace skgpu::graphite;
22
23namespace SkSurfaces {
24
26 AHardwareBuffer* hardwareBuffer,
27 sk_sp<SkColorSpace> colorSpace,
28 const SkSurfaceProps* surfaceProps,
29 BufferReleaseProc releaseP,
30 ReleaseContext releaseC,
31 bool fromWindow) {
32 if (!recorder || !hardwareBuffer) {
33 return nullptr;
34 }
35
36 AHardwareBuffer_Desc bufferDesc;
37 AHardwareBuffer_describe(hardwareBuffer, &bufferDesc);
38
39 if (!SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT) ||
40 !SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE)) {
41 if (releaseP) {
42 releaseP(releaseC);
43 }
44 return nullptr;
45 }
46
47 bool isProtectedContent = SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
48
49 bool fromWindowLocal = false;
50#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
51 fromWindowLocal = fromWindow;
52#endif
53
54 SkISize dims = SkISize::Make(bufferDesc.width, bufferDesc.height);
55
56 BackendTexture backendTexture = recorder->createBackendTexture(hardwareBuffer,
57 /* isRenderable= */ true,
58 isProtectedContent,
59 dims,
60 fromWindowLocal);
61 if (!backendTexture.isValid()) {
62 if (releaseP) {
63 releaseP(releaseC);
64 }
65 return nullptr;
66 }
67
69 AHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format);
70
71 // Will call 'releaseP' if SkSurface creation fails.
72 return SkSurfaces::WrapBackendTexture(recorder,
73 backendTexture,
75 std::move(colorSpace),
76 surfaceProps,
77 releaseP,
78 releaseC);
79}
80
81} // namespace SkSurfaces
82
83#endif // __ANDROID_API__ >= 26
struct AHardwareBuffer AHardwareBuffer
SkColorType
Definition: SkColorType.h:19
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
BackendTexture createBackendTexture(SkISize dimensions, const TextureInfo &)
Definition: Recorder.cpp:284
SK_API sk_sp< SkSurface > WrapAndroidHardwareBuffer(skgpu::graphite::Recorder *recorder, AHardwareBuffer *hardwareBuffer, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, BufferReleaseProc=nullptr, ReleaseContext=nullptr, bool fromWindow=false)
void * ReleaseContext
void(*)(ReleaseContext) BufferReleaseProc
SK_API sk_sp< SkSurface > WrapBackendTexture(GrRecordingContext *context, const GrBackendTexture &backendTexture, GrSurfaceOrigin origin, int sampleCnt, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, TextureReleaseProc textureReleaseProc=nullptr, ReleaseContext releaseContext=nullptr)
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20