Flutter Engine
The Flutter Engine
SkImage_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
13
19#include "include/core/SkData.h"
24#include "include/core/SkRect.h"
25#include "include/core/SkSize.h"
32#include "include/gpu/GrTypes.h"
61#include "src/gpu/ganesh/SkGr.h"
67
68#include <algorithm>
69#include <cstddef>
70#include <utility>
71
73
74namespace SkImages {
75
77 auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, nullptr,
79 return DeferredFromGenerator(std::move(gen));
80}
81
83 SkAlphaType at,
85 GrSurfaceOrigin surfaceOrigin) {
86 auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, cs, surfaceOrigin);
87 return DeferredFromGenerator(std::move(gen));
88}
89
91 const SkPixmap& pixmap,
92 AHardwareBuffer* hardwareBuffer,
93 GrSurfaceOrigin surfaceOrigin) {
94 AHardwareBuffer_Desc bufferDesc;
95 AHardwareBuffer_describe(hardwareBuffer, &bufferDesc);
96
97 if (!SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE)) {
98 return nullptr;
99 }
100
101
102 GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(dContext,
103 hardwareBuffer,
104 bufferDesc.format,
105 true);
106
107 if (!backendFormat.isValid()) {
108 return nullptr;
109 }
110
111 GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr;
112 GrAHardwareBufferUtils::UpdateImageProc updateImageProc = nullptr;
113 GrAHardwareBufferUtils::TexImageCtx deleteImageCtx = nullptr;
114
115 const bool isRenderable = SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER);
116
117 GrBackendTexture backendTexture =
118 GrAHardwareBufferUtils::MakeBackendTexture(dContext, hardwareBuffer,
119 bufferDesc.width, bufferDesc.height,
120 &deleteImageProc, &updateImageProc,
121 &deleteImageCtx, false, backendFormat,
122 isRenderable);
123 if (!backendTexture.isValid()) {
124 return nullptr;
125 }
126 SkASSERT(deleteImageProc);
127
128 auto releaseHelper = skgpu::RefCntedCallback::Make(deleteImageProc, deleteImageCtx);
129
131 AHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format);
132
134
135 GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
136 if (!proxyProvider) {
137 return nullptr;
138 }
139
140 sk_sp<GrTextureProxy> proxy = proxyProvider->wrapBackendTexture(
142 std::move(releaseHelper));
143 if (!proxy) {
144 return nullptr;
145 }
146
147 skgpu::Swizzle swizzle = dContext->priv().caps()->getReadSwizzle(backendFormat, grColorType);
148 GrSurfaceProxyView framebufferView(std::move(proxy), surfaceOrigin, swizzle);
149 SkColorInfo colorInfo = pixmap.info().colorInfo().makeColorType(colorType);
150 sk_sp<SkImage> image = sk_make_sp<SkImage_Ganesh>(
151 sk_ref_sp(dContext), kNeedNewImageUniqueID, framebufferView, std::move(colorInfo));
152 if (!image) {
153 return nullptr;
154 }
155
156 GrDrawingManager* drawingManager = dContext->priv().drawingManager();
157 if (!drawingManager) {
158 return nullptr;
159 }
160
161 skgpu::ganesh::SurfaceContext surfaceContext(
162 dContext, std::move(framebufferView), image->imageInfo().colorInfo());
163
164 surfaceContext.writePixels(dContext, pixmap, {0, 0});
165
166 GrSurfaceProxy* p[1] = {surfaceContext.asSurfaceProxy()};
167 drawingManager->flush(p, SkSurfaces::BackendSurfaceAccess::kNoAccess, {}, nullptr);
168
169 return image;
170}
171
172} // namespace SkImages
173
174#endif // defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
struct AHardwareBuffer AHardwareBuffer
@ kRW_GrIOType
Definition: GrTypesPriv.h:405
@ kBorrow_GrWrapOwnership
Definition: GrTypesPriv.h:79
GrColorType
Definition: GrTypesPriv.h:540
static constexpr GrColorType SkColorTypeToGrColorType(SkColorType ct)
Definition: GrTypesPriv.h:629
GrSurfaceOrigin
Definition: GrTypes.h:147
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
SkAlphaType
Definition: SkAlphaType.h:26
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkColorType
Definition: SkColorType.h:19
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
@ kNeedNewImageUniqueID
Definition: SkImage_Base.h:33
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
static std::unique_ptr< SkImageGenerator > Make(AHardwareBuffer *, SkAlphaType, sk_sp< SkColorSpace >, GrSurfaceOrigin)
bool isValid() const
bool isValid() const
const GrCaps * caps() const
skgpu::Swizzle getReadSwizzle(const GrBackendFormat &format, GrColorType colorType) const
Definition: GrCaps.cpp:443
GrDirectContextPriv priv()
bool flush(SkSpan< GrSurfaceProxy * > proxies, SkSurfaces::BackendSurfaceAccess access, const GrFlushInfo &, const skgpu::MutableTextureState *newState)
sk_sp< GrTextureProxy > wrapBackendTexture(const GrBackendTexture &, GrWrapOwnership, GrWrapCacheable, GrIOType, sk_sp< skgpu::RefCntedCallback >=nullptr)
GrDrawingManager * drawingManager()
GrProxyProvider * proxyProvider()
SkColorInfo makeColorType(SkColorType newColorType) const
Definition: SkImageInfo.cpp:80
const SkImageInfo & imageInfo() const
Definition: SkImage.h:279
const SkImageInfo & info() const
Definition: SkPixmap.h:135
static sk_sp< RefCntedCallback > Make(Callback proc, Context ctx)
SK_API sk_sp< SkImage > DeferredFromGenerator(std::unique_ptr< SkImageGenerator > imageGenerator)
SK_API sk_sp< SkImage > DeferredFromAHardwareBuffer(AHardwareBuffer *hardwareBuffer, SkAlphaType alphaType=kPremul_SkAlphaType)
SK_API sk_sp< SkImage > TextureFromAHardwareBufferWithData(GrDirectContext *context, const SkPixmap &pixmap, AHardwareBuffer *hardwareBuffer, GrSurfaceOrigin surfaceOrigin=kTopLeft_GrSurfaceOrigin)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
@ kNoAccess
back-end surface will not be used by client
Definition: gen.py:1
const SkColorInfo & colorInfo() const
Definition: SkImageInfo.h:404