1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "CompositingRenderTargetOGL.h"
8 #include "GLContext.h"
9 #include "GLReadTexImageHelper.h"
10 #include "ScopedGLHelpers.h"
11 #include "mozilla/gfx/2D.h"
12 
13 namespace mozilla {
14 namespace layers {
15 
16 using namespace mozilla::gfx;
17 using namespace mozilla::gl;
18 
~CompositingRenderTargetOGL()19 CompositingRenderTargetOGL::~CompositingRenderTargetOGL() {
20   if (mGLResourceOwnership == GLResourceOwnership::OWNED_BY_RENDER_TARGET &&
21       mGL && mGL->MakeCurrent()) {
22     mGL->fDeleteTextures(1, &mTextureHandle);
23     mGL->fDeleteFramebuffers(1, &mFBO);
24   }
25 }
26 
BindTexture(GLenum aTextureUnit,GLenum aTextureTarget)27 void CompositingRenderTargetOGL::BindTexture(GLenum aTextureUnit,
28                                              GLenum aTextureTarget) {
29   MOZ_ASSERT(!mNeedInitialization);
30   MOZ_ASSERT(mTextureHandle != 0);
31   mGL->fActiveTexture(aTextureUnit);
32   mGL->fBindTexture(aTextureTarget, mTextureHandle);
33 }
34 
BindRenderTarget()35 void CompositingRenderTargetOGL::BindRenderTarget() {
36   bool needsClear = false;
37 
38   if (mNeedInitialization) {
39     Initialize(mNeedInitialization->mFBOTextureTarget);
40     if (mNeedInitialization->mInitMode == INIT_MODE_CLEAR) {
41       needsClear = true;
42       mClearOnBind = false;
43     }
44     mNeedInitialization = Nothing();
45   } else {
46     GLuint fbo = GetFBO();
47     mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, fbo);
48     GLenum result = mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
49     if (result != LOCAL_GL_FRAMEBUFFER_COMPLETE) {
50       // The main framebuffer (0) of non-offscreen contexts
51       // might be backed by a EGLSurface that needs to be renewed.
52       if (mFBO == 0 && !mGL->IsOffscreen()) {
53         mGL->RenewSurface(mCompositor->GetWidget());
54         result = mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
55       }
56       if (result != LOCAL_GL_FRAMEBUFFER_COMPLETE) {
57         nsAutoCString msg;
58         msg.AppendPrintf(
59             "Framebuffer not complete -- CheckFramebufferStatus returned 0x%x, "
60             "GLContext=%p, IsOffscreen()=%d, mFBO=%d, "
61             "aRect.width=%d, aRect.height=%d",
62             result, mGL.get(), mGL->IsOffscreen(), mFBO, mSize.width,
63             mSize.height);
64         NS_WARNING(msg.get());
65       }
66     }
67 
68     needsClear = mClearOnBind;
69   }
70 
71   if (needsClear) {
72     ScopedGLState scopedScissorTestState(mGL, LOCAL_GL_SCISSOR_TEST, true);
73     ScopedScissorRect autoScissorRect(mGL, 0, 0, mSize.width, mSize.height);
74     mGL->fClearColor(0.0, 0.0, 0.0, 0.0);
75     mGL->fClearDepth(0.0);
76     mGL->fClear(LOCAL_GL_COLOR_BUFFER_BIT | LOCAL_GL_DEPTH_BUFFER_BIT);
77   }
78 }
79 
GetFBO() const80 GLuint CompositingRenderTargetOGL::GetFBO() const {
81   MOZ_ASSERT(!mNeedInitialization);
82   return mFBO == 0 ? mGL->GetDefaultFramebuffer() : mFBO;
83 }
84 
85 #ifdef MOZ_DUMP_PAINTING
Dump(Compositor * aCompositor)86 already_AddRefed<DataSourceSurface> CompositingRenderTargetOGL::Dump(
87     Compositor* aCompositor) {
88   MOZ_ASSERT(!mNeedInitialization);
89   CompositorOGL* compositorOGL = aCompositor->AsCompositorOGL();
90   return ReadBackSurface(mGL, mTextureHandle, true,
91                          compositorOGL->GetFBOFormat());
92 }
93 #endif
94 
Initialize(GLenum aFBOTextureTarget)95 void CompositingRenderTargetOGL::Initialize(GLenum aFBOTextureTarget) {
96   // TODO: call mGL->GetBackbufferFB(), use that
97   GLuint fbo = mFBO == 0 ? mGL->GetDefaultFramebuffer() : mFBO;
98   mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, fbo);
99   mGL->fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER, LOCAL_GL_COLOR_ATTACHMENT0,
100                              aFBOTextureTarget, mTextureHandle, 0);
101 
102   // Making this call to fCheckFramebufferStatus prevents a crash on
103   // PowerVR. See bug 695246.
104   GLenum result = mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
105   if (result != LOCAL_GL_FRAMEBUFFER_COMPLETE) {
106     nsAutoCString msg;
107     msg.AppendPrintf(
108         "Framebuffer not complete -- error 0x%x, aFBOTextureTarget 0x%x, mFBO "
109         "%d, mTextureHandle %d, aRect.width %d, aRect.height %d",
110         result, aFBOTextureTarget, mFBO, mTextureHandle, mSize.width,
111         mSize.height);
112     NS_ERROR(msg.get());
113   }
114 }
115 
116 }  // namespace layers
117 }  // namespace mozilla
118