Skip to content

Commit

Permalink
fix [Pixel]BufferDescriptor functor callbacks
Browse files Browse the repository at this point in the history
and attempt to make functor<->callback code less confusing.
  • Loading branch information
pixelflinger committed Nov 10, 2023
1 parent e8bed52 commit 1a0b5dd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
20 changes: 10 additions & 10 deletions filament/backend/include/backend/BufferDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,20 @@ class UTILS_PUBLIC BufferDescriptor {
/**
* Helper to create a BufferDescriptor that uses a KNOWN method pointer w/ object passed
* by pointer as the callback. e.g.:
* auto bd = BufferDescriptor::make(buffer, size, &Foo::method, foo);
* auto bd = BufferDescriptor::make<Foo, &Foo::method>(buffer, size, foo);
*
* @param buffer Memory address of the CPU buffer to reference
* @param size Size of the CPU buffer in bytes
* @param handler Handler to use to dispatch the callback, or nullptr for the default handler
* @return a new BufferDescriptor
*/
template<typename T, void(T::*method)(void const*, size_t)>
static BufferDescriptor make(
void const* buffer, size_t size, T* data, CallbackHandler* handler = nullptr) noexcept {
static BufferDescriptor make(void const* buffer, size_t size, T* data,
CallbackHandler* handler = nullptr) noexcept {
return {
buffer, size,
handler, [](void* b, size_t s, void* u) {
(*static_cast<T**>(u)->*method)(b, s);
(static_cast<T*>(u)->*method)(b, s);
}, data
};
}
Expand All @@ -145,14 +145,14 @@ class UTILS_PUBLIC BufferDescriptor {
* @return a new BufferDescriptor
*/
template<typename T>
static BufferDescriptor make(
void const* buffer, size_t size, T&& functor, CallbackHandler* handler = nullptr) noexcept {
static BufferDescriptor make(void const* buffer, size_t size, T&& functor,
CallbackHandler* handler = nullptr) noexcept {
return {
buffer, size,
handler, [](void* b, size_t s, void* u) {
T& that = *static_cast<T*>(u);
that(b, s);
delete &that;
T* const that = static_cast<T*>(u);
that->operator()(b, s);
delete that;
},
new T(std::forward<T>(functor))
};
Expand Down Expand Up @@ -201,7 +201,7 @@ class UTILS_PUBLIC BufferDescriptor {
return mUser;
}

//! CPU mempry-buffer virtual address
//! CPU memory-buffer virtual address
void* buffer = nullptr;

//! CPU memory-buffer size in bytes
Expand Down
24 changes: 12 additions & 12 deletions filament/backend/include/backend/PixelBufferDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,23 @@ class UTILS_PUBLIC PixelBufferDescriptor : public BufferDescriptor {
CallbackHandler* handler = nullptr) noexcept {
return { buffer, size, format, type, alignment, left, top, stride,
handler, [](void* b, size_t s, void* u) {
(*static_cast<T**>(u)->*method)(b, s); }, data };
(static_cast<T*>(u)->*method)(b, s); }, data };
}

template<typename T, void(T::*method)(void const*, size_t)>
static PixelBufferDescriptor make(void const* buffer, size_t size,
PixelDataFormat format, PixelDataType type, T* data,
CallbackHandler* handler = nullptr) noexcept {
return { buffer, size, format, type, handler, [](void* b, size_t s, void* u) {
(*static_cast<T**>(u)->*method)(b, s); }, data };
(static_cast<T*>(u)->*method)(b, s); }, data };
}

template<typename T, void(T::*method)(void const*, size_t)>
static PixelBufferDescriptor make(void const* buffer, size_t size,
backend::CompressedPixelDataType format, uint32_t imageSize, T* data,
CallbackHandler* handler = nullptr) noexcept {
return { buffer, size, format, imageSize, handler, [](void* b, size_t s, void* u) {
(*static_cast<T**>(u)->*method)(b, s); }, data
(static_cast<T*>(u)->*method)(b, s); }, data
};
}

