Skip to content

Commit

Permalink
feat: add setBackgroundColor
Browse files Browse the repository at this point in the history
  • Loading branch information
I-m-SuperMan authored and pingkai committed May 13, 2020
1 parent 29dc232 commit 3bdb7ef
Show file tree
Hide file tree
Showing 21 changed files with 169 additions and 39 deletions.
7 changes: 7 additions & 0 deletions framework/render/video/IVideoRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ class IVideoRender {
*/
virtual int clearScreen() = 0;

/*
* set background color
*/
virtual void setBackgroundColor(unsigned int color) {

};

/**
* set want draw frame.
* @param frame
Expand Down
13 changes: 12 additions & 1 deletion framework/render/video/glRender/CV420PProgramContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ void CV420PProgramContext::updateRotate(IVideoRender::Rotate rotate) {
}
}

void CV420PProgramContext::updateBackgroundColor(unsigned int color) {
if(color != mBackgroundColor) {
mBackgroundColor = color;

mColor[0] = ((color >> 16) & 0xff) / 255.0f;//r
mColor[1] = ((color >> 8) & 0xff) / 255.0f;//g
mColor[2] = ((color) & 0xff) / 255.0f;//b
mColor[3] = ((color >> 24) & 0xff) / 255.0f;//a
}
}

void CV420PProgramContext::updateWindowSize(int width, int height, bool windowChanged) {
if (mWindowWidth == width && mWindowHeight == height && !windowChanged) {
return;
Expand Down Expand Up @@ -198,7 +209,7 @@ int CV420PProgramContext::updateFrame(std::unique_ptr<IAFFrame> &frame) {

int64_t t2 = af_getsteady_ms();
glViewport(0, 0, mWindowWidth, mWindowHeight);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearColor(mColor[0], mColor[1], mColor[2], mColor[3]);
glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(mCVProgram);
Expand Down
5 changes: 5 additions & 0 deletions framework/render/video/glRender/CV420PProgramContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class CV420PProgramContext : public IProgramContext {

void updateRotate(IVideoRender::Rotate rotate) override;

void updateBackgroundColor(unsigned int color) override;

void updateWindowSize(int width, int height, bool windowChanged) override;

void updateFlip(IVideoRender::Flip flip) override;
Expand Down Expand Up @@ -98,6 +100,9 @@ class CV420PProgramContext : public IProgramContext {
GLfloat mUColorRange[3] = {0.0f};
int mColorRange = 0;

unsigned int mBackgroundColor = 0xff000000;
float mColor[4]={0.0f,0.0f,0.0f,1.0f};

};


Expand Down
16 changes: 14 additions & 2 deletions framework/render/video/glRender/GLRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ static const int MAX_IN_SIZE = 3;

using namespace Cicada;

#define CORRECT_COLOR(x) (((x) < 0.0f ? 0.0f : (x) > 255.0f ? 255.0f : (x)) / 255.0f)

GLRender::GLRender(float Hz)
{
mVSync = VSyncFactory::create(*this, Hz);
Expand Down Expand Up @@ -144,6 +146,11 @@ int GLRender::setScale(IVideoRender::Scale scale)
}


void GLRender::setBackgroundColor(unsigned int color)
{
mBackgroundColor = color;
};

int GLRender::onVSync(int64_t tick)
{
int ret = onVsyncInner(tick);
Expand Down Expand Up @@ -354,6 +361,7 @@ bool GLRender::renderActually()
mProgramContext->updateRotate(finalRotate);
mProgramContext->updateWindowSize(mWindowWidth, mWindowHeight, displayViewChanged);
mProgramContext->updateFlip(mFlip);
mProgramContext->updateBackgroundColor(mBackgroundColor);
int ret = mProgramContext->updateFrame(frame);
//work around for glReadPixels is upside-down.
{
Expand Down Expand Up @@ -396,7 +404,10 @@ bool GLRender::renderActually()

if (mClearScreenOn) {
glViewport(0, 0, mWindowWidth, mWindowHeight);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
/* {
std::unique_lock<mutex> lock(mClearColorMutex);
glClearColor(mClearColor[0], mClearColor[1], mClearColor[2], mClearColor[3]);
}*/
glClear(GL_COLOR_BUFFER_BIT);
mContext->Present(mGLSurface);

Expand Down Expand Up @@ -599,4 +610,5 @@ void GLRender::surfaceChanged()
std::unique_lock<mutex> lock(mRenderCallbackMutex);
mRenderCallbackCon.wait(lock);
#endif
};
}

