Haoxiang Li | 95917b1 | 2019-11-15 11:29:05 -0800 | [diff] [blame^] | 1 | // |
| 2 | // Copyright (C) 2019 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 | #include <ui/DisplayInfo.h> |
| 17 | #include <gui/bufferqueue/2.0/B2HGraphicBufferProducer.h> |
| 18 | |
| 19 | #include "CarWindowService.h" |
| 20 | |
| 21 | namespace android { |
| 22 | namespace frameworks { |
| 23 | namespace automotive { |
| 24 | namespace display { |
| 25 | namespace V1_0 { |
| 26 | namespace implementation { |
| 27 | |
| 28 | Return<sp<IGraphicBufferProducer>> |
| 29 | CarWindowService::getIGraphicBufferProducer() { |
| 30 | if (mSurface == nullptr) { |
| 31 | status_t err; |
| 32 | mSurfaceComposerClient = new SurfaceComposerClient(); |
| 33 | |
| 34 | err = mSurfaceComposerClient->initCheck(); |
| 35 | if (err != NO_ERROR) { |
| 36 | ALOGE("SurfaceComposerClient::initCheck error: %#x", err); |
| 37 | mSurfaceComposerClient = nullptr; |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
| 41 | // Get main display parameters. |
| 42 | sp<IBinder> mainDpy = SurfaceComposerClient::getInternalDisplayToken(); |
| 43 | if (mainDpy == nullptr) { |
| 44 | ALOGE("Failed to get internal display "); |
| 45 | return nullptr; |
| 46 | } |
| 47 | DisplayInfo mainDpyInfo; |
| 48 | err = SurfaceComposerClient::getDisplayInfo(mainDpy, &mainDpyInfo); |
| 49 | if (err != NO_ERROR) { |
| 50 | ALOGE("Failed to get display characteristics"); |
| 51 | return nullptr; |
| 52 | } |
| 53 | unsigned int mWidth, mHeight; |
| 54 | if (mainDpyInfo.orientation != ui::ROTATION_0 && |
| 55 | mainDpyInfo.orientation != ui::ROTATION_180) { |
| 56 | // rotated |
| 57 | mWidth = mainDpyInfo.h; |
| 58 | mHeight = mainDpyInfo.w; |
| 59 | } else { |
| 60 | mWidth = mainDpyInfo.w; |
| 61 | mHeight = mainDpyInfo.h; |
| 62 | } |
| 63 | |
| 64 | mSurfaceControl = mSurfaceComposerClient->createSurface( |
| 65 | String8("Automotive Display"), mWidth, mHeight, |
| 66 | PIXEL_FORMAT_RGBX_8888, ISurfaceComposerClient::eOpaque); |
| 67 | if (mSurfaceControl == nullptr || !mSurfaceControl->isValid()) { |
| 68 | ALOGE("Failed to create SurfaceControl"); |
| 69 | mSurfaceComposerClient = nullptr; |
| 70 | mSurfaceControl = nullptr; |
| 71 | return nullptr; |
| 72 | } |
| 73 | |
| 74 | // SurfaceControl::getSurface is guaranteed to be not null. |
| 75 | mSurface = mSurfaceControl->getSurface(); |
| 76 | } |
| 77 | |
| 78 | return new ::android::hardware::graphics::bufferqueue::V2_0::utils:: |
| 79 | B2HGraphicBufferProducer( |
| 80 | mSurface->getIGraphicBufferProducer()); |
| 81 | } |
| 82 | |
| 83 | Return<bool> CarWindowService::showWindow() { |
| 84 | status_t status = NO_ERROR; |
| 85 | |
| 86 | if (mSurfaceControl != nullptr) { |
| 87 | status = SurfaceComposerClient::Transaction{} |
| 88 | .setLayer( |
| 89 | mSurfaceControl, 0x7FFFFFFF) // always on top |
| 90 | .show(mSurfaceControl) |
| 91 | .apply(); |
| 92 | } else { |
| 93 | ALOGE("showWindow: Failed to get a valid SurfaceControl!"); |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | return status == NO_ERROR; |
| 98 | } |
| 99 | |
| 100 | Return<bool> CarWindowService::hideWindow() { |
| 101 | status_t status = NO_ERROR; |
| 102 | |
| 103 | if (mSurfaceControl != nullptr) { |
| 104 | status = SurfaceComposerClient::Transaction{} |
| 105 | .hide(mSurfaceControl) |
| 106 | .apply(); |
| 107 | } else { |
| 108 | ALOGE("hideWindow: Failed to get a valid SurfaceControl!"); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | return status == NO_ERROR; |
| 113 | } |
| 114 | |
| 115 | } // namespace implementation |
| 116 | } // namespace V1_0 |
| 117 | } // namespace display |
| 118 | } // namespace automotive |
| 119 | } // namespace frameworks |
| 120 | } // namespace android |
| 121 | |