2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "Sprites"
19 //#define LOG_NDEBUG 0
21 #include "SpriteController.h"
23 #include <cutils/log.h>
24 #include <utils/String8.h>
25 #include <gui/Surface.h>
31 #include <SkXfermode.h>
32 #include <android/native_window.h>
36 // --- SpriteController ---
38 SpriteController::SpriteController(const sp<Looper>& looper, int32_t overlayLayer) :
39 mLooper(looper), mOverlayLayer(overlayLayer) {
40 mHandler = new WeakMessageHandler(this);
42 mLocked.transactionNestingCount = 0;
43 mLocked.deferredSpriteUpdate = false;
46 SpriteController::~SpriteController() {
47 mLooper->removeMessages(mHandler);
49 if (mSurfaceComposerClient != NULL) {
50 mSurfaceComposerClient->dispose();
51 mSurfaceComposerClient.clear();
55 sp<Sprite> SpriteController::createSprite() {
56 return new SpriteImpl(this);
59 void SpriteController::openTransaction() {
62 mLocked.transactionNestingCount += 1;
65 void SpriteController::closeTransaction() {
68 LOG_ALWAYS_FATAL_IF(mLocked.transactionNestingCount == 0,
69 "Sprite closeTransaction() called but there is no open sprite transaction");
71 mLocked.transactionNestingCount -= 1;
72 if (mLocked.transactionNestingCount == 0 && mLocked.deferredSpriteUpdate) {
73 mLocked.deferredSpriteUpdate = false;
74 mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES));
78 void SpriteController::invalidateSpriteLocked(const sp<SpriteImpl>& sprite) {
79 bool wasEmpty = mLocked.invalidatedSprites.isEmpty();
80 mLocked.invalidatedSprites.push(sprite);
82 if (mLocked.transactionNestingCount != 0) {
83 mLocked.deferredSpriteUpdate = true;
85 mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES));
90 void SpriteController::disposeSurfaceLocked(const sp<SurfaceControl>& surfaceControl) {
91 bool wasEmpty = mLocked.disposedSurfaces.isEmpty();
92 mLocked.disposedSurfaces.push(surfaceControl);
94 mLooper->sendMessage(mHandler, Message(MSG_DISPOSE_SURFACES));
98 void SpriteController::handleMessage(const Message& message) {
99 switch (message.what) {
100 case MSG_UPDATE_SPRITES:
103 case MSG_DISPOSE_SURFACES:
109 void SpriteController::doUpdateSprites() {
110 // Collect information about sprite updates.
111 // Each sprite update record includes a reference to its associated sprite so we can
112 // be certain the sprites will not be deleted while this function runs. Sprites
113 // may invalidate themselves again during this time but we will handle those changes
114 // in the next iteration.
115 Vector<SpriteUpdate> updates;
120 numSprites = mLocked.invalidatedSprites.size();
121 for (size_t i = 0; i < numSprites; i++) {
122 const sp<SpriteImpl>& sprite = mLocked.invalidatedSprites.itemAt(i);
124 updates.push(SpriteUpdate(sprite, sprite->getStateLocked()));
125 sprite->resetDirtyLocked();
127 mLocked.invalidatedSprites.clear();
130 // Create missing surfaces.
131 bool surfaceChanged = false;
132 for (size_t i = 0; i < numSprites; i++) {
133 SpriteUpdate& update = updates.editItemAt(i);
135 if (update.state.surfaceControl == NULL && update.state.wantSurfaceVisible()) {
136 update.state.surfaceWidth = update.state.icon.bitmap.width();
137 update.state.surfaceHeight = update.state.icon.bitmap.height();
138 update.state.surfaceDrawn = false;
139 update.state.surfaceVisible = false;
140 update.state.surfaceControl = obtainSurface(
141 update.state.surfaceWidth, update.state.surfaceHeight);
142 if (update.state.surfaceControl != NULL) {
143 update.surfaceChanged = surfaceChanged = true;
148 // Resize sprites if needed, inside a global transaction.
149 bool haveGlobalTransaction = false;
150 for (size_t i = 0; i < numSprites; i++) {
151 SpriteUpdate& update = updates.editItemAt(i);
153 if (update.state.surfaceControl != NULL && update.state.wantSurfaceVisible()) {
154 int32_t desiredWidth = update.state.icon.bitmap.width();
155 int32_t desiredHeight = update.state.icon.bitmap.height();
156 if (update.state.surfaceWidth < desiredWidth
157 || update.state.surfaceHeight < desiredHeight) {
158 if (!haveGlobalTransaction) {
159 SurfaceComposerClient::openGlobalTransaction();
160 haveGlobalTransaction = true;
163 status_t status = update.state.surfaceControl->setSize(desiredWidth, desiredHeight);
165 ALOGE("Error %d resizing sprite surface from %dx%d to %dx%d",
166 status, update.state.surfaceWidth, update.state.surfaceHeight,
167 desiredWidth, desiredHeight);
169 update.state.surfaceWidth = desiredWidth;
170 update.state.surfaceHeight = desiredHeight;
171 update.state.surfaceDrawn = false;
172 update.surfaceChanged = surfaceChanged = true;
174 if (update.state.surfaceVisible) {
175 status = update.state.surfaceControl->hide();
177 ALOGE("Error %d hiding sprite surface after resize.", status);
179 update.state.surfaceVisible = false;
186 if (haveGlobalTransaction) {
187 SurfaceComposerClient::closeGlobalTransaction();
190 // Redraw sprites if needed.
191 for (size_t i = 0; i < numSprites; i++) {
192 SpriteUpdate& update = updates.editItemAt(i);
194 if ((update.state.dirty & DIRTY_BITMAP) && update.state.surfaceDrawn) {
195 update.state.surfaceDrawn = false;
196 update.surfaceChanged = surfaceChanged = true;
199 if (update.state.surfaceControl != NULL && !update.state.surfaceDrawn
200 && update.state.wantSurfaceVisible()) {
201 sp<Surface> surface = update.state.surfaceControl->getSurface();
202 ANativeWindow_Buffer outBuffer;
203 status_t status = surface->lock(&outBuffer, NULL);
205 ALOGE("Error %d locking sprite surface before drawing.", status);
207 SkBitmap surfaceBitmap;
208 ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
209 surfaceBitmap.installPixels(SkImageInfo::MakeN32Premul(outBuffer.width, outBuffer.height),
210 outBuffer.bits, bpr);
212 SkCanvas surfaceCanvas(surfaceBitmap);
215 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
216 surfaceCanvas.drawBitmap(update.state.icon.bitmap, 0, 0, &paint);
218 if (outBuffer.width > update.state.icon.bitmap.width()) {
219 paint.setColor(0); // transparent fill color
220 surfaceCanvas.drawRectCoords(update.state.icon.bitmap.width(), 0,
221 outBuffer.width, update.state.icon.bitmap.height(), paint);
223 if (outBuffer.height > update.state.icon.bitmap.height()) {
224 paint.setColor(0); // transparent fill color
225 surfaceCanvas.drawRectCoords(0, update.state.icon.bitmap.height(),
226 outBuffer.width, outBuffer.height, paint);
229 status = surface->unlockAndPost();
231 ALOGE("Error %d unlocking and posting sprite surface after drawing.", status);
233 update.state.surfaceDrawn = true;
234 update.surfaceChanged = surfaceChanged = true;
240 // Set sprite surface properties and make them visible.
241 bool haveTransaction = false;
242 for (size_t i = 0; i < numSprites; i++) {
243 SpriteUpdate& update = updates.editItemAt(i);
245 bool wantSurfaceVisibleAndDrawn = update.state.wantSurfaceVisible()
246 && update.state.surfaceDrawn;
247 bool becomingVisible = wantSurfaceVisibleAndDrawn && !update.state.surfaceVisible;
248 bool becomingHidden = !wantSurfaceVisibleAndDrawn && update.state.surfaceVisible;
249 if (update.state.surfaceControl != NULL && (becomingVisible || becomingHidden
250 || (wantSurfaceVisibleAndDrawn && (update.state.dirty & (DIRTY_ALPHA
251 | DIRTY_POSITION | DIRTY_TRANSFORMATION_MATRIX | DIRTY_LAYER
252 | DIRTY_VISIBILITY | DIRTY_HOTSPOT))))) {
254 if (!haveTransaction) {
255 SurfaceComposerClient::openGlobalTransaction();
256 haveTransaction = true;
259 if (wantSurfaceVisibleAndDrawn
260 && (becomingVisible || (update.state.dirty & DIRTY_ALPHA))) {
261 status = update.state.surfaceControl->setAlpha(update.state.alpha);
263 ALOGE("Error %d setting sprite surface alpha.", status);
267 if (wantSurfaceVisibleAndDrawn
268 && (becomingVisible || (update.state.dirty & (DIRTY_POSITION
269 | DIRTY_HOTSPOT)))) {
270 status = update.state.surfaceControl->setPosition(
271 update.state.positionX - update.state.icon.hotSpotX,
272 update.state.positionY - update.state.icon.hotSpotY);
274 ALOGE("Error %d setting sprite surface position.", status);
278 if (wantSurfaceVisibleAndDrawn
280 || (update.state.dirty & DIRTY_TRANSFORMATION_MATRIX))) {
281 status = update.state.surfaceControl->setMatrix(
282 update.state.transformationMatrix.dsdx,
283 update.state.transformationMatrix.dtdx,
284 update.state.transformationMatrix.dsdy,
285 update.state.transformationMatrix.dtdy);
287 ALOGE("Error %d setting sprite surface transformation matrix.", status);
291 int32_t surfaceLayer = mOverlayLayer + update.state.layer;
292 if (wantSurfaceVisibleAndDrawn
293 && (becomingVisible || (update.state.dirty & DIRTY_LAYER))) {
294 status = update.state.surfaceControl->setLayer(surfaceLayer);
296 ALOGE("Error %d setting sprite surface layer.", status);
300 if (becomingVisible) {
301 status = update.state.surfaceControl->show();
303 ALOGE("Error %d showing sprite surface.", status);
305 update.state.surfaceVisible = true;
306 update.surfaceChanged = surfaceChanged = true;
308 } else if (becomingHidden) {
309 status = update.state.surfaceControl->hide();
311 ALOGE("Error %d hiding sprite surface.", status);
313 update.state.surfaceVisible = false;
314 update.surfaceChanged = surfaceChanged = true;
320 if (haveTransaction) {
321 SurfaceComposerClient::closeGlobalTransaction();
324 // If any surfaces were changed, write back the new surface properties to the sprites.
325 if (surfaceChanged) { // acquire lock
328 for (size_t i = 0; i < numSprites; i++) {
329 const SpriteUpdate& update = updates.itemAt(i);
331 if (update.surfaceChanged) {
332 update.sprite->setSurfaceLocked(update.state.surfaceControl,
333 update.state.surfaceWidth, update.state.surfaceHeight,
334 update.state.surfaceDrawn, update.state.surfaceVisible);
339 // Clear the sprite update vector outside the lock. It is very important that
340 // we do not clear sprite references inside the lock since we could be releasing
341 // the last remaining reference to the sprite here which would result in the
342 // sprite being deleted and the lock being reacquired by the sprite destructor
343 // while already held.
347 void SpriteController::doDisposeSurfaces() {
348 // Collect disposed surfaces.
349 Vector<sp<SurfaceControl> > disposedSurfaces;
353 disposedSurfaces = mLocked.disposedSurfaces;
354 mLocked.disposedSurfaces.clear();
357 // Release the last reference to each surface outside of the lock.
358 // We don't want the surfaces to be deleted while we are holding our lock.
359 disposedSurfaces.clear();
362 void SpriteController::ensureSurfaceComposerClient() {
363 if (mSurfaceComposerClient == NULL) {
364 mSurfaceComposerClient = new SurfaceComposerClient();
368 sp<SurfaceControl> SpriteController::obtainSurface(int32_t width, int32_t height) {
369 ensureSurfaceComposerClient();
371 sp<SurfaceControl> surfaceControl = mSurfaceComposerClient->createSurface(
372 String8("Sprite"), width, height, PIXEL_FORMAT_RGBA_8888,
373 ISurfaceComposerClient::eHidden |
374 ISurfaceComposerClient::eCursorWindow);
375 if (surfaceControl == NULL || !surfaceControl->isValid()) {
376 ALOGE("Error creating sprite surface.");
379 return surfaceControl;
383 // --- SpriteController::SpriteImpl ---
385 SpriteController::SpriteImpl::SpriteImpl(const sp<SpriteController> controller) :
386 mController(controller) {
389 SpriteController::SpriteImpl::~SpriteImpl() {
390 AutoMutex _m(mController->mLock);
392 // Let the controller take care of deleting the last reference to sprite
393 // surfaces so that we do not block the caller on an IPC here.
394 if (mLocked.state.surfaceControl != NULL) {
395 mController->disposeSurfaceLocked(mLocked.state.surfaceControl);
396 mLocked.state.surfaceControl.clear();
400 void SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon) {
401 AutoMutex _l(mController->mLock);
404 if (icon.isValid()) {
405 icon.bitmap.copyTo(&mLocked.state.icon.bitmap, kN32_SkColorType);
407 if (!mLocked.state.icon.isValid()
408 || mLocked.state.icon.hotSpotX != icon.hotSpotX
409 || mLocked.state.icon.hotSpotY != icon.hotSpotY) {
410 mLocked.state.icon.hotSpotX = icon.hotSpotX;
411 mLocked.state.icon.hotSpotY = icon.hotSpotY;
412 dirty = DIRTY_BITMAP | DIRTY_HOTSPOT;
414 dirty = DIRTY_BITMAP;
416 } else if (mLocked.state.icon.isValid()) {
417 mLocked.state.icon.bitmap.reset();
418 dirty = DIRTY_BITMAP | DIRTY_HOTSPOT;
420 return; // setting to invalid icon and already invalid so nothing to do
423 invalidateLocked(dirty);
426 void SpriteController::SpriteImpl::setVisible(bool visible) {
427 AutoMutex _l(mController->mLock);
429 if (mLocked.state.visible != visible) {
430 mLocked.state.visible = visible;
431 invalidateLocked(DIRTY_VISIBILITY);
435 void SpriteController::SpriteImpl::setPosition(float x, float y) {
436 AutoMutex _l(mController->mLock);
438 if (mLocked.state.positionX != x || mLocked.state.positionY != y) {
439 mLocked.state.positionX = x;
440 mLocked.state.positionY = y;
441 invalidateLocked(DIRTY_POSITION);
445 void SpriteController::SpriteImpl::setLayer(int32_t layer) {
446 AutoMutex _l(mController->mLock);
448 if (mLocked.state.layer != layer) {
449 mLocked.state.layer = layer;
450 invalidateLocked(DIRTY_LAYER);
454 void SpriteController::SpriteImpl::setAlpha(float alpha) {
455 AutoMutex _l(mController->mLock);
457 if (mLocked.state.alpha != alpha) {
458 mLocked.state.alpha = alpha;
459 invalidateLocked(DIRTY_ALPHA);
463 void SpriteController::SpriteImpl::setTransformationMatrix(
464 const SpriteTransformationMatrix& matrix) {
465 AutoMutex _l(mController->mLock);
467 if (mLocked.state.transformationMatrix != matrix) {
468 mLocked.state.transformationMatrix = matrix;
469 invalidateLocked(DIRTY_TRANSFORMATION_MATRIX);
473 void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) {
474 bool wasDirty = mLocked.state.dirty;
475 mLocked.state.dirty |= dirty;
478 mController->invalidateSpriteLocked(this);
482 } // namespace android