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