OSDN Git Service

Remove freeze(), unfreeze() and setFreezeTint().
[android-x86/frameworks-native.git] / libs / gui / SurfaceComposerClient.cpp
1 /*
2  * Copyright (C) 2007 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 "SurfaceComposerClient"
18
19 #include <stdint.h>
20 #include <sys/types.h>
21
22 #include <utils/Errors.h>
23 #include <utils/Log.h>
24 #include <utils/Singleton.h>
25 #include <utils/SortedVector.h>
26 #include <utils/String8.h>
27 #include <utils/threads.h>
28
29 #include <binder/IMemory.h>
30 #include <binder/IServiceManager.h>
31
32 #include <ui/DisplayInfo.h>
33
34 #include <gui/ISurface.h>
35 #include <gui/ISurfaceComposer.h>
36 #include <gui/ISurfaceComposerClient.h>
37 #include <gui/SurfaceComposerClient.h>
38
39 #include <private/gui/ComposerService.h>
40 #include <private/gui/LayerState.h>
41 #include <private/gui/SharedBufferStack.h>
42
43 namespace android {
44 // ---------------------------------------------------------------------------
45
46 ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
47
48 ComposerService::ComposerService()
49 : Singleton<ComposerService>() {
50     const String16 name("SurfaceFlinger");
51     while (getService(name, &mComposerService) != NO_ERROR) {
52         usleep(250000);
53     }
54     mServerCblkMemory = mComposerService->getCblk();
55     mServerCblk = static_cast<surface_flinger_cblk_t volatile *>(
56             mServerCblkMemory->getBase());
57 }
58
59 sp<ISurfaceComposer> ComposerService::getComposerService() {
60     return ComposerService::getInstance().mComposerService;
61 }
62
63 surface_flinger_cblk_t const volatile * ComposerService::getControlBlock() {
64     return ComposerService::getInstance().mServerCblk;
65 }
66
67 static inline sp<ISurfaceComposer> getComposerService() {
68     return ComposerService::getComposerService();
69 }
70
71 static inline surface_flinger_cblk_t const volatile * get_cblk() {
72     return ComposerService::getControlBlock();
73 }
74
75 // ---------------------------------------------------------------------------
76
77 // NOTE: this is NOT a member function (it's a friend defined with its
78 // declaration).
79 static inline
80 int compare_type( const ComposerState& lhs, const ComposerState& rhs) {
81     if (lhs.client < rhs.client)  return -1;
82     if (lhs.client > rhs.client)  return 1;
83     if (lhs.state.surface < rhs.state.surface)  return -1;
84     if (lhs.state.surface > rhs.state.surface)  return 1;
85     return 0;
86 }
87
88 class Composer : public Singleton<Composer>
89 {
90     friend class Singleton<Composer>;
91
92     mutable Mutex               mLock;
93     SortedVector<ComposerState> mStates;
94     int                         mOrientation;
95     uint32_t                    mForceSynchronous;
96
97     Composer() : Singleton<Composer>(),
98         mOrientation(ISurfaceComposer::eOrientationUnchanged),
99         mForceSynchronous(0)
100     { }
101
102     void closeGlobalTransactionImpl(bool synchronous);
103
104     layer_state_t* getLayerStateLocked(
105             const sp<SurfaceComposerClient>& client, SurfaceID id);
106
107 public:
108
109     status_t setPosition(const sp<SurfaceComposerClient>& client, SurfaceID id,
110             float x, float y);
111     status_t setSize(const sp<SurfaceComposerClient>& client, SurfaceID id,
112             uint32_t w, uint32_t h);
113     status_t setLayer(const sp<SurfaceComposerClient>& client, SurfaceID id,
114             int32_t z);
115     status_t setFlags(const sp<SurfaceComposerClient>& client, SurfaceID id,
116             uint32_t flags, uint32_t mask);
117     status_t setTransparentRegionHint(
118             const sp<SurfaceComposerClient>& client, SurfaceID id,
119             const Region& transparentRegion);
120     status_t setAlpha(const sp<SurfaceComposerClient>& client, SurfaceID id,
121             float alpha);
122     status_t setMatrix(const sp<SurfaceComposerClient>& client, SurfaceID id,
123             float dsdx, float dtdx, float dsdy, float dtdy);
124     status_t setOrientation(int orientation);
125     status_t setCrop(const sp<SurfaceComposerClient>& client, SurfaceID id,
126             const Rect& crop);
127
128     static void closeGlobalTransaction(bool synchronous) {
129         Composer::getInstance().closeGlobalTransactionImpl(synchronous);
130     }
131 };
132
133 ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
134
135 // ---------------------------------------------------------------------------
136
137 void Composer::closeGlobalTransactionImpl(bool synchronous) {
138     sp<ISurfaceComposer> sm(getComposerService());
139
140     Vector<ComposerState> transaction;
141     int orientation;
142     uint32_t flags = 0;
143
144     { // scope for the lock
145         Mutex::Autolock _l(mLock);
146         transaction = mStates;
147         mStates.clear();
148
149         orientation = mOrientation;
150         mOrientation = ISurfaceComposer::eOrientationUnchanged;
151
152         if (synchronous || mForceSynchronous) {
153             flags |= ISurfaceComposer::eSynchronous;
154         }
155         mForceSynchronous = false;
156     }
157
158    sm->setTransactionState(transaction, orientation, flags);
159 }
160
161 layer_state_t* Composer::getLayerStateLocked(
162         const sp<SurfaceComposerClient>& client, SurfaceID id) {
163
164     ComposerState s;
165     s.client = client->mClient;
166     s.state.surface = id;
167
168     ssize_t index = mStates.indexOf(s);
169     if (index < 0) {
170         // we don't have it, add an initialized layer_state to our list
171         index = mStates.add(s);
172     }
173
174     ComposerState* const out = mStates.editArray();
175     return &(out[index].state);
176 }
177
178 status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
179         SurfaceID id, float x, float y) {
180     Mutex::Autolock _l(mLock);
181     layer_state_t* s = getLayerStateLocked(client, id);
182     if (!s)
183         return BAD_INDEX;
184     s->what |= ISurfaceComposer::ePositionChanged;
185     s->x = x;
186     s->y = y;
187     return NO_ERROR;
188 }
189
190 status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
191         SurfaceID id, uint32_t w, uint32_t h) {
192     Mutex::Autolock _l(mLock);
193     layer_state_t* s = getLayerStateLocked(client, id);
194     if (!s)
195         return BAD_INDEX;
196     s->what |= ISurfaceComposer::eSizeChanged;
197     s->w = w;
198     s->h = h;
199
200     // Resizing a surface makes the transaction synchronous.
201     mForceSynchronous = true;
202
203     return NO_ERROR;
204 }
205
206 status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
207         SurfaceID id, int32_t z) {
208     Mutex::Autolock _l(mLock);
209     layer_state_t* s = getLayerStateLocked(client, id);
210     if (!s)
211         return BAD_INDEX;
212     s->what |= ISurfaceComposer::eLayerChanged;
213     s->z = z;
214     return NO_ERROR;
215 }
216
217 status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
218         SurfaceID id, uint32_t flags,
219         uint32_t mask) {
220     Mutex::Autolock _l(mLock);
221     layer_state_t* s = getLayerStateLocked(client, id);
222     if (!s)
223         return BAD_INDEX;
224     s->what |= ISurfaceComposer::eVisibilityChanged;
225     s->flags &= ~mask;
226     s->flags |= (flags & mask);
227     s->mask |= mask;
228     return NO_ERROR;
229 }
230
231 status_t Composer::setTransparentRegionHint(
232         const sp<SurfaceComposerClient>& client, SurfaceID id,
233         const Region& transparentRegion) {
234     Mutex::Autolock _l(mLock);
235     layer_state_t* s = getLayerStateLocked(client, id);
236     if (!s)
237         return BAD_INDEX;
238     s->what |= ISurfaceComposer::eTransparentRegionChanged;
239     s->transparentRegion = transparentRegion;
240     return NO_ERROR;
241 }
242
243 status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
244         SurfaceID id, float alpha) {
245     Mutex::Autolock _l(mLock);
246     layer_state_t* s = getLayerStateLocked(client, id);
247     if (!s)
248         return BAD_INDEX;
249     s->what |= ISurfaceComposer::eAlphaChanged;
250     s->alpha = alpha;
251     return NO_ERROR;
252 }
253
254 status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
255         SurfaceID id, float dsdx, float dtdx,
256         float dsdy, float dtdy) {
257     Mutex::Autolock _l(mLock);
258     layer_state_t* s = getLayerStateLocked(client, id);
259     if (!s)
260         return BAD_INDEX;
261     s->what |= ISurfaceComposer::eMatrixChanged;
262     layer_state_t::matrix22_t matrix;
263     matrix.dsdx = dsdx;
264     matrix.dtdx = dtdx;
265     matrix.dsdy = dsdy;
266     matrix.dtdy = dtdy;
267     s->matrix = matrix;
268     return NO_ERROR;
269 }
270
271 status_t Composer::setOrientation(int orientation) {
272     Mutex::Autolock _l(mLock);
273     mOrientation = orientation;
274
275     // Changing the orientation makes the transaction synchronous.
276     mForceSynchronous = true;
277
278     return NO_ERROR;
279 }
280
281 status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
282         SurfaceID id, const Rect& crop) {
283     Mutex::Autolock _l(mLock);
284     layer_state_t* s = getLayerStateLocked(client, id);
285     if (!s)
286         return BAD_INDEX;
287     s->what |= ISurfaceComposer::eCropChanged;
288     s->crop = crop;
289     return NO_ERROR;
290 }
291
292 // ---------------------------------------------------------------------------
293
294 SurfaceComposerClient::SurfaceComposerClient()
295     : mStatus(NO_INIT), mComposer(Composer::getInstance())
296 {
297 }
298
299 void SurfaceComposerClient::onFirstRef() {
300     sp<ISurfaceComposer> sm(getComposerService());
301     if (sm != 0) {
302         sp<ISurfaceComposerClient> conn = sm->createConnection();
303         if (conn != 0) {
304             mClient = conn;
305             mStatus = NO_ERROR;
306         }
307     }
308 }
309
310 SurfaceComposerClient::~SurfaceComposerClient() {
311     dispose();
312 }
313
314 status_t SurfaceComposerClient::initCheck() const {
315     return mStatus;
316 }
317
318 sp<IBinder> SurfaceComposerClient::connection() const {
319     return (mClient != 0) ? mClient->asBinder() : 0;
320 }
321
322 status_t SurfaceComposerClient::linkToComposerDeath(
323         const sp<IBinder::DeathRecipient>& recipient,
324         void* cookie, uint32_t flags) {
325     sp<ISurfaceComposer> sm(getComposerService());
326     return sm->asBinder()->linkToDeath(recipient, cookie, flags);
327 }
328
329 void SurfaceComposerClient::dispose() {
330     // this can be called more than once.
331     sp<ISurfaceComposerClient> client;
332     Mutex::Autolock _lm(mLock);
333     if (mClient != 0) {
334         client = mClient; // hold ref while lock is held
335         mClient.clear();
336     }
337     mStatus = NO_INIT;
338 }
339
340 sp<SurfaceControl> SurfaceComposerClient::createSurface(
341         DisplayID display,
342         uint32_t w,
343         uint32_t h,
344         PixelFormat format,
345         uint32_t flags)
346 {
347     String8 name;
348     const size_t SIZE = 128;
349     char buffer[SIZE];
350     snprintf(buffer, SIZE, "<pid_%d>", getpid());
351     name.append(buffer);
352
353     return SurfaceComposerClient::createSurface(name, display,
354             w, h, format, flags);
355 }
356
357 sp<SurfaceControl> SurfaceComposerClient::createSurface(
358         const String8& name,
359         DisplayID display,
360         uint32_t w,
361         uint32_t h,
362         PixelFormat format,
363         uint32_t flags)
364 {
365     sp<SurfaceControl> result;
366     if (mStatus == NO_ERROR) {
367         ISurfaceComposerClient::surface_data_t data;
368         sp<ISurface> surface = mClient->createSurface(&data, name,
369                 display, w, h, format, flags);
370         if (surface != 0) {
371             result = new SurfaceControl(this, surface, data);
372         }
373     }
374     return result;
375 }
376
377 status_t SurfaceComposerClient::destroySurface(SurfaceID sid) {
378     if (mStatus != NO_ERROR)
379         return mStatus;
380     status_t err = mClient->destroySurface(sid);
381     return err;
382 }
383
384 inline Composer& SurfaceComposerClient::getComposer() {
385     return mComposer;
386 }
387
388 // ----------------------------------------------------------------------------
389
390 void SurfaceComposerClient::openGlobalTransaction() {
391     // Currently a no-op
392 }
393
394 void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
395     Composer::closeGlobalTransaction(synchronous);
396 }
397
398 // ----------------------------------------------------------------------------
399
400 status_t SurfaceComposerClient::setCrop(SurfaceID id, const Rect& crop) {
401     return getComposer().setCrop(this, id, crop);
402 }
403
404 status_t SurfaceComposerClient::setPosition(SurfaceID id, float x, float y) {
405     return getComposer().setPosition(this, id, x, y);
406 }
407
408 status_t SurfaceComposerClient::setSize(SurfaceID id, uint32_t w, uint32_t h) {
409     return getComposer().setSize(this, id, w, h);
410 }
411
412 status_t SurfaceComposerClient::setLayer(SurfaceID id, int32_t z) {
413     return getComposer().setLayer(this, id, z);
414 }
415
416 status_t SurfaceComposerClient::hide(SurfaceID id) {
417     return getComposer().setFlags(this, id,
418             ISurfaceComposer::eLayerHidden,
419             ISurfaceComposer::eLayerHidden);
420 }
421
422 status_t SurfaceComposerClient::show(SurfaceID id, int32_t) {
423     return getComposer().setFlags(this, id,
424             0,
425             ISurfaceComposer::eLayerHidden);
426 }
427
428 status_t SurfaceComposerClient::setFlags(SurfaceID id, uint32_t flags,
429         uint32_t mask) {
430     return getComposer().setFlags(this, id, flags, mask);
431 }
432
433 status_t SurfaceComposerClient::setTransparentRegionHint(SurfaceID id,
434         const Region& transparentRegion) {
435     return getComposer().setTransparentRegionHint(this, id, transparentRegion);
436 }
437
438 status_t SurfaceComposerClient::setAlpha(SurfaceID id, float alpha) {
439     return getComposer().setAlpha(this, id, alpha);
440 }
441
442 status_t SurfaceComposerClient::setMatrix(SurfaceID id, float dsdx, float dtdx,
443         float dsdy, float dtdy) {
444     return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
445 }
446
447 status_t SurfaceComposerClient::setOrientation(DisplayID dpy,
448         int orientation, uint32_t flags)
449 {
450     return Composer::getInstance().setOrientation(orientation);
451 }
452
453 // ----------------------------------------------------------------------------
454
455 status_t SurfaceComposerClient::getDisplayInfo(
456         DisplayID dpy, DisplayInfo* info)
457 {
458     if (uint32_t(dpy)>=NUM_DISPLAY_MAX)
459         return BAD_VALUE;
460
461     volatile surface_flinger_cblk_t const * cblk = get_cblk();
462     volatile display_cblk_t const * dcblk = cblk->displays + dpy;
463
464     info->w              = dcblk->w;
465     info->h              = dcblk->h;
466     info->orientation    = dcblk->orientation;
467     info->xdpi           = dcblk->xdpi;
468     info->ydpi           = dcblk->ydpi;
469     info->fps            = dcblk->fps;
470     info->density        = dcblk->density;
471     return getPixelFormatInfo(dcblk->format, &(info->pixelFormatInfo));
472 }
473
474 ssize_t SurfaceComposerClient::getDisplayWidth(DisplayID dpy)
475 {
476     if (uint32_t(dpy)>=NUM_DISPLAY_MAX)
477         return BAD_VALUE;
478     volatile surface_flinger_cblk_t const * cblk = get_cblk();
479     volatile display_cblk_t const * dcblk = cblk->displays + dpy;
480     return dcblk->w;
481 }
482
483 ssize_t SurfaceComposerClient::getDisplayHeight(DisplayID dpy)
484 {
485     if (uint32_t(dpy)>=NUM_DISPLAY_MAX)
486         return BAD_VALUE;
487     volatile surface_flinger_cblk_t const * cblk = get_cblk();
488     volatile display_cblk_t const * dcblk = cblk->displays + dpy;
489     return dcblk->h;
490 }
491
492 ssize_t SurfaceComposerClient::getDisplayOrientation(DisplayID dpy)
493 {
494     if (uint32_t(dpy)>=NUM_DISPLAY_MAX)
495         return BAD_VALUE;
496     volatile surface_flinger_cblk_t const * cblk = get_cblk();
497     volatile display_cblk_t const * dcblk = cblk->displays + dpy;
498     return dcblk->orientation;
499 }
500
501 ssize_t SurfaceComposerClient::getNumberOfDisplays()
502 {
503     volatile surface_flinger_cblk_t const * cblk = get_cblk();
504     uint32_t connected = cblk->connected;
505     int n = 0;
506     while (connected) {
507         if (connected&1) n++;
508         connected >>= 1;
509     }
510     return n;
511 }
512
513 // ----------------------------------------------------------------------------
514
515 ScreenshotClient::ScreenshotClient()
516     : mWidth(0), mHeight(0), mFormat(PIXEL_FORMAT_NONE) {
517 }
518
519 status_t ScreenshotClient::update() {
520     sp<ISurfaceComposer> s(ComposerService::getComposerService());
521     if (s == NULL) return NO_INIT;
522     mHeap = 0;
523     return s->captureScreen(0, &mHeap,
524             &mWidth, &mHeight, &mFormat, 0, 0,
525             0, -1UL);
526 }
527
528 status_t ScreenshotClient::update(uint32_t reqWidth, uint32_t reqHeight) {
529     sp<ISurfaceComposer> s(ComposerService::getComposerService());
530     if (s == NULL) return NO_INIT;
531     mHeap = 0;
532     return s->captureScreen(0, &mHeap,
533             &mWidth, &mHeight, &mFormat, reqWidth, reqHeight,
534             0, -1UL);
535 }
536
537 status_t ScreenshotClient::update(uint32_t reqWidth, uint32_t reqHeight,
538         uint32_t minLayerZ, uint32_t maxLayerZ) {
539     sp<ISurfaceComposer> s(ComposerService::getComposerService());
540     if (s == NULL) return NO_INIT;
541     mHeap = 0;
542     return s->captureScreen(0, &mHeap,
543             &mWidth, &mHeight, &mFormat, reqWidth, reqHeight,
544             minLayerZ, maxLayerZ);
545 }
546
547 void ScreenshotClient::release() {
548     mHeap = 0;
549 }
550
551 void const* ScreenshotClient::getPixels() const {
552     return mHeap->getBase();
553 }
554
555 uint32_t ScreenshotClient::getWidth() const {
556     return mWidth;
557 }
558
559 uint32_t ScreenshotClient::getHeight() const {
560     return mHeight;
561 }
562
563 PixelFormat ScreenshotClient::getFormat() const {
564     return mFormat;
565 }
566
567 uint32_t ScreenshotClient::getStride() const {
568     return mWidth;
569 }
570
571 size_t ScreenshotClient::getSize() const {
572     return mHeap->getSize();
573 }
574
575 // ----------------------------------------------------------------------------
576 }; // namespace android