From f6a150ae00b12f59ed3f1fb8ae1350db0e87f6cf Mon Sep 17 00:00:00 2001
From: Paulo Sergio Travaglia <pstglia@gmail.com>
Date: Sat, 28 Mar 2015 16:10:20 -0300
Subject: [PATCH] NETFLIX HACK: Disables CHECK macro functions and forces
 using allocateOutputBuffersFromNativeWindow

- Some CHECK macro functions along the code produces crashes when the conditions they test are not
met. I've blocked them, as they didn't appear to interfere on my tests (Although they seem to be important and required)

- In order to alloc buffers using "OMX.google.android.index.useAndroidNativeBuffer2" extension,
mStoreMetaDataInOutputBuffers must not be set. As I wanted to use this function (easier to lock/map the buffer)
I forced calling it, even mStoreMetaDataInOutputBuffers is true
---
 media/libstagefright/ACodec.cpp                     |  7 ++++++-
 media/libstagefright/foundation/ABuffer.cpp         |  2 +-
 media/libstagefright/omx/OMXNodeInstance.cpp        | 14 +++++++++++---
 media/libstagefright/omx/SimpleSoftOMXComponent.cpp |  4 ++--
 4 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/media/libstagefright/ACodec.cpp b/media/libstagefright/ACodec.cpp
index 9276818..b0c42fa 100644
--- a/media/libstagefright/ACodec.cpp
+++ b/media/libstagefright/ACodec.cpp
@@ -475,8 +475,11 @@ status_t ACodec::allocateBuffersOnPort(OMX_U32 portIndex) {
     status_t err;
     if (mNativeWindow != NULL && portIndex == kPortIndexOutput) {
         if (mStoreMetaDataInOutputBuffers) {
-            err = allocateOutputMetaDataBuffers();
+		ALOGI("PST DEBUG - Inside status_t ACodec::allocateBuffersOnPort - calling allocateOutputMetaDataBuffers");
+            //err = allocateOutputMetaDataBuffers();
+            err = allocateOutputBuffersFromNativeWindow();
         } else {
+		ALOGI("PST DEBUG - Inside status_t ACodec::allocateBuffersOnPort - calling allocateOutputBuffersFromNativeWindow");
             err = allocateOutputBuffersFromNativeWindow();
         }
     } else {
@@ -689,6 +692,8 @@ status_t ACodec::allocateOutputBuffersFromNativeWindow() {
         }
 
         sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(buf, false));
+	OMX_PTR vAddr = NULL;
+	graphicBuffer->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, &vAddr);
         BufferInfo info;
         info.mStatus = BufferInfo::OWNED_BY_US;
         info.mData = new ABuffer(NULL /* data */, bufferSize /* capacity */);
diff --git a/media/libstagefright/foundation/ABuffer.cpp b/media/libstagefright/foundation/ABuffer.cpp
index 6173db4..ceb139d 100644
--- a/media/libstagefright/foundation/ABuffer.cpp
+++ b/media/libstagefright/foundation/ABuffer.cpp
@@ -55,7 +55,7 @@ ABuffer::~ABuffer() {
 
 void ABuffer::setRange(size_t offset, size_t size) {
     CHECK_LE(offset, mCapacity);
-    CHECK_LE(offset + size, mCapacity);
+    //CHECK_LE(offset + size, mCapacity);
 
     mRangeOffset = offset;
     mRangeLength = size;
diff --git a/media/libstagefright/omx/OMXNodeInstance.cpp b/media/libstagefright/omx/OMXNodeInstance.cpp
index 6c5c857..95ff932 100644
--- a/media/libstagefright/omx/OMXNodeInstance.cpp
+++ b/media/libstagefright/omx/OMXNodeInstance.cpp
@@ -519,13 +519,21 @@ status_t OMXNodeInstance::useGraphicBuffer2_l(
     OMX_U8* bufferHandle = const_cast<OMX_U8*>(
             reinterpret_cast<const OMX_U8*>(graphicBuffer->handle));
 
+   OMX_PTR vAddr = NULL;
+   graphicBuffer->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, &vAddr);
+   if(vAddr == NULL)
+   {
+     ALOGE("Failed to get native buffer virtual address.\n");
+     return OMX_ErrorUndefined;
+    }
+
     err = OMX_UseBuffer(
             mHandle,
             &header,
             portIndex,
             bufferMeta,
             def.nBufferSize,
-            bufferHandle);
+            (OMX_U8*)vAddr);
 
     if (err != OMX_ErrorNone) {
         ALOGE("OMX_UseBuffer failed with error %d (0x%08x)", err, err);
@@ -535,8 +543,8 @@ status_t OMXNodeInstance::useGraphicBuffer2_l(
         return UNKNOWN_ERROR;
     }
 
-    CHECK_EQ(header->pBuffer, bufferHandle);
-    CHECK_EQ(header->pAppPrivate, bufferMeta);
+    //CHECK_EQ(header->pBuffer, bufferHandle);
+    //CHECK_EQ(header->pAppPrivate, bufferMeta);
 
     *buffer = header;
 
diff --git a/media/libstagefright/omx/SimpleSoftOMXComponent.cpp b/media/libstagefright/omx/SimpleSoftOMXComponent.cpp
index 4999663..89872cb 100644
--- a/media/libstagefright/omx/SimpleSoftOMXComponent.cpp
+++ b/media/libstagefright/omx/SimpleSoftOMXComponent.cpp
@@ -215,9 +215,9 @@ OMX_ERRORTYPE SimpleSoftOMXComponent::useBuffer(
 
     PortInfo *port = &mPorts.editItemAt(portIndex);
 
-    CHECK(mState == OMX_StateLoaded || port->mDef.bEnabled == OMX_FALSE);
+    //CHECK(mState == OMX_StateLoaded || port->mDef.bEnabled == OMX_FALSE);
 
-    CHECK_LT(port->mBuffers.size(), port->mDef.nBufferCountActual);
+    //CHECK_LT(port->mBuffers.size(), port->mDef.nBufferCountActual);
 
     port->mBuffers.push();
 
-- 
1.7.12.1

