~ubuntu-branches/ubuntu/trusty/libavg/trusty-proposed

« back to all changes in this revision

Viewing changes to src/player/PBOTexture.cpp

  • Committer: Package Import Robot
  • Author(s): OXullo Intersecans
  • Date: 2011-12-06 22:44:56 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20111206224456-qc7250z3ya1vi8s9
Tags: 1.7.0-0ubuntu1
* New upstream release (LP: #899183)
* Remove patches 0002-libav-0.7.patch, 0003-fglrx-segfault-on-startup.patch
  now merged to upstream
* Remove unnecessary .la files
* Update debian/watch file
* Fix debian/copyright dep-5 compliancy
* Update standards to version 3.9.2
* Add man pages for avg_checktouch, avg_checkvsync, avg_showsvg
* Minor debian/rules enhancement
* Add librsvg2-dev, libgdk-pixbuf2.0-dev to Build-Depends
* Proper transition to dh_python2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
//  libavg - Media Playback Engine. 
3
 
//  Copyright (C) 2003-2008 Ulrich von Zadow
4
 
//
5
 
//  This library is free software; you can redistribute it and/or
6
 
//  modify it under the terms of the GNU Lesser General Public
7
 
//  License as published by the Free Software Foundation; either
8
 
//  version 2 of the License, or (at your option) any later version.
9
 
//
10
 
//  This library is distributed in the hope that it will be useful,
11
 
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
//  Lesser General Public License for more details.
14
 
//
15
 
//  You should have received a copy of the GNU Lesser General Public
16
 
//  License along with this library; if not, write to the Free Software
17
 
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
//
19
 
//  Current versions can be found at www.libavg.de
20
 
//
21
 
 
22
 
#include "PBOTexture.h"
23
 
#include "SDLDisplayEngine.h"
24
 
#include "../graphics/VertexArray.h"
25
 
#include "../base/Logger.h"
26
 
#include "../base/Exception.h"
27
 
#include "../base/ScopeTimer.h"
28
 
#include "../base/ObjectCounter.h"
29
 
#include "../base/MathHelper.h"
30
 
#include "../base/StringHelper.h"
31
 
 
32
 
#include <iostream>
33
 
#include <string>
34
 
 
35
 
namespace avg {
36
 
 
37
 
using namespace std;
38
 
    
39
 
PBOTexture::PBOTexture(IntPoint size, PixelFormat pf, const MaterialInfo& material,
40
 
        SDLDisplayEngine * pEngine, OGLMemoryMode memoryMode) 
41
 
    : m_pf(pf),
42
 
      m_Material(material),
43
 
      m_pEngine(pEngine),
44
 
      m_MemoryMode(memoryMode)
45
 
{
46
 
    ObjectCounter::get()->incRef(&typeid(*this));
47
 
    m_ActiveSize = size;
48
 
    if (pEngine->usePOTTextures()) {
49
 
        m_Size.x = nextpow2(m_ActiveSize.x);
50
 
        m_Size.y = nextpow2(m_ActiveSize.y);
51
 
    } else {
52
 
        m_Size = m_ActiveSize;
53
 
    }
54
 
    if (m_Size.x > pEngine->getMaxTexSize() || m_Size.y > pEngine->getMaxTexSize()) {
55
 
        throw Exception(AVG_ERR_VIDEO_GENERAL, "Texture too large (" +toString(m_Size)
56
 
                + "). Maximum supported by graphics card is "
57
 
                + toString(pEngine->getMaxTexSize()));
58
 
    }
59
 
    createBitmap();
60
 
    createTexture();
61
 
}
62
 
 
63
 
PBOTexture::~PBOTexture()
64
 
{
65
 
    ObjectCounter::get()->decRef(&typeid(*this));
66
 
}
67
 
 
68
 
BitmapPtr PBOTexture::lockBmp()
69
 
{
70
 
    if (m_MemoryMode == MM_PBO) {
71
 
        return m_pWritePBO->lock();
72
 
    } else {
73
 
        return m_pBmp;
74
 
    }
75
 
}
76
 
 
77
 
void PBOTexture::unlockBmp()
78
 
{
79
 
    if (m_MemoryMode == MM_PBO) {
80
 
        m_pWritePBO->unlock();
81
 
    }
82
 
}
83
 
 
84
 
BitmapPtr PBOTexture::readbackBmp()
85
 
{
86
 
    if (m_MemoryMode == MM_PBO) {
87
 
        if (!m_pReadPBO) {
88
 
            m_pReadPBO = PBOPtr(new PBO(m_Size, m_pf, GL_DYNAMIC_READ));
89
 
        }
90
 
        return m_pReadPBO->moveTextureToBmp(m_pTex);
91
 
    } else {
92
 
        return BitmapPtr(new Bitmap(*m_pBmp));
93
 
    }
94
 
}
95
 
 
96
 
static ProfilingZoneID TexSubImageProfilingZone("Texture download");
97
 
 
98
 
void PBOTexture::download() const
99
 
{
100
 
    ScopeTimer Timer(TexSubImageProfilingZone);
101
 
    if (m_MemoryMode == MM_PBO) {
102
 
        m_pWritePBO->movePBOToTexture(m_pTex);
103
 
        if (m_pTex->hasMipmaps()) {
104
 
            m_pTex->generateMipmaps();
105
 
        }
106
 
    } else {
107
 
        m_pTex->activate();
108
 
        glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
109
 
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
110
 
        OGLErrorCheck(AVG_ERR_VIDEO_GENERAL, "PBOTexture::download: GL_UNPACK_ALIGNMENT");
111
 
        unsigned char * pStartPos = m_pBmp->getPixels();
112
 
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_ActiveSize.x, m_ActiveSize.y,
113
 
                m_pTex->getGLFormat(m_pf), m_pTex->getGLType(m_pf), 
114
 
                pStartPos);
115
 
        OGLErrorCheck(AVG_ERR_VIDEO_GENERAL, "PBOTexture::download: glTexSubImage2D()");
116
 
    }
117
 
}
118
 
 
119
 
void PBOTexture::setTex(GLTexturePtr pTex)
120
 
{
121
 
    AVG_ASSERT(m_MemoryMode == MM_PBO);
122
 
    AVG_ASSERT(!m_pBmp);
123
 
 
124
 
    m_pTex = pTex;
125
 
}
126
 
 
127
 
void PBOTexture::activate(int textureUnit)
128
 
{
129
 
    m_pTex->activate(textureUnit);
130
 
}
131
 
 
132
 
void PBOTexture::setMaterial(const MaterialInfo& material)
133
 
{
134
 
    if (m_Material.getUseMipmaps() != material.getUseMipmaps()) {
135
 
        m_Material = material;
136
 
        createTexture();
137
 
    } else {
138
 
        m_Material = material;
139
 
    }
140
 
}
141
 
 
142
 
const IntPoint& PBOTexture::getTextureSize() const
143
 
{
144
 
    return m_Size;
145
 
}
146
 
 
147
 
void PBOTexture::createBitmap()
148
 
{
149
 
    switch (m_MemoryMode) {
150
 
        case MM_PBO:
151
 
            {
152
 
                m_pWritePBO = PBOPtr(new PBO(m_ActiveSize, m_pf, GL_DYNAMIC_DRAW));
153
 
                m_pBmp = BitmapPtr();
154
 
            }
155
 
            break;
156
 
        case MM_OGL:
157
 
            m_pBmp = BitmapPtr(new Bitmap(m_ActiveSize, m_pf));
158
 
            break;
159
 
        default:
160
 
            AVG_ASSERT(0);
161
 
    }
162
 
}
163
 
 
164
 
void PBOTexture::createTexture()
165
 
{
166
 
    m_pTex = GLTexturePtr(new GLTexture(m_Size, m_pf, m_Material.getUseMipmaps(),
167
 
            m_Material.getTexWrapSMode(), m_Material.getTexWrapTMode())); 
168
 
 
169
 
    if (m_pEngine->usePOTTextures()) {
170
 
        // Make sure the texture is transparent and black before loading stuff 
171
 
        // into it to avoid garbage at the borders.
172
 
        int TexMemNeeded = m_Size.x*m_Size.y*Bitmap::getBytesPerPixel(m_pf);
173
 
        char * pPixels = new char[TexMemNeeded];
174
 
        memset(pPixels, 0, TexMemNeeded);
175
 
        glTexImage2D(GL_TEXTURE_2D, 0, m_pTex->getGLInternalFormat(), m_Size.x, 
176
 
                m_Size.y, 0, m_pTex->getGLFormat(m_pf), m_pTex->getGLType(m_pf), pPixels);
177
 
        OGLErrorCheck(AVG_ERR_VIDEO_GENERAL, "PBOTexture::createTexture: glTexImage2D()");
178
 
        delete[] pPixels;
179
 
    }
180
 
}
181
 
 
182
 
}