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