Home | History | Annotate | Download | only in legacy
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifdef LOG_TAG
     18 #undef LOG_TAG
     19 #endif
     20 #define LOG_TAG "VSoCGrallocRegionRegistry"
     21 
     22 #include <limits.h>
     23 #include <errno.h>
     24 #include <pthread.h>
     25 #include <unistd.h>
     26 #include <string.h>
     27 
     28 #include <sys/mman.h>
     29 #include <sys/stat.h>
     30 #include <sys/types.h>
     31 
     32 #include <cutils/hashmap.h>
     33 #include <cutils/log.h>
     34 #include <cutils/atomic.h>
     35 
     36 #include <linux/ashmem.h>
     37 
     38 #include <hardware/hardware.h>
     39 #include <hardware/gralloc.h>
     40 #include <system/graphics.h>
     41 
     42 #include "gralloc_vsoc_priv.h"
     43 
     44 // TODO(ghartman): Make the configurable through a property
     45 static const bool g_log_refs = false;
     46 
     47 struct GrallocRegion {
     48   void* base_;
     49   int   num_references_;
     50 
     51   GrallocRegion() : base_(0), num_references_(0) { }
     52   // Copy constructors are ok.
     53 };
     54 
     55 
     56 static const char* get_buffer_name(
     57     const private_handle_t* hnd, char output[ASHMEM_NAME_LEN]) {
     58   output[0] = '\0';
     59   if (!hnd) {
     60     ALOGE("Attempted to log gralloc name hnd=NULL");
     61     return output;
     62   }
     63   if (hnd->fd == -1) {
     64     ALOGE("Attempted to log gralloc name hnd=%p with fd == -1", hnd);
     65     return output;
     66   }
     67   int rval = ioctl(hnd->fd, ASHMEM_GET_NAME, output);
     68   if (rval == -1) {
     69     output[0] = '\0';
     70   }
     71   return output;
     72 }
     73 
     74 
     75 static int str_hash(void* str) {
     76   return hashmapHash(str, strlen(reinterpret_cast<const char*>(str)));
     77 }
     78 
     79 
     80 static bool str_equal(void* a, void* b) {
     81   return strcmp(
     82       reinterpret_cast<const char*>(a),
     83       reinterpret_cast<const char*>(b)) == 0;
     84 }
     85 
     86 
     87 static Hashmap* get_regions() {
     88   static Hashmap* regionMap = hashmapCreate(19, str_hash, str_equal);
     89   return regionMap;
     90 }
     91 
     92 
     93 static GrallocRegion* lock_region_for_handle(
     94     const private_handle_t* hnd, char region_name[ASHMEM_NAME_LEN]) {
     95   region_name[0] = '\0';
     96   get_buffer_name(hnd, region_name);
     97   Hashmap* hash = get_regions();
     98   hashmapLock(hash);
     99   GrallocRegion* region = reinterpret_cast<GrallocRegion*>(
    100       hashmapGet(hash, region_name));
    101   if (!region) {
    102     region = new GrallocRegion;
    103     hashmapPut(hash, strdup(region_name), region);
    104   }
    105   return region;
    106 }
    107 
    108 
    109 /* The current implementation uses only a single lock for all regions.
    110  * This method takes a region to simplfy the refactoring if we go to
    111  * finer-grained locks.
    112  */
    113 static inline void unlock_region(GrallocRegion* ) {
    114   hashmapUnlock(get_regions());
    115 }
    116 
    117 
    118 void* reference_region(const char* op, const private_handle_t* hnd) {
    119   char name_buf[ASHMEM_NAME_LEN];
    120   GrallocRegion* region = lock_region_for_handle(hnd, name_buf);
    121   if (!region->base_) {
    122     void* mappedAddress = mmap(
    123         0, hnd->total_size, PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0);
    124     if (mappedAddress == MAP_FAILED) {
    125       ALOGE("Could not mmap %s", strerror(errno));
    126       unlock_region(region);
    127       return NULL;
    128     }
    129     // Set up the guard pages. The last page is always a guard
    130     uintptr_t base = uintptr_t(mappedAddress);
    131     uintptr_t addr = base + hnd->total_size - PAGE_SIZE;
    132     if (mprotect((void*)addr, PAGE_SIZE, PROT_NONE) == -1) {
    133       ALOGE("mprotect base=%p, pg=%p failed (%s)", (void*)base, (void*)addr,
    134             strerror(errno));
    135     }
    136     region->base_ = mappedAddress;
    137     ALOGI("Mapped %s hnd=%p fd=%d base=%p format=%s(0x%x) width=%d height=%d",
    138           name_buf, hnd, hnd->fd, region->base_,
    139           pixel_format_to_string(hnd->format), hnd->format,
    140           hnd->x_res, hnd->y_res);
    141   }
    142 
    143   void* rval = region->base_;
    144   ++region->num_references_;
    145   ALOGI_IF(g_log_refs, "Referencing name=%s op=%s addr=%p new numRefs=%d",
    146         name_buf, op, region->base_, region->num_references_);
    147   unlock_region(region);
    148   return rval;
    149 }
    150 
    151 
    152 int unreference_region(const char* op, const private_handle_t* hnd) {
    153   char name_buf[ASHMEM_NAME_LEN];
    154 
    155   GrallocRegion* region = lock_region_for_handle(hnd, name_buf);
    156   if (!region->base_) {
    157     ALOGE("Unmapping region with no map hnd=%p", hnd);
    158     unlock_region(region);
    159     return -1;
    160   }
    161   if (region->num_references_ < 1) {
    162     ALOGE(
    163         "unmap with hnd=%p, numReferences=%d", hnd, region->num_references_);
    164     unlock_region(region);
    165     return -1;
    166   }
    167   --region->num_references_;
    168   if (!region->num_references_) {
    169     ALOGI("Unmapped %s hnd=%p fd=%d base=%p", name_buf, hnd,
    170           hnd->fd, region->base_);
    171     if (munmap(region->base_, hnd->total_size) < 0) {
    172       ALOGE("Could not unmap %s", strerror(errno));
    173     }
    174     region->base_ = 0;
    175   }
    176   ALOGI_IF(g_log_refs, "Unreferencing name=%s op=%s addr=%p new numRefs=%d",
    177         name_buf, op, region->base_, region->num_references_);
    178   unlock_region(region);
    179   return 0;
    180 }
    181