aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gl/SkGLDevice_FBO.cpp
blob: 552d61902e97296d0496b7ffc9609b4a49956237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "SkGLDevice_FBO.h"
#include "SkRegion.h"

SkGLDevice_FBO::SkGLDevice_FBO(const SkBitmap& bitmap, bool offscreen)
        : SkGLDevice(bitmap, offscreen) {
    fFBO = 0;
    fTextureID = 0;

    if (offscreen) {
        int nw = SkNextPow2(bitmap.rowBytesAsPixels());
        int nh = SkNextPow2(bitmap.height());
        
        glGenFramebuffersEXT(1, &fFBO);
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fFBO);

        glGenTextures(1, &fTextureID);
        glBindTexture(GL_TEXTURE_2D, fTextureID);
        SkGL::SetTexParamsClamp(false);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, nw, nh, 0,
                     GL_RGBA, GL_UNSIGNED_BYTE, NULL);

        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
                                  GL_TEXTURE_2D, fTextureID, 0);
        GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
        if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
            SkDebugf("-- glCheckFramebufferStatusEXT %x\n", status);
        }

        // now reset back to "normal" drawing target
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    }
}

SkGLDevice_FBO::~SkGLDevice_FBO() {
    if (fTextureID) {
        glDeleteTextures(1, &fTextureID);
    }
    if (fFBO) {
        glDeleteFramebuffersEXT(1, &fFBO);
    }
}

SkGLDevice::TexOrientation SkGLDevice_FBO::bindDeviceAsTexture() {
    if (fTextureID) {
        glBindTexture(GL_TEXTURE_2D, fTextureID);
        return kBottomToTop_TexOrientation;
    }
    return kNo_TexOrientation;
}

void SkGLDevice_FBO::gainFocus(SkCanvas* canvas) {
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fFBO);

    // now we're ready for the viewport and projection matrix
    this->INHERITED::gainFocus(canvas);
}