michael@0: /* michael@0: * Copyright (C) 2011 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #define LOG_TAG "Sprites" michael@0: michael@0: //#define LOG_NDEBUG 0 michael@0: michael@0: #include "SpriteController.h" michael@0: michael@0: #include "cutils_log.h" michael@0: #include michael@0: #ifdef HAVE_ANDROID_OS michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: namespace android { michael@0: michael@0: // --- SpriteController --- michael@0: michael@0: SpriteController::SpriteController(const sp& looper, int32_t overlayLayer) : michael@0: mLooper(looper), mOverlayLayer(overlayLayer) { michael@0: #ifdef HAVE_ANDROID_OS michael@0: mHandler = new WeakMessageHandler(this); michael@0: #endif michael@0: michael@0: mLocked.transactionNestingCount = 0; michael@0: mLocked.deferredSpriteUpdate = false; michael@0: } michael@0: michael@0: SpriteController::~SpriteController() { michael@0: #ifdef HAVE_ANDROID_OS michael@0: mLooper->removeMessages(mHandler); michael@0: michael@0: if (mSurfaceComposerClient != NULL) { michael@0: mSurfaceComposerClient->dispose(); michael@0: mSurfaceComposerClient.clear(); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: sp SpriteController::createSprite() { michael@0: return new SpriteImpl(this); michael@0: } michael@0: michael@0: void SpriteController::openTransaction() { michael@0: AutoMutex _l(mLock); michael@0: michael@0: mLocked.transactionNestingCount += 1; michael@0: } michael@0: michael@0: void SpriteController::closeTransaction() { michael@0: AutoMutex _l(mLock); michael@0: michael@0: LOG_ALWAYS_FATAL_IF(mLocked.transactionNestingCount == 0, michael@0: "Sprite closeTransaction() called but there is no open sprite transaction"); michael@0: michael@0: mLocked.transactionNestingCount -= 1; michael@0: if (mLocked.transactionNestingCount == 0 && mLocked.deferredSpriteUpdate) { michael@0: mLocked.deferredSpriteUpdate = false; michael@0: #ifdef HAVE_ANDROID_OS michael@0: mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES)); michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: void SpriteController::invalidateSpriteLocked(const sp& sprite) { michael@0: bool wasEmpty = mLocked.invalidatedSprites.isEmpty(); michael@0: mLocked.invalidatedSprites.push(sprite); michael@0: if (wasEmpty) { michael@0: if (mLocked.transactionNestingCount != 0) { michael@0: mLocked.deferredSpriteUpdate = true; michael@0: } else { michael@0: #ifdef HAVE_ANDROID_OS michael@0: mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES)); michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: void SpriteController::disposeSurfaceLocked(const sp& surfaceControl) { michael@0: bool wasEmpty = mLocked.disposedSurfaces.isEmpty(); michael@0: mLocked.disposedSurfaces.push(surfaceControl); michael@0: if (wasEmpty) { michael@0: mLooper->sendMessage(mHandler, Message(MSG_DISPOSE_SURFACES)); michael@0: } michael@0: } michael@0: michael@0: void SpriteController::handleMessage(const Message& message) { michael@0: switch (message.what) { michael@0: case MSG_UPDATE_SPRITES: michael@0: doUpdateSprites(); michael@0: break; michael@0: case MSG_DISPOSE_SURFACES: michael@0: doDisposeSurfaces(); michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: void SpriteController::doUpdateSprites() { michael@0: // Collect information about sprite updates. michael@0: // Each sprite update record includes a reference to its associated sprite so we can michael@0: // be certain the sprites will not be deleted while this function runs. Sprites michael@0: // may invalidate themselves again during this time but we will handle those changes michael@0: // in the next iteration. michael@0: Vector updates; michael@0: size_t numSprites; michael@0: { // acquire lock michael@0: AutoMutex _l(mLock); michael@0: michael@0: numSprites = mLocked.invalidatedSprites.size(); michael@0: for (size_t i = 0; i < numSprites; i++) { michael@0: const sp& sprite = mLocked.invalidatedSprites.itemAt(i); michael@0: michael@0: updates.push(SpriteUpdate(sprite, sprite->getStateLocked())); michael@0: sprite->resetDirtyLocked(); michael@0: } michael@0: mLocked.invalidatedSprites.clear(); michael@0: } // release lock michael@0: michael@0: // Create missing surfaces. michael@0: bool surfaceChanged = false; michael@0: for (size_t i = 0; i < numSprites; i++) { michael@0: SpriteUpdate& update = updates.editItemAt(i); michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: if (update.state.surfaceControl == NULL && update.state.wantSurfaceVisible()) { michael@0: update.state.surfaceWidth = update.state.icon.bitmap.width(); michael@0: update.state.surfaceHeight = update.state.icon.bitmap.height(); michael@0: update.state.surfaceDrawn = false; michael@0: update.state.surfaceVisible = false; michael@0: update.state.surfaceControl = obtainSurface( michael@0: update.state.surfaceWidth, update.state.surfaceHeight); michael@0: if (update.state.surfaceControl != NULL) { michael@0: update.surfaceChanged = surfaceChanged = true; michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: // Resize sprites if needed, inside a global transaction. michael@0: bool haveGlobalTransaction = false; michael@0: for (size_t i = 0; i < numSprites; i++) { michael@0: SpriteUpdate& update = updates.editItemAt(i); michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: if (update.state.surfaceControl != NULL && update.state.wantSurfaceVisible()) { michael@0: int32_t desiredWidth = update.state.icon.bitmap.width(); michael@0: int32_t desiredHeight = update.state.icon.bitmap.height(); michael@0: if (update.state.surfaceWidth < desiredWidth michael@0: || update.state.surfaceHeight < desiredHeight) { michael@0: if (!haveGlobalTransaction) { michael@0: SurfaceComposerClient::openGlobalTransaction(); michael@0: haveGlobalTransaction = true; michael@0: } michael@0: michael@0: status_t status = update.state.surfaceControl->setSize(desiredWidth, desiredHeight); michael@0: if (status) { michael@0: ALOGE("Error %d resizing sprite surface from %dx%d to %dx%d", michael@0: status, update.state.surfaceWidth, update.state.surfaceHeight, michael@0: desiredWidth, desiredHeight); michael@0: } else { michael@0: update.state.surfaceWidth = desiredWidth; michael@0: update.state.surfaceHeight = desiredHeight; michael@0: update.state.surfaceDrawn = false; michael@0: update.surfaceChanged = surfaceChanged = true; michael@0: michael@0: if (update.state.surfaceVisible) { michael@0: status = update.state.surfaceControl->hide(); michael@0: if (status) { michael@0: ALOGE("Error %d hiding sprite surface after resize.", status); michael@0: } else { michael@0: update.state.surfaceVisible = false; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: #ifdef HAVE_ANDROID_OS michael@0: if (haveGlobalTransaction) { michael@0: SurfaceComposerClient::closeGlobalTransaction(); michael@0: } michael@0: #endif michael@0: michael@0: // Redraw sprites if needed. michael@0: for (size_t i = 0; i < numSprites; i++) { michael@0: SpriteUpdate& update = updates.editItemAt(i); michael@0: michael@0: if ((update.state.dirty & DIRTY_BITMAP) && update.state.surfaceDrawn) { michael@0: update.state.surfaceDrawn = false; michael@0: update.surfaceChanged = surfaceChanged = true; michael@0: } michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: if (update.state.surfaceControl != NULL && !update.state.surfaceDrawn michael@0: && update.state.wantSurfaceVisible()) { michael@0: sp surface = update.state.surfaceControl->getSurface(); michael@0: ANativeWindow_Buffer outBuffer; michael@0: status_t status = surface->lock(&outBuffer, NULL); michael@0: if (status) { michael@0: ALOGE("Error %d locking sprite surface before drawing.", status); michael@0: } else { michael@0: SkBitmap surfaceBitmap; michael@0: ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format); michael@0: surfaceBitmap.setConfig(SkBitmap::kARGB_8888_Config, michael@0: outBuffer.width, outBuffer.height, bpr); michael@0: surfaceBitmap.setPixels(outBuffer.bits); michael@0: michael@0: SkCanvas surfaceCanvas(surfaceBitmap); michael@0: michael@0: SkPaint paint; michael@0: paint.setXfermodeMode(SkXfermode::kSrc_Mode); michael@0: surfaceCanvas.drawBitmap(update.state.icon.bitmap, 0, 0, &paint); michael@0: michael@0: if (outBuffer.width > uint32_t(update.state.icon.bitmap.width())) { michael@0: paint.setColor(0); // transparent fill color michael@0: surfaceCanvas.drawRectCoords(update.state.icon.bitmap.width(), 0, michael@0: outBuffer.width, update.state.icon.bitmap.height(), paint); michael@0: } michael@0: if (outBuffer.height > uint32_t(update.state.icon.bitmap.height())) { michael@0: paint.setColor(0); // transparent fill color michael@0: surfaceCanvas.drawRectCoords(0, update.state.icon.bitmap.height(), michael@0: outBuffer.width, outBuffer.height, paint); michael@0: } michael@0: michael@0: status = surface->unlockAndPost(); michael@0: if (status) { michael@0: ALOGE("Error %d unlocking and posting sprite surface after drawing.", status); michael@0: } else { michael@0: update.state.surfaceDrawn = true; michael@0: update.surfaceChanged = surfaceChanged = true; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: // Set sprite surface properties and make them visible. michael@0: bool haveTransaction = false; michael@0: for (size_t i = 0; i < numSprites; i++) { michael@0: SpriteUpdate& update = updates.editItemAt(i); michael@0: michael@0: bool wantSurfaceVisibleAndDrawn = update.state.wantSurfaceVisible() michael@0: && update.state.surfaceDrawn; michael@0: bool becomingVisible = wantSurfaceVisibleAndDrawn && !update.state.surfaceVisible; michael@0: bool becomingHidden = !wantSurfaceVisibleAndDrawn && update.state.surfaceVisible; michael@0: #ifdef HAVE_ANDROID_OS michael@0: if (update.state.surfaceControl != NULL && (becomingVisible || becomingHidden michael@0: || (wantSurfaceVisibleAndDrawn && (update.state.dirty & (DIRTY_ALPHA michael@0: | DIRTY_POSITION | DIRTY_TRANSFORMATION_MATRIX | DIRTY_LAYER michael@0: | DIRTY_VISIBILITY | DIRTY_HOTSPOT))))) { michael@0: status_t status; michael@0: if (!haveTransaction) { michael@0: SurfaceComposerClient::openGlobalTransaction(); michael@0: haveTransaction = true; michael@0: } michael@0: michael@0: if (wantSurfaceVisibleAndDrawn michael@0: && (becomingVisible || (update.state.dirty & DIRTY_ALPHA))) { michael@0: status = update.state.surfaceControl->setAlpha(update.state.alpha); michael@0: if (status) { michael@0: ALOGE("Error %d setting sprite surface alpha.", status); michael@0: } michael@0: } michael@0: michael@0: if (wantSurfaceVisibleAndDrawn michael@0: && (becomingVisible || (update.state.dirty & (DIRTY_POSITION michael@0: | DIRTY_HOTSPOT)))) { michael@0: status = update.state.surfaceControl->setPosition( michael@0: update.state.positionX - update.state.icon.hotSpotX, michael@0: update.state.positionY - update.state.icon.hotSpotY); michael@0: if (status) { michael@0: ALOGE("Error %d setting sprite surface position.", status); michael@0: } michael@0: } michael@0: michael@0: if (wantSurfaceVisibleAndDrawn michael@0: && (becomingVisible michael@0: || (update.state.dirty & DIRTY_TRANSFORMATION_MATRIX))) { michael@0: status = update.state.surfaceControl->setMatrix( michael@0: update.state.transformationMatrix.dsdx, michael@0: update.state.transformationMatrix.dtdx, michael@0: update.state.transformationMatrix.dsdy, michael@0: update.state.transformationMatrix.dtdy); michael@0: if (status) { michael@0: ALOGE("Error %d setting sprite surface transformation matrix.", status); michael@0: } michael@0: } michael@0: michael@0: int32_t surfaceLayer = mOverlayLayer + update.state.layer; michael@0: if (wantSurfaceVisibleAndDrawn michael@0: && (becomingVisible || (update.state.dirty & DIRTY_LAYER))) { michael@0: status = update.state.surfaceControl->setLayer(surfaceLayer); michael@0: if (status) { michael@0: ALOGE("Error %d setting sprite surface layer.", status); michael@0: } michael@0: } michael@0: michael@0: if (becomingVisible) { michael@0: status = update.state.surfaceControl->show(); michael@0: if (status) { michael@0: ALOGE("Error %d showing sprite surface.", status); michael@0: } else { michael@0: update.state.surfaceVisible = true; michael@0: update.surfaceChanged = surfaceChanged = true; michael@0: } michael@0: } else if (becomingHidden) { michael@0: status = update.state.surfaceControl->hide(); michael@0: if (status) { michael@0: ALOGE("Error %d hiding sprite surface.", status); michael@0: } else { michael@0: update.state.surfaceVisible = false; michael@0: update.surfaceChanged = surfaceChanged = true; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: if (haveTransaction) { michael@0: SurfaceComposerClient::closeGlobalTransaction(); michael@0: } michael@0: #endif michael@0: michael@0: // If any surfaces were changed, write back the new surface properties to the sprites. michael@0: if (surfaceChanged) { // acquire lock michael@0: AutoMutex _l(mLock); michael@0: michael@0: for (size_t i = 0; i < numSprites; i++) { michael@0: const SpriteUpdate& update = updates.itemAt(i); michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: if (update.surfaceChanged) { michael@0: update.sprite->setSurfaceLocked(update.state.surfaceControl, michael@0: update.state.surfaceWidth, update.state.surfaceHeight, michael@0: update.state.surfaceDrawn, update.state.surfaceVisible); michael@0: } michael@0: #endif michael@0: } michael@0: } // release lock michael@0: michael@0: // Clear the sprite update vector outside the lock. It is very important that michael@0: // we do not clear sprite references inside the lock since we could be releasing michael@0: // the last remaining reference to the sprite here which would result in the michael@0: // sprite being deleted and the lock being reacquired by the sprite destructor michael@0: // while already held. michael@0: updates.clear(); michael@0: } michael@0: michael@0: void SpriteController::doDisposeSurfaces() { michael@0: #ifdef HAVE_ANDROID_OS michael@0: // Collect disposed surfaces. michael@0: Vector > disposedSurfaces; michael@0: { // acquire lock michael@0: AutoMutex _l(mLock); michael@0: michael@0: disposedSurfaces = mLocked.disposedSurfaces; michael@0: mLocked.disposedSurfaces.clear(); michael@0: } // release lock michael@0: michael@0: // Release the last reference to each surface outside of the lock. michael@0: // We don't want the surfaces to be deleted while we are holding our lock. michael@0: disposedSurfaces.clear(); michael@0: #endif michael@0: } michael@0: michael@0: void SpriteController::ensureSurfaceComposerClient() { michael@0: #ifdef HAVE_ANDROID_OS michael@0: if (mSurfaceComposerClient == NULL) { michael@0: mSurfaceComposerClient = new SurfaceComposerClient(); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: sp SpriteController::obtainSurface(int32_t width, int32_t height) { michael@0: ensureSurfaceComposerClient(); michael@0: michael@0: sp surfaceControl = mSurfaceComposerClient->createSurface( michael@0: String8("Sprite"), width, height, PIXEL_FORMAT_RGBA_8888, michael@0: ISurfaceComposerClient::eHidden); michael@0: if (surfaceControl == NULL || !surfaceControl->isValid()) { michael@0: ALOGE("Error creating sprite surface."); michael@0: return NULL; michael@0: } michael@0: return surfaceControl; michael@0: } michael@0: #endif michael@0: michael@0: michael@0: // --- SpriteController::SpriteImpl --- michael@0: michael@0: SpriteController::SpriteImpl::SpriteImpl(const sp controller) : michael@0: mController(controller) { michael@0: } michael@0: michael@0: SpriteController::SpriteImpl::~SpriteImpl() { michael@0: AutoMutex _m(mController->mLock); michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: // Let the controller take care of deleting the last reference to sprite michael@0: // surfaces so that we do not block the caller on an IPC here. michael@0: if (mLocked.state.surfaceControl != NULL) { michael@0: mController->disposeSurfaceLocked(mLocked.state.surfaceControl); michael@0: mLocked.state.surfaceControl.clear(); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: void SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon) { michael@0: AutoMutex _l(mController->mLock); michael@0: michael@0: #ifdef HAVE_ANDROID_OS michael@0: uint32_t dirty; michael@0: if (icon.isValid()) { michael@0: icon.bitmap.copyTo(&mLocked.state.icon.bitmap, SkBitmap::kARGB_8888_Config); michael@0: michael@0: if (!mLocked.state.icon.isValid() michael@0: || mLocked.state.icon.hotSpotX != icon.hotSpotX michael@0: || mLocked.state.icon.hotSpotY != icon.hotSpotY) { michael@0: mLocked.state.icon.hotSpotX = icon.hotSpotX; michael@0: mLocked.state.icon.hotSpotY = icon.hotSpotY; michael@0: dirty = DIRTY_BITMAP | DIRTY_HOTSPOT; michael@0: } else { michael@0: dirty = DIRTY_BITMAP; michael@0: } michael@0: } else if (mLocked.state.icon.isValid()) { michael@0: mLocked.state.icon.bitmap.reset(); michael@0: dirty = DIRTY_BITMAP | DIRTY_HOTSPOT; michael@0: } else { michael@0: return; // setting to invalid icon and already invalid so nothing to do michael@0: } michael@0: michael@0: invalidateLocked(dirty); michael@0: #endif michael@0: } michael@0: michael@0: void SpriteController::SpriteImpl::setVisible(bool visible) { michael@0: AutoMutex _l(mController->mLock); michael@0: michael@0: if (mLocked.state.visible != visible) { michael@0: mLocked.state.visible = visible; michael@0: invalidateLocked(DIRTY_VISIBILITY); michael@0: } michael@0: } michael@0: michael@0: void SpriteController::SpriteImpl::setPosition(float x, float y) { michael@0: AutoMutex _l(mController->mLock); michael@0: michael@0: if (mLocked.state.positionX != x || mLocked.state.positionY != y) { michael@0: mLocked.state.positionX = x; michael@0: mLocked.state.positionY = y; michael@0: invalidateLocked(DIRTY_POSITION); michael@0: } michael@0: } michael@0: michael@0: void SpriteController::SpriteImpl::setLayer(int32_t layer) { michael@0: AutoMutex _l(mController->mLock); michael@0: michael@0: if (mLocked.state.layer != layer) { michael@0: mLocked.state.layer = layer; michael@0: invalidateLocked(DIRTY_LAYER); michael@0: } michael@0: } michael@0: michael@0: void SpriteController::SpriteImpl::setAlpha(float alpha) { michael@0: AutoMutex _l(mController->mLock); michael@0: michael@0: if (mLocked.state.alpha != alpha) { michael@0: mLocked.state.alpha = alpha; michael@0: invalidateLocked(DIRTY_ALPHA); michael@0: } michael@0: } michael@0: michael@0: void SpriteController::SpriteImpl::setTransformationMatrix( michael@0: const SpriteTransformationMatrix& matrix) { michael@0: AutoMutex _l(mController->mLock); michael@0: michael@0: if (mLocked.state.transformationMatrix != matrix) { michael@0: mLocked.state.transformationMatrix = matrix; michael@0: invalidateLocked(DIRTY_TRANSFORMATION_MATRIX); michael@0: } michael@0: } michael@0: michael@0: void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) { michael@0: bool wasDirty = mLocked.state.dirty; michael@0: mLocked.state.dirty |= dirty; michael@0: michael@0: if (!wasDirty) { michael@0: mController->invalidateSpriteLocked(this); michael@0: } michael@0: } michael@0: michael@0: } // namespace android