Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 1 | /* |
| 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 Luo | ecc1f90 | 2021-11-20 11:55:05 -0800 | [diff] [blame] | 23 | #include <android/gui/BnRegionSamplingListener.h> |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 24 | #include <binder/IPCThreadState.h> |
| 25 | #include <binder/ProcessState.h> |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 26 | #include <gui/ISurfaceComposer.h> |
| 27 | #include <gui/SurfaceComposerClient.h> |
| 28 | #include <gui/SurfaceControl.h> |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 29 | #include <private/gui/ComposerServiceAIDL.h> |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 30 | #include <utils/Trace.h> |
| 31 | |
| 32 | using namespace std::chrono_literals; |
| 33 | |
| 34 | namespace android { |
| 35 | |
Huihong Luo | ecc1f90 | 2021-11-20 11:55:05 -0800 | [diff] [blame] | 36 | class Button : public gui::BnRegionSamplingListener { |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 37 | public: |
| 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 Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 42 | ISurfaceComposerClient::eFXSurfaceEffect); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 43 | |
| 44 | const int32_t width = samplingArea.getWidth(); |
| 45 | const int32_t height = samplingArea.getHeight(); |
| 46 | |
| 47 | SurfaceComposerClient::Transaction{} |
| 48 | .setLayer(mButton, 0x7fffffff) |
chaviw | 2571450 | 2021-02-11 10:01:08 -0800 | [diff] [blame] | 49 | .setCrop(mButton, {0, 0, width - 2 * BUTTON_PADDING, height - 2 * BUTTON_PADDING}) |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 50 | .setPosition(mButton, samplingArea.left + BUTTON_PADDING, |
| 51 | samplingArea.top + BUTTON_PADDING) |
| 52 | .setColor(mButton, half3{1, 1, 1}) |
| 53 | .show(mButton) |
| 54 | .apply(); |
| 55 | |
| 56 | mButtonBlend = client->createSurface(String8(name) + "Blend", 0, 0, PIXEL_FORMAT_RGBA_8888, |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 57 | ISurfaceComposerClient::eFXSurfaceEffect); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 58 | |
| 59 | SurfaceComposerClient::Transaction{} |
| 60 | .setLayer(mButtonBlend, 0x7ffffffe) |
chaviw | 2571450 | 2021-02-11 10:01:08 -0800 | [diff] [blame] | 61 | .setCrop(mButtonBlend, |
| 62 | {0, 0, width - 2 * SAMPLE_AREA_PADDING, height - 2 * SAMPLE_AREA_PADDING}) |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 63 | .setPosition(mButtonBlend, samplingArea.left + SAMPLE_AREA_PADDING, |
| 64 | samplingArea.top + SAMPLE_AREA_PADDING) |
| 65 | .setColor(mButtonBlend, half3{1, 1, 1}) |
| 66 | .setAlpha(mButtonBlend, 0.2) |
| 67 | .show(mButtonBlend) |
| 68 | .apply(true); |
| 69 | |
| 70 | const bool HIGHLIGHT_SAMPLING_AREA = false; |
| 71 | if (HIGHLIGHT_SAMPLING_AREA) { |
| 72 | mSamplingArea = |
| 73 | client->createSurface(String8("SamplingArea"), 0, 0, PIXEL_FORMAT_RGBA_8888, |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 74 | ISurfaceComposerClient::eFXSurfaceEffect); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 75 | |
| 76 | SurfaceComposerClient::Transaction{} |
| 77 | .setLayer(mSamplingArea, 0x7ffffffd) |
chaviw | 2571450 | 2021-02-11 10:01:08 -0800 | [diff] [blame] | 78 | .setCrop(mSamplingArea, {0, 0, 100, 32}) |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 79 | .setPosition(mSamplingArea, 490, 1606) |
| 80 | .setColor(mSamplingArea, half3{0, 1, 0}) |
| 81 | .setAlpha(mSamplingArea, 0.1) |
| 82 | .show(mSamplingArea) |
| 83 | .apply(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | sp<IBinder> getStopLayerHandle() { return mButtonBlend->getHandle(); } |
| 88 | |
| 89 | private: |
| 90 | static const int32_t BLEND_WIDTH = 2; |
| 91 | static const int32_t SAMPLE_AREA_PADDING = 8; |
| 92 | static const int32_t BUTTON_PADDING = BLEND_WIDTH + SAMPLE_AREA_PADDING; |
| 93 | |
| 94 | void setColor(float color) { |
| 95 | const float complement = std::fmod(color + 0.5f, 1.0f); |
| 96 | SurfaceComposerClient::Transaction{} |
| 97 | .setColor(mButton, half3{complement, complement, complement}) |
| 98 | .setColor(mButtonBlend, half3{color, color, color}) |
| 99 | .apply(); |
| 100 | } |
| 101 | |
Huihong Luo | ecc1f90 | 2021-11-20 11:55:05 -0800 | [diff] [blame] | 102 | binder::Status onSampleCollected(float medianLuma) override { |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 103 | ATRACE_CALL(); |
| 104 | setColor(medianLuma); |
Huihong Luo | ecc1f90 | 2021-11-20 11:55:05 -0800 | [diff] [blame] | 105 | return binder::Status::ok(); |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | sp<SurfaceComposerClient> mClient; |
| 109 | sp<SurfaceControl> mButton; |
| 110 | sp<SurfaceControl> mButtonBlend; |
| 111 | sp<SurfaceControl> mSamplingArea; |
| 112 | }; |
| 113 | |
| 114 | } // namespace android |
| 115 | |
| 116 | using namespace android; |
| 117 | |
| 118 | int main(int, const char**) { |
| 119 | const Rect homeButtonArea{490, 1606, 590, 1654}; |
| 120 | sp<android::Button> homeButton = new android::Button("HomeButton", homeButtonArea); |
| 121 | const Rect backButtonArea{200, 1606, 248, 1654}; |
| 122 | sp<android::Button> backButton = new android::Button("BackButton", backButtonArea); |
| 123 | |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 124 | gui::ARect homeButtonAreaA; |
| 125 | homeButtonAreaA.left = 490; |
| 126 | homeButtonAreaA.top = 1606; |
| 127 | homeButtonAreaA.right = 590; |
| 128 | homeButtonAreaA.bottom = 1654; |
| 129 | |
| 130 | gui::ARect backButtonAreaA; |
| 131 | backButtonAreaA.left = 200; |
| 132 | backButtonAreaA.top = 1606; |
| 133 | backButtonAreaA.right = 248; |
| 134 | backButtonAreaA.bottom = 1654; |
| 135 | |
| 136 | sp<gui::ISurfaceComposer> composer = ComposerServiceAIDL::getComposerService(); |
| 137 | composer->addRegionSamplingListener(homeButtonAreaA, homeButton->getStopLayerHandle(), |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 138 | homeButton); |
Huihong Luo | 02186fb | 2022-02-23 14:21:54 -0800 | [diff] [blame] | 139 | composer->addRegionSamplingListener(backButtonAreaA, backButton->getStopLayerHandle(), |
Dan Stoza | ec46008 | 2018-12-17 15:35:09 -0800 | [diff] [blame] | 140 | backButton); |
| 141 | |
| 142 | ProcessState::self()->startThreadPool(); |
| 143 | IPCThreadState::self()->joinThreadPool(); |
| 144 | |
| 145 | return 0; |
| 146 | } |