1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright 2019 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "SamplingTest"
19*38e8c45fSAndroid Build Coastguard Worker
20*38e8c45fSAndroid Build Coastguard Worker #include <chrono>
21*38e8c45fSAndroid Build Coastguard Worker #include <thread>
22*38e8c45fSAndroid Build Coastguard Worker
23*38e8c45fSAndroid Build Coastguard Worker #include <android/gui/BnRegionSamplingListener.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <binder/IPCThreadState.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <binder/ProcessState.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <gui/ISurfaceComposer.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <gui/SurfaceComposerClient.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <gui/SurfaceControl.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <private/gui/ComposerServiceAIDL.h>
30*38e8c45fSAndroid Build Coastguard Worker #include <utils/Trace.h>
31*38e8c45fSAndroid Build Coastguard Worker
32*38e8c45fSAndroid Build Coastguard Worker using namespace std::chrono_literals;
33*38e8c45fSAndroid Build Coastguard Worker
34*38e8c45fSAndroid Build Coastguard Worker namespace android {
35*38e8c45fSAndroid Build Coastguard Worker
36*38e8c45fSAndroid Build Coastguard Worker class Button : public gui::BnRegionSamplingListener {
37*38e8c45fSAndroid Build Coastguard Worker public:
Button(const char * name,const Rect & samplingArea)38*38e8c45fSAndroid Build Coastguard Worker Button(const char* name, const Rect& samplingArea) {
39*38e8c45fSAndroid Build Coastguard Worker sp<SurfaceComposerClient> client = new SurfaceComposerClient;
40*38e8c45fSAndroid Build Coastguard Worker
41*38e8c45fSAndroid Build Coastguard Worker mButton = client->createSurface(String8(name), 0, 0, PIXEL_FORMAT_RGBA_8888,
42*38e8c45fSAndroid Build Coastguard Worker ISurfaceComposerClient::eFXSurfaceEffect);
43*38e8c45fSAndroid Build Coastguard Worker
44*38e8c45fSAndroid Build Coastguard Worker const int32_t width = samplingArea.getWidth();
45*38e8c45fSAndroid Build Coastguard Worker const int32_t height = samplingArea.getHeight();
46*38e8c45fSAndroid Build Coastguard Worker
47*38e8c45fSAndroid Build Coastguard Worker SurfaceComposerClient::Transaction{}
48*38e8c45fSAndroid Build Coastguard Worker .setLayer(mButton, 0x7fffffff)
49*38e8c45fSAndroid Build Coastguard Worker .setCrop(mButton,
50*38e8c45fSAndroid Build Coastguard Worker Rect{0, 0, width - 2 * BUTTON_PADDING, height - 2 * BUTTON_PADDING})
51*38e8c45fSAndroid Build Coastguard Worker .setPosition(mButton, samplingArea.left + BUTTON_PADDING,
52*38e8c45fSAndroid Build Coastguard Worker samplingArea.top + BUTTON_PADDING)
53*38e8c45fSAndroid Build Coastguard Worker .setColor(mButton, half3{1, 1, 1})
54*38e8c45fSAndroid Build Coastguard Worker .show(mButton)
55*38e8c45fSAndroid Build Coastguard Worker .apply();
56*38e8c45fSAndroid Build Coastguard Worker
57*38e8c45fSAndroid Build Coastguard Worker mButtonBlend = client->createSurface(String8(name) + "Blend", 0, 0, PIXEL_FORMAT_RGBA_8888,
58*38e8c45fSAndroid Build Coastguard Worker ISurfaceComposerClient::eFXSurfaceEffect);
59*38e8c45fSAndroid Build Coastguard Worker
60*38e8c45fSAndroid Build Coastguard Worker SurfaceComposerClient::Transaction{}
61*38e8c45fSAndroid Build Coastguard Worker .setLayer(mButtonBlend, 0x7ffffffe)
62*38e8c45fSAndroid Build Coastguard Worker .setCrop(mButtonBlend,
63*38e8c45fSAndroid Build Coastguard Worker Rect{0, 0, width - 2 * SAMPLE_AREA_PADDING,
64*38e8c45fSAndroid Build Coastguard Worker height - 2 * SAMPLE_AREA_PADDING})
65*38e8c45fSAndroid Build Coastguard Worker .setPosition(mButtonBlend, samplingArea.left + SAMPLE_AREA_PADDING,
66*38e8c45fSAndroid Build Coastguard Worker samplingArea.top + SAMPLE_AREA_PADDING)
67*38e8c45fSAndroid Build Coastguard Worker .setColor(mButtonBlend, half3{1, 1, 1})
68*38e8c45fSAndroid Build Coastguard Worker .setAlpha(mButtonBlend, 0.2)
69*38e8c45fSAndroid Build Coastguard Worker .show(mButtonBlend)
70*38e8c45fSAndroid Build Coastguard Worker .apply(true);
71*38e8c45fSAndroid Build Coastguard Worker
72*38e8c45fSAndroid Build Coastguard Worker const bool HIGHLIGHT_SAMPLING_AREA = false;
73*38e8c45fSAndroid Build Coastguard Worker if (HIGHLIGHT_SAMPLING_AREA) {
74*38e8c45fSAndroid Build Coastguard Worker mSamplingArea =
75*38e8c45fSAndroid Build Coastguard Worker client->createSurface(String8("SamplingArea"), 0, 0, PIXEL_FORMAT_RGBA_8888,
76*38e8c45fSAndroid Build Coastguard Worker ISurfaceComposerClient::eFXSurfaceEffect);
77*38e8c45fSAndroid Build Coastguard Worker
78*38e8c45fSAndroid Build Coastguard Worker SurfaceComposerClient::Transaction{}
79*38e8c45fSAndroid Build Coastguard Worker .setLayer(mSamplingArea, 0x7ffffffd)
80*38e8c45fSAndroid Build Coastguard Worker .setCrop(mSamplingArea, Rect{0, 0, 100, 32})
81*38e8c45fSAndroid Build Coastguard Worker .setPosition(mSamplingArea, 490, 1606)
82*38e8c45fSAndroid Build Coastguard Worker .setColor(mSamplingArea, half3{0, 1, 0})
83*38e8c45fSAndroid Build Coastguard Worker .setAlpha(mSamplingArea, 0.1)
84*38e8c45fSAndroid Build Coastguard Worker .show(mSamplingArea)
85*38e8c45fSAndroid Build Coastguard Worker .apply();
86*38e8c45fSAndroid Build Coastguard Worker }
87*38e8c45fSAndroid Build Coastguard Worker }
88*38e8c45fSAndroid Build Coastguard Worker
getStopLayerHandle()89*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> getStopLayerHandle() { return mButtonBlend->getHandle(); }
90*38e8c45fSAndroid Build Coastguard Worker
91*38e8c45fSAndroid Build Coastguard Worker private:
92*38e8c45fSAndroid Build Coastguard Worker static const int32_t BLEND_WIDTH = 2;
93*38e8c45fSAndroid Build Coastguard Worker static const int32_t SAMPLE_AREA_PADDING = 8;
94*38e8c45fSAndroid Build Coastguard Worker static const int32_t BUTTON_PADDING = BLEND_WIDTH + SAMPLE_AREA_PADDING;
95*38e8c45fSAndroid Build Coastguard Worker
setColor(float color)96*38e8c45fSAndroid Build Coastguard Worker void setColor(float color) {
97*38e8c45fSAndroid Build Coastguard Worker const float complement = std::fmod(color + 0.5f, 1.0f);
98*38e8c45fSAndroid Build Coastguard Worker SurfaceComposerClient::Transaction{}
99*38e8c45fSAndroid Build Coastguard Worker .setColor(mButton, half3{complement, complement, complement})
100*38e8c45fSAndroid Build Coastguard Worker .setColor(mButtonBlend, half3{color, color, color})
101*38e8c45fSAndroid Build Coastguard Worker .apply();
102*38e8c45fSAndroid Build Coastguard Worker }
103*38e8c45fSAndroid Build Coastguard Worker
onSampleCollected(float medianLuma)104*38e8c45fSAndroid Build Coastguard Worker binder::Status onSampleCollected(float medianLuma) override {
105*38e8c45fSAndroid Build Coastguard Worker ATRACE_CALL();
106*38e8c45fSAndroid Build Coastguard Worker setColor(medianLuma);
107*38e8c45fSAndroid Build Coastguard Worker return binder::Status::ok();
108*38e8c45fSAndroid Build Coastguard Worker }
109*38e8c45fSAndroid Build Coastguard Worker
110*38e8c45fSAndroid Build Coastguard Worker sp<SurfaceComposerClient> mClient;
111*38e8c45fSAndroid Build Coastguard Worker sp<SurfaceControl> mButton;
112*38e8c45fSAndroid Build Coastguard Worker sp<SurfaceControl> mButtonBlend;
113*38e8c45fSAndroid Build Coastguard Worker sp<SurfaceControl> mSamplingArea;
114*38e8c45fSAndroid Build Coastguard Worker };
115*38e8c45fSAndroid Build Coastguard Worker
116*38e8c45fSAndroid Build Coastguard Worker } // namespace android
117*38e8c45fSAndroid Build Coastguard Worker
118*38e8c45fSAndroid Build Coastguard Worker using namespace android;
119*38e8c45fSAndroid Build Coastguard Worker
main(int,const char **)120*38e8c45fSAndroid Build Coastguard Worker int main(int, const char**) {
121*38e8c45fSAndroid Build Coastguard Worker const Rect homeButtonArea{490, 1606, 590, 1654};
122*38e8c45fSAndroid Build Coastguard Worker sp<android::Button> homeButton = new android::Button("HomeButton", homeButtonArea);
123*38e8c45fSAndroid Build Coastguard Worker const Rect backButtonArea{200, 1606, 248, 1654};
124*38e8c45fSAndroid Build Coastguard Worker sp<android::Button> backButton = new android::Button("BackButton", backButtonArea);
125*38e8c45fSAndroid Build Coastguard Worker
126*38e8c45fSAndroid Build Coastguard Worker gui::ARect homeButtonAreaA;
127*38e8c45fSAndroid Build Coastguard Worker homeButtonAreaA.left = 490;
128*38e8c45fSAndroid Build Coastguard Worker homeButtonAreaA.top = 1606;
129*38e8c45fSAndroid Build Coastguard Worker homeButtonAreaA.right = 590;
130*38e8c45fSAndroid Build Coastguard Worker homeButtonAreaA.bottom = 1654;
131*38e8c45fSAndroid Build Coastguard Worker
132*38e8c45fSAndroid Build Coastguard Worker gui::ARect backButtonAreaA;
133*38e8c45fSAndroid Build Coastguard Worker backButtonAreaA.left = 200;
134*38e8c45fSAndroid Build Coastguard Worker backButtonAreaA.top = 1606;
135*38e8c45fSAndroid Build Coastguard Worker backButtonAreaA.right = 248;
136*38e8c45fSAndroid Build Coastguard Worker backButtonAreaA.bottom = 1654;
137*38e8c45fSAndroid Build Coastguard Worker
138*38e8c45fSAndroid Build Coastguard Worker sp<gui::ISurfaceComposer> composer = ComposerServiceAIDL::getComposerService();
139*38e8c45fSAndroid Build Coastguard Worker composer->addRegionSamplingListener(homeButtonAreaA, homeButton->getStopLayerHandle(),
140*38e8c45fSAndroid Build Coastguard Worker homeButton);
141*38e8c45fSAndroid Build Coastguard Worker composer->addRegionSamplingListener(backButtonAreaA, backButton->getStopLayerHandle(),
142*38e8c45fSAndroid Build Coastguard Worker backButton);
143*38e8c45fSAndroid Build Coastguard Worker
144*38e8c45fSAndroid Build Coastguard Worker ProcessState::self()->startThreadPool();
145*38e8c45fSAndroid Build Coastguard Worker IPCThreadState::self()->joinThreadPool();
146*38e8c45fSAndroid Build Coastguard Worker
147*38e8c45fSAndroid Build Coastguard Worker return 0;
148*38e8c45fSAndroid Build Coastguard Worker }
149