blob: eb2bc98ec9e9c4a66a01b962651d9c4f2e557a76 [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"
Jeff Brown5541de92011-04-11 11:54:25 -070018//#define LOG_NDEBUG 0
19
20#include "SpriteController.h"
21
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070022#include <log/log.h>
Jeff Brown5541de92011-04-11 11:54:25 -070023#include <utils/String8.h>
Mathias Agopian52800612013-02-14 17:11:20 -080024#include <gui/Surface.h>
Jeff Brown5541de92011-04-11 11:54:25 -070025
26#include <SkBitmap.h>
27#include <SkCanvas.h>
28#include <SkColor.h>
29#include <SkPaint.h>
Andreas Gampe6b83b762014-11-10 15:55:11 -080030
Mathias Agopian52800612013-02-14 17:11:20 -080031#include <android/native_window.h>
Jeff Brown5541de92011-04-11 11:54:25 -070032
33namespace android {
34
35// --- SpriteController ---
36
37SpriteController::SpriteController(const sp<Looper>& looper, int32_t overlayLayer) :
38 mLooper(looper), mOverlayLayer(overlayLayer) {
39 mHandler = new WeakMessageHandler(this);
Jeff Brown2352b972011-04-12 22:39:53 -070040
41 mLocked.transactionNestingCount = 0;
42 mLocked.deferredSpriteUpdate = false;
Jeff Brown5541de92011-04-11 11:54:25 -070043}
44
45SpriteController::~SpriteController() {
46 mLooper->removeMessages(mHandler);
47
48 if (mSurfaceComposerClient != NULL) {
49 mSurfaceComposerClient->dispose();
50 mSurfaceComposerClient.clear();
51 }
52}
53
54sp<Sprite> SpriteController::createSprite() {
55 return new SpriteImpl(this);
56}
57
Jeff Brown2352b972011-04-12 22:39:53 -070058void SpriteController::openTransaction() {
59 AutoMutex _l(mLock);
60
61 mLocked.transactionNestingCount += 1;
62}
63
64void SpriteController::closeTransaction() {
65 AutoMutex _l(mLock);
66
67 LOG_ALWAYS_FATAL_IF(mLocked.transactionNestingCount == 0,
68 "Sprite closeTransaction() called but there is no open sprite transaction");
69
70 mLocked.transactionNestingCount -= 1;
71 if (mLocked.transactionNestingCount == 0 && mLocked.deferredSpriteUpdate) {
72 mLocked.deferredSpriteUpdate = false;
Jeff Brown5541de92011-04-11 11:54:25 -070073 mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES));
74 }
75}
76
Jeff Brown2352b972011-04-12 22:39:53 -070077void SpriteController::invalidateSpriteLocked(const sp<SpriteImpl>& sprite) {
78 bool wasEmpty = mLocked.invalidatedSprites.isEmpty();
79 mLocked.invalidatedSprites.push(sprite);
80 if (wasEmpty) {
81 if (mLocked.transactionNestingCount != 0) {
82 mLocked.deferredSpriteUpdate = true;
83 } else {
84 mLooper->sendMessage(mHandler, Message(MSG_UPDATE_SPRITES));
85 }
86 }
87}
88
Jeff Brown5541de92011-04-11 11:54:25 -070089void SpriteController::disposeSurfaceLocked(const sp<SurfaceControl>& surfaceControl) {
Jeff Brown2352b972011-04-12 22:39:53 -070090 bool wasEmpty = mLocked.disposedSurfaces.isEmpty();
91 mLocked.disposedSurfaces.push(surfaceControl);
Jeff Brown5541de92011-04-11 11:54:25 -070092 if (wasEmpty) {
93 mLooper->sendMessage(mHandler, Message(MSG_DISPOSE_SURFACES));
94 }
95}
96
97void SpriteController::handleMessage(const Message& message) {
98 switch (message.what) {
99 case MSG_UPDATE_SPRITES:
100 doUpdateSprites();
101 break;
102 case MSG_DISPOSE_SURFACES:
103 doDisposeSurfaces();
104 break;
105 }
106}
107
108void SpriteController::doUpdateSprites() {
109 // Collect information about sprite updates.
110 // Each sprite update record includes a reference to its associated sprite so we can
111 // be certain the sprites will not be deleted while this function runs. Sprites
112 // may invalidate themselves again during this time but we will handle those changes
113 // in the next iteration.
114 Vector<SpriteUpdate> updates;
115 size_t numSprites;
116 { // acquire lock
117 AutoMutex _l(mLock);
118
Jeff Brown2352b972011-04-12 22:39:53 -0700119 numSprites = mLocked.invalidatedSprites.size();
Jeff Brown5541de92011-04-11 11:54:25 -0700120 for (size_t i = 0; i < numSprites; i++) {
Jeff Brown2352b972011-04-12 22:39:53 -0700121 const sp<SpriteImpl>& sprite = mLocked.invalidatedSprites.itemAt(i);
Jeff Brown5541de92011-04-11 11:54:25 -0700122
123 updates.push(SpriteUpdate(sprite, sprite->getStateLocked()));
124 sprite->resetDirtyLocked();
125 }
Jeff Brown2352b972011-04-12 22:39:53 -0700126 mLocked.invalidatedSprites.clear();
Jeff Brown5541de92011-04-11 11:54:25 -0700127 } // release lock
128
129 // Create missing surfaces.
130 bool surfaceChanged = false;
131 for (size_t i = 0; i < numSprites; i++) {
132 SpriteUpdate& update = updates.editItemAt(i);
133
134 if (update.state.surfaceControl == NULL && update.state.wantSurfaceVisible()) {
Jeff Brown2352b972011-04-12 22:39:53 -0700135 update.state.surfaceWidth = update.state.icon.bitmap.width();
136 update.state.surfaceHeight = update.state.icon.bitmap.height();
Jeff Brown5541de92011-04-11 11:54:25 -0700137 update.state.surfaceDrawn = false;
138 update.state.surfaceVisible = false;
139 update.state.surfaceControl = obtainSurface(
140 update.state.surfaceWidth, update.state.surfaceHeight);
141 if (update.state.surfaceControl != NULL) {
142 update.surfaceChanged = surfaceChanged = true;
143 }
144 }
145 }
146
Robert Carre13b58e2017-08-31 14:50:44 -0700147 // Resize sprites if needed.
148 SurfaceComposerClient::Transaction t;
149 bool needApplyTransaction = false;
Jeff Brown5541de92011-04-11 11:54:25 -0700150 for (size_t i = 0; i < numSprites; i++) {
151 SpriteUpdate& update = updates.editItemAt(i);
152
153 if (update.state.surfaceControl != NULL && update.state.wantSurfaceVisible()) {
Jeff Brown2352b972011-04-12 22:39:53 -0700154 int32_t desiredWidth = update.state.icon.bitmap.width();
155 int32_t desiredHeight = update.state.icon.bitmap.height();
Jeff Brown5541de92011-04-11 11:54:25 -0700156 if (update.state.surfaceWidth < desiredWidth
157 || update.state.surfaceHeight < desiredHeight) {
Robert Carre13b58e2017-08-31 14:50:44 -0700158 needApplyTransaction = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700159
Robert Carre13b58e2017-08-31 14:50:44 -0700160 t.setSize(update.state.surfaceControl,
161 desiredWidth, desiredHeight);
162 update.state.surfaceWidth = desiredWidth;
163 update.state.surfaceHeight = desiredHeight;
164 update.state.surfaceDrawn = false;
165 update.surfaceChanged = surfaceChanged = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700166
Robert Carre13b58e2017-08-31 14:50:44 -0700167 if (update.state.surfaceVisible) {
168 t.hide(update.state.surfaceControl);
169 update.state.surfaceVisible = false;
Jeff Brown5541de92011-04-11 11:54:25 -0700170 }
171 }
172 }
173 }
Robert Carre13b58e2017-08-31 14:50:44 -0700174 if (needApplyTransaction) {
175 t.apply();
Jeff Brown5541de92011-04-11 11:54:25 -0700176 }
177
178 // Redraw sprites if needed.
179 for (size_t i = 0; i < numSprites; i++) {
180 SpriteUpdate& update = updates.editItemAt(i);
181
182 if ((update.state.dirty & DIRTY_BITMAP) && update.state.surfaceDrawn) {
183 update.state.surfaceDrawn = false;
184 update.surfaceChanged = surfaceChanged = true;
185 }
186
187 if (update.state.surfaceControl != NULL && !update.state.surfaceDrawn
188 && update.state.wantSurfaceVisible()) {
189 sp<Surface> surface = update.state.surfaceControl->getSurface();
Mathias Agopian52800612013-02-14 17:11:20 -0800190 ANativeWindow_Buffer outBuffer;
191 status_t status = surface->lock(&outBuffer, NULL);
Jeff Brown5541de92011-04-11 11:54:25 -0700192 if (status) {
Steve Block3762c312012-01-06 19:20:56 +0000193 ALOGE("Error %d locking sprite surface before drawing.", status);
Jeff Brown5541de92011-04-11 11:54:25 -0700194 } else {
195 SkBitmap surfaceBitmap;
Mathias Agopian52800612013-02-14 17:11:20 -0800196 ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
Mike Reedb9330552014-06-16 17:31:48 -0400197 surfaceBitmap.installPixels(SkImageInfo::MakeN32Premul(outBuffer.width, outBuffer.height),
198 outBuffer.bits, bpr);
Jeff Brown5541de92011-04-11 11:54:25 -0700199
Derek Sollenbergerfc615a02012-12-20 14:39:57 -0500200 SkCanvas surfaceCanvas(surfaceBitmap);
Jeff Brown5541de92011-04-11 11:54:25 -0700201
202 SkPaint paint;
Mike Reed260ab722016-10-07 15:59:20 -0400203 paint.setBlendMode(SkBlendMode::kSrc);
Jeff Brown2352b972011-04-12 22:39:53 -0700204 surfaceCanvas.drawBitmap(update.state.icon.bitmap, 0, 0, &paint);
Jeff Brown5541de92011-04-11 11:54:25 -0700205
Michael Wright4d8779f2013-10-16 14:01:50 -0700206 if (outBuffer.width > update.state.icon.bitmap.width()) {
Jeff Brown5541de92011-04-11 11:54:25 -0700207 paint.setColor(0); // transparent fill color
Mike Reed6c9bb242017-04-04 09:15:37 -0400208 surfaceCanvas.drawRect(SkRect::MakeLTRB(update.state.icon.bitmap.width(), 0,
209 outBuffer.width, update.state.icon.bitmap.height()), paint);
Jeff Brown5541de92011-04-11 11:54:25 -0700210 }
Michael Wright4d8779f2013-10-16 14:01:50 -0700211 if (outBuffer.height > update.state.icon.bitmap.height()) {
Jeff Brown5541de92011-04-11 11:54:25 -0700212 paint.setColor(0); // transparent fill color
Mike Reed6c9bb242017-04-04 09:15:37 -0400213 surfaceCanvas.drawRect(SkRect::MakeLTRB(0, update.state.icon.bitmap.height(),
214 outBuffer.width, outBuffer.height), paint);
Jeff Brown5541de92011-04-11 11:54:25 -0700215 }
216
217 status = surface->unlockAndPost();
218 if (status) {
Steve Block3762c312012-01-06 19:20:56 +0000219 ALOGE("Error %d unlocking and posting sprite surface after drawing.", status);
Jeff Brown5541de92011-04-11 11:54:25 -0700220 } else {
221 update.state.surfaceDrawn = true;
222 update.surfaceChanged = surfaceChanged = true;
223 }
224 }
225 }
226 }
227
Robert Carre13b58e2017-08-31 14:50:44 -0700228 needApplyTransaction = false;
Jeff Brown5541de92011-04-11 11:54:25 -0700229 for (size_t i = 0; i < numSprites; i++) {
230 SpriteUpdate& update = updates.editItemAt(i);
231
232 bool wantSurfaceVisibleAndDrawn = update.state.wantSurfaceVisible()
233 && update.state.surfaceDrawn;
234 bool becomingVisible = wantSurfaceVisibleAndDrawn && !update.state.surfaceVisible;
235 bool becomingHidden = !wantSurfaceVisibleAndDrawn && update.state.surfaceVisible;
236 if (update.state.surfaceControl != NULL && (becomingVisible || becomingHidden
237 || (wantSurfaceVisibleAndDrawn && (update.state.dirty & (DIRTY_ALPHA
238 | DIRTY_POSITION | DIRTY_TRANSFORMATION_MATRIX | DIRTY_LAYER
239 | DIRTY_VISIBILITY | DIRTY_HOTSPOT))))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700240 needApplyTransaction = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700241
242 if (wantSurfaceVisibleAndDrawn
243 && (becomingVisible || (update.state.dirty & DIRTY_ALPHA))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700244 t.setAlpha(update.state.surfaceControl,
245 update.state.alpha);
Jeff Brown5541de92011-04-11 11:54:25 -0700246 }
247
248 if (wantSurfaceVisibleAndDrawn
249 && (becomingVisible || (update.state.dirty & (DIRTY_POSITION
250 | DIRTY_HOTSPOT)))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700251 t.setPosition(
252 update.state.surfaceControl,
Jeff Brown2352b972011-04-12 22:39:53 -0700253 update.state.positionX - update.state.icon.hotSpotX,
254 update.state.positionY - update.state.icon.hotSpotY);
Jeff Brown5541de92011-04-11 11:54:25 -0700255 }
256
257 if (wantSurfaceVisibleAndDrawn
258 && (becomingVisible
259 || (update.state.dirty & DIRTY_TRANSFORMATION_MATRIX))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700260 t.setMatrix(
261 update.state.surfaceControl,
Jeff Brown5541de92011-04-11 11:54:25 -0700262 update.state.transformationMatrix.dsdx,
263 update.state.transformationMatrix.dtdx,
264 update.state.transformationMatrix.dsdy,
265 update.state.transformationMatrix.dtdy);
Jeff Brown5541de92011-04-11 11:54:25 -0700266 }
267
268 int32_t surfaceLayer = mOverlayLayer + update.state.layer;
269 if (wantSurfaceVisibleAndDrawn
270 && (becomingVisible || (update.state.dirty & DIRTY_LAYER))) {
Robert Carre13b58e2017-08-31 14:50:44 -0700271 t.setLayer(update.state.surfaceControl, surfaceLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700272 }
273
274 if (becomingVisible) {
Robert Carre13b58e2017-08-31 14:50:44 -0700275 t.show(update.state.surfaceControl);
276
277 update.state.surfaceVisible = true;
278 update.surfaceChanged = surfaceChanged = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700279 } else if (becomingHidden) {
Robert Carre13b58e2017-08-31 14:50:44 -0700280 t.hide(update.state.surfaceControl);
281
282 update.state.surfaceVisible = false;
283 update.surfaceChanged = surfaceChanged = true;
Jeff Brown5541de92011-04-11 11:54:25 -0700284 }
285 }
286 }
287
Robert Carre13b58e2017-08-31 14:50:44 -0700288 if (needApplyTransaction) {
289 status_t status = t.apply();
290 if (status) {
291 ALOGE("Error applying Surface transaction");
292 }
Jeff Brown5541de92011-04-11 11:54:25 -0700293 }
294
295 // If any surfaces were changed, write back the new surface properties to the sprites.
296 if (surfaceChanged) { // acquire lock
297 AutoMutex _l(mLock);
298
299 for (size_t i = 0; i < numSprites; i++) {
300 const SpriteUpdate& update = updates.itemAt(i);
301
302 if (update.surfaceChanged) {
303 update.sprite->setSurfaceLocked(update.state.surfaceControl,
304 update.state.surfaceWidth, update.state.surfaceHeight,
305 update.state.surfaceDrawn, update.state.surfaceVisible);
306 }
307 }
308 } // release lock
309
310 // Clear the sprite update vector outside the lock. It is very important that
311 // we do not clear sprite references inside the lock since we could be releasing
312 // the last remaining reference to the sprite here which would result in the
313 // sprite being deleted and the lock being reacquired by the sprite destructor
314 // while already held.
315 updates.clear();
316}
317
318void SpriteController::doDisposeSurfaces() {
319 // Collect disposed surfaces.
320 Vector<sp<SurfaceControl> > disposedSurfaces;
321 { // acquire lock
Jeff Brown2352b972011-04-12 22:39:53 -0700322 AutoMutex _l(mLock);
323
324 disposedSurfaces = mLocked.disposedSurfaces;
325 mLocked.disposedSurfaces.clear();
Jeff Brown5541de92011-04-11 11:54:25 -0700326 } // release lock
327
328 // Release the last reference to each surface outside of the lock.
329 // We don't want the surfaces to be deleted while we are holding our lock.
330 disposedSurfaces.clear();
331}
332
333void SpriteController::ensureSurfaceComposerClient() {
334 if (mSurfaceComposerClient == NULL) {
335 mSurfaceComposerClient = new SurfaceComposerClient();
336 }
337}
338
339sp<SurfaceControl> SpriteController::obtainSurface(int32_t width, int32_t height) {
340 ensureSurfaceComposerClient();
341
342 sp<SurfaceControl> surfaceControl = mSurfaceComposerClient->createSurface(
Jeff Brown64a55af2012-08-26 02:47:39 -0700343 String8("Sprite"), width, height, PIXEL_FORMAT_RGBA_8888,
Riley Andrews68eccda2014-07-07 11:47:35 -0700344 ISurfaceComposerClient::eHidden |
345 ISurfaceComposerClient::eCursorWindow);
Mathias Agopian52800612013-02-14 17:11:20 -0800346 if (surfaceControl == NULL || !surfaceControl->isValid()) {
Steve Block3762c312012-01-06 19:20:56 +0000347 ALOGE("Error creating sprite surface.");
Jeff Brown5541de92011-04-11 11:54:25 -0700348 return NULL;
349 }
350 return surfaceControl;
351}
352
353
354// --- SpriteController::SpriteImpl ---
355
356SpriteController::SpriteImpl::SpriteImpl(const sp<SpriteController> controller) :
Jeff Brown2352b972011-04-12 22:39:53 -0700357 mController(controller) {
Jeff Brown5541de92011-04-11 11:54:25 -0700358}
359
360SpriteController::SpriteImpl::~SpriteImpl() {
361 AutoMutex _m(mController->mLock);
362
363 // Let the controller take care of deleting the last reference to sprite
364 // surfaces so that we do not block the caller on an IPC here.
Jeff Brown2352b972011-04-12 22:39:53 -0700365 if (mLocked.state.surfaceControl != NULL) {
366 mController->disposeSurfaceLocked(mLocked.state.surfaceControl);
367 mLocked.state.surfaceControl.clear();
Jeff Brown5541de92011-04-11 11:54:25 -0700368 }
369}
370
Jeff Brown2352b972011-04-12 22:39:53 -0700371void SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon) {
Jeff Brown5541de92011-04-11 11:54:25 -0700372 AutoMutex _l(mController->mLock);
373
Jeff Brown2352b972011-04-12 22:39:53 -0700374 uint32_t dirty;
375 if (icon.isValid()) {
Matt Sarett155d5212017-04-25 17:32:34 -0400376 SkBitmap* bitmapCopy = &mLocked.state.icon.bitmap;
377 if (bitmapCopy->tryAllocPixels(icon.bitmap.info().makeColorType(kN32_SkColorType))) {
378 icon.bitmap.readPixels(bitmapCopy->info(), bitmapCopy->getPixels(),
379 bitmapCopy->rowBytes(), 0, 0);
380 }
Jeff Brown5541de92011-04-11 11:54:25 -0700381
Jeff Brown2352b972011-04-12 22:39:53 -0700382 if (!mLocked.state.icon.isValid()
383 || mLocked.state.icon.hotSpotX != icon.hotSpotX
384 || mLocked.state.icon.hotSpotY != icon.hotSpotY) {
385 mLocked.state.icon.hotSpotX = icon.hotSpotX;
386 mLocked.state.icon.hotSpotY = icon.hotSpotY;
387 dirty = DIRTY_BITMAP | DIRTY_HOTSPOT;
388 } else {
389 dirty = DIRTY_BITMAP;
390 }
391 } else if (mLocked.state.icon.isValid()) {
392 mLocked.state.icon.bitmap.reset();
393 dirty = DIRTY_BITMAP | DIRTY_HOTSPOT;
394 } else {
395 return; // setting to invalid icon and already invalid so nothing to do
Jeff Brown5541de92011-04-11 11:54:25 -0700396 }
397
398 invalidateLocked(dirty);
399}
400
401void SpriteController::SpriteImpl::setVisible(bool visible) {
402 AutoMutex _l(mController->mLock);
403
Jeff Brown2352b972011-04-12 22:39:53 -0700404 if (mLocked.state.visible != visible) {
405 mLocked.state.visible = visible;
Jeff Brown5541de92011-04-11 11:54:25 -0700406 invalidateLocked(DIRTY_VISIBILITY);
407 }
408}
409
410void SpriteController::SpriteImpl::setPosition(float x, float y) {
411 AutoMutex _l(mController->mLock);
412
Jeff Brown2352b972011-04-12 22:39:53 -0700413 if (mLocked.state.positionX != x || mLocked.state.positionY != y) {
414 mLocked.state.positionX = x;
415 mLocked.state.positionY = y;
Jeff Brown5541de92011-04-11 11:54:25 -0700416 invalidateLocked(DIRTY_POSITION);
417 }
418}
419
420void SpriteController::SpriteImpl::setLayer(int32_t layer) {
421 AutoMutex _l(mController->mLock);
422
Jeff Brown2352b972011-04-12 22:39:53 -0700423 if (mLocked.state.layer != layer) {
424 mLocked.state.layer = layer;
Jeff Brown5541de92011-04-11 11:54:25 -0700425 invalidateLocked(DIRTY_LAYER);
426 }
427}
428
429void SpriteController::SpriteImpl::setAlpha(float alpha) {
430 AutoMutex _l(mController->mLock);
431
Jeff Brown2352b972011-04-12 22:39:53 -0700432 if (mLocked.state.alpha != alpha) {
433 mLocked.state.alpha = alpha;
Jeff Brown5541de92011-04-11 11:54:25 -0700434 invalidateLocked(DIRTY_ALPHA);
435 }
436}
437
438void SpriteController::SpriteImpl::setTransformationMatrix(
439 const SpriteTransformationMatrix& matrix) {
440 AutoMutex _l(mController->mLock);
441
Jeff Brown2352b972011-04-12 22:39:53 -0700442 if (mLocked.state.transformationMatrix != matrix) {
443 mLocked.state.transformationMatrix = matrix;
Jeff Brown5541de92011-04-11 11:54:25 -0700444 invalidateLocked(DIRTY_TRANSFORMATION_MATRIX);
445 }
446}
447
Jeff Brown5541de92011-04-11 11:54:25 -0700448void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) {
Jeff Brown2352b972011-04-12 22:39:53 -0700449 bool wasDirty = mLocked.state.dirty;
450 mLocked.state.dirty |= dirty;
451
452 if (!wasDirty) {
453 mController->invalidateSpriteLocked(this);
Jeff Brown5541de92011-04-11 11:54:25 -0700454 }
455}
456
457} // namespace android