Skip to content

Commit df578c3

Browse files
author
Jim Shargo
committed
BufferQueues: clean up constructors for GLConsumer and CpuConsumer
These currently use flags that muddy up client code. We've added nice new *::create methods, so lets use those and clean things up. BYPASS_IGBP_IGBC_API_REASON=warren buffers Bug: 398822412 Flag: EXEMPT refactor Test: builds, presubmit Change-Id: If86b22a6ddaed044afdaa06a2e2fb1c7a79ba941
1 parent fa43de9 commit df578c3

10 files changed

Lines changed: 109 additions & 38 deletions

cmds/flatland/GLHelper.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -202,26 +202,13 @@ bool GLHelper::getShaderProgram(const char* name, GLuint* outPgm) {
202202
}
203203

204204
bool GLHelper::createNamedSurfaceTexture(GLuint name, uint32_t w, uint32_t h,
205-
sp<GLConsumer>* glConsumer, EGLSurface* surface) {
206-
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
207-
sp<GLConsumer> glc = new GLConsumer(name, GL_TEXTURE_EXTERNAL_OES, false, true);
205+
sp<GLConsumer>* glConsumer, EGLSurface* surface) {
206+
auto [glc, surf] = GLConsumer::create(name, GL_TEXTURE_EXTERNAL_OES, false, true);
208207
glc->setDefaultBufferSize(w, h);
209-
glc->getSurface()->setMaxDequeuedBufferCount(2);
210208
glc->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
209+
surf->setMaxDequeuedBufferCount(2);
210+
sp<ANativeWindow> anw = surf;
211211

212-
sp<ANativeWindow> anw = glc->getSurface();
213-
#else
214-
sp<IGraphicBufferProducer> producer;
215-
sp<IGraphicBufferConsumer> consumer;
216-
BufferQueue::createBufferQueue(&producer, &consumer);
217-
sp<GLConsumer> glc = new GLConsumer(consumer, name,
218-
GL_TEXTURE_EXTERNAL_OES, false, true);
219-
glc->setDefaultBufferSize(w, h);
220-
producer->setMaxDequeuedBufferCount(2);
221-
glc->setConsumerUsageBits(GRALLOC_USAGE_HW_COMPOSER);
222-
223-
sp<ANativeWindow> anw = new Surface(producer);
224-
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
225212
EGLSurface s = eglCreateWindowSurface(mDisplay, mConfig, anw.get(), nullptr);
226213
if (s == EGL_NO_SURFACE) {
227214
fprintf(stderr, "eglCreateWindowSurface error: %#x\n", eglGetError());

libs/gui/CpuConsumer.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020

2121
#include <com_android_graphics_libgui_flags.h>
2222
#include <gui/BufferItem.h>
23+
#include <gui/BufferQueue.h>
2324
#include <gui/CpuConsumer.h>
25+
#include <gui/IGraphicBufferConsumer.h>
26+
#include <gui/IGraphicBufferProducer.h>
27+
#include <gui/Surface.h>
2428
#include <utils/Log.h>
2529

2630
#define CC_LOGV(x, ...) ALOGV("[%s] " x, mName.c_str(), ##__VA_ARGS__)
@@ -31,6 +35,28 @@
3135

3236
namespace android {
3337

38+
std::tuple<sp<CpuConsumer>, sp<Surface>> CpuConsumer::create(size_t maxLockedBuffers,
39+
bool controlledByApp,
40+
bool isConsumerSurfaceFlinger) {
41+
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
42+
sp<CpuConsumer> consumer =
43+
sp<CpuConsumer>::make(maxLockedBuffers, controlledByApp, isConsumerSurfaceFlinger);
44+
return {consumer, consumer->getSurface()};
45+
#else
46+
sp<IGraphicBufferProducer> igbp;
47+
sp<IGraphicBufferConsumer> igbc;
48+
BufferQueue::createBufferQueue(&igbp, &igbc, isConsumerSurfaceFlinger);
49+
50+
return {sp<CpuConsumer>::make(igbc, maxLockedBuffers, controlledByApp),
51+
sp<Surface>::make(igbp, controlledByApp)};
52+
#endif
53+
}
54+
55+
sp<CpuConsumer> CpuConsumer::create(const sp<IGraphicBufferConsumer>& bq, size_t maxLockedBuffers,
56+
bool controlledByApp) {
57+
return sp<CpuConsumer>::make(bq, maxLockedBuffers, controlledByApp);
58+
}
59+
3460
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
3561
CpuConsumer::CpuConsumer(size_t maxLockedBuffers, bool controlledByApp,
3662
bool isConsumerSurfaceFlinger)

libs/gui/GLConsumer.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <gui/DebugEGLImageTracker.h>
3838
#include <gui/GLConsumer.h>
3939
#include <gui/ISurfaceComposer.h>
40+
#include <gui/Surface.h>
4041
#include <gui/SurfaceComposerClient.h>
4142

4243
#include <private/gui/ComposerService.h>
@@ -101,6 +102,50 @@ static bool hasEglProtectedContent() {
101102
return hasIt;
102103
}
103104

105+
std::tuple<sp<GLConsumer>, sp<Surface>> GLConsumer::create(uint32_t tex, uint32_t textureTarget,
106+
bool useFenceSync,
107+
bool isControlledByApp) {
108+
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
109+
sp<GLConsumer> consumer =
110+
sp<GLConsumer>::make(tex, textureTarget, useFenceSync, isControlledByApp);
111+
return {consumer, consumer->getSurface()};
112+
#else
113+
sp<IGraphicBufferProducer> igbp;
114+
sp<IGraphicBufferConsumer> igbc;
115+
BufferQueue::createBufferQueue(&igbp, &igbc);
116+
117+
return {sp<GLConsumer>::make(igbc, tex, textureTarget, useFenceSync, isControlledByApp),
118+
sp<Surface>::make(igbp, isControlledByApp)};
119+
#endif
120+
}
121+
122+
std::tuple<sp<GLConsumer>, sp<Surface>> GLConsumer::create(uint32_t textureTarget,
123+
bool useFenceSync,
124+
bool isControlledByApp) {
125+
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
126+
sp<GLConsumer> consumer = sp<GLConsumer>::make(textureTarget, useFenceSync, isControlledByApp);
127+
return {consumer, consumer->getSurface()};
128+
#else
129+
sp<IGraphicBufferProducer> igbp;
130+
sp<IGraphicBufferConsumer> igbc;
131+
BufferQueue::createBufferQueue(&igbp, &igbc);
132+
133+
return {sp<GLConsumer>::make(igbc, textureTarget, useFenceSync, isControlledByApp),
134+
sp<Surface>::make(igbp, isControlledByApp)};
135+
#endif
136+
}
137+
138+
sp<GLConsumer> GLConsumer::create(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
139+
uint32_t textureTarget, bool useFenceSync,
140+
bool isControlledByApp) {
141+
return sp<GLConsumer>::make(bq, tex, textureTarget, useFenceSync, isControlledByApp);
142+
}
143+
144+
sp<GLConsumer> GLConsumer::create(const sp<IGraphicBufferConsumer>& bq, uint32_t textureTarget,
145+
bool useFenceSync, bool isControlledByApp) {
146+
return sp<GLConsumer>::make(bq, textureTarget, useFenceSync, isControlledByApp);
147+
}
148+
104149
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
105150
GLConsumer::GLConsumer(uint32_t tex, uint32_t texTarget, bool useFenceSync, bool isControlledByApp)
106151
: ConsumerBase(isControlledByApp, /* isConsumerSurfaceFlinger */ false),

libs/gui/include/gui/CpuConsumer.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace android {
3131
class BufferQueue;
3232
class GraphicBuffer;
3333
class String8;
34+
class Surface;
3435

3536
/**
3637
* CpuConsumer is a BufferQueue consumer endpoint that allows direct CPU
@@ -92,6 +93,13 @@ class CpuConsumer : public ConsumerBase
9293

9394
// Create a new CPU consumer. The maxLockedBuffers parameter specifies
9495
// how many buffers can be locked for user access at the same time.
96+
static std::tuple<sp<CpuConsumer>, sp<Surface>> create(size_t maxLockedBuffers,
97+
bool controlledByApp = false,
98+
bool isConsumerSurfaceFlinger = false);
99+
static sp<CpuConsumer> create(const sp<IGraphicBufferConsumer>& bq, size_t maxLockedBuffers,
100+
bool controlledByApp = false)
101+
__attribute((deprecated("Prefer ctors that create their own surface and consumer.")));
102+
95103
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
96104
CpuConsumer(size_t maxLockedBuffers, bool controlledByApp = false,
97105
bool isConsumerSurfaceFlinger = false);
@@ -100,8 +108,8 @@ class CpuConsumer : public ConsumerBase
100108
bool controlledByApp = false)
101109
__attribute((deprecated("Prefer ctors that create their own surface and consumer.")));
102110
#else
103-
CpuConsumer(const sp<IGraphicBufferConsumer>& bq,
104-
size_t maxLockedBuffers, bool controlledByApp = false);
111+
CpuConsumer(const sp<IGraphicBufferConsumer>& bq, size_t maxLockedBuffers,
112+
bool controlledByApp = false);
105113
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
106114

107115
// Gets the next graphics buffer from the producer and locks it for CPU use,

libs/gui/include/gui/GLConsumer.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ class GLConsumer : public ConsumerBase {
8383
// If the constructor without the tex parameter is used, the GLConsumer is
8484
// created in a detached state, and attachToContext must be called before
8585
// calls to updateTexImage.
86+
static std::tuple<sp<GLConsumer>, sp<Surface>> create(uint32_t tex, uint32_t textureTarget,
87+
bool useFenceSync,
88+
bool isControlledByApp);
89+
static std::tuple<sp<GLConsumer>, sp<Surface>> create(uint32_t textureTarget, bool useFenceSync,
90+
bool isControlledByApp);
91+
static sp<GLConsumer> create(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
92+
uint32_t textureTarget, bool useFenceSync, bool isControlledByApp)
93+
__attribute((deprecated(
94+
"Prefer create functions that create their own surface and consumer.")));
95+
static sp<GLConsumer> create(const sp<IGraphicBufferConsumer>& bq, uint32_t textureTarget,
96+
bool useFenceSync, bool isControlledByApp)
97+
__attribute((deprecated(
98+
"Prefer create functions that create their own surface and consumer.")));
99+
86100
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
87101
GLConsumer(uint32_t tex, uint32_t textureTarget, bool useFenceSync, bool isControlledByApp);
88102

libs/gui/tests/CpuConsumer_test.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ class CpuConsumerTest : public ::testing::TestWithParam<CpuConsumerTestParams> {
6666
test_info->name(),
6767
params.width, params.height,
6868
params.maxLockedBuffers, params.format);
69-
mCC = new CpuConsumer(params.maxLockedBuffers);
69+
std::tie(mCC, mSTC) = CpuConsumer::create(params.maxLockedBuffers);
7070
String8 name("CpuConsumer_Under_Test");
7171
mCC->setName(name);
72-
mSTC = mCC->getSurface();
7372
mANW = mSTC;
7473
}
7574

libs/gui/tests/MultiTextureConsumer_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class MultiTextureConsumerTest : public GLTest {
3434

3535
virtual void SetUp() {
3636
GLTest::SetUp();
37-
mGlConsumer = new GLConsumer(TEX_ID, GLConsumer::TEXTURE_EXTERNAL, true, false);
38-
mSurface = mGlConsumer->getSurface();
37+
std::tie(mGlConsumer, mSurface) =
38+
GLConsumer::create(TEX_ID, GLConsumer::TEXTURE_EXTERNAL, true, false);
3939
mANW = mSurface.get();
4040

4141
}

libs/gui/tests/SurfaceTextureClient_test.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class SurfaceTextureClientTest : public ::testing::Test {
4040
}
4141

4242
virtual void SetUp() {
43-
mST = new GLConsumer(123, GLConsumer::TEXTURE_EXTERNAL, true, false);
44-
mSTC = mST->getSurface();
43+
std::tie(mST, mSTC) = GLConsumer::create(123, GLConsumer::TEXTURE_EXTERNAL, true, false);
4544
mANW = mSTC;
4645

4746
// We need a valid GL context so we can test updateTexImage()
@@ -727,8 +726,7 @@ class MultiSurfaceTextureClientTest : public ::testing::Test {
727726
ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
728727

729728
for (int i = 0; i < NUM_SURFACE_TEXTURES; i++) {
730-
sp<GLConsumer> st(new GLConsumer(i, GLConsumer::TEXTURE_EXTERNAL, true, false));
731-
sp<Surface> stc = st->getSurface();
729+
auto [st, stc] = GLConsumer::create(i, GLConsumer::TEXTURE_EXTERNAL, true, false);
732730
mEglSurfaces[i] = eglCreateWindowSurface(mEglDisplay, myConfig,
733731
static_cast<ANativeWindow*>(stc.get()), nullptr);
734732
ASSERT_EQ(EGL_SUCCESS, eglGetError());

libs/gui/tests/SurfaceTextureGL.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ class SurfaceTextureGLTest : public GLTest {
3838

3939
void SetUp() {
4040
GLTest::SetUp();
41-
mST = new GLConsumer(TEX_ID, GLConsumer::TEXTURE_EXTERNAL, true, false);
42-
mSTC = mST->getSurface();
41+
std::tie(mST, mSTC) = GLConsumer::create(TEX_ID, GLConsumer::TEXTURE_EXTERNAL, true, false);
4342
mANW = mSTC;
4443
ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), TEST_PRODUCER_USAGE_BITS));
4544
mTextureRenderer = new TextureRenderer(TEX_ID, mST);

libs/gui/tests/Surface_test.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,8 @@ TEST_F(SurfaceTest, QueryConsumerUsage) {
307307
TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
308308
const android_dataspace TEST_DATASPACE = HAL_DATASPACE_V0_SRGB;
309309

310-
sp<CpuConsumer> cpuConsumer = new CpuConsumer(1);
310+
auto [cpuConsumer, s] = CpuConsumer::create(1);
311311
cpuConsumer->setDefaultBufferDataSpace(TEST_DATASPACE);
312-
313-
sp<Surface> s = cpuConsumer->getSurface();
314312
sp<ANativeWindow> anw(s);
315313

316314
android_dataspace dataSpace;
@@ -323,8 +321,7 @@ TEST_F(SurfaceTest, QueryDefaultBuffersDataSpace) {
323321
}
324322

325323
TEST_F(SurfaceTest, SettingGenerationNumber) {
326-
sp<CpuConsumer> cpuConsumer = new CpuConsumer(1);
327-
sp<Surface> surface = cpuConsumer->getSurface();
324+
auto [cpuConsumer, surface] = CpuConsumer::create(1);
328325
sp<ANativeWindow> window(surface);
329326

330327
// Allocate a buffer with a generation number of 0
@@ -2178,8 +2175,7 @@ TEST_F(SurfaceTest, BatchOperations) {
21782175
const int BUFFER_COUNT = 16;
21792176
const int BATCH_SIZE = 8;
21802177

2181-
sp<CpuConsumer> cpuConsumer = new CpuConsumer(1);
2182-
sp<Surface> surface = cpuConsumer->getSurface();
2178+
auto [cpuConsumer, surface] = CpuConsumer::create(1);
21832179
sp<ANativeWindow> window(surface);
21842180
sp<StubSurfaceListener> listener = new StubSurfaceListener();
21852181

@@ -2227,8 +2223,7 @@ TEST_F(SurfaceTest, BatchIllegalOperations) {
22272223
const int BUFFER_COUNT = 16;
22282224
const int BATCH_SIZE = 8;
22292225

2230-
sp<CpuConsumer> cpuConsumer = new CpuConsumer(1);
2231-
sp<Surface> surface = cpuConsumer->getSurface();
2226+
auto [cpuConsumer, surface] = CpuConsumer::create(1);
22322227
sp<ANativeWindow> window(surface);
22332228
sp<StubSurfaceListener> listener = new StubSurfaceListener();
22342229

0 commit comments

Comments
 (0)