2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FUiEffects_RendererGraphicsEngineRenderRenderTargetCache.cpp
20 * @brief Implementation of class for render targets cache
24 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineOpenGLImplementation.h>
25 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineRenderRenderTargetCache.h>
26 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineRender.h>
27 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineRenderTextureCache.h>
29 namespace Tizen { namespace Ui { namespace Effects { namespace _Renderer { namespace GraphicsEngine
32 StubAttachmentCache::StubAttachmentCache(Render *pRender, EngineModel::StubAttachment* pStubAttachment):
36 _pStubAttachment(pStubAttachment),
41 StubAttachmentCache::~StubAttachmentCache(void)
47 StubAttachmentCache::DropCache(void)
54 StubAttachmentCache::DestroyCache(void)
58 _pStubAttachment->SetCache(StubAttachmentCachePtr());
61 _pStubAttachment = null;
65 glDeleteRenderbuffers(1, &_id);
72 StubAttachmentCache::Die(void)
74 _pStubAttachment = null;
80 StubAttachmentCache::Validate(Render* pRender)
86 if (!_pStubAttachment)
93 glGenRenderbuffers(1, &_id);
96 glBindRenderbuffer(GL_RENDERBUFFER, _id);
97 glRenderbufferStorage(GL_RENDERBUFFER, RenderTextureCache::GetGlInternalFormat(_pStubAttachment->GetInternalFormat()), _width, _height);
104 StubAttachmentCache::AttachToFrameBuffer(Render* pRender, GLenum attachment, GLuint width, GLuint height)
106 if (width != _width || height != _height)
116 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, _id);
121 Texture2dAttachmentCache::Texture2dAttachmentCache(Render *pRender, EngineModel::Texture2dAttachmentProperty* pTexture2DAttachmentProperty):
122 RenderCache(pRender),
125 _pTexture2DAttachmentProperty(pTexture2DAttachmentProperty),
130 Texture2dAttachmentCache::~Texture2dAttachmentCache(void)
136 Texture2dAttachmentCache::DropCache(void)
143 Texture2dAttachmentCache::Die(void)
145 _pTexture2DAttachmentProperty = null;
151 Texture2dAttachmentCache::DestroyCache(void)
153 if (_pTexture2DAttachmentProperty)
155 _pTexture2DAttachmentProperty->SetCache(Texture2DAttachmentCachePtr());
158 _pTexture2DAttachmentProperty = null;
162 glDeleteTextures(1, &_id);
169 Texture2dAttachmentCache::Validate(Render* pRender)
171 Validate(pRender, 0);
176 Texture2dAttachmentCache::Validate(Render* pRender, GLuint stage)
182 if (!_pTexture2DAttachmentProperty)
188 glGenTextures(1, &_id);
191 glActiveTexture(GL_TEXTURE0 + stage);
192 glBindTexture(GL_TEXTURE_2D, _id);
194 GLenum internalFormat = RenderTextureCache::GetGlInternalFormat(_pTexture2DAttachmentProperty->GetInternalFormat());
195 GLenum dataFormat = RenderTextureCache::GetGlDataFormat(_pTexture2DAttachmentProperty->GetDataFormat());
196 GLenum dataType = RenderTextureCache::GetGlDataType(_pTexture2DAttachmentProperty->GetDataType());
198 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat,
199 static_cast<GLsizei>(_width),
200 static_cast<GLsizei>(_height),
201 0, dataFormat, dataType, null);
203 if (_pTexture2DAttachmentProperty->GetGenerateMipmaps())
205 glGenerateMipmap(GL_TEXTURE_2D);
208 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, RenderTextureCache::GetGlFilterType(_pTexture2DAttachmentProperty->GetMinFilter()));
209 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, RenderTextureCache::GetGlFilterType(_pTexture2DAttachmentProperty->GetMagFilter()));
211 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, RenderTextureCache::GetGlWrapType(_pTexture2DAttachmentProperty->GetWrapS()));
212 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, RenderTextureCache::GetGlWrapType(_pTexture2DAttachmentProperty->GetWrapT()));
214 glBindTexture(GL_TEXTURE_2D, 0);
221 Texture2dAttachmentCache::AttachToFrameBuffer(Render* pRender, GLenum attachment, GLuint width, GLuint height, GLuint mipmap)
223 if (width != _width || height != _height)
231 Validate(pRender, 0);
233 glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, _id, mipmap);
238 Texture2dAttachmentCache::Bind(Render* pRender, GLuint stage)
242 Validate(pRender, stage);
245 glActiveTexture(GL_TEXTURE0 + stage);
246 glBindTexture(GL_TEXTURE_2D, _id);
251 RenderTargetExternalRenderCache::RenderTargetExternalRenderCache(Render *pRender, EngineModel::RenderTargetExternalRender* pRenderTargetExternalRender)
252 : RenderCache(pRender)
253 , _pRenderTargetExternalRender(pRenderTargetExternalRender)
256 , _numColorBuffers(static_cast<GLuint>(0))
258 memset(_colorBuffers, 0, EngineModel::RenderTarget::Attachment::ATTACHMENT_MAX * sizeof(GLenum));
261 RenderTargetExternalRenderCache::~RenderTargetExternalRenderCache(void)
267 RenderTargetExternalRenderCache::DropCache(void)
274 RenderTargetExternalRenderCache::Die(void)
276 _pRenderTargetExternalRender = null;
282 RenderTargetExternalRenderCache::DestroyCache(void)
284 if (_pRenderTargetExternalRender)
286 _pRenderTargetExternalRender->SetCache(RenderTargetExternalRenderCachePtr());
289 _pRenderTargetExternalRender = null;
293 glDeleteFramebuffers(1, &_id);
301 #ifdef _EFFECTS_GRAPHICSENGINE_OPENGL_DESKTOP_
302 GLenum _attachments[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3,
303 GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5, GL_COLOR_ATTACHMENT6, GL_COLOR_ATTACHMENT7,
304 GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT};
307 #ifdef _EFFECTS_GRAPHICSENGINE_OPENGL_ES_
308 GLenum _attachments[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT0,
309 GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT0,
310 GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT};
315 RenderTargetExternalRenderCache::Validate(Render* pRender)
321 if (!_pRenderTargetExternalRender)
327 glGenFramebuffers(1, &_id);
329 _numColorBuffers = 0;
330 glBindFramebuffer(GL_FRAMEBUFFER, _id);
332 GLuint width = _pRenderTargetExternalRender->GetWidth();
333 GLuint height = _pRenderTargetExternalRender->GetHeight();
335 for (unsigned short i = EngineModel::RenderTarget::Attachment::COLOR_0;
336 EngineModel::RenderTarget::Attachment::STENCIL > i; ++i)
338 const EngineModel::RenderTarget::AttachInfo* pAttachment = _pRenderTargetExternalRender->GetAttachment(static_cast<Tizen::Ui::Effects::_Renderer::EngineModel::RenderTarget::Attachment::Value>(i));
339 if (null == pAttachment)
343 switch (pAttachment->textureType)
345 case EngineModel::RenderTarget::AttachInfo::TEXTURE_2D:
347 EngineModel::Texture2dAttachmentProperty* texture = pAttachment->textureInfo.texture2D.pTexture;
348 Texture2dAttachmentCache* textureTarget = static_cast<Texture2dAttachmentCache*>(System::GetImpl(texture->GetCache()));
351 pRender->BuildCache(*texture, 0);
352 textureTarget = static_cast<Texture2dAttachmentCache*>(System::GetImpl(texture->GetCache()));
354 textureTarget->AttachToFrameBuffer(pRender, _attachments[i], width, height, static_cast<GLuint>(pAttachment->textureInfo.texture2D.mipLevel));
357 case EngineModel::RenderTarget::AttachInfo::STUB:
359 EngineModel::StubAttachment* stub = pAttachment->textureInfo.stub.pStub;
360 StubAttachmentCache* stubTarget = static_cast<StubAttachmentCache*>(System::GetImpl(stub->GetCache()));
363 pRender->BuildCache(*stub);
364 stubTarget = static_cast<StubAttachmentCache*>(System::GetImpl(stub->GetCache()));
366 stubTarget->AttachToFrameBuffer(pRender, _attachments[i], width, height);
372 if (i < EngineModel::RenderTarget::Attachment::DEPTH)
374 _colorBuffers[_numColorBuffers] = _attachments[i];
379 glBindFramebuffer(GL_FRAMEBUFFER, 0);
386 RenderTargetExternalRenderCache::Draw(Render* pRender)
388 if (!_pRenderTargetExternalRender->GetExternalRender()->NeedRedraw())
396 glBindFramebuffer(GL_FRAMEBUFFER , _id);
397 #ifdef _EFFECTS_GRAPHICSENGINE_OPENGL_DESKTOP_
400 _pRenderTargetExternalRender->GetExternalRender()->Draw();
404 }}}}} // Tizen::Ui::Effects::_Renderer::GraphicsEngine