blob: 08cc75e631eb8100a4519c9b51f44b030d155b93 [file] [log] [blame]
Jeff Brown5541de92011-04-11 11:54:25 -07001/*
2 * Copyright (C) 2011 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#define LOG_TAG "Sprites"
18
19//#define LOG_NDEBUG 0
20
21#include "SpriteController.h"
22
23#include <cutils/log.h>
24#include <utils/String8.h>
25
26#include <SkBitmap.h>
27#include <SkCanvas.h>
28#include <SkColor.h>
29#include <SkPaint.h>
30#include <SkXfermode.h>
31
32namespace android {
33
34// --- SpriteController ---
35
36SpriteController::SpriteController(const sp<Looper>& looper, int32_t overlayLayer) :
37 mLooper(looper), mOverlayLayer(overlayLayer) {
38 mHandler = new WeakMessageHandler(this);
Jeff Brown2352b972011-04-12 22:39:53 -070039
40 mLocked.transactionNestingCount = 0;
41 mLocked.deferredSpriteUpdate = false;
Jeff Brown5541de92011-04-11 11:54:25 -070042}
43
44SpriteController::~SpriteController() {
45 mLooper->removeMessages(mHandler);
46
47 if (mSurfaceComposerClient != NULL) {
48 mSurfaceComposerClient->dispose();
49 mSurfaceComposerClient.clear();
50 }
51}
52
53sp<Sprite> SpriteController::createSprite() {
54 return new SpriteImpl(this);
55}
56
Jeff Brown2352b972011-04-12 22:39:53 -070057void SpriteController::openTransaction() {
58 AutoMutex _l(mLock);
59
60 mLocked.transactionNestingCount += 1;
61}
62
63void SpriteController::closeTransaction() {
64 AutoMutex _l(mLock);
65
66 LOG_ALWAYS_FATAL_IF(mLocked.transactionNestingCount == 0,
67 "Sprite closeTransaction() called but there is no open sprite transaction");
68
69 mLocked.transactionNestingCount -= 1;
70 if (mLocked.transactionNestingCount == 0 && mLocked.deferredSpriteUpdate) {
71 mLocked.deferredSpriteUpdate = false;
Jeff Brown5541de92011-04-11 11:54:25 -070072 mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES));
73 }
74}
75
Jeff Brown2352b972011-04-12 22:39:53 -070076void SpriteController::invalidateSpriteLocked(const sp<SpriteImpl>& sprite) {
77 bool wasEmpty = mLocked.invalidatedSprites.isEmpty();
78 mLocked.invalidatedSprites.push(sprite);
79 if (wasEmpty) {
80 if (mLocked.transactionNestingCount != 0) {
81 mLocked.deferredSpriteUpdate = true;
82 } else {
83 mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES));
84 }
85 }
86}
87
Jeff Brown5541de92011-04-11 11:54:25 -070088void SpriteController::disposeSurfaceLocked(const sp<SurfaceControl>& surfaceControl) {
Jeff Brown2352b972011-04-12 22:39:53 -070089 bool wasEmpty = mLocked.disposedSurfaces.isEmpty();
90 mLocked.disposedSurfaces.push(surfaceControl);
Jeff Brown5541de92011-04-11 11:54:25 -070091 if (wasEmpty) {
92 mLooper->sendMessage(mHandler, Message(MSG_DISPOSE_SURFACES));
93 }
94}
95
96void SpriteController::handleMessage(const Message& message) {
97 switch (message.what) {
98 case MSG_UPDATE_SPRITES:
99 doUpdateSprites();
100 break;
101 case MSG_DISPOSE_SURFACES:
102 doDisposeSurfaces();
103 break;
104 }
105}
106
107void SpriteController::doUpdateSprites() {
108 // Collect information about sprite updates.
109 // Each sprite update record includes a reference to its associated sprite so we can
110 // be certain the sprites will not be deleted while this function runs. Sprites
111 // may invalidate themselves again during this time but we will handle those changes
112 // in the next iteration.
113 Vector<SpriteUpdate> updates;
114 size_t numSprites;
115 { // acquire lock
116 AutoMutex _l(mLock);
117
Jeff Brown2352b972011-04-12 22:39:53 -0700118 numSprites = mLocked.invalidatedSprites.size();
Jeff Brown5541de92011-04-11 11:54:25 -0700119 for (size_t i = 0; i < numSprites; i++) {
Jeff Brown2352b972011-04-12 22:39:53 -0700120 const sp<SpriteImpl>& sprite = mLocked.invalidatedSprites.itemAt(i);
Jeff Brown5541de92011-04-11 11:54:25 -0700121
122 updates.push(SpriteUpdate(sprite, sprite->getStateLocked()));
123 sprite->resetDirtyLocked();
124 }
Jeff Brown2352b972011-04-12 22:39:53 -0700125 mLocked.invalidatedSprites.clear();
Jeff Brown5541de92011-04-11 11:54:25 -0700126 } // release lock
127
128 // Create missing surfaces.
129 bool surfaceChanged = false;
130 for (size_t i = 0; i < numSprites; i++) {
131 SpriteUpdate& update = updates.editItemAt(i);
132
133 if (update.state.surfaceControl == NULL && update.state.wantSurfaceVisible()) {
Jeff Brown2352b972011-04-12 22:39:53 -0700134 update.state.surfaceWidth = update.state.icon.bitmap.width();
135 update.state.surfaceHeight = update.state.icon.bitmap.height();
Jeff Brown5541de92011-04-11 11:54:25 -0700136 update.state.surfaceDrawn = false;
137 update.state.surfaceVisible = false;
138 update.state.surfaceControl = obtainSurface(
139 update.state.surfaceWidth, update.state.surfaceHeight);
140 if (update.state.surfaceControl != NULL) {
141 update.surfaceChanged = surfaceChanged = true;
142 }
143 }
144 }
145
146 // Resize sprites if needed, inside a global transaction.
147 bool haveGlobalTransaction = false;
148 for (size_t i = 0; i < numSprites; i++) {
149 SpriteUpdate& update = updates.editItemAt(i);
150
151 if (update.state.surfaceControl != NULL && update.state.wantSurfaceVisible()) {
Jeff Brown2352b972011-04-12 22:39:53 -0700152 int32_t desiredWidth = update.state.icon.bitmap.width();
153 int32_t desiredHeight = update.state.icon.bitmap.height();
Jeff Brown5541de92011-04-11 11:54:25 -0700154 if (update.state.surfaceWidth < desiredWidth
155 || update.state.surfaceHeight < desiredHeight) {
156 if (!haveGlobalTransaction) {
157 SurfaceComposerClient::openGlobalTransaction();
158 haveGlobalTransaction = true;
159 }
160
161 status_t status = update.state.surfaceControl->setSize(desiredWidth, desiredHeight);
162 if (status) {
163 LOGE("Error %d resizing sprite surface from %dx%d to %dx%d",
164 status, update.state.surfaceWidth, update.state.surfaceHeight,
165 desiredWidth, desiredHeight);
166 } else {
167 update.state.surfaceWidth = desiredWidth;
168 update.state.surfaceHeight = desiredHeight;
169 update.state.surfaceDrawn = false;
170 update.surfaceChanged = surfaceChanged = true;
171
172 if (update.state.surfaceVisible) {
173 status = update.state.surfaceControl->hide();
174 if (status) {
175 LOGE("Error %d hiding sprite surface after resize.", status);
176 } else {
177 update.state.surfaceVisible = false;
178 }
179 }
180 }
181 }
182 }
183 }
184 if (haveGlobalTransaction) {
185 SurfaceComposerClient::closeGlobalTransaction();
186 }
187
188 // Redraw sprites if needed.
189 for (size_t i = 0; i < numSprites; i++) {
190 SpriteUpdate& update = updates.editItemAt(i);
191
192 if ((update.state.dirty & DIRTY_BITMAP) && update.state.surfaceDrawn) {
193 update.state.surfaceDrawn = false;
194 update.surfaceChanged = surfaceChanged = true;
195 }
196
197 if (update.state.surfaceControl != NULL && !update.state.surfaceDrawn
198 && update.state.wantSurfaceVisible()) {
199 sp<Surface> surface = update.state.surfaceControl->getSurface();
200 Surface::SurfaceInfo surfaceInfo;
201 status_t status = surface->lock(&surfaceInfo);
202 if (status) {
203 LOGE("Error %d locking sprite surface before drawing.", status);
204 } else {
205 SkBitmap surfaceBitmap;
206 ssize_t bpr = surfaceInfo.s * bytesPerPixel(surfaceInfo.format);
207 surfaceBitmap.setConfig(SkBitmap::kARGB_8888_Config,
208 surfaceInfo.w, surfaceInfo.h, bpr);
209 surfaceBitmap.setPixels(surfaceInfo.bits);
210
211 SkCanvas surfaceCanvas;
212 surfaceCanvas.setBitmapDevice(surfaceBitmap);
213
214 SkPaint paint;
215 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
Jeff Brown2352b972011-04-12 22:39:53 -0700216 surfaceCanvas.drawBitmap(update.state.icon.bitmap, 0, 0, &paint);
Jeff Brown5541de92011-04-11 11:54:25 -0700217
Jeff Brown2352b972011-04-12 22:39:53 -0700218 if (surfaceInfo.w > uint32_t(update.state.icon.bitmap.width())) {
Jeff Brown5541de92011-04-11 11:54:25 -0700219 paint.setColor(0); // transparent fill color
Jeff Brown2352b972011-04-12 22:39:53 -0700220 surfaceCanvas.drawRectCoords(update.state.icon.bitmap.width(), 0,
221 surfaceInfo.w, update.state.icon.bitmap.height(), paint);
Jeff Brown5541de92011-04-11 11:54:25 -0700222 }
Jeff Brown2352b972011-04-12 22:39:53 -0700223 if (surfaceInfo.h > uint32_t(update.state.icon.bitmap.height())) {
Jeff Brown5541de92011-04-11 11:54:25 -0700224 paint.setColor(0); // transparent fill color
Jeff Brown2352b972011-04-12 22:39:53 -0700225 surfaceCanvas.drawRectCoords(0, update.state.icon.bitmap.height(),
Jeff Brown5541de92011-04-11 11:54:25 -0700226 surfaceInfo.w, surfaceInfo.h, paint);
227 }
228
229 status = surface->unlockAndPost();
230 if (status) {
231 LOGE("Error %d unlocking and posting sprite surface after drawing.", status);
232 } else {
233 update.state.surfaceDrawn = true;
234 update.surfaceChanged = surfaceChanged = true;
235 }
236 }
237 }
238 }
239
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);
244
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))))) {
253 status_t status;
254 if (!haveTransaction) {
255 status = mSurfaceComposerClient->openTransaction();
256 if (status) {
257 LOGE("Error %d opening transation to update sprite surface.", status);
258 break;
259 }
260 haveTransaction = true;
261 }
262
263 if (wantSurfaceVisibleAndDrawn
264 && (becomingVisible || (update.state.dirty & DIRTY_ALPHA))) {
265 status = update.state.surfaceControl->setAlpha(update.state.alpha);
266 if (status) {
267 LOGE("Error %d setting sprite surface alpha.", status);
268 }
269 }
270
271 if (wantSurfaceVisibleAndDrawn
272 && (becomingVisible || (update.state.dirty & (DIRTY_POSITION
273 | DIRTY_HOTSPOT)))) {
274 status = update.state.surfaceControl->setPosition(
Jeff Brown2352b972011-04-12 22:39:53 -0700275 update.state.positionX - update.state.icon.hotSpotX,
276 update.state.positionY - update.state.icon.hotSpotY);
Jeff Brown5541de92011-04-11 11:54:25 -0700277 if (status) {
278 LOGE("Error %d setting sprite surface position.", status);
279 }
280 }
281
282 if (wantSurfaceVisibleAndDrawn
283 && (becomingVisible
284 || (update.state.dirty & DIRTY_TRANSFORMATION_MATRIX))) {
285 status = update.state.surfaceControl->setMatrix(
286 update.state.transformationMatrix.dsdx,
287 update.state.transformationMatrix.dtdx,
288 update.state.transformationMatrix.dsdy,
289 update.state.transformationMatrix.dtdy);
290 if (status) {
291 LOGE("Error %d setting sprite surface transformation matrix.", status);
292 }
293 }
294
295 int32_t surfaceLayer = mOverlayLayer + update.state.layer;
296 if (wantSurfaceVisibleAndDrawn
297 && (becomingVisible || (update.state.dirty & DIRTY_LAYER))) {
298 status = update.state.surfaceControl->setLayer(surfaceLayer);
299 if (status) {
300 LOGE("Error %d setting sprite surface layer.", status);
301 }
302 }
303
304 if (becomingVisible) {
305 status = update.state.surfaceControl->show(surfaceLayer);
306 if (status) {
307 LOGE("Error %d showing sprite surface.", status);
308 } else {
309 update.state.surfaceVisible = true;
310 update.surfaceChanged = surfaceChanged = true;
311 }
312 } else if (becomingHidden) {
313 status = update.state.surfaceControl->hide();
314 if (status) {
315 LOGE("Error %d hiding sprite surface.", status);
316 } else {
317 update.state.surfaceVisible = false;
318 update.surfaceChanged = surfaceChanged = true;
319 }
320 }
321 }
322 }
323
324 if (haveTransaction) {
325 status_t status = mSurfaceComposerClient->closeTransaction();
326 if (status) {
327 LOGE("Error %d closing transaction to update sprite surface.", status);
328 }
329 }
330
331 // If any surfaces were changed, write back the new surface properties to the sprites.
332 if (surfaceChanged) { // acquire lock
333 AutoMutex _l(mLock);
334
335 for (size_t i = 0; i < numSprites; i++) {
336 const SpriteUpdate& update = updates.itemAt(i);
337
338 if (update.surfaceChanged) {
339 update.sprite->setSurfaceLocked(update.state.surfaceControl,
340 update.state.surfaceWidth, update.state.surfaceHeight,
341 update.state.surfaceDrawn, update.state.surfaceVisible);
342 }
343 }
344 } // release lock
345
346 // Clear the sprite update vector outside the lock. It is very important that
347 // we do not clear sprite references inside the lock since we could be releasing
348 // the last remaining reference to the sprite here which would result in the
349 // sprite being deleted and the lock being reacquired by the sprite destructor
350 // while already held.
351 updates.clear();
352}
353
354void SpriteController::doDisposeSurfaces() {
355 // Collect disposed surfaces.
356 Vector<sp<SurfaceControl> > disposedSurfaces;
357 { // acquire lock
Jeff Brown2352b972011-04-12 22:39:53 -0700358 AutoMutex _l(mLock);
359
360 disposedSurfaces = mLocked.disposedSurfaces;
361 mLocked.disposedSurfaces.clear();
Jeff Brown5541de92011-04-11 11:54:25 -0700362 } // release lock
363
364 // Release the last reference to each surface outside of the lock.
365 // We don't want the surfaces to be deleted while we are holding our lock.
366 disposedSurfaces.clear();
367}
368
369void SpriteController::ensureSurfaceComposerClient() {
370 if (mSurfaceComposerClient == NULL) {
371 mSurfaceComposerClient = new SurfaceComposerClient();
372 }
373}
374
375sp<SurfaceControl> SpriteController::obtainSurface(int32_t width, int32_t height) {
376 ensureSurfaceComposerClient();
377
378 sp<SurfaceControl> surfaceControl = mSurfaceComposerClient->createSurface(
Mathias Agopian9638e5c2011-04-20 14:19:32 -0700379 String8("Sprite"), 0, width, height, PIXEL_FORMAT_RGBA_8888);
Jeff Brown2352b972011-04-12 22:39:53 -0700380 if (surfaceControl == NULL || !surfaceControl->isValid()
381 || !surfaceControl->getSurface()->isValid()) {
Jeff Brown5541de92011-04-11 11:54:25 -0700382 LOGE("Error creating sprite surface.");
383 return NULL;
384 }
385 return surfaceControl;
386}
387
388
389// --- SpriteController::SpriteImpl ---
390
391SpriteController::SpriteImpl::SpriteImpl(const sp<SpriteController> controller) :
Jeff Brown2352b972011-04-12 22:39:53 -0700392 mController(controller) {
Jeff Brown5541de92011-04-11 11:54:25 -0700393}
394
395SpriteController::SpriteImpl::~SpriteImpl() {
396 AutoMutex _m(mController->mLock);
397
398 // Let the controller take care of deleting the last reference to sprite
399 // surfaces so that we do not block the caller on an IPC here.
Jeff Brown2352b972011-04-12 22:39:53 -0700400 if (mLocked.state.surfaceControl != NULL) {
401 mController->disposeSurfaceLocked(mLocked.state.surfaceControl);
402 mLocked.state.surfaceControl.clear();
Jeff Brown5541de92011-04-11 11:54:25 -0700403 }
404}
405
Jeff Brown2352b972011-04-12 22:39:53 -0700406void SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon) {
Jeff Brown5541de92011-04-11 11:54:25 -0700407 AutoMutex _l(mController->mLock);
408
Jeff Brown2352b972011-04-12 22:39:53 -0700409 uint32_t dirty;
410 if (icon.isValid()) {
411 icon.bitmap.copyTo(&mLocked.state.icon.bitmap, SkBitmap::kARGB_8888_Config);
Jeff Brown5541de92011-04-11 11:54:25 -0700412
Jeff Brown2352b972011-04-12 22:39:53 -0700413 if (!mLocked.state.icon.isValid()
414 || mLocked.state.icon.hotSpotX != icon.hotSpotX
415 || mLocked.state.icon.hotSpotY != icon.hotSpotY) {
416 mLocked.state.icon.hotSpotX = icon.hotSpotX;
417 mLocked.state.icon.hotSpotY = icon.hotSpotY;
418 dirty = DIRTY_BITMAP | DIRTY_HOTSPOT;
419 } else {
420 dirty = DIRTY_BITMAP;
421 }
422 } else if (mLocked.state.icon.isValid()) {
423 mLocked.state.icon.bitmap.reset();
424 dirty = DIRTY_BITMAP | DIRTY_HOTSPOT;
425 } else {
426 return; // setting to invalid icon and already invalid so nothing to do
Jeff Brown5541de92011-04-11 11:54:25 -0700427 }
428
429 invalidateLocked(dirty);
430}
431
432void SpriteController::SpriteImpl::setVisible(bool visible) {
433 AutoMutex _l(mController->mLock);
434
Jeff Brown2352b972011-04-12 22:39:53 -0700435 if (mLocked.state.visible != visible) {
436 mLocked.state.visible = visible;
Jeff Brown5541de92011-04-11 11:54:25 -0700437 invalidateLocked(DIRTY_VISIBILITY);
438 }
439}
440
441void SpriteController::SpriteImpl::setPosition(float x, float y) {
442 AutoMutex _l(mController->mLock);
443
Jeff Brown2352b972011-04-12 22:39:53 -0700444 if (mLocked.state.positionX != x || mLocked.state.positionY != y) {
445 mLocked.state.positionX = x;
446 mLocked.state.positionY = y;
Jeff Brown5541de92011-04-11 11:54:25 -0700447 invalidateLocked(DIRTY_POSITION);
448 }
449}
450
451void SpriteController::SpriteImpl::setLayer(int32_t layer) {
452 AutoMutex _l(mController->mLock);
453
Jeff Brown2352b972011-04-12 22:39:53 -0700454 if (mLocked.state.layer != layer) {
455 mLocked.state.layer = layer;
Jeff Brown5541de92011-04-11 11:54:25 -0700456 invalidateLocked(DIRTY_LAYER);
457 }
458}
459
460void SpriteController::SpriteImpl::setAlpha(float alpha) {
461 AutoMutex _l(mController->mLock);
462
Jeff Brown2352b972011-04-12 22:39:53 -0700463 if (mLocked.state.alpha != alpha) {
464 mLocked.state.alpha = alpha;
Jeff Brown5541de92011-04-11 11:54:25 -0700465 invalidateLocked(DIRTY_ALPHA);
466 }
467}
468
469void SpriteController::SpriteImpl::setTransformationMatrix(
470 const SpriteTransformationMatrix& matrix) {
471 AutoMutex _l(mController->mLock);
472
Jeff Brown2352b972011-04-12 22:39:53 -0700473 if (mLocked.state.transformationMatrix != matrix) {
474 mLocked.state.transformationMatrix = matrix;
Jeff Brown5541de92011-04-11 11:54:25 -0700475 invalidateLocked(DIRTY_TRANSFORMATION_MATRIX);
476 }
477}
478
Jeff Brown5541de92011-04-11 11:54:25 -0700479void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) {
Jeff Brown2352b972011-04-12 22:39:53 -0700480 bool wasDirty = mLocked.state.dirty;
481 mLocked.state.dirty |= dirty;
482
483 if (!wasDirty) {
484 mController->invalidateSpriteLocked(this);
Jeff Brown5541de92011-04-11 11:54:25 -0700485 }
486}
487
488} // namespace android