[Tizen] Fix SVACE issue (initialize in constructor)
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-frame-buffer.cpp
index b0cad27..61e043c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -26,18 +26,42 @@ namespace Internal
 {
 namespace Render
 {
+namespace
+{
+const GLenum COLOR_ATTACHMENTS[] =
+{
+    GL_COLOR_ATTACHMENT0,
+    GL_COLOR_ATTACHMENT1,
+    GL_COLOR_ATTACHMENT2,
+    GL_COLOR_ATTACHMENT3,
+    GL_COLOR_ATTACHMENT4,
+    GL_COLOR_ATTACHMENT5,
+    GL_COLOR_ATTACHMENT6,
+    GL_COLOR_ATTACHMENT7,
+};
+}
 
-FrameBuffer::FrameBuffer( unsigned int width, unsigned int height, unsigned int attachments )
-:mId( 0u ),
- mDepthBuffer( attachments & Dali::FrameBuffer::Attachment::DEPTH ),
- mStencilBuffer( attachments & Dali::FrameBuffer::Attachment::STENCIL ),
- mWidth( width ),
- mHeight( height )
+FrameBuffer::FrameBuffer( uint32_t width, uint32_t height, Mask attachments )
+: mId( 0u ),
+  mTextureId{ 0u },
+  mDepthBuffer( attachments & Dali::FrameBuffer::Attachment::DEPTH ),
+  mStencilBuffer( attachments & Dali::FrameBuffer::Attachment::STENCIL ),
+  mWidth( width ),
+  mHeight( height ),
+  mColorAttachmentCount( 0u ),
+  mRenderedBuffer( nullptr ),
+  mCaptureRenderedResult(false),
+  mCaptured(false)
 {
 }
 
 FrameBuffer::~FrameBuffer()
-{}
+{
+  if(mCaptured)
+  {
+    delete[] mRenderedBuffer;
+  }
+}
 
 void FrameBuffer::Destroy( Context& context )
 {
@@ -47,6 +71,11 @@ void FrameBuffer::Destroy( Context& context )
   }
 }
 
+void FrameBuffer::GlContextDestroyed()
+{
+  mId = 0u;
+}
+
 void FrameBuffer::Initialize(Context& context)
 {
   context.GenFramebuffers( 1, &mId );
@@ -73,18 +102,52 @@ void FrameBuffer::Initialize(Context& context)
   context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
 }
 
-void FrameBuffer::AttachColorTexture( Context& context, Render::NewTexture* texture, unsigned int mipmapLevel, unsigned int layer )
+void FrameBuffer::AttachColorTexture( Context& context, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer )
 {
   context.BindFramebuffer( GL_FRAMEBUFFER, mId );
 
+  const GLuint textureId = texture->GetId();
+  mTextureId[mColorAttachmentCount] = textureId;
+
   // Create a color attachment.
+  const GLenum iAttachment = COLOR_ATTACHMENTS[mColorAttachmentCount];
   if( texture->GetType() == TextureType::TEXTURE_2D )
   {
-    context.FramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->GetId(), mipmapLevel );
+    context.FramebufferTexture2D( GL_FRAMEBUFFER, iAttachment, texture->GetTarget(), textureId, mipmapLevel );
   }
   else
   {
-    context.FramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, texture->GetId(), mipmapLevel );
+    context.FramebufferTexture2D( GL_FRAMEBUFFER, iAttachment, GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, textureId, mipmapLevel );
+  }
+
+  ++mColorAttachmentCount;
+  context.DrawBuffers(mColorAttachmentCount, COLOR_ATTACHMENTS);
+  DALI_ASSERT_DEBUG(context.CheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
+
+  context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
+}
+
+void FrameBuffer::AttachDepthTexture( Context& context, Render::Texture* texture, uint32_t mipmapLevel )
+{
+  context.BindFramebuffer( GL_FRAMEBUFFER, mId );
+
+  // Create a depth attachment.
+  if( texture->GetType() == TextureType::TEXTURE_2D )
+  {
+    context.FramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture->GetId(), mipmapLevel );
+  }
+
+  context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
+}
+
+void FrameBuffer::AttachDepthStencilTexture( Context& context, Render::Texture* texture, uint32_t mipmapLevel )
+{
+  context.BindFramebuffer( GL_FRAMEBUFFER, mId );
+
+  // Create a depth/stencil attachment.
+  if( texture->GetType() == TextureType::TEXTURE_2D )
+  {
+    context.FramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, texture->GetId(), mipmapLevel );
   }
 
   context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
@@ -95,16 +158,53 @@ void FrameBuffer::Bind( Context& context )
   context.BindFramebuffer( GL_FRAMEBUFFER, mId );
 }
 
-unsigned int FrameBuffer::GetWidth() const
+uint32_t FrameBuffer::GetWidth() const
 {
   return mWidth;
 }
 
-unsigned int FrameBuffer::GetHeight() const
+uint32_t FrameBuffer::GetHeight() const
 {
   return mHeight;
 }
 
+void FrameBuffer::DrawRenderedBuffer(Context& context)
+{
+  if(!mCaptureRenderedResult)
+  {
+    return;
+  }
+
+  if(mCaptured)
+  {
+    delete[] mRenderedBuffer;
+  }
+
+  context.BindFramebuffer(GL_FRAMEBUFFER, mId);
+  mRenderedBuffer = new GLubyte[mWidth * mHeight * sizeof(GL_UNSIGNED_BYTE)];
+  context.ReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, mRenderedBuffer);
+  context.BindFramebuffer(GL_FRAMEBUFFER, 0);
+  mCaptureRenderedResult = false;
+  mCaptured = true;
+}
+
+GLubyte* FrameBuffer::GetRenderedBuffer()
+{
+  if(mCaptured)
+  {
+    return mRenderedBuffer;
+  }
+  else
+  {
+    return nullptr;
+  }
+}
+
+void FrameBuffer::CaptureRenderingResult()
+{
+  mCaptureRenderedResult = true;
+}
+
 
 } //Render