Layer.cpp revision 6950e428feaccc8164b989ef64e771a99948797a
1/* 2 * Copyright (C) 2007 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#include <stdlib.h> 18#include <stdint.h> 19#include <sys/types.h> 20 21#include <cutils/properties.h> 22#include <cutils/native_handle.h> 23 24#include <utils/Errors.h> 25#include <utils/Log.h> 26#include <utils/StopWatch.h> 27 28#include <ui/GraphicBuffer.h> 29#include <ui/PixelFormat.h> 30#include <ui/Surface.h> 31 32#include "clz.h" 33#include "Layer.h" 34#include "SurfaceFlinger.h" 35#include "DisplayHardware/DisplayHardware.h" 36 37 38#define DEBUG_RESIZE 0 39 40 41namespace android { 42 43// --------------------------------------------------------------------------- 44 45const uint32_t Layer::typeInfo = LayerBaseClient::typeInfo | 4; 46const char* const Layer::typeID = "Layer"; 47 48// --------------------------------------------------------------------------- 49 50Layer::Layer(SurfaceFlinger* flinger, DisplayID display, 51 const sp<Client>& c, int32_t i) 52 : LayerBaseClient(flinger, display, c, i), 53 mSecure(false), 54 mNeedsBlending(true), 55 mNeedsDithering(false) 56{ 57 // no OpenGL operation is possible here, since we might not be 58 // in the OpenGL thread. 59 mFrontBufferIndex = lcblk->getFrontBuffer(); 60} 61 62Layer::~Layer() 63{ 64 destroy(); 65 // the actual buffers will be destroyed here 66} 67 68void Layer::destroy() 69{ 70 for (size_t i=0 ; i<NUM_BUFFERS ; i++) { 71 if (mTextures[i].name != -1U) { 72 glDeleteTextures(1, &mTextures[i].name); 73 mTextures[i].name = -1U; 74 } 75 if (mTextures[i].image != EGL_NO_IMAGE_KHR) { 76 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); 77 eglDestroyImageKHR(dpy, mTextures[i].image); 78 mTextures[i].image = EGL_NO_IMAGE_KHR; 79 } 80 Mutex::Autolock _l(mLock); 81 mBuffers[i].clear(); 82 mWidth = mHeight = 0; 83 } 84 mSurface.clear(); 85} 86 87sp<LayerBaseClient::Surface> Layer::createSurface() const 88{ 89 return mSurface; 90} 91 92status_t Layer::ditch() 93{ 94 // the layer is not on screen anymore. free as much resources as possible 95 destroy(); 96 return NO_ERROR; 97} 98 99status_t Layer::setBuffers( uint32_t w, uint32_t h, 100 PixelFormat format, uint32_t flags) 101{ 102 // this surfaces pixel format 103 PixelFormatInfo info; 104 status_t err = getPixelFormatInfo(format, &info); 105 if (err) return err; 106 107 // the display's pixel format 108 const DisplayHardware& hw(graphicPlane(0).displayHardware()); 109 PixelFormatInfo displayInfo; 110 getPixelFormatInfo(hw.getFormat(), &displayInfo); 111 112 mFormat = format; 113 mWidth = w; 114 mHeight = h; 115 mSecure = (flags & ISurfaceComposer::eSecure) ? true : false; 116 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0; 117 118 // we use the red index 119 int displayRedSize = displayInfo.getSize(PixelFormatInfo::INDEX_RED); 120 int layerRedsize = info.getSize(PixelFormatInfo::INDEX_RED); 121 mNeedsDithering = layerRedsize > displayRedSize; 122 123 for (size_t i=0 ; i<NUM_BUFFERS ; i++) { 124 mBuffers[i] = new GraphicBuffer(); 125 } 126 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this); 127 return NO_ERROR; 128} 129 130void Layer::reloadTexture(const Region& dirty) 131{ 132 Mutex::Autolock _l(mLock); 133 sp<GraphicBuffer> buffer(getFrontBuffer()); 134 if (LIKELY((mFlags & DisplayHardware::DIRECT_TEXTURE) && 135 (buffer->usage & GRALLOC_USAGE_HW_TEXTURE))) { 136 int index = mFrontBufferIndex; 137 if (LIKELY(!mTextures[index].dirty)) { 138 glBindTexture(GL_TEXTURE_2D, mTextures[index].name); 139 } else { 140 // we need to recreate the texture 141 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay()); 142 143 // create the new texture name if needed 144 if (UNLIKELY(mTextures[index].name == -1U)) { 145 mTextures[index].name = createTexture(); 146 } else { 147 glBindTexture(GL_TEXTURE_2D, mTextures[index].name); 148 } 149 150 // free the previous image 151 if (mTextures[index].image != EGL_NO_IMAGE_KHR) { 152 eglDestroyImageKHR(dpy, mTextures[index].image); 153 mTextures[index].image = EGL_NO_IMAGE_KHR; 154 } 155 156 // construct an EGL_NATIVE_BUFFER_ANDROID 157 android_native_buffer_t* clientBuf = buffer->getNativeBuffer(); 158 159 // create the new EGLImageKHR 160 const EGLint attrs[] = { 161 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, 162 EGL_NONE, EGL_NONE 163 }; 164 mTextures[index].image = eglCreateImageKHR( 165 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, 166 (EGLClientBuffer)clientBuf, attrs); 167 168 LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR, 169 "eglCreateImageKHR() failed. err=0x%4x", 170 eglGetError()); 171 172 if (mTextures[index].image != EGL_NO_IMAGE_KHR) { 173 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, 174 (GLeglImageOES)mTextures[index].image); 175 GLint error = glGetError(); 176 if (UNLIKELY(error != GL_NO_ERROR)) { 177 // this failed, for instance, because we don't support 178 // NPOT. 179 // FIXME: do something! 180 LOGD("layer=%p, glEGLImageTargetTexture2DOES(%p) " 181 "failed err=0x%04x", 182 this, mTextures[index].image, error); 183 mFlags &= ~DisplayHardware::DIRECT_TEXTURE; 184 } else { 185 // Everything went okay! 186 mTextures[index].NPOTAdjust = false; 187 mTextures[index].dirty = false; 188 mTextures[index].width = clientBuf->width; 189 mTextures[index].height = clientBuf->height; 190 } 191 } 192 } 193 } else { 194 for (int i=0 ; i<NUM_BUFFERS ; i++) 195 mTextures[i].image = EGL_NO_IMAGE_KHR; 196 197 GGLSurface t; 198 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_OFTEN); 199 LOGE_IF(res, "error %d (%s) locking buffer %p", 200 res, strerror(res), buffer.get()); 201 202 if (res == NO_ERROR) { 203 if (UNLIKELY(mTextures[0].name == -1U)) { 204 mTextures[0].name = createTexture(); 205 mTextures[0].width = 0; 206 mTextures[0].height = 0; 207 } 208 loadTexture(&mTextures[0], dirty, t); 209 buffer->unlock(); 210 } 211 } 212} 213 214 215void Layer::onDraw(const Region& clip) const 216{ 217 int index = mFrontBufferIndex; 218 if (mTextures[index].image == EGL_NO_IMAGE_KHR) 219 index = 0; 220 GLuint textureName = mTextures[index].name; 221 if (UNLIKELY(textureName == -1LU)) { 222 // the texture has not been created yet, this Layer has 223 // in fact never been drawn into. this happens frequently with 224 // SurfaceView. 225 clearWithOpenGL(clip); 226 return; 227 } 228 drawWithOpenGL(clip, mTextures[index]); 229} 230 231sp<GraphicBuffer> Layer::requestBuffer(int index, int usage) 232{ 233 sp<GraphicBuffer> buffer; 234 235 // this ensures our client doesn't go away while we're accessing 236 // the shared area. 237 sp<Client> ourClient(client.promote()); 238 if (ourClient == 0) { 239 // oops, the client is already gone 240 return buffer; 241 } 242 243 /* 244 * This is called from the client's Surface::dequeue(). This can happen 245 * at any time, especially while we're in the middle of using the 246 * buffer 'index' as our front buffer. 247 * 248 * Make sure the buffer we're resizing is not the front buffer and has been 249 * dequeued. Once this condition is asserted, we are guaranteed that this 250 * buffer cannot become the front buffer under our feet, since we're called 251 * from Surface::dequeue() 252 */ 253 status_t err = lcblk->assertReallocate(index); 254 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err)); 255 if (err != NO_ERROR) { 256 // the surface may have died 257 return buffer; 258 } 259 260 uint32_t w, h; 261 { // scope for the lock 262 Mutex::Autolock _l(mLock); 263 w = mWidth; 264 h = mHeight; 265 buffer = mBuffers[index]; 266 267 // destroy() could have been called before we get here, we log it 268 // because it's uncommon, and the code below should handle it 269 LOGW_IF(buffer==0, 270 "mBuffers[%d] is null (mWidth=%d, mHeight=%d)", 271 index, w, h); 272 273 mBuffers[index].clear(); 274 } 275 276 const uint32_t effectiveUsage = getEffectiveUsage(usage); 277 if (buffer!=0 && buffer->getStrongCount() == 1) { 278 err = buffer->reallocate(w, h, mFormat, effectiveUsage); 279 } else { 280 // here we have to reallocate a new buffer because we could have a 281 // client in our process with a reference to it (eg: status bar), 282 // and we can't release the handle under its feet. 283 buffer.clear(); 284 buffer = new GraphicBuffer(w, h, mFormat, effectiveUsage); 285 err = buffer->initCheck(); 286 } 287 288 if (err || buffer->handle == 0) { 289 LOGE_IF(err || buffer->handle == 0, 290 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)", 291 this, index, w, h, strerror(-err)); 292 } else { 293 LOGD_IF(DEBUG_RESIZE, 294 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d, handle=%p", 295 this, index, w, h, buffer->handle); 296 } 297 298 if (err == NO_ERROR && buffer->handle != 0) { 299 Mutex::Autolock _l(mLock); 300 if (mWidth && mHeight) { 301 // and we have new buffer 302 mBuffers[index] = buffer; 303 // texture is now dirty... 304 mTextures[index].dirty = true; 305 } else { 306 // oops we got killed while we were allocating the buffer 307 buffer.clear(); 308 } 309 } 310 return buffer; 311} 312 313uint32_t Layer::getEffectiveUsage(uint32_t usage) const 314{ 315 /* 316 * buffers used for software rendering, but h/w composition 317 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE 318 * 319 * buffers used for h/w rendering and h/w composition 320 * are allocated with HW_RENDER | HW_TEXTURE 321 * 322 * buffers used with h/w rendering and either NPOT or no egl_image_ext 323 * are allocated with SW_READ_RARELY | HW_RENDER 324 * 325 */ 326 327 if (mSecure) { 328 // secure buffer, don't store it into the GPU 329 usage = GraphicBuffer::USAGE_SW_READ_OFTEN | 330 GraphicBuffer::USAGE_SW_WRITE_OFTEN; 331 } else { 332 // it's allowed to modify the usage flags here, but generally 333 // the requested flags should be honored. 334 usage |= GraphicBuffer::USAGE_HW_TEXTURE; 335 } 336 return usage; 337} 338 339uint32_t Layer::doTransaction(uint32_t flags) 340{ 341 const Layer::State& front(drawingState()); 342 const Layer::State& temp(currentState()); 343 344 if ((front.requested_w != temp.requested_w) || 345 (front.requested_h != temp.requested_h)) { 346 // the size changed, we need to ask our client to request a new buffer 347 LOGD_IF(DEBUG_RESIZE, 348 "resize (layer=%p), requested (%dx%d), " 349 "drawing (%d,%d), (%dx%d), (%dx%d)", 350 this, 351 int(temp.requested_w), int(temp.requested_h), 352 int(front.requested_w), int(front.requested_h), 353 int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()), 354 int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight())); 355 356 // we're being resized and there is a freeze display request, 357 // acquire a freeze lock, so that the screen stays put 358 // until we've redrawn at the new size; this is to avoid 359 // glitches upon orientation changes. 360 if (mFlinger->hasFreezeRequest()) { 361 // if the surface is hidden, don't try to acquire the 362 // freeze lock, since hidden surfaces may never redraw 363 if (!(front.flags & ISurfaceComposer::eLayerHidden)) { 364 mFreezeLock = mFlinger->getFreezeLock(); 365 } 366 } 367 368 // this will make sure LayerBase::doTransaction doesn't update 369 // the drawing state's size 370 Layer::State& editDraw(mDrawingState); 371 editDraw.requested_w = temp.requested_w; 372 editDraw.requested_h = temp.requested_h; 373 374 // record the new size, form this point on, when the client request a 375 // buffer, it'll get the new size. 376 setDrawingSize(temp.requested_w, temp.requested_h); 377 378 // all buffers need reallocation 379 lcblk->reallocate(); 380 } 381 382 if (temp.sequence != front.sequence) { 383 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) { 384 // this surface is now hidden, so it shouldn't hold a freeze lock 385 // (it may never redraw, which is fine if it is hidden) 386 mFreezeLock.clear(); 387 } 388 } 389 390 return LayerBase::doTransaction(flags); 391} 392 393void Layer::setDrawingSize(uint32_t w, uint32_t h) { 394 Mutex::Autolock _l(mLock); 395 mWidth = w; 396 mHeight = h; 397} 398 399// ---------------------------------------------------------------------------- 400// pageflip handling... 401// ---------------------------------------------------------------------------- 402 403void Layer::lockPageFlip(bool& recomputeVisibleRegions) 404{ 405 ssize_t buf = lcblk->retireAndLock(); 406 if (buf < NO_ERROR) { 407 //LOGW("nothing to retire (%s)", strerror(-buf)); 408 // NOTE: here the buffer is locked because we will used 409 // for composition later in the loop 410 return; 411 } 412 413 // we retired a buffer, which becomes the new front buffer 414 mFrontBufferIndex = buf; 415 416 // get the dirty region 417 sp<GraphicBuffer> newFrontBuffer(getBuffer(buf)); 418 const Region dirty(lcblk->getDirtyRegion(buf)); 419 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() ); 420 421 const Layer::State& front(drawingState()); 422 if (newFrontBuffer->getWidth() == front.requested_w && 423 newFrontBuffer->getHeight() == front.requested_h) 424 { 425 if ((front.w != front.requested_w) || 426 (front.h != front.requested_h)) 427 { 428 // Here we pretend the transaction happened by updating the 429 // current and drawing states. Drawing state is only accessed 430 // in this thread, no need to have it locked 431 Layer::State& editDraw(mDrawingState); 432 editDraw.w = editDraw.requested_w; 433 editDraw.h = editDraw.requested_h; 434 435 // We also need to update the current state so that we don't 436 // end-up doing too much work during the next transaction. 437 // NOTE: We actually don't need hold the transaction lock here 438 // because State::w and State::h are only accessed from 439 // this thread 440 Layer::State& editTemp(currentState()); 441 editTemp.w = editDraw.w; 442 editTemp.h = editDraw.h; 443 444 // recompute visible region 445 recomputeVisibleRegions = true; 446 447 // we now have the correct size, unfreeze the screen 448 mFreezeLock.clear(); 449 } 450 } 451 452 // FIXME: signal an event if we have more buffers waiting 453 // mFlinger->signalEvent(); 454 455 if (!mPostedDirtyRegion.isEmpty()) { 456 reloadTexture( mPostedDirtyRegion ); 457 } 458} 459 460void Layer::unlockPageFlip( 461 const Transform& planeTransform, Region& outDirtyRegion) 462{ 463 Region dirtyRegion(mPostedDirtyRegion); 464 if (!dirtyRegion.isEmpty()) { 465 mPostedDirtyRegion.clear(); 466 // The dirty region is given in the layer's coordinate space 467 // transform the dirty region by the surface's transformation 468 // and the global transformation. 469 const Layer::State& s(drawingState()); 470 const Transform tr(planeTransform * s.transform); 471 dirtyRegion = tr.transform(dirtyRegion); 472 473 // At this point, the dirty region is in screen space. 474 // Make sure it's constrained by the visible region (which 475 // is in screen space as well). 476 dirtyRegion.andSelf(visibleRegionScreen); 477 outDirtyRegion.orSelf(dirtyRegion); 478 } 479} 480 481void Layer::finishPageFlip() 482{ 483 status_t err = lcblk->unlock( mFrontBufferIndex ); 484 LOGE_IF(err!=NO_ERROR, 485 "layer %p, buffer=%d wasn't locked!", 486 this, mFrontBufferIndex); 487} 488 489// --------------------------------------------------------------------------- 490 491Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger, 492 SurfaceID id, const sp<Layer>& owner) 493 : Surface(flinger, id, owner->getIdentity(), owner) 494{ 495} 496 497Layer::SurfaceLayer::~SurfaceLayer() 498{ 499} 500 501sp<GraphicBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage) 502{ 503 sp<GraphicBuffer> buffer; 504 sp<Layer> owner(getOwner()); 505 if (owner != 0) { 506 LOGE_IF(uint32_t(index)>=NUM_BUFFERS, 507 "getBuffer() index (%d) out of range", index); 508 if (uint32_t(index) < NUM_BUFFERS) { 509 buffer = owner->requestBuffer(index, usage); 510 } 511 } 512 return buffer; 513} 514 515// --------------------------------------------------------------------------- 516 517 518}; // namespace android 519