DisplayPlane.cpp revision 6a6081a46a83da606cf21548879b37695adc7e1f
1/* 2 * Copyright © 2012 Intel Corporation 3 * All rights reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 * IN THE SOFTWARE. 23 * 24 * Authors: 25 * Jackie Li <yaodong.li@intel.com> 26 * 27 */ 28#include <Log.h> 29#include <Hwcomposer.h> 30#include <DisplayPlane.h> 31 32namespace android { 33namespace intel { 34 35static Log& log = Log::getInstance(); 36 37DisplayPlane::DisplayPlane(int index, int type, int disp) 38 : mIndex(index), 39 mType(type), 40 mDevice(disp), 41 mInitialized(false), 42 mGrallocBufferCache(0), 43 mTransform(PLANE_TRANSFORM_0) 44{ 45 log.v("DisplayPlane"); 46 47 mPosition.x = 0; 48 mPosition.y = 0; 49 mPosition.w = 0; 50 mPosition.h = 0; 51 52 mSrcCrop.x = 0; 53 mSrcCrop.y = 0; 54 mSrcCrop.w = 0; 55 mSrcCrop.h = 0; 56 57 // initialize 58 initialize(); 59} 60 61DisplayPlane::~DisplayPlane() 62{ 63 log.v("~DisplayPlane"); 64} 65 66bool DisplayPlane::initialize() 67{ 68 log.v("DisplayPlane::initialize"); 69 70 // create buffer cache 71 mGrallocBufferCache = new BufferCache(5); 72 if (!mGrallocBufferCache) { 73 LOGE("failed to create gralloc buffer cache\n"); 74 goto cache_err; 75 } 76 77 mInitialized = true; 78 return true; 79cache_err: 80 mInitialized = false; 81 return false; 82} 83 84void DisplayPlane::setPosition(int x, int y, int w, int h) 85{ 86 log.v("DisplayPlane::setPosition: %d, %d - %dx%d", x, y, w, h); 87 88 if (!initCheck()) { 89 log.e("DisplayPlane::setPosition: plane hasn't been initialized"); 90 return; 91 } 92 93 mPosition.x = x; 94 mPosition.y = y; 95 mPosition.w = w; 96 mPosition.h = h; 97} 98 99void DisplayPlane::setSourceCrop(int x, int y, int w, int h) 100{ 101 log.v("setSourceCrop: %d, %d - %dx%d", x, y, w, h); 102 103 if (!initCheck()) { 104 log.e("DisplayPlane::setSourceCrop: plane hasn't been initialized"); 105 return; 106 } 107 108 mSrcCrop.x = x; 109 mSrcCrop.y = y; 110 mSrcCrop.w = w; 111 mSrcCrop.h = h; 112} 113 114void DisplayPlane::setTransform(int trans) 115{ 116 log.v("DisplayPlane::setTransform: %d", trans); 117 118 if (!initCheck()) { 119 log.e("DisplayPlane::setTransform: plane hasn't been initialized"); 120 return; 121 } 122 123 switch (trans) { 124 case PLANE_TRANSFORM_90: 125 case PLANE_TRANSFORM_180: 126 case PLANE_TRANSFORM_270: 127 mTransform = trans; 128 break; 129 default: 130 mTransform = PLANE_TRANSFORM_0; 131 } 132} 133 134bool DisplayPlane::setDataBuffer(uint32_t handle) 135{ 136 DataBuffer *buffer; 137 BufferMapper *mapper; 138 BufferManager& bm(Hwcomposer::getInstance().getBufferManager()); 139 bool ret; 140 141 log.v("DisplayPlane::setDataBuffer 0x%x", handle); 142 143 if (!initCheck()) { 144 log.e("DisplayPlane::setDataBuffer: plane hasn't been initialized"); 145 return false; 146 } 147 148 if (!handle) { 149 log.e("DisplayPlane::setDataBuffer: invalid buffer handle"); 150 return false; 151 } 152 153 buffer = bm.get(handle); 154 if (!buffer) { 155 log.e("DisplayPlane::setDataBuffer: failed to get buffer"); 156 return false; 157 } 158 159 // update buffer's source crop 160 buffer->setCrop(mSrcCrop.x, mSrcCrop.y, mSrcCrop.w, mSrcCrop.h); 161 162 // map buffer if it's not in cache 163 mapper = mGrallocBufferCache->getMapper(buffer->getKey()); 164 if (!mapper) { 165 log.v("DisplayPlane::setDataBuffer: new buffer, will add it"); 166 mapper = bm.map(*buffer); 167 if (!mapper) { 168 log.e("DisplayPlane::setDataBuffer: failed to map buffer"); 169 goto mapper_err; 170 } 171 172 // add mapper 173 mGrallocBufferCache->addMapper(buffer->getKey(), mapper); 174 } 175 176 // put buffer after getting mapper 177 bm.put(*buffer); 178 179 return setDataBuffer(*mapper); 180mapper_err: 181 bm.put(*buffer); 182 return false; 183} 184 185void DisplayPlane::invalidateBufferCache() 186{ 187 BufferManager& bm(Hwcomposer::getInstance().getBufferManager()); 188 BufferMapper* mapper; 189 190 log.v("DisplayPlane::invalidateBufferCache"); 191 192 if (!initCheck()) { 193 log.e("DisplayPlane:invalidateBufferCache: plane hasn't been initialized"); 194 return; 195 } 196 197 for (size_t i = 0; i < mGrallocBufferCache->getCacheSize(); i++) { 198 mapper = mGrallocBufferCache->getMapper(i); 199 if (mapper) { 200 // unmap it 201 bm.unmap(*mapper); 202 // remove it from buffer cache 203 mGrallocBufferCache->removeMapper(mapper); 204 } 205 } 206} 207 208bool DisplayPlane::assignToDevice(int disp) 209{ 210 log.v("DisplayPlane::assignToDevice: disp = %d", disp); 211 212 if (!initCheck()) { 213 log.e("DisplayPlane::assignToDevice: plane hasn't been initialized"); 214 return false; 215 } 216 217 mDevice = disp; 218 return true; 219} 220 221} // namespace intel 222} // namespace android 223