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