Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement validateBufferSize() method #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion gralloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ static int gbm_mod_lock_ycbcr(gralloc_module_t const *mod, buffer_handle_t handl
return err;
}

static int32_t gbm_mod_validate_buffer_size(const gralloc_module_t *mod,
buffer_handle_t handle, uint32_t w, uint32_t h, int32_t format, int usage, uint32_t stride)
{
struct gbm_module_t *dmod = (struct gbm_module_t *) mod;
int err;

pthread_mutex_lock(&dmod->mutex);
err = gbm_validate_buffer_size(handle, w, h, format, usage, stride);
pthread_mutex_unlock(&dmod->mutex);

return err;
}

static int gbm_mod_close_gpu0(struct hw_device_t *dev)
{
struct gbm_module_t *dmod = (struct gbm_module_t *)dev->module;
Expand Down Expand Up @@ -265,7 +278,8 @@ struct gbm_module_t HAL_MODULE_INFO_SYM = {
.lock = gbm_mod_lock,
.unlock = gbm_mod_unlock,
.lock_ycbcr = gbm_mod_lock_ycbcr,
.perform = gbm_mod_perform
.perform = gbm_mod_perform,
.validateBufferSize = gbm_mod_validate_buffer_size
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this function exist on older Android versions?

},

.mutex = PTHREAD_MUTEX_INITIALIZER,
Expand Down
45 changes: 45 additions & 0 deletions gralloc_gbm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,48 @@ int gralloc_gbm_bo_lock_ycbcr(buffer_handle_t handle,

return 0;
}

/*
* Validate that the buffer can be safely accessed by a caller who assumes
* the specified width, height, format, usage, and stride.
*/
int32_t gbm_validate_buffer_size(buffer_handle_t handle, uint32_t w, uint32_t h, int32_t format,
int usage, uint32_t stride)
{
struct gralloc_handle_t *gbm_handle = gralloc_handle(handle);

if (!gbm_handle) {
ALOGE("buffer validation unsuccess, bad handle: %p", gbm_handle);
return -EINVAL;
}

if (w != gbm_handle->width) {
ALOGE("buffer validation unsuccess, specified width %d requires %d", w, gbm_handle->width);
return -ENOMEM;
}

if (h != gbm_handle->height) {
ALOGE("buffer validation unsuccess, specified height %d requires %d", h, gbm_handle->height);
return -ENOMEM;
}

if (format != gbm_handle->format) {
ALOGE("buffer validation unsuccess, specified format 0x%x requires 0x%x", format,
gbm_handle->format);
return -ENOMEM;
}

if (usage != gbm_handle->usage) {
ALOGE("buffer validation unsuccess, specified usage 0x%x requires 0x%x", usage,
gbm_handle->usage);
return -ENOMEM;
}

if (stride * gralloc_gbm_get_bpp(format) != gbm_handle->stride) {
ALOGE("buffer validation unsuccess, specified stride %d requires %d",
stride * gralloc_gbm_get_bpp(format), gbm_handle->stride);
return -ENOMEM;
}

return 0;
}
3 changes: 3 additions & 0 deletions gralloc_gbm_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ int gralloc_gbm_bo_unlock(buffer_handle_t handle);
int gralloc_gbm_bo_lock_ycbcr(buffer_handle_t handle, int usage,
int x, int y, int w, int h, struct android_ycbcr *ycbcr);

int32_t gbm_validate_buffer_size(buffer_handle_t handle, uint32_t w, uint32_t h, int32_t format,
int usage, uint32_t stride);

struct gbm_device *gbm_dev_create(void);
void gbm_dev_destroy(struct gbm_device *gbm);

Expand Down