blob: 572e7c89131c9f6ec675a9cf6f8539af3b478645 [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>
Mathias Agopiana8bca8d2013-02-27 22:03:19 -080020#include <math.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021
22#include <utils/Errors.h>
23#include <utils/Log.h>
Mathias Agopian310f8da2009-05-22 01:27:01 -070024#include <binder/IPCThreadState.h>
25#include <binder/IServiceManager.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <GLES/gl.h>
28#include <GLES/glext.h>
29
30#include <hardware/hardware.h>
31
32#include "clz.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070033#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include "LayerBase.h"
Mathias Agopian921e6ac2012-07-23 23:11:29 -070035#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include "SurfaceFlinger.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070037#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039namespace android {
40
41// ---------------------------------------------------------------------------
42
Mathias Agopianf6679fc2010-08-10 18:09:09 -070043int32_t LayerBase::sSequence = 1;
44
Mathias Agopian3ee454a2012-08-27 16:28:24 -070045LayerBase::LayerBase(SurfaceFlinger* flinger)
46 : contentDirty(false),
Mathias Agopianf6679fc2010-08-10 18:09:09 -070047 sequence(uint32_t(android_atomic_inc(&sSequence))),
Mathias Agopiana67932f2011-04-20 14:20:59 -070048 mFlinger(flinger), mFiltering(false),
Mathias Agopiana2f4e562012-04-15 23:34:59 -070049 mNeedsFiltering(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050 mTransactionFlags(0),
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080051 mPremultipliedAlpha(true), mName("unnamed"), mDebug(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053}
54
55LayerBase::~LayerBase()
56{
57}
58
Mathias Agopiand1296592010-03-09 19:17:47 -080059void LayerBase::setName(const String8& name) {
60 mName = name;
61}
62
63String8 LayerBase::getName() const {
64 return mName;
65}
66
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067void LayerBase::initStates(uint32_t w, uint32_t h, uint32_t flags)
68{
69 uint32_t layerFlags = 0;
Mathias Agopian3165cc22012-08-08 19:42:09 -070070 if (flags & ISurfaceComposerClient::eHidden)
71 layerFlags = layer_state_t::eLayerHidden;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
Mathias Agopian3165cc22012-08-08 19:42:09 -070073 if (flags & ISurfaceComposerClient::eNonPremultiplied)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 mPremultipliedAlpha = false;
75
Mathias Agopian93ffb862012-05-16 17:07:49 -070076 mCurrentState.active.w = w;
77 mCurrentState.active.h = h;
78 mCurrentState.active.crop.makeInvalid();
79 mCurrentState.z = 0;
80 mCurrentState.alpha = 0xFF;
Mathias Agopian87855782012-07-24 21:41:09 -070081 mCurrentState.layerStack = 0;
Mathias Agopian93ffb862012-05-16 17:07:49 -070082 mCurrentState.flags = layerFlags;
83 mCurrentState.sequence = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 mCurrentState.transform.set(0, 0);
Mathias Agopian93ffb862012-05-16 17:07:49 -070085 mCurrentState.requested = mCurrentState.active;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086
87 // drawing state & current state are identical
88 mDrawingState = mCurrentState;
89}
90
Mathias Agopianeba8c682012-09-19 23:14:45 -070091bool LayerBase::needsFiltering(const sp<const DisplayDevice>& hw) const {
92 return mNeedsFiltering || hw->needsFiltering();
93}
94
Mathias Agopianba6be542009-09-29 22:32:36 -070095void LayerBase::commitTransaction() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 mDrawingState = mCurrentState;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097}
98void LayerBase::forceVisibilityTransaction() {
99 // this can be called without SurfaceFlinger.mStateLock, but if we
100 // can atomically increment the sequence number, it doesn't matter.
101 android_atomic_inc(&mCurrentState.sequence);
102 requestTransaction();
103}
104bool LayerBase::requestTransaction() {
105 int32_t old = setTransactionFlags(eTransactionNeeded);
106 return ((old & eTransactionNeeded) == 0);
107}
108uint32_t LayerBase::getTransactionFlags(uint32_t flags) {
109 return android_atomic_and(~flags, &mTransactionFlags) & flags;
110}
111uint32_t LayerBase::setTransactionFlags(uint32_t flags) {
112 return android_atomic_or(flags, &mTransactionFlags);
113}
114
Mathias Agopian41b6aab2011-08-30 18:51:54 -0700115bool LayerBase::setPosition(float x, float y) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y)
117 return false;
118 mCurrentState.sequence++;
119 mCurrentState.transform.set(x, y);
120 requestTransaction();
121 return true;
122}
123bool LayerBase::setLayer(uint32_t z) {
124 if (mCurrentState.z == z)
125 return false;
126 mCurrentState.sequence++;
127 mCurrentState.z = z;
128 requestTransaction();
129 return true;
130}
131bool LayerBase::setSize(uint32_t w, uint32_t h) {
Mathias Agopian93ffb862012-05-16 17:07:49 -0700132 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 return false;
Mathias Agopian93ffb862012-05-16 17:07:49 -0700134 mCurrentState.requested.w = w;
135 mCurrentState.requested.h = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 requestTransaction();
137 return true;
138}
139bool LayerBase::setAlpha(uint8_t alpha) {
140 if (mCurrentState.alpha == alpha)
141 return false;
142 mCurrentState.sequence++;
143 mCurrentState.alpha = alpha;
144 requestTransaction();
145 return true;
146}
147bool LayerBase::setMatrix(const layer_state_t::matrix22_t& matrix) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 mCurrentState.sequence++;
149 mCurrentState.transform.set(
150 matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy);
151 requestTransaction();
152 return true;
153}
154bool LayerBase::setTransparentRegionHint(const Region& transparent) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 mCurrentState.sequence++;
156 mCurrentState.transparentRegion = transparent;
157 requestTransaction();
158 return true;
159}
160bool LayerBase::setFlags(uint8_t flags, uint8_t mask) {
161 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
162 if (mCurrentState.flags == newFlags)
163 return false;
164 mCurrentState.sequence++;
165 mCurrentState.flags = newFlags;
166 requestTransaction();
167 return true;
168}
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700169bool LayerBase::setCrop(const Rect& crop) {
Mathias Agopianb30c4152012-05-16 18:21:32 -0700170 if (mCurrentState.requested.crop == crop)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700171 return false;
172 mCurrentState.sequence++;
Mathias Agopianb30c4152012-05-16 18:21:32 -0700173 mCurrentState.requested.crop = crop;
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700174 requestTransaction();
175 return true;
176}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177
Mathias Agopian87855782012-07-24 21:41:09 -0700178bool LayerBase::setLayerStack(uint32_t layerStack) {
179 if (mCurrentState.layerStack == layerStack)
180 return false;
181 mCurrentState.sequence++;
182 mCurrentState.layerStack = layerStack;
183 requestTransaction();
184 return true;
185}
186
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187void LayerBase::setVisibleRegion(const Region& visibleRegion) {
188 // always called from main thread
Mathias Agopian4fec8732012-06-29 14:12:52 -0700189 this->visibleRegion = visibleRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190}
191
192void LayerBase::setCoveredRegion(const Region& coveredRegion) {
193 // always called from main thread
Mathias Agopian4fec8732012-06-29 14:12:52 -0700194 this->coveredRegion = coveredRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195}
196
Jesse Halla8026d22012-09-25 13:25:04 -0700197void LayerBase::setVisibleNonTransparentRegion(const Region&
198 setVisibleNonTransparentRegion) {
199 // always called from main thread
200 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
201}
202
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203uint32_t LayerBase::doTransaction(uint32_t flags)
204{
205 const Layer::State& front(drawingState());
206 const Layer::State& temp(currentState());
207
Mathias Agopian05cec9d2012-05-23 14:35:49 -0700208 // always set active to requested, unless we're asked not to
209 // this is used by Layer, which special cases resizes.
210 if (flags & eDontUpdateGeometryState) {
211 } else {
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700212 Layer::State& editTemp(currentState());
Mathias Agopianb30c4152012-05-16 18:21:32 -0700213 editTemp.active = temp.requested;
Mathias Agopian7e4a5872009-09-29 22:39:22 -0700214 }
Mathias Agopian05cec9d2012-05-23 14:35:49 -0700215
Mathias Agopianb30c4152012-05-16 18:21:32 -0700216 if (front.active != temp.active) {
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700217 // invalidate and recompute the visible regions if needed
218 flags |= Layer::eVisibleRegion;
Mathias Agopian6656dbc2009-09-30 12:48:47 -0700219 }
220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 if (temp.sequence != front.sequence) {
222 // invalidate and recompute the visible regions if needed
223 flags |= eVisibleRegion;
224 this->contentDirty = true;
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700225
Mathias Agopian733189d2010-12-02 21:32:29 -0800226 // we may use linear filtering, if the matrix scales us
227 const uint8_t type = temp.transform.getType();
228 mNeedsFiltering = (!temp.transform.preserveRects() ||
229 (type >= Transform::SCALE));
Mathias Agopiana2fe0a22009-09-23 18:34:53 -0700230 }
231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 // Commit the transaction
Mathias Agopianba6be542009-09-29 22:32:36 -0700233 commitTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 return flags;
235}
236
Mathias Agopian42977342012-08-05 00:40:46 -0700237void LayerBase::computeGeometry(const sp<const DisplayDevice>& hw, LayerMesh* mesh) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238{
239 const Layer::State& s(drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700240 const Transform tr(hw->getTransform() * s.transform);
241 const uint32_t hw_h = hw->getHeight();
Mathias Agopian93ffb862012-05-16 17:07:49 -0700242 Rect win(s.active.w, s.active.h);
Mathias Agopiana046dd92012-09-24 22:01:01 -0700243 if (!s.active.crop.isEmpty()) {
244 win.intersect(s.active.crop, &win);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700245 }
Mathias Agopian4fec8732012-06-29 14:12:52 -0700246 if (mesh) {
247 tr.transform(mesh->mVertices[0], win.left, win.top);
248 tr.transform(mesh->mVertices[1], win.left, win.bottom);
249 tr.transform(mesh->mVertices[2], win.right, win.bottom);
250 tr.transform(mesh->mVertices[3], win.right, win.top);
251 for (size_t i=0 ; i<4 ; i++) {
252 mesh->mVertices[i][1] = hw_h - mesh->mVertices[i][1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255}
256
Mathias Agopian4fec8732012-06-29 14:12:52 -0700257Rect LayerBase::computeBounds() const {
258 const Layer::State& s(drawingState());
Mathias Agopian4fec8732012-06-29 14:12:52 -0700259 Rect win(s.active.w, s.active.h);
Mathias Agopiana046dd92012-09-24 22:01:01 -0700260 if (!s.active.crop.isEmpty()) {
261 win.intersect(s.active.crop, &win);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700262 }
Mathias Agopian5219a062013-02-26 16:37:53 -0800263 return win;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264}
265
Mathias Agopian4fec8732012-06-29 14:12:52 -0700266Region LayerBase::latchBuffer(bool& recomputeVisibleRegions) {
267 Region result;
268 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269}
270
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800271
272Rect LayerBase::getContentCrop() const {
273 // regular layers just use their active area as the content crop
274 const State& s(drawingState());
275 return Rect(s.active.w, s.active.h);
276}
277
278uint32_t LayerBase::getContentTransform() const {
279 // regular layers don't have a content transform
280 return 0;
281}
282
283Rect LayerBase::computeCrop(const sp<const DisplayDevice>& hw) const {
284 // the content crop is the area of the content that gets scaled to the
285 // layer's size.
286 Rect crop(getContentCrop());
287
288 // the active.crop is the area of the window that gets cropped, but not
289 // scaled in any ways.
290 const State& s(drawingState());
291 Rect activeCrop(s.active.crop);
292 if (!activeCrop.isEmpty()) {
293 // Transform the window crop to match the buffer coordinate system,
294 // which means using the inverse of the current transform set on the
295 // SurfaceFlingerConsumer.
296 uint32_t invTransform = getContentTransform();
297 int winWidth = s.active.w;
298 int winHeight = s.active.h;
299 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
300 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
301 NATIVE_WINDOW_TRANSFORM_FLIP_H;
302 winWidth = s.active.h;
303 winHeight = s.active.w;
304 }
305 const Rect winCrop = activeCrop.transform(
306 invTransform, s.active.w, s.active.h);
307
308 // the code below essentially performs a scaled intersection
309 // of crop and winCrop
310 float xScale = float(crop.width()) / float(winWidth);
311 float yScale = float(crop.height()) / float(winHeight);
312
313 int insetL = int(ceilf( winCrop.left * xScale));
314 int insetT = int(ceilf( winCrop.top * yScale));
315 int insetR = int(ceilf((winWidth - winCrop.right ) * xScale));
316 int insetB = int(ceilf((winHeight - winCrop.bottom) * yScale));
317
318 crop.left += insetL;
319 crop.top += insetT;
320 crop.right -= insetR;
321 crop.bottom -= insetB;
322 }
323 return crop;
324}
325
Mathias Agopian4fec8732012-06-29 14:12:52 -0700326void LayerBase::setGeometry(
Mathias Agopian42977342012-08-05 00:40:46 -0700327 const sp<const DisplayDevice>& hw,
Mathias Agopian4fec8732012-06-29 14:12:52 -0700328 HWComposer::HWCLayerInterface& layer)
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700329{
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700330 layer.setDefaultState();
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700331
332 // this gives us only the "orientation" component of the transform
333 const State& s(drawingState());
334 const uint32_t finalTransform = s.transform.getOrientation();
335 // we can only handle simple transformation
336 if (finalTransform & Transform::ROT_INVALID) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700337 layer.setTransform(0);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700338 } else {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700339 layer.setTransform(finalTransform);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700340 }
341
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800342 if (!isOpaque() || s.alpha != 0xFF) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700343 layer.setBlending(mPremultipliedAlpha ?
344 HWC_BLENDING_PREMULT :
345 HWC_BLENDING_COVERAGE);
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700346 }
347
Mathias Agopian4fec8732012-06-29 14:12:52 -0700348
Mathias Agopian5219a062013-02-26 16:37:53 -0800349 // apply the layer's transform, followed by the display's global transform
350 // here we're guaranteed that the layer's transform preserves rects
351
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800352 Rect frame(s.transform.transform(computeBounds()));
353 const Transform& tr(hw->getTransform());
354 layer.setFrame(tr.transform(frame));
355 layer.setCrop(computeCrop(hw));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700356}
357
Mathias Agopian42977342012-08-05 00:40:46 -0700358void LayerBase::setPerFrameData(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700359 HWComposer::HWCLayerInterface& layer) {
Mathias Agopianc3973602012-08-31 17:51:25 -0700360 // we have to set the visible region on every frame because
361 // we currently free it during onLayerDisplayed(), which is called
362 // after HWComposer::commit() -- every frame.
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800363 // Apply this display's projection's viewport to the visible region
364 // before giving it to the HWC HAL.
Mathias Agopianc3973602012-08-31 17:51:25 -0700365 const Transform& tr = hw->getTransform();
Mathias Agopianf5f714a2013-02-26 16:54:05 -0800366 Region visible = tr.transform(visibleRegion.intersect(hw->getViewport()));
367 layer.setVisibleRegionScreen(visible);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700368}
369
Mathias Agopian42977342012-08-05 00:40:46 -0700370void LayerBase::setAcquireFence(const sp<const DisplayDevice>& hw,
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700371 HWComposer::HWCLayerInterface& layer) {
Jesse Hallc5c5a142012-07-02 16:49:28 -0700372 layer.setAcquireFenceFd(-1);
373}
374
Mathias Agopianc3973602012-08-31 17:51:25 -0700375void LayerBase::onLayerDisplayed(const sp<const DisplayDevice>& hw,
376 HWComposer::HWCLayerInterface* layer) {
377 if (layer) {
378 layer->onDisplayed();
379 }
380}
381
Mathias Agopiana67932f2011-04-20 14:20:59 -0700382void LayerBase::setFiltering(bool filtering)
383{
384 mFiltering = filtering;
385}
386
387bool LayerBase::getFiltering() const
388{
389 return mFiltering;
390}
391
Mathias Agopianda27af92012-09-13 18:17:13 -0700392bool LayerBase::isVisible() const {
393 const Layer::State& s(mDrawingState);
394 return !(s.flags & layer_state_t::eLayerHidden) && s.alpha;
395}
396
Mathias Agopian42977342012-08-05 00:40:46 -0700397void LayerBase::draw(const sp<const DisplayDevice>& hw, const Region& clip) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398{
Mathias Agopian1b031492012-06-20 17:51:20 -0700399 onDraw(hw, clip);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400}
401
Mathias Agopian42977342012-08-05 00:40:46 -0700402void LayerBase::draw(const sp<const DisplayDevice>& hw)
Mathias Agopian74c40c02010-09-29 13:02:36 -0700403{
Mathias Agopian42977342012-08-05 00:40:46 -0700404 onDraw( hw, Region(hw->bounds()) );
Mathias Agopian74c40c02010-09-29 13:02:36 -0700405}
406
Mathias Agopian42977342012-08-05 00:40:46 -0700407void LayerBase::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
Mathias Agopian1b031492012-06-20 17:51:20 -0700408 GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409{
Mathias Agopian42977342012-08-05 00:40:46 -0700410 const uint32_t fbHeight = hw->getHeight();
Mathias Agopian010fccb2010-05-26 22:26:12 -0700411 glColor4f(red,green,blue,alpha);
Mathias Agopian0a917752010-06-14 21:20:00 -0700412
Mathias Agopianc492e672011-10-18 14:49:27 -0700413 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700414 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 glDisable(GL_BLEND);
Mathias Agopian20f68782009-05-11 00:03:41 -0700416
Mathias Agopian4fec8732012-06-29 14:12:52 -0700417 LayerMesh mesh;
418 computeGeometry(hw, &mesh);
419
420 glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
421 glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422}
423
Mathias Agopian42977342012-08-05 00:40:46 -0700424void LayerBase::clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700425{
Mathias Agopian1b031492012-06-20 17:51:20 -0700426 clearWithOpenGL(hw, clip, 0,0,0,0);
Rebecca Schultz Zavin29aa74c2009-09-01 23:06:45 -0700427}
428
Mathias Agopian42977342012-08-05 00:40:46 -0700429void LayerBase::drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430{
Mathias Agopian42977342012-08-05 00:40:46 -0700431 const uint32_t fbHeight = hw->getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432 const State& s(drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433
Mathias Agopian49753262010-04-12 15:34:55 -0700434 GLenum src = mPremultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
Glenn Kasten99ed2242011-12-15 09:51:17 -0800435 if (CC_UNLIKELY(s.alpha < 0xFF)) {
Mathias Agopian78fd5012010-04-20 14:51:04 -0700436 const GLfloat alpha = s.alpha * (1.0f/255.0f);
Mathias Agopian49753262010-04-12 15:34:55 -0700437 if (mPremultipliedAlpha) {
438 glColor4f(alpha, alpha, alpha, alpha);
439 } else {
440 glColor4f(1, 1, 1, alpha);
441 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442 glEnable(GL_BLEND);
443 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopian49753262010-04-12 15:34:55 -0700444 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 } else {
Mathias Agopian78fd5012010-04-20 14:51:04 -0700446 glColor4f(1, 1, 1, 1);
Mathias Agopian49753262010-04-12 15:34:55 -0700447 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Mathias Agopiana67932f2011-04-20 14:20:59 -0700448 if (!isOpaque()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449 glEnable(GL_BLEND);
450 glBlendFunc(src, GL_ONE_MINUS_SRC_ALPHA);
451 } else {
452 glDisable(GL_BLEND);
453 }
454 }
455
Mathias Agopian4fec8732012-06-29 14:12:52 -0700456 LayerMesh mesh;
457 computeGeometry(hw, &mesh);
458
459 // TODO: we probably want to generate the texture coords with the mesh
460 // here we assume that we only have 4 vertices
461
Mathias Agopianb661d662010-08-19 17:01:19 -0700462 struct TexCoords {
463 GLfloat u;
464 GLfloat v;
Mathias Agopian78fd5012010-04-20 14:51:04 -0700465 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466
Mathias Agopiana046dd92012-09-24 22:01:01 -0700467 Rect win(s.active.w, s.active.h);
Mathias Agopian93ffb862012-05-16 17:07:49 -0700468 if (!s.active.crop.isEmpty()) {
Mathias Agopiana046dd92012-09-24 22:01:01 -0700469 win.intersect(s.active.crop, &win);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700470 }
Mathias Agopiana046dd92012-09-24 22:01:01 -0700471
472 GLfloat left = GLfloat(win.left) / GLfloat(s.active.w);
473 GLfloat top = GLfloat(win.top) / GLfloat(s.active.h);
474 GLfloat right = GLfloat(win.right) / GLfloat(s.active.w);
475 GLfloat bottom = GLfloat(win.bottom) / GLfloat(s.active.h);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700476
Mathias Agopianb661d662010-08-19 17:01:19 -0700477 TexCoords texCoords[4];
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700478 texCoords[0].u = left;
479 texCoords[0].v = top;
480 texCoords[1].u = left;
481 texCoords[1].v = bottom;
482 texCoords[2].u = right;
483 texCoords[2].v = bottom;
484 texCoords[3].u = right;
485 texCoords[3].v = top;
486 for (int i = 0; i < 4; i++) {
487 texCoords[i].v = 1.0f - texCoords[i].v;
488 }
Mathias Agopian78fd5012010-04-20 14:51:04 -0700489
490 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian78fd5012010-04-20 14:51:04 -0700491 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700492 glVertexPointer(2, GL_FLOAT, 0, mesh.getVertices());
493 glDrawArrays(GL_TRIANGLE_FAN, 0, mesh.getVertexCount());
Mathias Agopian78fd5012010-04-20 14:51:04 -0700494
Mathias Agopian78fd5012010-04-20 14:51:04 -0700495 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -0700496 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497}
498
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700499void LayerBase::dump(String8& result, char* buffer, size_t SIZE) const
500{
501 const Layer::State& s(drawingState());
Mathias Agopianc95dbdc2012-02-05 00:19:27 -0800502
503 snprintf(buffer, SIZE,
504 "+ %s %p (%s)\n",
505 getTypeId(), this, getName().string());
506 result.append(buffer);
507
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800508 s.transparentRegion.dump(result, "transparentRegion");
Mathias Agopian4fec8732012-06-29 14:12:52 -0700509 visibleRegion.dump(result, "visibleRegion");
Mathias Agopianc95dbdc2012-02-05 00:19:27 -0800510
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700511 snprintf(buffer, SIZE,
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700512 " "
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700513 "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), crop=(%4d,%4d,%4d,%4d), "
Mathias Agopiana67932f2011-04-20 14:20:59 -0700514 "isOpaque=%1d, needsDithering=%1d, invalidate=%1d, "
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700515 "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n",
Mathias Agopian791da602012-09-11 20:52:46 -0700516 s.layerStack, s.z, s.transform.tx(), s.transform.ty(), s.active.w, s.active.h,
Mathias Agopian93ffb862012-05-16 17:07:49 -0700517 s.active.crop.left, s.active.crop.top,
518 s.active.crop.right, s.active.crop.bottom,
Mathias Agopiana67932f2011-04-20 14:20:59 -0700519 isOpaque(), needsDithering(), contentDirty,
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700520 s.alpha, s.flags,
521 s.transform[0][0], s.transform[0][1],
522 s.transform[1][0], s.transform[1][1]);
523 result.append(buffer);
524}
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700525
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800526void LayerBase::shortDump(String8& result, char* scratch, size_t size) const {
Mathias Agopian48b888a2011-01-19 16:15:53 -0800527 LayerBase::dump(result, scratch, size);
528}
529
Mathias Agopian25e66fc2012-01-28 22:31:55 -0800530void LayerBase::dumpStats(String8& result, char* scratch, size_t SIZE) const {
531}
532
533void LayerBase::clearStats() {
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800534}
Mathias Agopian48b888a2011-01-19 16:15:53 -0800535
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700536sp<LayerBaseClient> LayerBase::getLayerBaseClient() const {
537 return 0;
538}
539
540sp<Layer> LayerBase::getLayer() const {
541 return 0;
542}
543
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544// ---------------------------------------------------------------------------
545
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700546LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger,
Mathias Agopian96f08192010-06-02 23:28:45 -0700547 const sp<Client>& client)
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700548 : LayerBase(flinger),
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800549 mHasSurface(false),
Mathias Agopianac9fa422013-02-11 16:40:36 -0800550 mClientRef(client)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700552}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554LayerBaseClient::~LayerBaseClient()
555{
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700556 sp<Client> c(mClientRef.promote());
Mathias Agopian96f08192010-06-02 23:28:45 -0700557 if (c != 0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700558 c->detachLayer(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559 }
560}
561
Mathias Agopiana67932f2011-04-20 14:20:59 -0700562sp<ISurface> LayerBaseClient::createSurface()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563{
Mathias Agopiana67932f2011-04-20 14:20:59 -0700564 class BSurface : public BnSurface, public LayerCleaner {
Andy McFadden2adaf042012-12-18 09:49:45 -0800565 virtual sp<IGraphicBufferProducer> getSurfaceTexture() const { return 0; }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700566 public:
567 BSurface(const sp<SurfaceFlinger>& flinger,
568 const sp<LayerBaseClient>& layer)
569 : LayerCleaner(flinger, layer) { }
570 };
571 sp<ISurface> sur(new BSurface(mFlinger, this));
572 return sur;
573}
574
575sp<ISurface> LayerBaseClient::getSurface()
576{
577 sp<ISurface> s;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700578 Mutex::Autolock _l(mLock);
Mathias Agopiana1f47b92011-02-15 19:01:06 -0800579
580 LOG_ALWAYS_FATAL_IF(mHasSurface,
581 "LayerBaseClient::getSurface() has already been called");
582
583 mHasSurface = true;
584 s = createSurface();
585 mClientSurfaceBinder = s->asBinder();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700586 return s;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587}
588
Mathias Agopian0d156122011-01-25 20:17:45 -0800589wp<IBinder> LayerBaseClient::getSurfaceBinder() const {
590 return mClientSurfaceBinder;
591}
592
Jamie Gennis582270d2011-08-17 18:19:00 -0700593wp<IBinder> LayerBaseClient::getSurfaceTextureBinder() const {
594 return 0;
595}
596
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700597void LayerBaseClient::dump(String8& result, char* buffer, size_t SIZE) const
598{
599 LayerBase::dump(result, buffer, SIZE);
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700600 sp<Client> client(mClientRef.promote());
Mathias Agopianac9fa422013-02-11 16:40:36 -0800601 snprintf(buffer, SIZE, " client=%p\n", client.get());
Mathias Agopian1b5e1022010-04-20 17:55:49 -0700602 result.append(buffer);
603}
604
Mathias Agopian48b888a2011-01-19 16:15:53 -0800605
606void LayerBaseClient::shortDump(String8& result, char* scratch, size_t size) const
607{
608 LayerBaseClient::dump(result, scratch, size);
609}
610
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700611// ---------------------------------------------------------------------------
612
Mathias Agopiana67932f2011-04-20 14:20:59 -0700613LayerBaseClient::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
614 const sp<LayerBaseClient>& layer)
615 : mFlinger(flinger), mLayer(layer) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700616}
617
Mathias Agopiana67932f2011-04-20 14:20:59 -0700618LayerBaseClient::LayerCleaner::~LayerCleaner() {
Mathias Agopian9a112062009-04-17 19:36:26 -0700619 // destroy client resources
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700620 mFlinger->onLayerDestroyed(mLayer);
Mathias Agopianb5b7f262010-05-07 15:58:44 -0700621}
622
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623// ---------------------------------------------------------------------------
624
625}; // namespace android