epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkGLDevice_FBO.h" |
| 9 | #include "SkRegion.h" |
| 10 | |
| 11 | SkGLDevice_FBO::SkGLDevice_FBO(const SkBitmap& bitmap, bool offscreen) |
| 12 | : SkGLDevice(bitmap, offscreen) { |
| 13 | fFBO = 0; |
| 14 | fTextureID = 0; |
| 15 | |
| 16 | if (offscreen) { |
| 17 | int nw = SkNextPow2(bitmap.rowBytesAsPixels()); |
| 18 | int nh = SkNextPow2(bitmap.height()); |
| 19 | |
| 20 | glGenFramebuffersEXT(1, &fFBO); |
| 21 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fFBO); |
| 22 | |
| 23 | glGenTextures(1, &fTextureID); |
| 24 | glBindTexture(GL_TEXTURE_2D, fTextureID); |
| 25 | SkGL::SetTexParamsClamp(false); |
| 26 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, nw, nh, 0, |
| 27 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 28 | |
| 29 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, |
| 30 | GL_TEXTURE_2D, fTextureID, 0); |
| 31 | GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); |
| 32 | if (status != GL_FRAMEBUFFER_COMPLETE_EXT) { |
| 33 | SkDebugf("-- glCheckFramebufferStatusEXT %x\n", status); |
| 34 | } |
| 35 | |
| 36 | // now reset back to "normal" drawing target |
| 37 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | SkGLDevice_FBO::~SkGLDevice_FBO() { |
| 42 | if (fTextureID) { |
| 43 | glDeleteTextures(1, &fTextureID); |
| 44 | } |
| 45 | if (fFBO) { |
| 46 | glDeleteFramebuffersEXT(1, &fFBO); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | SkGLDevice::TexOrientation SkGLDevice_FBO::bindDeviceAsTexture() { |
| 51 | if (fTextureID) { |
| 52 | glBindTexture(GL_TEXTURE_2D, fTextureID); |
| 53 | return kBottomToTop_TexOrientation; |
| 54 | } |
| 55 | return kNo_TexOrientation; |
| 56 | } |
| 57 | |
| 58 | void SkGLDevice_FBO::gainFocus(SkCanvas* canvas) { |
| 59 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fFBO); |
| 60 | |
| 61 | // now we're ready for the viewport and projection matrix |
| 62 | this->INHERITED::gainFocus(canvas); |
| 63 | } |
| 64 | |