Skip to content

Commit

Permalink
Allow the class to call NativeHWBuffer API functions when it is creat…
Browse files Browse the repository at this point in the history
…ed by attaching an existing AHardwareBuffer

Reviewed By: rudybear, corporateshark

Differential Revision: D68986254

fbshipit-source-id: 52bb3fc1d627f5d52f119c9552f005857de394a1
  • Loading branch information
Yijie Wang (FRLR) authored and facebook-github-bot committed Feb 6, 2025
1 parent 1ca69a8 commit 5cd8d2a
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 10 deletions.
17 changes: 17 additions & 0 deletions src/igl/android/NativeHWBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,23 @@ INativeHWTextureBuffer::~INativeHWTextureBuffer() {
hwBuffer_ = nullptr;
}
}
Result INativeHWTextureBuffer::createWithHWBuffer(AHardwareBuffer* buffer) {
if (hwBuffer_) {
return Result{Result::Code::InvalidOperation, "Hardware buffer already provided"};
}

AHardwareBuffer_acquire(buffer);

// textureDesc_ should be updated through createTextureInternal function.
Result result = createTextureInternal(buffer);
if (!result.isOk()) {
AHardwareBuffer_release(buffer);
} else {
hwBuffer_ = buffer;
}

return result;
}

Result INativeHWTextureBuffer::createHWBuffer(const TextureDesc& desc,
bool hasStorageAlready,
Expand Down
4 changes: 3 additions & 1 deletion src/igl/android/NativeHWBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class INativeHWTextureBuffer {

virtual ~INativeHWTextureBuffer();

virtual Result createTextureInternal(AHardwareBuffer* buffer) = 0;
Result createWithHWBuffer(AHardwareBuffer* buffer);

Result createHWBuffer(const TextureDesc& desc, bool hasStorageAlready, bool surfaceComposite);

[[nodiscard]] LockGuard lockHWBuffer(std::byte* IGL_NULLABLE* IGL_NONNULL dst,
Expand All @@ -64,6 +65,7 @@ class INativeHWTextureBuffer {
[[nodiscard]] TextureDesc getTextureDesc() const;

protected:
virtual Result createTextureInternal(AHardwareBuffer* buffer) = 0;
AHardwareBuffer* hwBuffer_ = nullptr;
TextureDesc textureDesc_;
};
Expand Down
2 changes: 1 addition & 1 deletion src/igl/opengl/egl/PlatformDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ std::shared_ptr<ITexture> PlatformDevice::createTextureWithSharedMemory(AHardwar

auto texture = std::make_shared<android::NativeHWTextureBuffer>(
getContext(), igl::android::getIglFormat(hwbDesc.format));
subResult = texture->createTextureInternal(buffer);
subResult = texture->createWithHWBuffer(buffer);
Result::setResult(outResult, subResult.code, subResult.message);
if (!subResult.isOk()) {
return nullptr;
Expand Down
8 changes: 2 additions & 6 deletions src/igl/opengl/egl/android/NativeHWBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ Result NativeHWTextureBuffer::create(const TextureDesc& desc, bool hasStorageAlr
}

Result NativeHWTextureBuffer::createTextureInternal(AHardwareBuffer* buffer) {
if (hwBuffer_) {
return Result{Result::Code::InvalidOperation, "Hardware buffer already provided"};
}

AHardwareBuffer_acquire(buffer);

AHardwareBuffer_Desc hwbDesc;
AHardwareBuffer_describe(buffer, &hwbDesc);

Expand Down Expand Up @@ -142,6 +136,8 @@ Result NativeHWTextureBuffer::createTextureInternal(AHardwareBuffer* buffer) {
hwBufferCtx->elgImage = eglImage;
hwBufferHelper_ = std::static_pointer_cast<AHardwareBufferHelper>(hwBufferCtx);

textureDesc_ = desc;

return Result{};
}

Expand Down
1 change: 1 addition & 0 deletions src/igl/opengl/egl/android/NativeHWBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class NativeHWTextureBuffer : public igl::android::INativeHWTextureBuffer,
setUsage(usage);
}

protected:
Result createTextureInternal(AHardwareBuffer* buffer) override;

private:
Expand Down
2 changes: 1 addition & 1 deletion src/igl/tests/android/NativeHWBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ TEST_F(NativeHWBufferTest, LockBuffer) {
{
NativeHWTextureBufferTest testTxBuffer;

EXPECT_TRUE(testTxBuffer.createTextureInternal(hwBuffer).isOk());
EXPECT_TRUE(testTxBuffer.createWithHWBuffer(hwBuffer).isOk());

std::byte* bytes = nullptr;
INativeHWTextureBuffer::RangeDesc outRange;
Expand Down
2 changes: 1 addition & 1 deletion src/igl/vulkan/PlatformDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ std::shared_ptr<ITexture> PlatformDevice::createTextureWithSharedMemory(

auto texture = std::make_shared<igl::vulkan::android::NativeHWTextureBuffer>(
device_, igl::android::getIglFormat(hwbDesc.format));
subResult = texture->createTextureInternal(buffer);
subResult = texture->createWithHWBuffer(buffer);
Result::setResult(outResult, subResult.code, subResult.message);
if (!subResult.isOk()) {
return nullptr;
Expand Down

0 comments on commit 5cd8d2a

Please sign in to comment.