Remove RenderSurface from Core
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-frame-buffer.cpp
index 35fe545..44c00b0 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.
@@ -17,7 +17,7 @@
 // CLASS HEADER
 #include <dali/internal/render/renderers/render-frame-buffer.h>
 
-//INTERNAL INCLUDES
+// INTERNAL INCLUDES
 #include <dali/internal/render/renderers/render-texture.h>
 
 namespace Dali
@@ -26,18 +26,35 @@ 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, Format format )
-:mId( 0u ),
- mDepthBuffer( (format == Dali::FrameBuffer::COLOR_DEPTH || format == Dali::FrameBuffer::COLOR_DEPTH_STENCIL ) ? 1u : 0u ),
- mStencilBuffer( (format == Dali::FrameBuffer::COLOR_STENCIL || format == Dali::FrameBuffer::COLOR_DEPTH_STENCIL ) ? 1u : 0u ),
- 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 )
 {
 }
 
 FrameBuffer::~FrameBuffer()
-{}
+{
+}
 
 void FrameBuffer::Destroy( Context& context )
 {
@@ -47,6 +64,11 @@ void FrameBuffer::Destroy( Context& context )
   }
 }
 
+void FrameBuffer::GlContextDestroyed()
+{
+  mId = 0u;
+}
+
 void FrameBuffer::Initialize(Context& context)
 {
   context.GenFramebuffers( 1, &mId );
@@ -54,38 +76,47 @@ void FrameBuffer::Initialize(Context& context)
 
   if( mDepthBuffer )
   {
-    //Create a depth render target
-    context.GenRenderbuffers(1, &mDepthBuffer );
-    context.BindRenderbuffer(GL_RENDERBUFFER, mDepthBuffer);
-    context.RenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mWidth, mHeight);
-    context.FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthBuffer);
+    // Create a depth render target.
+    context.GenRenderbuffers( 1, &mDepthBuffer );
+    context.BindRenderbuffer( GL_RENDERBUFFER, mDepthBuffer );
+    context.RenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mWidth, mHeight );
+    context.FramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthBuffer );
   }
 
   if( mStencilBuffer )
   {
-    //Create a stencil render target
-    context.GenRenderbuffers(1, &mStencilBuffer );
-    context.BindRenderbuffer(GL_RENDERBUFFER, mStencilBuffer);
-    context.RenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, mWidth, mHeight);
-    context.FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mStencilBuffer );
+    // Create a stencil render target.
+    context.GenRenderbuffers( 1, &mStencilBuffer );
+    context.BindRenderbuffer( GL_RENDERBUFFER, mStencilBuffer );
+    context.RenderbufferStorage( GL_RENDERBUFFER, GL_STENCIL_INDEX8, mWidth, mHeight );
+    context.FramebufferRenderbuffer( GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mStencilBuffer );
   }
 
   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 );
 }
 
@@ -94,12 +125,12 @@ 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;
 }