OSDN Git Service

Merge "add support for EGL_FRAMEBUFFER_TARGET_ANDROID" into jb-mr1-dev
[android-x86/frameworks-native.git] / services / surfaceflinger / LayerBase.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 #include <stdlib.h>
18 #include <stdint.h>
19 #include <sys/types.h>
20
21 #include <utils/Errors.h>
22 #include <utils/Log.h>
23 #include <binder/IPCThreadState.h>
24 #include <binder/IServiceManager.h>
25
26 #include <GLES/gl.h>
27 #include <GLES/glext.h>
28
29 #include <hardware/hardware.h>
30
31 #include "clz.h"
32 #include "Client.h"
33 #include "LayerBase.h"
34 #include "Layer.h"
35 #include "SurfaceFlinger.h"
36 #include "DisplayDevice.h"
37
38 namespace android {
39
40 // ---------------------------------------------------------------------------
41
42 int32_t LayerBase::sSequence = 1;
43
44 LayerBase::LayerBase(SurfaceFlinger* flinger)
45     : contentDirty(false),
46       sequence(uint32_t(android_atomic_inc(&sSequence))),
47       mFlinger(flinger), mFiltering(false),
48       mNeedsFiltering(false),
49       mTransactionFlags(0),
50       mPremultipliedAlpha(true), mName("unnamed"), mDebug(false)
51 {
52 }
53
54 LayerBase::~LayerBase()
55 {
56 }
57
58 void LayerBase::setName(const String8& name) {
59     mName = name;
60 }
61
62 String8 LayerBase::getName() const {
63     return mName;
64 }
65
66 void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
67 {
68     uint32_t layerFlags = 0;
69     if (flags & ISurfaceComposerClient::eHidden)
70         layerFlags = layer_state_t::eLayerHidden;
71
72     if (flags & ISurfaceComposerClient::eNonPremultiplied)
73         mPremultipliedAlpha = false;
74
75     mCurrentState.active.w = w;
76     mCurrentState.active.h = h;
77     mCurrentState.active.crop.makeInvalid();
78     mCurrentState.z = 0;
79     mCurrentState.alpha = 0xFF;
80     mCurrentState.layerStack = 0;
81     mCurrentState.flags = layerFlags;
82     mCurrentState.sequence = 0;
83     mCurrentState.transform.set(0, 0);
84     mCurrentState.requested = mCurrentState.active;
85
86     // drawing state & current state are identical
87     mDrawingState = mCurrentState;
88 }
89
90 bool LayerBase::needsFiltering(const sp<const DisplayDevice>& hw) const {
91     return mNeedsFiltering || hw->needsFiltering();
92 }
93
94 void LayerBase::commitTransaction() {
95     mDrawingState = mCurrentState;
96 }
97 void LayerBase::forceVisibilityTransaction() {
98     // this can be called without SurfaceFlinger.mStateLock, but if we
99     // can atomically increment the sequence number, it doesn't matter.
100     android_atomic_inc(&mCurrentState.sequence);
101     requestTransaction();
102 }
103 bool LayerBase::requestTransaction() {
104     int32_t old = setTransactionFlags(eTransactionNeeded);
105     return ((old & eTransactionNeeded) == 0);
106 }
107 uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
108     return android_atomic_and(~flags, &mTransactionFlags) & flags;
109 }
110 uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
111     return android_atomic_or(flags, &mTransactionFlags);
112 }
113
114 bool LayerBase::setPosition(float x, float y) {
115     if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
116         return false;
117     mCurrentState.sequence++;
118     mCurrentState.transform.set(x, y);
119     requestTransaction();
120     return true;
121 }
122 bool LayerBase::setLayer(uint32_t z) {
123     if (mCurrentState.z == z)
124         return false;
125     mCurrentState.sequence++;
126     mCurrentState.z = z;
127     requestTransaction();
128     return true;
129 }
130 bool LayerBase::setSize(uint32_t w, uint32_t h) {
131     if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
132         return false;
133     mCurrentState.requested.w = w;
134     mCurrentState.requested.h = h;
135     requestTransaction();
136     return true;
137 }
138 bool LayerBase::setAlpha(uint8_t alpha) {
139     if (mCurrentState.alpha == alpha)
140         return false;
141     mCurrentState.sequence++;
142     mCurrentState.alpha = alpha;
143     requestTransaction();
144     return true;
145 }
146 bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
147     mCurrentState.sequence++;
148     mCurrentState.transform.set(
149             matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
150     requestTransaction();
151     return true;
152 }
153 bool LayerBase::setTransparentRegionHint(const Region& transparent) {
154     mCurrentState.sequence++;
155     mCurrentState.transparentRegion = transparent;
156     requestTransaction();
157     return true;
158 }
159 bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
160     const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
161     if (mCurrentState.flags == newFlags)
162         return false;
163     mCurrentState.sequence++;
164     mCurrentState.flags = newFlags;
165     requestTransaction();
166     return true;
167 }
168 bool LayerBase::setCrop(const Rect& crop) {
169     if (mCurrentState.requested.crop == crop)
170         return false;
171     mCurrentState.sequence++;
172     mCurrentState.requested.crop = crop;
173     requestTransaction();
174     return true;
175 }
176
177 bool LayerBase::setLayerStack(uint32_t layerStack) {
178     if (mCurrentState.layerStack == layerStack)
179         return false;
180     mCurrentState.sequence++;
181     mCurrentState.layerStack = layerStack;
182     requestTransaction();
183     return true;
184 }
185
186 void LayerBase::setVisibleRegion(const Region& visibleRegion) {
187     // always called from main thread
188     this->visibleRegion = visibleRegion;
189 }
190
191 void LayerBase::setCoveredRegion(const Region& coveredRegion) {
192     // always called from main thread
193     this->coveredRegion = coveredRegion;
194 }
195
196 uint32_t LayerBase::doTransaction(uint32_t flags)
197 {
198     const Layer::State& front(drawingState());
199     const Layer::State& temp(currentState());
200
201     // always set active to requested, unless we're asked not to
202     // this is used by Layer, which special cases resizes.
203     if (flags & eDontUpdateGeometryState)  {
204     } else {
205         Layer::State& editTemp(currentState());
206         editTemp.active = temp.requested;
207     }
208
209     if (front.active != temp.active) {
210         // invalidate and recompute the visible regions if needed
211         flags |= Layer::eVisibleRegion;
212     }
213
214     if (temp.sequence != front.sequence) {
215         // invalidate and recompute the visible regions if needed
216         flags |= eVisibleRegion;
217         this->contentDirty = true;
218
219         // we may use linear filtering, if the matrix scales us
220         const uint8_t type = temp.transform.getType();
221         mNeedsFiltering = (!temp.transform.preserveRects() ||
222                 (type >= Transform::SCALE));
223     }
224
225     // Commit the transaction
226     commitTransaction();
227     return flags;
228 }
229
230 void LayerBase::computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const
231 {
232     const Layer::State& s(drawingState());
233     const Transform tr(hw->getTransform() * s.transform);
234     const uint32_t hw_h = hw->getHeight();
235     Rect win(s.active.w, s.active.h);
236     if (!s.active.crop.isEmpty()) {
237         win.intersect(s.active.crop, &win);
238     }
239     if (mesh) {
240         tr.transform(mesh->mVertices[0], win.left,  win.top);
241         tr.transform(mesh->mVertices[1], win.left,  win.bottom);
242         tr.transform(mesh->mVertices[2], win.right, win.bottom);
243         tr.transform(mesh->mVertices[3], win.right, win.top);
244         for (size_t i=0 ; i<4 ; i++) {
245             mesh->mVertices[i][1] = hw_h - mesh->mVertices[i][1];
246         }
247     }
248 }
249
250 Rect LayerBase::computeBounds() const {
251     const Layer::State& s(drawingState());
252     Rect win(s.active.w, s.active.h);
253     if (!s.active.crop.isEmpty()) {
254         win.intersect(s.active.crop, &win);
255     }
256     return s.transform.transform(win);
257 }
258
259 Region LayerBase::latchBuffer(bool& recomputeVisibleRegions) {
260     Region result;
261     return result;
262 }
263
264 void LayerBase::setGeometry(
265     const sp<const DisplayDevice>& hw,
266         HWComposer::HWCLayerInterface& layer)
267 {
268     layer.setDefaultState();
269
270     // this gives us only the "orientation" component of the transform
271     const State& s(drawingState());
272     const uint32_t finalTransform = s.transform.getOrientation();
273     // we can only handle simple transformation
274     if (finalTransform & Transform::ROT_INVALID) {
275         layer.setTransform(0);
276     } else {
277         layer.setTransform(finalTransform);
278     }
279
280     if (!isOpaque()) {
281         layer.setBlending(mPremultipliedAlpha ?
282                 HWC_BLENDING_PREMULT :
283                 HWC_BLENDING_COVERAGE);
284     }
285
286     const Transform& tr = hw->getTransform();
287     Rect transformedBounds(computeBounds());
288     transformedBounds = tr.transform(transformedBounds);
289
290     // scaling is already applied in transformedBounds
291     layer.setFrame(transformedBounds);
292     layer.setCrop(transformedBounds.getBounds());
293 }
294
295 void LayerBase::setPerFrameData(const sp<const DisplayDevice>& hw,
296         HWComposer::HWCLayerInterface& layer) {
297     // we have to set the visible region on every frame because
298     // we currently free it during onLayerDisplayed(), which is called
299     // after HWComposer::commit() -- every frame.
300     const Transform& tr = hw->getTransform();
301     layer.setVisibleRegionScreen(tr.transform(visibleRegion));
302 }
303
304 void LayerBase::setAcquireFence(const sp<const DisplayDevice>& hw,
305         HWComposer::HWCLayerInterface& layer) {
306     layer.setAcquireFenceFd(-1);
307 }
308
309 void LayerBase::onLayerDisplayed(const sp<const DisplayDevice>& hw,
310         HWComposer::HWCLayerInterface* layer) {
311     if (layer) {
312         layer->onDisplayed();
313     }
314 }
315
316 void LayerBase::setFiltering(bool filtering)
317 {
318     mFiltering = filtering;
319 }
320
321 bool LayerBase::getFiltering() const
322 {
323     return mFiltering;
324 }
325
326 bool LayerBase::isVisible() const {
327     const Layer::State& s(mDrawingState);
328     return !(s.flags & layer_state_t::eLayerHidden) && s.alpha;
329 }
330
331 void LayerBase::draw(const sp<const DisplayDevice>& hw, const Region& clip) const
332 {
333     onDraw(hw, clip);
334 }
335
336 void LayerBase::draw(const sp<const DisplayDevice>& hw)
337 {
338     onDraw( hw, Region(hw->bounds()) );
339 }
340
341 void LayerBase::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
342         GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) const
343 {
344     const uint32_t fbHeight = hw->getHeight();
345     glColor4f(red,green,blue,alpha);
346
347     glDisable(GL_TEXTURE_EXTERNAL_OES);
348     glDisable(GL_TEXTURE_2D);
349     glDisable(GL_BLEND);
350
351     LayerMesh mesh;
352     computeGeometry(hw, &mesh);
353
354     glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
355     glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
356 }
357
358 void LayerBase::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const
359 {
360     clearWithOpenGL(hw, clip, 0,0,0,0);
361 }
362
363 void LayerBase::drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const
364 {
365     const uint32_t fbHeight = hw->getHeight();
366     const State& s(drawingState());
367
368     GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
369     if (CC_UNLIKELY(s.alpha < 0xFF)) {
370         const GLfloat alpha = s.alpha * (1.0f/255.0f);
371         if (mPremultipliedAlpha) {
372             glColor4f(alpha, alpha, alpha, alpha);
373         } else {
374             glColor4f(1, 1, 1, alpha);
375         }
376         glEnable(GL_BLEND);
377         glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
378         glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
379     } else {
380         glColor4f(1, 1, 1, 1);
381         glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
382         if (!isOpaque()) {
383             glEnable(GL_BLEND);
384             glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
385         } else {
386             glDisable(GL_BLEND);
387         }
388     }
389
390     LayerMesh mesh;
391     computeGeometry(hw, &mesh);
392
393     // TODO: we probably want to generate the texture coords with the mesh
394     // here we assume that we only have 4 vertices
395
396     struct TexCoords {
397         GLfloat u;
398         GLfloat v;
399     };
400
401     Rect win(s.active.w, s.active.h);
402     if (!s.active.crop.isEmpty()) {
403         win.intersect(s.active.crop, &win);
404     }
405
406     GLfloat left   = GLfloat(win.left)   / GLfloat(s.active.w);
407     GLfloat top    = GLfloat(win.top)    / GLfloat(s.active.h);
408     GLfloat right  = GLfloat(win.right)  / GLfloat(s.active.w);
409     GLfloat bottom = GLfloat(win.bottom) / GLfloat(s.active.h);
410
411     TexCoords texCoords[4];
412     texCoords[0].u = left;
413     texCoords[0].v = top;
414     texCoords[1].u = left;
415     texCoords[1].v = bottom;
416     texCoords[2].u = right;
417     texCoords[2].v = bottom;
418     texCoords[3].u = right;
419     texCoords[3].v = top;
420     for (int i = 0; i < 4; i++) {
421         texCoords[i].v = 1.0f - texCoords[i].v;
422     }
423
424     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
425     glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
426     glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
427     glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
428
429     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
430     glDisable(GL_BLEND);
431 }
432
433 void LayerBase::dump(String8& result, char* buffer, size_t SIZE) const
434 {
435     const Layer::State& s(drawingState());
436
437     snprintf(buffer, SIZE,
438             "+ %s %p (%s)\n",
439             getTypeId(), this, getName().string());
440     result.append(buffer);
441
442     s.transparentRegion.dump(result, "transparentRegion");
443     visibleRegion.dump(result, "visibleRegion");
444
445     snprintf(buffer, SIZE,
446             "      "
447             "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), crop=(%4d,%4d,%4d,%4d), "
448             "isOpaque=%1d, needsDithering=%1d, invalidate=%1d, "
449             "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
450             s.layerStack, s.z, s.transform.tx(), s.transform.ty(), s.active.w, s.active.h,
451             s.active.crop.left, s.active.crop.top,
452             s.active.crop.right, s.active.crop.bottom,
453             isOpaque(), needsDithering(), contentDirty,
454             s.alpha, s.flags,
455             s.transform[0][0], s.transform[0][1],
456             s.transform[1][0], s.transform[1][1]);
457     result.append(buffer);
458 }
459
460 void LayerBase::shortDump(String8& result, char* scratch, size_t size) const {
461     LayerBase::dump(result, scratch, size);
462 }
463
464 void LayerBase::dumpStats(String8& result, char* scratch, size_t SIZE) const {
465 }
466
467 void LayerBase::clearStats() {
468 }
469
470 sp<LayerBaseClient> LayerBase::getLayerBaseClient() const {
471     return 0;
472 }
473
474 sp<Layer> LayerBase::getLayer() const {
475     return 0;
476 }
477
478 // ---------------------------------------------------------------------------
479
480 int32_t LayerBaseClient::sIdentity = 1;
481
482 LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger,
483         const sp<Client>& client)
484     : LayerBase(flinger),
485       mHasSurface(false),
486       mClientRef(client),
487       mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
488 {
489 }
490
491 LayerBaseClient::~LayerBaseClient()
492 {
493     sp<Client> c(mClientRef.promote());
494     if (c != 0) {
495         c->detachLayer(this);
496     }
497 }
498
499 sp<ISurface> LayerBaseClient::createSurface()
500 {
501     class BSurface : public BnSurface, public LayerCleaner {
502         virtual sp<ISurfaceTexture> getSurfaceTexture() const { return 0; }
503     public:
504         BSurface(const sp<SurfaceFlinger>& flinger,
505                 const sp<LayerBaseClient>& layer)
506             : LayerCleaner(flinger, layer) { }
507     };
508     sp<ISurface> sur(new BSurface(mFlinger, this));
509     return sur;
510 }
511
512 sp<ISurface> LayerBaseClient::getSurface()
513 {
514     sp<ISurface> s;
515     Mutex::Autolock _l(mLock);
516
517     LOG_ALWAYS_FATAL_IF(mHasSurface,
518             "LayerBaseClient::getSurface() has already been called");
519
520     mHasSurface = true;
521     s = createSurface();
522     mClientSurfaceBinder = s->asBinder();
523     return s;
524 }
525
526 wp<IBinder> LayerBaseClient::getSurfaceBinder() const {
527     return mClientSurfaceBinder;
528 }
529
530 wp<IBinder> LayerBaseClient::getSurfaceTextureBinder() const {
531     return 0;
532 }
533
534 void LayerBaseClient::dump(String8& result, char* buffer, size_t SIZE) const
535 {
536     LayerBase::dump(result, buffer, SIZE);
537
538     sp<Client> client(mClientRef.promote());
539     snprintf(buffer, SIZE,
540             "      client=%p, identity=%u\n",
541             client.get(), getIdentity());
542
543     result.append(buffer);
544 }
545
546
547 void LayerBaseClient::shortDump(String8& result, char* scratch, size_t size) const
548 {
549     LayerBaseClient::dump(result, scratch, size);
550 }
551
552 // ---------------------------------------------------------------------------
553
554 LayerBaseClient::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
555         const sp<LayerBaseClient>& layer)
556     : mFlinger(flinger), mLayer(layer) {
557 }
558
559 LayerBaseClient::LayerCleaner::~LayerCleaner() {
560     // destroy client resources
561     mFlinger->onLayerDestroyed(mLayer);
562 }
563
564 // ---------------------------------------------------------------------------
565
566 }; // namespace android