OpenGLRenderer.cpp revision 325740fb444af8fc7fb0119b2e30ce322c2ae134
18ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz/* 28ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * Copyright (C) 2010 The Android Open Source Project 38ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * 48ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * Licensed under the Apache License, Version 2.0 (the "License"); 58ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * you may not use this file except in compliance with the License. 68ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * You may obtain a copy of the License at 78ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * 88ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * http://www.apache.org/licenses/LICENSE-2.0 98ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * 108ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * Unless required by applicable law or agreed to in writing, software 118ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * distributed under the License is distributed on an "AS IS" BASIS, 128ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * See the License for the specific language governing permissions and 148ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * limitations under the License. 158ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz */ 168ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 178ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#define LOG_TAG "OpenGLRenderer" 188ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 198ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#include <stdlib.h> 208ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#include <stdint.h> 218ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#include <sys/types.h> 228ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 238ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#include <SkCanvas.h> 248ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#include <SkTypeface.h> 258ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 268ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#include <utils/Log.h> 278ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#include <utils/StopWatch.h> 288ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 298ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#include <private/hwui/DrawGlInfo.h> 308ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 318ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#include <ui/Rect.h> 328ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 338ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#include "OpenGLRenderer.h" 348ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#include "DisplayListRenderer.h" 358ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#include "Vector.h" 368ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 378ece050d85fc244c72610244e440b0e00aa618faSebastien Hertznamespace android { 388ece050d85fc244c72610244e440b0e00aa618faSebastien Hertznamespace uirenderer { 398ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 408ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz/////////////////////////////////////////////////////////////////////////////// 418ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz// Defines 428ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz/////////////////////////////////////////////////////////////////////////////// 438ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 448ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#define RAD_TO_DEG (180.0f / 3.14159265f) 458ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#define MIN_ANGLE 0.001f 468ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 478ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz// TODO: This should be set in properties 488ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH) 498ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 508ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST) 518ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 528ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz/////////////////////////////////////////////////////////////////////////////// 538ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz// Globals 548ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz/////////////////////////////////////////////////////////////////////////////// 558ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 568ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz/** 578ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * Structure mapping Skia xfermodes to OpenGL blending factors. 588ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz */ 598ece050d85fc244c72610244e440b0e00aa618faSebastien Hertzstruct Blender { 608ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz SkXfermode::Mode mode; 618ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz GLenum src; 628ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz GLenum dst; 638ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz}; // struct Blender 648ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 658ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz// In this array, the index of each Blender equals the value of the first 668ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] 678ece050d85fc244c72610244e440b0e00aa618faSebastien Hertzstatic const Blender gBlends[] = { 688ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 698ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, 708ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, 71c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 728ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 738ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, 748ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 758ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 76c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 778ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 788ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 798ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 808ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE }, 818ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR }, 828ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR } 838ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz}; 848ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 858ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz// This array contains the swapped version of each SkXfermode. For instance 86c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz// this array's SrcOver blending mode is actually DstOver. You can refer to 87c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz// createLayer() for more information on the purpose of this array. 88c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertzstatic const Blender gBlendsSwap[] = { 89c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 90c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE }, 91c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO }, 92c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 93c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 94c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 95c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO }, 96c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 97c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 98c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 99c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 100e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 101d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE }, 102d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO }, 103d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE } 104e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier}; 105e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier 106e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier/////////////////////////////////////////////////////////////////////////////// 107e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier// Constructors/destructor 108d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz/////////////////////////////////////////////////////////////////////////////// 109c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz 110c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien HertzOpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) { 111c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz mShader = NULL; 112c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz mColorFilter = NULL; 113c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz mHasShadow = false; 114c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz mHasDrawFilter = false; 115c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz 116c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); 117c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz 118c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz mFirstSnapshot = new Snapshot; 119c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz} 120c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz 1218ece050d85fc244c72610244e440b0e00aa618faSebastien HertzOpenGLRenderer::~OpenGLRenderer() { 122c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // The context has already been destroyed at this point, do not call 123c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // GL APIs. All GL state should be kept in Caches.h 124c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz} 125c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz 126c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz/////////////////////////////////////////////////////////////////////////////// 127c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz// Debug 128c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz/////////////////////////////////////////////////////////////////////////////// 129c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz 130d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertzvoid OpenGLRenderer::startMark(const char* name) const { 131c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz mCaches.startMark(0, name); 132c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz} 133c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz 134c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertzvoid OpenGLRenderer::endMark() const { 135c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz mCaches.endMark(); 136c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz} 137c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz 138d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz/////////////////////////////////////////////////////////////////////////////// 139c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz// Setup 140c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz/////////////////////////////////////////////////////////////////////////////// 141c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz 142c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertzuint32_t OpenGLRenderer::getStencilSize() { 143c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz return STENCIL_BUFFER_SIZE; 144c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz} 145c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz 146c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertzvoid OpenGLRenderer::setViewport(int width, int height) { 147c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); 148c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz 149c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz mWidth = width; 150c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz mHeight = height; 1518ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 152c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz mFirstSnapshot->height = height; 153c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz mFirstSnapshot->viewport.set(0, 0, width, height); 1548ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 1558ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz glDisable(GL_DITHER); 1563b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz glEnable(GL_SCISSOR_TEST); 157d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 158d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz 159d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz glEnableVertexAttribArray(Program::kBindingPosition); 160d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz} 161d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz 1628ece050d85fc244c72610244e440b0e00aa618faSebastien Hertzvoid OpenGLRenderer::prepare(bool opaque) { 1638ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque); 1648ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz} 1658ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 1668ece050d85fc244c72610244e440b0e00aa618faSebastien Hertzvoid OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) { 1678ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mCaches.clearGarbage(); 1688ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 1693b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz mSnapshot = new Snapshot(mFirstSnapshot, 170d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); 1718ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mSnapshot->fbo = getTargetFbo(); 1728ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mSaveCount = 1; 1738ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 1748ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz glViewport(0, 0, mWidth, mHeight); 1753b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top); 1768ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 1778ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mSnapshot->setClip(left, top, right, bottom); 1788ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mDirtyClip = false; 1798ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 1808ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (!opaque) { 1818ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz glClear(GL_COLOR_BUFFER_BIT); 1828ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 1838ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz} 1848ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 1858ece050d85fc244c72610244e440b0e00aa618faSebastien Hertzvoid OpenGLRenderer::finish() { 1868ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#if DEBUG_OPENGL 1878ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz GLenum status = GL_NO_ERROR; 1888ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz while ((status = glGetError()) != GL_NO_ERROR) { 1898ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz ALOGD("GL error from OpenGLRenderer: 0x%x", status); 1908ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz switch (status) { 1918ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz case GL_OUT_OF_MEMORY: 1928ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz ALOGE(" OpenGLRenderer is out of memory!"); 1938ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz break; 1948ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 1958ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 1968ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#endif 1978ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#if DEBUG_MEMORY_USAGE 1988ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mCaches.dumpMemoryUsage(); 1998ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#else 2008ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (mCaches.getDebugLevel() & kDebugMemory) { 2018ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mCaches.dumpMemoryUsage(); 2028ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 2038ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#endif 204c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz} 205c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 2068ece050d85fc244c72610244e440b0e00aa618faSebastien Hertzvoid OpenGLRenderer::interrupt() { 2073b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz if (mCaches.currentProgram) { 2083b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz if (mCaches.currentProgram->isInUse()) { 209d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz mCaches.currentProgram->remove(); 2108ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mCaches.currentProgram = NULL; 2118ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 2128ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 2138ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mCaches.unbindMeshBuffer(); 2148ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mCaches.unbindIndicesBuffer(); 2158ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mCaches.resetVertexPointers(); 2168ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mCaches.disbaleTexCoordsVertexArray(); 2173b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz} 2188ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 2198ece050d85fc244c72610244e440b0e00aa618faSebastien Hertzvoid OpenGLRenderer::resume() { 2208ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot; 2218ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 2228ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight()); 2238ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 2248ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 2258ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz glEnable(GL_SCISSOR_TEST); 2268ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mCaches.resetScissor(); 2278ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz dirtyClip(); 2288ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 2298ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mCaches.activeTexture(0); 2308ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo); 2318ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 2328ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mCaches.blend = true; 2338ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz glEnable(GL_BLEND); 234c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode); 235c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz glBlendEquation(GL_FUNC_ADD); 2368ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz} 2378ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 2383b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertzbool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) { 239a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao interrupt(); 2408ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (mDirtyClip) { 2418ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz setScissorFromClip(); 242d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz } 243d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz 244d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz Rect clip(*mSnapshot->clipRect); 2458ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz clip.snapToPixelBoundaries(); 2468ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 2478ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#if RENDER_LAYERS_AS_REGIONS 2488ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz // Since we don't know what the functor will draw, let's dirty 2498ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz // tne entire clip region 2508ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (hasLayer()) { 2518ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz dirtyLayerUnchecked(clip, getRegion()); 2523b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz } 253d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz#endif 2548ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 2558ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz DrawGlInfo info; 2568ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz info.clipLeft = clip.left; 2578ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz info.clipTop = clip.top; 2588ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz info.clipRight = clip.right; 2593b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz info.clipBottom = clip.bottom; 2608ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz info.isLayer = hasLayer(); 2618ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz getSnapshot()->transform->copyTo(&info.transform[0]); 2628ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 2638ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz status_t result = (*functor)(0, &info); 2648ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 2658ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (result != 0) { 2668ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom); 2678ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz dirty.unionWith(localDirty); 2688ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 2698ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 2708ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz resume(); 2718ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz return result != 0; 2728ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz} 2738ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 2748ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz/////////////////////////////////////////////////////////////////////////////// 2758ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz// State management 2768ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz/////////////////////////////////////////////////////////////////////////////// 2778ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 2788ece050d85fc244c72610244e440b0e00aa618faSebastien Hertzint OpenGLRenderer::getSaveCount() const { 279a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao return mSaveCount; 280a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao} 281d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz 282a3faaf4bece7f42529c013fe87bd41de59798656Jeff Haoint OpenGLRenderer::save(int flags) { 283a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao return saveSnapshot(flags); 284a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao} 285a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao 286a3faaf4bece7f42529c013fe87bd41de59798656Jeff Haovoid OpenGLRenderer::restore() { 287a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao if (mSaveCount > 1) { 288a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao restoreSnapshot(); 289a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao } 290a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao} 291a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao 292a3faaf4bece7f42529c013fe87bd41de59798656Jeff Haovoid OpenGLRenderer::restoreToCount(int saveCount) { 293a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao if (saveCount < 1) saveCount = 1; 294a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao 2958ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz while (mSaveCount > saveCount) { 296a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao restoreSnapshot(); 2978ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 2988ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz} 2998ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 3008ece050d85fc244c72610244e440b0e00aa618faSebastien Hertzint OpenGLRenderer::saveSnapshot(int flags) { 3018ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mSnapshot = new Snapshot(mSnapshot, flags); 3028ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz return mSaveCount++; 303c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz} 304c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 3058ece050d85fc244c72610244e440b0e00aa618faSebastien Hertzbool OpenGLRenderer::restoreSnapshot() { 306c61124bdeaae94f977ffc36ac69535e792c226f2Sebastien Hertz bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; 3073b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; 308d4beb6bc2b42b176c6d04fdd91d6c758e542c7c2Sebastien Hertz bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; 3098ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 3108ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz sp<Snapshot> current = mSnapshot; 3118ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz sp<Snapshot> previous = mSnapshot->previous; 3128ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 3138ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (restoreOrtho) { 3148ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz Rect& r = previous->viewport; 3158ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz glViewport(r.left, r.top, r.right, r.bottom); 3163b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz mOrthoMatrix.load(current->orthoMatrix); 3178ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 3188ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 3198ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mSaveCount--; 3208ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mSnapshot = previous; 3218ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 3228ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (restoreClip) { 3238ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz dirtyClip(); 3248ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 3258ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 3268ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (restoreLayer) { 3278ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz composeLayer(current, previous); 3288ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 3298ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 3308ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz return restoreClip; 3318ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz} 3328ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 333c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz/////////////////////////////////////////////////////////////////////////////// 334c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz// Layers 3358ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz/////////////////////////////////////////////////////////////////////////////// 3368ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 337c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartierint OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, 3388ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz SkPaint* p, int flags) { 3398ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz const GLuint previousFbo = mSnapshot->fbo; 3408ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz const int count = saveSnapshot(flags); 341c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier 342c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier if (!mSnapshot->isIgnored()) { 3438ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz int alpha = 255; 344c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier SkXfermode::Mode mode; 3458ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 3468ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (p) { 3478ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz alpha = p->getAlpha(); 3488ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (!mCaches.extensions.hasFramebufferFetch()) { 3498ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 350c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz if (!isMode) { 351c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // Assume SRC_OVER 3528ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mode = SkXfermode::kSrcOver_Mode; 3538ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 3548ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } else { 3552e2deeb6df3e5a952c194276146706e63ab644a1Ian Rogers mode = getXfermode(p->getXfermode()); 3568ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 3578ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } else { 3588ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mode = SkXfermode::kSrcOver_Mode; 3598ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 3608ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 3618ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo); 3628ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 3638ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 3648ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz return count; 3658ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz} 3668ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 3678ece050d85fc244c72610244e440b0e00aa618faSebastien Hertzint OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, 368c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz int alpha, int flags) { 369c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz if (alpha >= 255 - ALPHA_THRESHOLD) { 3708ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz return saveLayer(left, top, right, bottom, NULL, flags); 3718ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } else { 3728ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz SkPaint paint; 3732e2deeb6df3e5a952c194276146706e63ab644a1Ian Rogers paint.setAlpha(alpha); 3748ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz return saveLayer(left, top, right, bottom, &paint, flags); 3758ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 3768ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz} 3778ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 3788ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz/** 3798ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * Layers are viewed by Skia are slightly different than layers in image editing 3808ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * programs (for instance.) When a layer is created, previously created layers 3818ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * and the frame buffer still receive every drawing command. For instance, if a 3828ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * layer is created and a shape intersecting the bounds of the layers and the 3838ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * framebuffer is draw, the shape will be drawn on both (unless the layer was 3848ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * created with the SkCanvas::kClipToLayer_SaveFlag flag.) 3858ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * 386c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz * A way to implement layers is to create an FBO for each layer, backed by an RGBA 387c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz * texture. Unfortunately, this is inefficient as it requires every primitive to 3888ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * be drawn n + 1 times, where n is the number of active layers. In practice this 3898ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * means, for every primitive: 3908ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * - Switch active frame buffer 3912e2deeb6df3e5a952c194276146706e63ab644a1Ian Rogers * - Change viewport, clip and projection matrix 3928ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * - Issue the drawing 3938ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * 3948ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * Switching rendering target n + 1 times per drawn primitive is extremely costly. 3958ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * To avoid this, layers are implemented in a different way here, at least in the 3968ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * general case. FBOs are used, as an optimization, when the "clip to layer" flag 3978ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * is set. When this flag is set we can redirect all drawing operations into a 3988ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * single FBO. 3998ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * 4008ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * This implementation relies on the frame buffer being at least RGBA 8888. When 4018ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * a layer is created, only a texture is created, not an FBO. The content of the 4028ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * frame buffer contained within the layer's bounds is copied into this texture 4038ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame 404c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz * buffer and drawing continues as normal. This technique therefore treats the 405c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz * frame buffer as a scratch buffer for the layers. 4068ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * 4078ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * To compose the layers back onto the frame buffer, each layer texture 4088ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * (containing the original frame buffer data) is drawn as a simple quad over 4092e2deeb6df3e5a952c194276146706e63ab644a1Ian Rogers * the frame buffer. The trick is that the quad is set as the composition 4108ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * destination in the blending equation, and the frame buffer becomes the source 4118ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * of the composition. 4128ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * 4138ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * Drawing layers with an alpha value requires an extra step before composition. 4148ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * An empty quad is drawn over the layer's region in the frame buffer. This quad 4158ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the 4168ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * quad is used to multiply the colors in the frame buffer. This is achieved by 4178ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * changing the GL blend functions for the GL_FUNC_ADD blend equation to 4188ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * GL_ZERO, GL_SRC_ALPHA. 4198ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * 4208ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * Because glCopyTexImage2D() can be slow, an alternative implementation might 4218ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * be use to draw a single clipped layer. The implementation described above 422c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz * is correct in every case. 4238ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * 4248ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * (1) The frame buffer is actually not cleared right away. To allow the GPU 4258ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * to potentially optimize series of calls to glCopyTexImage2D, the frame 426c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz * buffer is left untouched until the first drawing operation. Only when 4278ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz * something actually gets drawn are the layers regions cleared. 428c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz */ 429c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertzbool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, 4303b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz float right, float bottom, int alpha, SkXfermode::Mode mode, 4313b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz int flags, GLuint previousFbo) { 4328ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top); 4338ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); 4348ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 4353b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag; 4368ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 4378ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz // Window coordinates of the layer 4388ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz Rect bounds(left, top, right, bottom); 4398ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (!fboLayer) { 4408ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mSnapshot->transform->mapRect(bounds); 4418ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 4428ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz // Layers only make sense if they are in the framebuffer's bounds 4438ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (bounds.intersect(*snapshot->clipRect)) { 4448ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz // We cannot work with sub-pixels in this case 4458ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz bounds.snapToPixelBoundaries(); 4468ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 4478ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz // When the layer is not an FBO, we may use glCopyTexImage so we 4488ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz // need to make sure the layer does not extend outside the bounds 4498ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz // of the framebuffer 4508ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (!bounds.intersect(snapshot->previous->viewport)) { 4518ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz bounds.setEmpty(); 4528ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 453c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } else { 454c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz bounds.setEmpty(); 4553b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz } 4563b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz } 4578ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 4588ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize || 4598ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz bounds.getHeight() > mCaches.maxTextureSize) { 4603b588e09eac6fb2aff64595e2232e479703850fcSebastien Hertz snapshot->empty = fboLayer; 4618ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } else { 4628ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer); 4638ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 4648ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 4658ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz // Bail out if we won't draw in this snapshot 4668ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (snapshot->invisible || snapshot->empty) { 4678ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz return false; 4688ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 4698ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 4708ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mCaches.activeTexture(0); 4718ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight()); 4728ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (!layer) { 4738ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz return false; 4748ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 4758ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 4768ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz layer->setAlpha(alpha, mode); 4778ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz layer->layer.set(bounds); 4788ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()), 4798ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz bounds.getWidth() / float(layer->getWidth()), 0.0f); 4808ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz layer->setColorFilter(mColorFilter); 4818ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 4828ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz // Save the layer in the snapshot 4838ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz snapshot->flags |= Snapshot::kFlagIsLayer; 4848ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz snapshot->layer = layer; 4858ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 4868ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (fboLayer) { 4878ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz return createFboLayer(layer, bounds, snapshot, previousFbo); 488947ff080753c786a74f1cd7aeb09f717bb7074bdSebastien Hertz } else { 489947ff080753c786a74f1cd7aeb09f717bb7074bdSebastien Hertz // Copy the framebuffer into the layer 4908ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz layer->bindTexture(); 4918ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (!bounds.isEmpty()) { 4928ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (layer->isEmpty()) { 4938ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4948ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz bounds.left, snapshot->height - bounds.bottom, 495947ff080753c786a74f1cd7aeb09f717bb7074bdSebastien Hertz layer->getWidth(), layer->getHeight(), 0); 496947ff080753c786a74f1cd7aeb09f717bb7074bdSebastien Hertz layer->setEmpty(false); 4978ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } else { 4988ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, 4998ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight()); 5008ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 501947ff080753c786a74f1cd7aeb09f717bb7074bdSebastien Hertz 5028ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz // Enqueue the buffer coordinates to clear the corresponding region later 5038ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mLayers.push(new Rect(bounds)); 5048ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz } 505947ff080753c786a74f1cd7aeb09f717bb7074bdSebastien Hertz } 5068ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 5078ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz return true; 5088ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz} 5098ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 5108ece050d85fc244c72610244e440b0e00aa618faSebastien Hertzbool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot, 5118ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz GLuint previousFbo) { 5128ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz layer->setFbo(mCaches.fboCache.get()); 5138ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 5148ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#if RENDER_LAYERS_AS_REGIONS 5158ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz snapshot->region = &snapshot->layer->region; 5168ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz snapshot->flags |= Snapshot::kFlagFboTarget; 5178ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz#endif 5188ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 5198ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz Rect clip(bounds); 5208ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz snapshot->transform->mapRect(clip); 5218ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz clip.intersect(*snapshot->clipRect); 5228ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz clip.snapToPixelBoundaries(); 5238ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz clip.intersect(snapshot->previous->viewport); 5248ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 5258ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz mat4 inverse; 5268ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz inverse.loadInverse(*mSnapshot->transform); 5278ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 528a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao inverse.mapRect(clip); 529a3faaf4bece7f42529c013fe87bd41de59798656Jeff Hao clip.snapToPixelBoundaries(); 530e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier clip.intersect(bounds); 5318ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz clip.translate(-bounds.left, -bounds.top); 5328ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 533e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier snapshot->flags |= Snapshot::kFlagIsFboLayer; 534e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier snapshot->fbo = layer->getFbo(); 535e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); 536e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom); 5378ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); 5388ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz snapshot->height = bounds.getHeight(); 5398ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz snapshot->flags |= Snapshot::kFlagDirtyOrtho; 540e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier snapshot->orthoMatrix.load(mOrthoMatrix); 5418ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 5428ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz // Bind texture to FBO 5438ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo()); 544e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier layer->bindTexture(); 5458ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 546e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier // Initialize the texture if needed 5478ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (layer->isEmpty()) { 5488ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE); 5498ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz layer->setEmpty(false); 550e861ebd5d9490cc86200f3859f3d36fadad4588cMathieu Chartier } 5518ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 5528ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 5538ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz layer->getTexture(), 0); 5548ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz 5551eda2268e84d384256814cb6c2ba2440a848f9edSebastien Hertz#if DEBUG_LAYERS_AS_REGIONS 5561eda2268e84d384256814cb6c2ba2440a848f9edSebastien Hertz GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); 5571eda2268e84d384256814cb6c2ba2440a848f9edSebastien Hertz if (status != GL_FRAMEBUFFER_COMPLETE) { 5581eda2268e84d384256814cb6c2ba2440a848f9edSebastien Hertz ALOGE("Framebuffer incomplete (GL error code 0x%x)", status); 559c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 560c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); 561c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz layer->deleteTexture(); 562c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz mCaches.fboCache.put(layer->getFbo()); 563c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 564c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz delete layer; 565c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 566c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz return false; 567c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } 568c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz#endif 569c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 570c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering 571c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f, 572c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz clip.getWidth() + 2.0f, clip.getHeight() + 2.0f); 573c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz glClear(GL_COLOR_BUFFER_BIT); 574c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 575c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz dirtyClip(); 576c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 577c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // Change the ortho projection 578c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz glViewport(0, 0, bounds.getWidth(), bounds.getHeight()); 579c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f); 580c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 581c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz return true; 582c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz} 583c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 584c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz/** 585c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz * Read the documentation of createLayer() before doing anything in this method. 586c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz */ 587c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertzvoid OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { 588c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz if (!current->layer) { 589c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz ALOGE("Attempting to compose a layer that does not exist"); 590c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz return; 591c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } 592c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 593c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer; 594c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 595c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz if (fboLayer) { 596c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // Unbind current FBO and restore previous one 597c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 598c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } 599c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 600c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz Layer* layer = current->layer; 601c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz const Rect& rect = layer->layer; 602c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 603c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz if (!fboLayer && layer->getAlpha() < 255) { 604c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz drawColorRect(rect.left, rect.top, rect.right, rect.bottom, 605c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true); 606c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // Required below, composeLayerRect() will divide by 255 607c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz layer->setAlpha(255); 608c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } 609c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 610c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz mCaches.unbindMeshBuffer(); 611c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 612c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz mCaches.activeTexture(0); 613c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 614c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // When the layer is stored in an FBO, we can save a bit of fillrate by 615c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // drawing only the dirty region 616c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz if (fboLayer) { 617c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform); 618c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz if (layer->getColorFilter()) { 619c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz setupColorFilter(layer->getColorFilter()); 620c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } 621c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz composeLayerRegion(layer, rect); 622c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz if (layer->getColorFilter()) { 623c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz resetColorFilter(); 624c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } 625c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } else if (!rect.isEmpty()) { 626c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz dirtyLayer(rect.left, rect.top, rect.right, rect.bottom); 627c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz composeLayerRect(layer, rect, true); 628c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } 629c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 630c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz if (fboLayer) { 631c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // Note: No need to use glDiscardFramebufferEXT() since we never 632c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // create/compose layers that are not on screen with this 633c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // code path 634c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // See LayerRenderer::destroyLayer(Layer*) 635c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 636c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // Detach the texture from the FBO 637c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz glBindFramebuffer(GL_FRAMEBUFFER, current->fbo); 638c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); 639c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 640c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 641c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed 642c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz mCaches.fboCache.put(current->fbo); 643c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } 644c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 645c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz dirtyClip(); 646c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 647c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz // Failing to add the layer to the cache should happen only if the layer is too large 648c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz if (!mCaches.layerCache.put(layer)) { 649c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz LAYER_LOGD("Deleting layer"); 650c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz layer->deleteTexture(); 651c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz delete layer; 652c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } 653c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz} 654c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 655c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertzvoid OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) { 656c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz float alpha = layer->getAlpha() / 255.0f; 657c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 658c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz mat4& transform = layer->getTransform(); 659c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz if (!transform.isIdentity()) { 660c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz save(0); 661c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz mSnapshot->transform->multiply(transform); 662c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } 663c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz 664c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz setupDraw(); 665c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz if (layer->getRenderTarget() == GL_TEXTURE_2D) { 666c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz setupDrawWithTexture(); 667c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } else { 668c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz setupDrawWithExternalTexture(); 669c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz } 670c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz setupDrawTextureTransform(); 671c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz setupDrawColor(alpha, alpha, alpha, alpha); 672c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz setupDrawColorFilter(); 673c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode()); 674c67148594b1580c278ae71e3ce5c6fd59bfa6bd3Sebastien Hertz setupDrawProgram(); 6758ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz setupDrawPureColorUniforms(); 6768ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz setupDrawColorFilterUniforms(); 6778ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz if (layer->getRenderTarget() == GL_TEXTURE_2D) { 6788ece050d85fc244c72610244e440b0e00aa618faSebastien Hertz setupDrawTexture(layer->getTexture()); 679 } else { 680 setupDrawExternalTexture(layer->getTexture()); 681 } 682 if (mSnapshot->transform->isPureTranslate() && 683 layer->getWidth() == (uint32_t) rect.getWidth() && 684 layer->getHeight() == (uint32_t) rect.getHeight()) { 685 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f); 686 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f); 687 688 layer->setFilter(GL_NEAREST); 689 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true); 690 } else { 691 layer->setFilter(GL_LINEAR); 692 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom); 693 } 694 setupDrawTextureTransformUniforms(layer->getTexTransform()); 695 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]); 696 697 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 698 699 finishDrawTexture(); 700 701 if (!transform.isIdentity()) { 702 restore(); 703 } 704} 705 706void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) { 707 if (!layer->isTextureLayer()) { 708 const Rect& texCoords = layer->texCoords; 709 resetDrawTextureTexCoords(texCoords.left, texCoords.top, 710 texCoords.right, texCoords.bottom); 711 712 float x = rect.left; 713 float y = rect.top; 714 bool simpleTransform = mSnapshot->transform->isPureTranslate() && 715 layer->getWidth() == (uint32_t) rect.getWidth() && 716 layer->getHeight() == (uint32_t) rect.getHeight(); 717 718 if (simpleTransform) { 719 // When we're swapping, the layer is already in screen coordinates 720 if (!swap) { 721 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f); 722 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f); 723 } 724 725 layer->setFilter(GL_NEAREST, true); 726 } else { 727 layer->setFilter(GL_LINEAR, true); 728 } 729 730 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(), 731 layer->getTexture(), layer->getAlpha() / 255.0f, 732 layer->getMode(), layer->isBlend(), 733 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 734 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform); 735 736 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 737 } else { 738 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f); 739 drawTextureLayer(layer, rect); 740 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 741 } 742} 743 744void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) { 745#if RENDER_LAYERS_AS_REGIONS 746 if (layer->region.isRect()) { 747 layer->setRegionAsRect(); 748 749 composeLayerRect(layer, layer->regionRect); 750 751 layer->region.clear(); 752 return; 753 } 754 755 // TODO: See LayerRenderer.cpp::generateMesh() for important 756 // information about this implementation 757 if (CC_LIKELY(!layer->region.isEmpty())) { 758 size_t count; 759 const android::Rect* rects = layer->region.getArray(&count); 760 761 const float alpha = layer->getAlpha() / 255.0f; 762 const float texX = 1.0f / float(layer->getWidth()); 763 const float texY = 1.0f / float(layer->getHeight()); 764 const float height = rect.getHeight(); 765 766 TextureVertex* mesh = mCaches.getRegionMesh(); 767 GLsizei numQuads = 0; 768 769 setupDraw(); 770 setupDrawWithTexture(); 771 setupDrawColor(alpha, alpha, alpha, alpha); 772 setupDrawColorFilter(); 773 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false); 774 setupDrawProgram(); 775 setupDrawDirtyRegionsDisabled(); 776 setupDrawPureColorUniforms(); 777 setupDrawColorFilterUniforms(); 778 setupDrawTexture(layer->getTexture()); 779 if (mSnapshot->transform->isPureTranslate()) { 780 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f); 781 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f); 782 783 layer->setFilter(GL_NEAREST); 784 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true); 785 } else { 786 layer->setFilter(GL_LINEAR); 787 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom); 788 } 789 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]); 790 791 for (size_t i = 0; i < count; i++) { 792 const android::Rect* r = &rects[i]; 793 794 const float u1 = r->left * texX; 795 const float v1 = (height - r->top) * texY; 796 const float u2 = r->right * texX; 797 const float v2 = (height - r->bottom) * texY; 798 799 // TODO: Reject quads outside of the clip 800 TextureVertex::set(mesh++, r->left, r->top, u1, v1); 801 TextureVertex::set(mesh++, r->right, r->top, u2, v1); 802 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); 803 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); 804 805 numQuads++; 806 807 if (numQuads >= REGION_MESH_QUAD_COUNT) { 808 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); 809 numQuads = 0; 810 mesh = mCaches.getRegionMesh(); 811 } 812 } 813 814 if (numQuads > 0) { 815 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); 816 } 817 818 finishDrawTexture(); 819 820#if DEBUG_LAYERS_AS_REGIONS 821 drawRegionRects(layer->region); 822#endif 823 824 layer->region.clear(); 825 } 826#else 827 composeLayerRect(layer, rect); 828#endif 829} 830 831void OpenGLRenderer::drawRegionRects(const Region& region) { 832#if DEBUG_LAYERS_AS_REGIONS 833 size_t count; 834 const android::Rect* rects = region.getArray(&count); 835 836 uint32_t colors[] = { 837 0x7fff0000, 0x7f00ff00, 838 0x7f0000ff, 0x7fff00ff, 839 }; 840 841 int offset = 0; 842 int32_t top = rects[0].top; 843 844 for (size_t i = 0; i < count; i++) { 845 if (top != rects[i].top) { 846 offset ^= 0x2; 847 top = rects[i].top; 848 } 849 850 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom); 851 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)], 852 SkXfermode::kSrcOver_Mode); 853 } 854#endif 855} 856 857void OpenGLRenderer::dirtyLayer(const float left, const float top, 858 const float right, const float bottom, const mat4 transform) { 859#if RENDER_LAYERS_AS_REGIONS 860 if (hasLayer()) { 861 Rect bounds(left, top, right, bottom); 862 transform.mapRect(bounds); 863 dirtyLayerUnchecked(bounds, getRegion()); 864 } 865#endif 866} 867 868void OpenGLRenderer::dirtyLayer(const float left, const float top, 869 const float right, const float bottom) { 870#if RENDER_LAYERS_AS_REGIONS 871 if (hasLayer()) { 872 Rect bounds(left, top, right, bottom); 873 dirtyLayerUnchecked(bounds, getRegion()); 874 } 875#endif 876} 877 878void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) { 879#if RENDER_LAYERS_AS_REGIONS 880 if (bounds.intersect(*mSnapshot->clipRect)) { 881 bounds.snapToPixelBoundaries(); 882 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); 883 if (!dirty.isEmpty()) { 884 region->orSelf(dirty); 885 } 886 } 887#endif 888} 889 890void OpenGLRenderer::clearLayerRegions() { 891 const size_t count = mLayers.size(); 892 if (count == 0) return; 893 894 if (!mSnapshot->isIgnored()) { 895 // Doing several glScissor/glClear here can negatively impact 896 // GPUs with a tiler architecture, instead we draw quads with 897 // the Clear blending mode 898 899 // The list contains bounds that have already been clipped 900 // against their initial clip rect, and the current clip 901 // is likely different so we need to disable clipping here 902 glDisable(GL_SCISSOR_TEST); 903 904 Vertex mesh[count * 6]; 905 Vertex* vertex = mesh; 906 907 for (uint32_t i = 0; i < count; i++) { 908 Rect* bounds = mLayers.itemAt(i); 909 910 Vertex::set(vertex++, bounds->left, bounds->bottom); 911 Vertex::set(vertex++, bounds->left, bounds->top); 912 Vertex::set(vertex++, bounds->right, bounds->top); 913 Vertex::set(vertex++, bounds->left, bounds->bottom); 914 Vertex::set(vertex++, bounds->right, bounds->top); 915 Vertex::set(vertex++, bounds->right, bounds->bottom); 916 917 delete bounds; 918 } 919 920 setupDraw(false); 921 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f); 922 setupDrawBlending(true, SkXfermode::kClear_Mode); 923 setupDrawProgram(); 924 setupDrawPureColorUniforms(); 925 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true); 926 setupDrawVertices(&mesh[0].position[0]); 927 928 glDrawArrays(GL_TRIANGLES, 0, count * 6); 929 930 glEnable(GL_SCISSOR_TEST); 931 } else { 932 for (uint32_t i = 0; i < count; i++) { 933 delete mLayers.itemAt(i); 934 } 935 } 936 937 mLayers.clear(); 938} 939 940/////////////////////////////////////////////////////////////////////////////// 941// Transforms 942/////////////////////////////////////////////////////////////////////////////// 943 944void OpenGLRenderer::translate(float dx, float dy) { 945 mSnapshot->transform->translate(dx, dy, 0.0f); 946} 947 948void OpenGLRenderer::rotate(float degrees) { 949 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); 950} 951 952void OpenGLRenderer::scale(float sx, float sy) { 953 mSnapshot->transform->scale(sx, sy, 1.0f); 954} 955 956void OpenGLRenderer::skew(float sx, float sy) { 957 mSnapshot->transform->skew(sx, sy); 958} 959 960void OpenGLRenderer::setMatrix(SkMatrix* matrix) { 961 if (matrix) { 962 mSnapshot->transform->load(*matrix); 963 } else { 964 mSnapshot->transform->loadIdentity(); 965 } 966} 967 968void OpenGLRenderer::getMatrix(SkMatrix* matrix) { 969 mSnapshot->transform->copyTo(*matrix); 970} 971 972void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { 973 SkMatrix transform; 974 mSnapshot->transform->copyTo(transform); 975 transform.preConcat(*matrix); 976 mSnapshot->transform->load(transform); 977} 978 979/////////////////////////////////////////////////////////////////////////////// 980// Clipping 981/////////////////////////////////////////////////////////////////////////////// 982 983void OpenGLRenderer::setScissorFromClip() { 984 Rect clip(*mSnapshot->clipRect); 985 clip.snapToPixelBoundaries(); 986 987 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom, 988 clip.getWidth(), clip.getHeight()); 989 990 mDirtyClip = false; 991} 992 993const Rect& OpenGLRenderer::getClipBounds() { 994 return mSnapshot->getLocalClip(); 995} 996 997bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { 998 if (mSnapshot->isIgnored()) { 999 return true; 1000 } 1001 1002 Rect r(left, top, right, bottom); 1003 mSnapshot->transform->mapRect(r); 1004 r.snapToPixelBoundaries(); 1005 1006 Rect clipRect(*mSnapshot->clipRect); 1007 clipRect.snapToPixelBoundaries(); 1008 1009 return !clipRect.intersects(r); 1010} 1011 1012bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { 1013 bool clipped = mSnapshot->clip(left, top, right, bottom, op); 1014 if (clipped) { 1015 dirtyClip(); 1016 } 1017 return !mSnapshot->clipRect->isEmpty(); 1018} 1019 1020/////////////////////////////////////////////////////////////////////////////// 1021// Drawing commands 1022/////////////////////////////////////////////////////////////////////////////// 1023 1024void OpenGLRenderer::setupDraw(bool clear) { 1025 if (clear) clearLayerRegions(); 1026 if (mDirtyClip) { 1027 setScissorFromClip(); 1028 } 1029 mDescription.reset(); 1030 mSetShaderColor = false; 1031 mColorSet = false; 1032 mColorA = mColorR = mColorG = mColorB = 0.0f; 1033 mTextureUnit = 0; 1034 mTrackDirtyRegions = true; 1035} 1036 1037void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) { 1038 mDescription.hasTexture = true; 1039 mDescription.hasAlpha8Texture = isAlpha8; 1040} 1041 1042void OpenGLRenderer::setupDrawWithExternalTexture() { 1043 mDescription.hasExternalTexture = true; 1044} 1045 1046void OpenGLRenderer::setupDrawNoTexture() { 1047 mCaches.disbaleTexCoordsVertexArray(); 1048} 1049 1050void OpenGLRenderer::setupDrawAALine() { 1051 mDescription.isAA = true; 1052} 1053 1054void OpenGLRenderer::setupDrawPoint(float pointSize) { 1055 mDescription.isPoint = true; 1056 mDescription.pointSize = pointSize; 1057} 1058 1059void OpenGLRenderer::setupDrawColor(int color) { 1060 setupDrawColor(color, (color >> 24) & 0xFF); 1061} 1062 1063void OpenGLRenderer::setupDrawColor(int color, int alpha) { 1064 mColorA = alpha / 255.0f; 1065 // Second divide of a by 255 is an optimization, allowing us to simply multiply 1066 // the rgb values by a instead of also dividing by 255 1067 const float a = mColorA / 255.0f; 1068 mColorR = a * ((color >> 16) & 0xFF); 1069 mColorG = a * ((color >> 8) & 0xFF); 1070 mColorB = a * ((color ) & 0xFF); 1071 mColorSet = true; 1072 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA); 1073} 1074 1075void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) { 1076 mColorA = alpha / 255.0f; 1077 // Double-divide of a by 255 is an optimization, allowing us to simply multiply 1078 // the rgb values by a instead of also dividing by 255 1079 const float a = mColorA / 255.0f; 1080 mColorR = a * ((color >> 16) & 0xFF); 1081 mColorG = a * ((color >> 8) & 0xFF); 1082 mColorB = a * ((color ) & 0xFF); 1083 mColorSet = true; 1084 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA); 1085} 1086 1087void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) { 1088 mColorA = a; 1089 mColorR = r; 1090 mColorG = g; 1091 mColorB = b; 1092 mColorSet = true; 1093 mSetShaderColor = mDescription.setColor(r, g, b, a); 1094} 1095 1096void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) { 1097 mColorA = a; 1098 mColorR = r; 1099 mColorG = g; 1100 mColorB = b; 1101 mColorSet = true; 1102 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a); 1103} 1104 1105void OpenGLRenderer::setupDrawShader() { 1106 if (mShader) { 1107 mShader->describe(mDescription, mCaches.extensions); 1108 } 1109} 1110 1111void OpenGLRenderer::setupDrawColorFilter() { 1112 if (mColorFilter) { 1113 mColorFilter->describe(mDescription, mCaches.extensions); 1114 } 1115} 1116 1117void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) { 1118 if (mColorSet && mode == SkXfermode::kClear_Mode) { 1119 mColorA = 1.0f; 1120 mColorR = mColorG = mColorB = 0.0f; 1121 mSetShaderColor = mDescription.modulate = true; 1122 } 1123} 1124 1125void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) { 1126 // When the blending mode is kClear_Mode, we need to use a modulate color 1127 // argb=1,0,0,0 1128 accountForClear(mode); 1129 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, 1130 mDescription, swapSrcDst); 1131} 1132 1133void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) { 1134 // When the blending mode is kClear_Mode, we need to use a modulate color 1135 // argb=1,0,0,0 1136 accountForClear(mode); 1137 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, 1138 mDescription, swapSrcDst); 1139} 1140 1141void OpenGLRenderer::setupDrawProgram() { 1142 useProgram(mCaches.programCache.get(mDescription)); 1143} 1144 1145void OpenGLRenderer::setupDrawDirtyRegionsDisabled() { 1146 mTrackDirtyRegions = false; 1147} 1148 1149void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom, 1150 bool ignoreTransform) { 1151 mModelView.loadTranslate(left, top, 0.0f); 1152 if (!ignoreTransform) { 1153 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1154 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1155 } else { 1156 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); 1157 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom); 1158 } 1159} 1160 1161void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) { 1162 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset); 1163} 1164 1165void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom, 1166 bool ignoreTransform, bool ignoreModelView) { 1167 if (!ignoreModelView) { 1168 mModelView.loadTranslate(left, top, 0.0f); 1169 mModelView.scale(right - left, bottom - top, 1.0f); 1170 } else { 1171 mModelView.loadIdentity(); 1172 } 1173 bool dirty = right - left > 0.0f && bottom - top > 0.0f; 1174 if (!ignoreTransform) { 1175 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1176 if (mTrackDirtyRegions && dirty) { 1177 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1178 } 1179 } else { 1180 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); 1181 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom); 1182 } 1183} 1184 1185void OpenGLRenderer::setupDrawPointUniforms() { 1186 int slot = mCaches.currentProgram->getUniform("pointSize"); 1187 glUniform1f(slot, mDescription.pointSize); 1188} 1189 1190void OpenGLRenderer::setupDrawColorUniforms() { 1191 if (mColorSet || (mShader && mSetShaderColor)) { 1192 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 1193 } 1194} 1195 1196void OpenGLRenderer::setupDrawPureColorUniforms() { 1197 if (mSetShaderColor) { 1198 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 1199 } 1200} 1201 1202void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) { 1203 if (mShader) { 1204 if (ignoreTransform) { 1205 mModelView.loadInverse(*mSnapshot->transform); 1206 } 1207 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit); 1208 } 1209} 1210 1211void OpenGLRenderer::setupDrawShaderIdentityUniforms() { 1212 if (mShader) { 1213 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit); 1214 } 1215} 1216 1217void OpenGLRenderer::setupDrawColorFilterUniforms() { 1218 if (mColorFilter) { 1219 mColorFilter->setupProgram(mCaches.currentProgram); 1220 } 1221} 1222 1223void OpenGLRenderer::setupDrawSimpleMesh() { 1224 bool force = mCaches.bindMeshBuffer(); 1225 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0); 1226 mCaches.unbindIndicesBuffer(); 1227} 1228 1229void OpenGLRenderer::setupDrawTexture(GLuint texture) { 1230 bindTexture(texture); 1231 mTextureUnit++; 1232 mCaches.enableTexCoordsVertexArray(); 1233} 1234 1235void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) { 1236 bindExternalTexture(texture); 1237 mTextureUnit++; 1238 mCaches.enableTexCoordsVertexArray(); 1239} 1240 1241void OpenGLRenderer::setupDrawTextureTransform() { 1242 mDescription.hasTextureTransform = true; 1243} 1244 1245void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) { 1246 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1, 1247 GL_FALSE, &transform.data[0]); 1248} 1249 1250void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { 1251 bool force = false; 1252 if (!vertices) { 1253 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); 1254 } else { 1255 force = mCaches.unbindMeshBuffer(); 1256 } 1257 1258 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices); 1259 if (mCaches.currentProgram->texCoords >= 0) { 1260 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords); 1261 } 1262 1263 mCaches.unbindIndicesBuffer(); 1264} 1265 1266void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) { 1267 bool force = mCaches.unbindMeshBuffer(); 1268 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices); 1269 if (mCaches.currentProgram->texCoords >= 0) { 1270 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords); 1271 } 1272} 1273 1274void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) { 1275 bool force = mCaches.unbindMeshBuffer(); 1276 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 1277 vertices, gVertexStride); 1278 mCaches.unbindIndicesBuffer(); 1279} 1280 1281/** 1282 * Sets up the shader to draw an AA line. We draw AA lines with quads, where there is an 1283 * outer boundary that fades out to 0. The variables set in the shader define the proportion of 1284 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength 1285 * attributes (one per vertex) are values from zero to one that tells the fragment 1286 * shader where the fragment is in relation to the line width/length overall; these values are 1287 * then used to compute the proper color, based on whether the fragment lies in the fading AA 1288 * region of the line. 1289 * Note that we only pass down the width values in this setup function. The length coordinates 1290 * are set up for each individual segment. 1291 */ 1292void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords, 1293 GLvoid* lengthCoords, float boundaryWidthProportion) { 1294 bool force = mCaches.unbindMeshBuffer(); 1295 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 1296 vertices, gAAVertexStride); 1297 mCaches.resetTexCoordsVertexPointer(); 1298 mCaches.unbindIndicesBuffer(); 1299 1300 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth"); 1301 glEnableVertexAttribArray(widthSlot); 1302 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords); 1303 1304 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength"); 1305 glEnableVertexAttribArray(lengthSlot); 1306 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords); 1307 1308 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth"); 1309 glUniform1f(boundaryWidthSlot, boundaryWidthProportion); 1310 1311 // Setting the inverse value saves computations per-fragment in the shader 1312 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth"); 1313 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion)); 1314} 1315 1316void OpenGLRenderer::finishDrawTexture() { 1317} 1318 1319/////////////////////////////////////////////////////////////////////////////// 1320// Drawing 1321/////////////////////////////////////////////////////////////////////////////// 1322 1323bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height, 1324 Rect& dirty, int32_t flags, uint32_t level) { 1325 if (quickReject(0.0f, 0.0f, width, height)) { 1326 return false; 1327 } 1328 1329 // All the usual checks and setup operations (quickReject, setupDraw, etc.) 1330 // will be performed by the display list itself 1331 if (displayList && displayList->isRenderable()) { 1332 return displayList->replay(*this, dirty, flags, level); 1333 } 1334 1335 return false; 1336} 1337 1338void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) { 1339 if (displayList) { 1340 displayList->output(*this, level); 1341 } 1342} 1343 1344void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) { 1345 int alpha; 1346 SkXfermode::Mode mode; 1347 getAlphaAndMode(paint, &alpha, &mode); 1348 1349 float x = left; 1350 float y = top; 1351 1352 GLenum filter = GL_LINEAR; 1353 bool ignoreTransform = false; 1354 if (mSnapshot->transform->isPureTranslate()) { 1355 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 1356 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 1357 ignoreTransform = true; 1358 filter = GL_NEAREST; 1359 } else { 1360 filter = FILTER(paint); 1361 } 1362 1363 setupDraw(); 1364 setupDrawWithTexture(true); 1365 if (paint) { 1366 setupDrawAlpha8Color(paint->getColor(), alpha); 1367 } 1368 setupDrawColorFilter(); 1369 setupDrawShader(); 1370 setupDrawBlending(true, mode); 1371 setupDrawProgram(); 1372 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform); 1373 1374 setupDrawTexture(texture->id); 1375 texture->setWrap(GL_CLAMP_TO_EDGE); 1376 texture->setFilter(filter); 1377 1378 setupDrawPureColorUniforms(); 1379 setupDrawColorFilterUniforms(); 1380 setupDrawShaderUniforms(); 1381 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 1382 1383 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1384 1385 finishDrawTexture(); 1386} 1387 1388void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { 1389 const float right = left + bitmap->width(); 1390 const float bottom = top + bitmap->height(); 1391 1392 if (quickReject(left, top, right, bottom)) { 1393 return; 1394 } 1395 1396 mCaches.activeTexture(0); 1397 Texture* texture = mCaches.textureCache.get(bitmap); 1398 if (!texture) return; 1399 const AutoTexture autoCleanup(texture); 1400 1401 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { 1402 drawAlphaBitmap(texture, left, top, paint); 1403 } else { 1404 drawTextureRect(left, top, right, bottom, texture, paint); 1405 } 1406} 1407 1408void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { 1409 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 1410 const mat4 transform(*matrix); 1411 transform.mapRect(r); 1412 1413 if (quickReject(r.left, r.top, r.right, r.bottom)) { 1414 return; 1415 } 1416 1417 mCaches.activeTexture(0); 1418 Texture* texture = mCaches.textureCache.get(bitmap); 1419 if (!texture) return; 1420 const AutoTexture autoCleanup(texture); 1421 1422 // This could be done in a cheaper way, all we need is pass the matrix 1423 // to the vertex shader. The save/restore is a bit overkill. 1424 save(SkCanvas::kMatrix_SaveFlag); 1425 concatMatrix(matrix); 1426 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint); 1427 restore(); 1428} 1429 1430void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight, 1431 float* vertices, int* colors, SkPaint* paint) { 1432 // TODO: Do a quickReject 1433 if (!vertices || mSnapshot->isIgnored()) { 1434 return; 1435 } 1436 1437 mCaches.activeTexture(0); 1438 Texture* texture = mCaches.textureCache.get(bitmap); 1439 if (!texture) return; 1440 const AutoTexture autoCleanup(texture); 1441 1442 texture->setWrap(GL_CLAMP_TO_EDGE, true); 1443 texture->setFilter(FILTER(paint), true); 1444 1445 int alpha; 1446 SkXfermode::Mode mode; 1447 getAlphaAndMode(paint, &alpha, &mode); 1448 1449 const uint32_t count = meshWidth * meshHeight * 6; 1450 1451 float left = FLT_MAX; 1452 float top = FLT_MAX; 1453 float right = FLT_MIN; 1454 float bottom = FLT_MIN; 1455 1456#if RENDER_LAYERS_AS_REGIONS 1457 const bool hasActiveLayer = hasLayer(); 1458#else 1459 const bool hasActiveLayer = false; 1460#endif 1461 1462 // TODO: Support the colors array 1463 TextureVertex mesh[count]; 1464 TextureVertex* vertex = mesh; 1465 for (int32_t y = 0; y < meshHeight; y++) { 1466 for (int32_t x = 0; x < meshWidth; x++) { 1467 uint32_t i = (y * (meshWidth + 1) + x) * 2; 1468 1469 float u1 = float(x) / meshWidth; 1470 float u2 = float(x + 1) / meshWidth; 1471 float v1 = float(y) / meshHeight; 1472 float v2 = float(y + 1) / meshHeight; 1473 1474 int ax = i + (meshWidth + 1) * 2; 1475 int ay = ax + 1; 1476 int bx = i; 1477 int by = bx + 1; 1478 int cx = i + 2; 1479 int cy = cx + 1; 1480 int dx = i + (meshWidth + 1) * 2 + 2; 1481 int dy = dx + 1; 1482 1483 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); 1484 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1); 1485 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); 1486 1487 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); 1488 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); 1489 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2); 1490 1491#if RENDER_LAYERS_AS_REGIONS 1492 if (hasActiveLayer) { 1493 // TODO: This could be optimized to avoid unnecessary ops 1494 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx]))); 1495 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy]))); 1496 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx]))); 1497 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy]))); 1498 } 1499#endif 1500 } 1501 } 1502 1503#if RENDER_LAYERS_AS_REGIONS 1504 if (hasActiveLayer) { 1505 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1506 } 1507#endif 1508 1509 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f, 1510 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0], 1511 GL_TRIANGLES, count, false, false, 0, false, false); 1512} 1513 1514void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 1515 float srcLeft, float srcTop, float srcRight, float srcBottom, 1516 float dstLeft, float dstTop, float dstRight, float dstBottom, 1517 SkPaint* paint) { 1518 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { 1519 return; 1520 } 1521 1522 mCaches.activeTexture(0); 1523 Texture* texture = mCaches.textureCache.get(bitmap); 1524 if (!texture) return; 1525 const AutoTexture autoCleanup(texture); 1526 1527 const float width = texture->width; 1528 const float height = texture->height; 1529 1530 const float u1 = fmax(0.0f, srcLeft / width); 1531 const float v1 = fmax(0.0f, srcTop / height); 1532 const float u2 = fmin(1.0f, srcRight / width); 1533 const float v2 = fmin(1.0f, srcBottom / height); 1534 1535 mCaches.unbindMeshBuffer(); 1536 resetDrawTextureTexCoords(u1, v1, u2, v2); 1537 1538 int alpha; 1539 SkXfermode::Mode mode; 1540 getAlphaAndMode(paint, &alpha, &mode); 1541 1542 texture->setWrap(GL_CLAMP_TO_EDGE, true); 1543 1544 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) { 1545 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f); 1546 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f); 1547 1548 GLenum filter = GL_NEAREST; 1549 // Enable linear filtering if the source rectangle is scaled 1550 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) { 1551 filter = FILTER(paint); 1552 } 1553 1554 texture->setFilter(filter, true); 1555 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop), 1556 texture->id, alpha / 255.0f, mode, texture->blend, 1557 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 1558 GL_TRIANGLE_STRIP, gMeshCount, false, true); 1559 } else { 1560 texture->setFilter(FILTER(paint), true); 1561 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f, 1562 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 1563 GL_TRIANGLE_STRIP, gMeshCount); 1564 } 1565 1566 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 1567} 1568 1569void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 1570 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, 1571 float left, float top, float right, float bottom, SkPaint* paint) { 1572 if (quickReject(left, top, right, bottom)) { 1573 return; 1574 } 1575 1576 mCaches.activeTexture(0); 1577 Texture* texture = mCaches.textureCache.get(bitmap); 1578 if (!texture) return; 1579 const AutoTexture autoCleanup(texture); 1580 texture->setWrap(GL_CLAMP_TO_EDGE, true); 1581 texture->setFilter(GL_LINEAR, true); 1582 1583 int alpha; 1584 SkXfermode::Mode mode; 1585 getAlphaAndMode(paint, &alpha, &mode); 1586 1587 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), 1588 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); 1589 1590 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) { 1591 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 1592#if RENDER_LAYERS_AS_REGIONS 1593 // Mark the current layer dirty where we are going to draw the patch 1594 if (hasLayer() && mesh->hasEmptyQuads) { 1595 const float offsetX = left + mSnapshot->transform->getTranslateX(); 1596 const float offsetY = top + mSnapshot->transform->getTranslateY(); 1597 const size_t count = mesh->quads.size(); 1598 for (size_t i = 0; i < count; i++) { 1599 const Rect& bounds = mesh->quads.itemAt(i); 1600 if (CC_LIKELY(pureTranslate)) { 1601 const float x = (int) floorf(bounds.left + offsetX + 0.5f); 1602 const float y = (int) floorf(bounds.top + offsetY + 0.5f); 1603 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight()); 1604 } else { 1605 dirtyLayer(left + bounds.left, top + bounds.top, 1606 left + bounds.right, top + bounds.bottom, *mSnapshot->transform); 1607 } 1608 } 1609 } 1610#endif 1611 1612 if (CC_LIKELY(pureTranslate)) { 1613 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 1614 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 1615 1616 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f, 1617 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 1618 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer, 1619 true, !mesh->hasEmptyQuads); 1620 } else { 1621 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, 1622 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 1623 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer, 1624 true, !mesh->hasEmptyQuads); 1625 } 1626 } 1627} 1628 1629/** 1630 * This function uses a similar approach to that of AA lines in the drawLines() function. 1631 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment 1632 * shader to compute the translucency of the color, determined by whether a given pixel is 1633 * within that boundary region and how far into the region it is. 1634 */ 1635void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom, 1636 int color, SkXfermode::Mode mode) { 1637 float inverseScaleX = 1.0f; 1638 float inverseScaleY = 1.0f; 1639 // The quad that we use needs to account for scaling. 1640 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) { 1641 Matrix4 *mat = mSnapshot->transform; 1642 float m00 = mat->data[Matrix4::kScaleX]; 1643 float m01 = mat->data[Matrix4::kSkewY]; 1644 float m02 = mat->data[2]; 1645 float m10 = mat->data[Matrix4::kSkewX]; 1646 float m11 = mat->data[Matrix4::kScaleX]; 1647 float m12 = mat->data[6]; 1648 float scaleX = sqrt(m00 * m00 + m01 * m01); 1649 float scaleY = sqrt(m10 * m10 + m11 * m11); 1650 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0; 1651 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0; 1652 } 1653 1654 setupDraw(); 1655 setupDrawNoTexture(); 1656 setupDrawAALine(); 1657 setupDrawColor(color); 1658 setupDrawColorFilter(); 1659 setupDrawShader(); 1660 setupDrawBlending(true, mode); 1661 setupDrawProgram(); 1662 setupDrawModelViewIdentity(true); 1663 setupDrawColorUniforms(); 1664 setupDrawColorFilterUniforms(); 1665 setupDrawShaderIdentityUniforms(); 1666 1667 AAVertex rects[4]; 1668 AAVertex* aaVertices = &rects[0]; 1669 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset; 1670 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset; 1671 1672 float boundarySizeX = .5 * inverseScaleX; 1673 float boundarySizeY = .5 * inverseScaleY; 1674 1675 // Adjust the rect by the AA boundary padding 1676 left -= boundarySizeX; 1677 right += boundarySizeX; 1678 top -= boundarySizeY; 1679 bottom += boundarySizeY; 1680 1681 float width = right - left; 1682 float height = bottom - top; 1683 1684 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0; 1685 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0; 1686 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion); 1687 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength"); 1688 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength"); 1689 glUniform1f(boundaryLengthSlot, boundaryHeightProportion); 1690 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion)); 1691 1692 if (!quickReject(left, top, right, bottom)) { 1693 AAVertex::set(aaVertices++, left, bottom, 1, 1); 1694 AAVertex::set(aaVertices++, left, top, 1, 0); 1695 AAVertex::set(aaVertices++, right, bottom, 0, 1); 1696 AAVertex::set(aaVertices++, right, top, 0, 0); 1697 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1698 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 1699 } 1700} 1701 1702/** 1703 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization 1704 * rules for those lines produces some unexpected results, and may vary between hardware devices. 1705 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the 1706 * corners of the quads on either side of each line endpoint, separated by the strokeWidth 1707 * of the line. Hairlines are more involved because we need to account for transform scaling 1708 * to end up with a one-pixel-wide line in screen space.. 1709 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader 1710 * in combination with values that we calculate and pass down in this method. The basic approach 1711 * is that the quad we create contains both the core line area plus a bounding area in which 1712 * the translucent/AA pixels are drawn. The values we calculate tell the shader what 1713 * proportion of the width and the length of a given segment is represented by the boundary 1714 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad. 1715 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel 1716 * on the inside). This ends up giving the result we want, with pixels that are completely 1717 * 'inside' the line area being filled opaquely and the other pixels being filled according to 1718 * how far into the boundary region they are, which is determined by shader interpolation. 1719 */ 1720void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { 1721 if (mSnapshot->isIgnored()) return; 1722 1723 const bool isAA = paint->isAntiAlias(); 1724 // We use half the stroke width here because we're going to position the quad 1725 // corner vertices half of the width away from the line endpoints 1726 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f; 1727 // A stroke width of 0 has a special meaning in Skia: 1728 // it draws a line 1 px wide regardless of current transform 1729 bool isHairLine = paint->getStrokeWidth() == 0.0f; 1730 float inverseScaleX = 1.0f; 1731 float inverseScaleY = 1.0f; 1732 bool scaled = false; 1733 int alpha; 1734 SkXfermode::Mode mode; 1735 int generatedVerticesCount = 0; 1736 int verticesCount = count; 1737 if (count > 4) { 1738 // Polyline: account for extra vertices needed for continuous tri-strip 1739 verticesCount += (count - 4); 1740 } 1741 1742 if (isHairLine || isAA) { 1743 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines 1744 // the line on the screen should always be one pixel wide regardless of scale. For 1745 // AA lines, we only want one pixel of translucent boundary around the quad. 1746 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) { 1747 Matrix4 *mat = mSnapshot->transform; 1748 float m00 = mat->data[Matrix4::kScaleX]; 1749 float m01 = mat->data[Matrix4::kSkewY]; 1750 float m02 = mat->data[2]; 1751 float m10 = mat->data[Matrix4::kSkewX]; 1752 float m11 = mat->data[Matrix4::kScaleX]; 1753 float m12 = mat->data[6]; 1754 float scaleX = sqrtf(m00 * m00 + m01 * m01); 1755 float scaleY = sqrtf(m10 * m10 + m11 * m11); 1756 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0; 1757 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0; 1758 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) { 1759 scaled = true; 1760 } 1761 } 1762 } 1763 1764 getAlphaAndMode(paint, &alpha, &mode); 1765 setupDraw(); 1766 setupDrawNoTexture(); 1767 if (isAA) { 1768 setupDrawAALine(); 1769 } 1770 setupDrawColor(paint->getColor(), alpha); 1771 setupDrawColorFilter(); 1772 setupDrawShader(); 1773 setupDrawBlending(isAA, mode); 1774 setupDrawProgram(); 1775 setupDrawModelViewIdentity(true); 1776 setupDrawColorUniforms(); 1777 setupDrawColorFilterUniforms(); 1778 setupDrawShaderIdentityUniforms(); 1779 1780 if (isHairLine) { 1781 // Set a real stroke width to be used in quad construction 1782 halfStrokeWidth = isAA? 1 : .5; 1783 } else if (isAA && !scaled) { 1784 // Expand boundary to enable AA calculations on the quad border 1785 halfStrokeWidth += .5f; 1786 } 1787 Vertex lines[verticesCount]; 1788 Vertex* vertices = &lines[0]; 1789 AAVertex wLines[verticesCount]; 1790 AAVertex* aaVertices = &wLines[0]; 1791 if (CC_UNLIKELY(!isAA)) { 1792 setupDrawVertices(vertices); 1793 } else { 1794 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset; 1795 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset; 1796 // innerProportion is the ratio of the inner (non-AA) part of the line to the total 1797 // AA stroke width (the base stroke width expanded by a half pixel on either side). 1798 // This value is used in the fragment shader to determine how to fill fragments. 1799 // We will need to calculate the actual width proportion on each segment for 1800 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled. 1801 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth); 1802 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion); 1803 } 1804 1805 AAVertex* prevAAVertex = NULL; 1806 Vertex* prevVertex = NULL; 1807 1808 int boundaryLengthSlot = -1; 1809 int inverseBoundaryLengthSlot = -1; 1810 int boundaryWidthSlot = -1; 1811 int inverseBoundaryWidthSlot = -1; 1812 for (int i = 0; i < count; i += 4) { 1813 // a = start point, b = end point 1814 vec2 a(points[i], points[i + 1]); 1815 vec2 b(points[i + 2], points[i + 3]); 1816 float length = 0; 1817 float boundaryLengthProportion = 0; 1818 float boundaryWidthProportion = 0; 1819 1820 // Find the normal to the line 1821 vec2 n = (b - a).copyNormalized() * halfStrokeWidth; 1822 if (isHairLine) { 1823 if (isAA) { 1824 float wideningFactor; 1825 if (fabs(n.x) >= fabs(n.y)) { 1826 wideningFactor = fabs(1.0f / n.x); 1827 } else { 1828 wideningFactor = fabs(1.0f / n.y); 1829 } 1830 n *= wideningFactor; 1831 } 1832 if (scaled) { 1833 n.x *= inverseScaleX; 1834 n.y *= inverseScaleY; 1835 } 1836 } else if (scaled) { 1837 // Extend n by .5 pixel on each side, post-transform 1838 vec2 extendedN = n.copyNormalized(); 1839 extendedN /= 2; 1840 extendedN.x *= inverseScaleX; 1841 extendedN.y *= inverseScaleY; 1842 float extendedNLength = extendedN.length(); 1843 // We need to set this value on the shader prior to drawing 1844 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength); 1845 n += extendedN; 1846 } 1847 float x = n.x; 1848 n.x = -n.y; 1849 n.y = x; 1850 1851 // aa lines expand the endpoint vertices to encompass the AA boundary 1852 if (isAA) { 1853 vec2 abVector = (b - a); 1854 length = abVector.length(); 1855 abVector.normalize(); 1856 if (scaled) { 1857 abVector.x *= inverseScaleX; 1858 abVector.y *= inverseScaleY; 1859 float abLength = abVector.length(); 1860 boundaryLengthProportion = abLength / (length + abLength); 1861 } else { 1862 boundaryLengthProportion = .5 / (length + 1); 1863 } 1864 abVector /= 2; 1865 a -= abVector; 1866 b += abVector; 1867 } 1868 1869 // Four corners of the rectangle defining a thick line 1870 vec2 p1 = a - n; 1871 vec2 p2 = a + n; 1872 vec2 p3 = b + n; 1873 vec2 p4 = b - n; 1874 1875 1876 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x))); 1877 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x))); 1878 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y))); 1879 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y))); 1880 1881 if (!quickReject(left, top, right, bottom)) { 1882 if (!isAA) { 1883 if (prevVertex != NULL) { 1884 // Issue two repeat vertices to create degenerate triangles to bridge 1885 // between the previous line and the new one. This is necessary because 1886 // we are creating a single triangle_strip which will contain 1887 // potentially discontinuous line segments. 1888 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]); 1889 Vertex::set(vertices++, p1.x, p1.y); 1890 generatedVerticesCount += 2; 1891 } 1892 Vertex::set(vertices++, p1.x, p1.y); 1893 Vertex::set(vertices++, p2.x, p2.y); 1894 Vertex::set(vertices++, p4.x, p4.y); 1895 Vertex::set(vertices++, p3.x, p3.y); 1896 prevVertex = vertices - 1; 1897 generatedVerticesCount += 4; 1898 } else { 1899 if (!isHairLine && scaled) { 1900 // Must set width proportions per-segment for scaled non-hairlines to use the 1901 // correct AA boundary dimensions 1902 if (boundaryWidthSlot < 0) { 1903 boundaryWidthSlot = 1904 mCaches.currentProgram->getUniform("boundaryWidth"); 1905 inverseBoundaryWidthSlot = 1906 mCaches.currentProgram->getUniform("inverseBoundaryWidth"); 1907 } 1908 glUniform1f(boundaryWidthSlot, boundaryWidthProportion); 1909 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion)); 1910 } 1911 if (boundaryLengthSlot < 0) { 1912 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength"); 1913 inverseBoundaryLengthSlot = 1914 mCaches.currentProgram->getUniform("inverseBoundaryLength"); 1915 } 1916 glUniform1f(boundaryLengthSlot, boundaryLengthProportion); 1917 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion)); 1918 1919 if (prevAAVertex != NULL) { 1920 // Issue two repeat vertices to create degenerate triangles to bridge 1921 // between the previous line and the new one. This is necessary because 1922 // we are creating a single triangle_strip which will contain 1923 // potentially discontinuous line segments. 1924 AAVertex::set(aaVertices++,prevAAVertex->position[0], 1925 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length); 1926 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); 1927 generatedVerticesCount += 2; 1928 } 1929 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); 1930 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0); 1931 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1); 1932 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0); 1933 prevAAVertex = aaVertices - 1; 1934 generatedVerticesCount += 4; 1935 } 1936 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top, 1937 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom, 1938 *mSnapshot->transform); 1939 } 1940 } 1941 if (generatedVerticesCount > 0) { 1942 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount); 1943 } 1944} 1945 1946void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) { 1947 if (mSnapshot->isIgnored()) return; 1948 1949 // TODO: The paint's cap style defines whether the points are square or circular 1950 // TODO: Handle AA for round points 1951 1952 // A stroke width of 0 has a special meaning in Skia: 1953 // it draws an unscaled 1px point 1954 float strokeWidth = paint->getStrokeWidth(); 1955 const bool isHairLine = paint->getStrokeWidth() == 0.0f; 1956 if (isHairLine) { 1957 // Now that we know it's hairline, we can set the effective width, to be used later 1958 strokeWidth = 1.0f; 1959 } 1960 const float halfWidth = strokeWidth / 2; 1961 int alpha; 1962 SkXfermode::Mode mode; 1963 getAlphaAndMode(paint, &alpha, &mode); 1964 1965 int verticesCount = count >> 1; 1966 int generatedVerticesCount = 0; 1967 1968 TextureVertex pointsData[verticesCount]; 1969 TextureVertex* vertex = &pointsData[0]; 1970 1971 setupDraw(); 1972 setupDrawNoTexture(); 1973 setupDrawPoint(strokeWidth); 1974 setupDrawColor(paint->getColor(), alpha); 1975 setupDrawColorFilter(); 1976 setupDrawShader(); 1977 setupDrawBlending(mode); 1978 setupDrawProgram(); 1979 setupDrawModelViewIdentity(true); 1980 setupDrawColorUniforms(); 1981 setupDrawColorFilterUniforms(); 1982 setupDrawPointUniforms(); 1983 setupDrawShaderIdentityUniforms(); 1984 setupDrawMesh(vertex); 1985 1986 for (int i = 0; i < count; i += 2) { 1987 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f); 1988 generatedVerticesCount++; 1989 float left = points[i] - halfWidth; 1990 float right = points[i] + halfWidth; 1991 float top = points[i + 1] - halfWidth; 1992 float bottom = points [i + 1] + halfWidth; 1993 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1994 } 1995 1996 glDrawArrays(GL_POINTS, 0, generatedVerticesCount); 1997} 1998 1999void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 2000 // No need to check against the clip, we fill the clip region 2001 if (mSnapshot->isIgnored()) return; 2002 2003 Rect& clip(*mSnapshot->clipRect); 2004 clip.snapToPixelBoundaries(); 2005 2006 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); 2007} 2008 2009void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) { 2010 if (!texture) return; 2011 const AutoTexture autoCleanup(texture); 2012 2013 const float x = left + texture->left - texture->offset; 2014 const float y = top + texture->top - texture->offset; 2015 2016 drawPathTexture(texture, x, y, paint); 2017} 2018 2019void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom, 2020 float rx, float ry, SkPaint* paint) { 2021 if (mSnapshot->isIgnored()) return; 2022 2023 mCaches.activeTexture(0); 2024 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect( 2025 right - left, bottom - top, rx, ry, paint); 2026 drawShape(left, top, texture, paint); 2027} 2028 2029void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) { 2030 if (mSnapshot->isIgnored()) return; 2031 2032 mCaches.activeTexture(0); 2033 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint); 2034 drawShape(x - radius, y - radius, texture, paint); 2035} 2036 2037void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) { 2038 if (mSnapshot->isIgnored()) return; 2039 2040 mCaches.activeTexture(0); 2041 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint); 2042 drawShape(left, top, texture, paint); 2043} 2044 2045void OpenGLRenderer::drawArc(float left, float top, float right, float bottom, 2046 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) { 2047 if (mSnapshot->isIgnored()) return; 2048 2049 if (fabs(sweepAngle) >= 360.0f) { 2050 drawOval(left, top, right, bottom, paint); 2051 return; 2052 } 2053 2054 mCaches.activeTexture(0); 2055 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top, 2056 startAngle, sweepAngle, useCenter, paint); 2057 drawShape(left, top, texture, paint); 2058} 2059 2060void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom, 2061 SkPaint* paint) { 2062 if (mSnapshot->isIgnored()) return; 2063 2064 mCaches.activeTexture(0); 2065 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint); 2066 drawShape(left, top, texture, paint); 2067} 2068 2069void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) { 2070 if (p->getStyle() != SkPaint::kFill_Style) { 2071 drawRectAsShape(left, top, right, bottom, p); 2072 return; 2073 } 2074 2075 if (quickReject(left, top, right, bottom)) { 2076 return; 2077 } 2078 2079 SkXfermode::Mode mode; 2080 if (!mCaches.extensions.hasFramebufferFetch()) { 2081 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 2082 if (!isMode) { 2083 // Assume SRC_OVER 2084 mode = SkXfermode::kSrcOver_Mode; 2085 } 2086 } else { 2087 mode = getXfermode(p->getXfermode()); 2088 } 2089 2090 int color = p->getColor(); 2091 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) { 2092 drawAARect(left, top, right, bottom, color, mode); 2093 } else { 2094 drawColorRect(left, top, right, bottom, color, mode); 2095 } 2096} 2097 2098void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count, 2099 const float* positions, SkPaint* paint) { 2100 if (text == NULL || count == 0 || mSnapshot->isIgnored() || 2101 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { 2102 return; 2103 } 2104 2105 // NOTE: Skia does not support perspective transform on drawPosText yet 2106 if (!mSnapshot->transform->isSimple()) { 2107 return; 2108 } 2109 2110 float x = 0.0f; 2111 float y = 0.0f; 2112 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 2113 if (pureTranslate) { 2114 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); 2115 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); 2116 } 2117 2118 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); 2119 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), 2120 paint->getTextSize()); 2121 2122 int alpha; 2123 SkXfermode::Mode mode; 2124 getAlphaAndMode(paint, &alpha, &mode); 2125 2126 // Pick the appropriate texture filtering 2127 bool linearFilter = mSnapshot->transform->changesBounds(); 2128 if (pureTranslate && !linearFilter) { 2129 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; 2130 } 2131 2132 mCaches.activeTexture(0); 2133 setupDraw(); 2134 setupDrawDirtyRegionsDisabled(); 2135 setupDrawWithTexture(true); 2136 setupDrawAlpha8Color(paint->getColor(), alpha); 2137 setupDrawColorFilter(); 2138 setupDrawShader(); 2139 setupDrawBlending(true, mode); 2140 setupDrawProgram(); 2141 setupDrawModelView(x, y, x, y, pureTranslate, true); 2142 setupDrawTexture(fontRenderer.getTexture(linearFilter)); 2143 setupDrawPureColorUniforms(); 2144 setupDrawColorFilterUniforms(); 2145 setupDrawShaderUniforms(pureTranslate); 2146 2147 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); 2148 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 2149 2150#if RENDER_LAYERS_AS_REGIONS 2151 const bool hasActiveLayer = hasLayer(); 2152#else 2153 const bool hasActiveLayer = false; 2154#endif 2155 2156 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y, 2157 positions, hasActiveLayer ? &bounds : NULL)) { 2158#if RENDER_LAYERS_AS_REGIONS 2159 if (hasActiveLayer) { 2160 if (!pureTranslate) { 2161 mSnapshot->transform->mapRect(bounds); 2162 } 2163 dirtyLayerUnchecked(bounds, getRegion()); 2164 } 2165#endif 2166 } 2167} 2168 2169void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, 2170 float x, float y, SkPaint* paint, float length) { 2171 if (text == NULL || count == 0 || mSnapshot->isIgnored() || 2172 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { 2173 return; 2174 } 2175 2176 switch (paint->getTextAlign()) { 2177 case SkPaint::kCenter_Align: 2178 if (length < 0.0f) length = paint->measureText(text, bytesCount); 2179 x -= length / 2.0f; 2180 break; 2181 case SkPaint::kRight_Align: 2182 if (length < 0.0f) length = paint->measureText(text, bytesCount); 2183 x -= length; 2184 break; 2185 default: 2186 break; 2187 } 2188 2189 SkPaint::FontMetrics metrics; 2190 paint->getFontMetrics(&metrics, 0.0f); 2191 // If no length was specified, just perform the hit test on the Y axis 2192 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) { 2193 return; 2194 } 2195 2196 const float oldX = x; 2197 const float oldY = y; 2198 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 2199 if (CC_LIKELY(pureTranslate)) { 2200 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); 2201 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); 2202 } 2203 2204#if DEBUG_GLYPHS 2205 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface())); 2206#endif 2207 2208 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); 2209 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), 2210 paint->getTextSize()); 2211 2212 int alpha; 2213 SkXfermode::Mode mode; 2214 getAlphaAndMode(paint, &alpha, &mode); 2215 2216 if (CC_UNLIKELY(mHasShadow)) { 2217 mCaches.activeTexture(0); 2218 2219 mCaches.dropShadowCache.setFontRenderer(fontRenderer); 2220 const ShadowTexture* shadow = mCaches.dropShadowCache.get( 2221 paint, text, bytesCount, count, mShadowRadius); 2222 const AutoTexture autoCleanup(shadow); 2223 2224 const float sx = oldX - shadow->left + mShadowDx; 2225 const float sy = oldY - shadow->top + mShadowDy; 2226 2227 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF); 2228 int shadowColor = mShadowColor; 2229 if (mShader) { 2230 shadowColor = 0xffffffff; 2231 } 2232 2233 setupDraw(); 2234 setupDrawWithTexture(true); 2235 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha); 2236 setupDrawColorFilter(); 2237 setupDrawShader(); 2238 setupDrawBlending(true, mode); 2239 setupDrawProgram(); 2240 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height); 2241 setupDrawTexture(shadow->id); 2242 setupDrawPureColorUniforms(); 2243 setupDrawColorFilterUniforms(); 2244 setupDrawShaderUniforms(); 2245 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 2246 2247 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 2248 } 2249 2250 // Pick the appropriate texture filtering 2251 bool linearFilter = mSnapshot->transform->changesBounds(); 2252 if (pureTranslate && !linearFilter) { 2253 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; 2254 } 2255 2256 mCaches.activeTexture(0); 2257 setupDraw(); 2258 setupDrawDirtyRegionsDisabled(); 2259 setupDrawWithTexture(true); 2260 setupDrawAlpha8Color(paint->getColor(), alpha); 2261 setupDrawColorFilter(); 2262 setupDrawShader(); 2263 setupDrawBlending(true, mode); 2264 setupDrawProgram(); 2265 setupDrawModelView(x, y, x, y, pureTranslate, true); 2266 setupDrawTexture(fontRenderer.getTexture(linearFilter)); 2267 setupDrawPureColorUniforms(); 2268 setupDrawColorFilterUniforms(); 2269 setupDrawShaderUniforms(pureTranslate); 2270 2271 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); 2272 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 2273 2274#if RENDER_LAYERS_AS_REGIONS 2275 const bool hasActiveLayer = hasLayer(); 2276#else 2277 const bool hasActiveLayer = false; 2278#endif 2279 2280 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y, 2281 hasActiveLayer ? &bounds : NULL)) { 2282#if RENDER_LAYERS_AS_REGIONS 2283 if (hasActiveLayer) { 2284 if (!pureTranslate) { 2285 mSnapshot->transform->mapRect(bounds); 2286 } 2287 dirtyLayerUnchecked(bounds, getRegion()); 2288 } 2289#endif 2290 } 2291 2292 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint); 2293} 2294 2295void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path, 2296 float hOffset, float vOffset, SkPaint* paint) { 2297 // TODO: Implement 2298} 2299 2300void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { 2301 if (mSnapshot->isIgnored()) return; 2302 2303 mCaches.activeTexture(0); 2304 2305 // TODO: Perform early clip test before we rasterize the path 2306 const PathTexture* texture = mCaches.pathCache.get(path, paint); 2307 if (!texture) return; 2308 const AutoTexture autoCleanup(texture); 2309 2310 const float x = texture->left - texture->offset; 2311 const float y = texture->top - texture->offset; 2312 2313 drawPathTexture(texture, x, y, paint); 2314} 2315 2316void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) { 2317 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) { 2318 return; 2319 } 2320 2321 mCaches.activeTexture(0); 2322 2323 int alpha; 2324 SkXfermode::Mode mode; 2325 getAlphaAndMode(paint, &alpha, &mode); 2326 2327 layer->setAlpha(alpha, mode); 2328 2329#if RENDER_LAYERS_AS_REGIONS 2330 if (CC_LIKELY(!layer->region.isEmpty())) { 2331 if (layer->region.isRect()) { 2332 composeLayerRect(layer, layer->regionRect); 2333 } else if (layer->mesh) { 2334 const float a = alpha / 255.0f; 2335 const Rect& rect = layer->layer; 2336 2337 setupDraw(); 2338 setupDrawWithTexture(); 2339 setupDrawColor(a, a, a, a); 2340 setupDrawColorFilter(); 2341 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false); 2342 setupDrawProgram(); 2343 setupDrawPureColorUniforms(); 2344 setupDrawColorFilterUniforms(); 2345 setupDrawTexture(layer->getTexture()); 2346 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) { 2347 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); 2348 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); 2349 2350 layer->setFilter(GL_NEAREST); 2351 setupDrawModelViewTranslate(x, y, 2352 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true); 2353 } else { 2354 layer->setFilter(GL_LINEAR); 2355 setupDrawModelViewTranslate(x, y, 2356 x + layer->layer.getWidth(), y + layer->layer.getHeight()); 2357 } 2358 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]); 2359 2360 glDrawElements(GL_TRIANGLES, layer->meshElementCount, 2361 GL_UNSIGNED_SHORT, layer->meshIndices); 2362 2363 finishDrawTexture(); 2364 2365#if DEBUG_LAYERS_AS_REGIONS 2366 drawRegionRects(layer->region); 2367#endif 2368 } 2369 } 2370#else 2371 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight()); 2372 composeLayerRect(layer, r); 2373#endif 2374} 2375 2376/////////////////////////////////////////////////////////////////////////////// 2377// Shaders 2378/////////////////////////////////////////////////////////////////////////////// 2379 2380void OpenGLRenderer::resetShader() { 2381 mShader = NULL; 2382} 2383 2384void OpenGLRenderer::setupShader(SkiaShader* shader) { 2385 mShader = shader; 2386 if (mShader) { 2387 mShader->set(&mCaches.textureCache, &mCaches.gradientCache); 2388 } 2389} 2390 2391/////////////////////////////////////////////////////////////////////////////// 2392// Color filters 2393/////////////////////////////////////////////////////////////////////////////// 2394 2395void OpenGLRenderer::resetColorFilter() { 2396 mColorFilter = NULL; 2397} 2398 2399void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { 2400 mColorFilter = filter; 2401} 2402 2403/////////////////////////////////////////////////////////////////////////////// 2404// Drop shadow 2405/////////////////////////////////////////////////////////////////////////////// 2406 2407void OpenGLRenderer::resetShadow() { 2408 mHasShadow = false; 2409} 2410 2411void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { 2412 mHasShadow = true; 2413 mShadowRadius = radius; 2414 mShadowDx = dx; 2415 mShadowDy = dy; 2416 mShadowColor = color; 2417} 2418 2419/////////////////////////////////////////////////////////////////////////////// 2420// Draw filters 2421/////////////////////////////////////////////////////////////////////////////// 2422 2423void OpenGLRenderer::resetPaintFilter() { 2424 mHasDrawFilter = false; 2425} 2426 2427void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) { 2428 mHasDrawFilter = true; 2429 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags; 2430 mPaintFilterSetBits = setBits & SkPaint::kAllFlags; 2431} 2432 2433SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) { 2434 if (!mHasDrawFilter || !paint) return paint; 2435 2436 uint32_t flags = paint->getFlags(); 2437 2438 mFilteredPaint = *paint; 2439 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits); 2440 2441 return &mFilteredPaint; 2442} 2443 2444/////////////////////////////////////////////////////////////////////////////// 2445// Drawing implementation 2446/////////////////////////////////////////////////////////////////////////////// 2447 2448void OpenGLRenderer::drawPathTexture(const PathTexture* texture, 2449 float x, float y, SkPaint* paint) { 2450 if (quickReject(x, y, x + texture->width, y + texture->height)) { 2451 return; 2452 } 2453 2454 int alpha; 2455 SkXfermode::Mode mode; 2456 getAlphaAndMode(paint, &alpha, &mode); 2457 2458 setupDraw(); 2459 setupDrawWithTexture(true); 2460 setupDrawAlpha8Color(paint->getColor(), alpha); 2461 setupDrawColorFilter(); 2462 setupDrawShader(); 2463 setupDrawBlending(true, mode); 2464 setupDrawProgram(); 2465 setupDrawModelView(x, y, x + texture->width, y + texture->height); 2466 setupDrawTexture(texture->id); 2467 setupDrawPureColorUniforms(); 2468 setupDrawColorFilterUniforms(); 2469 setupDrawShaderUniforms(); 2470 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 2471 2472 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 2473 2474 finishDrawTexture(); 2475} 2476 2477// Same values used by Skia 2478#define kStdStrikeThru_Offset (-6.0f / 21.0f) 2479#define kStdUnderline_Offset (1.0f / 9.0f) 2480#define kStdUnderline_Thickness (1.0f / 18.0f) 2481 2482void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, 2483 float x, float y, SkPaint* paint) { 2484 // Handle underline and strike-through 2485 uint32_t flags = paint->getFlags(); 2486 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { 2487 SkPaint paintCopy(*paint); 2488 float underlineWidth = length; 2489 // If length is > 0.0f, we already measured the text for the text alignment 2490 if (length <= 0.0f) { 2491 underlineWidth = paintCopy.measureText(text, bytesCount); 2492 } 2493 2494 float offsetX = 0; 2495 switch (paintCopy.getTextAlign()) { 2496 case SkPaint::kCenter_Align: 2497 offsetX = underlineWidth * 0.5f; 2498 break; 2499 case SkPaint::kRight_Align: 2500 offsetX = underlineWidth; 2501 break; 2502 default: 2503 break; 2504 } 2505 2506 if (CC_LIKELY(underlineWidth > 0.0f)) { 2507 const float textSize = paintCopy.getTextSize(); 2508 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f); 2509 2510 const float left = x - offsetX; 2511 float top = 0.0f; 2512 2513 int linesCount = 0; 2514 if (flags & SkPaint::kUnderlineText_Flag) linesCount++; 2515 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++; 2516 2517 const int pointsCount = 4 * linesCount; 2518 float points[pointsCount]; 2519 int currentPoint = 0; 2520 2521 if (flags & SkPaint::kUnderlineText_Flag) { 2522 top = y + textSize * kStdUnderline_Offset; 2523 points[currentPoint++] = left; 2524 points[currentPoint++] = top; 2525 points[currentPoint++] = left + underlineWidth; 2526 points[currentPoint++] = top; 2527 } 2528 2529 if (flags & SkPaint::kStrikeThruText_Flag) { 2530 top = y + textSize * kStdStrikeThru_Offset; 2531 points[currentPoint++] = left; 2532 points[currentPoint++] = top; 2533 points[currentPoint++] = left + underlineWidth; 2534 points[currentPoint++] = top; 2535 } 2536 2537 paintCopy.setStrokeWidth(strokeWidth); 2538 2539 drawLines(&points[0], pointsCount, &paintCopy); 2540 } 2541 } 2542} 2543 2544void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 2545 int color, SkXfermode::Mode mode, bool ignoreTransform) { 2546 // If a shader is set, preserve only the alpha 2547 if (mShader) { 2548 color |= 0x00ffffff; 2549 } 2550 2551 setupDraw(); 2552 setupDrawNoTexture(); 2553 setupDrawColor(color); 2554 setupDrawShader(); 2555 setupDrawColorFilter(); 2556 setupDrawBlending(mode); 2557 setupDrawProgram(); 2558 setupDrawModelView(left, top, right, bottom, ignoreTransform); 2559 setupDrawColorUniforms(); 2560 setupDrawShaderUniforms(ignoreTransform); 2561 setupDrawColorFilterUniforms(); 2562 setupDrawSimpleMesh(); 2563 2564 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 2565} 2566 2567void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 2568 Texture* texture, SkPaint* paint) { 2569 int alpha; 2570 SkXfermode::Mode mode; 2571 getAlphaAndMode(paint, &alpha, &mode); 2572 2573 texture->setWrap(GL_CLAMP_TO_EDGE, true); 2574 2575 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) { 2576 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 2577 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 2578 2579 texture->setFilter(GL_NEAREST, true); 2580 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id, 2581 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL, 2582 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true); 2583 } else { 2584 texture->setFilter(FILTER(paint), true); 2585 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, 2586 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, 2587 GL_TRIANGLE_STRIP, gMeshCount); 2588 } 2589} 2590 2591void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 2592 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { 2593 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, 2594 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); 2595} 2596 2597void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 2598 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, 2599 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 2600 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) { 2601 2602 setupDraw(); 2603 setupDrawWithTexture(); 2604 setupDrawColor(alpha, alpha, alpha, alpha); 2605 setupDrawColorFilter(); 2606 setupDrawBlending(blend, mode, swapSrcDst); 2607 setupDrawProgram(); 2608 if (!dirty) { 2609 setupDrawDirtyRegionsDisabled(); 2610 } 2611 if (!ignoreScale) { 2612 setupDrawModelView(left, top, right, bottom, ignoreTransform); 2613 } else { 2614 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform); 2615 } 2616 setupDrawPureColorUniforms(); 2617 setupDrawColorFilterUniforms(); 2618 setupDrawTexture(texture); 2619 setupDrawMesh(vertices, texCoords, vbo); 2620 2621 glDrawArrays(drawMode, 0, elementsCount); 2622 2623 finishDrawTexture(); 2624} 2625 2626void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, 2627 ProgramDescription& description, bool swapSrcDst) { 2628 blend = blend || mode != SkXfermode::kSrcOver_Mode; 2629 if (blend) { 2630 // These blend modes are not supported by OpenGL directly and have 2631 // to be implemented using shaders. Since the shader will perform 2632 // the blending, turn blending off here 2633 // If the blend mode cannot be implemented using shaders, fall 2634 // back to the default SrcOver blend mode instead 2635 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) { 2636 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) { 2637 description.framebufferMode = mode; 2638 description.swapSrcDst = swapSrcDst; 2639 2640 if (mCaches.blend) { 2641 glDisable(GL_BLEND); 2642 mCaches.blend = false; 2643 } 2644 2645 return; 2646 } else { 2647 mode = SkXfermode::kSrcOver_Mode; 2648 } 2649 } 2650 2651 if (!mCaches.blend) { 2652 glEnable(GL_BLEND); 2653 } 2654 2655 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; 2656 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; 2657 2658 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { 2659 glBlendFunc(sourceMode, destMode); 2660 mCaches.lastSrcMode = sourceMode; 2661 mCaches.lastDstMode = destMode; 2662 } 2663 } else if (mCaches.blend) { 2664 glDisable(GL_BLEND); 2665 } 2666 mCaches.blend = blend; 2667} 2668 2669bool OpenGLRenderer::useProgram(Program* program) { 2670 if (!program->isInUse()) { 2671 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); 2672 program->use(); 2673 mCaches.currentProgram = program; 2674 return false; 2675 } 2676 return true; 2677} 2678 2679void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 2680 TextureVertex* v = &mMeshVertices[0]; 2681 TextureVertex::setUV(v++, u1, v1); 2682 TextureVertex::setUV(v++, u2, v1); 2683 TextureVertex::setUV(v++, u1, v2); 2684 TextureVertex::setUV(v++, u2, v2); 2685} 2686 2687void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 2688 if (paint) { 2689 *mode = getXfermode(paint->getXfermode()); 2690 2691 // Skia draws using the color's alpha channel if < 255 2692 // Otherwise, it uses the paint's alpha 2693 int color = paint->getColor(); 2694 *alpha = (color >> 24) & 0xFF; 2695 if (*alpha == 255) { 2696 *alpha = paint->getAlpha(); 2697 } 2698 } else { 2699 *mode = SkXfermode::kSrcOver_Mode; 2700 *alpha = 255; 2701 } 2702} 2703 2704SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) { 2705 SkXfermode::Mode resultMode; 2706 if (!SkXfermode::AsMode(mode, &resultMode)) { 2707 resultMode = SkXfermode::kSrcOver_Mode; 2708 } 2709 return resultMode; 2710} 2711 2712}; // namespace uirenderer 2713}; // namespace android 2714