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