OpenGLRenderer.cpp revision 99ecdc480dd4f9b550b2a62ea39f77845a4fec49
161ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten/* 261ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * Copyright (C) 2010 The Android Open Source Project 361ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * 461ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * Licensed under the Apache License, Version 2.0 (the "License"); 561ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * you may not use this file except in compliance with the License. 661ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * You may obtain a copy of the License at 761ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * 861ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * http://www.apache.org/licenses/LICENSE-2.0 961ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * 1061ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * Unless required by applicable law or agreed to in writing, software 1161ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * distributed under the License is distributed on an "AS IS" BASIS, 1261ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1361ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * See the License for the specific language governing permissions and 1461ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * limitations under the License. 1561ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten */ 1661ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten 1761ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten#define LOG_TAG "OpenGLRenderer" 1861ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten 1961ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten#include <stdlib.h> 2061ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten#include <stdint.h> 21ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten#include <sys/types.h> 2261ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten 2361ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten#include <SkCanvas.h> 24ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten#include <SkTypeface.h> 25ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 26bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten#include <utils/Log.h> 27bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten#include <utils/StopWatch.h> 28bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten 29bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten#include <private/hwui/DrawGlInfo.h> 30ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 31ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten#include <ui/Rect.h> 32ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 3361ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten#include "OpenGLRenderer.h" 3461ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten#include "DisplayListRenderer.h" 35ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten#include "Vector.h" 3661ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten 3761ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kastennamespace android { 38ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kastennamespace uirenderer { 39ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 40ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten/////////////////////////////////////////////////////////////////////////////// 41ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten// Defines 42ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten/////////////////////////////////////////////////////////////////////////////// 43bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten 44bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten#define RAD_TO_DEG (180.0f / 3.14159265f) 45bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten#define MIN_ANGLE 0.001f 46bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten 47ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten// TODO: This should be set in properties 48ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH) 49ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 50ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten/////////////////////////////////////////////////////////////////////////////// 51ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten// Globals 5261ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten/////////////////////////////////////////////////////////////////////////////// 5361ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten 54ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten/** 5561ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten * Structure mapping Skia xfermodes to OpenGL blending factors. 5661ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten */ 57ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kastenstruct Blender { 58ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten SkXfermode::Mode mode; 59d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten GLenum src; 60ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten GLenum dst; 61ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten}; // struct Blender 62ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 63ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten// In this array, the index of each Blender equals the value of the first 64ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] 65bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kastenstatic const Blender gBlends[] = { 66bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, 67bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, 68bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, 69ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 70ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 71ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, 72ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 7361ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 7461ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 75ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 7661ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 7761ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } 78ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten}; 79ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 80ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten// This array contains the swapped version of each SkXfermode. For instance 81ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten// this array's SrcOver blending mode is actually DstOver. You can refer to 82ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten// createLayer() for more information on the purpose of this array. 83bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kastenstatic const Blender gBlendsSwap[] = { 84bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, 85bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE }, 86bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO }, 87ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, 88ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, 89ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA }, 90ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO }, 91ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, 9261ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, 9361ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, 94ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, 9561ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } 9661ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten}; 97ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 98ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kastenstatic const GLenum gTextureUnits[] = { 99d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten GL_TEXTURE0, 100ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten GL_TEXTURE1, 101ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten GL_TEXTURE2 102ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten}; 103ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 104ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten/////////////////////////////////////////////////////////////////////////////// 105bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten// Constructors/destructor 106bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten/////////////////////////////////////////////////////////////////////////////// 107bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten 108bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn KastenOpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) { 109ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten mShader = NULL; 110ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten mColorFilter = NULL; 111ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten mHasShadow = false; 112ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 11361ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); 11461ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten 115ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten mFirstSnapshot = new Snapshot; 11661ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten} 11761ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten 118ed46c29d6a09112dbbf584c82953f63289596fd6Glenn KastenOpenGLRenderer::~OpenGLRenderer() { 119ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten // The context has already been destroyed at this point, do not call 120ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten // GL APIs. All GL state should be kept in Caches.h 121ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten} 122ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 123ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten/////////////////////////////////////////////////////////////////////////////// 124ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten// Setup 125ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten/////////////////////////////////////////////////////////////////////////////// 126ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 127bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kastenvoid OpenGLRenderer::setViewport(int width, int height) { 128bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten glViewport(0, 0, width, height); 129bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); 130bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten 131ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten mWidth = width; 132ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten mHeight = height; 133ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 134ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten mFirstSnapshot->height = height; 135ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten mFirstSnapshot->viewport.set(0, 0, width, height); 136ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 13761ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten mDirtyClip = false; 13861ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten} 139ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten 14061ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kastenvoid OpenGLRenderer::prepare(bool opaque) { 14161ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque); 14261ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten} 14361ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten 14461ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kastenvoid OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) { 14561ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten mCaches.clearGarbage(); 14661ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten 14761ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten mSnapshot = new Snapshot(mFirstSnapshot, 14861ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); 14961ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten mSnapshot->fbo = getTargetFbo(); 15061ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten 151bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten mSaveCount = 1; 152bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten 153bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten glViewport(0, 0, mWidth, mHeight); 154d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten 155bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten glDisable(GL_DITHER); 156bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten 15761ac0ade16f84d877dfd8d0e984eb203d4a2901dGlenn Kasten glEnable(GL_SCISSOR_TEST); 158 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top); 159 mSnapshot->setClip(left, top, right, bottom); 160 161 if (!opaque) { 162 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 163 glClear(GL_COLOR_BUFFER_BIT); 164 } 165} 166 167void OpenGLRenderer::finish() { 168#if DEBUG_OPENGL 169 GLenum status = GL_NO_ERROR; 170 while ((status = glGetError()) != GL_NO_ERROR) { 171 LOGD("GL error from OpenGLRenderer: 0x%x", status); 172 switch (status) { 173 case GL_OUT_OF_MEMORY: 174 LOGE(" OpenGLRenderer is out of memory!"); 175 break; 176 } 177 } 178#endif 179#if DEBUG_MEMORY_USAGE 180 mCaches.dumpMemoryUsage(); 181#else 182 if (mCaches.getDebugLevel() & kDebugMemory) { 183 mCaches.dumpMemoryUsage(); 184 } 185#endif 186} 187 188void OpenGLRenderer::interrupt() { 189 if (mCaches.currentProgram) { 190 if (mCaches.currentProgram->isInUse()) { 191 mCaches.currentProgram->remove(); 192 mCaches.currentProgram = NULL; 193 } 194 } 195 mCaches.unbindMeshBuffer(); 196} 197 198void OpenGLRenderer::resume() { 199 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight()); 200 201 glEnable(GL_SCISSOR_TEST); 202 dirtyClip(); 203 204 glDisable(GL_DITHER); 205 206 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo()); 207 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 208 209 mCaches.blend = true; 210 glEnable(GL_BLEND); 211 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode); 212 glBlendEquation(GL_FUNC_ADD); 213} 214 215bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) { 216 interrupt(); 217 if (mDirtyClip) { 218 setScissorFromClip(); 219 } 220 221 Rect clip(*mSnapshot->clipRect); 222 clip.snapToPixelBoundaries(); 223 224#if RENDER_LAYERS_AS_REGIONS 225 // Since we don't know what the functor will draw, let's dirty 226 // tne entire clip region 227 if (hasLayer()) { 228 dirtyLayerUnchecked(clip, getRegion()); 229 } 230#endif 231 232 DrawGlInfo info; 233 info.clipLeft = clip.left; 234 info.clipTop = clip.top; 235 info.clipRight = clip.right; 236 info.clipBottom = clip.bottom; 237 info.isLayer = hasLayer(); 238 getSnapshot()->transform->copyTo(&info.transform[0]); 239 240 status_t result = (*functor)(0, &info); 241 242 if (result != 0) { 243 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom); 244 dirty.unionWith(localDirty); 245 } 246 247 resume(); 248 return result != 0; 249} 250 251/////////////////////////////////////////////////////////////////////////////// 252// State management 253/////////////////////////////////////////////////////////////////////////////// 254 255int OpenGLRenderer::getSaveCount() const { 256 return mSaveCount; 257} 258 259int OpenGLRenderer::save(int flags) { 260 return saveSnapshot(flags); 261} 262 263void OpenGLRenderer::restore() { 264 if (mSaveCount > 1) { 265 restoreSnapshot(); 266 } 267} 268 269void OpenGLRenderer::restoreToCount(int saveCount) { 270 if (saveCount < 1) saveCount = 1; 271 272 while (mSaveCount > saveCount) { 273 restoreSnapshot(); 274 } 275} 276 277int OpenGLRenderer::saveSnapshot(int flags) { 278 mSnapshot = new Snapshot(mSnapshot, flags); 279 return mSaveCount++; 280} 281 282bool OpenGLRenderer::restoreSnapshot() { 283 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; 284 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; 285 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; 286 287 sp<Snapshot> current = mSnapshot; 288 sp<Snapshot> previous = mSnapshot->previous; 289 290 if (restoreOrtho) { 291 Rect& r = previous->viewport; 292 glViewport(r.left, r.top, r.right, r.bottom); 293 mOrthoMatrix.load(current->orthoMatrix); 294 } 295 296 mSaveCount--; 297 mSnapshot = previous; 298 299 if (restoreClip) { 300 dirtyClip(); 301 } 302 303 if (restoreLayer) { 304 composeLayer(current, previous); 305 } 306 307 return restoreClip; 308} 309 310/////////////////////////////////////////////////////////////////////////////// 311// Layers 312/////////////////////////////////////////////////////////////////////////////// 313 314int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, 315 SkPaint* p, int flags) { 316 const GLuint previousFbo = mSnapshot->fbo; 317 const int count = saveSnapshot(flags); 318 319 if (!mSnapshot->isIgnored()) { 320 int alpha = 255; 321 SkXfermode::Mode mode; 322 323 if (p) { 324 alpha = p->getAlpha(); 325 if (!mCaches.extensions.hasFramebufferFetch()) { 326 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 327 if (!isMode) { 328 // Assume SRC_OVER 329 mode = SkXfermode::kSrcOver_Mode; 330 } 331 } else { 332 mode = getXfermode(p->getXfermode()); 333 } 334 } else { 335 mode = SkXfermode::kSrcOver_Mode; 336 } 337 338 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo); 339 } 340 341 return count; 342} 343 344int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, 345 int alpha, int flags) { 346 if (alpha >= 255 - ALPHA_THRESHOLD) { 347 return saveLayer(left, top, right, bottom, NULL, flags); 348 } else { 349 SkPaint paint; 350 paint.setAlpha(alpha); 351 return saveLayer(left, top, right, bottom, &paint, flags); 352 } 353} 354 355/** 356 * Layers are viewed by Skia are slightly different than layers in image editing 357 * programs (for instance.) When a layer is created, previously created layers 358 * and the frame buffer still receive every drawing command. For instance, if a 359 * layer is created and a shape intersecting the bounds of the layers and the 360 * framebuffer is draw, the shape will be drawn on both (unless the layer was 361 * created with the SkCanvas::kClipToLayer_SaveFlag flag.) 362 * 363 * A way to implement layers is to create an FBO for each layer, backed by an RGBA 364 * texture. Unfortunately, this is inefficient as it requires every primitive to 365 * be drawn n + 1 times, where n is the number of active layers. In practice this 366 * means, for every primitive: 367 * - Switch active frame buffer 368 * - Change viewport, clip and projection matrix 369 * - Issue the drawing 370 * 371 * Switching rendering target n + 1 times per drawn primitive is extremely costly. 372 * To avoid this, layers are implemented in a different way here, at least in the 373 * general case. FBOs are used, as an optimization, when the "clip to layer" flag 374 * is set. When this flag is set we can redirect all drawing operations into a 375 * single FBO. 376 * 377 * This implementation relies on the frame buffer being at least RGBA 8888. When 378 * a layer is created, only a texture is created, not an FBO. The content of the 379 * frame buffer contained within the layer's bounds is copied into this texture 380 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame 381 * buffer and drawing continues as normal. This technique therefore treats the 382 * frame buffer as a scratch buffer for the layers. 383 * 384 * To compose the layers back onto the frame buffer, each layer texture 385 * (containing the original frame buffer data) is drawn as a simple quad over 386 * the frame buffer. The trick is that the quad is set as the composition 387 * destination in the blending equation, and the frame buffer becomes the source 388 * of the composition. 389 * 390 * Drawing layers with an alpha value requires an extra step before composition. 391 * An empty quad is drawn over the layer's region in the frame buffer. This quad 392 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the 393 * quad is used to multiply the colors in the frame buffer. This is achieved by 394 * changing the GL blend functions for the GL_FUNC_ADD blend equation to 395 * GL_ZERO, GL_SRC_ALPHA. 396 * 397 * Because glCopyTexImage2D() can be slow, an alternative implementation might 398 * be use to draw a single clipped layer. The implementation described above 399 * is correct in every case. 400 * 401 * (1) The frame buffer is actually not cleared right away. To allow the GPU 402 * to potentially optimize series of calls to glCopyTexImage2D, the frame 403 * buffer is left untouched until the first drawing operation. Only when 404 * something actually gets drawn are the layers regions cleared. 405 */ 406bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, 407 float right, float bottom, int alpha, SkXfermode::Mode mode, 408 int flags, GLuint previousFbo) { 409 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top); 410 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); 411 412 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag; 413 414 // Window coordinates of the layer 415 Rect bounds(left, top, right, bottom); 416 if (!fboLayer) { 417 mSnapshot->transform->mapRect(bounds); 418 419 // Layers only make sense if they are in the framebuffer's bounds 420 if (bounds.intersect(*snapshot->clipRect)) { 421 // We cannot work with sub-pixels in this case 422 bounds.snapToPixelBoundaries(); 423 424 // When the layer is not an FBO, we may use glCopyTexImage so we 425 // need to make sure the layer does not extend outside the bounds 426 // of the framebuffer 427 if (!bounds.intersect(snapshot->previous->viewport)) { 428 bounds.setEmpty(); 429 } 430 } else { 431 bounds.setEmpty(); 432 } 433 } 434 435 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize || 436 bounds.getHeight() > mCaches.maxTextureSize) { 437 snapshot->empty = fboLayer; 438 } else { 439 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer); 440 } 441 442 // Bail out if we won't draw in this snapshot 443 if (snapshot->invisible || snapshot->empty) { 444 return false; 445 } 446 447 glActiveTexture(gTextureUnits[0]); 448 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight()); 449 if (!layer) { 450 return false; 451 } 452 453 layer->mode = mode; 454 layer->alpha = alpha; 455 layer->layer.set(bounds); 456 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height), 457 bounds.getWidth() / float(layer->width), 0.0f); 458 layer->colorFilter = mColorFilter; 459 460 // Save the layer in the snapshot 461 snapshot->flags |= Snapshot::kFlagIsLayer; 462 snapshot->layer = layer; 463 464 if (fboLayer) { 465 return createFboLayer(layer, bounds, snapshot, previousFbo); 466 } else { 467 // Copy the framebuffer into the layer 468 glBindTexture(GL_TEXTURE_2D, layer->texture); 469 if (!bounds.isEmpty()) { 470 if (layer->empty) { 471 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, 472 snapshot->height - bounds.bottom, layer->width, layer->height, 0); 473 layer->empty = false; 474 } else { 475 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, 476 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight()); 477 } 478 479 // Clear the framebuffer where the layer will draw 480 glScissor(bounds.left, mSnapshot->height - bounds.bottom, 481 bounds.getWidth(), bounds.getHeight()); 482 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 483 glClear(GL_COLOR_BUFFER_BIT); 484 485 dirtyClip(); 486 } 487 } 488 489 return true; 490} 491 492bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot, 493 GLuint previousFbo) { 494 layer->fbo = mCaches.fboCache.get(); 495 496#if RENDER_LAYERS_AS_REGIONS 497 snapshot->region = &snapshot->layer->region; 498 snapshot->flags |= Snapshot::kFlagFboTarget; 499#endif 500 501 Rect clip(bounds); 502 snapshot->transform->mapRect(clip); 503 clip.intersect(*snapshot->clipRect); 504 clip.snapToPixelBoundaries(); 505 clip.intersect(snapshot->previous->viewport); 506 507 mat4 inverse; 508 inverse.loadInverse(*mSnapshot->transform); 509 510 inverse.mapRect(clip); 511 clip.snapToPixelBoundaries(); 512 clip.intersect(bounds); 513 clip.translate(-bounds.left, -bounds.top); 514 515 snapshot->flags |= Snapshot::kFlagIsFboLayer; 516 snapshot->fbo = layer->fbo; 517 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); 518 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom); 519 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); 520 snapshot->height = bounds.getHeight(); 521 snapshot->flags |= Snapshot::kFlagDirtyOrtho; 522 snapshot->orthoMatrix.load(mOrthoMatrix); 523 524 // Bind texture to FBO 525 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); 526 glBindTexture(GL_TEXTURE_2D, layer->texture); 527 528 // Initialize the texture if needed 529 if (layer->empty) { 530 layer->empty = false; 531 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0, 532 GL_RGBA, GL_UNSIGNED_BYTE, NULL); 533 } 534 535 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 536 layer->texture, 0); 537 538#if DEBUG_LAYERS_AS_REGIONS 539 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); 540 if (status != GL_FRAMEBUFFER_COMPLETE) { 541 LOGE("Framebuffer incomplete (GL error code 0x%x)", status); 542 543 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); 544 glDeleteTextures(1, &layer->texture); 545 mCaches.fboCache.put(layer->fbo); 546 547 delete layer; 548 549 return false; 550 } 551#endif 552 553 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering 554 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f, 555 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f); 556 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 557 glClear(GL_COLOR_BUFFER_BIT); 558 559 dirtyClip(); 560 561 // Change the ortho projection 562 glViewport(0, 0, bounds.getWidth(), bounds.getHeight()); 563 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f); 564 565 return true; 566} 567 568/** 569 * Read the documentation of createLayer() before doing anything in this method. 570 */ 571void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { 572 if (!current->layer) { 573 LOGE("Attempting to compose a layer that does not exist"); 574 return; 575 } 576 577 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer; 578 579 if (fboLayer) { 580 // Unbind current FBO and restore previous one 581 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 582 } 583 584 Layer* layer = current->layer; 585 const Rect& rect = layer->layer; 586 587 if (!fboLayer && layer->alpha < 255) { 588 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, 589 layer->alpha << 24, SkXfermode::kDstIn_Mode, true); 590 // Required below, composeLayerRect() will divide by 255 591 layer->alpha = 255; 592 } 593 594 mCaches.unbindMeshBuffer(); 595 596 glActiveTexture(gTextureUnits[0]); 597 598 // When the layer is stored in an FBO, we can save a bit of fillrate by 599 // drawing only the dirty region 600 if (fboLayer) { 601 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform); 602 if (layer->colorFilter) { 603 setupColorFilter(layer->colorFilter); 604 } 605 composeLayerRegion(layer, rect); 606 if (layer->colorFilter) { 607 resetColorFilter(); 608 } 609 } else { 610 if (!rect.isEmpty()) { 611 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom); 612 composeLayerRect(layer, rect, true); 613 } 614 } 615 616 if (fboLayer) { 617 // Detach the texture from the FBO 618 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo); 619 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); 620 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); 621 622 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed 623 mCaches.fboCache.put(current->fbo); 624 } 625 626 dirtyClip(); 627 628 // Failing to add the layer to the cache should happen only if the layer is too large 629 if (!mCaches.layerCache.put(layer)) { 630 LAYER_LOGD("Deleting layer"); 631 glDeleteTextures(1, &layer->texture); 632 delete layer; 633 } 634} 635 636void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) { 637 float alpha = layer->alpha / 255.0f; 638 639 setupDraw(); 640 if (layer->renderTarget == GL_TEXTURE_2D) { 641 setupDrawWithTexture(); 642 } else { 643 setupDrawWithExternalTexture(); 644 } 645 setupDrawTextureTransform(); 646 setupDrawColor(alpha, alpha, alpha, alpha); 647 setupDrawColorFilter(); 648 setupDrawBlending(layer->blend, layer->mode); 649 setupDrawProgram(); 650 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom); 651 setupDrawPureColorUniforms(); 652 setupDrawColorFilterUniforms(); 653 if (layer->renderTarget == GL_TEXTURE_2D) { 654 setupDrawTexture(layer->texture); 655 } else { 656 setupDrawExternalTexture(layer->texture); 657 } 658 setupDrawTextureTransformUniforms(layer->texTransform); 659 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]); 660 661 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 662 663 finishDrawTexture(); 664} 665 666void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) { 667 if (!layer->isTextureLayer) { 668 const Rect& texCoords = layer->texCoords; 669 resetDrawTextureTexCoords(texCoords.left, texCoords.top, 670 texCoords.right, texCoords.bottom); 671 672 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture, 673 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0], 674 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap); 675 676 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 677 } else { 678 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f); 679 drawTextureLayer(layer, rect); 680 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 681 } 682} 683 684void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) { 685#if RENDER_LAYERS_AS_REGIONS 686 if (layer->region.isRect()) { 687 layer->setRegionAsRect(); 688 689 composeLayerRect(layer, layer->regionRect); 690 691 layer->region.clear(); 692 return; 693 } 694 695 if (!layer->region.isEmpty()) { 696 size_t count; 697 const android::Rect* rects = layer->region.getArray(&count); 698 699 const float alpha = layer->alpha / 255.0f; 700 const float texX = 1.0f / float(layer->width); 701 const float texY = 1.0f / float(layer->height); 702 const float height = rect.getHeight(); 703 704 TextureVertex* mesh = mCaches.getRegionMesh(); 705 GLsizei numQuads = 0; 706 707 setupDraw(); 708 setupDrawWithTexture(); 709 setupDrawColor(alpha, alpha, alpha, alpha); 710 setupDrawColorFilter(); 711 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false); 712 setupDrawProgram(); 713 setupDrawDirtyRegionsDisabled(); 714 setupDrawPureColorUniforms(); 715 setupDrawColorFilterUniforms(); 716 setupDrawTexture(layer->texture); 717 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom); 718 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]); 719 720 for (size_t i = 0; i < count; i++) { 721 const android::Rect* r = &rects[i]; 722 723 const float u1 = r->left * texX; 724 const float v1 = (height - r->top) * texY; 725 const float u2 = r->right * texX; 726 const float v2 = (height - r->bottom) * texY; 727 728 // TODO: Reject quads outside of the clip 729 TextureVertex::set(mesh++, r->left, r->top, u1, v1); 730 TextureVertex::set(mesh++, r->right, r->top, u2, v1); 731 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); 732 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); 733 734 numQuads++; 735 736 if (numQuads >= REGION_MESH_QUAD_COUNT) { 737 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); 738 numQuads = 0; 739 mesh = mCaches.getRegionMesh(); 740 } 741 } 742 743 if (numQuads > 0) { 744 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); 745 } 746 747 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 748 finishDrawTexture(); 749 750#if DEBUG_LAYERS_AS_REGIONS 751 drawRegionRects(layer->region); 752#endif 753 754 layer->region.clear(); 755 } 756#else 757 composeLayerRect(layer, rect); 758#endif 759} 760 761void OpenGLRenderer::drawRegionRects(const Region& region) { 762#if DEBUG_LAYERS_AS_REGIONS 763 size_t count; 764 const android::Rect* rects = region.getArray(&count); 765 766 uint32_t colors[] = { 767 0x7fff0000, 0x7f00ff00, 768 0x7f0000ff, 0x7fff00ff, 769 }; 770 771 int offset = 0; 772 int32_t top = rects[0].top; 773 774 for (size_t i = 0; i < count; i++) { 775 if (top != rects[i].top) { 776 offset ^= 0x2; 777 top = rects[i].top; 778 } 779 780 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom); 781 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)], 782 SkXfermode::kSrcOver_Mode); 783 } 784#endif 785} 786 787void OpenGLRenderer::dirtyLayer(const float left, const float top, 788 const float right, const float bottom, const mat4 transform) { 789#if RENDER_LAYERS_AS_REGIONS 790 if (hasLayer()) { 791 Rect bounds(left, top, right, bottom); 792 transform.mapRect(bounds); 793 dirtyLayerUnchecked(bounds, getRegion()); 794 } 795#endif 796} 797 798void OpenGLRenderer::dirtyLayer(const float left, const float top, 799 const float right, const float bottom) { 800#if RENDER_LAYERS_AS_REGIONS 801 if (hasLayer()) { 802 Rect bounds(left, top, right, bottom); 803 dirtyLayerUnchecked(bounds, getRegion()); 804 } 805#endif 806} 807 808void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) { 809#if RENDER_LAYERS_AS_REGIONS 810 if (bounds.intersect(*mSnapshot->clipRect)) { 811 bounds.snapToPixelBoundaries(); 812 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); 813 if (!dirty.isEmpty()) { 814 region->orSelf(dirty); 815 } 816 } 817#endif 818} 819 820/////////////////////////////////////////////////////////////////////////////// 821// Transforms 822/////////////////////////////////////////////////////////////////////////////// 823 824void OpenGLRenderer::translate(float dx, float dy) { 825 mSnapshot->transform->translate(dx, dy, 0.0f); 826} 827 828void OpenGLRenderer::rotate(float degrees) { 829 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); 830} 831 832void OpenGLRenderer::scale(float sx, float sy) { 833 mSnapshot->transform->scale(sx, sy, 1.0f); 834} 835 836void OpenGLRenderer::skew(float sx, float sy) { 837 mSnapshot->transform->skew(sx, sy); 838} 839 840void OpenGLRenderer::setMatrix(SkMatrix* matrix) { 841 mSnapshot->transform->load(*matrix); 842} 843 844const float* OpenGLRenderer::getMatrix() const { 845 if (mSnapshot->fbo != 0) { 846 return &mSnapshot->transform->data[0]; 847 } 848 return &mIdentity.data[0]; 849} 850 851void OpenGLRenderer::getMatrix(SkMatrix* matrix) { 852 mSnapshot->transform->copyTo(*matrix); 853} 854 855void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { 856 SkMatrix transform; 857 mSnapshot->transform->copyTo(transform); 858 transform.preConcat(*matrix); 859 mSnapshot->transform->load(transform); 860} 861 862/////////////////////////////////////////////////////////////////////////////// 863// Clipping 864/////////////////////////////////////////////////////////////////////////////// 865 866void OpenGLRenderer::setScissorFromClip() { 867 Rect clip(*mSnapshot->clipRect); 868 clip.snapToPixelBoundaries(); 869 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); 870 mDirtyClip = false; 871} 872 873const Rect& OpenGLRenderer::getClipBounds() { 874 return mSnapshot->getLocalClip(); 875} 876 877bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { 878 if (mSnapshot->isIgnored()) { 879 return true; 880 } 881 882 Rect r(left, top, right, bottom); 883 mSnapshot->transform->mapRect(r); 884 r.snapToPixelBoundaries(); 885 886 Rect clipRect(*mSnapshot->clipRect); 887 clipRect.snapToPixelBoundaries(); 888 889 return !clipRect.intersects(r); 890} 891 892bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { 893 bool clipped = mSnapshot->clip(left, top, right, bottom, op); 894 if (clipped) { 895 dirtyClip(); 896 } 897 return !mSnapshot->clipRect->isEmpty(); 898} 899 900/////////////////////////////////////////////////////////////////////////////// 901// Drawing commands 902/////////////////////////////////////////////////////////////////////////////// 903 904void OpenGLRenderer::setupDraw() { 905 if (mDirtyClip) { 906 setScissorFromClip(); 907 } 908 mDescription.reset(); 909 mSetShaderColor = false; 910 mColorSet = false; 911 mColorA = mColorR = mColorG = mColorB = 0.0f; 912 mTextureUnit = 0; 913 mTrackDirtyRegions = true; 914 mTexCoordsSlot = -1; 915} 916 917void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) { 918 mDescription.hasTexture = true; 919 mDescription.hasAlpha8Texture = isAlpha8; 920} 921 922void OpenGLRenderer::setupDrawWithExternalTexture() { 923 mDescription.hasExternalTexture = true; 924} 925 926void OpenGLRenderer::setupDrawAALine() { 927 mDescription.isAA = true; 928} 929 930void OpenGLRenderer::setupDrawPoint(float pointSize) { 931 mDescription.isPoint = true; 932 mDescription.pointSize = pointSize; 933} 934 935void OpenGLRenderer::setupDrawColor(int color) { 936 setupDrawColor(color, (color >> 24) & 0xFF); 937} 938 939void OpenGLRenderer::setupDrawColor(int color, int alpha) { 940 mColorA = alpha / 255.0f; 941 // Second divide of a by 255 is an optimization, allowing us to simply multiply 942 // the rgb values by a instead of also dividing by 255 943 const float a = mColorA / 255.0f; 944 mColorR = a * ((color >> 16) & 0xFF); 945 mColorG = a * ((color >> 8) & 0xFF); 946 mColorB = a * ((color ) & 0xFF); 947 mColorSet = true; 948 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA); 949} 950 951void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) { 952 mColorA = alpha / 255.0f; 953 // Double-divide of a by 255 is an optimization, allowing us to simply multiply 954 // the rgb values by a instead of also dividing by 255 955 const float a = mColorA / 255.0f; 956 mColorR = a * ((color >> 16) & 0xFF); 957 mColorG = a * ((color >> 8) & 0xFF); 958 mColorB = a * ((color ) & 0xFF); 959 mColorSet = true; 960 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA); 961} 962 963void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) { 964 mColorA = a; 965 mColorR = r; 966 mColorG = g; 967 mColorB = b; 968 mColorSet = true; 969 mSetShaderColor = mDescription.setColor(r, g, b, a); 970} 971 972void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) { 973 mColorA = a; 974 mColorR = r; 975 mColorG = g; 976 mColorB = b; 977 mColorSet = true; 978 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a); 979} 980 981void OpenGLRenderer::setupDrawShader() { 982 if (mShader) { 983 mShader->describe(mDescription, mCaches.extensions); 984 } 985} 986 987void OpenGLRenderer::setupDrawColorFilter() { 988 if (mColorFilter) { 989 mColorFilter->describe(mDescription, mCaches.extensions); 990 } 991} 992 993void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) { 994 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, 995 mDescription, swapSrcDst); 996} 997 998void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) { 999 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, 1000 mDescription, swapSrcDst); 1001} 1002 1003void OpenGLRenderer::setupDrawProgram() { 1004 useProgram(mCaches.programCache.get(mDescription)); 1005} 1006 1007void OpenGLRenderer::setupDrawDirtyRegionsDisabled() { 1008 mTrackDirtyRegions = false; 1009} 1010 1011void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom, 1012 bool ignoreTransform) { 1013 mModelView.loadTranslate(left, top, 0.0f); 1014 if (!ignoreTransform) { 1015 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1016 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1017 } else { 1018 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); 1019 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom); 1020 } 1021} 1022 1023void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) { 1024 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset); 1025} 1026 1027void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom, 1028 bool ignoreTransform, bool ignoreModelView) { 1029 if (!ignoreModelView) { 1030 mModelView.loadTranslate(left, top, 0.0f); 1031 mModelView.scale(right - left, bottom - top, 1.0f); 1032 } else { 1033 mModelView.loadIdentity(); 1034 } 1035 bool dirty = right - left > 0.0f && bottom - top > 0.0f; 1036 if (!ignoreTransform) { 1037 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); 1038 if (mTrackDirtyRegions && dirty) { 1039 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1040 } 1041 } else { 1042 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); 1043 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom); 1044 } 1045} 1046 1047void OpenGLRenderer::setupDrawPointUniforms() { 1048 int slot = mCaches.currentProgram->getUniform("pointSize"); 1049 glUniform1f(slot, mDescription.pointSize); 1050} 1051 1052void OpenGLRenderer::setupDrawColorUniforms() { 1053 if (mColorSet || (mShader && mSetShaderColor)) { 1054 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 1055 } 1056} 1057 1058void OpenGLRenderer::setupDrawPureColorUniforms() { 1059 if (mSetShaderColor) { 1060 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); 1061 } 1062} 1063 1064void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) { 1065 if (mShader) { 1066 if (ignoreTransform) { 1067 mModelView.loadInverse(*mSnapshot->transform); 1068 } 1069 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit); 1070 } 1071} 1072 1073void OpenGLRenderer::setupDrawShaderIdentityUniforms() { 1074 if (mShader) { 1075 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit); 1076 } 1077} 1078 1079void OpenGLRenderer::setupDrawColorFilterUniforms() { 1080 if (mColorFilter) { 1081 mColorFilter->setupProgram(mCaches.currentProgram); 1082 } 1083} 1084 1085void OpenGLRenderer::setupDrawSimpleMesh() { 1086 mCaches.bindMeshBuffer(); 1087 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1088 gMeshStride, 0); 1089} 1090 1091void OpenGLRenderer::setupDrawTexture(GLuint texture) { 1092 bindTexture(texture); 1093 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++); 1094 1095 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); 1096 glEnableVertexAttribArray(mTexCoordsSlot); 1097} 1098 1099void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) { 1100 bindExternalTexture(texture); 1101 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++); 1102 1103 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); 1104 glEnableVertexAttribArray(mTexCoordsSlot); 1105} 1106 1107void OpenGLRenderer::setupDrawTextureTransform() { 1108 mDescription.hasTextureTransform = true; 1109} 1110 1111void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) { 1112 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1, 1113 GL_FALSE, &transform.data[0]); 1114} 1115 1116void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { 1117 if (!vertices) { 1118 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); 1119 } else { 1120 mCaches.unbindMeshBuffer(); 1121 } 1122 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1123 gMeshStride, vertices); 1124 if (mTexCoordsSlot >= 0) { 1125 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); 1126 } 1127} 1128 1129void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) { 1130 mCaches.unbindMeshBuffer(); 1131 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1132 gVertexStride, vertices); 1133} 1134 1135/** 1136 * Sets up the shader to draw an AA line. We draw AA lines with quads, where there is an 1137 * outer boundary that fades out to 0. The variables set in the shader define the proportion of 1138 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength 1139 * attributes (one per vertex) are values from zero to one that tells the fragment 1140 * shader where the fragment is in relation to the line width/length overall; these values are 1141 * then used to compute the proper color, based on whether the fragment lies in the fading AA 1142 * region of the line. 1143 * Note that we only pass down the width values in this setup function. The length coordinates 1144 * are set up for each individual segment. 1145 */ 1146void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords, 1147 GLvoid* lengthCoords, float boundaryWidthProportion) { 1148 mCaches.unbindMeshBuffer(); 1149 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, 1150 gAAVertexStride, vertices); 1151 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth"); 1152 glEnableVertexAttribArray(widthSlot); 1153 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords); 1154 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength"); 1155 glEnableVertexAttribArray(lengthSlot); 1156 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords); 1157 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth"); 1158 glUniform1f(boundaryWidthSlot, boundaryWidthProportion); 1159 // Setting the inverse value saves computations per-fragment in the shader 1160 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth"); 1161 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion)); 1162} 1163 1164void OpenGLRenderer::finishDrawTexture() { 1165 glDisableVertexAttribArray(mTexCoordsSlot); 1166} 1167 1168/////////////////////////////////////////////////////////////////////////////// 1169// Drawing 1170/////////////////////////////////////////////////////////////////////////////// 1171 1172bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height, 1173 Rect& dirty, uint32_t level) { 1174 if (quickReject(0.0f, 0.0f, width, height)) { 1175 return false; 1176 } 1177 1178 // All the usual checks and setup operations (quickReject, setupDraw, etc.) 1179 // will be performed by the display list itself 1180 if (displayList) { 1181 return displayList->replay(*this, dirty, level); 1182 } 1183 1184 return false; 1185} 1186 1187void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) { 1188 int alpha; 1189 SkXfermode::Mode mode; 1190 getAlphaAndMode(paint, &alpha, &mode); 1191 1192 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 1193 1194 float x = left; 1195 float y = top; 1196 1197 bool ignoreTransform = false; 1198 if (mSnapshot->transform->isPureTranslate()) { 1199 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 1200 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 1201 ignoreTransform = true; 1202 } 1203 1204 setupDraw(); 1205 setupDrawWithTexture(true); 1206 if (paint) { 1207 setupDrawAlpha8Color(paint->getColor(), alpha); 1208 } 1209 setupDrawColorFilter(); 1210 setupDrawShader(); 1211 setupDrawBlending(true, mode); 1212 setupDrawProgram(); 1213 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform); 1214 setupDrawTexture(texture->id); 1215 setupDrawPureColorUniforms(); 1216 setupDrawColorFilterUniforms(); 1217 setupDrawShaderUniforms(); 1218 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 1219 1220 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1221 1222 finishDrawTexture(); 1223} 1224 1225void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { 1226 const float right = left + bitmap->width(); 1227 const float bottom = top + bitmap->height(); 1228 1229 if (quickReject(left, top, right, bottom)) { 1230 return; 1231 } 1232 1233 glActiveTexture(gTextureUnits[0]); 1234 Texture* texture = mCaches.textureCache.get(bitmap); 1235 if (!texture) return; 1236 const AutoTexture autoCleanup(texture); 1237 1238 if (bitmap->getConfig() == SkBitmap::kA8_Config) { 1239 drawAlphaBitmap(texture, left, top, paint); 1240 } else { 1241 drawTextureRect(left, top, right, bottom, texture, paint); 1242 } 1243} 1244 1245void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { 1246 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); 1247 const mat4 transform(*matrix); 1248 transform.mapRect(r); 1249 1250 if (quickReject(r.left, r.top, r.right, r.bottom)) { 1251 return; 1252 } 1253 1254 glActiveTexture(gTextureUnits[0]); 1255 Texture* texture = mCaches.textureCache.get(bitmap); 1256 if (!texture) return; 1257 const AutoTexture autoCleanup(texture); 1258 1259 // This could be done in a cheaper way, all we need is pass the matrix 1260 // to the vertex shader. The save/restore is a bit overkill. 1261 save(SkCanvas::kMatrix_SaveFlag); 1262 concatMatrix(matrix); 1263 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint); 1264 restore(); 1265} 1266 1267void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight, 1268 float* vertices, int* colors, SkPaint* paint) { 1269 // TODO: Do a quickReject 1270 if (!vertices || mSnapshot->isIgnored()) { 1271 return; 1272 } 1273 1274 glActiveTexture(gTextureUnits[0]); 1275 Texture* texture = mCaches.textureCache.get(bitmap); 1276 if (!texture) return; 1277 const AutoTexture autoCleanup(texture); 1278 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 1279 1280 int alpha; 1281 SkXfermode::Mode mode; 1282 getAlphaAndMode(paint, &alpha, &mode); 1283 1284 const uint32_t count = meshWidth * meshHeight * 6; 1285 1286 float left = FLT_MAX; 1287 float top = FLT_MAX; 1288 float right = FLT_MIN; 1289 float bottom = FLT_MIN; 1290 1291#if RENDER_LAYERS_AS_REGIONS 1292 bool hasActiveLayer = hasLayer(); 1293#else 1294 bool hasActiveLayer = false; 1295#endif 1296 1297 // TODO: Support the colors array 1298 TextureVertex mesh[count]; 1299 TextureVertex* vertex = mesh; 1300 for (int32_t y = 0; y < meshHeight; y++) { 1301 for (int32_t x = 0; x < meshWidth; x++) { 1302 uint32_t i = (y * (meshWidth + 1) + x) * 2; 1303 1304 float u1 = float(x) / meshWidth; 1305 float u2 = float(x + 1) / meshWidth; 1306 float v1 = float(y) / meshHeight; 1307 float v2 = float(y + 1) / meshHeight; 1308 1309 int ax = i + (meshWidth + 1) * 2; 1310 int ay = ax + 1; 1311 int bx = i; 1312 int by = bx + 1; 1313 int cx = i + 2; 1314 int cy = cx + 1; 1315 int dx = i + (meshWidth + 1) * 2 + 2; 1316 int dy = dx + 1; 1317 1318 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); 1319 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1); 1320 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); 1321 1322 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); 1323 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); 1324 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2); 1325 1326#if RENDER_LAYERS_AS_REGIONS 1327 if (hasActiveLayer) { 1328 // TODO: This could be optimized to avoid unnecessary ops 1329 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx]))); 1330 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy]))); 1331 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx]))); 1332 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy]))); 1333 } 1334#endif 1335 } 1336 } 1337 1338#if RENDER_LAYERS_AS_REGIONS 1339 if (hasActiveLayer) { 1340 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1341 } 1342#endif 1343 1344 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f, 1345 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0], 1346 GL_TRIANGLES, count, false, false, 0, false, false); 1347} 1348 1349void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, 1350 float srcLeft, float srcTop, float srcRight, float srcBottom, 1351 float dstLeft, float dstTop, float dstRight, float dstBottom, 1352 SkPaint* paint) { 1353 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { 1354 return; 1355 } 1356 1357 glActiveTexture(gTextureUnits[0]); 1358 Texture* texture = mCaches.textureCache.get(bitmap); 1359 if (!texture) return; 1360 const AutoTexture autoCleanup(texture); 1361 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 1362 1363 const float width = texture->width; 1364 const float height = texture->height; 1365 1366 const float u1 = srcLeft / width; 1367 const float v1 = srcTop / height; 1368 const float u2 = srcRight / width; 1369 const float v2 = srcBottom / height; 1370 1371 mCaches.unbindMeshBuffer(); 1372 resetDrawTextureTexCoords(u1, v1, u2, v2); 1373 1374 int alpha; 1375 SkXfermode::Mode mode; 1376 getAlphaAndMode(paint, &alpha, &mode); 1377 1378 if (mSnapshot->transform->isPureTranslate()) { 1379 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f); 1380 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f); 1381 1382 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop), 1383 texture->id, alpha / 255.0f, mode, texture->blend, 1384 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 1385 GL_TRIANGLE_STRIP, gMeshCount, false, true); 1386 } else { 1387 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f, 1388 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], 1389 GL_TRIANGLE_STRIP, gMeshCount); 1390 } 1391 1392 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 1393} 1394 1395void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 1396 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, 1397 float left, float top, float right, float bottom, SkPaint* paint) { 1398 if (quickReject(left, top, right, bottom)) { 1399 return; 1400 } 1401 1402 glActiveTexture(gTextureUnits[0]); 1403 Texture* texture = mCaches.textureCache.get(bitmap); 1404 if (!texture) return; 1405 const AutoTexture autoCleanup(texture); 1406 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 1407 1408 int alpha; 1409 SkXfermode::Mode mode; 1410 getAlphaAndMode(paint, &alpha, &mode); 1411 1412 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), 1413 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); 1414 1415 if (mesh && mesh->verticesCount > 0) { 1416 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 1417#if RENDER_LAYERS_AS_REGIONS 1418 // Mark the current layer dirty where we are going to draw the patch 1419 if (hasLayer() && mesh->hasEmptyQuads) { 1420 const float offsetX = left + mSnapshot->transform->getTranslateX(); 1421 const float offsetY = top + mSnapshot->transform->getTranslateY(); 1422 const size_t count = mesh->quads.size(); 1423 for (size_t i = 0; i < count; i++) { 1424 const Rect& bounds = mesh->quads.itemAt(i); 1425 if (pureTranslate) { 1426 const float x = (int) floorf(bounds.left + offsetX + 0.5f); 1427 const float y = (int) floorf(bounds.top + offsetY + 0.5f); 1428 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight()); 1429 } else { 1430 dirtyLayer(left + bounds.left, top + bounds.top, 1431 left + bounds.right, top + bounds.bottom, *mSnapshot->transform); 1432 } 1433 } 1434 } 1435#endif 1436 1437 if (pureTranslate) { 1438 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 1439 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 1440 1441 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f, 1442 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 1443 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer, 1444 true, !mesh->hasEmptyQuads); 1445 } else { 1446 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, 1447 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 1448 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer, 1449 true, !mesh->hasEmptyQuads); 1450 } 1451 } 1452} 1453 1454/** 1455 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization 1456 * rules for those lines produces some unexpected results, and may vary between hardware devices. 1457 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the 1458 * corners of the quads on either side of each line endpoint, separated by the strokeWidth 1459 * of the line. Hairlines are more involved because we need to account for transform scaling 1460 * to end up with a one-pixel-wide line in screen space.. 1461 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader 1462 * in combination with values that we calculate and pass down in this method. The basic approach 1463 * is that the quad we create contains both the core line area plus a bounding area in which 1464 * the translucent/AA pixels are drawn. The values we calculate tell the shader what 1465 * proportion of the width and the length of a given segment is represented by the boundary 1466 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad. 1467 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel 1468 * on the inside). This ends up giving the result we want, with pixels that are completely 1469 * 'inside' the line area being filled opaquely and the other pixels being filled according to 1470 * how far into the boundary region they are, which is determined by shader interpolation. 1471 */ 1472void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { 1473 if (mSnapshot->isIgnored()) return; 1474 1475 const bool isAA = paint->isAntiAlias(); 1476 // We use half the stroke width here because we're going to position the quad 1477 // corner vertices half of the width away from the line endpoints 1478 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f; 1479 // A stroke width of 0 has a special meaning in Skia: 1480 // it draws a line 1 px wide regardless of current transform 1481 bool isHairLine = paint->getStrokeWidth() == 0.0f; 1482 float inverseScaleX = 1.0f; 1483 float inverseScaleY = 1.0f; 1484 bool scaled = false; 1485 int alpha; 1486 SkXfermode::Mode mode; 1487 int generatedVerticesCount = 0; 1488 int verticesCount = count; 1489 if (count > 4) { 1490 // Polyline: account for extra vertices needed for continous tri-strip 1491 verticesCount += (count - 4); 1492 } 1493 1494 if (isHairLine || isAA) { 1495 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines 1496 // the line on the screen should always be one pixel wide regardless of scale. For 1497 // AA lines, we only want one pixel of translucent boundary around the quad. 1498 if (!mSnapshot->transform->isPureTranslate()) { 1499 Matrix4 *mat = mSnapshot->transform; 1500 float m00 = mat->data[Matrix4::kScaleX]; 1501 float m01 = mat->data[Matrix4::kSkewY]; 1502 float m02 = mat->data[2]; 1503 float m10 = mat->data[Matrix4::kSkewX]; 1504 float m11 = mat->data[Matrix4::kScaleX]; 1505 float m12 = mat->data[6]; 1506 float scaleX = sqrt(m00*m00 + m01*m01); 1507 float scaleY = sqrt(m10*m10 + m11*m11); 1508 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0; 1509 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0; 1510 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) { 1511 scaled = true; 1512 } 1513 } 1514 } 1515 1516 getAlphaAndMode(paint, &alpha, &mode); 1517 setupDraw(); 1518 if (isAA) { 1519 setupDrawAALine(); 1520 } 1521 setupDrawColor(paint->getColor(), alpha); 1522 setupDrawColorFilter(); 1523 setupDrawShader(); 1524 if (isAA) { 1525 setupDrawBlending(true, mode); 1526 } else { 1527 setupDrawBlending(mode); 1528 } 1529 setupDrawProgram(); 1530 setupDrawModelViewIdentity(true); 1531 setupDrawColorUniforms(); 1532 setupDrawColorFilterUniforms(); 1533 setupDrawShaderIdentityUniforms(); 1534 1535 if (isHairLine) { 1536 // Set a real stroke width to be used in quad construction 1537 halfStrokeWidth = isAA? 1 : .5; 1538 } else if (isAA && !scaled) { 1539 // Expand boundary to enable AA calculations on the quad border 1540 halfStrokeWidth += .5f; 1541 } 1542 Vertex lines[verticesCount]; 1543 Vertex* vertices = &lines[0]; 1544 AAVertex wLines[verticesCount]; 1545 AAVertex* aaVertices = &wLines[0]; 1546 if (!isAA) { 1547 setupDrawVertices(vertices); 1548 } else { 1549 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset; 1550 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset; 1551 // innerProportion is the ratio of the inner (non-AA) part of the line to the total 1552 // AA stroke width (the base stroke width expanded by a half pixel on either side). 1553 // This value is used in the fragment shader to determine how to fill fragments. 1554 // We will need to calculate the actual width proportion on each segment for 1555 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled. 1556 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth); 1557 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion); 1558 } 1559 1560 AAVertex* prevAAVertex = NULL; 1561 Vertex* prevVertex = NULL; 1562 1563 int boundaryLengthSlot = -1; 1564 int inverseBoundaryLengthSlot = -1; 1565 int boundaryWidthSlot = -1; 1566 int inverseBoundaryWidthSlot = -1; 1567 for (int i = 0; i < count; i += 4) { 1568 // a = start point, b = end point 1569 vec2 a(points[i], points[i + 1]); 1570 vec2 b(points[i + 2], points[i + 3]); 1571 float length = 0; 1572 float boundaryLengthProportion = 0; 1573 float boundaryWidthProportion = 0; 1574 1575 // Find the normal to the line 1576 vec2 n = (b - a).copyNormalized() * halfStrokeWidth; 1577 if (isHairLine) { 1578 if (isAA) { 1579 float wideningFactor; 1580 if (fabs(n.x) >= fabs(n.y)) { 1581 wideningFactor = fabs(1.0f / n.x); 1582 } else { 1583 wideningFactor = fabs(1.0f / n.y); 1584 } 1585 n *= wideningFactor; 1586 } 1587 if (scaled) { 1588 n.x *= inverseScaleX; 1589 n.y *= inverseScaleY; 1590 } 1591 } else if (scaled) { 1592 // Extend n by .5 pixel on each side, post-transform 1593 vec2 extendedN = n.copyNormalized(); 1594 extendedN /= 2; 1595 extendedN.x *= inverseScaleX; 1596 extendedN.y *= inverseScaleY; 1597 float extendedNLength = extendedN.length(); 1598 // We need to set this value on the shader prior to drawing 1599 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength); 1600 n += extendedN; 1601 } 1602 float x = n.x; 1603 n.x = -n.y; 1604 n.y = x; 1605 1606 // aa lines expand the endpoint vertices to encompass the AA boundary 1607 if (isAA) { 1608 vec2 abVector = (b - a); 1609 length = abVector.length(); 1610 abVector.normalize(); 1611 if (scaled) { 1612 abVector.x *= inverseScaleX; 1613 abVector.y *= inverseScaleY; 1614 float abLength = abVector.length(); 1615 boundaryLengthProportion = abLength / (length + abLength); 1616 } else { 1617 boundaryLengthProportion = .5 / (length + 1); 1618 } 1619 abVector /= 2; 1620 a -= abVector; 1621 b += abVector; 1622 } 1623 1624 // Four corners of the rectangle defining a thick line 1625 vec2 p1 = a - n; 1626 vec2 p2 = a + n; 1627 vec2 p3 = b + n; 1628 vec2 p4 = b - n; 1629 1630 1631 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x))); 1632 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x))); 1633 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y))); 1634 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y))); 1635 1636 if (!quickReject(left, top, right, bottom)) { 1637 if (!isAA) { 1638 if (prevVertex != NULL) { 1639 // Issue two repeat vertices to create degenerate triangles to bridge 1640 // between the previous line and the new one. This is necessary because 1641 // we are creating a single triangle_strip which will contain 1642 // potentially discontinuous line segments. 1643 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]); 1644 Vertex::set(vertices++, p1.x, p1.y); 1645 generatedVerticesCount += 2; 1646 } 1647 Vertex::set(vertices++, p1.x, p1.y); 1648 Vertex::set(vertices++, p2.x, p2.y); 1649 Vertex::set(vertices++, p4.x, p4.y); 1650 Vertex::set(vertices++, p3.x, p3.y); 1651 prevVertex = vertices - 1; 1652 generatedVerticesCount += 4; 1653 } else { 1654 if (!isHairLine && scaled) { 1655 // Must set width proportions per-segment for scaled non-hairlines to use the 1656 // correct AA boundary dimensions 1657 if (boundaryWidthSlot < 0) { 1658 boundaryWidthSlot = 1659 mCaches.currentProgram->getUniform("boundaryWidth"); 1660 inverseBoundaryWidthSlot = 1661 mCaches.currentProgram->getUniform("inverseBoundaryWidth"); 1662 } 1663 glUniform1f(boundaryWidthSlot, boundaryWidthProportion); 1664 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion)); 1665 } 1666 if (boundaryLengthSlot < 0) { 1667 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength"); 1668 inverseBoundaryLengthSlot = 1669 mCaches.currentProgram->getUniform("inverseBoundaryLength"); 1670 } 1671 glUniform1f(boundaryLengthSlot, boundaryLengthProportion); 1672 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion)); 1673 1674 if (prevAAVertex != NULL) { 1675 // Issue two repeat vertices to create degenerate triangles to bridge 1676 // between the previous line and the new one. This is necessary because 1677 // we are creating a single triangle_strip which will contain 1678 // potentially discontinuous line segments. 1679 AAVertex::set(aaVertices++,prevAAVertex->position[0], 1680 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length); 1681 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); 1682 generatedVerticesCount += 2; 1683 } 1684 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); 1685 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0); 1686 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1); 1687 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0); 1688 prevAAVertex = aaVertices - 1; 1689 generatedVerticesCount += 4; 1690 } 1691 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top, 1692 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom, 1693 *mSnapshot->transform); 1694 } 1695 } 1696 if (generatedVerticesCount > 0) { 1697 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount); 1698 } 1699} 1700 1701void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) { 1702 if (mSnapshot->isIgnored()) return; 1703 1704 // TODO: The paint's cap style defines whether the points are square or circular 1705 // TODO: Handle AA for round points 1706 1707 // A stroke width of 0 has a special meaning in Skia: 1708 // it draws an unscaled 1px point 1709 float strokeWidth = paint->getStrokeWidth(); 1710 const bool isHairLine = paint->getStrokeWidth() == 0.0f; 1711 if (isHairLine) { 1712 // Now that we know it's hairline, we can set the effective width, to be used later 1713 strokeWidth = 1.0f; 1714 } 1715 const float halfWidth = strokeWidth / 2; 1716 int alpha; 1717 SkXfermode::Mode mode; 1718 getAlphaAndMode(paint, &alpha, &mode); 1719 1720 int verticesCount = count >> 1; 1721 int generatedVerticesCount = 0; 1722 1723 TextureVertex pointsData[verticesCount]; 1724 TextureVertex* vertex = &pointsData[0]; 1725 1726 setupDraw(); 1727 setupDrawPoint(strokeWidth); 1728 setupDrawColor(paint->getColor(), alpha); 1729 setupDrawColorFilter(); 1730 setupDrawShader(); 1731 setupDrawBlending(mode); 1732 setupDrawProgram(); 1733 setupDrawModelViewIdentity(true); 1734 setupDrawColorUniforms(); 1735 setupDrawColorFilterUniforms(); 1736 setupDrawPointUniforms(); 1737 setupDrawShaderIdentityUniforms(); 1738 setupDrawMesh(vertex); 1739 1740 for (int i = 0; i < count; i += 2) { 1741 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f); 1742 generatedVerticesCount++; 1743 float left = points[i] - halfWidth; 1744 float right = points[i] + halfWidth; 1745 float top = points[i + 1] - halfWidth; 1746 float bottom = points [i + 1] + halfWidth; 1747 dirtyLayer(left, top, right, bottom, *mSnapshot->transform); 1748 } 1749 1750 glDrawArrays(GL_POINTS, 0, generatedVerticesCount); 1751} 1752 1753void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { 1754 // No need to check against the clip, we fill the clip region 1755 if (mSnapshot->isIgnored()) return; 1756 1757 Rect& clip(*mSnapshot->clipRect); 1758 clip.snapToPixelBoundaries(); 1759 1760 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); 1761} 1762 1763void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) { 1764 if (!texture) return; 1765 const AutoTexture autoCleanup(texture); 1766 1767 const float x = left + texture->left - texture->offset; 1768 const float y = top + texture->top - texture->offset; 1769 1770 drawPathTexture(texture, x, y, paint); 1771} 1772 1773void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom, 1774 float rx, float ry, SkPaint* paint) { 1775 if (mSnapshot->isIgnored()) return; 1776 1777 glActiveTexture(gTextureUnits[0]); 1778 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect( 1779 right - left, bottom - top, rx, ry, paint); 1780 drawShape(left, top, texture, paint); 1781} 1782 1783void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) { 1784 if (mSnapshot->isIgnored()) return; 1785 1786 glActiveTexture(gTextureUnits[0]); 1787 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint); 1788 drawShape(x - radius, y - radius, texture, paint); 1789} 1790 1791void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) { 1792 if (mSnapshot->isIgnored()) return; 1793 1794 glActiveTexture(gTextureUnits[0]); 1795 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint); 1796 drawShape(left, top, texture, paint); 1797} 1798 1799void OpenGLRenderer::drawArc(float left, float top, float right, float bottom, 1800 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) { 1801 if (mSnapshot->isIgnored()) return; 1802 1803 if (fabs(sweepAngle) >= 360.0f) { 1804 drawOval(left, top, right, bottom, paint); 1805 return; 1806 } 1807 1808 glActiveTexture(gTextureUnits[0]); 1809 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top, 1810 startAngle, sweepAngle, useCenter, paint); 1811 drawShape(left, top, texture, paint); 1812} 1813 1814void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom, 1815 SkPaint* paint) { 1816 if (mSnapshot->isIgnored()) return; 1817 1818 glActiveTexture(gTextureUnits[0]); 1819 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint); 1820 drawShape(left, top, texture, paint); 1821} 1822 1823void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) { 1824 if (p->getStyle() != SkPaint::kFill_Style) { 1825 drawRectAsShape(left, top, right, bottom, p); 1826 return; 1827 } 1828 1829 if (quickReject(left, top, right, bottom)) { 1830 return; 1831 } 1832 1833 SkXfermode::Mode mode; 1834 if (!mCaches.extensions.hasFramebufferFetch()) { 1835 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); 1836 if (!isMode) { 1837 // Assume SRC_OVER 1838 mode = SkXfermode::kSrcOver_Mode; 1839 } 1840 } else { 1841 mode = getXfermode(p->getXfermode()); 1842 } 1843 1844 int color = p->getColor(); 1845 drawColorRect(left, top, right, bottom, color, mode); 1846} 1847 1848void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, 1849 float x, float y, SkPaint* paint) { 1850 if (text == NULL || count == 0) { 1851 return; 1852 } 1853 if (mSnapshot->isIgnored()) return; 1854 1855 paint->setAntiAlias(true); 1856 1857 float length = -1.0f; 1858 switch (paint->getTextAlign()) { 1859 case SkPaint::kCenter_Align: 1860 length = paint->measureText(text, bytesCount); 1861 x -= length / 2.0f; 1862 break; 1863 case SkPaint::kRight_Align: 1864 length = paint->measureText(text, bytesCount); 1865 x -= length; 1866 break; 1867 default: 1868 break; 1869 } 1870 1871 const float oldX = x; 1872 const float oldY = y; 1873 const bool pureTranslate = mSnapshot->transform->isPureTranslate(); 1874 if (pureTranslate) { 1875 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); 1876 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); 1877 } 1878 1879 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); 1880 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), 1881 paint->getTextSize()); 1882 1883 int alpha; 1884 SkXfermode::Mode mode; 1885 getAlphaAndMode(paint, &alpha, &mode); 1886 1887 if (mHasShadow) { 1888 mCaches.dropShadowCache.setFontRenderer(fontRenderer); 1889 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount, 1890 count, mShadowRadius); 1891 const AutoTexture autoCleanup(shadow); 1892 1893 const float sx = oldX - shadow->left + mShadowDx; 1894 const float sy = oldY - shadow->top + mShadowDy; 1895 1896 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF); 1897 int shadowColor = mShadowColor; 1898 if (mShader) { 1899 shadowColor = 0xffffffff; 1900 } 1901 1902 glActiveTexture(gTextureUnits[0]); 1903 setupDraw(); 1904 setupDrawWithTexture(true); 1905 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha); 1906 setupDrawColorFilter(); 1907 setupDrawShader(); 1908 setupDrawBlending(true, mode); 1909 setupDrawProgram(); 1910 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height); 1911 setupDrawTexture(shadow->id); 1912 setupDrawPureColorUniforms(); 1913 setupDrawColorFilterUniforms(); 1914 setupDrawShaderUniforms(); 1915 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 1916 1917 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 1918 1919 finishDrawTexture(); 1920 } 1921 1922 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) { 1923 return; 1924 } 1925 1926 // Pick the appropriate texture filtering 1927 bool linearFilter = mSnapshot->transform->changesBounds(); 1928 if (pureTranslate && !linearFilter) { 1929 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; 1930 } 1931 1932 glActiveTexture(gTextureUnits[0]); 1933 setupDraw(); 1934 setupDrawDirtyRegionsDisabled(); 1935 setupDrawWithTexture(true); 1936 setupDrawAlpha8Color(paint->getColor(), alpha); 1937 setupDrawColorFilter(); 1938 setupDrawShader(); 1939 setupDrawBlending(true, mode); 1940 setupDrawProgram(); 1941 setupDrawModelView(x, y, x, y, pureTranslate, true); 1942 setupDrawTexture(fontRenderer.getTexture(linearFilter)); 1943 setupDrawPureColorUniforms(); 1944 setupDrawColorFilterUniforms(); 1945 setupDrawShaderUniforms(pureTranslate); 1946 1947 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); 1948 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); 1949 1950#if RENDER_LAYERS_AS_REGIONS 1951 bool hasActiveLayer = hasLayer(); 1952#else 1953 bool hasActiveLayer = false; 1954#endif 1955 mCaches.unbindMeshBuffer(); 1956 1957 // Tell font renderer the locations of position and texture coord 1958 // attributes so it can bind its data properly 1959 int positionSlot = mCaches.currentProgram->position; 1960 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot); 1961 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y, 1962 hasActiveLayer ? &bounds : NULL)) { 1963#if RENDER_LAYERS_AS_REGIONS 1964 if (hasActiveLayer) { 1965 if (!pureTranslate) { 1966 mSnapshot->transform->mapRect(bounds); 1967 } 1968 dirtyLayerUnchecked(bounds, getRegion()); 1969 } 1970#endif 1971 } 1972 1973 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 1974 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); 1975 1976 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint); 1977} 1978 1979void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { 1980 if (mSnapshot->isIgnored()) return; 1981 1982 glActiveTexture(gTextureUnits[0]); 1983 1984 const PathTexture* texture = mCaches.pathCache.get(path, paint); 1985 if (!texture) return; 1986 const AutoTexture autoCleanup(texture); 1987 1988 const float x = texture->left - texture->offset; 1989 const float y = texture->top - texture->offset; 1990 1991 drawPathTexture(texture, x, y, paint); 1992} 1993 1994void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) { 1995 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) { 1996 return; 1997 } 1998 1999 glActiveTexture(gTextureUnits[0]); 2000 2001 int alpha; 2002 SkXfermode::Mode mode; 2003 getAlphaAndMode(paint, &alpha, &mode); 2004 2005 layer->alpha = alpha; 2006 layer->mode = mode; 2007 2008#if RENDER_LAYERS_AS_REGIONS 2009 if (!layer->region.isEmpty()) { 2010 if (layer->region.isRect()) { 2011 composeLayerRect(layer, layer->regionRect); 2012 } else if (layer->mesh) { 2013 const float a = alpha / 255.0f; 2014 const Rect& rect = layer->layer; 2015 2016 setupDraw(); 2017 setupDrawWithTexture(); 2018 setupDrawColor(a, a, a, a); 2019 setupDrawColorFilter(); 2020 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false); 2021 setupDrawProgram(); 2022 setupDrawModelViewTranslate(x, y, 2023 x + layer->layer.getWidth(), y + layer->layer.getHeight()); 2024 setupDrawPureColorUniforms(); 2025 setupDrawColorFilterUniforms(); 2026 setupDrawTexture(layer->texture); 2027 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]); 2028 2029 glDrawElements(GL_TRIANGLES, layer->meshElementCount, 2030 GL_UNSIGNED_SHORT, layer->meshIndices); 2031 2032 finishDrawTexture(); 2033 2034#if DEBUG_LAYERS_AS_REGIONS 2035 drawRegionRects(layer->region); 2036#endif 2037 } 2038 } 2039#else 2040 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight()); 2041 composeLayerRect(layer, r); 2042#endif 2043} 2044 2045/////////////////////////////////////////////////////////////////////////////// 2046// Shaders 2047/////////////////////////////////////////////////////////////////////////////// 2048 2049void OpenGLRenderer::resetShader() { 2050 mShader = NULL; 2051} 2052 2053void OpenGLRenderer::setupShader(SkiaShader* shader) { 2054 mShader = shader; 2055 if (mShader) { 2056 mShader->set(&mCaches.textureCache, &mCaches.gradientCache); 2057 } 2058} 2059 2060/////////////////////////////////////////////////////////////////////////////// 2061// Color filters 2062/////////////////////////////////////////////////////////////////////////////// 2063 2064void OpenGLRenderer::resetColorFilter() { 2065 mColorFilter = NULL; 2066} 2067 2068void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { 2069 mColorFilter = filter; 2070} 2071 2072/////////////////////////////////////////////////////////////////////////////// 2073// Drop shadow 2074/////////////////////////////////////////////////////////////////////////////// 2075 2076void OpenGLRenderer::resetShadow() { 2077 mHasShadow = false; 2078} 2079 2080void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { 2081 mHasShadow = true; 2082 mShadowRadius = radius; 2083 mShadowDx = dx; 2084 mShadowDy = dy; 2085 mShadowColor = color; 2086} 2087 2088/////////////////////////////////////////////////////////////////////////////// 2089// Drawing implementation 2090/////////////////////////////////////////////////////////////////////////////// 2091 2092void OpenGLRenderer::drawPathTexture(const PathTexture* texture, 2093 float x, float y, SkPaint* paint) { 2094 if (quickReject(x, y, x + texture->width, y + texture->height)) { 2095 return; 2096 } 2097 2098 int alpha; 2099 SkXfermode::Mode mode; 2100 getAlphaAndMode(paint, &alpha, &mode); 2101 2102 setupDraw(); 2103 setupDrawWithTexture(true); 2104 setupDrawAlpha8Color(paint->getColor(), alpha); 2105 setupDrawColorFilter(); 2106 setupDrawShader(); 2107 setupDrawBlending(true, mode); 2108 setupDrawProgram(); 2109 setupDrawModelView(x, y, x + texture->width, y + texture->height); 2110 setupDrawTexture(texture->id); 2111 setupDrawPureColorUniforms(); 2112 setupDrawColorFilterUniforms(); 2113 setupDrawShaderUniforms(); 2114 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); 2115 2116 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 2117 2118 finishDrawTexture(); 2119} 2120 2121// Same values used by Skia 2122#define kStdStrikeThru_Offset (-6.0f / 21.0f) 2123#define kStdUnderline_Offset (1.0f / 9.0f) 2124#define kStdUnderline_Thickness (1.0f / 18.0f) 2125 2126void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, 2127 float x, float y, SkPaint* paint) { 2128 // Handle underline and strike-through 2129 uint32_t flags = paint->getFlags(); 2130 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { 2131 float underlineWidth = length; 2132 // If length is > 0.0f, we already measured the text for the text alignment 2133 if (length <= 0.0f) { 2134 underlineWidth = paint->measureText(text, bytesCount); 2135 } 2136 2137 float offsetX = 0; 2138 switch (paint->getTextAlign()) { 2139 case SkPaint::kCenter_Align: 2140 offsetX = underlineWidth * 0.5f; 2141 break; 2142 case SkPaint::kRight_Align: 2143 offsetX = underlineWidth; 2144 break; 2145 default: 2146 break; 2147 } 2148 2149 if (underlineWidth > 0.0f) { 2150 const float textSize = paint->getTextSize(); 2151 // TODO: Support stroke width < 1.0f when we have AA lines 2152 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f); 2153 2154 const float left = x - offsetX; 2155 float top = 0.0f; 2156 2157 int linesCount = 0; 2158 if (flags & SkPaint::kUnderlineText_Flag) linesCount++; 2159 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++; 2160 2161 const int pointsCount = 4 * linesCount; 2162 float points[pointsCount]; 2163 int currentPoint = 0; 2164 2165 if (flags & SkPaint::kUnderlineText_Flag) { 2166 top = y + textSize * kStdUnderline_Offset; 2167 points[currentPoint++] = left; 2168 points[currentPoint++] = top; 2169 points[currentPoint++] = left + underlineWidth; 2170 points[currentPoint++] = top; 2171 } 2172 2173 if (flags & SkPaint::kStrikeThruText_Flag) { 2174 top = y + textSize * kStdStrikeThru_Offset; 2175 points[currentPoint++] = left; 2176 points[currentPoint++] = top; 2177 points[currentPoint++] = left + underlineWidth; 2178 points[currentPoint++] = top; 2179 } 2180 2181 SkPaint linesPaint(*paint); 2182 linesPaint.setStrokeWidth(strokeWidth); 2183 2184 drawLines(&points[0], pointsCount, &linesPaint); 2185 } 2186 } 2187} 2188 2189void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, 2190 int color, SkXfermode::Mode mode, bool ignoreTransform) { 2191 // If a shader is set, preserve only the alpha 2192 if (mShader) { 2193 color |= 0x00ffffff; 2194 } 2195 2196 setupDraw(); 2197 setupDrawColor(color); 2198 setupDrawShader(); 2199 setupDrawColorFilter(); 2200 setupDrawBlending(mode); 2201 setupDrawProgram(); 2202 setupDrawModelView(left, top, right, bottom, ignoreTransform); 2203 setupDrawColorUniforms(); 2204 setupDrawShaderUniforms(ignoreTransform); 2205 setupDrawColorFilterUniforms(); 2206 setupDrawSimpleMesh(); 2207 2208 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); 2209} 2210 2211void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 2212 Texture* texture, SkPaint* paint) { 2213 int alpha; 2214 SkXfermode::Mode mode; 2215 getAlphaAndMode(paint, &alpha, &mode); 2216 2217 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 2218 2219 if (mSnapshot->transform->isPureTranslate()) { 2220 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); 2221 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); 2222 2223 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id, 2224 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL, 2225 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true); 2226 } else { 2227 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, 2228 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, 2229 GL_TRIANGLE_STRIP, gMeshCount); 2230 } 2231} 2232 2233void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, 2234 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { 2235 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, 2236 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); 2237} 2238 2239void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, 2240 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, 2241 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 2242 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) { 2243 2244 setupDraw(); 2245 setupDrawWithTexture(); 2246 setupDrawColor(alpha, alpha, alpha, alpha); 2247 setupDrawColorFilter(); 2248 setupDrawBlending(blend, mode, swapSrcDst); 2249 setupDrawProgram(); 2250 if (!dirty) { 2251 setupDrawDirtyRegionsDisabled(); 2252 } 2253 if (!ignoreScale) { 2254 setupDrawModelView(left, top, right, bottom, ignoreTransform); 2255 } else { 2256 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform); 2257 } 2258 setupDrawPureColorUniforms(); 2259 setupDrawColorFilterUniforms(); 2260 setupDrawTexture(texture); 2261 setupDrawMesh(vertices, texCoords, vbo); 2262 2263 glDrawArrays(drawMode, 0, elementsCount); 2264 2265 finishDrawTexture(); 2266} 2267 2268void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, 2269 ProgramDescription& description, bool swapSrcDst) { 2270 blend = blend || mode != SkXfermode::kSrcOver_Mode; 2271 if (blend) { 2272 if (mode < SkXfermode::kPlus_Mode) { 2273 if (!mCaches.blend) { 2274 glEnable(GL_BLEND); 2275 } 2276 2277 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; 2278 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; 2279 2280 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { 2281 glBlendFunc(sourceMode, destMode); 2282 mCaches.lastSrcMode = sourceMode; 2283 mCaches.lastDstMode = destMode; 2284 } 2285 } else { 2286 // These blend modes are not supported by OpenGL directly and have 2287 // to be implemented using shaders. Since the shader will perform 2288 // the blending, turn blending off here 2289 if (mCaches.extensions.hasFramebufferFetch()) { 2290 description.framebufferMode = mode; 2291 description.swapSrcDst = swapSrcDst; 2292 } 2293 2294 if (mCaches.blend) { 2295 glDisable(GL_BLEND); 2296 } 2297 blend = false; 2298 } 2299 } else if (mCaches.blend) { 2300 glDisable(GL_BLEND); 2301 } 2302 mCaches.blend = blend; 2303} 2304 2305bool OpenGLRenderer::useProgram(Program* program) { 2306 if (!program->isInUse()) { 2307 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); 2308 program->use(); 2309 mCaches.currentProgram = program; 2310 return false; 2311 } 2312 return true; 2313} 2314 2315void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { 2316 TextureVertex* v = &mMeshVertices[0]; 2317 TextureVertex::setUV(v++, u1, v1); 2318 TextureVertex::setUV(v++, u2, v1); 2319 TextureVertex::setUV(v++, u1, v2); 2320 TextureVertex::setUV(v++, u2, v2); 2321} 2322 2323void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { 2324 if (paint) { 2325 if (!mCaches.extensions.hasFramebufferFetch()) { 2326 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); 2327 if (!isMode) { 2328 // Assume SRC_OVER 2329 *mode = SkXfermode::kSrcOver_Mode; 2330 } 2331 } else { 2332 *mode = getXfermode(paint->getXfermode()); 2333 } 2334 2335 // Skia draws using the color's alpha channel if < 255 2336 // Otherwise, it uses the paint's alpha 2337 int color = paint->getColor(); 2338 *alpha = (color >> 24) & 0xFF; 2339 if (*alpha == 255) { 2340 *alpha = paint->getAlpha(); 2341 } 2342 } else { 2343 *mode = SkXfermode::kSrcOver_Mode; 2344 *alpha = 255; 2345 } 2346} 2347 2348SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) { 2349 // In the future we should look at unifying the Porter-Duff modes and 2350 // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode). 2351 if (mode == NULL) { 2352 return SkXfermode::kSrcOver_Mode; 2353 } 2354 return mode->fMode; 2355} 2356 2357void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) { 2358 bool bound = false; 2359 if (wrapS != texture->wrapS) { 2360 glBindTexture(GL_TEXTURE_2D, texture->id); 2361 bound = true; 2362 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); 2363 texture->wrapS = wrapS; 2364 } 2365 if (wrapT != texture->wrapT) { 2366 if (!bound) { 2367 glBindTexture(GL_TEXTURE_2D, texture->id); 2368 } 2369 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); 2370 texture->wrapT = wrapT; 2371 } 2372} 2373 2374}; // namespace uirenderer 2375}; // namespace android 2376