Flutter Engine
The Flutter Engine
SkSurface_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 defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
11
20#include "include/core/SkRect.h"
21#include "include/core/SkSize.h"
29#include "include/gpu/GrTypes.h"
34#include "src/core/SkDevice.h"
56
57#include <algorithm>
58#include <cstddef>
59#include <utility>
60
62
63namespace SkSurfaces {
64
66 AHardwareBuffer* hardwareBuffer,
67 GrSurfaceOrigin origin,
68 sk_sp<SkColorSpace> colorSpace,
69 const SkSurfaceProps* surfaceProps,
70 bool fromWindow) {
71 AHardwareBuffer_Desc bufferDesc;
72 AHardwareBuffer_describe(hardwareBuffer, &bufferDesc);
73
74 if (!SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT)) {
75 return nullptr;
76 }
77
78 bool isTextureable = SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE);
79 if (!isTextureable) {
80 return nullptr;
81 }
82
83 GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(
84 dContext, hardwareBuffer, bufferDesc.format, true);
85 if (!backendFormat.isValid()) {
86 return nullptr;
87 }
88
89 GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr;
90 GrAHardwareBufferUtils::UpdateImageProc updateImageProc = nullptr;
91 GrAHardwareBufferUtils::TexImageCtx deleteImageCtx = nullptr;
92
93 bool isProtectedContent =
94 SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
95
96 bool fromWindowLocal = false;
97#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
98 fromWindowLocal = fromWindow;
99#endif
100
101 GrBackendTexture backendTexture =
102 GrAHardwareBufferUtils::MakeBackendTexture(dContext,
103 hardwareBuffer,
104 bufferDesc.width,
105 bufferDesc.height,
106 &deleteImageProc,
107 &updateImageProc,
108 &deleteImageCtx,
109 isProtectedContent,
110 backendFormat,
111 true,
112 fromWindowLocal);
113 if (!backendTexture.isValid()) {
114 return nullptr;
115 }
116
118 AHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format);
119
120 // Will call deleteImageProc if SkSurface creation fails.
122 backendTexture,
123 origin,
124 0,
125 colorType,
126 std::move(colorSpace),
127 surfaceProps,
128 deleteImageProc,
129 deleteImageCtx);
130
131 return surface;
132}
133
134} // namespace SkSurfaces
135
136#endif
struct AHardwareBuffer AHardwareBuffer
GrSurfaceOrigin
Definition: GrTypes.h:147
SkColorType
Definition: SkColorType.h:19
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
bool isValid() const
bool isValid() const
VkSurfaceKHR surface
Definition: main.cc:49
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)
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)