15 changes: 9 additions & 6 deletions framework/render/video/glRender/GLRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#endif

using namespace Cicada;
using namespace Cicada;


#if TARGET_OS_IPHONE
Expand All @@ -45,6 +45,8 @@ class GLRender : public IVideoRender, private IVSync::Listener {

int clearScreen() override;

void setBackgroundColor(unsigned int color) override;

int renderFrame(std::unique_ptr<IAFFrame> &frame) override;

void setRenderResultCallback(std::function<void(int64_t, bool)> renderResultCallback) override;
Expand Down Expand Up @@ -93,16 +95,17 @@ class GLRender : public IVideoRender, private IVSync::Listener {

void calculateFPS(int64_t tick);

IProgramContext *getProgram(int frameFormat , IAFFrame *frame = nullptr);
IProgramContext *getProgram(int frameFormat, IAFFrame *frame = nullptr);

int onVsyncInner(int64_t tick);

protected:

Rotate mVideoRotate = Rotate_None;
Rotate mRotate = Rotate_None;
Flip mFlip = Flip_None;
Scale mScale = Scale_AspectFit;
std::atomic<Rotate> mVideoRotate{Rotate_None};
std::atomic<Rotate> mRotate{Rotate_None};
std::atomic<Flip> mFlip{Flip_None};
std::atomic<Scale> mScale{Scale_AspectFit};
std::atomic_uint mBackgroundColor{0xff000000};

int mWindowWidth = 0;
int mWindowHeight = 0;
Expand Down
2 changes: 2 additions & 0 deletions framework/render/video/glRender/IProgramContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class IProgramContext {

virtual void updateRotate(IVideoRender::Rotate rotate) = 0;

virtual void updateBackgroundColor(unsigned int color) = 0;

virtual void updateWindowSize(int width, int height, bool windowChanged) = 0;

virtual int updateFrame(std::unique_ptr<IAFFrame> &frame) = 0;
Expand Down
69 changes: 40 additions & 29 deletions framework/render/video/glRender/OESProgramContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ int OESProgramContext::initProgram() {
AF_LOGD("createProgram ");
mOESProgram = glCreateProgram();

GLuint mVertShader = 0;
GLuint mVertShader = 0;
GLuint mFragmentShader = 0;
int mInitRet = compileShader(&mVertShader, OES_VERTEX_SHADER, GL_VERTEX_SHADER);
int mInitRet = compileShader(&mVertShader, OES_VERTEX_SHADER, GL_VERTEX_SHADER);

if (mInitRet != 0) {
AF_LOGE("compileShader mVertShader failed. ret = %d ", mInitRet);
Expand All @@ -80,7 +80,7 @@ int OESProgramContext::initProgram() {
glDeleteShader(mFragmentShader);

if (status != GL_TRUE) {
int length = 0;
int length = 0;
GLchar glchar[256] = {0};
glGetProgramInfoLog(mOESProgram, 256, &length, glchar);
AF_LOGW("linkProgram error is %s \n", glchar);
Expand Down Expand Up @@ -111,7 +111,7 @@ void OESProgramContext::updateWindowSize(int width, int height, bool windowChang
return;
}

mWindowWidth = width;
mWindowWidth = width;
mWindowHeight = height;
mRegionChanged = true;
}
Expand Down Expand Up @@ -139,28 +139,28 @@ void OESProgramContext::updateDrawRegion() {
mDrawRegion[7] = (GLfloat) 1.0f;
mDrawRegion[8] = (GLfloat) 0.0f;

mDrawRegion[9] = (GLfloat) -1.0f;
mDrawRegion[9] = (GLfloat) -1.0f;
mDrawRegion[10] = (GLfloat) 1.0f;
mDrawRegion[11] = (GLfloat) 0.0f;

return;
}

int windowWidth = mWindowWidth;
int windowHeight = mWindowHeight;
int off_x = 0;
int off_y = 0;
int w = mWindowWidth;
int h = mWindowHeight;
int realWidth = 0;
int realHeight = 0;
int windowWidth = mWindowWidth;
int windowHeight = mWindowHeight;
int off_x = 0;
int off_y = 0;
int w = mWindowWidth;
int h = mWindowHeight;
int realWidth = 0;
int realHeight = 0;

if (mRotate == IVideoRender::Rotate::Rotate_90 ||
mRotate == IVideoRender::Rotate::Rotate_270) {
realWidth = mFrameHeight;
realWidth = mFrameHeight;
realHeight = static_cast<int>(mFrameHeight * mDar);
} else {
realWidth = static_cast<int>(mFrameHeight * mDar);
realWidth = static_cast<int>(mFrameHeight * mDar);
realHeight = mFrameHeight;
}

Expand All @@ -169,18 +169,18 @@ void OESProgramContext::updateDrawRegion() {

if (mScale == IVideoRender::Scale::Scale_AspectFit) {
if (scale_w >= scale_h) {
w = static_cast<int>(scale_h * realWidth);
w = static_cast<int>(scale_h * realWidth);
off_x = (windowWidth - w);
} else {
h = static_cast<int>(scale_w * realHeight);
h = static_cast<int>(scale_w * realHeight);
off_y = (windowHeight - h);
}
} else if (mScale == IVideoRender::Scale::Scale_AspectFill) {
if (scale_w < scale_h) {
w = static_cast<int>(scale_h * realWidth);
w = static_cast<int>(scale_h * realWidth);
off_x = (windowWidth - w);
} else {
h = static_cast<int>(scale_w * realHeight);
h = static_cast<int>(scale_w * realHeight);
off_y = (windowHeight - h);
}
}
Expand All @@ -201,7 +201,7 @@ void OESProgramContext::updateDrawRegion() {
mDrawRegion[7] = (GLfloat) 1.0f - offY;
mDrawRegion[8] = (GLfloat) 0.0f;

mDrawRegion[9] = (GLfloat) -1.0f + offX;
mDrawRegion[9] = (GLfloat) -1.0f + offX;
mDrawRegion[10] = (GLfloat) 1.0f - offY;
mDrawRegion[11] = (GLfloat) 0.0f;
} else if (mRotate == IVideoRender::Rotate::Rotate_90) {
Expand All @@ -218,7 +218,7 @@ void OESProgramContext::updateDrawRegion() {
mDrawRegion[7] = (GLfloat) -1.0f + offY;
mDrawRegion[8] = (GLfloat) 0.0f;

mDrawRegion[9] = (GLfloat) 1.0f - offX;
mDrawRegion[9] = (GLfloat) 1.0f - offX;
mDrawRegion[10] = (GLfloat) 1.0f - offY;
mDrawRegion[11] = (GLfloat) 0.0f;

Expand All @@ -235,7 +235,7 @@ void OESProgramContext::updateDrawRegion() {
mDrawRegion[7] = (GLfloat) -1.0f + offY;
mDrawRegion[8] = (GLfloat) 0.0f;

mDrawRegion[9] = (GLfloat) 1.0f - offX;
mDrawRegion[9] = (GLfloat) 1.0f - offX;
mDrawRegion[10] = (GLfloat) -1.0f + offY;
mDrawRegion[11] = (GLfloat) 0.0f;
} else if (mRotate == IVideoRender::Rotate::Rotate_270) {
Expand All @@ -251,7 +251,7 @@ void OESProgramContext::updateDrawRegion() {
mDrawRegion[7] = (GLfloat) 1.0f - offY;
mDrawRegion[8] = (GLfloat) 0.0f;

mDrawRegion[9] = (GLfloat) -1.0f + offX;
mDrawRegion[9] = (GLfloat) -1.0f + offX;
mDrawRegion[10] = (GLfloat) -1.0f + offY;
mDrawRegion[11] = (GLfloat) 0.0f;
}
Expand All @@ -276,7 +276,7 @@ void OESProgramContext::updateFlipCoords() {
mOESFlipCoords[5] = 0.0f;
mOESFlipCoords[6] = 0.0f;
mOESFlipCoords[7] = 0.0f;
} else if(mFlip == IVideoRender::Flip::Flip_Both){
} else if (mFlip == IVideoRender::Flip::Flip_Both) {
mOESFlipCoords[0] = 0.0f;
mOESFlipCoords[1] = 1.0f;
mOESFlipCoords[2] = 1.0f;
Expand Down Expand Up @@ -325,7 +325,7 @@ int OESProgramContext::updateFrame(std::unique_ptr<IAFFrame> &frame) {
}
}

if(frame == nullptr && !mRegionChanged && !mCoordsChanged){
if (frame == nullptr && !mRegionChanged && !mCoordsChanged) {
//frame is null and nothing changed , don`t need redraw. such as paused.
// AF_LOGW("0918, nothing changed");
return -1;
Expand Down Expand Up @@ -363,9 +363,9 @@ int OESProgramContext::updateFrame(std::unique_ptr<IAFFrame> &frame) {
glUseProgram(mOESProgram);

auto positionIndex = static_cast<GLuint>(glGetAttribLocation(mOESProgram,
"aPosition"));
"aPosition"));
auto texCoordIndex = static_cast<GLuint>(glGetAttribLocation(mOESProgram,
"aTextureCoord"));
"aTextureCoord"));

glEnableVertexAttribArray(positionIndex);
glEnableVertexAttribArray(texCoordIndex);
Expand All @@ -374,7 +374,7 @@ int OESProgramContext::updateFrame(std::unique_ptr<IAFFrame> &frame) {
glVertexAttribPointer(texCoordIndex, 2, GL_FLOAT, GL_FALSE, 8, mOESFlipCoords);

GLint MVPMatrixLocation = glGetUniformLocation(mOESProgram, "uMVPMatrix");
GLint STMatrixLocation = glGetUniformLocation(mOESProgram, "uSTMatrix");
GLint STMatrixLocation = glGetUniformLocation(mOESProgram, "uSTMatrix");

mDecoderSurface->UpdateTexImg();
mDecoderSurface->GetTransformMatrix(mOESSTMatrix);
Expand All @@ -387,7 +387,7 @@ int OESProgramContext::updateFrame(std::unique_ptr<IAFFrame> &frame) {


glViewport(0, 0, mWindowWidth, mWindowHeight);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearColor(mColor[0], mColor[1], mColor[2], mColor[3]);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, mOutTextureId);
Expand Down Expand Up @@ -426,3 +426,14 @@ void OESProgramContext::createSurface() {
mFrameAvailable = false;
}
}

void OESProgramContext::updateBackgroundColor(unsigned int color) {
if(color != mBackgroundColor) {
mBackgroundColor = color;

mColor[0] = ((color >> 16) & 0xff) / 255.0f;//r
mColor[1] = ((color >> 8) & 0xff) / 255.0f;//g
mColor[2] = ((color) & 0xff) / 255.0f;//b
mColor[3] = ((color >> 24) & 0xff) / 255.0f;//a
}
}
5 changes: 5 additions & 0 deletions framework/render/video/glRender/OESProgramContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class OESProgramContext : public IProgramContext , private DecoderSurfaceCallbac

void updateFlip(IVideoRender::Flip flip) override ;

void updateBackgroundColor(unsigned int color) override;

int updateFrame(std::unique_ptr<IAFFrame> &frame) override;

private:
Expand Down Expand Up @@ -78,6 +80,9 @@ class OESProgramContext : public IProgramContext , private DecoderSurfaceCallbac
std::mutex mFrameAvailableMutex;
std::condition_variable mFrameAvailableCon;
bool mFrameAvailable = false;

float mColor[4]={0.0f,0.0f,0.0f,1.0f};
unsigned int mBackgroundColor = 0xff000000;
};


Expand Down
13 changes: 12 additions & 1 deletion framework/render/video/glRender/YUVProgramContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ int YUVProgramContext::updateFrame(std::unique_ptr<IAFFrame> &frame) {
}

glViewport(0, 0, mWindowWidth, mWindowHeight);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearColor(mColor[0], mColor[1], mColor[2], mColor[3]);
glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(mProgram);
Expand Down Expand Up @@ -575,3 +575,14 @@ void YUVProgramContext::updateColorSpace() {

}
}

void YUVProgramContext::updateBackgroundColor(unsigned int color) {
if(color != mBackgroundColor) {
mBackgroundColor = color;

mColor[0] = ((color >> 16) & 0xff) / 255.0f;//r
mColor[1] = ((color >> 8) & 0xff) / 255.0f;//g
mColor[2] = ((color) & 0xff) / 255.0f;//b
mColor[3] = ((color >> 24) & 0xff) / 255.0f;//a
}
}
Loading

0 comments on commit 3bdb7ef

Please sign in to comment.