| Changyeon Jo | ab614c4 | 2020-02-03 16:34:50 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2020 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 | #include <gui/Surface.h> | 
|  | 18 | #include <gui/bufferqueue/2.0/H2BGraphicBufferProducer.h> | 
|  | 19 |  | 
|  | 20 | #include "include/bufferqueueconverter/BufferQueueConverter.h" | 
|  | 21 |  | 
|  | 22 |  | 
|  | 23 | using ::android::Surface; | 
|  | 24 | using ::android::IGraphicBufferProducer; | 
|  | 25 | using ::android::hardware::graphics::bufferqueue::V2_0::utils::H2BGraphicBufferProducer; | 
|  | 26 |  | 
|  | 27 |  | 
|  | 28 | namespace android { | 
|  | 29 |  | 
|  | 30 | struct SurfaceHolder { | 
|  | 31 | sp<Surface> surface; | 
|  | 32 | SurfaceHolder(const sp<Surface>& s) : surface(s) {} | 
|  | 33 | }; | 
|  | 34 |  | 
|  | 35 | /** | 
|  | 36 | * Custom deleter for SurfaceHolder unique pointer | 
|  | 37 | */ | 
|  | 38 | void destroySurfaceHolder(SurfaceHolder* surfaceHolder) { | 
|  | 39 | delete surfaceHolder; | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 |  | 
|  | 43 | SurfaceHolderUniquePtr getSurfaceFromHGBP(const sp<HGraphicBufferProducer>& token) { | 
|  | 44 | if (token == nullptr) { | 
|  | 45 | ALOGE("Passed IGraphicBufferProducer handle is invalid."); | 
|  | 46 | return SurfaceHolderUniquePtr(nullptr, nullptr); | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | sp<IGraphicBufferProducer> bufferProducer = new H2BGraphicBufferProducer(token); | 
|  | 50 | if (bufferProducer == nullptr) { | 
|  | 51 | ALOGE("Failed to get IGraphicBufferProducer."); | 
|  | 52 | return SurfaceHolderUniquePtr(nullptr, nullptr); | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | sp<Surface> newSurface(new Surface(bufferProducer, true)); | 
|  | 56 | if (newSurface == nullptr) { | 
|  | 57 | ALOGE("Failed to create Surface from HGBP."); | 
|  | 58 | return SurfaceHolderUniquePtr(nullptr, nullptr); | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | return SurfaceHolderUniquePtr(new SurfaceHolder(newSurface), destroySurfaceHolder); | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 |  | 
|  | 65 | ANativeWindow* getNativeWindow(SurfaceHolder* handle) { | 
|  | 66 | if (handle == nullptr) { | 
|  | 67 | ALOGE("SurfaceHolder is invalid."); | 
|  | 68 | return nullptr; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | return static_cast<ANativeWindow*>(handle->surface.get()); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | } // namespace android |