Skip to content

Commit

Permalink
gralloc: Remove opaque types
Browse files Browse the repository at this point in the history
Remove opaque types like size_t, uintptr_t, intptr_t to support
32bit and 64bit processes together.

When a 64bit process creates a handle and a 32bit process validates
the incoming ints against expected ints, opaque types lead to
different and mismatching values.

Always use unit64_t for base address for 32bit and 64bit SF.
Use unsigned int for offset and size, since ION uses that.

Change-Id: I7db5544556a8924f98010b965f837592e9f0b4ca
  • Loading branch information
Saurabh Shah authored and Simon Wilson committed Jan 9, 2015
1 parent ab05b00 commit 92e456f
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 92 deletions.
22 changes: 11 additions & 11 deletions msm8226/libgralloc/alloc_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ bool isMacroTileEnabled(int format, int usage)
}

// helper function
size_t getSize(int format, int width, int height, const int alignedw,
unsigned int getSize(int format, int width, int height, const int alignedw,
const int alignedh) {
size_t size = 0;
unsigned int size = 0;

switch (format) {
case HAL_PIXEL_FORMAT_RGBA_8888:
Expand Down Expand Up @@ -433,7 +433,7 @@ size_t getSize(int format, int width, int height, const int alignedw,
}
size = alignedw*alignedh +
(ALIGN(alignedw/2, 16) * (alignedh/2))*2;
size = ALIGN(size, (size_t)4096);
size = ALIGN(size, (unsigned int)4096);
break;
case HAL_PIXEL_FORMAT_YCbCr_420_SP:
case HAL_PIXEL_FORMAT_YCrCb_420_SP:
Expand Down Expand Up @@ -501,10 +501,10 @@ size_t getSize(int format, int width, int height, const int alignedw,
return size;
}

size_t getBufferSizeAndDimensions(int width, int height, int format,
unsigned int getBufferSizeAndDimensions(int width, int height, int format,
int& alignedw, int &alignedh)
{
size_t size;
unsigned int size;

AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
height,
Expand All @@ -519,10 +519,10 @@ size_t getBufferSizeAndDimensions(int width, int height, int format,
}


size_t getBufferSizeAndDimensions(int width, int height, int format, int usage,
int& alignedw, int &alignedh)
unsigned int getBufferSizeAndDimensions(int width, int height, int format,
int usage, int& alignedw, int &alignedh)
{
size_t size;
unsigned int size;
int tileEnabled = isMacroTileEnabled(format, usage);

AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
Expand All @@ -539,7 +539,7 @@ size_t getBufferSizeAndDimensions(int width, int height, int format, int usage,


void getBufferAttributes(int width, int height, int format, int usage,
int& alignedw, int &alignedh, int& tileEnabled, size_t& size)
int& alignedw, int &alignedh, int& tileEnabled, unsigned int& size)
{
tileEnabled = isMacroTileEnabled(format, usage);

Expand All @@ -555,7 +555,7 @@ void getBufferAttributes(int width, int height, int format, int usage,
int getYUVPlaneInfo(private_handle_t* hnd, struct android_ycbcr* ycbcr)
{
int err = 0;
size_t ystride, cstride;
unsigned int ystride, cstride;
memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));

// Get the chroma offsets from the handle width/height. We take advantage
Expand Down Expand Up @@ -647,7 +647,7 @@ int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage)
private_handle_t* hnd = new private_handle_t(data.fd, data.size,
data.allocType, 0, format,
alignedw, alignedh);
hnd->base = (uintptr_t) data.base;
hnd->base = (uint64_t) data.base;
hnd->offset = data.offset;
hnd->gpuaddr = 0;
*pHnd = hnd;
Expand Down
2 changes: 1 addition & 1 deletion msm8226/libgralloc/fb_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct private_module_t {
uint32_t fbFormat;
uint32_t flags;
uint32_t numBuffers;
size_t bufferMask;
uint32_t bufferMask;
pthread_mutex_t lock;
private_handle_t *currentBuffer;
struct fb_var_screeninfo info;
Expand Down
9 changes: 5 additions & 4 deletions msm8226/libgralloc/framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
reinterpret_cast<private_module_t*>(dev->common.module);
private_handle_t *hnd = static_cast<private_handle_t*>
(const_cast<native_handle_t*>(buffer));
const size_t offset = hnd->base - m->framebuffer->base;
const unsigned int offset = (unsigned int) (hnd->base -
m->framebuffer->base);
m->info.activate = FB_ACTIVATE_VBL;
m->info.yoffset = (int)(offset / m->finfo.line_length);
if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
Expand Down Expand Up @@ -204,7 +205,7 @@ int mapFrameBufferLocked(struct private_module_t* module)
}

//adreno needs 4k aligned offsets. Max hole size is 4096-1
size_t size = roundUpToPageSize(info.yres * info.xres *
unsigned int size = roundUpToPageSize(info.yres * info.xres *
(info.bits_per_pixel/8));

/*
Expand Down Expand Up @@ -326,7 +327,7 @@ int mapFrameBufferLocked(struct private_module_t* module)
module->numBuffers = info.yres_virtual / info.yres;
module->bufferMask = 0;
//adreno needs page aligned offsets. Align the fbsize to pagesize.
size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
unsigned int fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
module->numBuffers;
module->framebuffer = new private_handle_t(fd, fbSize,
private_handle_t::PRIV_FLAGS_USES_ION,
Expand All @@ -338,7 +339,7 @@ int mapFrameBufferLocked(struct private_module_t* module)
close(fd);
return -errno;
}
module->framebuffer->base = uintptr_t(vaddr);
module->framebuffer->base = uint64_t(vaddr);
memset(vaddr, 0, fbSize);
//Enable vsync
int enable = 1;
Expand Down
29 changes: 15 additions & 14 deletions msm8226/libgralloc/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ gpu_context_t::gpu_context_t(const private_module_t* module,

}

int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage,
int gpu_context_t::gralloc_alloc_buffer(unsigned int size, int usage,
buffer_handle_t* pHandle, int bufferType,
int format, int width, int height)
{
Expand Down Expand Up @@ -152,13 +152,13 @@ int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage,
}

flags |= data.allocType;
uintptr_t eBaseAddr = (uintptr_t)(eData.base) + eData.offset;
uint64_t eBaseAddr = (uint64_t)(eData.base) + eData.offset;
private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
bufferType, format, width, height, eData.fd, eData.offset,
eBaseAddr);

hnd->offset = data.offset;
hnd->base = (uintptr_t)(data.base) + data.offset;
hnd->base = (uint64_t)(data.base) + data.offset;
hnd->gpuaddr = 0;
setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace);

Expand Down Expand Up @@ -199,9 +199,9 @@ int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage,
return -EINVAL;
}

const size_t bufferMask = m->bufferMask;
const unsigned int bufferMask = m->bufferMask;
const uint32_t numBuffers = m->numBuffers;
size_t bufferSize = m->finfo.line_length * m->info.yres;
unsigned int bufferSize = m->finfo.line_length * m->info.yres;

//adreno needs FB size to be page aligned
bufferSize = roundUpToPageSize(bufferSize);
Expand All @@ -221,7 +221,7 @@ int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage,
}

// create a "fake" handle for it
uintptr_t vaddr = uintptr_t(m->framebuffer->base);
uint64_t vaddr = uint64_t(m->framebuffer->base);
private_handle_t* hnd = new private_handle_t(
dup(m->framebuffer->fd), bufferSize,
private_handle_t::PRIV_FLAGS_USES_PMEM |
Expand All @@ -238,7 +238,7 @@ int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage,
vaddr += bufferSize;
}
hnd->base = vaddr;
hnd->offset = vaddr - uintptr_t(m->framebuffer->base);
hnd->offset = (unsigned int)(vaddr - m->framebuffer->base);
*pHandle = hnd;
return 0;
}
Expand All @@ -256,11 +256,11 @@ int gpu_context_t::gralloc_alloc_framebuffer(int usage,

int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
buffer_handle_t* pHandle, int* pStride,
size_t bufferSize) {
unsigned int bufferSize) {
if (!pHandle || !pStride)
return -EINVAL;

size_t size;
unsigned int size;
int alignedw, alignedh;
int grallocFormat = format;
int bufferType;
Expand All @@ -287,7 +287,7 @@ int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw,
alignedh);

if ((ssize_t)size <= 0)
if ((unsigned int)size <= 0)
return -EINVAL;
size = (bufferSize >= size)? bufferSize : size;

Expand Down Expand Up @@ -319,19 +319,20 @@ int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
int gpu_context_t::free_impl(private_handle_t const* hnd) {
private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
const size_t bufferSize = m->finfo.line_length * m->info.yres;
size_t index = (hnd->base - m->framebuffer->base) / bufferSize;
const unsigned int bufferSize = m->finfo.line_length * m->info.yres;
unsigned int index = (unsigned int) ((hnd->base - m->framebuffer->base)
/ bufferSize);
m->bufferMask &= ~(1LU<<index);
} else {

terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
int err = memalloc->free_buffer((void*)hnd->base, hnd->size,
hnd->offset, hnd->fd);
if(err)
return err;
// free the metadata space
size_t size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
err = memalloc->free_buffer((void*)hnd->base_metadata,
size, hnd->offset_metadata,
hnd->fd_metadata);
Expand Down
4 changes: 2 additions & 2 deletions msm8226/libgralloc/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class gpu_context_t : public alloc_device_t {
gpu_context_t(const private_module_t* module,
IAllocController* alloc_ctrl);

int gralloc_alloc_buffer(size_t size, int usage,
int gralloc_alloc_buffer(unsigned int size, int usage,
buffer_handle_t* pHandle,
int bufferType, int format,
int width, int height);
Expand All @@ -44,7 +44,7 @@ class gpu_context_t : public alloc_device_t {

int alloc_impl(int w, int h, int format, int usage,
buffer_handle_t* pHandle, int* pStride,
size_t bufferSize = 0);
unsigned int bufferSize = 0);

static int gralloc_alloc(alloc_device_t* dev, int w, int h,
int format, int usage,
Expand Down
12 changes: 6 additions & 6 deletions msm8226/libgralloc/gr.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
struct private_module_t;
struct private_handle_t;

inline size_t roundUpToPageSize(size_t x) {
inline unsigned int roundUpToPageSize(unsigned int x) {
return (x + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
}

Expand All @@ -47,16 +47,16 @@ inline Type ALIGN(Type x, Type align) {

int mapFrameBufferLocked(struct private_module_t* module);
int terminateBuffer(gralloc_module_t const* module, private_handle_t* hnd);
size_t getBufferSizeAndDimensions(int width, int height, int format, int usage,
int& alignedw, int &alignedh);
size_t getBufferSizeAndDimensions(int width, int height, int format,
int& alignedw, int &alignedh);
unsigned int getBufferSizeAndDimensions(int width, int height, int format,
int usage, int& alignedw, int &alignedh);
unsigned int getBufferSizeAndDimensions(int width, int height, int format,
int& alignedw, int &alignedh);


// Attributes include aligned width, aligned height, tileEnabled and size of the buffer
void getBufferAttributes(int width, int height, int format, int usage,
int& alignedw, int &alignedh,
int& tileEnabled, size_t &size);
int& tileEnabled, unsigned int &size);


bool isMacroTileEnabled(int format, int usage);
Expand Down
31 changes: 16 additions & 15 deletions msm8226/libgralloc/gralloc_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,36 +211,37 @@ struct private_handle_t : public native_handle {
// ints
int magic;
int flags;
size_t size;
size_t offset;
unsigned int size;
unsigned int offset;
int bufferType;
uintptr_t base;
size_t offset_metadata;
uint64_t base __attribute__((aligned(8)));
unsigned int offset_metadata;
// The gpu address mapped into the mmu.
uintptr_t gpuaddr;
uint64_t gpuaddr __attribute__((aligned(8)));
int format;
int width;
int height;
uintptr_t base_metadata;
uint64_t base_metadata __attribute__((aligned(8)));

#ifdef __cplusplus
//TODO64: Revisit this on 64-bit
static const int sNumInts = (6 + (3 * (sizeof(size_t)/sizeof(int))) +
(3 * (sizeof(uintptr_t)/sizeof(int))));
static const int sNumFds = 2;
static inline int sNumInts() {
return ((sizeof(private_handle_t) - sizeof(native_handle_t)) /
sizeof(int)) - sNumFds;
}
static const int sMagic = 'gmsm';

private_handle_t(int fd, size_t size, int flags, int bufferType,
private_handle_t(int fd, unsigned int size, int flags, int bufferType,
int format, int width, int height, int eFd = -1,
size_t eOffset = 0, uintptr_t eBase = 0) :
unsigned int eOffset = 0, uint64_t eBase = 0) :
fd(fd), fd_metadata(eFd), magic(sMagic),
flags(flags), size(size), offset(0), bufferType(bufferType),
base(0), offset_metadata(eOffset), gpuaddr(0),
format(format), width(width), height(height),
base_metadata(eBase)
{
version = (int) sizeof(native_handle);
numInts = sNumInts;
numInts = sNumInts();
numFds = sNumFds;
}
~private_handle_t() {
Expand All @@ -254,15 +255,15 @@ struct private_handle_t : public native_handle {
static int validate(const native_handle* h) {
const private_handle_t* hnd = (const private_handle_t*)h;
if (!h || h->version != sizeof(native_handle) ||
h->numInts != sNumInts || h->numFds != sNumFds ||
h->numInts != sNumInts() || h->numFds != sNumFds ||
hnd->magic != sMagic)
{
ALOGD("Invalid gralloc handle (at %p): "
"ver(%d/%zu) ints(%d/%d) fds(%d/%d)"
"ver(%d/%u) ints(%d/%d) fds(%d/%d)"
"magic(%c%c%c%c/%c%c%c%c)",
h,
h ? h->version : -1, sizeof(native_handle),
h ? h->numInts : -1, sNumInts,
h ? h->numInts : -1, sNumInts(),
h ? h->numFds : -1, sNumFds,
hnd ? (((hnd->magic >> 24) & 0xFF)?
((hnd->magic >> 24) & 0xFF) : '-') : '?',
Expand Down
Loading

0 comments on commit 92e456f

Please sign in to comment.