lock will now return the vaddr of the buffer. map/umap are gone.

- make sure to return an error if a buffer is locked twice or unlocked while not locked.
- added registerBuffer() and unregisterBuffer() to the gralloc module so that we can do some cleanup when a buffer is no longer needed. this became necessary after we removed map/unmap so we have a place to unmap buffers without the need of a kernel module.
- change the constants for GRALLOC_USAGE_SW_{READ|WRITE}_NEVER to 0, so that NOT specifying them means "NEVER".
diff --git a/modules/gralloc/mapper.cpp b/modules/gralloc/mapper.cpp
index a5f52ed..94be43b 100644
--- a/modules/gralloc/mapper.cpp
+++ b/modules/gralloc/mapper.cpp
@@ -38,14 +38,9 @@
     int base;
     int refCount;
 
-    int init(private_handle_t* hnd) {
-        size = hnd->size;
-        base = 0;
-        refCount = 0;
-        struct stat buf;
-        int result = 0;
-        key = intptr_t(hnd);
-        return result;
+    mapped_buffer_t() { /* no init */ };
+    mapped_buffer_t(private_handle_t* hnd) 
+        : key(intptr_t(hnd)), size(hnd->size), base(0), refCount(0) {
     }
 };
 
@@ -68,12 +63,9 @@
             void** vaddr)
     {
         private_handle_t* hnd = (private_handle_t*)(handle);
-        mapped_buffer_t key;
-        int result = key.init(hnd);
+        mapped_buffer_t key(hnd);
         //printRecord(ANDROID_LOG_DEBUG, "map", &key);
-        if (result) {
-            return result;
-        }
+        int result = 0;
         mapped_buffer_t* record = 0;
         pthread_mutex_lock(&mutex);
         // From here to the end of the function we return by jumping to "exit"
@@ -116,11 +108,9 @@
     int unmap(gralloc_module_t const* module,
             buffer_handle_t handle)
     {
-        mapped_buffer_t key;
-        key.init((private_handle_t*) handle);
+        mapped_buffer_t key((private_handle_t*) handle);
         //printRecord(ANDROID_LOG_DEBUG, "unmap", &key);
         int index = -1;
-
         int result = 0;
         mapped_buffer_t* record = 0;
         pthread_mutex_lock(&mutex);
@@ -214,18 +204,114 @@
     return sMappedBuffers.unmap(module, handle);
 }
 
+int gralloc_register_buffer(gralloc_module_t const* module,
+        buffer_handle_t handle)
+{
+    if (private_handle_t::validate(handle) < 0)
+        return -EINVAL;
+    
+    // In this implementation, we don't need to do anything here
+
+    /* FIXME: we need to initialize the buffer as not mapped/not locked
+     * because it shouldn't when this function is called the first time
+     * in a new process. ideally these flags shouldn't be part of the
+     * handle, but instead maintained in the kernel or at least 
+     * out-of-line
+     */ 
+    private_handle_t* hnd = (private_handle_t*)(handle);
+    hnd->base = 0;
+    hnd->flags &= ~(private_handle_t::PRIV_FLAGS_LOCKED | 
+                    private_handle_t::PRIV_FLAGS_MAPPED);
+    
+    return 0;
+}
+
+int gralloc_unregister_buffer(gralloc_module_t const* module,
+        buffer_handle_t handle)
+{
+    if (private_handle_t::validate(handle) < 0)
+        return -EINVAL;
+
+    /*
+     * If the buffer has been mapped during a lock operation, it's time
+     * to unmap it. It's an error to be here with a locked buffer.
+     * NOTE: the framebuffer is handled differently and is never unmapped.
+     */
+
+    private_handle_t* hnd = (private_handle_t*)(handle);
+    
+    LOGE_IF(hnd->flags & private_handle_t::PRIV_FLAGS_LOCKED,
+            "handle %p still locked", hnd);
+
+    if (hnd->flags & private_handle_t::PRIV_FLAGS_MAPPED) {
+        if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
+            gralloc_unmap(module, handle);
+            LOGE_IF(hnd->base,
+                    "handle %p still mapped at %p",
+                    hnd, (void*)hnd->base);
+        }
+    }
+    
+    return 0;
+}
 
 int gralloc_lock(gralloc_module_t const* module,
         buffer_handle_t handle, int usage,
-        int l, int t, int w, int h)
+        int l, int t, int w, int h,
+        void** vaddr)
 {
     // FIXME: gralloc_lock() needs implementation
-    return 0;
+
+    if (private_handle_t::validate(handle) < 0)
+        return -EINVAL;
+
+    int err = 0;
+    private_handle_t* hnd = (private_handle_t*)(handle);
+
+    // already locked
+    if (hnd->flags & private_handle_t::PRIV_FLAGS_LOCKED) {
+        LOGE("handle %p already locked", handle);
+        return -EBUSY;
+    }
+    
+    uint32_t mask = GRALLOC_USAGE_SW_READ_MASK | 
+                    GRALLOC_USAGE_SW_WRITE_MASK;
+    if ((usage & mask) && vaddr){
+        if (hnd->flags & private_handle_t::PRIV_FLAGS_MAPPED) {
+            *vaddr = (void*)hnd->base;
+        } else {
+            hnd->flags |= private_handle_t::PRIV_FLAGS_MAPPED;
+            err = gralloc_map(module, handle, vaddr);
+        }
+    }
+    
+    hnd->flags |= private_handle_t::PRIV_FLAGS_LOCKED;
+    return err;
 }
 
 int gralloc_unlock(gralloc_module_t const* module, 
         buffer_handle_t handle)
 {
     // FIXME: gralloc_unlock() needs implementation
+    if (private_handle_t::validate(handle) < 0)
+        return -EINVAL;
+
+    private_handle_t* hnd = (private_handle_t*)(handle);
+
+    // not locked
+    if (!(hnd->flags & private_handle_t::PRIV_FLAGS_LOCKED)) {
+        LOGE("handle %p is not locked", handle);
+        return -EINVAL;
+    }
+
+    /* FOR DEBUGGING
+    if (hnd->flags & private_handle_t::PRIV_FLAGS_MAPPED) {
+        if (gralloc_unmap(module, handle) == 0) {
+            hnd->flags &= ~private_handle_t::PRIV_FLAGS_MAPPED;
+        }
+    }
+    */
+    
+    hnd->flags &= ~private_handle_t::PRIV_FLAGS_LOCKED;
     return 0;
 }