OSDN Git Service

6f078d9a9b8fa7cd7901e1e594e34fde437f68f4
[android-x86/external-drm_hwcomposer.git] / drmframebuffer.h
1 /*
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>
21
22 #include <sync/sync.h>
23
24 #include <ui/GraphicBuffer.h>
25
26 namespace android {
27
28 struct DrmFramebuffer {
29   DrmFramebuffer() : release_fence_fd_(-1) {
30   }
31
32   ~DrmFramebuffer() {
33     if (release_fence_fd() >= 0)
34       close(release_fence_fd());
35   }
36
37   bool is_valid() {
38     return buffer_ != NULL;
39   }
40
41   sp<GraphicBuffer> buffer() {
42     return buffer_;
43   }
44
45   int release_fence_fd() {
46     return release_fence_fd_;
47   }
48
49   void set_release_fence_fd(int fd) {
50     if (release_fence_fd_ >= 0)
51       close(release_fence_fd_);
52     release_fence_fd_ = fd;
53   }
54
55   bool Allocate(uint32_t w, uint32_t h) {
56     if (is_valid()) {
57       if (buffer_->getWidth() == w && buffer_->getHeight() == h)
58         return true;
59
60       if (release_fence_fd_ >= 0) {
61         if (sync_wait(release_fence_fd_, -1) != 0) {
62           return false;
63         }
64       }
65       Clear();
66     }
67     buffer_ = new GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888,
68                                 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_HW_RENDER |
69                                     GRALLOC_USAGE_HW_COMPOSER);
70     release_fence_fd_ = -1;
71     return is_valid();
72   }
73
74   void Clear() {
75     if (!is_valid())
76       return;
77
78     if (release_fence_fd_ >= 0) {
79       close(release_fence_fd_);
80       release_fence_fd_ = -1;
81     }
82
83     buffer_.clear();
84   }
85
86   int WaitReleased(int timeout_milliseconds) {
87     if (!is_valid())
88       return 0;
89     if (release_fence_fd_ < 0)
90       return 0;
91
92     int ret = sync_wait(release_fence_fd_, timeout_milliseconds);
93     return ret;
94   }
95
96  private:
97   sp<GraphicBuffer> buffer_;
98   int release_fence_fd_;
99 };
100 }
101
102 #endif  // ANDROID_DRM_FRAMEBUFFER_