blob: c6d4390fcc9214c2bb158ce6a7e452e8268d47fe [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);
39}
40
41SpriteController::~SpriteController() {
42 mLooper->removeMessages(mHandler);
43
44 if (mSurfaceComposerClient != NULL) {
45 mSurfaceComposerClient->dispose();
46 mSurfaceComposerClient.clear();
47 }
48}
49
50sp<Sprite> SpriteController::createSprite() {
51 return new SpriteImpl(this);
52}
53
54void SpriteController::invalidateSpriteLocked(const sp<SpriteImpl>& sprite) {
55 bool wasEmpty = mInvalidatedSprites.isEmpty();
56 mInvalidatedSprites.push(sprite);
57 if (wasEmpty) {
58 mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES));
59 }
60}
61
62void SpriteController::disposeSurfaceLocked(const sp<SurfaceControl>& surfaceControl) {
63 bool wasEmpty = mDisposedSurfaces.isEmpty();
64 mDisposedSurfaces.push(surfaceControl);
65 if (wasEmpty) {
66 mLooper->sendMessage(mHandler, Message(MSG_DISPOSE_SURFACES));
67 }
68}
69
70void SpriteController::handleMessage(const Message& message) {
71 switch (message.what) {
72 case MSG_UPDATE_SPRITES:
73 doUpdateSprites();
74 break;
75 case MSG_DISPOSE_SURFACES:
76 doDisposeSurfaces();
77 break;
78 }
79}
80
81void SpriteController::doUpdateSprites() {
82 // Collect information about sprite updates.
83 // Each sprite update record includes a reference to its associated sprite so we can
84 // be certain the sprites will not be deleted while this function runs. Sprites
85 // may invalidate themselves again during this time but we will handle those changes
86 // in the next iteration.
87 Vector<SpriteUpdate> updates;
88 size_t numSprites;
89 { // acquire lock
90 AutoMutex _l(mLock);
91
92 numSprites = mInvalidatedSprites.size();
93 for (size_t i = 0; i < numSprites; i++) {
94 const sp<SpriteImpl>& sprite = mInvalidatedSprites.itemAt(i);
95
96 updates.push(SpriteUpdate(sprite, sprite->getStateLocked()));
97 sprite->resetDirtyLocked();
98 }
99 mInvalidatedSprites.clear();
100 } // release lock
101
102 // Create missing surfaces.
103 bool surfaceChanged = false;
104 for (size_t i = 0; i < numSprites; i++) {
105 SpriteUpdate& update = updates.editItemAt(i);
106
107 if (update.state.surfaceControl == NULL && update.state.wantSurfaceVisible()) {
108 update.state.surfaceWidth = update.state.bitmap.width();
109 update.state.surfaceHeight = update.state.bitmap.height();
110 update.state.surfaceDrawn = false;
111 update.state.surfaceVisible = false;
112 update.state.surfaceControl = obtainSurface(
113 update.state.surfaceWidth, update.state.surfaceHeight);
114 if (update.state.surfaceControl != NULL) {
115 update.surfaceChanged = surfaceChanged = true;
116 }
117 }
118 }
119
120 // Resize sprites if needed, inside a global transaction.
121 bool haveGlobalTransaction = false;
122 for (size_t i = 0; i < numSprites; i++) {
123 SpriteUpdate& update = updates.editItemAt(i);
124
125 if (update.state.surfaceControl != NULL && update.state.wantSurfaceVisible()) {
126 int32_t desiredWidth = update.state.bitmap.width();
127 int32_t desiredHeight = update.state.bitmap.height();
128 if (update.state.surfaceWidth < desiredWidth
129 || update.state.surfaceHeight < desiredHeight) {
130 if (!haveGlobalTransaction) {
131 SurfaceComposerClient::openGlobalTransaction();
132 haveGlobalTransaction = true;
133 }
134
135 status_t status = update.state.surfaceControl->setSize(desiredWidth, desiredHeight);
136 if (status) {
137 LOGE("Error %d resizing sprite surface from %dx%d to %dx%d",
138 status, update.state.surfaceWidth, update.state.surfaceHeight,
139 desiredWidth, desiredHeight);
140 } else {
141 update.state.surfaceWidth = desiredWidth;
142 update.state.surfaceHeight = desiredHeight;
143 update.state.surfaceDrawn = false;
144 update.surfaceChanged = surfaceChanged = true;
145
146 if (update.state.surfaceVisible) {
147 status = update.state.surfaceControl->hide();
148 if (status) {
149 LOGE("Error %d hiding sprite surface after resize.", status);
150 } else {
151 update.state.surfaceVisible = false;
152 }
153 }
154 }
155 }
156 }
157 }
158 if (haveGlobalTransaction) {
159 SurfaceComposerClient::closeGlobalTransaction();
160 }
161
162 // Redraw sprites if needed.
163 for (size_t i = 0; i < numSprites; i++) {
164 SpriteUpdate& update = updates.editItemAt(i);
165
166 if ((update.state.dirty & DIRTY_BITMAP) && update.state.surfaceDrawn) {
167 update.state.surfaceDrawn = false;
168 update.surfaceChanged = surfaceChanged = true;
169 }
170
171 if (update.state.surfaceControl != NULL && !update.state.surfaceDrawn
172 && update.state.wantSurfaceVisible()) {
173 sp<Surface> surface = update.state.surfaceControl->getSurface();
174 Surface::SurfaceInfo surfaceInfo;
175 status_t status = surface->lock(&surfaceInfo);
176 if (status) {
177 LOGE("Error %d locking sprite surface before drawing.", status);
178 } else {
179 SkBitmap surfaceBitmap;
180 ssize_t bpr = surfaceInfo.s * bytesPerPixel(surfaceInfo.format);
181 surfaceBitmap.setConfig(SkBitmap::kARGB_8888_Config,
182 surfaceInfo.w, surfaceInfo.h, bpr);
183 surfaceBitmap.setPixels(surfaceInfo.bits);
184
185 SkCanvas surfaceCanvas;
186 surfaceCanvas.setBitmapDevice(surfaceBitmap);
187
188 SkPaint paint;
189 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
190 surfaceCanvas.drawBitmap(update.state.bitmap, 0, 0, &paint);
191
192 if (surfaceInfo.w > uint32_t(update.state.bitmap.width())) {
193 paint.setColor(0); // transparent fill color
194 surfaceCanvas.drawRectCoords(update.state.bitmap.width(), 0,
195 surfaceInfo.w, update.state.bitmap.height(), paint);
196 }
197 if (surfaceInfo.h > uint32_t(update.state.bitmap.height())) {
198 paint.setColor(0); // transparent fill color
199 surfaceCanvas.drawRectCoords(0, update.state.bitmap.height(),
200 surfaceInfo.w, surfaceInfo.h, paint);
201 }
202
203 status = surface->unlockAndPost();
204 if (status) {
205 LOGE("Error %d unlocking and posting sprite surface after drawing.", status);
206 } else {
207 update.state.surfaceDrawn = true;
208 update.surfaceChanged = surfaceChanged = true;
209 }
210 }
211 }
212 }
213
214 // Set sprite surface properties and make them visible.
215 bool haveTransaction = false;
216 for (size_t i = 0; i < numSprites; i++) {
217 SpriteUpdate& update = updates.editItemAt(i);
218
219 bool wantSurfaceVisibleAndDrawn = update.state.wantSurfaceVisible()
220 && update.state.surfaceDrawn;
221 bool becomingVisible = wantSurfaceVisibleAndDrawn && !update.state.surfaceVisible;
222 bool becomingHidden = !wantSurfaceVisibleAndDrawn && update.state.surfaceVisible;
223 if (update.state.surfaceControl != NULL && (becomingVisible || becomingHidden
224 || (wantSurfaceVisibleAndDrawn && (update.state.dirty & (DIRTY_ALPHA
225 | DIRTY_POSITION | DIRTY_TRANSFORMATION_MATRIX | DIRTY_LAYER
226 | DIRTY_VISIBILITY | DIRTY_HOTSPOT))))) {
227 status_t status;
228 if (!haveTransaction) {
229 status = mSurfaceComposerClient->openTransaction();
230 if (status) {
231 LOGE("Error %d opening transation to update sprite surface.", status);
232 break;
233 }
234 haveTransaction = true;
235 }
236
237 if (wantSurfaceVisibleAndDrawn
238 && (becomingVisible || (update.state.dirty & DIRTY_ALPHA))) {
239 status = update.state.surfaceControl->setAlpha(update.state.alpha);
240 if (status) {
241 LOGE("Error %d setting sprite surface alpha.", status);
242 }
243 }
244
245 if (wantSurfaceVisibleAndDrawn
246 && (becomingVisible || (update.state.dirty & (DIRTY_POSITION
247 | DIRTY_HOTSPOT)))) {
248 status = update.state.surfaceControl->setPosition(
249 update.state.positionX - update.state.hotSpotX,
250 update.state.positionY - update.state.hotSpotY);
251 if (status) {
252 LOGE("Error %d setting sprite surface position.", status);
253 }
254 }
255
256 if (wantSurfaceVisibleAndDrawn
257 && (becomingVisible
258 || (update.state.dirty & DIRTY_TRANSFORMATION_MATRIX))) {
259 status = update.state.surfaceControl->setMatrix(
260 update.state.transformationMatrix.dsdx,
261 update.state.transformationMatrix.dtdx,
262 update.state.transformationMatrix.dsdy,
263 update.state.transformationMatrix.dtdy);
264 if (status) {
265 LOGE("Error %d setting sprite surface transformation matrix.", status);
266 }
267 }
268
269 int32_t surfaceLayer = mOverlayLayer + update.state.layer;
270 if (wantSurfaceVisibleAndDrawn
271 && (becomingVisible || (update.state.dirty & DIRTY_LAYER))) {
272 status = update.state.surfaceControl->setLayer(surfaceLayer);
273 if (status) {
274 LOGE("Error %d setting sprite surface layer.", status);
275 }
276 }
277
278 if (becomingVisible) {
279 status = update.state.surfaceControl->show(surfaceLayer);
280 if (status) {
281 LOGE("Error %d showing sprite surface.", status);
282 } else {
283 update.state.surfaceVisible = true;
284 update.surfaceChanged = surfaceChanged = true;
285 }
286 } else if (becomingHidden) {
287 status = update.state.surfaceControl->hide();
288 if (status) {
289 LOGE("Error %d hiding sprite surface.", status);
290 } else {
291 update.state.surfaceVisible = false;
292 update.surfaceChanged = surfaceChanged = true;
293 }
294 }
295 }
296 }
297
298 if (haveTransaction) {
299 status_t status = mSurfaceComposerClient->closeTransaction();
300 if (status) {
301 LOGE("Error %d closing transaction to update sprite surface.", status);
302 }
303 }
304
305 // If any surfaces were changed, write back the new surface properties to the sprites.
306 if (surfaceChanged) { // acquire lock
307 AutoMutex _l(mLock);
308
309 for (size_t i = 0; i < numSprites; i++) {
310 const SpriteUpdate& update = updates.itemAt(i);
311
312 if (update.surfaceChanged) {
313 update.sprite->setSurfaceLocked(update.state.surfaceControl,
314 update.state.surfaceWidth, update.state.surfaceHeight,
315 update.state.surfaceDrawn, update.state.surfaceVisible);
316 }
317 }
318 } // release lock
319
320 // Clear the sprite update vector outside the lock. It is very important that
321 // we do not clear sprite references inside the lock since we could be releasing
322 // the last remaining reference to the sprite here which would result in the
323 // sprite being deleted and the lock being reacquired by the sprite destructor
324 // while already held.
325 updates.clear();
326}
327
328void SpriteController::doDisposeSurfaces() {
329 // Collect disposed surfaces.
330 Vector<sp<SurfaceControl> > disposedSurfaces;
331 { // acquire lock
332 disposedSurfaces = mDisposedSurfaces;
333 mDisposedSurfaces.clear();
334 } // release lock
335
336 // Release the last reference to each surface outside of the lock.
337 // We don't want the surfaces to be deleted while we are holding our lock.
338 disposedSurfaces.clear();
339}
340
341void SpriteController::ensureSurfaceComposerClient() {
342 if (mSurfaceComposerClient == NULL) {
343 mSurfaceComposerClient = new SurfaceComposerClient();
344 }
345}
346
347sp<SurfaceControl> SpriteController::obtainSurface(int32_t width, int32_t height) {
348 ensureSurfaceComposerClient();
349
350 sp<SurfaceControl> surfaceControl = mSurfaceComposerClient->createSurface(
351 getpid(), String8("Sprite"), 0, width, height, PIXEL_FORMAT_RGBA_8888);
352 if (surfaceControl == NULL) {
353 LOGE("Error creating sprite surface.");
354 return NULL;
355 }
356 return surfaceControl;
357}
358
359
360// --- SpriteController::SpriteImpl ---
361
362SpriteController::SpriteImpl::SpriteImpl(const sp<SpriteController> controller) :
363 mController(controller), mTransactionNestingCount(0) {
364}
365
366SpriteController::SpriteImpl::~SpriteImpl() {
367 AutoMutex _m(mController->mLock);
368
369 // Let the controller take care of deleting the last reference to sprite
370 // surfaces so that we do not block the caller on an IPC here.
371 if (mState.surfaceControl != NULL) {
372 mController->disposeSurfaceLocked(mState.surfaceControl);
373 mState.surfaceControl.clear();
374 }
375}
376
377void SpriteController::SpriteImpl::setBitmap(const SkBitmap* bitmap,
378 float hotSpotX, float hotSpotY) {
379 AutoMutex _l(mController->mLock);
380
381 if (bitmap) {
382 bitmap->copyTo(&mState.bitmap, SkBitmap::kARGB_8888_Config);
383 } else {
384 mState.bitmap.reset();
385 }
386
387 uint32_t dirty = DIRTY_BITMAP;
388 if (mState.hotSpotX != hotSpotX || mState.hotSpotY != hotSpotY) {
389 mState.hotSpotX = hotSpotX;
390 mState.hotSpotY = hotSpotY;
391 dirty |= DIRTY_HOTSPOT;
392 }
393
394 invalidateLocked(dirty);
395}
396
397void SpriteController::SpriteImpl::setVisible(bool visible) {
398 AutoMutex _l(mController->mLock);
399
400 if (mState.visible != visible) {
401 mState.visible = visible;
402 invalidateLocked(DIRTY_VISIBILITY);
403 }
404}
405
406void SpriteController::SpriteImpl::setPosition(float x, float y) {
407 AutoMutex _l(mController->mLock);
408
409 if (mState.positionX != x || mState.positionY != y) {
410 mState.positionX = x;
411 mState.positionY = y;
412 invalidateLocked(DIRTY_POSITION);
413 }
414}
415
416void SpriteController::SpriteImpl::setLayer(int32_t layer) {
417 AutoMutex _l(mController->mLock);
418
419 if (mState.layer != layer) {
420 mState.layer = layer;
421 invalidateLocked(DIRTY_LAYER);
422 }
423}
424
425void SpriteController::SpriteImpl::setAlpha(float alpha) {
426 AutoMutex _l(mController->mLock);
427
428 if (mState.alpha != alpha) {
429 mState.alpha = alpha;
430 invalidateLocked(DIRTY_ALPHA);
431 }
432}
433
434void SpriteController::SpriteImpl::setTransformationMatrix(
435 const SpriteTransformationMatrix& matrix) {
436 AutoMutex _l(mController->mLock);
437
438 if (mState.transformationMatrix != matrix) {
439 mState.transformationMatrix = matrix;
440 invalidateLocked(DIRTY_TRANSFORMATION_MATRIX);
441 }
442}
443
444void SpriteController::SpriteImpl::openTransaction() {
445 AutoMutex _l(mController->mLock);
446
447 mTransactionNestingCount += 1;
448}
449
450void SpriteController::SpriteImpl::closeTransaction() {
451 AutoMutex _l(mController->mLock);
452
453 LOG_ALWAYS_FATAL_IF(mTransactionNestingCount == 0,
454 "Sprite closeTransaction() called but there is no open sprite transaction");
455
456 mTransactionNestingCount -= 1;
457 if (mTransactionNestingCount == 0 && mState.dirty) {
458 mController->invalidateSpriteLocked(this);
459 }
460}
461
462void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) {
463 if (mTransactionNestingCount > 0) {
464 bool wasDirty = mState.dirty;
465 mState.dirty |= dirty;
466 if (!wasDirty) {
467 mController->invalidateSpriteLocked(this);
468 }
469 }
470}
471
472} // namespace android