blob: fd4522e75739e309e5a34967b258acec1fd70547 [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
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080039 const auto displayToken = SurfaceComposerClient::getInternalDisplayToken();
40 if (displayToken == nullptr) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080041 fprintf(stderr, "ERROR: no display\n");
42 return;
43 }
44
Marin Shalamanova7fe3042021-01-29 21:02:08 +010045 ui::DisplayMode displayMode;
46 err = SurfaceComposerClient::getActiveDisplayMode(displayToken, &displayMode);
Andy McFadden6ef57d72014-03-05 15:06:53 -080047 if (err != NO_ERROR) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +010048 fprintf(stderr, "ERROR: unable to get active display mode\n");
Andy McFadden6ef57d72014-03-05 15:06:53 -080049 return;
50 }
51
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080052 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
Marin Shalamanova7fe3042021-01-29 21:02:08 +010059 const ui::Size& resolution = displayMode.resolution;
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080060 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 McFadden709d7de2014-03-12 09:50:15 -070066 }
67
Andy McFadden6ef57d72014-03-05 15:06:53 -080068 sp<SurfaceControl> sc = surfaceComposerClient->createSurface(
Andy McFadden709d7de2014-03-12 09:50:15 -070069 String8("Benchmark"), width, height,
Andy McFaddenf3ed9a22014-03-25 14:59:26 -070070 PIXEL_FORMAT_RGBX_8888, ISurfaceComposerClient::eOpaque);
Yi Kong48a6cd22018-07-18 10:07:09 -070071 if (sc == nullptr || !sc->isValid()) {
Andy McFadden6ef57d72014-03-05 15:06:53 -080072 fprintf(stderr, "Failed to create SurfaceControl\n");
73 return;
74 }
75
Robert Carr4cdc58f2017-08-23 14:22:20 -070076 SurfaceComposerClient::Transaction{}
77 .setLayer(sc, 0x7FFFFFFF)
78 .show(sc)
79 .apply();
Andy McFadden6ef57d72014-03-05 15:06:53 -080080
81 mSurfaceControl = sc;
82}
83
84EGLNativeWindowType WindowSurface::getSurface() const {
85 sp<ANativeWindow> anw = mSurfaceControl->getSurface();
86 return (EGLNativeWindowType) anw.get();
87}
88