• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Surface"
18 
19 #include <stdint.h>
20 #include <errno.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 
24 #include <android/native_window.h>
25 
26 #include <utils/CallStack.h>
27 #include <utils/Errors.h>
28 #include <utils/Log.h>
29 #include <utils/threads.h>
30 
31 #include <binder/IPCThreadState.h>
32 
33 #include <ui/DisplayInfo.h>
34 #include <ui/GraphicBuffer.h>
35 #include <ui/Rect.h>
36 
37 #include <gui/ISurface.h>
38 #include <gui/ISurfaceComposer.h>
39 #include <gui/Surface.h>
40 #include <gui/SurfaceComposerClient.h>
41 #include <gui/SurfaceTextureClient.h>
42 
43 namespace android {
44 
45 // ============================================================================
46 //  SurfaceControl
47 // ============================================================================
48 
SurfaceControl(const sp<SurfaceComposerClient> & client,const sp<ISurface> & surface,const ISurfaceComposerClient::surface_data_t & data)49 SurfaceControl::SurfaceControl(
50         const sp<SurfaceComposerClient>& client,
51         const sp<ISurface>& surface,
52         const ISurfaceComposerClient::surface_data_t& data)
53     : mClient(client), mSurface(surface),
54       mToken(data.token), mIdentity(data.identity)
55 {
56 }
57 
~SurfaceControl()58 SurfaceControl::~SurfaceControl()
59 {
60     destroy();
61 }
62 
destroy()63 void SurfaceControl::destroy()
64 {
65     if (isValid()) {
66         mClient->destroySurface(mToken);
67     }
68 
69     // clear all references and trigger an IPC now, to make sure things
70     // happen without delay, since these resources are quite heavy.
71     mClient.clear();
72     mSurface.clear();
73     IPCThreadState::self()->flushCommands();
74 }
75 
clear()76 void SurfaceControl::clear()
77 {
78     // here, the window manager tells us explicitly that we should destroy
79     // the surface's resource. Soon after this call, it will also release
80     // its last reference (which will call the dtor); however, it is possible
81     // that a client living in the same process still holds references which
82     // would delay the call to the dtor -- that is why we need this explicit
83     // "clear()" call.
84     destroy();
85 }
86 
isSameSurface(const sp<SurfaceControl> & lhs,const sp<SurfaceControl> & rhs)87 bool SurfaceControl::isSameSurface(
88         const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
89 {
90     if (lhs == 0 || rhs == 0)
91         return false;
92     return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
93 }
94 
setLayerStack(int32_t layerStack)95 status_t SurfaceControl::setLayerStack(int32_t layerStack) {
96     status_t err = validate();
97     if (err < 0) return err;
98     const sp<SurfaceComposerClient>& client(mClient);
99     return client->setLayerStack(mToken, layerStack);
100 }
setLayer(int32_t layer)101 status_t SurfaceControl::setLayer(int32_t layer) {
102     status_t err = validate();
103     if (err < 0) return err;
104     const sp<SurfaceComposerClient>& client(mClient);
105     return client->setLayer(mToken, layer);
106 }
setPosition(int32_t x,int32_t y)107 status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
108     status_t err = validate();
109     if (err < 0) return err;
110     const sp<SurfaceComposerClient>& client(mClient);
111     return client->setPosition(mToken, x, y);
112 }
setSize(uint32_t w,uint32_t h)113 status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
114     status_t err = validate();
115     if (err < 0) return err;
116     const sp<SurfaceComposerClient>& client(mClient);
117     return client->setSize(mToken, w, h);
118 }
hide()119 status_t SurfaceControl::hide() {
120     status_t err = validate();
121     if (err < 0) return err;
122     const sp<SurfaceComposerClient>& client(mClient);
123     return client->hide(mToken);
124 }
show()125 status_t SurfaceControl::show() {
126     status_t err = validate();
127     if (err < 0) return err;
128     const sp<SurfaceComposerClient>& client(mClient);
129     return client->show(mToken);
130 }
setFlags(uint32_t flags,uint32_t mask)131 status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
132     status_t err = validate();
133     if (err < 0) return err;
134     const sp<SurfaceComposerClient>& client(mClient);
135     return client->setFlags(mToken, flags, mask);
136 }
setTransparentRegionHint(const Region & transparent)137 status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
138     status_t err = validate();
139     if (err < 0) return err;
140     const sp<SurfaceComposerClient>& client(mClient);
141     return client->setTransparentRegionHint(mToken, transparent);
142 }
setAlpha(float alpha)143 status_t SurfaceControl::setAlpha(float alpha) {
144     status_t err = validate();
145     if (err < 0) return err;
146     const sp<SurfaceComposerClient>& client(mClient);
147     return client->setAlpha(mToken, alpha);
148 }
setMatrix(float dsdx,float dtdx,float dsdy,float dtdy)149 status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
150     status_t err = validate();
151     if (err < 0) return err;
152     const sp<SurfaceComposerClient>& client(mClient);
153     return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
154 }
setCrop(const Rect & crop)155 status_t SurfaceControl::setCrop(const Rect& crop) {
156     status_t err = validate();
157     if (err < 0) return err;
158     const sp<SurfaceComposerClient>& client(mClient);
159     return client->setCrop(mToken, crop);
160 }
161 
validate() const162 status_t SurfaceControl::validate() const
163 {
164     if (mToken<0 || mClient==0) {
165         ALOGE("invalid token (%d, identity=%u) or client (%p)",
166                 mToken, mIdentity, mClient.get());
167         return NO_INIT;
168     }
169     return NO_ERROR;
170 }
171 
writeSurfaceToParcel(const sp<SurfaceControl> & control,Parcel * parcel)172 status_t SurfaceControl::writeSurfaceToParcel(
173         const sp<SurfaceControl>& control, Parcel* parcel)
174 {
175     sp<ISurface> sur;
176     uint32_t identity = 0;
177     if (SurfaceControl::isValid(control)) {
178         sur      = control->mSurface;
179         identity = control->mIdentity;
180     }
181     parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
182     parcel->writeStrongBinder(NULL);  // NULL ISurfaceTexture in this case.
183     parcel->writeInt32(identity);
184     return NO_ERROR;
185 }
186 
getSurface() const187 sp<Surface> SurfaceControl::getSurface() const
188 {
189     Mutex::Autolock _l(mLock);
190     if (mSurfaceData == 0) {
191         sp<SurfaceControl> surface_control(const_cast<SurfaceControl*>(this));
192         mSurfaceData = new Surface(surface_control);
193     }
194     return mSurfaceData;
195 }
196 
197 // ============================================================================
198 //  Surface
199 // ============================================================================
200 
201 // ---------------------------------------------------------------------------
202 
Surface(const sp<SurfaceControl> & surface)203 Surface::Surface(const sp<SurfaceControl>& surface)
204     : SurfaceTextureClient(),
205       mSurface(surface->mSurface),
206       mIdentity(surface->mIdentity)
207 {
208     sp<ISurfaceTexture> st;
209     if (mSurface != NULL) {
210         st = mSurface->getSurfaceTexture();
211     }
212     init(st);
213 }
214 
Surface(const Parcel & parcel,const sp<IBinder> & ref)215 Surface::Surface(const Parcel& parcel, const sp<IBinder>& ref)
216     : SurfaceTextureClient()
217 {
218     mSurface = interface_cast<ISurface>(ref);
219     sp<IBinder> st_binder(parcel.readStrongBinder());
220     sp<ISurfaceTexture> st;
221     if (st_binder != NULL) {
222         st = interface_cast<ISurfaceTexture>(st_binder);
223     } else if (mSurface != NULL) {
224         st = mSurface->getSurfaceTexture();
225     }
226 
227     mIdentity   = parcel.readInt32();
228     init(st);
229 }
230 
Surface(const sp<ISurfaceTexture> & st)231 Surface::Surface(const sp<ISurfaceTexture>& st)
232     : SurfaceTextureClient(),
233       mSurface(NULL),
234       mIdentity(0)
235 {
236     init(st);
237 }
238 
writeToParcel(const sp<Surface> & surface,Parcel * parcel)239 status_t Surface::writeToParcel(
240         const sp<Surface>& surface, Parcel* parcel)
241 {
242     sp<ISurface> sur;
243     sp<ISurfaceTexture> st;
244     uint32_t identity = 0;
245     if (Surface::isValid(surface)) {
246         sur      = surface->mSurface;
247         st       = surface->getISurfaceTexture();
248         identity = surface->mIdentity;
249     } else if (surface != 0 &&
250             (surface->mSurface != NULL ||
251              surface->getISurfaceTexture() != NULL)) {
252         ALOGE("Parceling invalid surface with non-NULL ISurface/ISurfaceTexture as NULL: "
253              "mSurface = %p, surfaceTexture = %p, mIdentity = %d, ",
254              surface->mSurface.get(), surface->getISurfaceTexture().get(),
255              surface->mIdentity);
256     }
257 
258     parcel->writeStrongBinder(sur != NULL ? sur->asBinder() : NULL);
259     parcel->writeStrongBinder(st != NULL ? st->asBinder() : NULL);
260     parcel->writeInt32(identity);
261     return NO_ERROR;
262 
263 }
264 
265 Mutex Surface::sCachedSurfacesLock;
266 DefaultKeyedVector<wp<IBinder>, wp<Surface> > Surface::sCachedSurfaces;
267 
readFromParcel(const Parcel & data)268 sp<Surface> Surface::readFromParcel(const Parcel& data) {
269     Mutex::Autolock _l(sCachedSurfacesLock);
270     sp<IBinder> binder(data.readStrongBinder());
271     sp<Surface> surface = sCachedSurfaces.valueFor(binder).promote();
272     if (surface == 0) {
273        surface = new Surface(data, binder);
274        sCachedSurfaces.add(binder, surface);
275     } else {
276         // The Surface was found in the cache, but we still should clear any
277         // remaining data from the parcel.
278         data.readStrongBinder();  // ISurfaceTexture
279         data.readInt32();         // identity
280     }
281     if (surface->mSurface == NULL && surface->getISurfaceTexture() == NULL) {
282         surface = 0;
283     }
284     cleanCachedSurfacesLocked();
285     return surface;
286 }
287 
288 // Remove the stale entries from the surface cache.  This should only be called
289 // with sCachedSurfacesLock held.
cleanCachedSurfacesLocked()290 void Surface::cleanCachedSurfacesLocked() {
291     for (int i = sCachedSurfaces.size()-1; i >= 0; --i) {
292         wp<Surface> s(sCachedSurfaces.valueAt(i));
293         if (s == 0 || s.promote() == 0) {
294             sCachedSurfaces.removeItemsAt(i);
295         }
296     }
297 }
298 
init(const sp<ISurfaceTexture> & surfaceTexture)299 void Surface::init(const sp<ISurfaceTexture>& surfaceTexture)
300 {
301     if (mSurface != NULL || surfaceTexture != NULL) {
302         ALOGE_IF(surfaceTexture==0, "got a NULL ISurfaceTexture from ISurface");
303         if (surfaceTexture != NULL) {
304             setISurfaceTexture(surfaceTexture);
305             setUsage(GraphicBuffer::USAGE_HW_RENDER);
306         }
307 
308         // TODO: the display metrics should come from the display manager
309         DisplayInfo dinfo;
310         sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(
311                 ISurfaceComposer::eDisplayIdMain);
312         SurfaceComposerClient::getDisplayInfo(display, &dinfo);
313         const_cast<float&>(ANativeWindow::xdpi) = dinfo.xdpi;
314         const_cast<float&>(ANativeWindow::ydpi) = dinfo.ydpi;
315         const_cast<uint32_t&>(ANativeWindow::flags) = 0;
316     }
317 }
318 
~Surface()319 Surface::~Surface()
320 {
321     // clear all references and trigger an IPC now, to make sure things
322     // happen without delay, since these resources are quite heavy.
323     mSurface.clear();
324     IPCThreadState::self()->flushCommands();
325 }
326 
isValid()327 bool Surface::isValid() {
328     return getISurfaceTexture() != NULL;
329 }
330 
getSurfaceTexture()331 sp<ISurfaceTexture> Surface::getSurfaceTexture() {
332     return getISurfaceTexture();
333 }
334 
asBinder() const335 sp<IBinder> Surface::asBinder() const {
336     return mSurface!=0 ? mSurface->asBinder() : 0;
337 }
338 
339 // ----------------------------------------------------------------------------
340 
query(int what,int * value) const341 int Surface::query(int what, int* value) const {
342     switch (what) {
343     case NATIVE_WINDOW_CONCRETE_TYPE:
344         *value = NATIVE_WINDOW_SURFACE;
345         return NO_ERROR;
346     }
347     return SurfaceTextureClient::query(what, value);
348 }
349 
350 // ----------------------------------------------------------------------------
351 
lock(SurfaceInfo * other,Region * inOutDirtyRegion)352 status_t Surface::lock(SurfaceInfo* other, Region* inOutDirtyRegion) {
353     ANativeWindow_Buffer outBuffer;
354 
355     ARect temp;
356     ARect* inOutDirtyBounds = NULL;
357     if (inOutDirtyRegion) {
358         temp = inOutDirtyRegion->getBounds();
359         inOutDirtyBounds = &temp;
360     }
361 
362     status_t err = SurfaceTextureClient::lock(&outBuffer, inOutDirtyBounds);
363 
364     if (err == NO_ERROR) {
365         other->w = uint32_t(outBuffer.width);
366         other->h = uint32_t(outBuffer.height);
367         other->s = uint32_t(outBuffer.stride);
368         other->usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
369         other->format = uint32_t(outBuffer.format);
370         other->bits = outBuffer.bits;
371     }
372 
373     if (inOutDirtyRegion) {
374         inOutDirtyRegion->set( static_cast<Rect const&>(temp) );
375     }
376 
377     return err;
378 }
379 
unlockAndPost()380 status_t Surface::unlockAndPost() {
381     return SurfaceTextureClient::unlockAndPost();
382 }
383 
384 // ----------------------------------------------------------------------------
385 }; // namespace android
386