blob: ecb6b326476c1d1f702759dbf9d6ea773a235818 [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 <cutils/properties.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022#include <cutils/native_handle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/StopWatch.h>
27
28#include <ui/PixelFormat.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <ui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
Mathias Agopiancbb288b2009-09-07 16:32:45 -070031#include "Buffer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include "clz.h"
33#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include "DisplayHardware/DisplayHardware.h"
36
37
38#define DEBUG_RESIZE 0
39
40
41namespace android {
42
43// ---------------------------------------------------------------------------
44
45const uint32_t Layer::typeInfo = LayerBaseClient::typeInfo | 4;
46const char* const Layer::typeID = "Layer";
47
48// ---------------------------------------------------------------------------
49
Mathias Agopiancbb288b2009-09-07 16:32:45 -070050Layer::Layer(SurfaceFlinger* flinger, DisplayID display,
51 const sp<Client>& c, int32_t i)
52 : LayerBaseClient(flinger, display, c, i), lcblk(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053 mSecure(false),
Mathias Agopiancbb288b2009-09-07 16:32:45 -070054 mNeedsBlending(true)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055{
56 // no OpenGL operation is possible here, since we might not be
57 // in the OpenGL thread.
Mathias Agopiancbb288b2009-09-07 16:32:45 -070058 lcblk = new SharedBufferServer(c->ctrlblk, i, NUM_BUFFERS);
59 mFrontBufferIndex = lcblk->getFrontBuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060}
61
62Layer::~Layer()
63{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070064 destroy();
65 // the actual buffers will be destroyed here
Mathias Agopiancbb288b2009-09-07 16:32:45 -070066 delete lcblk;
67
Mathias Agopian0aa758d2009-04-22 15:23:34 -070068}
69
70void Layer::destroy()
71{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070072 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070073 if (mTextures[i].name != -1U) {
Mathias Agopian550b79f2009-04-22 15:49:28 -070074 glDeleteTextures(1, &mTextures[i].name);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070075 mTextures[i].name = -1U;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070076 }
77 if (mTextures[i].image != EGL_NO_IMAGE_KHR) {
78 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
79 eglDestroyImageKHR(dpy, mTextures[i].image);
Mathias Agopian0aa758d2009-04-22 15:23:34 -070080 mTextures[i].image = EGL_NO_IMAGE_KHR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070081 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -070082 mBuffers[i].clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 }
Mathias Agopian759fdb22009-07-02 17:33:40 -070084 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085}
86
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087sp<LayerBaseClient::Surface> Layer::createSurface() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088{
89 return mSurface;
90}
91
Mathias Agopian9a112062009-04-17 19:36:26 -070092status_t Layer::ditch()
93{
Mathias Agopian0aa758d2009-04-22 15:23:34 -070094 // the layer is not on screen anymore. free as much resources as possible
Mathias Agopian0aa758d2009-04-22 15:23:34 -070095 destroy();
Mathias Agopian9a112062009-04-17 19:36:26 -070096 return NO_ERROR;
97}
98
Mathias Agopianf9d93272009-06-19 17:00:27 -070099status_t Layer::setBuffers( uint32_t w, uint32_t h,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 PixelFormat format, uint32_t flags)
101{
102 PixelFormatInfo info;
103 status_t err = getPixelFormatInfo(format, &info);
104 if (err) return err;
105
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700106 uint32_t bufferFlags = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700107 if (flags & ISurfaceComposer::eSecure)
108 bufferFlags |= Buffer::SECURE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700110 mFormat = format;
111 mWidth = w;
112 mHeight = h;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700113 mSecure = (bufferFlags & Buffer::SECURE) ? true : false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 mNeedsBlending = (info.h_alpha - info.l_alpha) > 0;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700115 mBufferFlags = bufferFlags;
116 for (size_t i=0 ; i<NUM_BUFFERS ; i++) {
117 mBuffers[i] = new Buffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 }
Mathias Agopian9a112062009-04-17 19:36:26 -0700119 mSurface = new SurfaceLayer(mFlinger, clientIndex(), this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 return NO_ERROR;
121}
122
123void Layer::reloadTexture(const Region& dirty)
124{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700125 Mutex::Autolock _l(mLock);
126 sp<Buffer> buffer(getFrontBuffer());
Mathias Agopian0926f502009-05-04 14:17:04 -0700127 if (LIKELY(mFlags & DisplayHardware::DIRECT_TEXTURE)) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700128 int index = mFrontBufferIndex;
129 if (LIKELY(!mTextures[index].dirty)) {
130 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
131 } else {
132 // we need to recreate the texture
133 EGLDisplay dpy(mFlinger->graphicPlane(0).getEGLDisplay());
134
135 // create the new texture name if needed
136 if (UNLIKELY(mTextures[index].name == -1U)) {
137 mTextures[index].name = createTexture();
138 } else {
139 glBindTexture(GL_TEXTURE_2D, mTextures[index].name);
140 }
141
142 // free the previous image
143 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
144 eglDestroyImageKHR(dpy, mTextures[index].image);
145 mTextures[index].image = EGL_NO_IMAGE_KHR;
146 }
147
148 // construct an EGL_NATIVE_BUFFER_ANDROID
149 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
150
151 // create the new EGLImageKHR
152 const EGLint attrs[] = {
153 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
154 EGL_NONE, EGL_NONE
155 };
156 mTextures[index].image = eglCreateImageKHR(
157 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
158 (EGLClientBuffer)clientBuf, attrs);
159
160 LOGE_IF(mTextures[index].image == EGL_NO_IMAGE_KHR,
161 "eglCreateImageKHR() failed. err=0x%4x",
162 eglGetError());
163
164 if (mTextures[index].image != EGL_NO_IMAGE_KHR) {
165 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
166 (GLeglImageOES)mTextures[index].image);
167 GLint error = glGetError();
168 if (UNLIKELY(error != GL_NO_ERROR)) {
169 // this failed, for instance, because we don't support
170 // NPOT.
171 // FIXME: do something!
172 mFlags &= ~DisplayHardware::DIRECT_TEXTURE;
173 } else {
174 // Everything went okay!
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700175 mTextures[index].dirty = false;
176 mTextures[index].width = clientBuf->width;
177 mTextures[index].height = clientBuf->height;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700178 }
179 }
180 }
181 } else {
182 GGLSurface t;
Mathias Agopian0926f502009-05-04 14:17:04 -0700183 status_t res = buffer->lock(&t, GRALLOC_USAGE_SW_READ_RARELY);
184 LOGE_IF(res, "error %d (%s) locking buffer %p",
185 res, strerror(res), buffer.get());
186 if (res == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700187 if (UNLIKELY(mTextures[0].name == -1U)) {
188 mTextures[0].name = createTexture();
189 }
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700190 loadTexture(&mTextures[0], mTextures[0].name, dirty, t);
Mathias Agopian0926f502009-05-04 14:17:04 -0700191 buffer->unlock();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700192 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194}
195
196
197void Layer::onDraw(const Region& clip) const
198{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700199 const int index = (mFlags & DisplayHardware::DIRECT_TEXTURE) ?
200 mFrontBufferIndex : 0;
201 GLuint textureName = mTextures[index].name;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700202 if (UNLIKELY(textureName == -1LU)) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700203 //LOGW("Layer %p doesn't have a texture", this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 // the texture has not been created yet, this Layer has
205 // in fact never been drawn into. this happens frequently with
206 // SurfaceView.
207 clearWithOpenGL(clip);
208 return;
209 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700210
Mathias Agopian1fed11c2009-06-23 18:08:22 -0700211 drawWithOpenGL(clip, mTextures[index]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212}
213
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700214sp<SurfaceBuffer> Layer::requestBuffer(int index, int usage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700216 /*
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700217 * This is called from the client's Surface::dequeue(). This can happen
218 * at any time, especially while we're in the middle of using the
219 * buffer 'index' as our front buffer.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700220 *
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700221 * Make sure the buffer we're resizing is not the front buffer and has been
222 * dequeued. Once this condition is asserted, we are guaranteed that this
223 * buffer cannot become the front buffer under our feet, since we're called
224 * from Surface::dequeue()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700225 */
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700226 status_t err = lcblk->assertReallocate(index);
227 LOGE_IF(err, "assertReallocate(%d) failed (%s)", index, strerror(-err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700229 Mutex::Autolock _l(mLock);
230 uint32_t w = mWidth;
231 uint32_t h = mHeight;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700232
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700233 sp<Buffer>& buffer(mBuffers[index]);
234 if (buffer->getStrongCount() == 1) {
235 err = buffer->reallocate(w, h, mFormat, usage, mBufferFlags);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700236 } else {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700237 // here we have to reallocate a new buffer because we could have a
238 // client in our process with a reference to it (eg: status bar),
239 // and we can't release the handle under its feet.
240 buffer.clear();
241 buffer = new Buffer(w, h, mFormat, usage, mBufferFlags);
242 err = buffer->initCheck();
243 }
244
245 if (err || buffer->handle == 0) {
246 LOGE_IF(err || buffer->handle == 0,
247 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d failed (%s)",
248 this, index, w, h, strerror(-err));
249 } else {
250 LOGD_IF(DEBUG_RESIZE,
251 "Layer::requestBuffer(this=%p), index=%d, w=%d, h=%d",
252 this, index, w, h);
253 }
254
255 if (err == NO_ERROR && buffer->handle != 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700256 // texture is now dirty...
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700257 mTextures[index].dirty = true;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700258 }
259 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260}
261
262uint32_t Layer::doTransaction(uint32_t flags)
263{
264 const Layer::State& front(drawingState());
265 const Layer::State& temp(currentState());
266
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 // Index of the back buffer
268 const bool backbufferChanged = (front.w != temp.w) || (front.h != temp.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700270 if (backbufferChanged) {
271 // the size changed, we need to ask our client to request a new buffer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 LOGD_IF(DEBUG_RESIZE,
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700273 "resize (layer=%p), requested (%dx%d), "
274 "drawing (%d,%d), (%dx%d), (%dx%d)",
275 this, int(temp.w), int(temp.h),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 int(drawingState().w), int(drawingState().h),
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700277 int(mBuffers[0]->getWidth()), int(mBuffers[0]->getHeight()),
278 int(mBuffers[1]->getWidth()), int(mBuffers[1]->getHeight()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700280 // record the new size, form this point on, when the client request a
281 // buffer, it'll get the new size.
282 setDrawingSize(temp.w, temp.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700284 // all buffers need reallocation
285 lcblk->reallocate();
286
287 // recompute the visible region
288 // FIXME: ideally we would do that only when we have received
289 // a buffer of the right size
290 flags |= Layer::eVisibleRegion;
291 this->contentDirty = true;
292
293#if 0
294 // FIXME: handle freeze lock
295 // we're being resized and there is a freeze display request,
296 // acquire a freeze lock, so that the screen stays put
297 // until we've redrawn at the new size; this is to avoid
298 // glitches upon orientation changes.
299 if (mFlinger->hasFreezeRequest()) {
300 // if the surface is hidden, don't try to acquire the
301 // freeze lock, since hidden surfaces may never redraw
302 if (!(front.flags & ISurfaceComposer::eLayerHidden)) {
303 mFreezeLock = mFlinger->getFreezeLock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 }
305 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700306#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700308
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 if (temp.sequence != front.sequence) {
310 if (temp.flags & ISurfaceComposer::eLayerHidden || temp.alpha == 0) {
311 // this surface is now hidden, so it shouldn't hold a freeze lock
312 // (it may never redraw, which is fine if it is hidden)
313 mFreezeLock.clear();
314 }
315 }
316
317 return LayerBase::doTransaction(flags);
318}
319
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700320void Layer::setDrawingSize(uint32_t w, uint32_t h) {
321 Mutex::Autolock _l(mLock);
322 mWidth = w;
323 mHeight = h;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324}
325
326// ----------------------------------------------------------------------------
327// pageflip handling...
328// ----------------------------------------------------------------------------
329
330void Layer::lockPageFlip(bool& recomputeVisibleRegions)
331{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700332 ssize_t buf = lcblk->retireAndLock();
333 if (buf < NO_ERROR) {
334 //LOGW("nothing to retire (%s)", strerror(-buf));
335 // NOTE: here the buffer is locked because we will used
336 // for composition later in the loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 return;
338 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700339
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700340 // we retired a buffer, which becomes the new front buffer
341 mFrontBufferIndex = buf;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700343 // get the dirty region
344 sp<Buffer> newFrontBuffer(getBuffer(buf));
345 const Region dirty(lcblk->getDirtyRegion(buf));
346 mPostedDirtyRegion = dirty.intersect( newFrontBuffer->getBounds() );
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700347
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700348 // FIXME: signal an event if we have more buffers waiting
349 // mFlinger->signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700351 reloadTexture( mPostedDirtyRegion );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352}
353
354void Layer::unlockPageFlip(
355 const Transform& planeTransform, Region& outDirtyRegion)
356{
357 Region dirtyRegion(mPostedDirtyRegion);
358 if (!dirtyRegion.isEmpty()) {
359 mPostedDirtyRegion.clear();
360 // The dirty region is given in the layer's coordinate space
361 // transform the dirty region by the surface's transformation
362 // and the global transformation.
363 const Layer::State& s(drawingState());
364 const Transform tr(planeTransform * s.transform);
365 dirtyRegion = tr.transform(dirtyRegion);
366
367 // At this point, the dirty region is in screen space.
368 // Make sure it's constrained by the visible region (which
369 // is in screen space as well).
370 dirtyRegion.andSelf(visibleRegionScreen);
371 outDirtyRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372 }
373}
374
375void Layer::finishPageFlip()
376{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700377 status_t err = lcblk->unlock( mFrontBufferIndex );
378 LOGE_IF(err!=NO_ERROR,
379 "layer %p, buffer=%d wasn't locked!",
380 this, mFrontBufferIndex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381}
382
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700383// ---------------------------------------------------------------------------
384
Mathias Agopian9a112062009-04-17 19:36:26 -0700385Layer::SurfaceLayer::SurfaceLayer(const sp<SurfaceFlinger>& flinger,
386 SurfaceID id, const sp<Layer>& owner)
387 : Surface(flinger, id, owner->getIdentity(), owner)
388{
389}
390
391Layer::SurfaceLayer::~SurfaceLayer()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700392{
393}
394
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700395sp<SurfaceBuffer> Layer::SurfaceLayer::requestBuffer(int index, int usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700396{
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700397 sp<SurfaceBuffer> buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700398 sp<Layer> owner(getOwner());
399 if (owner != 0) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700400 LOGE_IF(uint32_t(index)>=NUM_BUFFERS,
401 "getBuffer() index (%d) out of range", index);
402 if (uint32_t(index) < NUM_BUFFERS) {
403 buffer = owner->requestBuffer(index, usage);
404 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700405 }
406 return buffer;
407}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408
409// ---------------------------------------------------------------------------
410
411
412}; // namespace android