• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
11 
12 #include "include/android/SkImageAndroid.h"
13 
14 #include "include/android/AHardwareBufferUtils.h"
15 #include "include/android/GrAHardwareBufferUtils.h"
16 #include "include/core/SkAlphaType.h"
17 #include "include/core/SkBitmap.h"
18 #include "include/core/SkColorSpace.h"
19 #include "include/core/SkData.h"
20 #include "include/core/SkImage.h"
21 #include "include/core/SkImageGenerator.h"
22 #include "include/core/SkImageInfo.h"
23 #include "include/core/SkPixmap.h"
24 #include "include/core/SkRect.h"
25 #include "include/core/SkSize.h"
26 #include "include/core/SkSurface.h"
27 #include "include/gpu/GpuTypes.h"
28 #include "include/gpu/ganesh/GrBackendSurface.h"
29 #include "include/gpu/ganesh/GrContextThreadSafeProxy.h"
30 #include "include/gpu/ganesh/GrDirectContext.h"
31 #include "include/gpu/ganesh/GrExternalTextureGenerator.h"
32 #include "include/gpu/ganesh/GrRecordingContext.h"
33 #include "include/gpu/ganesh/GrTypes.h"
34 #include "include/gpu/ganesh/SkImageGanesh.h"
35 #include "include/private/base/SkAssert.h"
36 #include "include/private/gpu/ganesh/GrImageContext.h"
37 #include "include/private/gpu/ganesh/GrTypesPriv.h"
38 #include "src/core/SkAutoPixmapStorage.h"
39 #include "src/core/SkImageInfoPriv.h"
40 #include "src/gpu/RefCntedCallback.h"
41 #include "src/gpu/SkBackingFit.h"
42 #include "src/gpu/ganesh/GrAHardwareBufferImageGenerator.h"
43 #include "src/gpu/ganesh/GrBackendTextureImageGenerator.h"
44 #include "src/gpu/ganesh/GrBackendUtils.h"
45 #include "src/gpu/ganesh/GrCaps.h"
46 #include "src/gpu/ganesh/GrColorInfo.h"
47 #include "src/gpu/ganesh/GrColorSpaceXform.h"
48 #include "src/gpu/ganesh/GrContextThreadSafeProxyPriv.h"
49 #include "src/gpu/ganesh/GrDirectContextPriv.h"
50 #include "src/gpu/ganesh/GrDrawingManager.h"
51 #include "src/gpu/ganesh/GrFragmentProcessor.h"
52 #include "src/gpu/ganesh/GrGpu.h"
53 #include "src/gpu/ganesh/GrGpuResourcePriv.h"
54 #include "src/gpu/ganesh/GrImageContextPriv.h"
55 #include "src/gpu/ganesh/GrImageInfo.h"
56 #include "src/gpu/ganesh/GrProxyProvider.h"
57 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
58 #include "src/gpu/ganesh/GrRenderTask.h"
59 #include "src/gpu/ganesh/GrSemaphore.h"
60 #include "src/gpu/ganesh/GrSurfaceProxy.h"
61 #include "src/gpu/ganesh/GrTexture.h"
62 #include "src/gpu/ganesh/GrTextureProxy.h"
63 #include "src/gpu/ganesh/SkGr.h"
64 #include "src/gpu/ganesh/SurfaceContext.h"
65 #include "src/gpu/ganesh/SurfaceFillContext.h"
66 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
67 #include "src/gpu/ganesh/image/SkImage_Ganesh.h"
68 #include "src/image/SkImage_Base.h"
69 
70 #include <algorithm>
71 #include <cstddef>
72 #include <utility>
73 
74 #include <android/hardware_buffer.h>
75 
76 namespace SkImages {
77 
DeferredFromAHardwareBuffer(AHardwareBuffer * graphicBuffer,SkAlphaType at)78 sk_sp<SkImage> DeferredFromAHardwareBuffer(AHardwareBuffer* graphicBuffer, SkAlphaType at) {
79     auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, nullptr,
80                                                      kTopLeft_GrSurfaceOrigin);
81     return DeferredFromTextureGenerator(std::move(gen));
82 }
83 
DeferredFromAHardwareBuffer(AHardwareBuffer * graphicBuffer,SkAlphaType at,sk_sp<SkColorSpace> cs,GrSurfaceOrigin surfaceOrigin)84 sk_sp<SkImage> DeferredFromAHardwareBuffer(AHardwareBuffer* graphicBuffer,
85                                            SkAlphaType at,
86                                            sk_sp<SkColorSpace> cs,
87                                            GrSurfaceOrigin surfaceOrigin) {
88     auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, cs, surfaceOrigin);
89     return DeferredFromTextureGenerator(std::move(gen));
90 }
91 
TextureFromAHardwareBufferWithData(GrDirectContext * dContext,const SkPixmap & pixmap,AHardwareBuffer * hardwareBuffer,GrSurfaceOrigin surfaceOrigin)92 sk_sp<SkImage> TextureFromAHardwareBufferWithData(GrDirectContext* dContext,
93                                                   const SkPixmap& pixmap,
94                                                   AHardwareBuffer* hardwareBuffer,
95                                                   GrSurfaceOrigin surfaceOrigin) {
96     AHardwareBuffer_Desc bufferDesc;
97     AHardwareBuffer_describe(hardwareBuffer, &bufferDesc);
98 
99     if (!SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE)) {
100         return nullptr;
101     }
102 
103 
104     GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(dContext,
105                                                                              hardwareBuffer,
106                                                                              bufferDesc.format,
107                                                                              true);
108 
109     if (!backendFormat.isValid()) {
110         return nullptr;
111     }
112 
113     GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr;
114     GrAHardwareBufferUtils::UpdateImageProc updateImageProc = nullptr;
115     GrAHardwareBufferUtils::TexImageCtx deleteImageCtx = nullptr;
116 
117     const bool isRenderable = SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER);
118 
119     GrBackendTexture backendTexture =
120             GrAHardwareBufferUtils::MakeBackendTexture(dContext, hardwareBuffer,
121                                                        bufferDesc.width, bufferDesc.height,
122                                                        &deleteImageProc, &updateImageProc,
123                                                        &deleteImageCtx, false, backendFormat,
124                                                        isRenderable);
125     if (!backendTexture.isValid()) {
126         return nullptr;
127     }
128     SkASSERT(deleteImageProc);
129 
130     auto releaseHelper = skgpu::RefCntedCallback::Make(deleteImageProc, deleteImageCtx);
131 
132     SkColorType colorType =
133             AHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format);
134 
135     GrColorType grColorType = SkColorTypeToGrColorType(colorType);
136 
137     GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
138     if (!proxyProvider) {
139         return nullptr;
140     }
141 
142     sk_sp<GrTextureProxy> proxy = proxyProvider->wrapBackendTexture(
143             backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType,
144             std::move(releaseHelper));
145     if (!proxy) {
146         return nullptr;
147     }
148 
149     skgpu::Swizzle swizzle = dContext->priv().caps()->getReadSwizzle(backendFormat, grColorType);
150     GrSurfaceProxyView framebufferView(std::move(proxy), surfaceOrigin, swizzle);
151     SkColorInfo colorInfo = pixmap.info().colorInfo().makeColorType(colorType);
152     sk_sp<SkImage> image = sk_make_sp<SkImage_Ganesh>(
153             sk_ref_sp(dContext), kNeedNewImageUniqueID, framebufferView, std::move(colorInfo));
154     if (!image) {
155         return nullptr;
156     }
157 
158     GrDrawingManager* drawingManager = dContext->priv().drawingManager();
159     if (!drawingManager) {
160         return nullptr;
161     }
162 
163     skgpu::ganesh::SurfaceContext surfaceContext(
164             dContext, std::move(framebufferView), image->imageInfo().colorInfo());
165 
166     surfaceContext.writePixels(dContext, pixmap, {0, 0});
167 
168     GrSurfaceProxy* p[1] = {surfaceContext.asSurfaceProxy()};
169     drawingManager->flush(p, SkSurfaces::BackendSurfaceAccess::kNoAccess, {}, nullptr);
170 
171     return image;
172 }
173 
174 }  // namespace SkImages
175 
176 #endif // defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
177