| 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> | 
|  | 24 | #include <ui/DisplayConfig.h> | 
|  | 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 |  | 
| Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 39 | const auto displayToken = SurfaceComposerClient::getInternalDisplayToken(); | 
|  | 40 | if (displayToken == nullptr) { | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 41 | fprintf(stderr, "ERROR: no display\n"); | 
|  | 42 | return; | 
|  | 43 | } | 
|  | 44 |  | 
| Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 45 | DisplayConfig displayConfig; | 
|  | 46 | err = SurfaceComposerClient::getActiveDisplayConfig(displayToken, &displayConfig); | 
| Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 47 | if (err != NO_ERROR) { | 
| Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 48 | fprintf(stderr, "ERROR: unable to get active display config\n"); | 
| Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 49 | return; | 
|  | 50 | } | 
|  | 51 |  | 
| Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 52 | ui::DisplayState displayState; | 
|  | 53 | err = SurfaceComposerClient::getDisplayState(displayToken, &displayState); | 
|  | 54 | if (err != NO_ERROR) { | 
|  | 55 | fprintf(stderr, "ERROR: unable to get display state\n"); | 
|  | 56 | return; | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | const ui::Size& resolution = displayConfig.resolution; | 
|  | 60 | auto width = resolution.getWidth(); | 
|  | 61 | auto height = resolution.getHeight(); | 
|  | 62 |  | 
|  | 63 | if (displayState.orientation == ui::ROTATION_90 || | 
|  | 64 | displayState.orientation == ui::ROTATION_270) { | 
|  | 65 | std::swap(width, height); | 
| Andy McFadden | 709d7de | 2014-03-12 09:50:15 -0700 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
| Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 68 | sp<SurfaceControl> sc = surfaceComposerClient->createSurface( | 
| Andy McFadden | 709d7de | 2014-03-12 09:50:15 -0700 | [diff] [blame] | 69 | String8("Benchmark"), width, height, | 
| Andy McFadden | f3ed9a2 | 2014-03-25 14:59:26 -0700 | [diff] [blame] | 70 | PIXEL_FORMAT_RGBX_8888, ISurfaceComposerClient::eOpaque); | 
| Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 71 | if (sc == nullptr || !sc->isValid()) { | 
| Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 72 | fprintf(stderr, "Failed to create SurfaceControl\n"); | 
|  | 73 | return; | 
|  | 74 | } | 
|  | 75 |  | 
| Robert Carr | 4cdc58f | 2017-08-23 14:22:20 -0700 | [diff] [blame] | 76 | SurfaceComposerClient::Transaction{} | 
|  | 77 | .setLayer(sc, 0x7FFFFFFF) | 
|  | 78 | .show(sc) | 
|  | 79 | .apply(); | 
| Andy McFadden | 6ef57d7 | 2014-03-05 15:06:53 -0800 | [diff] [blame] | 80 |  | 
|  | 81 | mSurfaceControl = sc; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | EGLNativeWindowType WindowSurface::getSurface() const { | 
|  | 85 | sp<ANativeWindow> anw = mSurfaceControl->getSurface(); | 
|  | 86 | return (EGLNativeWindowType) anw.get(); | 
|  | 87 | } | 
|  | 88 |  |