RESTRICT AUTOMERGE Revert "Consider generation of buffers in pipeline management"

This reverts commit 4f540e07687c79a663869a4fb8195922f975ad00.
Bug: 119676062
Change-Id: I99c2ebece4f0832f7815c8d1a10fe1307ba600c3
(cherry picked from commit 76d2867bd2c655cd5ef3f51601654ed6e9ef5cb9)
diff --git a/media/sfplugin/CCodecBufferChannel.cpp b/media/sfplugin/CCodecBufferChannel.cpp
index 6b2c2a0..6e8bd90 100644
--- a/media/sfplugin/CCodecBufferChannel.cpp
+++ b/media/sfplugin/CCodecBufferChannel.cpp
@@ -1434,19 +1434,6 @@
     return prevOutput + 1;
 }
 
-int CCodecBufferChannel::PipelineCapacity::forceAllocateOutputSlot(
-        const char* callerTag) {
-    int prevOutput = output.fetch_sub(1, std::memory_order_relaxed);
-    ALOGV("[%s] %s -- PipelineCapacity::forceAllocateOutputSlot(): "
-          "pipeline availability -1 output ==> "
-          "input = %d, component = %d, output = %d",
-            mName, callerTag ? callerTag : "*",
-            input.load(std::memory_order_relaxed),
-            component.load(std::memory_order_relaxed),
-            prevOutput - 1);
-    return prevOutput - 1;
-}
-
 // CCodecBufferChannel
 
 CCodecBufferChannel::CCodecBufferChannel(
@@ -1901,6 +1888,29 @@
         return UNKNOWN_ERROR;
     }
 
+    // Query delays
+    C2PortRequestedDelayTuning::input inputDelay;
+    C2PortRequestedDelayTuning::output outputDelay;
+    C2RequestedPipelineDelayTuning pipelineDelay;
+#if 0
+    err = mComponent->query(
+            { &inputDelay, &pipelineDelay, &outputDelay },
+            {},
+            C2_DONT_BLOCK,
+            nullptr);
+    mAvailablePipelineCapacity.initialize(
+            inputDelay,
+            inputDelay + pipelineDelay,
+            inputDelay + pipelineDelay + outputDelay,
+            mName);
+#else
+    mAvailablePipelineCapacity.initialize(
+            kMinInputBufferArraySize,
+            kMaxPipelineCapacity,
+            kMinOutputBufferArraySize,
+            mName);
+#endif
+
     // TODO: get this from input format
     bool secure = mComponent->getName().find(".secure") != std::string::npos;
 
@@ -2140,34 +2150,6 @@
         }
     }
 
-    // Set up pipeline control. This has to be done after mInputBuffers and
-    // mOutputBuffers are initialized to make sure that lingering callbacks
-    // about buffers from the previous generation do not interfere with the
-    // newly initialized pipeline capacity.
-
-    // Query delays
-    C2PortRequestedDelayTuning::input inputDelay;
-    C2PortRequestedDelayTuning::output outputDelay;
-    C2RequestedPipelineDelayTuning pipelineDelay;
-#if 0
-    err = mComponent->query(
-            { &inputDelay, &pipelineDelay, &outputDelay },
-            {},
-            C2_DONT_BLOCK,
-            nullptr);
-    mAvailablePipelineCapacity.initialize(
-            inputDelay,
-            inputDelay + pipelineDelay,
-            inputDelay + pipelineDelay + outputDelay,
-            mName);
-#else
-    mAvailablePipelineCapacity.initialize(
-            kMinInputBufferArraySize,
-            kMaxPipelineCapacity,
-            kMinOutputBufferArraySize,
-            mName);
-#endif
-
     mInputMetEos = false;
     mSync.start();
     return OK;
