• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 #pragma once
18 
19 #ifndef LOG_TAG
20 #warning "ComposerResources.h included without LOG_TAG"
21 #endif
22 
23 #include <composer-resources/2.1/ComposerResources.h>
24 
25 namespace android {
26 namespace hardware {
27 namespace graphics {
28 namespace composer {
29 namespace V2_2 {
30 namespace hal {
31 
32 using V2_1::hal::ComposerHandleCache;
33 using V2_1::hal::ComposerHandleImporter;
34 
35 class ComposerDisplayResource : public V2_1::hal::ComposerDisplayResource {
36   public:
ComposerDisplayResource(DisplayType type,ComposerHandleImporter & importer,uint32_t outputBufferCacheSize)37     ComposerDisplayResource(DisplayType type, ComposerHandleImporter& importer,
38                             uint32_t outputBufferCacheSize)
39         : V2_1::hal::ComposerDisplayResource(type, importer, outputBufferCacheSize),
40           mReadbackBufferCache(importer, ComposerHandleCache::HandleType::BUFFER, 1) {}
41 
getReadbackBuffer(const native_handle_t * inHandle,const native_handle_t ** outHandle,const native_handle ** outReplacedHandle)42     Error getReadbackBuffer(const native_handle_t* inHandle, const native_handle_t** outHandle,
43                             const native_handle** outReplacedHandle) {
44         const uint32_t slot = 0;
45         const bool fromCache = false;
46         return mReadbackBufferCache.getHandle(slot, fromCache, inHandle, outHandle,
47                                               outReplacedHandle);
48     }
49 
50   protected:
51     ComposerHandleCache mReadbackBufferCache;
52 };
53 
54 class ComposerResources : public V2_1::hal::ComposerResources {
55   public:
create()56     static std::unique_ptr<ComposerResources> create() {
57         auto resources = std::make_unique<ComposerResources>();
58         return resources->init() ? std::move(resources) : nullptr;
59     }
60 
getDisplayReadbackBuffer(Display display,const native_handle_t * rawHandle,const native_handle_t ** outHandle,ReplacedHandle * outReplacedHandle)61     Error getDisplayReadbackBuffer(Display display, const native_handle_t* rawHandle,
62                                    const native_handle_t** outHandle,
63                                    ReplacedHandle* outReplacedHandle) {
64         // import buffer
65         const native_handle_t* importedHandle;
66         Error error = mImporter.importBuffer(rawHandle, &importedHandle);
67         if (error != Error::NONE) {
68             return error;
69         }
70 
71         std::lock_guard<std::mutex> lock(mDisplayResourcesMutex);
72 
73         auto iter = mDisplayResources.find(display);
74         if (iter == mDisplayResources.end()) {
75             mImporter.freeBuffer(importedHandle);
76             return Error::BAD_DISPLAY;
77         }
78         ComposerDisplayResource& displayResource =
79                 *static_cast<ComposerDisplayResource*>(iter->second.get());
80 
81         // update cache
82         const native_handle_t* replacedHandle;
83         error = displayResource.getReadbackBuffer(importedHandle, outHandle, &replacedHandle);
84         if (error != Error::NONE) {
85             mImporter.freeBuffer(importedHandle);
86             return error;
87         }
88 
89         outReplacedHandle->reset(&mImporter, replacedHandle);
90         return Error::NONE;
91     }
92 
93   protected:
createDisplayResource(ComposerDisplayResource::DisplayType type,uint32_t outputBufferCacheSize)94     std::unique_ptr<V2_1::hal::ComposerDisplayResource> createDisplayResource(
95             ComposerDisplayResource::DisplayType type, uint32_t outputBufferCacheSize) override {
96         return std::make_unique<ComposerDisplayResource>(type, mImporter, outputBufferCacheSize);
97     }
98 };
99 
100 }  // namespace hal
101 }  // namespace V2_2
102 }  // namespace composer
103 }  // namespace graphics
104 }  // namespace hardware
105 }  // namespace android
106