OSDN Git Service

ed24094fa2327fc8b9035c846c09890daafd9629
[android-x86/frameworks-native.git] / libs / gui / Surface.cpp
1 /*
2  * Copyright (C) 2010 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
17 #define LOG_TAG "Surface"
18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19 //#define LOG_NDEBUG 0
20
21 #include <android/native_window.h>
22
23 #include <binder/Parcel.h>
24
25 #include <utils/Log.h>
26 #include <utils/Trace.h>
27 #include <utils/NativeHandle.h>
28
29 #include <ui/Fence.h>
30 #include <ui/Region.h>
31
32 #include <gui/IProducerListener.h>
33 #include <gui/ISurfaceComposer.h>
34 #include <gui/SurfaceComposerClient.h>
35 #include <gui/GLConsumer.h>
36 #include <gui/Surface.h>
37
38 #include <private/gui/ComposerService.h>
39
40 namespace android {
41
42 Surface::Surface(
43         const sp<IGraphicBufferProducer>& bufferProducer,
44         bool controlledByApp)
45     : mGraphicBufferProducer(bufferProducer),
46       mGenerationNumber(0)
47 {
48     // Initialize the ANativeWindow function pointers.
49     ANativeWindow::setSwapInterval  = hook_setSwapInterval;
50     ANativeWindow::dequeueBuffer    = hook_dequeueBuffer;
51     ANativeWindow::cancelBuffer     = hook_cancelBuffer;
52     ANativeWindow::queueBuffer      = hook_queueBuffer;
53     ANativeWindow::query            = hook_query;
54     ANativeWindow::perform          = hook_perform;
55
56     ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
57     ANativeWindow::cancelBuffer_DEPRECATED  = hook_cancelBuffer_DEPRECATED;
58     ANativeWindow::lockBuffer_DEPRECATED    = hook_lockBuffer_DEPRECATED;
59     ANativeWindow::queueBuffer_DEPRECATED   = hook_queueBuffer_DEPRECATED;
60
61     const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
62     const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
63
64     mReqWidth = 0;
65     mReqHeight = 0;
66     mReqFormat = 0;
67     mReqUsage = 0;
68     mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
69     mDataSpace = HAL_DATASPACE_UNKNOWN;
70     mCrop.clear();
71     mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
72     mTransform = 0;
73     mStickyTransform = 0;
74     mDefaultWidth = 0;
75     mDefaultHeight = 0;
76     mUserWidth = 0;
77     mUserHeight = 0;
78     mTransformHint = 0;
79     mConsumerRunningBehind = false;
80     mConnectedToCpu = false;
81     mProducerControlledByApp = controlledByApp;
82     mSwapIntervalZero = false;
83 }
84
85 Surface::~Surface() {
86     if (mConnectedToCpu) {
87         Surface::disconnect(NATIVE_WINDOW_API_CPU);
88     }
89 }
90
91 sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
92     return mGraphicBufferProducer;
93 }
94
95 void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
96     mGraphicBufferProducer->setSidebandStream(stream);
97 }
98
99 void Surface::allocateBuffers() {
100     uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
101     uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
102     mGraphicBufferProducer->allocateBuffers(mSwapIntervalZero, reqWidth,
103             reqHeight, mReqFormat, mReqUsage);
104 }
105
106 status_t Surface::setGenerationNumber(uint32_t generation) {
107     status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
108     if (result == NO_ERROR) {
109         mGenerationNumber = generation;
110     }
111     return result;
112 }
113
114 String8 Surface::getConsumerName() const {
115     return mGraphicBufferProducer->getConsumerName();
116 }
117
118 int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
119     Surface* c = getSelf(window);
120     return c->setSwapInterval(interval);
121 }
122
123 int Surface::hook_dequeueBuffer(ANativeWindow* window,
124         ANativeWindowBuffer** buffer, int* fenceFd) {
125     Surface* c = getSelf(window);
126     return c->dequeueBuffer(buffer, fenceFd);
127 }
128
129 int Surface::hook_cancelBuffer(ANativeWindow* window,
130         ANativeWindowBuffer* buffer, int fenceFd) {
131     Surface* c = getSelf(window);
132     return c->cancelBuffer(buffer, fenceFd);
133 }
134
135 int Surface::hook_queueBuffer(ANativeWindow* window,
136         ANativeWindowBuffer* buffer, int fenceFd) {
137     Surface* c = getSelf(window);
138     return c->queueBuffer(buffer, fenceFd);
139 }
140
141 int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
142         ANativeWindowBuffer** buffer) {
143     Surface* c = getSelf(window);
144     ANativeWindowBuffer* buf = NULL;
145     int fenceFd = -1;
146     int result = c->dequeueBuffer(&buf, &fenceFd);
147
148     if (result != NO_ERROR) return result;
149
150     sp<Fence> fence(new Fence(fenceFd));
151     int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
152     if (waitResult != OK) {
153         ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
154                 waitResult);
155         c->cancelBuffer(buf, -1);
156         return waitResult;
157     }
158     *buffer = buf;
159     return result;
160 }
161
162 int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
163         ANativeWindowBuffer* buffer) {
164     Surface* c = getSelf(window);
165     return c->cancelBuffer(buffer, -1);
166 }
167
168 int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
169         ANativeWindowBuffer* buffer) {
170     Surface* c = getSelf(window);
171     return c->lockBuffer_DEPRECATED(buffer);
172 }
173
174 int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
175         ANativeWindowBuffer* buffer) {
176     Surface* c = getSelf(window);
177     return c->queueBuffer(buffer, -1);
178 }
179
180 int Surface::hook_query(const ANativeWindow* window,
181                                 int what, int* value) {
182     const Surface* c = getSelf(window);
183     return c->query(what, value);
184 }
185
186 int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
187     va_list args;
188     va_start(args, operation);
189     Surface* c = getSelf(window);
190     return c->perform(operation, args);
191 }
192
193 int Surface::setSwapInterval(int interval) {
194     ATRACE_CALL();
195     // EGL specification states:
196     //  interval is silently clamped to minimum and maximum implementation
197     //  dependent values before being stored.
198
199     if (interval < minSwapInterval)
200         interval = minSwapInterval;
201
202     if (interval > maxSwapInterval)
203         interval = maxSwapInterval;
204
205     mSwapIntervalZero = (interval == 0);
206
207     return NO_ERROR;
208 }
209
210 int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
211     ATRACE_CALL();
212     ALOGV("Surface::dequeueBuffer");
213
214     uint32_t reqWidth;
215     uint32_t reqHeight;
216     bool swapIntervalZero;
217     PixelFormat reqFormat;
218     uint32_t reqUsage;
219
220     {
221         Mutex::Autolock lock(mMutex);
222
223         reqWidth = mReqWidth ? mReqWidth : mUserWidth;
224         reqHeight = mReqHeight ? mReqHeight : mUserHeight;
225
226         swapIntervalZero = mSwapIntervalZero;
227         reqFormat = mReqFormat;
228         reqUsage = mReqUsage;
229     } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
230
231     int buf = -1;
232     sp<Fence> fence;
233     status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, swapIntervalZero,
234             reqWidth, reqHeight, reqFormat, reqUsage);
235
236     if (result < 0) {
237         ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer(%d, %d, %d, %d, %d)"
238              "failed: %d", swapIntervalZero, reqWidth, reqHeight, reqFormat,
239              reqUsage, result);
240         return result;
241     }
242
243     Mutex::Autolock lock(mMutex);
244
245     sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
246
247     // this should never happen
248     ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
249
250     if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
251         freeAllBuffers();
252     }
253
254     if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
255         result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
256         if (result != NO_ERROR) {
257             ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
258             mGraphicBufferProducer->cancelBuffer(buf, fence);
259             return result;
260         }
261     }
262
263     if ((fence != NULL) && fence->isValid()) {
264         *fenceFd = fence->dup();
265         if (*fenceFd == -1) {
266             ALOGE("dequeueBuffer: error duping fence: %d", errno);
267             // dup() should never fail; something is badly wrong. Soldier on
268             // and hope for the best; the worst that should happen is some
269             // visible corruption that lasts until the next frame.
270         }
271     } else {
272         *fenceFd = -1;
273     }
274
275     *buffer = gbuf.get();
276     return OK;
277 }
278
279 int Surface::cancelBuffer(android_native_buffer_t* buffer,
280         int fenceFd) {
281     ATRACE_CALL();
282     ALOGV("Surface::cancelBuffer");
283     Mutex::Autolock lock(mMutex);
284     int i = getSlotFromBufferLocked(buffer);
285     if (i < 0) {
286         if (fenceFd >= 0) {
287             close(fenceFd);
288         }
289         return i;
290     }
291     sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
292     mGraphicBufferProducer->cancelBuffer(i, fence);
293     return OK;
294 }
295
296 int Surface::getSlotFromBufferLocked(
297         android_native_buffer_t* buffer) const {
298     for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
299         if (mSlots[i].buffer != NULL &&
300                 mSlots[i].buffer->handle == buffer->handle) {
301             return i;
302         }
303     }
304     ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
305     return BAD_VALUE;
306 }
307
308 int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
309     ALOGV("Surface::lockBuffer");
310     Mutex::Autolock lock(mMutex);
311     return OK;
312 }
313
314 int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
315     ATRACE_CALL();
316     ALOGV("Surface::queueBuffer");
317     Mutex::Autolock lock(mMutex);
318     int64_t timestamp;
319     bool isAutoTimestamp = false;
320     if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
321         timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
322         isAutoTimestamp = true;
323         ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
324             timestamp / 1000000.f);
325     } else {
326         timestamp = mTimestamp;
327     }
328     int i = getSlotFromBufferLocked(buffer);
329     if (i < 0) {
330         if (fenceFd >= 0) {
331             close(fenceFd);
332         }
333         return i;
334     }
335
336
337     // Make sure the crop rectangle is entirely inside the buffer.
338     Rect crop;
339     mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
340
341     sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
342     IGraphicBufferProducer::QueueBufferOutput output;
343     IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
344             mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
345             mSwapIntervalZero, fence, mStickyTransform);
346
347     if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
348         input.setSurfaceDamage(Region::INVALID_REGION);
349     } else {
350         // Here we do two things:
351         // 1) The surface damage was specified using the OpenGL ES convention of
352         //    the origin being in the bottom-left corner. Here we flip to the
353         //    convention that the rest of the system uses (top-left corner) by
354         //    subtracting all top/bottom coordinates from the buffer height.
355         // 2) If the buffer is coming in rotated (for example, because the EGL
356         //    implementation is reacting to the transform hint coming back from
357         //    SurfaceFlinger), the surface damage needs to be rotated the
358         //    opposite direction, since it was generated assuming an unrotated
359         //    buffer (the app doesn't know that the EGL implementation is
360         //    reacting to the transform hint behind its back). The
361         //    transformations in the switch statement below apply those
362         //    complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
363
364         int width = buffer->width;
365         int height = buffer->height;
366         bool rotated90 = (mTransform ^ mStickyTransform) &
367                 NATIVE_WINDOW_TRANSFORM_ROT_90;
368         if (rotated90) {
369             std::swap(width, height);
370         }
371
372         Region flippedRegion;
373         for (auto rect : mDirtyRegion) {
374             int left = rect.left;
375             int right = rect.right;
376             int top = height - rect.bottom; // Flip from OpenGL convention
377             int bottom = height - rect.top; // Flip from OpenGL convention
378             switch (mTransform ^ mStickyTransform) {
379                 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
380                     // Rotate 270 degrees
381                     Rect flippedRect{top, width - right, bottom, width - left};
382                     flippedRegion.orSelf(flippedRect);
383                     break;
384                 }
385                 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
386                     // Rotate 180 degrees
387                     Rect flippedRect{width - right, height - bottom,
388                             width - left, height - top};
389                     flippedRegion.orSelf(flippedRect);
390                     break;
391                 }
392                 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
393                     // Rotate 90 degrees
394                     Rect flippedRect{height - bottom, left,
395                             height - top, right};
396                     flippedRegion.orSelf(flippedRect);
397                     break;
398                 }
399                 default: {
400                     Rect flippedRect{left, top, right, bottom};
401                     flippedRegion.orSelf(flippedRect);
402                     break;
403                 }
404             }
405         }
406
407         input.setSurfaceDamage(flippedRegion);
408     }
409
410     status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
411     if (err != OK)  {
412         ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
413     }
414     uint32_t numPendingBuffers = 0;
415     uint32_t hint = 0;
416     output.deflate(&mDefaultWidth, &mDefaultHeight, &hint,
417             &numPendingBuffers);
418
419     // Disable transform hint if sticky transform is set.
420     if (mStickyTransform == 0) {
421         mTransformHint = hint;
422     }
423
424     mConsumerRunningBehind = (numPendingBuffers >= 2);
425
426     if (!mConnectedToCpu) {
427         // Clear surface damage back to full-buffer
428         mDirtyRegion = Region::INVALID_REGION;
429     }
430
431     return err;
432 }
433
434 int Surface::query(int what, int* value) const {
435     ATRACE_CALL();
436     ALOGV("Surface::query");
437     { // scope for the lock
438         Mutex::Autolock lock(mMutex);
439         switch (what) {
440             case NATIVE_WINDOW_FORMAT:
441                 if (mReqFormat) {
442                     *value = static_cast<int>(mReqFormat);
443                     return NO_ERROR;
444                 }
445                 break;
446             case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
447                 sp<ISurfaceComposer> composer(
448                         ComposerService::getComposerService());
449                 if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) {
450                     *value = 1;
451                 } else {
452                     *value = 0;
453                 }
454                 return NO_ERROR;
455             }
456             case NATIVE_WINDOW_CONCRETE_TYPE:
457                 *value = NATIVE_WINDOW_SURFACE;
458                 return NO_ERROR;
459             case NATIVE_WINDOW_DEFAULT_WIDTH:
460                 *value = static_cast<int>(
461                         mUserWidth ? mUserWidth : mDefaultWidth);
462                 return NO_ERROR;
463             case NATIVE_WINDOW_DEFAULT_HEIGHT:
464                 *value = static_cast<int>(
465                         mUserHeight ? mUserHeight : mDefaultHeight);
466                 return NO_ERROR;
467             case NATIVE_WINDOW_TRANSFORM_HINT:
468                 *value = static_cast<int>(mTransformHint);
469                 return NO_ERROR;
470             case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
471                 status_t err = NO_ERROR;
472                 if (!mConsumerRunningBehind) {
473                     *value = 0;
474                 } else {
475                     err = mGraphicBufferProducer->query(what, value);
476                     if (err == NO_ERROR) {
477                         mConsumerRunningBehind = *value;
478                     }
479                 }
480                 return err;
481             }
482         }
483     }
484     return mGraphicBufferProducer->query(what, value);
485 }
486
487 int Surface::perform(int operation, va_list args)
488 {
489     int res = NO_ERROR;
490     switch (operation) {
491     case NATIVE_WINDOW_CONNECT:
492         // deprecated. must return NO_ERROR.
493         break;
494     case NATIVE_WINDOW_DISCONNECT:
495         // deprecated. must return NO_ERROR.
496         break;
497     case NATIVE_WINDOW_SET_USAGE:
498         res = dispatchSetUsage(args);
499         break;
500     case NATIVE_WINDOW_SET_CROP:
501         res = dispatchSetCrop(args);
502         break;
503     case NATIVE_WINDOW_SET_BUFFER_COUNT:
504         res = dispatchSetBufferCount(args);
505         break;
506     case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
507         res = dispatchSetBuffersGeometry(args);
508         break;
509     case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
510         res = dispatchSetBuffersTransform(args);
511         break;
512     case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
513         res = dispatchSetBuffersStickyTransform(args);
514         break;
515     case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
516         res = dispatchSetBuffersTimestamp(args);
517         break;
518     case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
519         res = dispatchSetBuffersDimensions(args);
520         break;
521     case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
522         res = dispatchSetBuffersUserDimensions(args);
523         break;
524     case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
525         res = dispatchSetBuffersFormat(args);
526         break;
527     case NATIVE_WINDOW_LOCK:
528         res = dispatchLock(args);
529         break;
530     case NATIVE_WINDOW_UNLOCK_AND_POST:
531         res = dispatchUnlockAndPost(args);
532         break;
533     case NATIVE_WINDOW_SET_SCALING_MODE:
534         res = dispatchSetScalingMode(args);
535         break;
536     case NATIVE_WINDOW_API_CONNECT:
537         res = dispatchConnect(args);
538         break;
539     case NATIVE_WINDOW_API_DISCONNECT:
540         res = dispatchDisconnect(args);
541         break;
542     case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
543         res = dispatchSetSidebandStream(args);
544         break;
545     case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
546         res = dispatchSetBuffersDataSpace(args);
547         break;
548     case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
549         res = dispatchSetSurfaceDamage(args);
550         break;
551     default:
552         res = NAME_NOT_FOUND;
553         break;
554     }
555     return res;
556 }
557
558 int Surface::dispatchConnect(va_list args) {
559     int api = va_arg(args, int);
560     return connect(api);
561 }
562
563 int Surface::dispatchDisconnect(va_list args) {
564     int api = va_arg(args, int);
565     return disconnect(api);
566 }
567
568 int Surface::dispatchSetUsage(va_list args) {
569     int usage = va_arg(args, int);
570     return setUsage(static_cast<uint32_t>(usage));
571 }
572
573 int Surface::dispatchSetCrop(va_list args) {
574     android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
575     return setCrop(reinterpret_cast<Rect const*>(rect));
576 }
577
578 int Surface::dispatchSetBufferCount(va_list args) {
579     size_t bufferCount = va_arg(args, size_t);
580     return setBufferCount(static_cast<int32_t>(bufferCount));
581 }
582
583 int Surface::dispatchSetBuffersGeometry(va_list args) {
584     uint32_t width = va_arg(args, uint32_t);
585     uint32_t height = va_arg(args, uint32_t);
586     PixelFormat format = va_arg(args, PixelFormat);
587     int err = setBuffersDimensions(width, height);
588     if (err != 0) {
589         return err;
590     }
591     return setBuffersFormat(format);
592 }
593
594 int Surface::dispatchSetBuffersDimensions(va_list args) {
595     uint32_t width = va_arg(args, uint32_t);
596     uint32_t height = va_arg(args, uint32_t);
597     return setBuffersDimensions(width, height);
598 }
599
600 int Surface::dispatchSetBuffersUserDimensions(va_list args) {
601     uint32_t width = va_arg(args, uint32_t);
602     uint32_t height = va_arg(args, uint32_t);
603     return setBuffersUserDimensions(width, height);
604 }
605
606 int Surface::dispatchSetBuffersFormat(va_list args) {
607     PixelFormat format = va_arg(args, PixelFormat);
608     return setBuffersFormat(format);
609 }
610
611 int Surface::dispatchSetScalingMode(va_list args) {
612     int mode = va_arg(args, int);
613     return setScalingMode(mode);
614 }
615
616 int Surface::dispatchSetBuffersTransform(va_list args) {
617     uint32_t transform = va_arg(args, uint32_t);
618     return setBuffersTransform(transform);
619 }
620
621 int Surface::dispatchSetBuffersStickyTransform(va_list args) {
622     uint32_t transform = va_arg(args, uint32_t);
623     return setBuffersStickyTransform(transform);
624 }
625
626 int Surface::dispatchSetBuffersTimestamp(va_list args) {
627     int64_t timestamp = va_arg(args, int64_t);
628     return setBuffersTimestamp(timestamp);
629 }
630
631 int Surface::dispatchLock(va_list args) {
632     ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
633     ARect* inOutDirtyBounds = va_arg(args, ARect*);
634     return lock(outBuffer, inOutDirtyBounds);
635 }
636
637 int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
638     return unlockAndPost();
639 }
640
641 int Surface::dispatchSetSidebandStream(va_list args) {
642     native_handle_t* sH = va_arg(args, native_handle_t*);
643     sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
644     setSidebandStream(sidebandHandle);
645     return OK;
646 }
647
648 int Surface::dispatchSetBuffersDataSpace(va_list args) {
649     android_dataspace dataspace =
650             static_cast<android_dataspace>(va_arg(args, int));
651     return setBuffersDataSpace(dataspace);
652 }
653
654 int Surface::dispatchSetSurfaceDamage(va_list args) {
655     android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
656     size_t numRects = va_arg(args, size_t);
657     setSurfaceDamage(rects, numRects);
658     return NO_ERROR;
659 }
660
661 int Surface::connect(int api) {
662     static sp<IProducerListener> listener = new DummyProducerListener();
663     return connect(api, listener);
664 }
665
666 int Surface::connect(int api, const sp<IProducerListener>& listener) {
667     ATRACE_CALL();
668     ALOGV("Surface::connect");
669     Mutex::Autolock lock(mMutex);
670     IGraphicBufferProducer::QueueBufferOutput output;
671     int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
672     if (err == NO_ERROR) {
673         uint32_t numPendingBuffers = 0;
674         uint32_t hint = 0;
675         output.deflate(&mDefaultWidth, &mDefaultHeight, &hint,
676                 &numPendingBuffers);
677
678         // Disable transform hint if sticky transform is set.
679         if (mStickyTransform == 0) {
680             mTransformHint = hint;
681         }
682
683         mConsumerRunningBehind = (numPendingBuffers >= 2);
684     }
685     if (!err && api == NATIVE_WINDOW_API_CPU) {
686         mConnectedToCpu = true;
687         // Clear the dirty region in case we're switching from a non-CPU API
688         mDirtyRegion.clear();
689     } else if (!err) {
690         // Initialize the dirty region for tracking surface damage
691         mDirtyRegion = Region::INVALID_REGION;
692     }
693
694     return err;
695 }
696
697
698 int Surface::disconnect(int api) {
699     ATRACE_CALL();
700     ALOGV("Surface::disconnect");
701     Mutex::Autolock lock(mMutex);
702     freeAllBuffers();
703     int err = mGraphicBufferProducer->disconnect(api);
704     if (!err) {
705         mReqFormat = 0;
706         mReqWidth = 0;
707         mReqHeight = 0;
708         mReqUsage = 0;
709         mCrop.clear();
710         mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
711         mTransform = 0;
712         mStickyTransform = 0;
713
714         if (api == NATIVE_WINDOW_API_CPU) {
715             mConnectedToCpu = false;
716         }
717     }
718     return err;
719 }
720
721 int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
722         sp<Fence>* outFence) {
723     ATRACE_CALL();
724     ALOGV("Surface::detachNextBuffer");
725
726     if (outBuffer == NULL || outFence == NULL) {
727         return BAD_VALUE;
728     }
729
730     Mutex::Autolock lock(mMutex);
731
732     sp<GraphicBuffer> buffer(NULL);
733     sp<Fence> fence(NULL);
734     status_t result = mGraphicBufferProducer->detachNextBuffer(
735             &buffer, &fence);
736     if (result != NO_ERROR) {
737         return result;
738     }
739
740     *outBuffer = buffer;
741     if (fence != NULL && fence->isValid()) {
742         *outFence = fence;
743     } else {
744         *outFence = Fence::NO_FENCE;
745     }
746
747     return NO_ERROR;
748 }
749
750 int Surface::attachBuffer(ANativeWindowBuffer* buffer)
751 {
752     ATRACE_CALL();
753     ALOGV("Surface::attachBuffer");
754
755     Mutex::Autolock lock(mMutex);
756
757     sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
758     uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
759     graphicBuffer->mGenerationNumber = mGenerationNumber;
760     int32_t attachedSlot = -1;
761     status_t result = mGraphicBufferProducer->attachBuffer(
762             &attachedSlot, graphicBuffer);
763     if (result != NO_ERROR) {
764         ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
765         graphicBuffer->mGenerationNumber = priorGeneration;
766         return result;
767     }
768     mSlots[attachedSlot].buffer = graphicBuffer;
769
770     return NO_ERROR;
771 }
772
773 int Surface::setUsage(uint32_t reqUsage)
774 {
775     ALOGV("Surface::setUsage");
776     Mutex::Autolock lock(mMutex);
777     mReqUsage = reqUsage;
778     return OK;
779 }
780
781 int Surface::setCrop(Rect const* rect)
782 {
783     ATRACE_CALL();
784
785     Rect realRect;
786     if (rect == NULL || rect->isEmpty()) {
787         realRect.clear();
788     } else {
789         realRect = *rect;
790     }
791
792     ALOGV("Surface::setCrop rect=[%d %d %d %d]",
793             realRect.left, realRect.top, realRect.right, realRect.bottom);
794
795     Mutex::Autolock lock(mMutex);
796     mCrop = realRect;
797     return NO_ERROR;
798 }
799
800 int Surface::setBufferCount(int bufferCount)
801 {
802     ATRACE_CALL();
803     ALOGV("Surface::setBufferCount");
804     Mutex::Autolock lock(mMutex);
805
806     status_t err = mGraphicBufferProducer->setBufferCount(bufferCount);
807     ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
808             bufferCount, strerror(-err));
809
810     if (err == NO_ERROR) {
811         freeAllBuffers();
812     }
813
814     return err;
815 }
816
817 int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
818 {
819     ATRACE_CALL();
820     ALOGV("Surface::setBuffersDimensions");
821
822     if ((width && !height) || (!width && height))
823         return BAD_VALUE;
824
825     Mutex::Autolock lock(mMutex);
826     mReqWidth = width;
827     mReqHeight = height;
828     return NO_ERROR;
829 }
830
831 int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
832 {
833     ATRACE_CALL();
834     ALOGV("Surface::setBuffersUserDimensions");
835
836     if ((width && !height) || (!width && height))
837         return BAD_VALUE;
838
839     Mutex::Autolock lock(mMutex);
840     mUserWidth = width;
841     mUserHeight = height;
842     return NO_ERROR;
843 }
844
845 int Surface::setBuffersFormat(PixelFormat format)
846 {
847     ALOGV("Surface::setBuffersFormat");
848
849     Mutex::Autolock lock(mMutex);
850     mReqFormat = format;
851     return NO_ERROR;
852 }
853
854 int Surface::setScalingMode(int mode)
855 {
856     ATRACE_CALL();
857     ALOGV("Surface::setScalingMode(%d)", mode);
858
859     switch (mode) {
860         case NATIVE_WINDOW_SCALING_MODE_FREEZE:
861         case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
862         case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
863             break;
864         default:
865             ALOGE("unknown scaling mode: %d", mode);
866             return BAD_VALUE;
867     }
868
869     Mutex::Autolock lock(mMutex);
870     mScalingMode = mode;
871     return NO_ERROR;
872 }
873
874 int Surface::setBuffersTransform(uint32_t transform)
875 {
876     ATRACE_CALL();
877     ALOGV("Surface::setBuffersTransform");
878     Mutex::Autolock lock(mMutex);
879     mTransform = transform;
880     return NO_ERROR;
881 }
882
883 int Surface::setBuffersStickyTransform(uint32_t transform)
884 {
885     ATRACE_CALL();
886     ALOGV("Surface::setBuffersStickyTransform");
887     Mutex::Autolock lock(mMutex);
888     mStickyTransform = transform;
889     return NO_ERROR;
890 }
891
892 int Surface::setBuffersTimestamp(int64_t timestamp)
893 {
894     ALOGV("Surface::setBuffersTimestamp");
895     Mutex::Autolock lock(mMutex);
896     mTimestamp = timestamp;
897     return NO_ERROR;
898 }
899
900 int Surface::setBuffersDataSpace(android_dataspace dataSpace)
901 {
902     ALOGV("Surface::setBuffersDataSpace");
903     Mutex::Autolock lock(mMutex);
904     mDataSpace = dataSpace;
905     return NO_ERROR;
906 }
907
908 void Surface::freeAllBuffers() {
909     for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
910         mSlots[i].buffer = 0;
911     }
912 }
913
914 void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
915     ATRACE_CALL();
916     ALOGV("Surface::setSurfaceDamage");
917     Mutex::Autolock lock(mMutex);
918
919     if (mConnectedToCpu || numRects == 0) {
920         mDirtyRegion = Region::INVALID_REGION;
921         return;
922     }
923
924     mDirtyRegion.clear();
925     for (size_t r = 0; r < numRects; ++r) {
926         // We intentionally flip top and bottom here, since because they're
927         // specified with a bottom-left origin, top > bottom, which fails
928         // validation in the Region class. We will fix this up when we flip to a
929         // top-left origin in queueBuffer.
930         Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
931         mDirtyRegion.orSelf(rect);
932     }
933 }
934
935 // ----------------------------------------------------------------------
936 // the lock/unlock APIs must be used from the same thread
937
938 static status_t copyBlt(
939         const sp<GraphicBuffer>& dst,
940         const sp<GraphicBuffer>& src,
941         const Region& reg)
942 {
943     // src and dst with, height and format must be identical. no verification
944     // is done here.
945     status_t err;
946     uint8_t* src_bits = NULL;
947     err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
948             reinterpret_cast<void**>(&src_bits));
949     ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
950
951     uint8_t* dst_bits = NULL;
952     err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
953             reinterpret_cast<void**>(&dst_bits));
954     ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
955
956     Region::const_iterator head(reg.begin());
957     Region::const_iterator tail(reg.end());
958     if (head != tail && src_bits && dst_bits) {
959         const size_t bpp = bytesPerPixel(src->format);
960         const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
961         const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
962
963         while (head != tail) {
964             const Rect& r(*head++);
965             int32_t h = r.height();
966             if (h <= 0) continue;
967             size_t size = static_cast<uint32_t>(r.width()) * bpp;
968             uint8_t const * s = src_bits +
969                     static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
970             uint8_t       * d = dst_bits +
971                     static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
972             if (dbpr==sbpr && size==sbpr) {
973                 size *= static_cast<size_t>(h);
974                 h = 1;
975             }
976             do {
977                 memcpy(d, s, size);
978                 d += dbpr;
979                 s += sbpr;
980             } while (--h > 0);
981         }
982     }
983
984     if (src_bits)
985         src->unlock();
986
987     if (dst_bits)
988         dst->unlock();
989
990     return err;
991 }
992
993 // ----------------------------------------------------------------------------
994
995 status_t Surface::lock(
996         ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
997 {
998     if (mLockedBuffer != 0) {
999         ALOGE("Surface::lock failed, already locked");
1000         return INVALID_OPERATION;
1001     }
1002
1003     if (!mConnectedToCpu) {
1004         int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1005         if (err) {
1006             return err;
1007         }
1008         // we're intending to do software rendering from this point
1009         setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1010     }
1011
1012     ANativeWindowBuffer* out;
1013     int fenceFd = -1;
1014     status_t err = dequeueBuffer(&out, &fenceFd);
1015     ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1016     if (err == NO_ERROR) {
1017         sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
1018         const Rect bounds(backBuffer->width, backBuffer->height);
1019
1020         Region newDirtyRegion;
1021         if (inOutDirtyBounds) {
1022             newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1023             newDirtyRegion.andSelf(bounds);
1024         } else {
1025             newDirtyRegion.set(bounds);
1026         }
1027
1028         // figure out if we can copy the frontbuffer back
1029         const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1030         const bool canCopyBack = (frontBuffer != 0 &&
1031                 backBuffer->width  == frontBuffer->width &&
1032                 backBuffer->height == frontBuffer->height &&
1033                 backBuffer->format == frontBuffer->format);
1034
1035         if (canCopyBack) {
1036             // copy the area that is invalid and not repainted this round
1037             const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
1038             if (!copyback.isEmpty())
1039                 copyBlt(backBuffer, frontBuffer, copyback);
1040         } else {
1041             // if we can't copy-back anything, modify the user's dirty
1042             // region to make sure they redraw the whole buffer
1043             newDirtyRegion.set(bounds);
1044             mDirtyRegion.clear();
1045             Mutex::Autolock lock(mMutex);
1046             for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1047                 mSlots[i].dirtyRegion.clear();
1048             }
1049         }
1050
1051
1052         { // scope for the lock
1053             Mutex::Autolock lock(mMutex);
1054             int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1055             if (backBufferSlot >= 0) {
1056                 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1057                 mDirtyRegion.subtract(dirtyRegion);
1058                 dirtyRegion = newDirtyRegion;
1059             }
1060         }
1061
1062         mDirtyRegion.orSelf(newDirtyRegion);
1063         if (inOutDirtyBounds) {
1064             *inOutDirtyBounds = newDirtyRegion.getBounds();
1065         }
1066
1067         void* vaddr;
1068         status_t res = backBuffer->lockAsync(
1069                 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
1070                 newDirtyRegion.bounds(), &vaddr, fenceFd);
1071
1072         ALOGW_IF(res, "failed locking buffer (handle = %p)",
1073                 backBuffer->handle);
1074
1075         if (res != 0) {
1076             err = INVALID_OPERATION;
1077         } else {
1078             mLockedBuffer = backBuffer;
1079             outBuffer->width  = backBuffer->width;
1080             outBuffer->height = backBuffer->height;
1081             outBuffer->stride = backBuffer->stride;
1082             outBuffer->format = backBuffer->format;
1083             outBuffer->bits   = vaddr;
1084         }
1085     }
1086     return err;
1087 }
1088
1089 status_t Surface::unlockAndPost()
1090 {
1091     if (mLockedBuffer == 0) {
1092         ALOGE("Surface::unlockAndPost failed, no locked buffer");
1093         return INVALID_OPERATION;
1094     }
1095
1096     int fd = -1;
1097     status_t err = mLockedBuffer->unlockAsync(&fd);
1098     ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1099
1100     err = queueBuffer(mLockedBuffer.get(), fd);
1101     ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1102             mLockedBuffer->handle, strerror(-err));
1103
1104     mPostedBuffer = mLockedBuffer;
1105     mLockedBuffer = 0;
1106     return err;
1107 }
1108
1109 }; // namespace android