Expand All @@ -168,9 +168,9 @@ class UTILS_PUBLIC PixelBufferDescriptor : public BufferDescriptor {
CallbackHandler* handler = nullptr) noexcept {
return { buffer, size, format, type, alignment, left, top, stride,
handler, [](void* b, size_t s, void* u) {
T& that = *static_cast<T*>(u);
that(b, s);
delete &that;
T* const that = static_cast<T*>(u);
that->operator()(b, s);
delete that;
}, new T(std::forward<T>(functor))
};
}
Expand All @@ -181,9 +181,9 @@ class UTILS_PUBLIC PixelBufferDescriptor : public BufferDescriptor {
CallbackHandler* handler = nullptr) noexcept {
return { buffer, size, format, type,
handler, [](void* b, size_t s, void* u) {
T& that = *static_cast<T*>(u);
that(b, s);
delete &that;
T* const that = static_cast<T*>(u);
that->operator()(b, s);
delete that;
}, new T(std::forward<T>(functor))
};
}
Expand All @@ -194,9 +194,9 @@ class UTILS_PUBLIC PixelBufferDescriptor : public BufferDescriptor {
CallbackHandler* handler = nullptr) noexcept {
return { buffer, size, format, imageSize,
handler, [](void* b, size_t s, void* u) {
T& that = *static_cast<T*>(u);
that(b, s);
delete &that;
T* const that = static_cast<T*>(u);
that->operator()(b, s);
delete that;
}, new T(std::forward<T>(functor))
};
}
Expand Down
24 changes: 12 additions & 12 deletions filament/include/filament/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,10 @@ class UTILS_PUBLIC View : public FilamentAPI {
* @param handler Handler to dispatch the callback or nullptr for the default handler.
*/
template<typename T, void(T::*method)(PickingQueryResult const&)>
void pick(uint32_t x, uint32_t y, T* instance, backend::CallbackHandler* handler = nullptr) noexcept {
void pick(uint32_t x, uint32_t y, T* instance,
backend::CallbackHandler* handler = nullptr) noexcept {
PickingQuery& query = pick(x, y, [](PickingQueryResult const& result, PickingQuery* pq) {
void* user = pq->storage;
(*static_cast<T**>(user)->*method)(result);
(static_cast<T*>(pq->storage[0])->*method)(result);
}, handler);
query.storage[0] = instance;
}
Expand All @@ -762,11 +762,11 @@ class UTILS_PUBLIC View : public FilamentAPI {
* @param handler Handler to dispatch the callback or nullptr for the default handler.
*/
template<typename T, void(T::*method)(PickingQueryResult const&)>
void pick(uint32_t x, uint32_t y, T instance, backend::CallbackHandler* handler = nullptr) noexcept {
void pick(uint32_t x, uint32_t y, T instance,
backend::CallbackHandler* handler = nullptr) noexcept {
static_assert(sizeof(instance) <= sizeof(PickingQuery::storage), "user data too large");
PickingQuery& query = pick(x, y, [](PickingQueryResult const& result, PickingQuery* pq) {
void* user = pq->storage;
T* that = static_cast<T*>(user);
T* const that = static_cast<T*>(reinterpret_cast<void*>(pq->storage));
(that->*method)(result);
that->~T();
}, handler);
Expand All @@ -783,15 +783,15 @@ class UTILS_PUBLIC View : public FilamentAPI {
* @param handler Handler to dispatch the callback or nullptr for the default handler.
*/
template<typename T>
void pick(uint32_t x, uint32_t y, T functor, backend::CallbackHandler* handler = nullptr) noexcept {
void pick(uint32_t x, uint32_t y, T functor,
backend::CallbackHandler* handler = nullptr) noexcept {
static_assert(sizeof(functor) <= sizeof(PickingQuery::storage), "functor too large");
PickingQuery& query = pick(x, y, handler,
(PickingQueryResultCallback)[](PickingQueryResult const& result, PickingQuery* pq) {
void* user = pq->storage;
T& that = *static_cast<T*>(user);
that(result);
that.~T();
});
T* const that = static_cast<T*>(reinterpret_cast<void*>(pq->storage));
that->operator()(result);
that->~T();
});
new(query.storage) T(std::move(functor));
}

Expand Down
19 changes: 10 additions & 9 deletions libs/utils/include/utils/JobSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ class JobSystem {
// the caller must ensure the object will outlive the Job
template<typename T, void(T::*method)(JobSystem&, Job*)>
Job* createJob(Job* parent, T* data) noexcept {
Job* job = create(parent, [](void* user, JobSystem& js, Job* job) {
(*static_cast<T**>(user)->*method)(js, job);
Job* job = create(parent, +[](void* storage, JobSystem& js, Job* job) {
T* const that = static_cast<T*>(reinterpret_cast<void**>(storage)[0]);
(that->*method)(js, job);
});
if (job) {
job->storage[0] = data;
Expand All @@ -182,8 +183,8 @@ class JobSystem {
template<typename T, void(T::*method)(JobSystem&, Job*)>
Job* createJob(Job* parent, T data) noexcept {
static_assert(sizeof(data) <= sizeof(Job::storage), "user data too large");
Job* job = create(parent, [](void* user, JobSystem& js, Job* job) {
T* that = static_cast<T*>(user);
Job* job = create(parent, [](void* storage, JobSystem& js, Job* job) {
T* const that = static_cast<T*>(storage);
(that->*method)(js, job);
that->~T();
});
Expand All @@ -197,10 +198,10 @@ class JobSystem {
template<typename T>
Job* createJob(Job* parent, T functor) noexcept {
static_assert(sizeof(functor) <= sizeof(Job::storage), "functor too large");
Job* job = create(parent, [](void* user, JobSystem& js, Job* job){
T& that = *static_cast<T*>(user);
that(js, job);
that.~T();
Job* job = create(parent, [](void* storage, JobSystem& js, Job* job){
T* const that = static_cast<T*>(storage);
that->operator()(js, job);
that->~T();
});
if (job) {
new(job->storage) T(std::move(functor));
Expand Down Expand Up @@ -252,7 +253,7 @@ class JobSystem {
void signal() noexcept;

/*
* Add job to this thread's execution queue and and keep a reference to it.
* Add job to this thread's execution queue and keep a reference to it.
* Current thread must be owned by JobSystem's thread pool. See adopt().
*
* This job MUST BE waited on with wait(), or released with release().
Expand Down

0 comments on commit 1a0b5dd

Please sign in to comment.