blob: d75dddd2b423e0b7a6c47a438b02bb91fe48ec43 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <utils/Errors.h>
22#include <utils/Log.h>
Mathias Agopian310f8da2009-05-22 01:27:01 -070023#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025
26#include <GLES/gl.h>
27#include <GLES/glext.h>
28
29#include <hardware/hardware.h>
30
31#include "clz.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070032#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include "LayerBase.h"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070034#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include "SurfaceFlinger.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070036#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038namespace android {
39
40// ---------------------------------------------------------------------------
41
Mathias Agopianf6679fc2010-08-10 18:09:09 -070042int32_t LayerBase::sSequence = 1;
43
Mathias Agopian3ee454a2012-08-27 16:28:24 -070044LayerBase::LayerBase(SurfaceFlinger* flinger)
45 : contentDirty(false),
Mathias Agopianf6679fc2010-08-10 18:09:09 -070046 sequence(uint32_t(android_atomic_inc(&sSequence))),
Mathias Agopiana67932f2011-04-20 14:20:59 -070047 mFlinger(flinger), mFiltering(false),
Mathias Agopiana2f4e562012-04-15 23:34:59 -070048 mNeedsFiltering(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049 mTransactionFlags(0),
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080050 mPremultipliedAlpha(true), mName("unnamed"), mDebug(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052}
53
54LayerBase::~LayerBase()
55{
56}
57
Mathias Agopiand1296592010-03-09 19:17:47 -080058void LayerBase::setName(const String8& name) {
59 mName = name;
60}
61
62String8 LayerBase::getName() const {
63 return mName;
64}
65
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
67{
68 uint32_t layerFlags = 0;
Mathias Agopian3165cc22012-08-08 19:42:09 -070069 if (flags & ISurfaceComposerClient::eHidden)
70 layerFlags = layer_state_t::eLayerHidden;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
Mathias Agopian3165cc22012-08-08 19:42:09 -070072 if (flags & ISurfaceComposerClient::eNonPremultiplied)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 mPremultipliedAlpha = false;
74
Mathias Agopian93ffb862012-05-16 17:07:49 -070075 mCurrentState.active.w = w;
76 mCurrentState.active.h = h;
77 mCurrentState.active.crop.makeInvalid();
78 mCurrentState.z = 0;
79 mCurrentState.alpha = 0xFF;
Mathias Agopian87855782012-07-24 21:41:09 -070080 mCurrentState.layerStack = 0;
Mathias Agopian93ffb862012-05-16 17:07:49 -070081 mCurrentState.flags = layerFlags;
82 mCurrentState.sequence = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 mCurrentState.transform.set(0, 0);
Mathias Agopian93ffb862012-05-16 17:07:49 -070084 mCurrentState.requested = mCurrentState.active;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085
86 // drawing state & current state are identical
87 mDrawingState = mCurrentState;
88}
89
Mathias Agopianeba8c682012-09-19 23:14:45 -070090bool LayerBase::needsFiltering(const sp<const DisplayDevice>& hw) const {
91 return mNeedsFiltering || hw->needsFiltering();
92}
93
Mathias Agopianba6be542009-09-29 22:32:36 -070094void LayerBase::commitTransaction() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 mDrawingState = mCurrentState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096}
97void 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}
103bool LayerBase::requestTransaction() {
104 int32_t old = setTransactionFlags(eTransactionNeeded);
105 return ((old & eTransactionNeeded) == 0);
106}
107uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
108 return android_atomic_and(~flags, &mTransactionFlags) & flags;
109}
110uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
111 return android_atomic_or(flags, &mTransactionFlags);
112}
113
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700114bool LayerBase::setPosition(float x, float y) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 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}
122bool 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}
130bool LayerBase::setSize(uint32_t w, uint32_t h) {
Mathias Agopian93ffb862012-05-16 17:07:49 -0700131 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 return false;
Mathias Agopian93ffb862012-05-16 17:07:49 -0700133 mCurrentState.requested.w = w;
134 mCurrentState.requested.h = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 requestTransaction();
136 return true;
137}
138bool 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}
146bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 mCurrentState.sequence++;
148 mCurrentState.transform.set(
149 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
150 requestTransaction();
151 return true;
152}
153bool LayerBase::setTransparentRegionHint(const Region& transparent) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 mCurrentState.sequence++;
155 mCurrentState.transparentRegion = transparent;
156 requestTransaction();
157 return true;
158}
159bool 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}
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700168bool LayerBase::setCrop(const Rect& crop) {
Mathias Agopianb30c4152012-05-16 18:21:32 -0700169 if (mCurrentState.requested.crop == crop)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700170 return false;
171 mCurrentState.sequence++;
Mathias Agopianb30c4152012-05-16 18:21:32 -0700172 mCurrentState.requested.crop = crop;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700173 requestTransaction();
174 return true;
175}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176
Mathias Agopian87855782012-07-24 21:41:09 -0700177bool 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186void LayerBase::setVisibleRegion(const Region& visibleRegion) {
187 // always called from main thread
Mathias Agopian4fec8732012-06-29 14:12:52 -0700188 this->visibleRegion = visibleRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189}
190
191void LayerBase::setCoveredRegion(const Region& coveredRegion) {
192 // always called from main thread
Mathias Agopian4fec8732012-06-29 14:12:52 -0700193 this->coveredRegion = coveredRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194}
195
196uint32_t LayerBase::doTransaction(uint32_t flags)
197{
198 const Layer::State& front(drawingState());
199 const Layer::State& temp(currentState());
200
Mathias Agopian05cec9d2012-05-23 14:35:49 -0700201 // 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 {
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700205 Layer::State& editTemp(currentState());
Mathias Agopianb30c4152012-05-16 18:21:32 -0700206 editTemp.active = temp.requested;
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700207 }
Mathias Agopian05cec9d2012-05-23 14:35:49 -0700208
Mathias Agopianb30c4152012-05-16 18:21:32 -0700209 if (front.active != temp.active) {
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700210 // invalidate and recompute the visible regions if needed
211 flags |= Layer::eVisibleRegion;
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700212 }
213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 if (temp.sequence != front.sequence) {
215 // invalidate and recompute the visible regions if needed
216 flags |= eVisibleRegion;
217 this->contentDirty = true;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700218
Mathias Agopian733189d2010-12-02 21:32:29 -0800219 // 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));
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700223 }
224
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225 // Commit the transaction
Mathias Agopianba6be542009-09-29 22:32:36 -0700226 commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 return flags;
228}
229
Mathias Agopian42977342012-08-05 00:40:46 -0700230void LayerBase::computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231{
232 const Layer::State& s(drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700233 const Transform tr(hw->getTransform() * s.transform);
234 const uint32_t hw_h = hw->getHeight();
Mathias Agopian93ffb862012-05-16 17:07:49 -0700235 Rect win(s.active.w, s.active.h);
Mathias Agopiana046dd92012-09-24 22:01:01 -0700236 if (!s.active.crop.isEmpty()) {
237 win.intersect(s.active.crop, &win);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700238 }
Mathias Agopian4fec8732012-06-29 14:12:52 -0700239 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];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248}
249
Mathias Agopian4fec8732012-06-29 14:12:52 -0700250Rect LayerBase::computeBounds() const {
251 const Layer::State& s(drawingState());
Mathias Agopian4fec8732012-06-29 14:12:52 -0700252 Rect win(s.active.w, s.active.h);
Mathias Agopiana046dd92012-09-24 22:01:01 -0700253 if (!s.active.crop.isEmpty()) {
254 win.intersect(s.active.crop, &win);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700255 }
256 return s.transform.transform(win);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257}
258
Mathias Agopian4fec8732012-06-29 14:12:52 -0700259Region LayerBase::latchBuffer(bool& recomputeVisibleRegions) {
260 Region result;
261 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262}
263
Mathias Agopian4fec8732012-06-29 14:12:52 -0700264void LayerBase::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700265 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700266 HWComposer::HWCLayerInterface& layer)
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700267{
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700268 layer.setDefaultState();
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700269
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) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700275 layer.setTransform(0);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700276 } else {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700277 layer.setTransform(finalTransform);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700278 }
279
280 if (!isOpaque()) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700281 layer.setBlending(mPremultipliedAlpha ?
282 HWC_BLENDING_PREMULT :
283 HWC_BLENDING_COVERAGE);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700284 }
285
Mathias Agopian42977342012-08-05 00:40:46 -0700286 const Transform& tr = hw->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700287 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());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700293}
294
Mathias Agopian42977342012-08-05 00:40:46 -0700295void LayerBase::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700296 HWComposer::HWCLayerInterface& layer) {
Mathias Agopianc3973602012-08-31 17:51:25 -0700297 // 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));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700302}
303
Mathias Agopian42977342012-08-05 00:40:46 -0700304void LayerBase::setAcquireFence(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700305 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700306 layer.setAcquireFenceFd(-1);
307}
308
Mathias Agopianc3973602012-08-31 17:51:25 -0700309void LayerBase::onLayerDisplayed(const sp<const DisplayDevice>& hw,
310 HWComposer::HWCLayerInterface* layer) {
311 if (layer) {
312 layer->onDisplayed();
313 }
314}
315
Mathias Agopiana67932f2011-04-20 14:20:59 -0700316void LayerBase::setFiltering(bool filtering)
317{
318 mFiltering = filtering;
319}
320
321bool LayerBase::getFiltering() const
322{
323 return mFiltering;
324}
325
Mathias Agopianda27af92012-09-13 18:17:13 -0700326bool LayerBase::isVisible() const {
327 const Layer::State& s(mDrawingState);
328 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha;
329}
330
Mathias Agopian42977342012-08-05 00:40:46 -0700331void LayerBase::draw(const sp<const DisplayDevice>& hw, const Region& clip) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332{
Mathias Agopian1b031492012-06-20 17:51:20 -0700333 onDraw(hw, clip);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334}
335
Mathias Agopian42977342012-08-05 00:40:46 -0700336void LayerBase::draw(const sp<const DisplayDevice>& hw)
Mathias Agopian74c40c02010-09-29 13:02:36 -0700337{
Mathias Agopian42977342012-08-05 00:40:46 -0700338 onDraw( hw, Region(hw->bounds()) );
Mathias Agopian74c40c02010-09-29 13:02:36 -0700339}
340
Mathias Agopian42977342012-08-05 00:40:46 -0700341void LayerBase::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
Mathias Agopian1b031492012-06-20 17:51:20 -0700342 GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343{
Mathias Agopian42977342012-08-05 00:40:46 -0700344 const uint32_t fbHeight = hw->getHeight();
Mathias Agopian010fccb2010-05-26 22:26:12 -0700345 glColor4f(red,green,blue,alpha);
Mathias Agopian0a917752010-06-14 21:20:00 -0700346
Mathias Agopianc492e672011-10-18 14:49:27 -0700347 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700348 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349 glDisable(GL_BLEND);
Mathias Agopian20f68782009-05-11 00:03:41 -0700350
Mathias Agopian4fec8732012-06-29 14:12:52 -0700351 LayerMesh mesh;
352 computeGeometry(hw, &mesh);
353
354 glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
355 glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356}
357
Mathias Agopian42977342012-08-05 00:40:46 -0700358void LayerBase::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700359{
Mathias Agopian1b031492012-06-20 17:51:20 -0700360 clearWithOpenGL(hw, clip, 0,0,0,0);
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700361}
362
Mathias Agopian42977342012-08-05 00:40:46 -0700363void LayerBase::drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364{
Mathias Agopian42977342012-08-05 00:40:46 -0700365 const uint32_t fbHeight = hw->getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366 const State& s(drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367
Mathias Agopian49753262010-04-12 15:34:55 -0700368 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
Glenn Kasten99ed2242011-12-15 09:51:17 -0800369 if (CC_UNLIKELY(s.alpha < 0xFF)) {
Mathias Agopian78fd5012010-04-20 14:51:04 -0700370 const GLfloat alpha = s.alpha * (1.0f/255.0f);
Mathias Agopian49753262010-04-12 15:34:55 -0700371 if (mPremultipliedAlpha) {
372 glColor4f(alpha, alpha, alpha, alpha);
373 } else {
374 glColor4f(1, 1, 1, alpha);
375 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 glEnable(GL_BLEND);
377 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopian49753262010-04-12 15:34:55 -0700378 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 } else {
Mathias Agopian78fd5012010-04-20 14:51:04 -0700380 glColor4f(1, 1, 1, 1);
Mathias Agopian49753262010-04-12 15:34:55 -0700381 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700382 if (!isOpaque()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 glEnable(GL_BLEND);
384 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
385 } else {
386 glDisable(GL_BLEND);
387 }
388 }
389
Mathias Agopian4fec8732012-06-29 14:12:52 -0700390 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
Mathias Agopianb661d662010-08-19 17:01:19 -0700396 struct TexCoords {
397 GLfloat u;
398 GLfloat v;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700399 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400
Mathias Agopiana046dd92012-09-24 22:01:01 -0700401 Rect win(s.active.w, s.active.h);
Mathias Agopian93ffb862012-05-16 17:07:49 -0700402 if (!s.active.crop.isEmpty()) {
Mathias Agopiana046dd92012-09-24 22:01:01 -0700403 win.intersect(s.active.crop, &win);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700404 }
Mathias Agopiana046dd92012-09-24 22:01:01 -0700405
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);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700410
Mathias Agopianb661d662010-08-19 17:01:19 -0700411 TexCoords texCoords[4];
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700412 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 }
Mathias Agopian78fd5012010-04-20 14:51:04 -0700423
424 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian78fd5012010-04-20 14:51:04 -0700425 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700426 glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
427 glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
Mathias Agopian78fd5012010-04-20 14:51:04 -0700428
Mathias Agopian78fd5012010-04-20 14:51:04 -0700429 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -0700430 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431}
432
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700433void LayerBase::dump(String8& result, char* buffer, size_t SIZE) const
434{
435 const Layer::State& s(drawingState());
Mathias Agopianc95dbdc2012-02-05 00:19:27 -0800436
437 snprintf(buffer, SIZE,
438 "+ %s %p (%s)\n",
439 getTypeId(), this, getName().string());
440 result.append(buffer);
441
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800442 s.transparentRegion.dump(result, "transparentRegion");
Mathias Agopian4fec8732012-06-29 14:12:52 -0700443 visibleRegion.dump(result, "visibleRegion");
Mathias Agopianc95dbdc2012-02-05 00:19:27 -0800444
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700445 snprintf(buffer, SIZE,
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700446 " "
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700447 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), crop=(%4d,%4d,%4d,%4d), "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700448 "isOpaque=%1d, needsDithering=%1d, invalidate=%1d, "
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700449 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian791da602012-09-11 20:52:46 -0700450 s.layerStack, s.z, s.transform.tx(), s.transform.ty(), s.active.w, s.active.h,
Mathias Agopian93ffb862012-05-16 17:07:49 -0700451 s.active.crop.left, s.active.crop.top,
452 s.active.crop.right, s.active.crop.bottom,
Mathias Agopiana67932f2011-04-20 14:20:59 -0700453 isOpaque(), needsDithering(), contentDirty,
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700454 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}
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700459
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800460void LayerBase::shortDump(String8& result, char* scratch, size_t size) const {
Mathias Agopian48b888a2011-01-19 16:15:53 -0800461 LayerBase::dump(result, scratch, size);
462}
463
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800464void LayerBase::dumpStats(String8& result, char* scratch, size_t SIZE) const {
465}
466
467void LayerBase::clearStats() {
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800468}
Mathias Agopian48b888a2011-01-19 16:15:53 -0800469
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700470sp<LayerBaseClient> LayerBase::getLayerBaseClient() const {
471 return 0;
472}
473
474sp<Layer> LayerBase::getLayer() const {
475 return 0;
476}
477
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478// ---------------------------------------------------------------------------
479
Mathias Agopian631f3582010-05-25 17:51:34 -0700480int32_t LayerBaseClient::sIdentity = 1;
Mathias Agopian2e123242009-06-23 20:06:46 -0700481
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700482LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700483 const sp<Client>& client)
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700484 : LayerBase(flinger),
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800485 mHasSurface(false),
486 mClientRef(client),
Mathias Agopian948d69f2010-03-08 19:29:09 -0800487 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700489}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491LayerBaseClient::~LayerBaseClient()
492{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700493 sp<Client> c(mClientRef.promote());
Mathias Agopian96f08192010-06-02 23:28:45 -0700494 if (c != 0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700495 c->detachLayer(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496 }
497}
498
Mathias Agopiana67932f2011-04-20 14:20:59 -0700499sp<ISurface> LayerBaseClient::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700501 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
512sp<ISurface> LayerBaseClient::getSurface()
513{
514 sp<ISurface> s;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700515 Mutex::Autolock _l(mLock);
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800516
517 LOG_ALWAYS_FATAL_IF(mHasSurface,
518 "LayerBaseClient::getSurface() has already been called");
519
520 mHasSurface = true;
521 s = createSurface();
522 mClientSurfaceBinder = s->asBinder();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700523 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524}
525
Mathias Agopian0d156122011-01-25 20:17:45 -0800526wp<IBinder> LayerBaseClient::getSurfaceBinder() const {
527 return mClientSurfaceBinder;
528}
529
Jamie Gennis582270d2011-08-17 18:19:00 -0700530wp<IBinder> LayerBaseClient::getSurfaceTextureBinder() const {
531 return 0;
532}
533
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700534void LayerBaseClient::dump(String8& result, char* buffer, size_t SIZE) const
535{
536 LayerBase::dump(result, buffer, SIZE);
537
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700538 sp<Client> client(mClientRef.promote());
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700539 snprintf(buffer, SIZE,
Mathias Agopian96f08192010-06-02 23:28:45 -0700540 " client=%p, identity=%u\n",
Mathias Agopian96f08192010-06-02 23:28:45 -0700541 client.get(), getIdentity());
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700542
543 result.append(buffer);
544}
545
Mathias Agopian48b888a2011-01-19 16:15:53 -0800546
547void LayerBaseClient::shortDump(String8& result, char* scratch, size_t size) const
548{
549 LayerBaseClient::dump(result, scratch, size);
550}
551
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700552// ---------------------------------------------------------------------------
553
Mathias Agopiana67932f2011-04-20 14:20:59 -0700554LayerBaseClient::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
555 const sp<LayerBaseClient>& layer)
556 : mFlinger(flinger), mLayer(layer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700557}
558
Mathias Agopiana67932f2011-04-20 14:20:59 -0700559LayerBaseClient::LayerCleaner::~LayerCleaner() {
Mathias Agopian9a112062009-04-17 19:36:26 -0700560 // destroy client resources
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700561 mFlinger->onLayerDestroyed(mLayer);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700562}
563
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800564// ---------------------------------------------------------------------------
565
566}; // namespace android