forked from yrzroger/NativeSFDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeSurfaceWrapper.cpp
More file actions
executable file
·86 lines (70 loc) · 3.13 KB
/
NativeSurfaceWrapper.cpp
File metadata and controls
executable file
·86 lines (70 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright (C) 2021 The Android Open Source Project
*/
#define LOG_TAG "NativeSurfaceWrapper"
#include <android-base/properties.h>
#include <gui/ISurfaceComposerClient.h>
#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>
#include <utils/Log.h>
#include "NativeSurfaceWrapper.h"
namespace android {
NativeSurfaceWrapper::NativeSurfaceWrapper(const String8& name, uint32_t layerStack)
: mName(name), mLayerStack(layerStack) {}
void NativeSurfaceWrapper::onFirstRef() {
sp<SurfaceComposerClient> surfaceComposerClient = new SurfaceComposerClient;
status_t err = surfaceComposerClient->initCheck();
if (err != NO_ERROR) {
ALOGD("SurfaceComposerClient::initCheck error: %#x\n", err);
return;
}
// Get main display parameters.
sp<IBinder> displayToken = SurfaceComposerClient::getInternalDisplayToken();
if (displayToken == nullptr)
return;
ui::DisplayMode displayMode;
const status_t error =
SurfaceComposerClient::getActiveDisplayMode(displayToken, &displayMode);
if (error != NO_ERROR)
return;
ui::Size resolution = displayMode.resolution;
resolution = limitSurfaceSize(resolution.width, resolution.height);
// create the native surface
sp<SurfaceControl> surfaceControl = surfaceComposerClient->createSurface(mName, resolution.getWidth(),
resolution.getHeight(), PIXEL_FORMAT_RGBA_8888,
ISurfaceComposerClient::eFXSurfaceBufferState,
/*parent*/ nullptr);
SurfaceComposerClient::Transaction{}
.setLayer(surfaceControl, std::numeric_limits<int32_t>::max())
.show(surfaceControl)
.setLayerStack(surfaceControl, mLayerStack)
.apply();
mSurfaceControl = surfaceControl;
mWidth = resolution.getWidth();
mHeight = resolution.getHeight();
}
sp<ANativeWindow> NativeSurfaceWrapper::getSurface() const {
sp<ANativeWindow> anw = mSurfaceControl->getSurface();
return anw;
}
ui::Size NativeSurfaceWrapper::limitSurfaceSize(int width, int height) const {
ui::Size limited(width, height);
bool wasLimited = false;
const float aspectRatio = float(width) / float(height);
int maxWidth = android::base::GetIntProperty("ro.surface_flinger.max_graphics_width", 0);
int maxHeight = android::base::GetIntProperty("ro.surface_flinger.max_graphics_height", 0);
if (maxWidth != 0 && width > maxWidth) {
limited.height = maxWidth / aspectRatio;
limited.width = maxWidth;
wasLimited = true;
}
if (maxHeight != 0 && limited.height > maxHeight) {
limited.height = maxHeight;
limited.width = maxHeight * aspectRatio;
wasLimited = true;
}
SLOGV_IF(wasLimited, "Surface size has been limited to [%dx%d] from [%dx%d]",
limited.width, limited.height, width, height);
return limited;
}
} // namespace android