blob: ebc58eeb03c2e7c7d17417987cec4012ed8709ae [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 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 "PointerController"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages about pointer updates
22#define DEBUG_POINTER_UPDATES 0
23
24#include "PointerController.h"
25
26#include <cutils/log.h>
27
28#include <SkBitmap.h>
29#include <SkCanvas.h>
30#include <SkColor.h>
31#include <SkPaint.h>
32#include <SkXfermode.h>
33
34namespace android {
35
36// --- PointerController ---
37
38PointerController::PointerController(int32_t pointerLayer) :
39 mPointerLayer(pointerLayer) {
40 AutoMutex _l(mLock);
41
42 mLocked.displayWidth = -1;
43 mLocked.displayHeight = -1;
44 mLocked.displayOrientation = DISPLAY_ORIENTATION_0;
45
46 mLocked.pointerX = 0;
47 mLocked.pointerY = 0;
48 mLocked.buttonState = 0;
49
50 mLocked.iconBitmap = NULL;
51 mLocked.iconHotSpotX = 0;
52 mLocked.iconHotSpotY = 0;
53
54 mLocked.wantVisible = false;
55 mLocked.visible = false;
56 mLocked.drawn = false;
57}
58
59PointerController::~PointerController() {
60 if (mSurfaceControl != NULL) {
61 mSurfaceControl->clear();
62 mSurfaceControl.clear();
63 }
64
65 if (mSurfaceComposerClient != NULL) {
66 mSurfaceComposerClient->dispose();
67 mSurfaceComposerClient.clear();
68 }
69
70 delete mLocked.iconBitmap;
71}
72
73bool PointerController::getBounds(float* outMinX, float* outMinY,
74 float* outMaxX, float* outMaxY) const {
75 AutoMutex _l(mLock);
76
77 return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY);
78}
79
80bool PointerController::getBoundsLocked(float* outMinX, float* outMinY,
81 float* outMaxX, float* outMaxY) const {
82 if (mLocked.displayWidth <= 0 || mLocked.displayHeight <= 0) {
83 return false;
84 }
85
86 *outMinX = 0;
87 *outMinY = 0;
88 switch (mLocked.displayOrientation) {
89 case DISPLAY_ORIENTATION_90:
90 case DISPLAY_ORIENTATION_270:
91 *outMaxX = mLocked.displayHeight;
92 *outMaxY = mLocked.displayWidth;
93 break;
94 default:
95 *outMaxX = mLocked.displayWidth;
96 *outMaxY = mLocked.displayHeight;
97 break;
98 }
99 return true;
100}
101
102void PointerController::move(float deltaX, float deltaY) {
103#if DEBUG_POINTER_UPDATES
104 LOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY);
105#endif
106 if (deltaX == 0.0f && deltaY == 0.0f) {
107 return;
108 }
109
110 AutoMutex _l(mLock);
111
112 setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY);
113}
114
115void PointerController::setButtonState(uint32_t buttonState) {
116#if DEBUG_POINTER_UPDATES
117 LOGD("Set button state 0x%08x", buttonState);
118#endif
119 AutoMutex _l(mLock);
120
121 if (mLocked.buttonState != buttonState) {
122 mLocked.buttonState = buttonState;
123 mLocked.wantVisible = true;
124 updateLocked();
125 }
126}
127
128uint32_t PointerController::getButtonState() const {
129 AutoMutex _l(mLock);
130
131 return mLocked.buttonState;
132}
133
134void PointerController::setPosition(float x, float y) {
135#if DEBUG_POINTER_UPDATES
136 LOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y);
137#endif
138 AutoMutex _l(mLock);
139
140 setPositionLocked(x, y);
141}
142
143void PointerController::setPositionLocked(float x, float y) {
144 float minX, minY, maxX, maxY;
145 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
146 if (x <= minX) {
147 mLocked.pointerX = minX;
148 } else if (x >= maxX) {
149 mLocked.pointerX = maxX;
150 } else {
151 mLocked.pointerX = x;
152 }
153 if (y <= minY) {
154 mLocked.pointerY = minY;
155 } else if (y >= maxY) {
156 mLocked.pointerY = maxY;
157 } else {
158 mLocked.pointerY = y;
159 }
160 mLocked.wantVisible = true;
161 updateLocked();
162 }
163}
164
165void PointerController::getPosition(float* outX, float* outY) const {
166 AutoMutex _l(mLock);
167
168 *outX = mLocked.pointerX;
169 *outY = mLocked.pointerY;
170}
171
172void PointerController::updateLocked() {
173 bool wantVisibleAndHavePointerIcon = mLocked.wantVisible && mLocked.iconBitmap;
174
175 if (wantVisibleAndHavePointerIcon) {
176 // Want the pointer to be visible.
177 // Ensure the surface is created and drawn.
178 if (!createSurfaceIfNeededLocked() || !drawPointerIfNeededLocked()) {
179 return;
180 }
181 } else {
182 // Don't want the pointer to be visible.
183 // If it is not visible then we are done.
184 if (mSurfaceControl == NULL || !mLocked.visible) {
185 return;
186 }
187 }
188
189 status_t status = mSurfaceComposerClient->openTransaction();
190 if (status) {
191 LOGE("Error opening surface transaction to update pointer surface.");
192 return;
193 }
194
195 if (wantVisibleAndHavePointerIcon) {
196 status = mSurfaceControl->setPosition(
197 mLocked.pointerX - mLocked.iconHotSpotX,
198 mLocked.pointerY - mLocked.iconHotSpotY);
199 if (status) {
200 LOGE("Error %d moving pointer surface.", status);
201 goto CloseTransaction;
202 }
203
204 if (!mLocked.visible) {
205 status = mSurfaceControl->setLayer(mPointerLayer);
206 if (status) {
207 LOGE("Error %d setting pointer surface layer.", status);
208 goto CloseTransaction;
209 }
210
211 status = mSurfaceControl->show(mPointerLayer);
212 if (status) {
213 LOGE("Error %d showing pointer surface.", status);
214 goto CloseTransaction;
215 }
216
217 mLocked.visible = true;
218 }
219 } else {
220 if (mLocked.visible) {
221 status = mSurfaceControl->hide();
222 if (status) {
223 LOGE("Error %d hiding pointer surface.", status);
224 goto CloseTransaction;
225 }
226
227 mLocked.visible = false;
228 }
229 }
230
231CloseTransaction:
232 status = mSurfaceComposerClient->closeTransaction();
233 if (status) {
234 LOGE("Error closing surface transaction to update pointer surface.");
235 }
236}
237
238void PointerController::setDisplaySize(int32_t width, int32_t height) {
239 AutoMutex _l(mLock);
240
241 if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
242 mLocked.displayWidth = width;
243 mLocked.displayHeight = height;
244
245 float minX, minY, maxX, maxY;
246 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
247 mLocked.pointerX = (minX + maxX) * 0.5f;
248 mLocked.pointerY = (minY + maxY) * 0.5f;
249 } else {
250 mLocked.pointerX = 0;
251 mLocked.pointerY = 0;
252 }
253
254 updateLocked();
255 }
256}
257
258void PointerController::setDisplayOrientation(int32_t orientation) {
259 AutoMutex _l(mLock);
260
261 if (mLocked.displayOrientation != orientation) {
262 float absoluteX, absoluteY;
263
264 // Map from oriented display coordinates to absolute display coordinates.
265 switch (mLocked.displayOrientation) {
266 case DISPLAY_ORIENTATION_90:
267 absoluteX = mLocked.displayWidth - mLocked.pointerY;
268 absoluteY = mLocked.pointerX;
269 break;
270 case DISPLAY_ORIENTATION_180:
271 absoluteX = mLocked.displayWidth - mLocked.pointerX;
272 absoluteY = mLocked.displayHeight - mLocked.pointerY;
273 break;
274 case DISPLAY_ORIENTATION_270:
275 absoluteX = mLocked.pointerY;
276 absoluteY = mLocked.displayHeight - mLocked.pointerX;
277 break;
278 default:
279 absoluteX = mLocked.pointerX;
280 absoluteY = mLocked.pointerY;
281 break;
282 }
283
284 // Map from absolute display coordinates to oriented display coordinates.
285 switch (orientation) {
286 case DISPLAY_ORIENTATION_90:
287 mLocked.pointerX = absoluteY;
288 mLocked.pointerY = mLocked.displayWidth - absoluteX;
289 break;
290 case DISPLAY_ORIENTATION_180:
291 mLocked.pointerX = mLocked.displayWidth - absoluteX;
292 mLocked.pointerY = mLocked.displayHeight - absoluteY;
293 break;
294 case DISPLAY_ORIENTATION_270:
295 mLocked.pointerX = mLocked.displayHeight - absoluteY;
296 mLocked.pointerY = absoluteX;
297 break;
298 default:
299 mLocked.pointerX = absoluteX;
300 mLocked.pointerY = absoluteY;
301 break;
302 }
303
304 mLocked.displayOrientation = orientation;
305
306 updateLocked();
307 }
308}
309
310void PointerController::setPointerIcon(const SkBitmap* bitmap, float hotSpotX, float hotSpotY) {
311 AutoMutex _l(mLock);
312
313 delete mLocked.iconBitmap;
314 mLocked.iconBitmap = bitmap ? new SkBitmap(*bitmap) : NULL;
315 mLocked.iconHotSpotX = hotSpotX;
316 mLocked.iconHotSpotY = hotSpotY;
317 mLocked.drawn = false;
318}
319
320bool PointerController::createSurfaceIfNeededLocked() {
321 if (!mLocked.iconBitmap) {
322 // If we don't have a pointer icon, then no point allocating a surface now.
323 return false;
324 }
325
326 if (mSurfaceComposerClient == NULL) {
327 mSurfaceComposerClient = new SurfaceComposerClient();
328 }
329
330 if (mSurfaceControl == NULL) {
331 mSurfaceControl = mSurfaceComposerClient->createSurface(getpid(),
332 String8("Pointer Icon"), 0,
333 mLocked.iconBitmap->width(), mLocked.iconBitmap->height(),
334 PIXEL_FORMAT_RGBA_8888);
335 if (mSurfaceControl == NULL) {
336 LOGE("Error creating pointer surface.");
337 return false;
338 }
339 }
340 return true;
341}
342
343bool PointerController::drawPointerIfNeededLocked() {
344 if (!mLocked.drawn) {
345 if (!mLocked.iconBitmap) {
346 return false;
347 }
348
349 if (!resizeSurfaceLocked(mLocked.iconBitmap->width(), mLocked.iconBitmap->height())) {
350 return false;
351 }
352
353 sp<Surface> surface = mSurfaceControl->getSurface();
354
355 Surface::SurfaceInfo surfaceInfo;
356 status_t status = surface->lock(&surfaceInfo);
357 if (status) {
358 LOGE("Error %d locking pointer surface before drawing.", status);
359 return false;
360 }
361
362 SkBitmap surfaceBitmap;
363 ssize_t bpr = surfaceInfo.s * bytesPerPixel(surfaceInfo.format);
364 surfaceBitmap.setConfig(SkBitmap::kARGB_8888_Config, surfaceInfo.w, surfaceInfo.h, bpr);
365 surfaceBitmap.setPixels(surfaceInfo.bits);
366
367 SkCanvas surfaceCanvas;
368 surfaceCanvas.setBitmapDevice(surfaceBitmap);
369
370 SkPaint paint;
371 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
372 surfaceCanvas.drawBitmap(*mLocked.iconBitmap, 0, 0, &paint);
373
374 status = surface->unlockAndPost();
375 if (status) {
376 LOGE("Error %d unlocking pointer surface after drawing.", status);
377 return false;
378 }
379 }
380
381 mLocked.drawn = true;
382 return true;
383}
384
385bool PointerController::resizeSurfaceLocked(int32_t width, int32_t height) {
386 status_t status = mSurfaceComposerClient->openTransaction();
387 if (status) {
388 LOGE("Error opening surface transaction to resize pointer surface.");
389 return false;
390 }
391
392 status = mSurfaceControl->setSize(width, height);
393 if (status) {
394 LOGE("Error %d setting pointer surface size.", status);
395 return false;
396 }
397
398 status = mSurfaceComposerClient->closeTransaction();
399 if (status) {
400 LOGE("Error closing surface transaction to resize pointer surface.");
401 return false;
402 }
403
404 return true;
405}
406
407} // namespace android