1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef GRALLOC_ANDROID 16 #define GRALLOC_ANDROID 17 18 #include <hardware/gralloc.h> 19 #include <cutils/log.h> 20 21 #ifdef HAVE_GRALLOC1 22 #include <hardware/gralloc1.h> 23 #include <sync/sync.h> 24 #endif 25 26 #include <unistd.h> // for close() 27 28 class GrallocModule 29 { 30 public: 31 static GrallocModule *getInstance(); 32 int lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr) 33 { 34 switch(m_major_version) 35 { 36 case 0: 37 { 38 return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr); 39 } 40 case 1: 41 #ifdef HAVE_GRALLOC1 42 { 43 gralloc1_rect_t outRect{}; 44 outRect.left = left; 45 outRect.top = top; 46 outRect.width = width; 47 outRect.height = height; 48 return m_gralloc1_lock(m_gralloc1_device, handle, usage, usage, &outRect, vaddr, -1); 49 } 50 #endif 51 default: 52 { 53 ALOGE("no gralloc module to lock"); 54 return -1; 55 } 56 } 57 } 58 59 int unlock(buffer_handle_t handle) 60 { 61 switch(m_major_version) 62 { 63 case 0: 64 { 65 return m_module->unlock(m_module, handle); 66 } 67 case 1: 68 #ifdef HAVE_GRALLOC1 69 { 70 int32_t fenceFd = -1; 71 int error = m_gralloc1_unlock(m_gralloc1_device, handle, &fenceFd); 72 if (!error) 73 { 74 sync_wait(fenceFd, -1); 75 close(fenceFd); 76 } 77 return error; 78 } 79 #endif 80 default: 81 { 82 ALOGE("no gralloc module to unlock"); 83 return -1; 84 } 85 } 86 } 87 88 private: 89 GrallocModule(); 90 uint8_t m_major_version; 91 const gralloc_module_t *m_module; 92 #ifdef HAVE_GRALLOC1 93 gralloc1_device_t *m_gralloc1_device = nullptr; 94 GRALLOC1_PFN_LOCK m_gralloc1_lock = nullptr; 95 GRALLOC1_PFN_UNLOCK m_gralloc1_unlock = nullptr; 96 #endif 97 }; 98 99 #endif // GRALLOC_ANDROID 100