@@ -2250,7 +2232,7 @@
 
 void CCodecBufferChannel::stop() {
     mSync.stop();
-    mFirstValidFrameIndex = mFrameIndex.load(std::memory_order_relaxed);
+    mFirstValidFrameIndex = mFrameIndex.load();
     if (mInputSurface != nullptr) {
         mInputSurface->disconnect();
         mInputSurface.reset();
@@ -2294,11 +2276,13 @@
         std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
         const C2StreamInitDataInfo::output *initData,
         size_t numDiscardedInputBuffers) {
+    mAvailablePipelineCapacity.freeInputSlots(numDiscardedInputBuffers,
+                                              "onWorkDone");
+    mAvailablePipelineCapacity.freeComponentSlot("onWorkDone");
     if (handleWork(std::move(work), outputFormat, initData)) {
-        mAvailablePipelineCapacity.freeInputSlots(numDiscardedInputBuffers,
-                                                  "onWorkDone");
-        feedInputBufferIfAvailable();
+        mAvailablePipelineCapacity.freeOutputSlot("onWorkDone");
     }
+    feedInputBufferIfAvailable();
 }
 
 void CCodecBufferChannel::onInputBufferDone(
@@ -2307,11 +2291,9 @@
     {
         Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
         newInputSlotAvailable = (*buffers)->expireComponentBuffer(buffer);
-        if (newInputSlotAvailable) {
-            mAvailablePipelineCapacity.freeInputSlots(1, "onInputBufferDone");
-        }
     }
     if (newInputSlotAvailable) {
+        mAvailablePipelineCapacity.freeInputSlots(1, "onInputBufferDone");
         feedInputBufferIfAvailable();
     }
 }
@@ -2320,21 +2302,12 @@
         std::unique_ptr<C2Work> work,
         const sp<AMessage> &outputFormat,
         const C2StreamInitDataInfo::output *initData) {
-    if ((work->input.ordinal.frameIndex - mFirstValidFrameIndex.load()).peek() < 0) {
-        // Discard frames from previous generation.
-        ALOGD("[%s] Discard frames from previous generation.", mName);
-        return false;
-    }
-
-    mAvailablePipelineCapacity.freeComponentSlot("handleWork");
-
-    if (work->result == C2_NOT_FOUND) {
-        ALOGD("[%s] flushed work; ignored.", mName);
-        mAvailablePipelineCapacity.freeOutputSlot("handleWork (flushed)");
-        return true;
-    }
-
     if (work->result != C2_OK) {
+        if (work->result == C2_NOT_FOUND) {
+            // TODO: Define what flushed work's result is.
+            ALOGD("[%s] flushed work; ignored.", mName);
+            return true;
+        }
         ALOGD("[%s] work failed to complete: %d", mName, work->result);
         mCCodecCallback->onError(work->result, ACTION_CODE_FATAL);
         return false;
@@ -2349,7 +2322,11 @@
     }
 
     const std::unique_ptr<C2Worklet> &worklet = work->worklets.front();
-
+    if ((worklet->output.ordinal.frameIndex - mFirstValidFrameIndex.load()).peek() < 0) {
+        // Discard frames from previous generation.
+        ALOGD("[%s] Discard frames from previous generation.", mName);
+        return true;
+    }
     std::shared_ptr<C2Buffer> buffer;
     // NOTE: MediaCodec usage supposedly have only one output stream.
     if (worklet->output.buffers.size() > 1u) {
@@ -2388,7 +2365,7 @@
         ALOGV("[%s] onWorkDone: output EOS", mName);
     }
 
-    bool csdReported = false;
+    bool feedNeeded = true;
     sp<MediaCodecBuffer> outBuffer;
     size_t index;
 
@@ -2418,7 +2395,7 @@
             buffers.unlock();
             mCallback->onOutputBufferAvailable(index, outBuffer);
             buffers.lock();
-            csdReported = true;
+            feedNeeded = false;
         } else {
             ALOGD("[%s] onWorkDone: unable to register csd", mName);
             buffers.unlock();
@@ -2431,12 +2408,7 @@
     if (!buffer && !flags) {
         ALOGV("[%s] onWorkDone: Not reporting output buffer (%lld)",
               mName, work->input.ordinal.frameIndex.peekull());
-        if (!csdReported) {
-            // onOutputBufferAvailable() has not been and will not be called for
-            // this work item.
-            mAvailablePipelineCapacity.freeOutputSlot("handleWork (no csd)");
-        }
-        return true;
+        return feedNeeded;
     }
 
     if (buffer) {
@@ -2470,11 +2442,8 @@
     outBuffer->meta()->setInt32("flags", flags);
     ALOGV("[%s] onWorkDone: out buffer index = %zu [%p] => %p + %zu",
             mName, index, outBuffer.get(), outBuffer->data(), outBuffer->size());
-    if (csdReported) {
-        mAvailablePipelineCapacity.forceAllocateOutputSlot("handleWork");
-    }
     mCallback->onOutputBufferAvailable(index, outBuffer);
-    return true;
+    return false;
 }
 
 status_t CCodecBufferChannel::setSurface(const sp<Surface> &newSurface) {
diff --git a/media/sfplugin/CCodecBufferChannel.h b/media/sfplugin/CCodecBufferChannel.h
index 3c23c81..8631235 100644
--- a/media/sfplugin/CCodecBufferChannel.h
+++ b/media/sfplugin/CCodecBufferChannel.h
@@ -305,8 +305,7 @@
         //
         // allocate() is called by CCodecBufferChannel to check whether it can
         // receive another input buffer. If the return value is true,
-        // onInputBufferAvailable() and onOutputBufferAvailable() can be called
-        // afterwards.
+        // onInputBufferAvailable() can (and will) be called afterwards.
         bool allocate(const char* callerTag = nullptr);
 
         // Increase #input, #component and #output by one.
@@ -345,15 +344,6 @@
         // renderOutputBuffer() is called.
         int freeOutputSlot(const char* callerTag = nullptr);
 
-        // Force-allocate an output slot.
-        //
-        // callerTag is used for logging only.
-        //
-        // forceAllocateOutputSlot() is called by CCodecBufferChannel to report
-        // a configuration change. #output may become negative when
-        // forceAllocateOutputSlot() returns.
-        int forceAllocateOutputSlot(const char* callerTag = nullptr);
-
     private:
         // Component name. Used for logging.
         const char* mName;