Skip to content

Commit 8269429

Browse files
Treehugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "Stabilize libgui RegionSamplingTests" into main
2 parents 799de38 + f2d8a82 commit 8269429

10 files changed

Lines changed: 188 additions & 3 deletions

libs/gui/SurfaceComposerClient.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3020,13 +3020,34 @@ status_t SurfaceComposerClient::addRegionSamplingListener(
30203020
return statusTFromBinderStatus(status);
30213021
}
30223022

3023+
status_t SurfaceComposerClient::addRegionSamplingListenerWithStopLayerId(
3024+
const Rect& samplingArea, const int32_t stopLayerId,
3025+
const sp<IRegionSamplingListener>& listener) {
3026+
gui::ARect rect;
3027+
rect.left = samplingArea.left;
3028+
rect.top = samplingArea.top;
3029+
rect.right = samplingArea.right;
3030+
rect.bottom = samplingArea.bottom;
3031+
binder::Status status =
3032+
ComposerServiceAIDL::getComposerService()
3033+
->addRegionSamplingListenerWithStopLayerId(rect, stopLayerId, listener);
3034+
return statusTFromBinderStatus(status);
3035+
}
3036+
30233037
status_t SurfaceComposerClient::removeRegionSamplingListener(
30243038
const sp<IRegionSamplingListener>& listener) {
30253039
binder::Status status =
30263040
ComposerServiceAIDL::getComposerService()->removeRegionSamplingListener(listener);
30273041
return statusTFromBinderStatus(status);
30283042
}
30293043

