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