blob: 8fea689c9196178130aa92720777e7f7ccbe3ece [file] [log] [blame]
Dan Stozaec460082018-12-17 15:35:09 -08001/*
2 * Copyright 2019 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#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18#define LOG_TAG "SamplingTest"
19
20#include <chrono>
21#include <thread>
22
Huihong Luoecc1f902021-11-20 11:55:05 -080023#include <android/gui/BnRegionSamplingListener.h>
Dan Stozaec460082018-12-17 15:35:09 -080024#include <binder/IPCThreadState.h>
25#include <binder/ProcessState.h>
Dan Stozaec460082018-12-17 15:35:09 -080026#include <gui/ISurfaceComposer.h>
27#include <gui/SurfaceComposerClient.h>
28#include <gui/SurfaceControl.h>
Huihong Luo02186fb2022-02-23 14:21:54 -080029#include <private/gui/ComposerServiceAIDL.h>
Dan Stozaec460082018-12-17 15:35:09 -080030#include <utils/Trace.h>
31
32using namespace std::chrono_literals;
33
34namespace android {
35
Huihong Luoecc1f902021-11-20 11:55:05 -080036class Button : public gui::BnRegionSamplingListener {
Dan Stozaec460082018-12-17 15:35:09 -080037public:
38 Button(const char* name, const Rect& samplingArea) {
39 sp<SurfaceComposerClient> client = new SurfaceComposerClient;
40
41 mButton = client->createSurface(String8(name), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -080042 ISurfaceComposerClient::eFXSurfaceEffect);
Dan Stozaec460082018-12-17 15:35:09 -080043
44 const int32_t width = samplingArea.getWidth();
45 const int32_t height = samplingArea.getHeight();
46
47 SurfaceComposerClient::Transaction{}
48 .setLayer(mButton, 0x7fffffff)
Vishnu Naira9123c82024-10-03 03:56:44 +000049 .setCrop(mButton,
50 Rect{0, 0, width - 2 * BUTTON_PADDING, height - 2 * BUTTON_PADDING})
Dan Stozaec460082018-12-17 15:35:09 -080051 .setPosition(mButton, samplingArea.left + BUTTON_PADDING,
52 samplingArea.top + BUTTON_PADDING)
53 .setColor(mButton, half3{1, 1, 1})
54 .show(mButton)
55 .apply();
56
57 mButtonBlend = client->createSurface(String8(name) + "Blend", 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -080058 ISurfaceComposerClient::eFXSurfaceEffect);
Dan Stozaec460082018-12-17 15:35:09 -080059
60 SurfaceComposerClient::Transaction{}
61 .setLayer(mButtonBlend, 0x7ffffffe)
chaviw25714502021-02-11 10:01:08 -080062 .setCrop(mButtonBlend,
Vishnu Naira9123c82024-10-03 03:56:44 +000063 Rect{0, 0, width - 2 * SAMPLE_AREA_PADDING,
64 height - 2 * SAMPLE_AREA_PADDING})
Dan Stozaec460082018-12-17 15:35:09 -080065 .setPosition(mButtonBlend, samplingArea.left + SAMPLE_AREA_PADDING,
66 samplingArea.top + SAMPLE_AREA_PADDING)
67 .setColor(mButtonBlend, half3{1, 1, 1})
68 .setAlpha(mButtonBlend, 0.2)
69 .show(mButtonBlend)
70 .apply(true);
71
72 const bool HIGHLIGHT_SAMPLING_AREA = false;
73 if (HIGHLIGHT_SAMPLING_AREA) {
74 mSamplingArea =
75 client->createSurface(String8("SamplingArea"), 0, 0, PIXEL_FORMAT_RGBA_8888,
Vishnu Nairfa247b12020-02-11 08:58:26 -080076 ISurfaceComposerClient::eFXSurfaceEffect);
Dan Stozaec460082018-12-17 15:35:09 -080077
78 SurfaceComposerClient::Transaction{}
79 .setLayer(mSamplingArea, 0x7ffffffd)
Vishnu Naira9123c82024-10-03 03:56:44 +000080 .setCrop(mSamplingArea, Rect{0, 0, 100, 32})
Dan Stozaec460082018-12-17 15:35:09 -080081 .setPosition(mSamplingArea, 490, 1606)
82 .setColor(mSamplingArea, half3{0, 1, 0})
83 .setAlpha(mSamplingArea, 0.1)
84 .show(mSamplingArea)
85 .apply();
86 }
87 }
88
89 sp<IBinder> getStopLayerHandle() { return mButtonBlend->getHandle(); }
90
91private:
92 static const int32_t BLEND_WIDTH = 2;
93 static const int32_t SAMPLE_AREA_PADDING = 8;
94 static const int32_t BUTTON_PADDING = BLEND_WIDTH + SAMPLE_AREA_PADDING;
95
96 void setColor(float color) {
97 const float complement = std::fmod(color + 0.5f, 1.0f);
98 SurfaceComposerClient::Transaction{}
99 .setColor(mButton, half3{complement, complement, complement})
100 .setColor(mButtonBlend, half3{color, color, color})
101 .apply();
102 }
103
Huihong Luoecc1f902021-11-20 11:55:05 -0800104 binder::Status onSampleCollected(float medianLuma) override {
Dan Stozaec460082018-12-17 15:35:09 -0800105 ATRACE_CALL();
106 setColor(medianLuma);
Huihong Luoecc1f902021-11-20 11:55:05 -0800107 return binder::Status::ok();
Dan Stozaec460082018-12-17 15:35:09 -0800108 }
109
110 sp<SurfaceComposerClient> mClient;
111 sp<SurfaceControl> mButton;
112 sp<SurfaceControl> mButtonBlend;
113 sp<SurfaceControl> mSamplingArea;
114};
115
116} // namespace android
117
118using namespace android;
119
120int main(int, const char**) {
121 const Rect homeButtonArea{490, 1606, 590, 1654};
122 sp<android::Button> homeButton = new android::Button("HomeButton", homeButtonArea);
123 const Rect backButtonArea{200, 1606, 248, 1654};
124 sp<android::Button> backButton = new android::Button("BackButton", backButtonArea);
125
Huihong Luo02186fb2022-02-23 14:21:54 -0800126 gui::ARect homeButtonAreaA;
127 homeButtonAreaA.left = 490;
128 homeButtonAreaA.top = 1606;
129 homeButtonAreaA.right = 590;
130 homeButtonAreaA.bottom = 1654;
131
132 gui::ARect backButtonAreaA;
133 backButtonAreaA.left = 200;
134 backButtonAreaA.top = 1606;
135 backButtonAreaA.right = 248;
136 backButtonAreaA.bottom = 1654;
137
138 sp<gui::ISurfaceComposer> composer = ComposerServiceAIDL::getComposerService();
139 composer->addRegionSamplingListener(homeButtonAreaA, homeButton->getStopLayerHandle(),
Dan Stozaec460082018-12-17 15:35:09 -0800140 homeButton);
Huihong Luo02186fb2022-02-23 14:21:54 -0800141 composer->addRegionSamplingListener(backButtonAreaA, backButton->getStopLayerHandle(),
Dan Stozaec460082018-12-17 15:35:09 -0800142 backButton);
143
144 ProcessState::self()->startThreadPool();
145 IPCThreadState::self()->joinThreadPool();
146
147 return 0;
148}