blob: 5ae0ee0e60e28d5d0be6ec036251ac1f10483e95 [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 const Rect& crop(s.active.crop);
Mathias Agopian93ffb862012-05-16 17:07:49 -0700236 Rect win(s.active.w, s.active.h);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700237 if (!crop.isEmpty()) {
238 win.intersect(crop, &win);
239 }
Mathias Agopian4fec8732012-06-29 14:12:52 -0700240 if (mesh) {
241 tr.transform(mesh->mVertices[0], win.left, win.top);
242 tr.transform(mesh->mVertices[1], win.left, win.bottom);
243 tr.transform(mesh->mVertices[2], win.right, win.bottom);
244 tr.transform(mesh->mVertices[3], win.right, win.top);
245 for (size_t i=0 ; i<4 ; i++) {
246 mesh->mVertices[i][1] = hw_h - mesh->mVertices[i][1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249}
250
Mathias Agopian4fec8732012-06-29 14:12:52 -0700251Rect LayerBase::computeBounds() const {
252 const Layer::State& s(drawingState());
253 const Rect& crop(s.active.crop);
254 Rect win(s.active.w, s.active.h);
255 if (!crop.isEmpty()) {
256 win.intersect(crop, &win);
257 }
258 return s.transform.transform(win);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259}
260
Mathias Agopian4fec8732012-06-29 14:12:52 -0700261Region LayerBase::latchBuffer(bool& recomputeVisibleRegions) {
262 Region result;
263 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264}
265
Mathias Agopian4fec8732012-06-29 14:12:52 -0700266void LayerBase::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700267 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700268 HWComposer::HWCLayerInterface& layer)
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700269{
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700270 layer.setDefaultState();
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700271
272 // this gives us only the "orientation" component of the transform
273 const State& s(drawingState());
274 const uint32_t finalTransform = s.transform.getOrientation();
275 // we can only handle simple transformation
276 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700277 layer.setTransform(0);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700278 } else {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700279 layer.setTransform(finalTransform);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700280 }
281
282 if (!isOpaque()) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700283 layer.setBlending(mPremultipliedAlpha ?
284 HWC_BLENDING_PREMULT :
285 HWC_BLENDING_COVERAGE);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700286 }
287
Mathias Agopian42977342012-08-05 00:40:46 -0700288 const Transform& tr = hw->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700289 Rect transformedBounds(computeBounds());
290 transformedBounds = tr.transform(transformedBounds);
291
292 // scaling is already applied in transformedBounds
293 layer.setFrame(transformedBounds);
294 layer.setCrop(transformedBounds.getBounds());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700295}
296
Mathias Agopian42977342012-08-05 00:40:46 -0700297void LayerBase::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700298 HWComposer::HWCLayerInterface& layer) {
Mathias Agopianc3973602012-08-31 17:51:25 -0700299 // we have to set the visible region on every frame because
300 // we currently free it during onLayerDisplayed(), which is called
301 // after HWComposer::commit() -- every frame.
302 const Transform& tr = hw->getTransform();
303 layer.setVisibleRegionScreen(tr.transform(visibleRegion));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700304}
305
Mathias Agopian42977342012-08-05 00:40:46 -0700306void LayerBase::setAcquireFence(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700307 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700308 layer.setAcquireFenceFd(-1);
309}
310
Mathias Agopianc3973602012-08-31 17:51:25 -0700311void LayerBase::onLayerDisplayed(const sp<const DisplayDevice>& hw,
312 HWComposer::HWCLayerInterface* layer) {
313 if (layer) {
314 layer->onDisplayed();
315 }
316}
317
Mathias Agopiana67932f2011-04-20 14:20:59 -0700318void LayerBase::setFiltering(bool filtering)
319{
320 mFiltering = filtering;
321}
322
323bool LayerBase::getFiltering() const
324{
325 return mFiltering;
326}
327
Mathias Agopianda27af92012-09-13 18:17:13 -0700328bool LayerBase::isVisible() const {
329 const Layer::State& s(mDrawingState);
330 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha;
331}
332
Mathias Agopian42977342012-08-05 00:40:46 -0700333void LayerBase::draw(const sp<const DisplayDevice>& hw, const Region& clip) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334{
Mathias Agopian1b031492012-06-20 17:51:20 -0700335 onDraw(hw, clip);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336}
337
Mathias Agopian42977342012-08-05 00:40:46 -0700338void LayerBase::draw(const sp<const DisplayDevice>& hw)
Mathias Agopian74c40c02010-09-29 13:02:36 -0700339{
Mathias Agopian42977342012-08-05 00:40:46 -0700340 onDraw( hw, Region(hw->bounds()) );
Mathias Agopian74c40c02010-09-29 13:02:36 -0700341}
342
Mathias Agopian42977342012-08-05 00:40:46 -0700343void LayerBase::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
Mathias Agopian1b031492012-06-20 17:51:20 -0700344 GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345{
Mathias Agopian42977342012-08-05 00:40:46 -0700346 const uint32_t fbHeight = hw->getHeight();
Mathias Agopian010fccb2010-05-26 22:26:12 -0700347 glColor4f(red,green,blue,alpha);
Mathias Agopian0a917752010-06-14 21:20:00 -0700348
Mathias Agopianc492e672011-10-18 14:49:27 -0700349 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700350 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 glDisable(GL_BLEND);
Mathias Agopian20f68782009-05-11 00:03:41 -0700352
Mathias Agopian4fec8732012-06-29 14:12:52 -0700353 LayerMesh mesh;
354 computeGeometry(hw, &mesh);
355
356 glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
357 glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358}
359
Mathias Agopian42977342012-08-05 00:40:46 -0700360void LayerBase::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700361{
Mathias Agopian1b031492012-06-20 17:51:20 -0700362 clearWithOpenGL(hw, clip, 0,0,0,0);
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700363}
364
Mathias Agopian42977342012-08-05 00:40:46 -0700365void LayerBase::drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366{
Mathias Agopian42977342012-08-05 00:40:46 -0700367 const uint32_t fbHeight = hw->getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368 const State& s(drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369
Mathias Agopian49753262010-04-12 15:34:55 -0700370 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
Glenn Kasten99ed2242011-12-15 09:51:17 -0800371 if (CC_UNLIKELY(s.alpha < 0xFF)) {
Mathias Agopian78fd5012010-04-20 14:51:04 -0700372 const GLfloat alpha = s.alpha * (1.0f/255.0f);
Mathias Agopian49753262010-04-12 15:34:55 -0700373 if (mPremultipliedAlpha) {
374 glColor4f(alpha, alpha, alpha, alpha);
375 } else {
376 glColor4f(1, 1, 1, alpha);
377 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 glEnable(GL_BLEND);
379 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopian49753262010-04-12 15:34:55 -0700380 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381 } else {
Mathias Agopian78fd5012010-04-20 14:51:04 -0700382 glColor4f(1, 1, 1, 1);
Mathias Agopian49753262010-04-12 15:34:55 -0700383 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700384 if (!isOpaque()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385 glEnable(GL_BLEND);
386 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
387 } else {
388 glDisable(GL_BLEND);
389 }
390 }
391
Mathias Agopian4fec8732012-06-29 14:12:52 -0700392 LayerMesh mesh;
393 computeGeometry(hw, &mesh);
394
395 // TODO: we probably want to generate the texture coords with the mesh
396 // here we assume that we only have 4 vertices
397
Mathias Agopianb661d662010-08-19 17:01:19 -0700398 struct TexCoords {
399 GLfloat u;
400 GLfloat v;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700401 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402
Mathias Agopian93ffb862012-05-16 17:07:49 -0700403 Rect crop(s.active.w, s.active.h);
404 if (!s.active.crop.isEmpty()) {
405 crop = s.active.crop;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700406 }
Mathias Agopian4fec8732012-06-29 14:12:52 -0700407 GLfloat left = GLfloat(crop.left) / GLfloat(s.active.w);
408 GLfloat top = GLfloat(crop.top) / GLfloat(s.active.h);
409 GLfloat right = GLfloat(crop.right) / GLfloat(s.active.w);
Mathias Agopian93ffb862012-05-16 17:07:49 -0700410 GLfloat bottom = GLfloat(crop.bottom) / GLfloat(s.active.h);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700411
Mathias Agopianb661d662010-08-19 17:01:19 -0700412 TexCoords texCoords[4];
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700413 texCoords[0].u = left;
414 texCoords[0].v = top;
415 texCoords[1].u = left;
416 texCoords[1].v = bottom;
417 texCoords[2].u = right;
418 texCoords[2].v = bottom;
419 texCoords[3].u = right;
420 texCoords[3].v = top;
421 for (int i = 0; i < 4; i++) {
422 texCoords[i].v = 1.0f - texCoords[i].v;
423 }
Mathias Agopian78fd5012010-04-20 14:51:04 -0700424
425 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian78fd5012010-04-20 14:51:04 -0700426 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700427 glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
428 glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
Mathias Agopian78fd5012010-04-20 14:51:04 -0700429
Mathias Agopian78fd5012010-04-20 14:51:04 -0700430 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -0700431 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432}
433
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700434void LayerBase::dump(String8& result, char* buffer, size_t SIZE) const
435{
436 const Layer::State& s(drawingState());
Mathias Agopianc95dbdc2012-02-05 00:19:27 -0800437
438 snprintf(buffer, SIZE,
439 "+ %s %p (%s)\n",
440 getTypeId(), this, getName().string());
441 result.append(buffer);
442
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800443 s.transparentRegion.dump(result, "transparentRegion");
Mathias Agopian4fec8732012-06-29 14:12:52 -0700444 visibleRegion.dump(result, "visibleRegion");
Mathias Agopianc95dbdc2012-02-05 00:19:27 -0800445
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700446 snprintf(buffer, SIZE,
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700447 " "
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700448 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), crop=(%4d,%4d,%4d,%4d), "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700449 "isOpaque=%1d, needsDithering=%1d, invalidate=%1d, "
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700450 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian791da602012-09-11 20:52:46 -0700451 s.layerStack, s.z, s.transform.tx(), s.transform.ty(), s.active.w, s.active.h,
Mathias Agopian93ffb862012-05-16 17:07:49 -0700452 s.active.crop.left, s.active.crop.top,
453 s.active.crop.right, s.active.crop.bottom,
Mathias Agopiana67932f2011-04-20 14:20:59 -0700454 isOpaque(), needsDithering(), contentDirty,
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700455 s.alpha, s.flags,
456 s.transform[0][0], s.transform[0][1],
457 s.transform[1][0], s.transform[1][1]);
458 result.append(buffer);
459}
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700460
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800461void LayerBase::shortDump(String8& result, char* scratch, size_t size) const {
Mathias Agopian48b888a2011-01-19 16:15:53 -0800462 LayerBase::dump(result, scratch, size);
463}
464
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800465void LayerBase::dumpStats(String8& result, char* scratch, size_t SIZE) const {
466}
467
468void LayerBase::clearStats() {
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800469}
Mathias Agopian48b888a2011-01-19 16:15:53 -0800470
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700471sp<LayerBaseClient> LayerBase::getLayerBaseClient() const {
472 return 0;
473}
474
475sp<Layer> LayerBase::getLayer() const {
476 return 0;
477}
478
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479// ---------------------------------------------------------------------------
480
Mathias Agopian631f3582010-05-25 17:51:34 -0700481int32_t LayerBaseClient::sIdentity = 1;
Mathias Agopian2e123242009-06-23 20:06:46 -0700482
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700483LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700484 const sp<Client>& client)
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700485 : LayerBase(flinger),
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800486 mHasSurface(false),
487 mClientRef(client),
Mathias Agopian948d69f2010-03-08 19:29:09 -0800488 mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700490}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492LayerBaseClient::~LayerBaseClient()
493{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700494 sp<Client> c(mClientRef.promote());
Mathias Agopian96f08192010-06-02 23:28:45 -0700495 if (c != 0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700496 c->detachLayer(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497 }
498}
499
Mathias Agopiana67932f2011-04-20 14:20:59 -0700500sp<ISurface> LayerBaseClient::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700502 class BSurface : public BnSurface, public LayerCleaner {
503 virtual sp<ISurfaceTexture> getSurfaceTexture() const { return 0; }
504 public:
505 BSurface(const sp<SurfaceFlinger>& flinger,
506 const sp<LayerBaseClient>& layer)
507 : LayerCleaner(flinger, layer) { }
508 };
509 sp<ISurface> sur(new BSurface(mFlinger, this));
510 return sur;
511}
512
513sp<ISurface> LayerBaseClient::getSurface()
514{
515 sp<ISurface> s;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700516 Mutex::Autolock _l(mLock);
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800517
518 LOG_ALWAYS_FATAL_IF(mHasSurface,
519 "LayerBaseClient::getSurface() has already been called");
520
521 mHasSurface = true;
522 s = createSurface();
523 mClientSurfaceBinder = s->asBinder();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700524 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800525}
526
Mathias Agopian0d156122011-01-25 20:17:45 -0800527wp<IBinder> LayerBaseClient::getSurfaceBinder() const {
528 return mClientSurfaceBinder;
529}
530
Jamie Gennis582270d2011-08-17 18:19:00 -0700531wp<IBinder> LayerBaseClient::getSurfaceTextureBinder() const {
532 return 0;
533}
534
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700535void LayerBaseClient::dump(String8& result, char* buffer, size_t SIZE) const
536{
537 LayerBase::dump(result, buffer, SIZE);
538
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700539 sp<Client> client(mClientRef.promote());
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700540 snprintf(buffer, SIZE,
Mathias Agopian96f08192010-06-02 23:28:45 -0700541 " client=%p, identity=%u\n",
Mathias Agopian96f08192010-06-02 23:28:45 -0700542 client.get(), getIdentity());
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700543
544 result.append(buffer);
545}
546
Mathias Agopian48b888a2011-01-19 16:15:53 -0800547
548void LayerBaseClient::shortDump(String8& result, char* scratch, size_t size) const
549{
550 LayerBaseClient::dump(result, scratch, size);
551}
552
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700553// ---------------------------------------------------------------------------
554
Mathias Agopiana67932f2011-04-20 14:20:59 -0700555LayerBaseClient::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
556 const sp<LayerBaseClient>& layer)
557 : mFlinger(flinger), mLayer(layer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700558}
559
Mathias Agopiana67932f2011-04-20 14:20:59 -0700560LayerBaseClient::LayerCleaner::~LayerCleaner() {
Mathias Agopian9a112062009-04-17 19:36:26 -0700561 // destroy client resources
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700562 mFlinger->onLayerDestroyed(mLayer);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700563}
564
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565// ---------------------------------------------------------------------------
566
567}; // namespace android