Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 <WindowSurface.h> |
| 18 | |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 19 | #include <utility> |
| 20 | |
Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 21 | #include <gui/ISurfaceComposer.h> |
| 22 | #include <gui/Surface.h> |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 23 | #include <gui/SurfaceComposerClient.h> |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 24 | #include <ui/DisplayMode.h> |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 25 | #include <ui/DisplayState.h> |
Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 26 | |
| 27 | using namespace android; |
| 28 | |
| 29 | WindowSurface::WindowSurface() { |
| 30 | status_t err; |
| 31 | |
| 32 | sp<SurfaceComposerClient> surfaceComposerClient = new SurfaceComposerClient; |
| 33 | err = surfaceComposerClient->initCheck(); |
| 34 | if (err != NO_ERROR) { |
| 35 | fprintf(stderr, "SurfaceComposerClient::initCheck error: %#x\n", err); |
| 36 | return; |
| 37 | } |
| 38 | |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame^] | 39 | const auto ids = SurfaceComposerClient::getPhysicalDisplayIds(); |
| 40 | if (ids.empty()) { |
| 41 | fprintf(stderr, "Failed to get ID for any displays.\n"); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | // display 0 is picked for now, can extend to support all displays if needed |
| 46 | const auto displayToken = SurfaceComposerClient::getPhysicalDisplayToken(ids.front()); |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 47 | if (displayToken == nullptr) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 48 | fprintf(stderr, "ERROR: no display\n"); |
| 49 | return; |
| 50 | } |
| 51 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 52 | ui::DisplayMode displayMode; |
| 53 | err = SurfaceComposerClient::getActiveDisplayMode(displayToken, &displayMode); |
Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 54 | if (err != NO_ERROR) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 55 | fprintf(stderr, "ERROR: unable to get active display mode\n"); |
Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 59 | ui::DisplayState displayState; |
| 60 | err = SurfaceComposerClient::getDisplayState(displayToken, &displayState); |
| 61 | if (err != NO_ERROR) { |
| 62 | fprintf(stderr, "ERROR: unable to get display state\n"); |
| 63 | return; |
| 64 | } |
| 65 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 66 | const ui::Size& resolution = displayMode.resolution; |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 67 | auto width = resolution.getWidth(); |
| 68 | auto height = resolution.getHeight(); |
| 69 | |
| 70 | if (displayState.orientation == ui::ROTATION_90 || |
| 71 | displayState.orientation == ui::ROTATION_270) { |
| 72 | std::swap(width, height); |
Andy McFadden | 709d7de | 2014-03-12 09:50:15 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 75 | sp<SurfaceControl> sc = surfaceComposerClient->createSurface( |
Andy McFadden | 709d7de | 2014-03-12 09:50:15 -0700 | [diff] [blame] | 76 | String8("Benchmark"), width, height, |
Andy McFadden | f3ed9a2 | 2014-03-25 14:59:26 -0700 | [diff] [blame] | 77 | PIXEL_FORMAT_RGBX_8888, ISurfaceComposerClient::eOpaque); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 78 | if (sc == nullptr || !sc->isValid()) { |
Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 79 | fprintf(stderr, "Failed to create SurfaceControl\n"); |
| 80 | return; |
| 81 | } |
| 82 | |
Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 83 | SurfaceComposerClient::Transaction{} |
| 84 | .setLayer(sc, 0x7FFFFFFF) |
| 85 | .show(sc) |
| 86 | .apply(); |
Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 87 | |
| 88 | mSurfaceControl = sc; |
| 89 | } |
| 90 | |
| 91 | EGLNativeWindowType WindowSurface::getSurface() const { |
| 92 | sp<ANativeWindow> anw = mSurfaceControl->getSurface(); |
| 93 | return (EGLNativeWindowType) anw.get(); |
| 94 | } |
| 95 | |