2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/types.h>
21 #include <utils/Errors.h>
22 #include <utils/Log.h>
23 #include <binder/IPCThreadState.h>
24 #include <binder/IServiceManager.h>
27 #include <GLES/glext.h>
29 #include <hardware/hardware.h>
33 #include "LayerBase.h"
35 #include "SurfaceFlinger.h"
36 #include "DisplayDevice.h"
40 // ---------------------------------------------------------------------------
42 int32_t LayerBase::sSequence = 1;
44 LayerBase::LayerBase(SurfaceFlinger* flinger)
45 : contentDirty(false),
46 sequence(uint32_t(android_atomic_inc(&sSequence))),
47 mFlinger(flinger), mFiltering(false),
48 mNeedsFiltering(false),
50 mPremultipliedAlpha(true), mName("unnamed"), mDebug(false)
54 LayerBase::~LayerBase()
58 void LayerBase::setName(const String8& name) {
62 String8 LayerBase::getName() const {
66 void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
68 uint32_t layerFlags = 0;
69 if (flags & ISurfaceComposerClient::eHidden)
70 layerFlags = layer_state_t::eLayerHidden;
72 if (flags & ISurfaceComposerClient::eNonPremultiplied)
73 mPremultipliedAlpha = false;
75 mCurrentState.active.w = w;
76 mCurrentState.active.h = h;
77 mCurrentState.active.crop.makeInvalid();
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;
86 // drawing state & current state are identical
87 mDrawingState = mCurrentState;
90 bool LayerBase::needsFiltering(const sp<const DisplayDevice>& hw) const {
91 return mNeedsFiltering || hw->needsFiltering();
94 void LayerBase::commitTransaction() {
95 mDrawingState = mCurrentState;
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();
103 bool LayerBase::requestTransaction() {
104 int32_t old = setTransactionFlags(eTransactionNeeded);
105 return ((old & eTransactionNeeded) == 0);
107 uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
108 return android_atomic_and(~flags, &mTransactionFlags) & flags;
110 uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
111 return android_atomic_or(flags, &mTransactionFlags);
114 bool LayerBase::setPosition(float x, float y) {
115 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
117 mCurrentState.sequence++;
118 mCurrentState.transform.set(x, y);
119 requestTransaction();
122 bool LayerBase::setLayer(uint32_t z) {
123 if (mCurrentState.z == z)
125 mCurrentState.sequence++;
127 requestTransaction();
130 bool LayerBase::setSize(uint32_t w, uint32_t h) {
131 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
133 mCurrentState.requested.w = w;
134 mCurrentState.requested.h = h;
135 requestTransaction();
138 bool LayerBase::setAlpha(uint8_t alpha) {
139 if (mCurrentState.alpha == alpha)
141 mCurrentState.sequence++;
142 mCurrentState.alpha = alpha;
143 requestTransaction();
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();
153 bool LayerBase::setTransparentRegionHint(const Region& transparent) {
154 mCurrentState.sequence++;
155 mCurrentState.transparentRegion = transparent;
156 requestTransaction();
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)
163 mCurrentState.sequence++;
164 mCurrentState.flags = newFlags;
165 requestTransaction();
168 bool LayerBase::setCrop(const Rect& crop) {
169 if (mCurrentState.requested.crop == crop)
171 mCurrentState.sequence++;
172 mCurrentState.requested.crop = crop;
173 requestTransaction();
177 bool LayerBase::setLayerStack(uint32_t layerStack) {
178 if (mCurrentState.layerStack == layerStack)
180 mCurrentState.sequence++;
181 mCurrentState.layerStack = layerStack;
182 requestTransaction();
186 void LayerBase::setVisibleRegion(const Region& visibleRegion) {
187 // always called from main thread
188 this->visibleRegion = visibleRegion;
191 void LayerBase::setCoveredRegion(const Region& coveredRegion) {
192 // always called from main thread
193 this->coveredRegion = coveredRegion;
196 uint32_t LayerBase::doTransaction(uint32_t flags)
198 const Layer::State& front(drawingState());
199 const Layer::State& temp(currentState());
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) {
205 Layer::State& editTemp(currentState());
206 editTemp.active = temp.requested;
209 if (front.active != temp.active) {
210 // invalidate and recompute the visible regions if needed
211 flags |= Layer::eVisibleRegion;
214 if (temp.sequence != front.sequence) {
215 // invalidate and recompute the visible regions if needed
216 flags |= eVisibleRegion;
217 this->contentDirty = true;
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));
225 // Commit the transaction
230 void LayerBase::computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const
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);
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];
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);
256 return s.transform.transform(win);
259 Region LayerBase::latchBuffer(bool& recomputeVisibleRegions) {
264 void LayerBase::setGeometry(
265 const sp<const DisplayDevice>& hw,
266 HWComposer::HWCLayerInterface& layer)
268 layer.setDefaultState();
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);
277 layer.setTransform(finalTransform);
281 layer.setBlending(mPremultipliedAlpha ?
282 HWC_BLENDING_PREMULT :
283 HWC_BLENDING_COVERAGE);
286 const Transform& tr = hw->getTransform();
287 Rect transformedBounds(computeBounds());
288 transformedBounds = tr.transform(transformedBounds);
290 // scaling is already applied in transformedBounds
291 layer.setFrame(transformedBounds);
292 layer.setCrop(transformedBounds.getBounds());
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));
304 void LayerBase::setAcquireFence(const sp<const DisplayDevice>& hw,
305 HWComposer::HWCLayerInterface& layer) {
306 layer.setAcquireFenceFd(-1);
309 void LayerBase::onLayerDisplayed(const sp<const DisplayDevice>& hw,
310 HWComposer::HWCLayerInterface* layer) {
312 layer->onDisplayed();
316 void LayerBase::setFiltering(bool filtering)
318 mFiltering = filtering;
321 bool LayerBase::getFiltering() const
326 bool LayerBase::isVisible() const {
327 const Layer::State& s(mDrawingState);
328 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha;
331 void LayerBase::draw(const sp<const DisplayDevice>& hw, const Region& clip) const
336 void LayerBase::draw(const sp<const DisplayDevice>& hw)
338 onDraw( hw, Region(hw->bounds()) );
341 void LayerBase::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
342 GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) const
344 const uint32_t fbHeight = hw->getHeight();
345 glColor4f(red,green,blue,alpha);
347 glDisable(GL_TEXTURE_EXTERNAL_OES);
348 glDisable(GL_TEXTURE_2D);
352 computeGeometry(hw, &mesh);
354 glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
355 glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
358 void LayerBase::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const
360 clearWithOpenGL(hw, clip, 0,0,0,0);
363 void LayerBase::drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const
365 const uint32_t fbHeight = hw->getHeight();
366 const State& s(drawingState());
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);
374 glColor4f(1, 1, 1, alpha);
377 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
378 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
380 glColor4f(1, 1, 1, 1);
381 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
384 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
391 computeGeometry(hw, &mesh);
393 // TODO: we probably want to generate the texture coords with the mesh
394 // here we assume that we only have 4 vertices
401 Rect win(s.active.w, s.active.h);
402 if (!s.active.crop.isEmpty()) {
403 win.intersect(s.active.crop, &win);
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);
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;
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());
429 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
433 void LayerBase::dump(String8& result, char* buffer, size_t SIZE) const
435 const Layer::State& s(drawingState());
437 snprintf(buffer, SIZE,
439 getTypeId(), this, getName().string());
440 result.append(buffer);
442 s.transparentRegion.dump(result, "transparentRegion");
443 visibleRegion.dump(result, "visibleRegion");
445 snprintf(buffer, SIZE,
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,
455 s.transform[0][0], s.transform[0][1],
456 s.transform[1][0], s.transform[1][1]);
457 result.append(buffer);
460 void LayerBase::shortDump(String8& result, char* scratch, size_t size) const {
461 LayerBase::dump(result, scratch, size);
464 void LayerBase::dumpStats(String8& result, char* scratch, size_t SIZE) const {
467 void LayerBase::clearStats() {
470 sp<LayerBaseClient> LayerBase::getLayerBaseClient() const {
474 sp<Layer> LayerBase::getLayer() const {
478 // ---------------------------------------------------------------------------
480 int32_t LayerBaseClient::sIdentity = 1;
482 LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger,
483 const sp<Client>& client)
484 : LayerBase(flinger),
487 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
491 LayerBaseClient::~LayerBaseClient()
493 sp<Client> c(mClientRef.promote());
495 c->detachLayer(this);
499 sp<ISurface> LayerBaseClient::createSurface()
501 class BSurface : public BnSurface, public LayerCleaner {
502 virtual sp<ISurfaceTexture> getSurfaceTexture() const { return 0; }
504 BSurface(const sp<SurfaceFlinger>& flinger,
505 const sp<LayerBaseClient>& layer)
506 : LayerCleaner(flinger, layer) { }
508 sp<ISurface> sur(new BSurface(mFlinger, this));
512 sp<ISurface> LayerBaseClient::getSurface()
515 Mutex::Autolock _l(mLock);
517 LOG_ALWAYS_FATAL_IF(mHasSurface,
518 "LayerBaseClient::getSurface() has already been called");
522 mClientSurfaceBinder = s->asBinder();
526 wp<IBinder> LayerBaseClient::getSurfaceBinder() const {
527 return mClientSurfaceBinder;
530 wp<IBinder> LayerBaseClient::getSurfaceTextureBinder() const {
534 void LayerBaseClient::dump(String8& result, char* buffer, size_t SIZE) const
536 LayerBase::dump(result, buffer, SIZE);
538 sp<Client> client(mClientRef.promote());
539 snprintf(buffer, SIZE,
540 " client=%p, identity=%u\n",
541 client.get(), getIdentity());
543 result.append(buffer);
547 void LayerBaseClient::shortDump(String8& result, char* scratch, size_t size) const
549 LayerBaseClient::dump(result, scratch, size);
552 // ---------------------------------------------------------------------------
554 LayerBaseClient::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
555 const sp<LayerBaseClient>& layer)
556 : mFlinger(flinger), mLayer(layer) {
559 LayerBaseClient::LayerCleaner::~LayerCleaner() {
560 // destroy client resources
561 mFlinger->onLayerDestroyed(mLayer);
564 // ---------------------------------------------------------------------------
566 }; // namespace android