• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 displayToken = SurfaceComposerClient::getInternalDisplayToken();
40     if (displayToken == nullptr) {
41         fprintf(stderr, "ERROR: no display\n");
42         return;
43     }
44 
45     ui::DisplayMode displayMode;
46     err = SurfaceComposerClient::getActiveDisplayMode(displayToken, &displayMode);
47     if (err != NO_ERROR) {
48         fprintf(stderr, "ERROR: unable to get active display mode\n");
49         return;
50     }
51 
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 = displayMode.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);
66     }
67 
68     sp<SurfaceControl> sc = surfaceComposerClient->createSurface(
69             String8("Benchmark"), width, height,
70             PIXEL_FORMAT_RGBX_8888, ISurfaceComposerClient::eOpaque);
71     if (sc == nullptr || !sc->isValid()) {
72         fprintf(stderr, "Failed to create SurfaceControl\n");
73         return;
74     }
75 
76     SurfaceComposerClient::Transaction{}
77             .setLayer(sc, 0x7FFFFFFF)
78             .show(sc)
79             .apply();
80 
81     mSurfaceControl = sc;
82 }
83 
getSurface() const84 EGLNativeWindowType WindowSurface::getSurface() const {
85     sp<ANativeWindow> anw = mSurfaceControl->getSurface();
86     return (EGLNativeWindowType) anw.get();
87 }
88 
89