| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef _UI_SPRITES_H |
| 18 |
#define _UI_SPRITES_H |
| 19 |
|
| 20 |
#include <utils/RefBase.h> |
| 21 |
#include <utils/Looper.h> |
| 22 |
|
| 23 |
#include <gui/SurfaceComposerClient.h> |
| 24 |
|
| 25 |
#include "SpriteIcon.h" |
| 26 |
|
| 27 |
namespace android { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
struct SpriteTransformationMatrix { |
| 33 |
inline SpriteTransformationMatrix() : dsdx(1.0f), dtdx(0.0f), dsdy(0.0f), dtdy(1.0f) { } |
| 34 |
inline SpriteTransformationMatrix(float dsdx, float dtdx, float dsdy, float dtdy) : |
| 35 |
dsdx(dsdx), dtdx(dtdx), dsdy(dsdy), dtdy(dtdy) { } |
| 36 |
|
| 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 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
class Sprite : public RefBase { |
| 60 |
protected: |
| 61 |
Sprite() { } |
| 62 |
virtual ~Sprite() { } |
| 63 |
|
| 64 |
public: |
| 65 |
enum { |
| 66 |
|
| 67 |
BASE_LAYER_POINTER = 0, |
| 68 |
|
| 69 |
|
| 70 |
BASE_LAYER_SPOT = 1, |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
virtual void setIcon(const SpriteIcon& icon) = 0; |
| 76 |
|
| 77 |
inline void clearIcon() { |
| 78 |
setIcon(SpriteIcon()); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
virtual void setVisible(bool visible) = 0; |
| 83 |
|
| 84 |
|
| 85 |
virtual void setPosition(float x, float y) = 0; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
virtual void setLayer(int32_t layer) = 0; |
| 90 |
|
| 91 |
|
| 92 |
virtual void setAlpha(float alpha) = 0; |
| 93 |
|
| 94 |
|
| 95 |
virtual void setTransformationMatrix(const SpriteTransformationMatrix& matrix) = 0; |
| 96 |
|
| 97 |
|
| 98 |
virtual void setDisplayId(int32_t displayId) = 0; |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
class SpriteController : public MessageHandler { |
| 113 |
protected: |
| 114 |
virtual ~SpriteController(); |
| 115 |
|
| 116 |
public: |
| 117 |
using ParentSurfaceProvider = std::function<sp<SurfaceControl>(int )>; |
| 118 |
SpriteController(const sp<Looper>& looper, int32_t overlayLayer, ParentSurfaceProvider parent); |
| 119 |
|
| 120 |
|
| 121 |
virtual sp<Sprite> createSprite(); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
virtual void openTransaction(); |
| 129 |
virtual void closeTransaction(); |
| 130 |
|
| 131 |
private: |
| 132 |
enum { |
| 133 |
MSG_UPDATE_SPRITES, |
| 134 |
MSG_DISPOSE_SURFACES, |
| 135 |
}; |
| 136 |
|
| 137 |
enum { |
| 138 |
DIRTY_BITMAP = 1 << 0, |
| 139 |
DIRTY_ALPHA = 1 << 1, |
| 140 |
DIRTY_POSITION = 1 << 2, |
| 141 |
DIRTY_TRANSFORMATION_MATRIX = 1 << 3, |
| 142 |
DIRTY_LAYER = 1 << 4, |
| 143 |
DIRTY_VISIBILITY = 1 << 5, |
| 144 |
DIRTY_HOTSPOT = 1 << 6, |
| 145 |
DIRTY_DISPLAY_ID = 1 << 7, |
| 146 |
DIRTY_ICON_STYLE = 1 << 8, |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
struct SpriteState { |
| 155 |
inline SpriteState() : |
| 156 |
dirty(0), visible(false), |
| 157 |
positionX(0), positionY(0), layer(0), alpha(1.0f), displayId(ADISPLAY_ID_DEFAULT), |
| 158 |
surfaceWidth(0), surfaceHeight(0), surfaceDrawn(false), surfaceVisible(false) { |
| 159 |
} |
| 160 |
|
| 161 |
uint32_t dirty; |
| 162 |
|
| 163 |
SpriteIcon icon; |
| 164 |
bool visible; |
| 165 |
float positionX; |
| 166 |
float positionY; |
| 167 |
int32_t layer; |
| 168 |
float alpha; |
| 169 |
SpriteTransformationMatrix transformationMatrix; |
| 170 |
int32_t displayId; |
| 171 |
|
| 172 |
sp<SurfaceControl> surfaceControl; |
| 173 |
int32_t surfaceWidth; |
| 174 |
int32_t surfaceHeight; |
| 175 |
bool surfaceDrawn; |
| 176 |
bool surfaceVisible; |
| 177 |
|
| 178 |
inline bool wantSurfaceVisible() const { |
| 179 |
return visible && alpha > 0.0f && icon.isValid(); |
| 180 |
} |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
class SpriteImpl : public Sprite { |
| 191 |
protected: |
| 192 |
virtual ~SpriteImpl(); |
| 193 |
|
| 194 |
public: |
| 195 |
explicit SpriteImpl(const sp<SpriteController> controller); |
| 196 |
|
| 197 |
virtual void setIcon(const SpriteIcon& icon); |
| 198 |
virtual void setVisible(bool visible); |
| 199 |
virtual void setPosition(float x, float y); |
| 200 |
virtual void setLayer(int32_t layer); |
| 201 |
virtual void setAlpha(float alpha); |
| 202 |
virtual void setTransformationMatrix(const SpriteTransformationMatrix& matrix); |
| 203 |
virtual void setDisplayId(int32_t displayId); |
| 204 |
|
| 205 |
inline const SpriteState& getStateLocked() const { |
| 206 |
return mLocked.state; |
| 207 |
} |
| 208 |
|
| 209 |
inline void resetDirtyLocked() { |
| 210 |
mLocked.state.dirty = 0; |
| 211 |
} |
| 212 |
|
| 213 |
inline void setSurfaceLocked(const sp<SurfaceControl>& surfaceControl, |
| 214 |
int32_t width, int32_t height, bool drawn, bool visible) { |
| 215 |
mLocked.state.surfaceControl = surfaceControl; |
| 216 |
mLocked.state.surfaceWidth = width; |
| 217 |
mLocked.state.surfaceHeight = height; |
| 218 |
mLocked.state.surfaceDrawn = drawn; |
| 219 |
mLocked.state.surfaceVisible = visible; |
| 220 |
} |
| 221 |
|
| 222 |
private: |
| 223 |
sp<SpriteController> mController; |
| 224 |
|
| 225 |
struct Locked { |
| 226 |
SpriteState state; |
| 227 |
} mLocked; |
| 228 |
|
| 229 |
void invalidateLocked(uint32_t dirty); |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
struct SpriteUpdate { |
| 234 |
inline SpriteUpdate() : surfaceChanged(false) { } |
| 235 |
inline SpriteUpdate(const sp<SpriteImpl> sprite, const SpriteState& state) : |
| 236 |
sprite(sprite), state(state), surfaceChanged(false) { |
| 237 |
} |
| 238 |
|
| 239 |
sp<SpriteImpl> sprite; |
| 240 |
SpriteState state; |
| 241 |
bool surfaceChanged; |
| 242 |
}; |
| 243 |
|
| 244 |
mutable Mutex mLock; |
| 245 |
|
| 246 |
sp<Looper> mLooper; |
| 247 |
const int32_t mOverlayLayer; |
| 248 |
sp<WeakMessageHandler> mHandler; |
| 249 |
ParentSurfaceProvider mParentSurfaceProvider; |
| 250 |
|
| 251 |
sp<SurfaceComposerClient> mSurfaceComposerClient; |
| 252 |
|
| 253 |
struct Locked { |
| 254 |
std::vector<sp<SpriteImpl>> invalidatedSprites; |
| 255 |
std::vector<sp<SurfaceControl>> disposedSurfaces; |
| 256 |
uint32_t transactionNestingCount; |
| 257 |
bool deferredSpriteUpdate; |
| 258 |
} mLocked; |
| 259 |
|
| 260 |
void invalidateSpriteLocked(const sp<SpriteImpl>& sprite); |
| 261 |
void disposeSurfaceLocked(const sp<SurfaceControl>& surfaceControl); |
| 262 |
|
| 263 |
void handleMessage(const Message& message); |
| 264 |
void doUpdateSprites(); |
| 265 |
void doDisposeSurfaces(); |
| 266 |
|
| 267 |
void ensureSurfaceComposerClient(); |
| 268 |
sp<SurfaceControl> obtainSurface(int32_t width, int32_t height); |
| 269 |
}; |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
#endif |