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
19 #include <utility>
20
21 #include <gui/ISurfaceComposer.h>
22 #include <gui/Surface.h>
23 #include <gui/SurfaceComposerClient.h>
24 #include <ui/DisplayMode.h>
25 #include <ui/DisplayState.h>
26
27 using namespace android;
28
WindowSurface()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
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());
47 if (displayToken == nullptr) {
48 fprintf(stderr, "ERROR: no display\n");
49 return;
50 }
51
52 ui::DisplayMode displayMode;
53 err = SurfaceComposerClient::getActiveDisplayMode(displayToken, &displayMode);
54 if (err != NO_ERROR) {
55 fprintf(stderr, "ERROR: unable to get active display mode\n");
56 return;
57 }
58
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
66 const ui::Size& resolution = displayMode.resolution;
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);
73 }
74
75 sp<SurfaceControl> sc = surfaceComposerClient->createSurface(
76 String8("Benchmark"), width, height,
77 PIXEL_FORMAT_RGBX_8888, ISurfaceComposerClient::eOpaque);
78 if (sc == nullptr || !sc->isValid()) {
79 fprintf(stderr, "Failed to create SurfaceControl\n");
80 return;
81 }
82
83 SurfaceComposerClient::Transaction{}
84 .setLayer(sc, 0x7FFFFFFF)
85 .show(sc)
86 .apply();
87
88 mSurfaceControl = sc;
89 }
90
getSurface() const91 EGLNativeWindowType WindowSurface::getSurface() const {
92 sp<ANativeWindow> anw = mSurfaceControl->getSurface();
93 return (EGLNativeWindowType) anw.get();
94 }
95
96