blob: 33ca0db8e03b92623d7ae59d48a9fadf3e5e8376 [file] [log] [blame]
Zach Reizner713a6782015-07-31 15:12:44 -07001/*
2 * Copyright (C) 2015 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#ifndef ANDROID_DRM_FRAMEBUFFER_
18#define ANDROID_DRM_FRAMEBUFFER_
19
20#include <stdint.h>
Zach Reizner713a6782015-07-31 15:12:44 -070021#include <sync/sync.h>
Zach Reizner713a6782015-07-31 15:12:44 -070022#include <ui/GraphicBuffer.h>
23
24namespace android {
25
26struct DrmFramebuffer {
27 DrmFramebuffer() : release_fence_fd_(-1) {
28 }
29
30 ~DrmFramebuffer() {
31 if (release_fence_fd() >= 0)
32 close(release_fence_fd());
33 }
34
35 bool is_valid() {
36 return buffer_ != NULL;
37 }
38
39 sp<GraphicBuffer> buffer() {
40 return buffer_;
41 }
42
43 int release_fence_fd() {
44 return release_fence_fd_;
45 }
46
47 void set_release_fence_fd(int fd) {
48 if (release_fence_fd_ >= 0)
49 close(release_fence_fd_);
50 release_fence_fd_ = fd;
51 }
52
53 bool Allocate(uint32_t w, uint32_t h) {
54 if (is_valid()) {
55 if (buffer_->getWidth() == w && buffer_->getHeight() == h)
56 return true;
57
58 if (release_fence_fd_ >= 0) {
Sean Pauld106b912015-09-29 00:56:00 -040059 if (sync_wait(release_fence_fd_, kReleaseWaitTimeoutMs) != 0) {
60 ALOGE("Wait for release fence failed\n");
Zach Reizner713a6782015-07-31 15:12:44 -070061 return false;
62 }
63 }
64 Clear();
65 }
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010066 buffer_ = new GraphicBuffer(w, h, PIXEL_FORMAT_RGB_888,
Zach Reizner713a6782015-07-31 15:12:44 -070067 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_HW_RENDER |
68 GRALLOC_USAGE_HW_COMPOSER);
69 release_fence_fd_ = -1;
70 return is_valid();
71 }
72
73 void Clear() {
74 if (!is_valid())
75 return;
76
77 if (release_fence_fd_ >= 0) {
78 close(release_fence_fd_);
79 release_fence_fd_ = -1;
80 }
81
82 buffer_.clear();
83 }
84
85 int WaitReleased(int timeout_milliseconds) {
86 if (!is_valid())
87 return 0;
88 if (release_fence_fd_ < 0)
89 return 0;
90
91 int ret = sync_wait(release_fence_fd_, timeout_milliseconds);
92 return ret;
93 }
94
Sean Pauld106b912015-09-29 00:56:00 -040095 // Somewhat arbitrarily chosen, but wanted to stay below 3000ms, which is the
96 // system timeout
97 static const int kReleaseWaitTimeoutMs = 1500;
98
Zach Reizner713a6782015-07-31 15:12:44 -070099 private:
100 sp<GraphicBuffer> buffer_;
101 int release_fence_fd_;
102};
Sean Paulf72cccd2018-08-27 13:59:08 -0400103} // namespace android
Zach Reizner713a6782015-07-31 15:12:44 -0700104
105#endif // ANDROID_DRM_FRAMEBUFFER_