blob: 35776e9961b3d71c99c910fb2721308ebbf0ab5f [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#ifndef _UI_SPRITES_H
18#define _UI_SPRITES_H
19
20#include <utils/RefBase.h>
21#include <utils/Looper.h>
22
Mathias Agopian8335f1c2012-02-25 18:48:35 -080023#include <gui/SurfaceComposerClient.h>
Jeff Brown5541de92011-04-11 11:54:25 -070024
Garfield Tan7e3457e2020-05-28 14:31:29 -070025#include "SpriteIcon.h"
26
Jeff Brown5541de92011-04-11 11:54:25 -070027namespace android {
28
29/*
30 * Transformation matrix for a sprite.
31 */
32struct SpriteTransformationMatrix {
33 inline SpriteTransformationMatrix() : dsdx(1.0f), dtdx(0.0f), dsdy(0.0f), dtdy(1.0f) { }
Jeff Brown2352b972011-04-12 22:39:53 -070034 inline SpriteTransformationMatrix(float dsdx, float dtdx, float dsdy, float dtdy) :
35 dsdx(dsdx), dtdx(dtdx), dsdy(dsdy), dtdy(dtdy) { }
Jeff Brown5541de92011-04-11 11:54:25 -070036
37 float dsdx;
38 float dtdx;
39 float dsdy;
40 float dtdy;
41
42 inline bool operator== (const SpriteTransformationMatrix& other) {
43 return dsdx == other.dsdx
44 && dtdx == other.dtdx
45 && dsdy == other.dsdy
46 && dtdy == other.dtdy;
47 }
48
49 inline bool operator!= (const SpriteTransformationMatrix& other) {
50 return !(*this == other);
51 }
52};
53
54/*
55 * A sprite is a simple graphical object that is displayed on-screen above other layers.
56 * The basic sprite class is an interface.
57 * The implementation is provided by the sprite controller.
58 */
59class Sprite : public RefBase {
60protected:
61 Sprite() { }
62 virtual ~Sprite() { }
63
64public:
Jeff Brown2352b972011-04-12 22:39:53 -070065 enum {
66 // The base layer for pointer sprites.
67 BASE_LAYER_POINTER = 0, // reserve space for 1 pointer
68
69 // The base layer for spot sprites.
70 BASE_LAYER_SPOT = 1, // reserve space for MAX_POINTER_ID spots
71 };
72
Jeff Brown5541de92011-04-11 11:54:25 -070073 /* Sets the bitmap that is drawn by the sprite.
74 * The sprite retains a copy of the bitmap for subsequent rendering. */
Jeff Brown2352b972011-04-12 22:39:53 -070075 virtual void setIcon(const SpriteIcon& icon) = 0;
76
77 inline void clearIcon() {
78 setIcon(SpriteIcon());
79 }
Jeff Brown5541de92011-04-11 11:54:25 -070080
81 /* Sets whether the sprite is visible. */
82 virtual void setVisible(bool visible) = 0;
83
84 /* Sets the sprite position on screen, relative to the sprite's hot spot. */
85 virtual void setPosition(float x, float y) = 0;
86
87 /* Sets the layer of the sprite, relative to the system sprite overlay layer.
88 * Layer 0 is the overlay layer, > 0 appear above this layer. */
89 virtual void setLayer(int32_t layer) = 0;
90
91 /* Sets the sprite alpha blend ratio between 0.0 and 1.0. */
92 virtual void setAlpha(float alpha) = 0;
93
94 /* Sets the sprite transformation matrix. */
95 virtual void setTransformationMatrix(const SpriteTransformationMatrix& matrix) = 0;
Arthur Hungb9b32002018-12-18 17:39:43 +080096
97 /* Sets the id of the display where the sprite should be shown. */
98 virtual void setDisplayId(int32_t displayId) = 0;
Jeff Brown5541de92011-04-11 11:54:25 -070099};
100
101/*
102 * Displays sprites on the screen.
103 *
104 * This interface is used by PointerController and SpotController to draw pointers or
105 * spot representations of fingers. It is not intended for general purpose use
106 * by other components.
107 *
108 * All sprite position updates and rendering is performed asynchronously.
109 *
110 * Clients are responsible for animating sprites by periodically updating their properties.
111 */
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000112class SpriteController {
Jeff Brown5541de92011-04-11 11:54:25 -0700113public:
Prabir Pradhanfef0c612021-11-04 14:08:50 -0700114 using ParentSurfaceProvider = std::function<sp<SurfaceControl>(int /*displayId*/)>;
115 SpriteController(const sp<Looper>& looper, int32_t overlayLayer, ParentSurfaceProvider parent);
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000116 SpriteController(const SpriteController&) = delete;
117 SpriteController& operator=(const SpriteController&) = delete;
118 virtual ~SpriteController();
Jeff Brown5541de92011-04-11 11:54:25 -0700119
Prabir Pradhan4cc1a632023-06-09 21:31:26 +0000120 /* Initialize the callback for the message handler. */
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000121 void setHandlerController(const std::shared_ptr<SpriteController>& controller);
Prabir Pradhan4cc1a632023-06-09 21:31:26 +0000122
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000123 /* Creates a new sprite, initially invisible. The lifecycle of the sprite must not extend beyond
124 * the lifecycle of this SpriteController. */
Garfield Tan67e479a2019-08-05 16:47:40 -0700125 virtual sp<Sprite> createSprite();
Jeff Brown5541de92011-04-11 11:54:25 -0700126
Jeff Brown2352b972011-04-12 22:39:53 -0700127 /* Opens or closes a transaction to perform a batch of sprite updates as part of
128 * a single operation such as setPosition and setAlpha. It is not necessary to
129 * open a transaction when updating a single property.
130 * Calls to openTransaction() nest and must be matched by an equal number
131 * of calls to closeTransaction(). */
Garfield Tan67e479a2019-08-05 16:47:40 -0700132 virtual void openTransaction();
133 virtual void closeTransaction();
Jeff Brown2352b972011-04-12 22:39:53 -0700134
Jeff Brown5541de92011-04-11 11:54:25 -0700135private:
Prabir Pradhan4cc1a632023-06-09 21:31:26 +0000136 class Handler : public virtual android::MessageHandler {
137 public:
138 enum { MSG_UPDATE_SPRITES, MSG_DISPOSE_SURFACES };
139
140 void handleMessage(const Message& message) override;
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000141 std::weak_ptr<SpriteController> spriteController;
Jeff Brown5541de92011-04-11 11:54:25 -0700142 };
143
144 enum {
145 DIRTY_BITMAP = 1 << 0,
146 DIRTY_ALPHA = 1 << 1,
147 DIRTY_POSITION = 1 << 2,
148 DIRTY_TRANSFORMATION_MATRIX = 1 << 3,
149 DIRTY_LAYER = 1 << 4,
150 DIRTY_VISIBILITY = 1 << 5,
151 DIRTY_HOTSPOT = 1 << 6,
Arthur Hungb9b32002018-12-18 17:39:43 +0800152 DIRTY_DISPLAY_ID = 1 << 7,
Garfield Tan67e479a2019-08-05 16:47:40 -0700153 DIRTY_ICON_STYLE = 1 << 8,
Pat Manningacbe18d2024-03-05 17:54:30 +0000154 DIRTY_DRAW_DROP_SHADOW = 1 << 9,
Jeff Brown5541de92011-04-11 11:54:25 -0700155 };
156
157 /* Describes the state of a sprite.
158 * This structure is designed so that it can be copied during updates so that
159 * surfaces can be resized and redrawn without blocking the client by holding a lock
160 * on the sprites for a long time.
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400161 * Note that the SpriteIcon holds a reference to a shared (and immutable) bitmap. */
Jeff Brown5541de92011-04-11 11:54:25 -0700162 struct SpriteState {
163 inline SpriteState() :
Jeff Brown2352b972011-04-12 22:39:53 -0700164 dirty(0), visible(false),
Arthur Hungb9b32002018-12-18 17:39:43 +0800165 positionX(0), positionY(0), layer(0), alpha(1.0f), displayId(ADISPLAY_ID_DEFAULT),
Jeff Brown5541de92011-04-11 11:54:25 -0700166 surfaceWidth(0), surfaceHeight(0), surfaceDrawn(false), surfaceVisible(false) {
167 }
168
169 uint32_t dirty;
170
Jeff Brown2352b972011-04-12 22:39:53 -0700171 SpriteIcon icon;
Jeff Brown5541de92011-04-11 11:54:25 -0700172 bool visible;
173 float positionX;
174 float positionY;
175 int32_t layer;
176 float alpha;
177 SpriteTransformationMatrix transformationMatrix;
Arthur Hungb9b32002018-12-18 17:39:43 +0800178 int32_t displayId;
Jeff Brown5541de92011-04-11 11:54:25 -0700179
180 sp<SurfaceControl> surfaceControl;
181 int32_t surfaceWidth;
182 int32_t surfaceHeight;
183 bool surfaceDrawn;
184 bool surfaceVisible;
185
186 inline bool wantSurfaceVisible() const {
Jeff Brown2352b972011-04-12 22:39:53 -0700187 return visible && alpha > 0.0f && icon.isValid();
Jeff Brown5541de92011-04-11 11:54:25 -0700188 }
189 };
190
191 /* Client interface for a sprite.
192 * Requests acquire a lock on the controller, update local state and request the
193 * controller to invalidate the sprite.
194 * The real heavy lifting of creating, resizing and redrawing surfaces happens
195 * asynchronously with no locks held except in short critical section to copy
196 * the sprite state before the work and update the sprite surface control afterwards.
197 */
198 class SpriteImpl : public Sprite {
199 protected:
200 virtual ~SpriteImpl();
201
202 public:
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000203 explicit SpriteImpl(SpriteController& controller);
Jeff Brown5541de92011-04-11 11:54:25 -0700204
Jeff Brown2352b972011-04-12 22:39:53 -0700205 virtual void setIcon(const SpriteIcon& icon);
Jeff Brown5541de92011-04-11 11:54:25 -0700206 virtual void setVisible(bool visible);
207 virtual void setPosition(float x, float y);
208 virtual void setLayer(int32_t layer);
209 virtual void setAlpha(float alpha);
210 virtual void setTransformationMatrix(const SpriteTransformationMatrix& matrix);
Arthur Hungb9b32002018-12-18 17:39:43 +0800211 virtual void setDisplayId(int32_t displayId);
Jeff Brown5541de92011-04-11 11:54:25 -0700212
213 inline const SpriteState& getStateLocked() const {
Jeff Brown2352b972011-04-12 22:39:53 -0700214 return mLocked.state;
Jeff Brown5541de92011-04-11 11:54:25 -0700215 }
216
217 inline void resetDirtyLocked() {
Jeff Brown2352b972011-04-12 22:39:53 -0700218 mLocked.state.dirty = 0;
Jeff Brown5541de92011-04-11 11:54:25 -0700219 }
220
221 inline void setSurfaceLocked(const sp<SurfaceControl>& surfaceControl,
222 int32_t width, int32_t height, bool drawn, bool visible) {
Jeff Brown2352b972011-04-12 22:39:53 -0700223 mLocked.state.surfaceControl = surfaceControl;
224 mLocked.state.surfaceWidth = width;
225 mLocked.state.surfaceHeight = height;
226 mLocked.state.surfaceDrawn = drawn;
227 mLocked.state.surfaceVisible = visible;
Jeff Brown5541de92011-04-11 11:54:25 -0700228 }
229
230 private:
Prabir Pradhan27c6d992023-08-18 19:44:55 +0000231 SpriteController& mController;
Jeff Brown5541de92011-04-11 11:54:25 -0700232
Jeff Brown2352b972011-04-12 22:39:53 -0700233 struct Locked {
234 SpriteState state;
235 } mLocked; // guarded by mController->mLock
Jeff Brown5541de92011-04-11 11:54:25 -0700236
237 void invalidateLocked(uint32_t dirty);
238 };
239
240 /* Stores temporary information collected during the sprite update cycle. */
241 struct SpriteUpdate {
242 inline SpriteUpdate() : surfaceChanged(false) { }
243 inline SpriteUpdate(const sp<SpriteImpl> sprite, const SpriteState& state) :
244 sprite(sprite), state(state), surfaceChanged(false) {
245 }
246
247 sp<SpriteImpl> sprite;
248 SpriteState state;
249 bool surfaceChanged;
250 };
251
252 mutable Mutex mLock;
253
254 sp<Looper> mLooper;
255 const int32_t mOverlayLayer;
Prabir Pradhan4cc1a632023-06-09 21:31:26 +0000256 sp<Handler> mHandler;
Prabir Pradhanfef0c612021-11-04 14:08:50 -0700257 ParentSurfaceProvider mParentSurfaceProvider;
Jeff Brown5541de92011-04-11 11:54:25 -0700258
259 sp<SurfaceComposerClient> mSurfaceComposerClient;
260
Jeff Brown2352b972011-04-12 22:39:53 -0700261 struct Locked {
Prabir Pradhan3f8b2892021-11-18 08:55:02 -0800262 std::vector<sp<SpriteImpl>> invalidatedSprites;
263 std::vector<sp<SurfaceControl>> disposedSurfaces;
Jeff Brown2352b972011-04-12 22:39:53 -0700264 uint32_t transactionNestingCount;
265 bool deferredSpriteUpdate;
266 } mLocked; // guarded by mLock
Jeff Brown5541de92011-04-11 11:54:25 -0700267
268 void invalidateSpriteLocked(const sp<SpriteImpl>& sprite);
269 void disposeSurfaceLocked(const sp<SurfaceControl>& surfaceControl);
270
Jeff Brown5541de92011-04-11 11:54:25 -0700271 void doUpdateSprites();
272 void doDisposeSurfaces();
273
274 void ensureSurfaceComposerClient();
Arthur Hung2b5cd172022-05-30 12:54:27 +0000275 sp<SurfaceControl> obtainSurface(int32_t width, int32_t height, int32_t displayId);
Jeff Brown5541de92011-04-11 11:54:25 -0700276};
277
278} // namespace android
279
280#endif // _UI_SPRITES_H