3044+
status_t SurfaceComposerClient::getRegionSamplingListeners(
3045+
std::vector<gui::RegionSamplingDescriptor>* listeners) {
3046+
binder::Status status =
3047+
ComposerServiceAIDL::getComposerService()->getRegionSamplingListeners(listeners);
3048+
return statusTFromBinderStatus(status);
3049+
}
3050+
30303051
status_t SurfaceComposerClient::addFpsListener(int32_t taskId,
30313052
const sp<gui::IFpsListener>& listener) {
30323053
binder::Status status =

libs/gui/aidl/android/gui/ISurfaceComposer.aidl

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import android.gui.IJankListener;
4747
import android.gui.LayerCaptureArgs;
4848
import android.gui.OverlayProperties;
4949
import android.gui.PullAtomData;
50+
import android.gui.RegionSamplingDescriptor;
5051
import android.gui.ScreenCaptureResults;
5152
import android.gui.ARect;
5253
import android.gui.SchedulingPolicy;
@@ -350,19 +351,42 @@ interface ISurfaceComposer {
350351
* The sampling area is bounded by both samplingArea and the given stopLayerHandle
351352
* (i.e., only layers behind the stop layer will be captured and sampled).
352353
*
353-
* Multiple listeners may be provided so long as they have independent listeners.
354-
* If multiple listeners are provided, the effective sampling region for each listener will
355-
* be bounded by whichever stop layer has a lower Z value.
354+
* Multiple listeners for the same sampling region may be provided so long as they have
355+
* independent IRegionSamplingListener objects. If multiple listeners are provided, the
356+
* effective sampling region for each listener will be bounded by whichever stop layer has
357+
* a lower Z-value.
356358
*
357359
* Requires the same permissions as captureLayers and captureScreen.
358360
*/
359361
void addRegionSamplingListener(in ARect samplingArea, @nullable IBinder stopLayerHandle, IRegionSamplingListener listener);
360362

363+
/**
364+
* Registers a listener by stopLayerId to stream median luma updates from SurfaceFlinger.
365+
*
366+
* The sampling area is bounded by both samplingArea and the given stopLayerId
367+
* (i.e., only layers behind the stop layer will be captured and sampled).
368+
*
369+
* Multiple listeners for the same sampling region may be provided so long as they have
370+
* independent IRegionSamplingListener objects. If multiple listeners are provided, the
371+
* effective sampling region for each listener will be bounded by whichever stop layer has
372+
* a lower Z-value.
373+
*
374+
* Requires the ACCESS_SURFACE_FLINGER permission.
375+
*/
376+
void addRegionSamplingListenerWithStopLayerId(in ARect samplingArea, int stopLayerId, IRegionSamplingListener listener);
377+
361378
/**
362379
* Removes a listener that was streaming median luma updates from SurfaceFlinger.
363380
*/
364381
void removeRegionSamplingListener(IRegionSamplingListener listener);
365382

383+
/**
384+
* Gets all listeners that are streaming median luma updates from SurfaceFlinger.
385+
*
386+
* Requires the ACCESS_SURFACE_FLINGER permission.
387+
*/
388+
List<RegionSamplingDescriptor> getRegionSamplingListeners();
389+
366390
/**
367391
* Registers a listener that streams fps updates from SurfaceFlinger.
368392
*
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2025 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+
package android.gui;
18+
19+
import android.gui.ARect;
20+
import android.gui.IRegionSamplingListener;
21+
22+
/** @hide */
23+
parcelable RegionSamplingDescriptor {
24+
// Sampling area region
25+
ARect area;
26+
27+
// All layers under this layer ID will be sampled from
28+
int stopLayerId;
29+
30+
// Listener receiving median luma notifications
31+
IRegionSamplingListener listener;
32+
}

libs/gui/include/gui/SurfaceComposerClient.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
#include <android/gui/BnJankListener.h>
4747
#include <android/gui/ISurfaceComposerClient.h>
48+
#include <android/gui/RegionSamplingDescriptor.h>
4849

4950
#include <gui/BufferReleaseChannel.h>
5051
#include <gui/CpuConsumer.h>
@@ -838,7 +839,11 @@ class SurfaceComposerClient : public RefBase
838839
static status_t addRegionSamplingListener(const Rect& samplingArea,
839840
const sp<IBinder>& stopLayerHandle,
840841
const sp<IRegionSamplingListener>& listener);
842+
static status_t addRegionSamplingListenerWithStopLayerId(
843+
const Rect& samplingArea, const int32_t stopLayerId,
844+
const sp<IRegionSamplingListener>& listener);
841845
static status_t removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener);
846+
static status_t getRegionSamplingListeners(std::vector<gui::RegionSamplingDescriptor>*);
842847
static status_t addFpsListener(int32_t taskId, const sp<gui::IFpsListener>& listener);
843848
static status_t removeFpsListener(const sp<gui::IFpsListener>& listener);
844849
static status_t addTunnelModeEnabledListener(

libs/gui/tests/RegionSampling_test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,23 @@ struct RegionSamplingTest : ::testing::Test {
212212
.setPosition(mTopLayer, 0, 0)
213213
.show(mBackgroundLayer)
214214
.apply();
215+
216+
// Cache any existing listeners that could impact luma sampling test results
217+
sp<gui::ISurfaceComposer> composer = ComposerServiceAIDL::getComposerService();
218+
composer->getRegionSamplingListeners(&mExistingListeners);
219+
for (const auto& descriptor : mExistingListeners) {
220+
composer->removeRegionSamplingListener(descriptor.listener);
221+
}
222+
}
223+
224+
void TearDown() override {
225+
// Restore device state by re-adding listeners that were present prior to the test run
226+
sp<gui::ISurfaceComposer> composer = ComposerServiceAIDL::getComposerService();
227+
for (const auto& descriptor : mExistingListeners) {
228+
composer->addRegionSamplingListenerWithStopLayerId(descriptor.area,
229+
descriptor.stopLayerId,
230+
descriptor.listener);
231+
}
215232
}
216233

217234
void fill_render(uint32_t rgba_value) {
@@ -234,6 +251,7 @@ struct RegionSamplingTest : ::testing::Test {
234251
sp<SurfaceControl> mBackgroundLayer;
235252
sp<SurfaceControl> mContentLayer;
236253
sp<SurfaceControl> mTopLayer;
254+
std::vector<gui::RegionSamplingDescriptor> mExistingListeners;
237255

238256
uint32_t const rgba_green = 0xFF00FF00;
239257
float const luma_green = 0.7152;

libs/gui/tests/Surface_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,11 +879,22 @@ class FakeSurfaceComposerAIDL : public gui::ISurfaceComposer {
879879
return binder::Status::ok();
880880
}
881881

882+
binder::Status addRegionSamplingListenerWithStopLayerId(
883+
const gui::ARect& /*samplingArea*/, const int32_t /*stopLayerId*/,
884+
const sp<gui::IRegionSamplingListener>& /*listener*/) override {
885+
return binder::Status::ok();
886+
}
887+
882888
binder::Status removeRegionSamplingListener(
883889
const sp<gui::IRegionSamplingListener>& /*listener*/) override {
884890
return binder::Status::ok();
885891
}
886892

893+
binder::Status getRegionSamplingListeners(
894+
std::vector<gui::RegionSamplingDescriptor>*) override {
895+
return binder::Status::ok();
896+
}
897+
887898
binder::Status addFpsListener(int32_t /*taskId*/,
888899
const sp<gui::IFpsListener>& /*listener*/) override {
889900
return binder::Status::ok();

services/surfaceflinger/RegionSamplingThread.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,25 @@ void RegionSamplingThread::removeListener(const sp<IRegionSamplingListener>& lis
141141
mDescriptors.erase(wp<IBinder>(IInterface::asBinder(listener)));
142142
}
143143

144+
const std::vector<gui::RegionSamplingDescriptor> RegionSamplingThread::getListeners() {
145+
std::lock_guard lock(mSamplingMutex);
146+
std::vector<gui::RegionSamplingDescriptor> listeners;
147+
for (const auto& [listener, descriptor] : mDescriptors) {
148+
gui::ARect guiRect;
149+
guiRect.left = descriptor.area.left;
150+
guiRect.top = descriptor.area.top;
151+
guiRect.right = descriptor.area.right;
152+
guiRect.bottom = descriptor.area.bottom;
153+
154+
gui::RegionSamplingDescriptor guiDescriptor;
155+
guiDescriptor.area = guiRect;
156+
guiDescriptor.stopLayerId = descriptor.stopLayerId;
157+
guiDescriptor.listener = descriptor.listener;
158+
listeners.emplace_back(guiDescriptor);
159+
}
160+
return listeners;
161+
}
162+
144163
void RegionSamplingThread::checkForStaleLuma() {
145164
std::lock_guard lock(mThreadControlMutex);
146165

services/surfaceflinger/RegionSamplingThread.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <android-base/thread_annotations.h>
2020
#include <android/gui/IRegionSamplingListener.h>
21+
#include <android/gui/RegionSamplingDescriptor.h>
2122
#include <binder/IBinder.h>
2223
#include <renderengine/ExternalTexture.h>
2324
#include <ui/GraphicBuffer.h>
@@ -77,6 +78,8 @@ class RegionSamplingThread : public IBinder::DeathRecipient {
7778
const sp<IRegionSamplingListener>& listener);
7879
// Remove the listener to stop receiving median luma notifications.
7980
void removeListener(const sp<IRegionSamplingListener>& listener);
81+
// Gets all listeners that are receiving median luma notifications.
82+
const std::vector<gui::RegionSamplingDescriptor> getListeners();
8083

8184
// Notifies sampling engine that composition is done and new content is
8285
// available, and the deadline for the sampling work on the main thread to

services/surfaceflinger/SurfaceFlinger.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,18 @@ status_t SurfaceFlinger::addRegionSamplingListener(const Rect& samplingArea,
20572057
return NO_ERROR;
20582058
}
20592059

2060+
status_t SurfaceFlinger::addRegionSamplingListenerWithStopLayerId(
2061+
const Rect& samplingArea, const int32_t stopLayerId,
2062+
const sp<IRegionSamplingListener>& listener) {
2063+
if (!listener || samplingArea == Rect::INVALID_RECT || samplingArea.isEmpty()) {
2064+
return BAD_VALUE;
2065+
}
2066+
2067+
mRegionSamplingThread->addListener(samplingArea,
2068+
stopLayerId ? stopLayerId : UNASSIGNED_LAYER_ID, listener);
2069+
return NO_ERROR;
2070+
}
2071+
20602072
status_t SurfaceFlinger::removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener) {
20612073
if (!listener) {
20622074
return BAD_VALUE;
@@ -2065,6 +2077,12 @@ status_t SurfaceFlinger::removeRegionSamplingListener(const sp<IRegionSamplingLi
20652077
return NO_ERROR;
20662078
}
20672079

2080+
status_t SurfaceFlinger::getRegionSamplingListeners(
2081+
std::vector<gui::RegionSamplingDescriptor>* listeners) const {
2082+
*listeners = mRegionSamplingThread->getListeners();
2083+
return NO_ERROR;
2084+
}
2085+
20682086
status_t SurfaceFlinger::addFpsListener(int32_t taskId, const sp<gui::IFpsListener>& listener) {
20692087
if (!listener) {
20702088
return BAD_VALUE;
@@ -9358,6 +9376,22 @@ binder::Status SurfaceComposerAIDL::addRegionSamplingListener(
93589376
return binderStatusFromStatusT(status);
93599377
}
93609378

9379+
binder::Status SurfaceComposerAIDL::addRegionSamplingListenerWithStopLayerId(
9380+
const gui::ARect& samplingArea, const int32_t stopLayerId,
9381+
const sp<gui::IRegionSamplingListener>& listener) {
9382+
status_t status = checkAccessPermission();
9383+
if (status != OK) {
9384+
return binderStatusFromStatusT(status);
9385+
}
9386+
android::Rect rect;
9387+
rect.left = samplingArea.left;
9388+
rect.top = samplingArea.top;
9389+
rect.right = samplingArea.right;
9390+
rect.bottom = samplingArea.bottom;
9391+
status = mFlinger->addRegionSamplingListenerWithStopLayerId(rect, stopLayerId, listener);
9392+
return binderStatusFromStatusT(status);
9393+
}
9394+
93619395
binder::Status SurfaceComposerAIDL::removeRegionSamplingListener(
93629396
const sp<gui::IRegionSamplingListener>& listener) {
93639397
status_t status = checkReadFrameBufferPermission();
@@ -9367,6 +9401,15 @@ binder::Status SurfaceComposerAIDL::removeRegionSamplingListener(
93679401
return binderStatusFromStatusT(status);
93689402
}
93699403

9404+
binder::Status SurfaceComposerAIDL::getRegionSamplingListeners(
9405+
std::vector<gui::RegionSamplingDescriptor>* listeners) {
9406+
status_t status = checkAccessPermission();
9407+
if (status == OK) {
9408+
status = mFlinger->getRegionSamplingListeners(listeners);
9409+
}
9410+
return binderStatusFromStatusT(status);
9411+
}
9412+
93709413
binder::Status SurfaceComposerAIDL::addFpsListener(int32_t taskId,
93719414
const sp<gui::IFpsListener>& listener) {
93729415
status_t status = checkReadFrameBufferPermission();

services/surfaceflinger/SurfaceFlinger.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,12 @@ class SurfaceFlinger : public BnSurfaceComposer,
602602
status_t isWideColorDisplay(const sp<IBinder>& displayToken, bool* outIsWideColorDisplay) const;
603603
status_t addRegionSamplingListener(const Rect& samplingArea, const sp<IBinder>& stopLayerHandle,
604604
const sp<IRegionSamplingListener>& listener);
605+
status_t addRegionSamplingListenerWithStopLayerId(const Rect& samplingArea,
606+
const int32_t stopLayerId,
607+
const sp<IRegionSamplingListener>& listener);
605608
status_t removeRegionSamplingListener(const sp<IRegionSamplingListener>& listener);
609+
status_t getRegionSamplingListeners(
610+
std::vector<gui::RegionSamplingDescriptor>* listeners) const;
606611
status_t addFpsListener(int32_t taskId, const sp<gui::IFpsListener>& listener);
607612
status_t removeFpsListener(const sp<gui::IFpsListener>& listener);
608613
status_t addTunnelModeEnabledListener(const sp<gui::ITunnelModeEnabledListener>& listener);
@@ -1640,8 +1645,12 @@ class SurfaceComposerAIDL : public gui::BnSurfaceComposer {
16401645
binder::Status addRegionSamplingListener(
16411646
const gui::ARect& samplingArea, const sp<IBinder>& stopLayerHandle,
16421647
const sp<gui::IRegionSamplingListener>& listener) override;
1648+
binder::Status addRegionSamplingListenerWithStopLayerId(
1649+
const gui::ARect& samplingArea, const int32_t stopLayerId,
1650+
const sp<gui::IRegionSamplingListener>& listener) override;
16431651
binder::Status removeRegionSamplingListener(
16441652
const sp<gui::IRegionSamplingListener>& listener) override;
1653+
binder::Status getRegionSamplingListeners(std::vector<gui::RegionSamplingDescriptor>*) override;
16451654
binder::Status addFpsListener(int32_t taskId, const sp<gui::IFpsListener>& listener) override;
16461655
binder::Status removeFpsListener(const sp<gui::IFpsListener>& listener) override;
16471656
binder::Status addTunnelModeEnabledListener(

0 commit comments

Comments
 (0)