michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim: set ts=8 sts=4 et sw=4 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "GLBlitTextureImageHelper.h" michael@0: #include "GLUploadHelpers.h" michael@0: #include "DecomposeIntoNoRepeatTriangles.h" michael@0: #include "GLContext.h" michael@0: #include "ScopedGLHelpers.h" michael@0: #include "nsRect.h" michael@0: #include "gfx2DGlue.h" michael@0: #include "gfxUtils.h" michael@0: michael@0: namespace mozilla { michael@0: namespace gl { michael@0: michael@0: GLBlitTextureImageHelper::GLBlitTextureImageHelper(GLContext* gl) michael@0: : mGL(gl) michael@0: , mBlitProgram(0) michael@0: , mBlitFramebuffer(0) michael@0: michael@0: { michael@0: } michael@0: michael@0: GLBlitTextureImageHelper::~GLBlitTextureImageHelper() michael@0: { michael@0: // Likely used by OGL Layers. michael@0: mGL->fDeleteProgram(mBlitProgram); michael@0: mGL->fDeleteFramebuffers(1, &mBlitFramebuffer); michael@0: } michael@0: michael@0: void michael@0: GLBlitTextureImageHelper::BlitTextureImage(TextureImage *aSrc, const nsIntRect& aSrcRect, michael@0: TextureImage *aDst, const nsIntRect& aDstRect) michael@0: { michael@0: NS_ASSERTION(!aSrc->InUpdate(), "Source texture is in update!"); michael@0: NS_ASSERTION(!aDst->InUpdate(), "Destination texture is in update!"); michael@0: michael@0: if (aSrcRect.IsEmpty() || aDstRect.IsEmpty()) michael@0: return; michael@0: michael@0: int savedFb = 0; michael@0: mGL->fGetIntegerv(LOCAL_GL_FRAMEBUFFER_BINDING, &savedFb); michael@0: michael@0: ScopedGLState scopedScissorTestState(mGL, LOCAL_GL_SCISSOR_TEST, false); michael@0: ScopedGLState scopedBlendState(mGL, LOCAL_GL_BLEND, false); michael@0: michael@0: // 2.0 means scale up by two michael@0: float blitScaleX = float(aDstRect.width) / float(aSrcRect.width); michael@0: float blitScaleY = float(aDstRect.height) / float(aSrcRect.height); michael@0: michael@0: // We start iterating over all destination tiles michael@0: aDst->BeginTileIteration(); michael@0: do { michael@0: // calculate portion of the tile that is going to be painted to michael@0: nsIntRect dstSubRect; michael@0: nsIntRect dstTextureRect = ThebesIntRect(aDst->GetTileRect()); michael@0: dstSubRect.IntersectRect(aDstRect, dstTextureRect); michael@0: michael@0: // this tile is not part of the destination rectangle aDstRect michael@0: if (dstSubRect.IsEmpty()) michael@0: continue; michael@0: michael@0: // (*) transform the rect of this tile into the rectangle defined by aSrcRect... michael@0: nsIntRect dstInSrcRect(dstSubRect); michael@0: dstInSrcRect.MoveBy(-aDstRect.TopLeft()); michael@0: // ...which might be of different size, hence scale accordingly michael@0: dstInSrcRect.ScaleRoundOut(1.0f / blitScaleX, 1.0f / blitScaleY); michael@0: dstInSrcRect.MoveBy(aSrcRect.TopLeft()); michael@0: michael@0: SetBlitFramebufferForDestTexture(aDst->GetTextureID()); michael@0: UseBlitProgram(); michael@0: michael@0: aSrc->BeginTileIteration(); michael@0: // now iterate over all tiles in the source Image... michael@0: do { michael@0: // calculate portion of the source tile that is in the source rect michael@0: nsIntRect srcSubRect; michael@0: nsIntRect srcTextureRect = ThebesIntRect(aSrc->GetTileRect()); michael@0: srcSubRect.IntersectRect(aSrcRect, srcTextureRect); michael@0: michael@0: // this tile is not part of the source rect michael@0: if (srcSubRect.IsEmpty()) { michael@0: continue; michael@0: } michael@0: // calculate intersection of source rect with destination rect michael@0: srcSubRect.IntersectRect(srcSubRect, dstInSrcRect); michael@0: // this tile does not overlap the current destination tile michael@0: if (srcSubRect.IsEmpty()) { michael@0: continue; michael@0: } michael@0: // We now have the intersection of michael@0: // the current source tile michael@0: // and the desired source rectangle michael@0: // and the destination tile michael@0: // and the desired destination rectange michael@0: // in destination space. michael@0: // We need to transform this back into destination space, inverting the transform from (*) michael@0: nsIntRect srcSubInDstRect(srcSubRect); michael@0: srcSubInDstRect.MoveBy(-aSrcRect.TopLeft()); michael@0: srcSubInDstRect.ScaleRoundOut(blitScaleX, blitScaleY); michael@0: srcSubInDstRect.MoveBy(aDstRect.TopLeft()); michael@0: michael@0: // we transform these rectangles to be relative to the current src and dst tiles, respectively michael@0: nsIntSize srcSize = srcTextureRect.Size(); michael@0: nsIntSize dstSize = dstTextureRect.Size(); michael@0: srcSubRect.MoveBy(-srcTextureRect.x, -srcTextureRect.y); michael@0: srcSubInDstRect.MoveBy(-dstTextureRect.x, -dstTextureRect.y); michael@0: michael@0: float dx0 = 2.0f * float(srcSubInDstRect.x) / float(dstSize.width) - 1.0f; michael@0: float dy0 = 2.0f * float(srcSubInDstRect.y) / float(dstSize.height) - 1.0f; michael@0: float dx1 = 2.0f * float(srcSubInDstRect.x + srcSubInDstRect.width) / float(dstSize.width) - 1.0f; michael@0: float dy1 = 2.0f * float(srcSubInDstRect.y + srcSubInDstRect.height) / float(dstSize.height) - 1.0f; michael@0: ScopedViewportRect autoViewportRect(mGL, 0, 0, dstSize.width, dstSize.height); michael@0: michael@0: RectTriangles rects; michael@0: michael@0: nsIntSize realTexSize = srcSize; michael@0: if (!CanUploadNonPowerOfTwo(mGL)) { michael@0: realTexSize = nsIntSize(gfx::NextPowerOfTwo(srcSize.width), michael@0: gfx::NextPowerOfTwo(srcSize.height)); michael@0: } michael@0: michael@0: if (aSrc->GetWrapMode() == LOCAL_GL_REPEAT) { michael@0: rects.addRect(/* dest rectangle */ michael@0: dx0, dy0, dx1, dy1, michael@0: /* tex coords */ michael@0: srcSubRect.x / float(realTexSize.width), michael@0: srcSubRect.y / float(realTexSize.height), michael@0: srcSubRect.XMost() / float(realTexSize.width), michael@0: srcSubRect.YMost() / float(realTexSize.height)); michael@0: } else { michael@0: DecomposeIntoNoRepeatTriangles(srcSubRect, realTexSize, rects); michael@0: michael@0: // now put the coords into the d[xy]0 .. d[xy]1 coordinate space michael@0: // from the 0..1 that it comes out of decompose michael@0: InfallibleTArray& coords = rects.vertCoords(); michael@0: michael@0: for (unsigned int i = 0; i < coords.Length(); ++i) { michael@0: coords[i].x = (coords[i].x * (dx1 - dx0)) + dx0; michael@0: coords[i].y = (coords[i].y * (dy1 - dy0)) + dy0; michael@0: } michael@0: } michael@0: michael@0: ScopedBindTextureUnit autoTexUnit(mGL, LOCAL_GL_TEXTURE0); michael@0: ScopedBindTexture autoTex(mGL, aSrc->GetTextureID()); michael@0: michael@0: mGL->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, 0); michael@0: michael@0: mGL->fVertexAttribPointer(0, 2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0, rects.vertCoords().Elements()); michael@0: mGL->fVertexAttribPointer(1, 2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0, rects.texCoords().Elements()); michael@0: michael@0: mGL->fEnableVertexAttribArray(0); michael@0: mGL->fEnableVertexAttribArray(1); michael@0: michael@0: mGL->fDrawArrays(LOCAL_GL_TRIANGLES, 0, rects.elements()); michael@0: michael@0: mGL->fDisableVertexAttribArray(0); michael@0: mGL->fDisableVertexAttribArray(1); michael@0: michael@0: } while (aSrc->NextTile()); michael@0: } while (aDst->NextTile()); michael@0: michael@0: mGL->fVertexAttribPointer(0, 2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0, nullptr); michael@0: mGL->fVertexAttribPointer(1, 2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0, nullptr); michael@0: michael@0: // unbind the previous texture from the framebuffer michael@0: SetBlitFramebufferForDestTexture(0); michael@0: michael@0: mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, savedFb); michael@0: } michael@0: michael@0: void michael@0: GLBlitTextureImageHelper::SetBlitFramebufferForDestTexture(GLuint aTexture) michael@0: { michael@0: if (!mBlitFramebuffer) { michael@0: mGL->fGenFramebuffers(1, &mBlitFramebuffer); michael@0: } michael@0: michael@0: mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, mBlitFramebuffer); michael@0: mGL->fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER, michael@0: LOCAL_GL_COLOR_ATTACHMENT0, michael@0: LOCAL_GL_TEXTURE_2D, michael@0: aTexture, michael@0: 0); michael@0: michael@0: GLenum result = mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER); michael@0: if (aTexture && (result != LOCAL_GL_FRAMEBUFFER_COMPLETE)) { michael@0: nsAutoCString msg; michael@0: msg.Append("Framebuffer not complete -- error 0x"); michael@0: msg.AppendInt(result, 16); michael@0: // Note: if you are hitting this, it is likely that michael@0: // your texture is not texture complete -- that is, you michael@0: // allocated a texture name, but didn't actually define its michael@0: // size via a call to TexImage2D. michael@0: NS_RUNTIMEABORT(msg.get()); michael@0: } michael@0: } michael@0: michael@0: void michael@0: GLBlitTextureImageHelper::UseBlitProgram() michael@0: { michael@0: if (mBlitProgram) { michael@0: mGL->fUseProgram(mBlitProgram); michael@0: return; michael@0: } michael@0: michael@0: mBlitProgram = mGL->fCreateProgram(); michael@0: michael@0: GLuint shaders[2]; michael@0: shaders[0] = mGL->fCreateShader(LOCAL_GL_VERTEX_SHADER); michael@0: shaders[1] = mGL->fCreateShader(LOCAL_GL_FRAGMENT_SHADER); michael@0: michael@0: const char *blitVSSrc = michael@0: "attribute vec2 aVertex;" michael@0: "attribute vec2 aTexCoord;" michael@0: "varying vec2 vTexCoord;" michael@0: "void main() {" michael@0: " vTexCoord = aTexCoord;" michael@0: " gl_Position = vec4(aVertex, 0.0, 1.0);" michael@0: "}"; michael@0: const char *blitFSSrc = "#ifdef GL_ES\nprecision mediump float;\n#endif\n" michael@0: "uniform sampler2D uSrcTexture;" michael@0: "varying vec2 vTexCoord;" michael@0: "void main() {" michael@0: " gl_FragColor = texture2D(uSrcTexture, vTexCoord);" michael@0: "}"; michael@0: michael@0: mGL->fShaderSource(shaders[0], 1, (const GLchar**) &blitVSSrc, nullptr); michael@0: mGL->fShaderSource(shaders[1], 1, (const GLchar**) &blitFSSrc, nullptr); michael@0: michael@0: for (int i = 0; i < 2; ++i) { michael@0: GLint success, len = 0; michael@0: michael@0: mGL->fCompileShader(shaders[i]); michael@0: mGL->fGetShaderiv(shaders[i], LOCAL_GL_COMPILE_STATUS, &success); michael@0: NS_ASSERTION(success, "Shader compilation failed!"); michael@0: michael@0: if (!success) { michael@0: nsAutoCString log; michael@0: mGL->fGetShaderiv(shaders[i], LOCAL_GL_INFO_LOG_LENGTH, (GLint*) &len); michael@0: log.SetCapacity(len); michael@0: mGL->fGetShaderInfoLog(shaders[i], len, (GLint*) &len, (char*) log.BeginWriting()); michael@0: log.SetLength(len); michael@0: michael@0: printf_stderr("Shader %d compilation failed:\n%s\n", i, log.get()); michael@0: return; michael@0: } michael@0: michael@0: mGL->fAttachShader(mBlitProgram, shaders[i]); michael@0: mGL->fDeleteShader(shaders[i]); michael@0: } michael@0: michael@0: mGL->fBindAttribLocation(mBlitProgram, 0, "aVertex"); michael@0: mGL->fBindAttribLocation(mBlitProgram, 1, "aTexCoord"); michael@0: michael@0: mGL->fLinkProgram(mBlitProgram); michael@0: michael@0: GLint success, len = 0; michael@0: mGL->fGetProgramiv(mBlitProgram, LOCAL_GL_LINK_STATUS, &success); michael@0: NS_ASSERTION(success, "Shader linking failed!"); michael@0: michael@0: if (!success) { michael@0: nsAutoCString log; michael@0: mGL->fGetProgramiv(mBlitProgram, LOCAL_GL_INFO_LOG_LENGTH, (GLint*) &len); michael@0: log.SetCapacity(len); michael@0: mGL->fGetProgramInfoLog(mBlitProgram, len, (GLint*) &len, (char*) log.BeginWriting()); michael@0: log.SetLength(len); michael@0: michael@0: printf_stderr("Program linking failed:\n%s\n", log.get()); michael@0: return; michael@0: } michael@0: michael@0: mGL->fUseProgram(mBlitProgram); michael@0: mGL->fUniform1i(mGL->fGetUniformLocation(mBlitProgram, "uSrcTexture"), 0); michael@0: } michael@0: michael@0: } michael@0: }