blob: e94b565f11619759bcfb0e5da0ab9f0098500828 [file] [log] [blame]
Andy McFadden6ef57d72014-03-05 15:06:53 -08001/*
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 Laskowski3cb3d4e2019-11-21 11:14:45 -080019#include <utility>
20
Andy McFadden6ef57d72014-03-05 15:06:53 -080021#include <gui/ISurfaceComposer.h>
22#include <gui/Surface.h>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080023#include <gui/SurfaceComposerClient.h>
Marin Shalamanova7fe3042021-01-29 21:02:08 +010024#include <ui/DisplayMode.h>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080025#include <ui/DisplayState.h>
Andy McFadden6ef57d72014-03-05 15:06:53 -080026
27using namespace android;
28
29WindowSurface::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 Luo31b5ac22022-08-15 20:38:10 -070039 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 Laskowski3cb3d4e2019-11-21 11:14:45 -080047 if (displayToken == nullptr) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080048 fprintf(stderr, "ERROR: no display\n");
49 return;
50 }
51
Marin Shalamanova7fe3042021-01-29 21:02:08 +010052 ui::DisplayMode displayMode;
53 err = SurfaceComposerClient::getActiveDisplayMode(displayToken, &displayMode);
Andy McFadden6ef57d72014-03-05 15:06:53 -080054 if (err != NO_ERROR) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +010055 fprintf(stderr, "ERROR: unable to get active display mode\n");
Andy McFadden6ef57d72014-03-05 15:06:53 -080056 return;
57 }
58
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080059 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 Shalamanova7fe3042021-01-29 21:02:08 +010066 const ui::Size& resolution = displayMode.resolution;
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080067 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 McFadden709d7de2014-03-12 09:50:15 -070073 }
74
Andy McFadden6ef57d72014-03-05 15:06:53 -080075 sp<SurfaceControl> sc = surfaceComposerClient->createSurface(
Andy McFadden709d7de2014-03-12 09:50:15 -070076 String8("Benchmark"), width, height,
Andy McFaddenf3ed9a22014-03-25 14:59:26 -070077 PIXEL_FORMAT_RGBX_8888, ISurfaceComposerClient::eOpaque);
Yi Kong48a6cd22018-07-18 10:07:09 -070078 if (sc == nullptr || !sc->isValid()) {
Andy McFadden6ef57d72014-03-05 15:06:53 -080079 fprintf(stderr, "Failed to create SurfaceControl\n");
80 return;
81 }
82
Robert Carr4cdc58f2017-08-23 14:22:20 -070083 SurfaceComposerClient::Transaction{}
84 .setLayer(sc, 0x7FFFFFFF)
85 .show(sc)
86 .apply();
Andy McFadden6ef57d72014-03-05 15:06:53 -080087
88 mSurfaceControl = sc;
89}
90
91EGLNativeWindowType WindowSurface::getSurface() const {
92 sp<ANativeWindow> anw = mSurfaceControl->getSurface();
93 return (EGLNativeWindowType) anw.get();
94}
95