FramebufferNativeWindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. **
  3. ** Copyright 2007 The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License Version 2.0(the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing software
  12. ** distributed under the License is distributed on an "AS IS" BASIS
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #define LOG_TAG "FramebufferNativeWindow"
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <cutils/log.h>
  23. #include <cutils/atomic.h>
  24. #include <utils/threads.h>
  25. #include <utils/RefBase.h>
  26. #include <ui/ANativeObjectBase.h>
  27. #include <ui/Fence.h>
  28. #define INCLUDED_FROM_FRAMEBUFFER_NATIVE_WINDOW_CPP
  29. #include <ui/FramebufferNativeWindow.h>
  30. #undef INCLUDED_FROM_FRAMEBUFFER_NATIVE_WINDOW_CPP
  31. #include <ui/Rect.h>
  32. #include <EGL/egl.h>
  33. #include <hardware/hardware.h>
  34. #include <hardware/gralloc.h>
  35. // ----------------------------------------------------------------------------
  36. namespace android {
  37. // ----------------------------------------------------------------------------
  38. class NativeBuffer final
  39. : public ANativeObjectBase<
  40. ANativeWindowBuffer,
  41. NativeBuffer,
  42. LightRefBase<NativeBuffer>>
  43. {
  44. public:
  45. NativeBuffer(int w, int h, int f, int u) : BASE() {
  46. ANativeWindowBuffer::width = w;
  47. ANativeWindowBuffer::height = h;
  48. ANativeWindowBuffer::format = f;
  49. ANativeWindowBuffer::usage = u;
  50. }
  51. private:
  52. friend class LightRefBase<NativeBuffer>;
  53. };
  54. /*
  55. * This implements the (main) framebuffer management. This class is used
  56. * mostly by SurfaceFlinger, but also by command line GL application.
  57. *
  58. * In fact this is an implementation of ANativeWindow on top of
  59. * the framebuffer.
  60. *
  61. * Currently it is pretty simple, it manages only two buffers (the front and
  62. * back buffer).
  63. *
  64. */
  65. FramebufferNativeWindow::FramebufferNativeWindow()
  66. : BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false)
  67. {
  68. hw_module_t const* module;
  69. if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
  70. int err;
  71. int i;
  72. err = framebuffer_open(module, &fbDev);
  73. ALOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err));
  74. err = gralloc_open(module, &grDev);
  75. ALOGE_IF(err, "couldn't open gralloc HAL (%s)", strerror(-err));
  76. // bail out if we can't initialize the modules
  77. if (!fbDev || !grDev)
  78. return;
  79. mUpdateOnDemand = (fbDev->setUpdateRect != 0);
  80. // initialize the buffer FIFO
  81. if(fbDev->numFramebuffers >= MIN_NUM_FRAME_BUFFERS &&
  82. fbDev->numFramebuffers <= MAX_NUM_FRAME_BUFFERS){
  83. mNumBuffers = fbDev->numFramebuffers;
  84. } else {
  85. mNumBuffers = MIN_NUM_FRAME_BUFFERS;
  86. }
  87. mNumFreeBuffers = mNumBuffers;
  88. mBufferHead = mNumBuffers-1;
  89. /*
  90. * This does not actually change the framebuffer format. It merely
  91. * fakes this format to surfaceflinger so that when it creates
  92. * framebuffer surfaces it will use this format. It's really a giant
  93. * HACK to allow interworking with buggy gralloc+GPU driver
  94. * implementations. You should *NEVER* need to set this for shipping
  95. * devices.
  96. */
  97. #ifdef FRAMEBUFFER_FORCE_FORMAT
  98. *((uint32_t *)&fbDev->format) = FRAMEBUFFER_FORCE_FORMAT;
  99. #endif
  100. for (i = 0; i < mNumBuffers; i++) {
  101. buffers[i] = new NativeBuffer(
  102. static_cast<int>(fbDev->width),
  103. static_cast<int>(fbDev->height),
  104. fbDev->format, GRALLOC_USAGE_HW_FB);
  105. }
  106. for (i = 0; i < mNumBuffers; i++) {
  107. err = grDev->alloc(grDev,
  108. static_cast<int>(fbDev->width),
  109. static_cast<int>(fbDev->height),
  110. fbDev->format, GRALLOC_USAGE_HW_FB,
  111. &buffers[i]->handle, &buffers[i]->stride);
  112. ALOGE_IF(err, "fb buffer %d allocation failed w=%d, h=%d, err=%s",
  113. i, fbDev->width, fbDev->height, strerror(-err));
  114. if (err) {
  115. mNumBuffers = i;
  116. mNumFreeBuffers = i;
  117. mBufferHead = mNumBuffers-1;
  118. break;
  119. }
  120. }
  121. const_cast<uint32_t&>(ANativeWindow::flags) = fbDev->flags;
  122. const_cast<float&>(ANativeWindow::xdpi) = fbDev->xdpi;
  123. const_cast<float&>(ANativeWindow::ydpi) = fbDev->ydpi;
  124. const_cast<int&>(ANativeWindow::minSwapInterval) =
  125. fbDev->minSwapInterval;
  126. const_cast<int&>(ANativeWindow::maxSwapInterval) =
  127. fbDev->maxSwapInterval;
  128. } else {
  129. ALOGE("Couldn't get gralloc module");
  130. }
  131. ANativeWindow::setSwapInterval = setSwapInterval;
  132. ANativeWindow::dequeueBuffer = dequeueBuffer;
  133. ANativeWindow::queueBuffer = queueBuffer;
  134. ANativeWindow::query = query;
  135. ANativeWindow::perform = perform;
  136. ANativeWindow::dequeueBuffer_DEPRECATED = dequeueBuffer_DEPRECATED;
  137. ANativeWindow::lockBuffer_DEPRECATED = lockBuffer_DEPRECATED;
  138. ANativeWindow::queueBuffer_DEPRECATED = queueBuffer_DEPRECATED;
  139. }
  140. FramebufferNativeWindow::~FramebufferNativeWindow()
  141. {
  142. if (grDev) {
  143. for(int i = 0; i < mNumBuffers; i++) {
  144. if (buffers[i] != NULL) {
  145. grDev->free(grDev, buffers[i]->handle);
  146. }
  147. }
  148. gralloc_close(grDev);
  149. }
  150. if (fbDev) {
  151. framebuffer_close(fbDev);
  152. }
  153. }
  154. status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r)
  155. {
  156. if (!mUpdateOnDemand) {
  157. return INVALID_OPERATION;
  158. }
  159. return fbDev->setUpdateRect(fbDev, r.left, r.top, r.width(), r.height());
  160. }
  161. status_t FramebufferNativeWindow::compositionComplete()
  162. {
  163. if (fbDev->compositionComplete) {
  164. return fbDev->compositionComplete(fbDev);
  165. }
  166. return INVALID_OPERATION;
  167. }
  168. int FramebufferNativeWindow::setSwapInterval(
  169. ANativeWindow* window, int interval)
  170. {
  171. framebuffer_device_t* fb = getSelf(window)->fbDev;
  172. return fb->setSwapInterval(fb, interval);
  173. }
  174. void FramebufferNativeWindow::dump(String8& result) {
  175. if (fbDev->common.version >= 1 && fbDev->dump) {
  176. const size_t SIZE = 4096;
  177. char buffer[SIZE];
  178. fbDev->dump(fbDev, buffer, SIZE);
  179. result.append(buffer);
  180. }
  181. }
  182. // only for debugging / logging
  183. int FramebufferNativeWindow::getCurrentBufferIndex() const
  184. {
  185. Mutex::Autolock _l(mutex);
  186. const int index = mCurrentBufferIndex;
  187. return index;
  188. }
  189. int FramebufferNativeWindow::dequeueBuffer_DEPRECATED(ANativeWindow* window,
  190. ANativeWindowBuffer** buffer)
  191. {
  192. int fenceFd = -1;
  193. int result = dequeueBuffer(window, buffer, &fenceFd);
  194. sp<Fence> fence(new Fence(fenceFd));
  195. int waitResult = fence->wait(Fence::TIMEOUT_NEVER);
  196. if (waitResult != OK) {
  197. ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an "
  198. "error: %d", waitResult);
  199. return waitResult;
  200. }
  201. return result;
  202. }
  203. int FramebufferNativeWindow::dequeueBuffer(ANativeWindow* window,
  204. ANativeWindowBuffer** buffer, int* fenceFd)
  205. {
  206. FramebufferNativeWindow* self = getSelf(window);
  207. Mutex::Autolock _l(self->mutex);
  208. int index = self->mBufferHead++;
  209. if (self->mBufferHead >= self->mNumBuffers)
  210. self->mBufferHead = 0;
  211. // wait for a free non-front buffer
  212. while (self->mNumFreeBuffers < 2) {
  213. self->mCondition.wait(self->mutex);
  214. }
  215. ALOG_ASSERT(self->buffers[index] != self->front, "");
  216. // get this buffer
  217. self->mNumFreeBuffers--;
  218. self->mCurrentBufferIndex = index;
  219. *buffer = self->buffers[index].get();
  220. *fenceFd = -1;
  221. return 0;
  222. }
  223. int FramebufferNativeWindow::lockBuffer_DEPRECATED(ANativeWindow* /*window*/,
  224. ANativeWindowBuffer* /*buffer*/)
  225. {
  226. return NO_ERROR;
  227. }
  228. int FramebufferNativeWindow::queueBuffer_DEPRECATED(ANativeWindow* window,
  229. ANativeWindowBuffer* buffer)
  230. {
  231. return queueBuffer(window, buffer, -1);
  232. }
  233. int FramebufferNativeWindow::queueBuffer(ANativeWindow* window,
  234. ANativeWindowBuffer* buffer, int fenceFd)
  235. {
  236. FramebufferNativeWindow* self = getSelf(window);
  237. Mutex::Autolock _l(self->mutex);
  238. framebuffer_device_t* fb = self->fbDev;
  239. buffer_handle_t handle = static_cast<NativeBuffer*>(buffer)->handle;
  240. sp<Fence> fence(new Fence(fenceFd));
  241. fence->wait(Fence::TIMEOUT_NEVER);
  242. int res = fb->post(fb, handle);
  243. self->front = static_cast<NativeBuffer*>(buffer);
  244. self->mNumFreeBuffers++;
  245. self->mCondition.broadcast();
  246. return res;
  247. }
  248. int FramebufferNativeWindow::query(const ANativeWindow* window,
  249. int what, int* value)
  250. {
  251. const FramebufferNativeWindow* self = getSelf(window);
  252. Mutex::Autolock _l(self->mutex);
  253. framebuffer_device_t* fb = self->fbDev;
  254. switch (what) {
  255. case NATIVE_WINDOW_WIDTH:
  256. *value = static_cast<int>(fb->width);
  257. return NO_ERROR;
  258. case NATIVE_WINDOW_HEIGHT:
  259. *value = static_cast<int>(fb->height);
  260. return NO_ERROR;
  261. case NATIVE_WINDOW_FORMAT:
  262. *value = fb->format;
  263. return NO_ERROR;
  264. case NATIVE_WINDOW_CONCRETE_TYPE:
  265. *value = NATIVE_WINDOW_FRAMEBUFFER;
  266. return NO_ERROR;
  267. case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
  268. *value = 0;
  269. return NO_ERROR;
  270. case NATIVE_WINDOW_DEFAULT_WIDTH:
  271. *value = static_cast<int>(fb->width);
  272. return NO_ERROR;
  273. case NATIVE_WINDOW_DEFAULT_HEIGHT:
  274. *value = static_cast<int>(fb->height);
  275. return NO_ERROR;
  276. case NATIVE_WINDOW_TRANSFORM_HINT:
  277. *value = 0;
  278. return NO_ERROR;
  279. }
  280. *value = 0;
  281. return BAD_VALUE;
  282. }
  283. int FramebufferNativeWindow::perform(ANativeWindow* /*window*/,
  284. int operation, ...)
  285. {
  286. switch (operation) {
  287. case NATIVE_WINDOW_CONNECT:
  288. case NATIVE_WINDOW_DISCONNECT:
  289. case NATIVE_WINDOW_SET_USAGE:
  290. case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
  291. case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
  292. case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
  293. case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
  294. case NATIVE_WINDOW_API_CONNECT:
  295. case NATIVE_WINDOW_API_DISCONNECT:
  296. // TODO: we should implement these
  297. return NO_ERROR;
  298. case NATIVE_WINDOW_LOCK:
  299. case NATIVE_WINDOW_UNLOCK_AND_POST:
  300. case NATIVE_WINDOW_SET_CROP:
  301. case NATIVE_WINDOW_SET_BUFFER_COUNT:
  302. case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
  303. case NATIVE_WINDOW_SET_SCALING_MODE:
  304. return INVALID_OPERATION;
  305. }
  306. return NAME_NOT_FOUND;
  307. }
  308. // ----------------------------------------------------------------------------
  309. }; // namespace android
  310. // ----------------------------------------------------------------------------
  311. using android::sp;
  312. using android::FramebufferNativeWindow;
  313. EGLNativeWindowType android_createDisplaySurface(void)
  314. {
  315. FramebufferNativeWindow* w;
  316. w = new FramebufferNativeWindow();
  317. if (w->getDevice() == NULL) {
  318. // get a ref so it can be destroyed when we exit this block
  319. sp<FramebufferNativeWindow> ref(w);
  320. return NULL;
  321. }
  322. return static_cast<EGLNativeWindowType>(w);
  323. }