| 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/AHardwareBufferUtils.h" |
| 13 |
#include "include/android/GrAHardwareBufferUtils.h" |
| 14 |
#include "include/android/SkSurfaceAndroid.h" |
| 15 |
#include "include/core/SkCanvas.h" |
| 16 |
#include "include/core/SkColorSpace.h" |
| 17 |
#include "include/core/SkColorType.h" |
| 18 |
#include "include/core/SkImage.h" |
| 19 |
#include "include/core/SkPoint.h" |
| 20 |
#include "include/core/SkRect.h" |
| 21 |
#include "include/core/SkSize.h" |
| 22 |
#include "include/core/SkSurface.h" |
| 23 |
#include "include/core/SkSurfaceProps.h" |
| 24 |
#include "include/gpu/GpuTypes.h" |
| 25 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 26 |
#include "include/gpu/ganesh/GrContextThreadSafeProxy.h" |
| 27 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 28 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 29 |
#include "include/gpu/ganesh/GrTypes.h" |
| 30 |
#include "include/gpu/ganesh/SkSurfaceGanesh.h" |
| 31 |
#include "include/private/base/SkTo.h" |
| 32 |
#include "include/private/chromium/GrSurfaceCharacterization.h" |
| 33 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 34 |
#include "src/core/SkDevice.h" |
| 35 |
#include "src/core/SkSurfacePriv.h" |
| 36 |
#include "src/gpu/RefCntedCallback.h" |
| 37 |
#include "src/gpu/SkBackingFit.h" |
| 38 |
#include "src/gpu/ganesh/Device.h" |
| 39 |
#include "src/gpu/ganesh/GrCaps.h" |
| 40 |
#include "src/gpu/ganesh/GrContextThreadSafeProxyPriv.h" |
| 41 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 42 |
#include "src/gpu/ganesh/GrGpuResourcePriv.h" |
| 43 |
#include "src/gpu/ganesh/GrProxyProvider.h" |
| 44 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 45 |
#include "src/gpu/ganesh/GrRenderTarget.h" |
| 46 |
#include "src/gpu/ganesh/GrRenderTargetProxy.h" |
| 47 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 48 |
#include "src/gpu/ganesh/GrSurfaceProxyPriv.h" |
| 49 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 50 |
#include "src/gpu/ganesh/GrTexture.h" |
| 51 |
#include "src/gpu/ganesh/GrTextureProxy.h" |
| 52 |
#include "src/gpu/ganesh/image/SkImage_Ganesh.h" |
| 53 |
#include "src/gpu/ganesh/surface/SkSurface_Ganesh.h" |
| 54 |
#include "src/image/SkImage_Base.h" |
| 55 |
|
| 56 |
#include <algorithm> |
| 57 |
#include <cstddef> |
| 58 |
#include <utility> |
| 59 |
|
| 60 |
#include <android/hardware_buffer.h> |
| 61 |
#include <inttypes.h> |
| 62 |
|
| 63 |
namespace SkSurfaces { |
| 64 |
|
| 65 |
sk_sp<SkSurface> WrapAndroidHardwareBuffer(GrDirectContext* dContext, |
| 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 |
SkDebugf("%s failed due buffer lacking AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT usage bit", |
| 76 |
__func__); |
| 77 |
return nullptr; |
| 78 |
} |
| 79 |
|
| 80 |
bool isTextureable = SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE); |
| 81 |
if (!isTextureable) { |
| 82 |
SkDebugf("%s failed due buffer lacking AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE usage bit", |
| 83 |
__func__); |
| 84 |
return nullptr; |
| 85 |
} |
| 86 |
|
| 87 |
GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat( |
| 88 |
dContext, hardwareBuffer, bufferDesc.format, true); |
| 89 |
if (!backendFormat.isValid()) { |
| 90 |
SkDebugf("%s requires a known format, but failed due not finding a valid backend format for" |
| 91 |
" %" PRIu32 ".", __func__, bufferDesc.format); |
| 92 |
return nullptr; |
| 93 |
} |
| 94 |
|
| 95 |
GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr; |
| 96 |
GrAHardwareBufferUtils::UpdateImageProc updateImageProc = nullptr; |
| 97 |
GrAHardwareBufferUtils::TexImageCtx deleteImageCtx = nullptr; |
| 98 |
|
| 99 |
bool isProtectedContent = |
| 100 |
SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT); |
| 101 |
|
| 102 |
bool fromWindowLocal = false; |
| 103 |
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 104 |
fromWindowLocal = fromWindow; |
| 105 |
#endif |
| 106 |
|
| 107 |
GrBackendTexture backendTexture = |
| 108 |
GrAHardwareBufferUtils::MakeBackendTexture(dContext, |
| 109 |
hardwareBuffer, |
| 110 |
bufferDesc.width, |
| 111 |
bufferDesc.height, |
| 112 |
&deleteImageProc, |
| 113 |
&updateImageProc, |
| 114 |
&deleteImageCtx, |
| 115 |
isProtectedContent, |
| 116 |
backendFormat, |
| 117 |
true, |
| 118 |
fromWindowLocal); |
| 119 |
if (!backendTexture.isValid()) { |
| 120 |
SkDebugf("%s failed due to backend texture creation failing", __func__); |
| 121 |
return nullptr; |
| 122 |
} |
| 123 |
|
| 124 |
SkColorType colorType = |
| 125 |
AHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format); |
| 126 |
|
| 127 |
// Will call deleteImageProc if SkSurface creation fails. |
| 128 |
sk_sp<SkSurface> surface = SkSurfaces::WrapBackendTexture(dContext, |
| 129 |
backendTexture, |
| 130 |
origin, |
| 131 |
0, |
| 132 |
colorType, |
| 133 |
std::move(colorSpace), |
| 134 |
surfaceProps, |
| 135 |
deleteImageProc, |
| 136 |
deleteImageCtx); |
| 137 |
|
| 138 |
return surface; |
| 139 |
} |
| 140 |
|
| 141 |
} // namespace SkSurfaces |
| 142 |
|
| 143 |
#endif |