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 | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 24 | #include <SkPathMeasure.h> |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 25 | #include <SkTypeface.h> |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 26 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 27 | #include <utils/Log.h> |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 28 | #include <utils/StopWatch.h> |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 29 | |
Romain Guy | 08aa2cb | 2011-03-17 11:06:57 -0700 | [diff] [blame] | 30 | #include <private/hwui/DrawGlInfo.h> |
| 31 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 32 | #include <ui/Rect.h> |
| 33 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 34 | #include "OpenGLRenderer.h" |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 35 | #include "DisplayListRenderer.h" |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 36 | #include "PathRenderer.h" |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 37 | #include "Properties.h" |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 38 | #include "Vector.h" |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 39 | |
| 40 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 41 | namespace uirenderer { |
| 42 | |
| 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | // Defines |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 47 | #define RAD_TO_DEG (180.0f / 3.14159265f) |
| 48 | #define MIN_ANGLE 0.001f |
| 49 | |
Romain Guy | f877308 | 2012-07-12 18:01:00 -0700 | [diff] [blame] | 50 | #define ALPHA_THRESHOLD 0 |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 51 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 52 | #define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST) |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 53 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 54 | /////////////////////////////////////////////////////////////////////////////// |
| 55 | // Globals |
| 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Structure mapping Skia xfermodes to OpenGL blending factors. |
| 60 | */ |
| 61 | struct Blender { |
| 62 | SkXfermode::Mode mode; |
| 63 | GLenum src; |
| 64 | GLenum dst; |
| 65 | }; // struct Blender |
| 66 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 67 | // In this array, the index of each Blender equals the value of the first |
| 68 | // entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] |
| 69 | static const Blender gBlends[] = { |
Romain Guy | 2ffefd4 | 2011-09-08 15:33:03 -0700 | [diff] [blame] | 70 | { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 71 | { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, |
| 72 | { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, |
| 73 | { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 74 | { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 75 | { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 76 | { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 77 | { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 78 | { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 79 | { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 80 | { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 81 | { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 82 | { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE }, |
| 83 | { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR }, |
| 84 | { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR } |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 85 | }; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 86 | |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 87 | // This array contains the swapped version of each SkXfermode. For instance |
| 88 | // this array's SrcOver blending mode is actually DstOver. You can refer to |
| 89 | // createLayer() for more information on the purpose of this array. |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 90 | static const Blender gBlendsSwap[] = { |
Romain Guy | 2ffefd4 | 2011-09-08 15:33:03 -0700 | [diff] [blame] | 91 | { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 92 | { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE }, |
| 93 | { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO }, |
| 94 | { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 95 | { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 96 | { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 97 | { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 98 | { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 99 | { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 100 | { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 101 | { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 102 | { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 103 | { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE }, |
| 104 | { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO }, |
| 105 | { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE } |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 108 | /////////////////////////////////////////////////////////////////////////////// |
| 109 | // Constructors/destructor |
| 110 | /////////////////////////////////////////////////////////////////////////////// |
| 111 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 112 | OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 113 | mShader = NULL; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 114 | mColorFilter = NULL; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 115 | mHasShadow = false; |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 116 | mHasDrawFilter = false; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 117 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 118 | memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); |
| 119 | |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 120 | mFirstSnapshot = new Snapshot; |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 121 | |
| 122 | mScissorOptimizationDisabled = false; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 125 | OpenGLRenderer::~OpenGLRenderer() { |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 126 | // The context has already been destroyed at this point, do not call |
| 127 | // GL APIs. All GL state should be kept in Caches.h |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 130 | void OpenGLRenderer::initProperties() { |
| 131 | char property[PROPERTY_VALUE_MAX]; |
| 132 | if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) { |
| 133 | mScissorOptimizationDisabled = !strcasecmp(property, "true"); |
| 134 | INIT_LOGD(" Scissor optimization %s", |
| 135 | mScissorOptimizationDisabled ? "disabled" : "enabled"); |
| 136 | } else { |
| 137 | INIT_LOGD(" Scissor optimization enabled"); |
| 138 | } |
Romain Guy | 13631f3 | 2012-01-30 17:41:55 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 142 | // Setup |
| 143 | /////////////////////////////////////////////////////////////////////////////// |
| 144 | |
Romain Guy | 49c5fc0 | 2012-05-15 11:10:01 -0700 | [diff] [blame] | 145 | bool OpenGLRenderer::isDeferred() { |
| 146 | return false; |
| 147 | } |
| 148 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 149 | void OpenGLRenderer::setViewport(int width, int height) { |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 150 | initViewport(width, height); |
| 151 | |
| 152 | glDisable(GL_DITHER); |
| 153 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 154 | |
| 155 | glEnableVertexAttribArray(Program::kBindingPosition); |
| 156 | } |
| 157 | |
| 158 | void OpenGLRenderer::initViewport(int width, int height) { |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 159 | mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 160 | |
| 161 | mWidth = width; |
| 162 | mHeight = height; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 163 | |
| 164 | mFirstSnapshot->height = height; |
| 165 | mFirstSnapshot->viewport.set(0, 0, width, height); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 168 | status_t OpenGLRenderer::prepare(bool opaque) { |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 169 | return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque); |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 172 | status_t OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, |
| 173 | bool opaque) { |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 174 | mCaches.clearGarbage(); |
| 175 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 176 | mSnapshot = new Snapshot(mFirstSnapshot, |
| 177 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
Romain Guy | 84962f2 | 2011-03-02 15:43:44 -0800 | [diff] [blame] | 178 | mSnapshot->fbo = getTargetFbo(); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 179 | mSaveCount = 1; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 180 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 181 | mSnapshot->setClip(left, top, right, bottom); |
Romain Guy | 41308e2 | 2012-10-22 20:02:43 -0700 | [diff] [blame] | 182 | mDirtyClip = true; |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 183 | |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 184 | updateLayers(); |
| 185 | |
Romain Guy | 45e4c3d | 2012-09-11 17:17:07 -0700 | [diff] [blame] | 186 | // If we know that we are going to redraw the entire framebuffer, |
| 187 | // perform a discard to let the driver know we don't need to preserve |
| 188 | // the back buffer for this frame. |
| 189 | if (mCaches.extensions.hasDiscardFramebuffer() && |
| 190 | left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) { |
| 191 | const GLenum attachments[] = { getTargetFbo() == 0 ? GL_COLOR_EXT : GL_COLOR_ATTACHMENT0 }; |
| 192 | glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments); |
| 193 | } |
| 194 | |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 195 | syncState(); |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 196 | |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 197 | // Functors break the tiling extension in pretty spectacular ways |
| 198 | // This ensures we don't use tiling when a functor is going to be |
| 199 | // invoked during the frame |
| 200 | mSuppressTiling = mCaches.hasRegisteredFunctors(); |
| 201 | |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 202 | mTilingSnapshot = mSnapshot; |
Romain Guy | 57b5268 | 2012-09-20 17:38:46 -0700 | [diff] [blame] | 203 | startTiling(mTilingSnapshot, true); |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 204 | |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 205 | debugOverdraw(true, true); |
| 206 | |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 207 | return clear(left, top, right, bottom, opaque); |
| 208 | } |
| 209 | |
| 210 | status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) { |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 211 | if (!opaque) { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 212 | mCaches.enableScissor(); |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 213 | mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top); |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 214 | glClear(GL_COLOR_BUFFER_BIT); |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 215 | return DrawGlInfo::kStatusDrew; |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 216 | } |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 217 | |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 218 | mCaches.resetScissor(); |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 219 | return DrawGlInfo::kStatusDone; |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void OpenGLRenderer::syncState() { |
| 223 | glViewport(0, 0, mWidth, mHeight); |
| 224 | |
| 225 | if (mCaches.blend) { |
| 226 | glEnable(GL_BLEND); |
| 227 | } else { |
| 228 | glDisable(GL_BLEND); |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 229 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Romain Guy | 57b5268 | 2012-09-20 17:38:46 -0700 | [diff] [blame] | 232 | void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) { |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 233 | if (!mSuppressTiling) { |
| 234 | Rect* clip = mTilingSnapshot->clipRect; |
| 235 | if (s->flags & Snapshot::kFlagIsFboLayer) { |
| 236 | clip = s->clipRect; |
| 237 | } |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 238 | |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 239 | mCaches.startTiling(clip->left, s->height - clip->bottom, |
| 240 | clip->right - clip->left, clip->bottom - clip->top, opaque); |
| 241 | } |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | void OpenGLRenderer::endTiling() { |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 245 | if (!mSuppressTiling) mCaches.endTiling(); |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 248 | void OpenGLRenderer::finish() { |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 249 | renderOverdraw(); |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 250 | endTiling(); |
| 251 | |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 252 | if (!suppressErrorChecks()) { |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 253 | #if DEBUG_OPENGL |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 254 | GLenum status = GL_NO_ERROR; |
| 255 | while ((status = glGetError()) != GL_NO_ERROR) { |
| 256 | ALOGD("GL error from OpenGLRenderer: 0x%x", status); |
| 257 | switch (status) { |
| 258 | case GL_INVALID_ENUM: |
| 259 | ALOGE(" GL_INVALID_ENUM"); |
| 260 | break; |
| 261 | case GL_INVALID_VALUE: |
| 262 | ALOGE(" GL_INVALID_VALUE"); |
| 263 | break; |
| 264 | case GL_INVALID_OPERATION: |
| 265 | ALOGE(" GL_INVALID_OPERATION"); |
| 266 | break; |
| 267 | case GL_OUT_OF_MEMORY: |
| 268 | ALOGE(" Out of memory!"); |
| 269 | break; |
| 270 | } |
Romain Guy | a07105b | 2011-01-10 21:14:18 -0800 | [diff] [blame] | 271 | } |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 272 | #endif |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 273 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 274 | #if DEBUG_MEMORY_USAGE |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 275 | mCaches.dumpMemoryUsage(); |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 276 | #else |
| 277 | if (mCaches.getDebugLevel() & kDebugMemory) { |
| 278 | mCaches.dumpMemoryUsage(); |
| 279 | } |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 280 | #endif |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 281 | } |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 284 | void OpenGLRenderer::interrupt() { |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 285 | if (mCaches.currentProgram) { |
| 286 | if (mCaches.currentProgram->isInUse()) { |
| 287 | mCaches.currentProgram->remove(); |
| 288 | mCaches.currentProgram = NULL; |
| 289 | } |
| 290 | } |
Romain Guy | 50c0f09 | 2010-10-19 11:42:22 -0700 | [diff] [blame] | 291 | mCaches.unbindMeshBuffer(); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 292 | mCaches.unbindIndicesBuffer(); |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 293 | mCaches.resetVertexPointers(); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 294 | mCaches.disbaleTexCoordsVertexArray(); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 295 | debugOverdraw(false, false); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 298 | void OpenGLRenderer::resume() { |
Chet Haase | 08837c2 | 2011-11-28 11:53:21 -0800 | [diff] [blame] | 299 | sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot; |
Chet Haase | 08837c2 | 2011-11-28 11:53:21 -0800 | [diff] [blame] | 300 | glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight()); |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 301 | glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 302 | debugOverdraw(true, false); |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 303 | |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 304 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 305 | |
Chet Haase | 8025061 | 2012-08-15 13:46:54 -0700 | [diff] [blame] | 306 | mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST); |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 307 | mCaches.enableScissor(); |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 308 | mCaches.resetScissor(); |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 309 | dirtyClip(); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 310 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 311 | mCaches.activeTexture(0); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 312 | |
Romain Guy | 50c0f09 | 2010-10-19 11:42:22 -0700 | [diff] [blame] | 313 | mCaches.blend = true; |
| 314 | glEnable(GL_BLEND); |
| 315 | glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode); |
| 316 | glBlendEquation(GL_FUNC_ADD); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 319 | void OpenGLRenderer::resumeAfterLayer() { |
| 320 | sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot; |
| 321 | glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight()); |
| 322 | glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 323 | debugOverdraw(true, false); |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 324 | |
| 325 | mCaches.resetScissor(); |
| 326 | dirtyClip(); |
| 327 | } |
| 328 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 329 | void OpenGLRenderer::detachFunctor(Functor* functor) { |
Chris Craik | 932b7f6 | 2012-06-06 13:59:33 -0700 | [diff] [blame] | 330 | mFunctors.remove(functor); |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void OpenGLRenderer::attachFunctor(Functor* functor) { |
| 334 | mFunctors.add(functor); |
| 335 | } |
| 336 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 337 | status_t OpenGLRenderer::invokeFunctors(Rect& dirty) { |
| 338 | status_t result = DrawGlInfo::kStatusDone; |
Romain Guy | 3d745c0 | 2012-04-23 20:36:17 -0700 | [diff] [blame] | 339 | size_t count = mFunctors.size(); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 340 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 341 | if (count > 0) { |
| 342 | SortedVector<Functor*> functors(mFunctors); |
| 343 | mFunctors.clear(); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 344 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 345 | DrawGlInfo info; |
| 346 | info.clipLeft = 0; |
| 347 | info.clipTop = 0; |
| 348 | info.clipRight = 0; |
| 349 | info.clipBottom = 0; |
| 350 | info.isLayer = false; |
| 351 | info.width = 0; |
| 352 | info.height = 0; |
| 353 | memset(info.transform, 0, sizeof(float) * 16); |
| 354 | |
| 355 | for (size_t i = 0; i < count; i++) { |
| 356 | Functor* f = functors.itemAt(i); |
| 357 | result |= (*f)(DrawGlInfo::kModeProcess, &info); |
| 358 | |
Chris Craik | c2c9543 | 2012-04-25 15:13:52 -0700 | [diff] [blame] | 359 | if (result & DrawGlInfo::kStatusDraw) { |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 360 | Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom); |
| 361 | dirty.unionWith(localDirty); |
Chris Craik | c2c9543 | 2012-04-25 15:13:52 -0700 | [diff] [blame] | 362 | } |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 363 | |
Chris Craik | c2c9543 | 2012-04-25 15:13:52 -0700 | [diff] [blame] | 364 | if (result & DrawGlInfo::kStatusInvoke) { |
| 365 | mFunctors.add(f); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
Chet Haase | beb8bd0 | 2012-09-09 16:13:26 -0700 | [diff] [blame] | 368 | // protect against functors binding to other buffers |
| 369 | mCaches.unbindMeshBuffer(); |
| 370 | mCaches.unbindIndicesBuffer(); |
| 371 | mCaches.activeTexture(0); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | return result; |
| 375 | } |
| 376 | |
| 377 | status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) { |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 378 | interrupt(); |
Chris Craik | 932b7f6 | 2012-06-06 13:59:33 -0700 | [diff] [blame] | 379 | detachFunctor(functor); |
Chris Craik | c8538ade | 2012-05-22 11:54:06 -0700 | [diff] [blame] | 380 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 381 | mCaches.enableScissor(); |
Romain Guy | f90f817 | 2011-01-25 22:53:24 -0800 | [diff] [blame] | 382 | if (mDirtyClip) { |
| 383 | setScissorFromClip(); |
| 384 | } |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 385 | |
Romain Guy | 80911b8 | 2011-03-16 15:30:12 -0700 | [diff] [blame] | 386 | Rect clip(*mSnapshot->clipRect); |
| 387 | clip.snapToPixelBoundaries(); |
| 388 | |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 389 | // Since we don't know what the functor will draw, let's dirty |
| 390 | // tne entire clip region |
| 391 | if (hasLayer()) { |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 392 | dirtyLayerUnchecked(clip, getRegion()); |
| 393 | } |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 394 | |
Romain Guy | 08aa2cb | 2011-03-17 11:06:57 -0700 | [diff] [blame] | 395 | DrawGlInfo info; |
| 396 | info.clipLeft = clip.left; |
| 397 | info.clipTop = clip.top; |
| 398 | info.clipRight = clip.right; |
| 399 | info.clipBottom = clip.bottom; |
| 400 | info.isLayer = hasLayer(); |
Chet Haase | 7b6a758 | 2012-04-11 14:32:02 -0700 | [diff] [blame] | 401 | info.width = getSnapshot()->viewport.getWidth(); |
| 402 | info.height = getSnapshot()->height; |
Romain Guy | 08aa2cb | 2011-03-17 11:06:57 -0700 | [diff] [blame] | 403 | getSnapshot()->transform->copyTo(&info.transform[0]); |
Romain Guy | 80911b8 | 2011-03-16 15:30:12 -0700 | [diff] [blame] | 404 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 405 | status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew; |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 406 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 407 | if (result != DrawGlInfo::kStatusDone) { |
Romain Guy | 08aa2cb | 2011-03-17 11:06:57 -0700 | [diff] [blame] | 408 | Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom); |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 409 | dirty.unionWith(localDirty); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 410 | |
Chris Craik | 65924a3 | 2012-04-05 17:52:11 -0700 | [diff] [blame] | 411 | if (result & DrawGlInfo::kStatusInvoke) { |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 412 | mFunctors.add(functor); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 413 | } |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 416 | resume(); |
Romain Guy | 6554943 | 2012-03-26 16:45:05 -0700 | [diff] [blame] | 417 | return result; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 420 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 421 | // Debug |
| 422 | /////////////////////////////////////////////////////////////////////////////// |
| 423 | |
| 424 | void OpenGLRenderer::startMark(const char* name) const { |
| 425 | mCaches.startMark(0, name); |
| 426 | } |
| 427 | |
| 428 | void OpenGLRenderer::endMark() const { |
| 429 | mCaches.endMark(); |
| 430 | } |
| 431 | |
| 432 | void OpenGLRenderer::debugOverdraw(bool enable, bool clear) { |
| 433 | if (mCaches.debugOverdraw && getTargetFbo() == 0) { |
| 434 | if (clear) { |
| 435 | mCaches.disableScissor(); |
| 436 | mCaches.stencil.clear(); |
| 437 | } |
| 438 | if (enable) { |
| 439 | mCaches.stencil.enableDebugWrite(); |
| 440 | } else { |
| 441 | mCaches.stencil.disable(); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | void OpenGLRenderer::renderOverdraw() { |
| 447 | if (mCaches.debugOverdraw && getTargetFbo() == 0) { |
| 448 | const Rect* clip = mTilingSnapshot->clipRect; |
| 449 | |
| 450 | mCaches.enableScissor(); |
| 451 | mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom, |
| 452 | clip->right - clip->left, clip->bottom - clip->top); |
| 453 | |
| 454 | mCaches.stencil.enableDebugTest(2); |
| 455 | drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode); |
| 456 | mCaches.stencil.enableDebugTest(3); |
| 457 | drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode); |
| 458 | mCaches.stencil.enableDebugTest(4); |
| 459 | drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode); |
| 460 | mCaches.stencil.enableDebugTest(4, true); |
| 461 | drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode); |
| 462 | mCaches.stencil.disable(); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 467 | // Layers |
| 468 | /////////////////////////////////////////////////////////////////////////////// |
| 469 | |
| 470 | bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) { |
| 471 | if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) { |
| 472 | OpenGLRenderer* renderer = layer->renderer; |
| 473 | Rect& dirty = layer->dirtyRect; |
| 474 | |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 475 | if (inFrame) { |
| 476 | endTiling(); |
| 477 | debugOverdraw(false, false); |
| 478 | } |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 479 | |
| 480 | renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight()); |
| 481 | renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend()); |
| 482 | renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren); |
| 483 | renderer->finish(); |
| 484 | |
| 485 | if (inFrame) { |
| 486 | resumeAfterLayer(); |
| 487 | startTiling(mSnapshot); |
| 488 | } |
| 489 | |
| 490 | dirty.setEmpty(); |
| 491 | layer->deferredUpdateScheduled = false; |
| 492 | layer->renderer = NULL; |
| 493 | layer->displayList = NULL; |
| 494 | |
| 495 | return true; |
| 496 | } |
| 497 | |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | void OpenGLRenderer::updateLayers() { |
| 502 | int count = mLayerUpdates.size(); |
| 503 | if (count > 0) { |
| 504 | startMark("Layer Updates"); |
| 505 | |
| 506 | // Note: it is very important to update the layers in reverse order |
| 507 | for (int i = count - 1; i >= 0; i--) { |
| 508 | Layer* layer = mLayerUpdates.itemAt(i); |
| 509 | updateLayer(layer, false); |
| 510 | mCaches.resourceCache.decrementRefcount(layer); |
| 511 | } |
| 512 | mLayerUpdates.clear(); |
| 513 | |
| 514 | glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo()); |
| 515 | endMark(); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | void OpenGLRenderer::pushLayerUpdate(Layer* layer) { |
| 520 | if (layer) { |
| 521 | mLayerUpdates.push_back(layer); |
| 522 | mCaches.resourceCache.incrementRefcount(layer); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | void OpenGLRenderer::clearLayerUpdates() { |
| 527 | size_t count = mLayerUpdates.size(); |
| 528 | if (count > 0) { |
| 529 | mCaches.resourceCache.lock(); |
| 530 | for (size_t i = 0; i < count; i++) { |
| 531 | mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i)); |
| 532 | } |
| 533 | mCaches.resourceCache.unlock(); |
| 534 | mLayerUpdates.clear(); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 539 | // State management |
| 540 | /////////////////////////////////////////////////////////////////////////////// |
| 541 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 542 | int OpenGLRenderer::getSaveCount() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 543 | return mSaveCount; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | int OpenGLRenderer::save(int flags) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 547 | return saveSnapshot(flags); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void OpenGLRenderer::restore() { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 551 | if (mSaveCount > 1) { |
| 552 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 553 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | void OpenGLRenderer::restoreToCount(int saveCount) { |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 557 | if (saveCount < 1) saveCount = 1; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 558 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 559 | while (mSaveCount > saveCount) { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 560 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 561 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 564 | int OpenGLRenderer::saveSnapshot(int flags) { |
| 565 | mSnapshot = new Snapshot(mSnapshot, flags); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 566 | return mSaveCount++; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | bool OpenGLRenderer::restoreSnapshot() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 570 | bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 571 | bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 572 | bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 573 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 574 | sp<Snapshot> current = mSnapshot; |
| 575 | sp<Snapshot> previous = mSnapshot->previous; |
| 576 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 577 | if (restoreOrtho) { |
| 578 | Rect& r = previous->viewport; |
| 579 | glViewport(r.left, r.top, r.right, r.bottom); |
| 580 | mOrthoMatrix.load(current->orthoMatrix); |
| 581 | } |
| 582 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 583 | mSaveCount--; |
| 584 | mSnapshot = previous; |
| 585 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 586 | if (restoreClip) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 587 | dirtyClip(); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 588 | } |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 589 | |
Romain Guy | 5ec9924 | 2010-11-03 16:19:08 -0700 | [diff] [blame] | 590 | if (restoreLayer) { |
| 591 | composeLayer(current, previous); |
| 592 | } |
| 593 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 594 | return restoreClip; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 597 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 598 | // Layers |
| 599 | /////////////////////////////////////////////////////////////////////////////// |
| 600 | |
| 601 | int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 602 | SkPaint* p, int flags) { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 603 | const GLuint previousFbo = mSnapshot->fbo; |
| 604 | const int count = saveSnapshot(flags); |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 605 | |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 606 | if (!mSnapshot->isIgnored()) { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 607 | int alpha = 255; |
| 608 | SkXfermode::Mode mode; |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 609 | |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 610 | if (p) { |
| 611 | alpha = p->getAlpha(); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 612 | mode = getXfermode(p->getXfermode()); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 613 | } else { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 614 | mode = SkXfermode::kSrcOver_Mode; |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 615 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 616 | |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 617 | createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo); |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 618 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 619 | |
| 620 | return count; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, |
| 624 | int alpha, int flags) { |
Romain Guy | f877308 | 2012-07-12 18:01:00 -0700 | [diff] [blame] | 625 | if (alpha >= 255) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 626 | return saveLayer(left, top, right, bottom, NULL, flags); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 627 | } else { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 628 | SkPaint paint; |
| 629 | paint.setAlpha(alpha); |
| 630 | return saveLayer(left, top, right, bottom, &paint, flags); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 631 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 632 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 633 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 634 | /** |
| 635 | * Layers are viewed by Skia are slightly different than layers in image editing |
| 636 | * programs (for instance.) When a layer is created, previously created layers |
| 637 | * and the frame buffer still receive every drawing command. For instance, if a |
| 638 | * layer is created and a shape intersecting the bounds of the layers and the |
| 639 | * framebuffer is draw, the shape will be drawn on both (unless the layer was |
| 640 | * created with the SkCanvas::kClipToLayer_SaveFlag flag.) |
| 641 | * |
| 642 | * A way to implement layers is to create an FBO for each layer, backed by an RGBA |
| 643 | * texture. Unfortunately, this is inefficient as it requires every primitive to |
| 644 | * be drawn n + 1 times, where n is the number of active layers. In practice this |
| 645 | * means, for every primitive: |
| 646 | * - Switch active frame buffer |
| 647 | * - Change viewport, clip and projection matrix |
| 648 | * - Issue the drawing |
| 649 | * |
| 650 | * Switching rendering target n + 1 times per drawn primitive is extremely costly. |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 651 | * To avoid this, layers are implemented in a different way here, at least in the |
| 652 | * general case. FBOs are used, as an optimization, when the "clip to layer" flag |
| 653 | * is set. When this flag is set we can redirect all drawing operations into a |
| 654 | * single FBO. |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 655 | * |
| 656 | * This implementation relies on the frame buffer being at least RGBA 8888. When |
| 657 | * a layer is created, only a texture is created, not an FBO. The content of the |
| 658 | * 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] | 659 | * 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] | 660 | * buffer and drawing continues as normal. This technique therefore treats the |
| 661 | * frame buffer as a scratch buffer for the layers. |
| 662 | * |
| 663 | * To compose the layers back onto the frame buffer, each layer texture |
| 664 | * (containing the original frame buffer data) is drawn as a simple quad over |
| 665 | * the frame buffer. The trick is that the quad is set as the composition |
| 666 | * destination in the blending equation, and the frame buffer becomes the source |
| 667 | * of the composition. |
| 668 | * |
| 669 | * Drawing layers with an alpha value requires an extra step before composition. |
| 670 | * An empty quad is drawn over the layer's region in the frame buffer. This quad |
| 671 | * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the |
| 672 | * quad is used to multiply the colors in the frame buffer. This is achieved by |
| 673 | * changing the GL blend functions for the GL_FUNC_ADD blend equation to |
| 674 | * GL_ZERO, GL_SRC_ALPHA. |
| 675 | * |
| 676 | * Because glCopyTexImage2D() can be slow, an alternative implementation might |
| 677 | * be use to draw a single clipped layer. The implementation described above |
| 678 | * is correct in every case. |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 679 | * |
| 680 | * (1) The frame buffer is actually not cleared right away. To allow the GPU |
| 681 | * to potentially optimize series of calls to glCopyTexImage2D, the frame |
| 682 | * buffer is left untouched until the first drawing operation. Only when |
| 683 | * something actually gets drawn are the layers regions cleared. |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 684 | */ |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 685 | bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom, |
| 686 | int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 687 | LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 688 | LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 689 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 690 | const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag; |
| 691 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 692 | // Window coordinates of the layer |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 693 | Rect clip; |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 694 | Rect bounds(left, top, right, bottom); |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 695 | Rect untransformedBounds(bounds); |
| 696 | mSnapshot->transform->mapRect(bounds); |
Romain Guy | ae51759 | 2010-10-22 10:40:27 -0700 | [diff] [blame] | 697 | |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 698 | // Layers only make sense if they are in the framebuffer's bounds |
| 699 | if (bounds.intersect(*mSnapshot->clipRect)) { |
| 700 | // We cannot work with sub-pixels in this case |
| 701 | bounds.snapToPixelBoundaries(); |
Romain Guy | ae51759 | 2010-10-22 10:40:27 -0700 | [diff] [blame] | 702 | |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 703 | // When the layer is not an FBO, we may use glCopyTexImage so we |
| 704 | // need to make sure the layer does not extend outside the bounds |
| 705 | // of the framebuffer |
| 706 | if (!bounds.intersect(mSnapshot->previous->viewport)) { |
Romain Guy | ad37cd3 | 2011-03-15 11:12:25 -0700 | [diff] [blame] | 707 | bounds.setEmpty(); |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 708 | } else if (fboLayer) { |
| 709 | clip.set(bounds); |
| 710 | mat4 inverse; |
| 711 | inverse.loadInverse(*mSnapshot->transform); |
| 712 | inverse.mapRect(clip); |
| 713 | clip.snapToPixelBoundaries(); |
| 714 | if (clip.intersect(untransformedBounds)) { |
| 715 | clip.translate(-left, -top); |
| 716 | bounds.set(untransformedBounds); |
| 717 | } else { |
| 718 | clip.setEmpty(); |
| 719 | } |
Romain Guy | ad37cd3 | 2011-03-15 11:12:25 -0700 | [diff] [blame] | 720 | } |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 721 | } else { |
| 722 | bounds.setEmpty(); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 723 | } |
Romain Guy | bf43411 | 2010-09-16 14:40:17 -0700 | [diff] [blame] | 724 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 725 | if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize || |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 726 | bounds.getHeight() > mCaches.maxTextureSize || |
| 727 | (fboLayer && clip.isEmpty())) { |
| 728 | mSnapshot->empty = fboLayer; |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 729 | } else { |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 730 | mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer); |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | // Bail out if we won't draw in this snapshot |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 734 | if (mSnapshot->invisible || mSnapshot->empty) { |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 735 | return false; |
| 736 | } |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 737 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 738 | mCaches.activeTexture(0); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 739 | Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight()); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 740 | if (!layer) { |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 741 | return false; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 744 | layer->setAlpha(alpha, mode); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 745 | layer->layer.set(bounds); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 746 | layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()), |
| 747 | bounds.getWidth() / float(layer->getWidth()), 0.0f); |
| 748 | layer->setColorFilter(mColorFilter); |
Chet Haase | a23eed8 | 2012-04-12 15:19:04 -0700 | [diff] [blame] | 749 | layer->setBlend(true); |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 750 | layer->setDirty(false); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 751 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 752 | // Save the layer in the snapshot |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 753 | mSnapshot->flags |= Snapshot::kFlagIsLayer; |
| 754 | mSnapshot->layer = layer; |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 755 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 756 | if (fboLayer) { |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 757 | return createFboLayer(layer, bounds, clip, previousFbo); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 758 | } else { |
| 759 | // Copy the framebuffer into the layer |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 760 | layer->bindTexture(); |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 761 | if (!bounds.isEmpty()) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 762 | if (layer->isEmpty()) { |
| 763 | glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 764 | bounds.left, mSnapshot->height - bounds.bottom, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 765 | layer->getWidth(), layer->getHeight(), 0); |
| 766 | layer->setEmpty(false); |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 767 | } else { |
| 768 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 769 | mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight()); |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 770 | } |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 771 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 772 | // Enqueue the buffer coordinates to clear the corresponding region later |
| 773 | mLayers.push(new Rect(bounds)); |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 774 | } |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 775 | } |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 776 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 777 | return true; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 780 | bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 781 | layer->setFbo(mCaches.fboCache.get()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 782 | |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 783 | mSnapshot->region = &mSnapshot->layer->region; |
| 784 | mSnapshot->flags |= Snapshot::kFlagFboTarget; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 785 | |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 786 | mSnapshot->flags |= Snapshot::kFlagIsFboLayer; |
| 787 | mSnapshot->fbo = layer->getFbo(); |
| 788 | mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); |
| 789 | mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom); |
| 790 | mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); |
| 791 | mSnapshot->height = bounds.getHeight(); |
| 792 | mSnapshot->flags |= Snapshot::kFlagDirtyOrtho; |
| 793 | mSnapshot->orthoMatrix.load(mOrthoMatrix); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 794 | |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 795 | endTiling(); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 796 | debugOverdraw(false, false); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 797 | // Bind texture to FBO |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 798 | glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo()); |
| 799 | layer->bindTexture(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 800 | |
| 801 | // Initialize the texture if needed |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 802 | if (layer->isEmpty()) { |
| 803 | layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE); |
| 804 | layer->setEmpty(false); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 808 | layer->getTexture(), 0); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 809 | |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 810 | startTiling(mSnapshot); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 811 | |
| 812 | // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 813 | mCaches.enableScissor(); |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 814 | mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f, |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 815 | clip.getWidth() + 2.0f, clip.getHeight() + 2.0f); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 816 | glClear(GL_COLOR_BUFFER_BIT); |
| 817 | |
| 818 | dirtyClip(); |
| 819 | |
| 820 | // Change the ortho projection |
| 821 | glViewport(0, 0, bounds.getWidth(), bounds.getHeight()); |
| 822 | mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f); |
| 823 | |
| 824 | return true; |
| 825 | } |
| 826 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 827 | /** |
| 828 | * Read the documentation of createLayer() before doing anything in this method. |
| 829 | */ |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 830 | void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { |
| 831 | if (!current->layer) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 832 | ALOGE("Attempting to compose a layer that does not exist"); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 833 | return; |
| 834 | } |
| 835 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 836 | const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 837 | |
| 838 | if (fboLayer) { |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 839 | endTiling(); |
| 840 | |
Romain Guy | e0aa84b | 2012-04-03 19:30:26 -0700 | [diff] [blame] | 841 | // Detach the texture from the FBO |
| 842 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 843 | // Unbind current FBO and restore previous one |
| 844 | glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 845 | debugOverdraw(true, false); |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 846 | |
| 847 | startTiling(previous); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 850 | Layer* layer = current->layer; |
| 851 | const Rect& rect = layer->layer; |
| 852 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 853 | if (!fboLayer && layer->getAlpha() < 255) { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 854 | drawColorRect(rect.left, rect.top, rect.right, rect.bottom, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 855 | layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 856 | // Required below, composeLayerRect() will divide by 255 |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 857 | layer->setAlpha(255); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 860 | mCaches.unbindMeshBuffer(); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 861 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 862 | mCaches.activeTexture(0); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 863 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 864 | // When the layer is stored in an FBO, we can save a bit of fillrate by |
| 865 | // drawing only the dirty region |
| 866 | if (fboLayer) { |
| 867 | dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 868 | if (layer->getColorFilter()) { |
| 869 | setupColorFilter(layer->getColorFilter()); |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 870 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 871 | composeLayerRegion(layer, rect); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 872 | if (layer->getColorFilter()) { |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 873 | resetColorFilter(); |
| 874 | } |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 875 | } else if (!rect.isEmpty()) { |
| 876 | dirtyLayer(rect.left, rect.top, rect.right, rect.bottom); |
| 877 | composeLayerRect(layer, rect, true); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 878 | } |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 879 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 880 | if (fboLayer) { |
Romain Guy | 9c4b79a | 2011-11-10 19:23:58 -0800 | [diff] [blame] | 881 | // Note: No need to use glDiscardFramebufferEXT() since we never |
| 882 | // create/compose layers that are not on screen with this |
| 883 | // code path |
| 884 | // See LayerRenderer::destroyLayer(Layer*) |
| 885 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 886 | // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed |
| 887 | mCaches.fboCache.put(current->fbo); |
Romain Guy | 5c88fc7 | 2012-04-02 17:43:05 -0700 | [diff] [blame] | 888 | layer->setFbo(0); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 891 | dirtyClip(); |
| 892 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 893 | // 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] | 894 | if (!mCaches.layerCache.put(layer)) { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 895 | LAYER_LOGD("Deleting layer"); |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 896 | Caches::getInstance().resourceCache.decrementRefcount(layer); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 900 | void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 901 | float alpha = layer->getAlpha() / 255.0f; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 902 | |
| 903 | setupDraw(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 904 | if (layer->getRenderTarget() == GL_TEXTURE_2D) { |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 905 | setupDrawWithTexture(); |
| 906 | } else { |
| 907 | setupDrawWithExternalTexture(); |
| 908 | } |
| 909 | setupDrawTextureTransform(); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 910 | setupDrawColor(alpha, alpha, alpha, alpha); |
| 911 | setupDrawColorFilter(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 912 | setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode()); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 913 | setupDrawProgram(); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 914 | setupDrawPureColorUniforms(); |
| 915 | setupDrawColorFilterUniforms(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 916 | if (layer->getRenderTarget() == GL_TEXTURE_2D) { |
| 917 | setupDrawTexture(layer->getTexture()); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 918 | } else { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 919 | setupDrawExternalTexture(layer->getTexture()); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 920 | } |
Romain Guy | ec19b4a | 2011-07-07 21:05:04 -0700 | [diff] [blame] | 921 | if (mSnapshot->transform->isPureTranslate() && |
| 922 | layer->getWidth() == (uint32_t) rect.getWidth() && |
| 923 | layer->getHeight() == (uint32_t) rect.getHeight()) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 924 | const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 925 | const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 926 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 927 | layer->setFilter(GL_NEAREST); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 928 | setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true); |
| 929 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 930 | layer->setFilter(GL_LINEAR); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 931 | setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom); |
| 932 | } |
| 933 | setupDrawTextureTransformUniforms(layer->getTexTransform()); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 934 | setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]); |
| 935 | |
| 936 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 937 | |
| 938 | finishDrawTexture(); |
| 939 | } |
| 940 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 941 | void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 942 | if (!layer->isTextureLayer()) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 943 | const Rect& texCoords = layer->texCoords; |
| 944 | resetDrawTextureTexCoords(texCoords.left, texCoords.top, |
| 945 | texCoords.right, texCoords.bottom); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 946 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 947 | float x = rect.left; |
| 948 | float y = rect.top; |
Romain Guy | b247915 | 2011-07-08 11:57:29 -0700 | [diff] [blame] | 949 | bool simpleTransform = mSnapshot->transform->isPureTranslate() && |
Romain Guy | ec19b4a | 2011-07-07 21:05:04 -0700 | [diff] [blame] | 950 | layer->getWidth() == (uint32_t) rect.getWidth() && |
Romain Guy | b247915 | 2011-07-08 11:57:29 -0700 | [diff] [blame] | 951 | layer->getHeight() == (uint32_t) rect.getHeight(); |
| 952 | |
| 953 | if (simpleTransform) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 954 | // When we're swapping, the layer is already in screen coordinates |
| 955 | if (!swap) { |
| 956 | x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 957 | y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 958 | } |
| 959 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 960 | layer->setFilter(GL_NEAREST, true); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 961 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 962 | layer->setFilter(GL_LINEAR, true); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(), |
| 966 | layer->getTexture(), layer->getAlpha() / 255.0f, |
| 967 | layer->getMode(), layer->isBlend(), |
| 968 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 969 | GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 970 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 971 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 972 | } else { |
| 973 | resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f); |
| 974 | drawTextureLayer(layer, rect); |
| 975 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 976 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 980 | if (layer->region.isRect()) { |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 981 | layer->setRegionAsRect(); |
| 982 | |
Romain Guy | 4066767 | 2011-03-18 14:34:03 -0700 | [diff] [blame] | 983 | composeLayerRect(layer, layer->regionRect); |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 984 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 985 | layer->region.clear(); |
| 986 | return; |
| 987 | } |
| 988 | |
Romain Guy | 8a3957d | 2011-09-07 17:55:15 -0700 | [diff] [blame] | 989 | // TODO: See LayerRenderer.cpp::generateMesh() for important |
| 990 | // information about this implementation |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 991 | if (CC_LIKELY(!layer->region.isEmpty())) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 992 | size_t count; |
| 993 | const android::Rect* rects = layer->region.getArray(&count); |
| 994 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 995 | const float alpha = layer->getAlpha() / 255.0f; |
| 996 | const float texX = 1.0f / float(layer->getWidth()); |
| 997 | const float texY = 1.0f / float(layer->getHeight()); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 998 | const float height = rect.getHeight(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 999 | |
| 1000 | TextureVertex* mesh = mCaches.getRegionMesh(); |
| 1001 | GLsizei numQuads = 0; |
| 1002 | |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 1003 | setupDraw(); |
| 1004 | setupDrawWithTexture(); |
| 1005 | setupDrawColor(alpha, alpha, alpha, alpha); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 1006 | setupDrawColorFilter(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1007 | setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false); |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 1008 | setupDrawProgram(); |
| 1009 | setupDrawDirtyRegionsDisabled(); |
| 1010 | setupDrawPureColorUniforms(); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 1011 | setupDrawColorFilterUniforms(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1012 | setupDrawTexture(layer->getTexture()); |
| 1013 | if (mSnapshot->transform->isPureTranslate()) { |
| 1014 | const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 1015 | const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 1016 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1017 | layer->setFilter(GL_NEAREST); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1018 | setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true); |
| 1019 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1020 | layer->setFilter(GL_LINEAR); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1021 | setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom); |
| 1022 | } |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1023 | setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1024 | |
| 1025 | for (size_t i = 0; i < count; i++) { |
| 1026 | const android::Rect* r = &rects[i]; |
| 1027 | |
| 1028 | const float u1 = r->left * texX; |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1029 | const float v1 = (height - r->top) * texY; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1030 | const float u2 = r->right * texX; |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1031 | const float v2 = (height - r->bottom) * texY; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1032 | |
| 1033 | // TODO: Reject quads outside of the clip |
| 1034 | TextureVertex::set(mesh++, r->left, r->top, u1, v1); |
| 1035 | TextureVertex::set(mesh++, r->right, r->top, u2, v1); |
| 1036 | TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); |
| 1037 | TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); |
| 1038 | |
| 1039 | numQuads++; |
| 1040 | |
| 1041 | if (numQuads >= REGION_MESH_QUAD_COUNT) { |
| 1042 | glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); |
| 1043 | numQuads = 0; |
| 1044 | mesh = mCaches.getRegionMesh(); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | if (numQuads > 0) { |
| 1049 | glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); |
| 1050 | } |
| 1051 | |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 1052 | finishDrawTexture(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1053 | |
| 1054 | #if DEBUG_LAYERS_AS_REGIONS |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 1055 | drawRegionRects(layer->region); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1056 | #endif |
| 1057 | |
| 1058 | layer->region.clear(); |
| 1059 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 1062 | void OpenGLRenderer::drawRegionRects(const Region& region) { |
| 1063 | #if DEBUG_LAYERS_AS_REGIONS |
| 1064 | size_t count; |
| 1065 | const android::Rect* rects = region.getArray(&count); |
| 1066 | |
| 1067 | uint32_t colors[] = { |
| 1068 | 0x7fff0000, 0x7f00ff00, |
| 1069 | 0x7f0000ff, 0x7fff00ff, |
| 1070 | }; |
| 1071 | |
| 1072 | int offset = 0; |
| 1073 | int32_t top = rects[0].top; |
| 1074 | |
| 1075 | for (size_t i = 0; i < count; i++) { |
| 1076 | if (top != rects[i].top) { |
| 1077 | offset ^= 0x2; |
| 1078 | top = rects[i].top; |
| 1079 | } |
| 1080 | |
| 1081 | Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom); |
| 1082 | drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)], |
| 1083 | SkXfermode::kSrcOver_Mode); |
| 1084 | } |
| 1085 | #endif |
| 1086 | } |
| 1087 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1088 | void OpenGLRenderer::dirtyLayer(const float left, const float top, |
| 1089 | const float right, const float bottom, const mat4 transform) { |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1090 | if (hasLayer()) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1091 | Rect bounds(left, top, right, bottom); |
| 1092 | transform.mapRect(bounds); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1093 | dirtyLayerUnchecked(bounds, getRegion()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1094 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | void OpenGLRenderer::dirtyLayer(const float left, const float top, |
| 1098 | const float right, const float bottom) { |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1099 | if (hasLayer()) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1100 | Rect bounds(left, top, right, bottom); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1101 | dirtyLayerUnchecked(bounds, getRegion()); |
Romain Guy | 1bd1bad | 2011-01-14 20:07:20 -0800 | [diff] [blame] | 1102 | } |
Romain Guy | 1bd1bad | 2011-01-14 20:07:20 -0800 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) { |
Romain Guy | 1bd1bad | 2011-01-14 20:07:20 -0800 | [diff] [blame] | 1106 | if (bounds.intersect(*mSnapshot->clipRect)) { |
| 1107 | bounds.snapToPixelBoundaries(); |
| 1108 | android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); |
| 1109 | if (!dirty.isEmpty()) { |
| 1110 | region->orSelf(dirty); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1111 | } |
| 1112 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1115 | void OpenGLRenderer::clearLayerRegions() { |
| 1116 | const size_t count = mLayers.size(); |
| 1117 | if (count == 0) return; |
| 1118 | |
| 1119 | if (!mSnapshot->isIgnored()) { |
| 1120 | // Doing several glScissor/glClear here can negatively impact |
| 1121 | // GPUs with a tiler architecture, instead we draw quads with |
| 1122 | // the Clear blending mode |
| 1123 | |
| 1124 | // The list contains bounds that have already been clipped |
| 1125 | // against their initial clip rect, and the current clip |
| 1126 | // is likely different so we need to disable clipping here |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1127 | bool scissorChanged = mCaches.disableScissor(); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1128 | |
| 1129 | Vertex mesh[count * 6]; |
| 1130 | Vertex* vertex = mesh; |
| 1131 | |
| 1132 | for (uint32_t i = 0; i < count; i++) { |
| 1133 | Rect* bounds = mLayers.itemAt(i); |
| 1134 | |
| 1135 | Vertex::set(vertex++, bounds->left, bounds->bottom); |
| 1136 | Vertex::set(vertex++, bounds->left, bounds->top); |
| 1137 | Vertex::set(vertex++, bounds->right, bounds->top); |
| 1138 | Vertex::set(vertex++, bounds->left, bounds->bottom); |
| 1139 | Vertex::set(vertex++, bounds->right, bounds->top); |
| 1140 | Vertex::set(vertex++, bounds->right, bounds->bottom); |
| 1141 | |
| 1142 | delete bounds; |
| 1143 | } |
| 1144 | |
| 1145 | setupDraw(false); |
| 1146 | setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f); |
| 1147 | setupDrawBlending(true, SkXfermode::kClear_Mode); |
| 1148 | setupDrawProgram(); |
| 1149 | setupDrawPureColorUniforms(); |
| 1150 | setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true); |
Romain Guy | 39d252a | 2011-12-12 18:14:06 -0800 | [diff] [blame] | 1151 | setupDrawVertices(&mesh[0].position[0]); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1152 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1153 | glDrawArrays(GL_TRIANGLES, 0, count * 6); |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1154 | |
| 1155 | if (scissorChanged) mCaches.enableScissor(); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1156 | } else { |
| 1157 | for (uint32_t i = 0; i < count; i++) { |
| 1158 | delete mLayers.itemAt(i); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | mLayers.clear(); |
| 1163 | } |
| 1164 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1165 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1166 | // Transforms |
| 1167 | /////////////////////////////////////////////////////////////////////////////// |
| 1168 | |
| 1169 | void OpenGLRenderer::translate(float dx, float dy) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 1170 | mSnapshot->transform->translate(dx, dy, 0.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | void OpenGLRenderer::rotate(float degrees) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 1174 | mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | void OpenGLRenderer::scale(float sx, float sy) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 1178 | mSnapshot->transform->scale(sx, sy, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 1181 | void OpenGLRenderer::skew(float sx, float sy) { |
| 1182 | mSnapshot->transform->skew(sx, sy); |
| 1183 | } |
| 1184 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1185 | void OpenGLRenderer::setMatrix(SkMatrix* matrix) { |
Romain Guy | e707859 | 2011-10-28 14:32:20 -0700 | [diff] [blame] | 1186 | if (matrix) { |
| 1187 | mSnapshot->transform->load(*matrix); |
| 1188 | } else { |
| 1189 | mSnapshot->transform->loadIdentity(); |
| 1190 | } |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | void OpenGLRenderer::getMatrix(SkMatrix* matrix) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 1194 | mSnapshot->transform->copyTo(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 1198 | SkMatrix transform; |
| 1199 | mSnapshot->transform->copyTo(transform); |
| 1200 | transform.preConcat(*matrix); |
| 1201 | mSnapshot->transform->load(transform); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | /////////////////////////////////////////////////////////////////////////////// |
| 1205 | // Clipping |
| 1206 | /////////////////////////////////////////////////////////////////////////////// |
| 1207 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 1208 | void OpenGLRenderer::setScissorFromClip() { |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 1209 | Rect clip(*mSnapshot->clipRect); |
| 1210 | clip.snapToPixelBoundaries(); |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 1211 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1212 | if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom, |
| 1213 | clip.getWidth(), clip.getHeight())) { |
| 1214 | mDirtyClip = false; |
| 1215 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | const Rect& OpenGLRenderer::getClipBounds() { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 1219 | return mSnapshot->getLocalClip(); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1222 | bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) { |
| 1223 | if (mSnapshot->isIgnored()) { |
| 1224 | return true; |
| 1225 | } |
| 1226 | |
| 1227 | Rect r(left, top, right, bottom); |
| 1228 | mSnapshot->transform->mapRect(r); |
| 1229 | r.snapToPixelBoundaries(); |
| 1230 | |
| 1231 | Rect clipRect(*mSnapshot->clipRect); |
| 1232 | clipRect.snapToPixelBoundaries(); |
| 1233 | |
| 1234 | return !clipRect.intersects(r); |
| 1235 | } |
| 1236 | |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 1237 | bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom, |
| 1238 | Rect& transformed, Rect& clip) { |
| 1239 | if (mSnapshot->isIgnored()) { |
| 1240 | return true; |
| 1241 | } |
| 1242 | |
| 1243 | transformed.set(left, top, right, bottom); |
| 1244 | mSnapshot->transform->mapRect(transformed); |
| 1245 | transformed.snapToPixelBoundaries(); |
| 1246 | |
| 1247 | clip.set(*mSnapshot->clipRect); |
| 1248 | clip.snapToPixelBoundaries(); |
| 1249 | |
| 1250 | return !clip.intersects(transformed); |
| 1251 | } |
| 1252 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1253 | bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom, SkPaint* paint) { |
| 1254 | if (paint->getStyle() != SkPaint::kFill_Style) { |
| 1255 | float outset = paint->getStrokeWidth() * 0.5f; |
| 1256 | return quickReject(left - outset, top - outset, right + outset, bottom + outset); |
| 1257 | } else { |
| 1258 | return quickReject(left, top, right, bottom); |
| 1259 | } |
| 1260 | } |
| 1261 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 1262 | bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { |
Chris Craik | bf09ffb | 2012-10-01 13:50:37 -0700 | [diff] [blame] | 1263 | if (mSnapshot->isIgnored() || bottom <= top || right <= left) { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 1264 | return true; |
| 1265 | } |
| 1266 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 1267 | Rect r(left, top, right, bottom); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 1268 | mSnapshot->transform->mapRect(r); |
Romain Guy | d2a1ff0 | 2010-10-14 14:46:44 -0700 | [diff] [blame] | 1269 | r.snapToPixelBoundaries(); |
| 1270 | |
| 1271 | Rect clipRect(*mSnapshot->clipRect); |
| 1272 | clipRect.snapToPixelBoundaries(); |
| 1273 | |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 1274 | bool rejected = !clipRect.intersects(r); |
| 1275 | if (!isDeferred() && !rejected) { |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 1276 | mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r)); |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | return rejected; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 1282 | bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 1283 | bool clipped = mSnapshot->clip(left, top, right, bottom, op); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 1284 | if (clipped) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1285 | dirtyClip(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 1286 | } |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 1287 | return !mSnapshot->clipRect->isEmpty(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
Chet Haase | a23eed8 | 2012-04-12 15:19:04 -0700 | [diff] [blame] | 1290 | Rect* OpenGLRenderer::getClipRect() { |
| 1291 | return mSnapshot->clipRect; |
| 1292 | } |
| 1293 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1294 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1295 | // Drawing commands |
| 1296 | /////////////////////////////////////////////////////////////////////////////// |
| 1297 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1298 | void OpenGLRenderer::setupDraw(bool clear) { |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1299 | // TODO: It would be best if we could do this before quickReject() |
| 1300 | // changes the scissor test state |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1301 | if (clear) clearLayerRegions(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1302 | if (mDirtyClip) { |
| 1303 | setScissorFromClip(); |
| 1304 | } |
| 1305 | mDescription.reset(); |
| 1306 | mSetShaderColor = false; |
| 1307 | mColorSet = false; |
| 1308 | mColorA = mColorR = mColorG = mColorB = 0.0f; |
| 1309 | mTextureUnit = 0; |
| 1310 | mTrackDirtyRegions = true; |
| 1311 | } |
| 1312 | |
| 1313 | void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) { |
| 1314 | mDescription.hasTexture = true; |
| 1315 | mDescription.hasAlpha8Texture = isAlpha8; |
| 1316 | } |
| 1317 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1318 | void OpenGLRenderer::setupDrawWithExternalTexture() { |
| 1319 | mDescription.hasExternalTexture = true; |
| 1320 | } |
| 1321 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1322 | void OpenGLRenderer::setupDrawNoTexture() { |
| 1323 | mCaches.disbaleTexCoordsVertexArray(); |
| 1324 | } |
| 1325 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1326 | void OpenGLRenderer::setupDrawAA() { |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1327 | mDescription.isAA = true; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1330 | void OpenGLRenderer::setupDrawVertexShape() { |
| 1331 | mDescription.isVertexShape = true; |
Chris Craik | 6ebdc11 | 2012-08-31 18:24:33 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1334 | void OpenGLRenderer::setupDrawPoint(float pointSize) { |
| 1335 | mDescription.isPoint = true; |
| 1336 | mDescription.pointSize = pointSize; |
| 1337 | } |
| 1338 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1339 | void OpenGLRenderer::setupDrawColor(int color) { |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1340 | setupDrawColor(color, (color >> 24) & 0xFF); |
| 1341 | } |
| 1342 | |
| 1343 | void OpenGLRenderer::setupDrawColor(int color, int alpha) { |
| 1344 | mColorA = alpha / 255.0f; |
Chet Haase | 6cfdf45 | 2011-04-22 16:42:10 -0700 | [diff] [blame] | 1345 | // Second divide of a by 255 is an optimization, allowing us to simply multiply |
| 1346 | // the rgb values by a instead of also dividing by 255 |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1347 | const float a = mColorA / 255.0f; |
Romain Guy | fa7952d | 2010-12-14 13:45:54 -0800 | [diff] [blame] | 1348 | mColorR = a * ((color >> 16) & 0xFF); |
| 1349 | mColorG = a * ((color >> 8) & 0xFF); |
| 1350 | mColorB = a * ((color ) & 0xFF); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1351 | mColorSet = true; |
| 1352 | mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA); |
| 1353 | } |
| 1354 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1355 | void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) { |
| 1356 | mColorA = alpha / 255.0f; |
Chet Haase | 6cfdf45 | 2011-04-22 16:42:10 -0700 | [diff] [blame] | 1357 | // Double-divide of a by 255 is an optimization, allowing us to simply multiply |
| 1358 | // the rgb values by a instead of also dividing by 255 |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1359 | const float a = mColorA / 255.0f; |
| 1360 | mColorR = a * ((color >> 16) & 0xFF); |
| 1361 | mColorG = a * ((color >> 8) & 0xFF); |
| 1362 | mColorB = a * ((color ) & 0xFF); |
| 1363 | mColorSet = true; |
| 1364 | mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA); |
| 1365 | } |
| 1366 | |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 1367 | void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) { |
| 1368 | mCaches.fontRenderer->describe(mDescription, paint); |
| 1369 | } |
| 1370 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1371 | void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) { |
| 1372 | mColorA = a; |
| 1373 | mColorR = r; |
| 1374 | mColorG = g; |
| 1375 | mColorB = b; |
| 1376 | mColorSet = true; |
| 1377 | mSetShaderColor = mDescription.setColor(r, g, b, a); |
| 1378 | } |
| 1379 | |
| 1380 | void OpenGLRenderer::setupDrawShader() { |
| 1381 | if (mShader) { |
| 1382 | mShader->describe(mDescription, mCaches.extensions); |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | void OpenGLRenderer::setupDrawColorFilter() { |
| 1387 | if (mColorFilter) { |
| 1388 | mColorFilter->describe(mDescription, mCaches.extensions); |
| 1389 | } |
| 1390 | } |
| 1391 | |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1392 | void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) { |
| 1393 | if (mColorSet && mode == SkXfermode::kClear_Mode) { |
| 1394 | mColorA = 1.0f; |
| 1395 | mColorR = mColorG = mColorB = 0.0f; |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1396 | mSetShaderColor = mDescription.modulate = true; |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1400 | void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) { |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1401 | // When the blending mode is kClear_Mode, we need to use a modulate color |
| 1402 | // argb=1,0,0,0 |
| 1403 | accountForClear(mode); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1404 | chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, |
| 1405 | mDescription, swapSrcDst); |
| 1406 | } |
| 1407 | |
| 1408 | void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) { |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1409 | // When the blending mode is kClear_Mode, we need to use a modulate color |
| 1410 | // argb=1,0,0,0 |
| 1411 | accountForClear(mode); |
Romain Guy | e83221c | 2012-09-24 16:01:35 -0700 | [diff] [blame] | 1412 | chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) || |
| 1413 | (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | void OpenGLRenderer::setupDrawProgram() { |
| 1417 | useProgram(mCaches.programCache.get(mDescription)); |
| 1418 | } |
| 1419 | |
| 1420 | void OpenGLRenderer::setupDrawDirtyRegionsDisabled() { |
| 1421 | mTrackDirtyRegions = false; |
| 1422 | } |
| 1423 | |
| 1424 | void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom, |
| 1425 | bool ignoreTransform) { |
| 1426 | mModelView.loadTranslate(left, top, 0.0f); |
| 1427 | if (!ignoreTransform) { |
| 1428 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
| 1429 | if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform); |
| 1430 | } else { |
| 1431 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); |
| 1432 | if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom); |
| 1433 | } |
| 1434 | } |
| 1435 | |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1436 | void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) { |
| 1437 | mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset); |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1438 | } |
| 1439 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1440 | void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom, |
| 1441 | bool ignoreTransform, bool ignoreModelView) { |
| 1442 | if (!ignoreModelView) { |
| 1443 | mModelView.loadTranslate(left, top, 0.0f); |
| 1444 | mModelView.scale(right - left, bottom - top, 1.0f); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1445 | } else { |
| 1446 | mModelView.loadIdentity(); |
| 1447 | } |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1448 | bool dirty = right - left > 0.0f && bottom - top > 0.0f; |
| 1449 | if (!ignoreTransform) { |
| 1450 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
| 1451 | if (mTrackDirtyRegions && dirty) { |
| 1452 | dirtyLayer(left, top, right, bottom, *mSnapshot->transform); |
| 1453 | } |
| 1454 | } else { |
| 1455 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); |
| 1456 | if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom); |
| 1457 | } |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1458 | } |
| 1459 | |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1460 | void OpenGLRenderer::setupDrawPointUniforms() { |
| 1461 | int slot = mCaches.currentProgram->getUniform("pointSize"); |
| 1462 | glUniform1f(slot, mDescription.pointSize); |
| 1463 | } |
| 1464 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1465 | void OpenGLRenderer::setupDrawColorUniforms() { |
Romain Guy | 55fd2c9 | 2012-03-09 17:36:01 -0800 | [diff] [blame] | 1466 | if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1467 | mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); |
| 1468 | } |
| 1469 | } |
| 1470 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1471 | void OpenGLRenderer::setupDrawPureColorUniforms() { |
Romain Guy | 5536841 | 2010-12-14 10:59:41 -0800 | [diff] [blame] | 1472 | if (mSetShaderColor) { |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1473 | mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); |
Romain Guy | 5536841 | 2010-12-14 10:59:41 -0800 | [diff] [blame] | 1474 | } |
| 1475 | } |
| 1476 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1477 | void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) { |
| 1478 | if (mShader) { |
| 1479 | if (ignoreTransform) { |
| 1480 | mModelView.loadInverse(*mSnapshot->transform); |
| 1481 | } |
| 1482 | mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit); |
| 1483 | } |
| 1484 | } |
| 1485 | |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1486 | void OpenGLRenderer::setupDrawShaderIdentityUniforms() { |
| 1487 | if (mShader) { |
| 1488 | mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit); |
| 1489 | } |
| 1490 | } |
| 1491 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1492 | void OpenGLRenderer::setupDrawColorFilterUniforms() { |
| 1493 | if (mColorFilter) { |
| 1494 | mColorFilter->setupProgram(mCaches.currentProgram); |
| 1495 | } |
| 1496 | } |
| 1497 | |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 1498 | void OpenGLRenderer::setupDrawTextGammaUniforms() { |
| 1499 | mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram); |
| 1500 | } |
| 1501 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1502 | void OpenGLRenderer::setupDrawSimpleMesh() { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1503 | bool force = mCaches.bindMeshBuffer(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1504 | mCaches.bindPositionVertexPointer(force, 0); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1505 | mCaches.unbindIndicesBuffer(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | void OpenGLRenderer::setupDrawTexture(GLuint texture) { |
| 1509 | bindTexture(texture); |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 1510 | mTextureUnit++; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1511 | mCaches.enableTexCoordsVertexArray(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1512 | } |
| 1513 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1514 | void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) { |
| 1515 | bindExternalTexture(texture); |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 1516 | mTextureUnit++; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1517 | mCaches.enableTexCoordsVertexArray(); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 1520 | void OpenGLRenderer::setupDrawTextureTransform() { |
| 1521 | mDescription.hasTextureTransform = true; |
| 1522 | } |
| 1523 | |
| 1524 | void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1525 | glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1, |
| 1526 | GL_FALSE, &transform.data[0]); |
| 1527 | } |
| 1528 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1529 | void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1530 | bool force = false; |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1531 | if (!vertices) { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1532 | force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1533 | } else { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1534 | force = mCaches.unbindMeshBuffer(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1535 | } |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 1536 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1537 | mCaches.bindPositionVertexPointer(force, vertices); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1538 | if (mCaches.currentProgram->texCoords >= 0) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1539 | mCaches.bindTexCoordsVertexPointer(force, texCoords); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | mCaches.unbindIndicesBuffer(); |
| 1543 | } |
| 1544 | |
| 1545 | void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) { |
| 1546 | bool force = mCaches.unbindMeshBuffer(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1547 | mCaches.bindPositionVertexPointer(force, vertices); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1548 | if (mCaches.currentProgram->texCoords >= 0) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1549 | mCaches.bindTexCoordsVertexPointer(force, texCoords); |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1550 | } |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1551 | } |
| 1552 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1553 | void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1554 | bool force = mCaches.unbindMeshBuffer(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1555 | mCaches.bindPositionVertexPointer(force, vertices, gVertexStride); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1556 | mCaches.unbindIndicesBuffer(); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | /** |
| 1560 | * 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] | 1561 | * outer boundary that fades out to 0. The variables set in the shader define the proportion of |
| 1562 | * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength |
| 1563 | * attributes (one per vertex) are values from zero to one that tells the fragment |
| 1564 | * shader where the fragment is in relation to the line width/length overall; these values are |
| 1565 | * then used to compute the proper color, based on whether the fragment lies in the fading AA |
| 1566 | * region of the line. |
| 1567 | * Note that we only pass down the width values in this setup function. The length coordinates |
| 1568 | * are set up for each individual segment. |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1569 | */ |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1570 | void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords, |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 1571 | GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1572 | bool force = mCaches.unbindMeshBuffer(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1573 | mCaches.bindPositionVertexPointer(force, vertices, gAAVertexStride); |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1574 | mCaches.resetTexCoordsVertexPointer(); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1575 | mCaches.unbindIndicesBuffer(); |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 1576 | |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 1577 | widthSlot = mCaches.currentProgram->getAttrib("vtxWidth"); |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1578 | glEnableVertexAttribArray(widthSlot); |
| 1579 | glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords); |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 1580 | |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 1581 | lengthSlot = mCaches.currentProgram->getAttrib("vtxLength"); |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1582 | glEnableVertexAttribArray(lengthSlot); |
| 1583 | glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords); |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 1584 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1585 | int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth"); |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1586 | glUniform1f(boundaryWidthSlot, boundaryWidthProportion); |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) { |
| 1590 | glDisableVertexAttribArray(widthSlot); |
| 1591 | glDisableVertexAttribArray(lengthSlot); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1594 | void OpenGLRenderer::finishDrawTexture() { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1598 | // Drawing |
| 1599 | /////////////////////////////////////////////////////////////////////////////// |
| 1600 | |
Chet Haase | 1271e2c | 2012-04-20 09:54:27 -0700 | [diff] [blame] | 1601 | status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 1602 | Rect& dirty, int32_t flags, uint32_t level) { |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 1603 | |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 1604 | // All the usual checks and setup operations (quickReject, setupDraw, etc.) |
| 1605 | // will be performed by the display list itself |
Romain Guy | 04c9d8c | 2011-08-25 14:01:48 -0700 | [diff] [blame] | 1606 | if (displayList && displayList->isRenderable()) { |
Chet Haase | 1271e2c | 2012-04-20 09:54:27 -0700 | [diff] [blame] | 1607 | return displayList->replay(*this, dirty, flags, level); |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 1608 | } |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 1609 | |
Romain Guy | 6554943 | 2012-03-26 16:45:05 -0700 | [diff] [blame] | 1610 | return DrawGlInfo::kStatusDone; |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 1611 | } |
| 1612 | |
Chet Haase | ed30fd8 | 2011-04-22 16:18:45 -0700 | [diff] [blame] | 1613 | void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) { |
| 1614 | if (displayList) { |
| 1615 | displayList->output(*this, level); |
| 1616 | } |
| 1617 | } |
| 1618 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1619 | void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) { |
| 1620 | int alpha; |
| 1621 | SkXfermode::Mode mode; |
| 1622 | getAlphaAndMode(paint, &alpha, &mode); |
| 1623 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1624 | float x = left; |
| 1625 | float y = top; |
| 1626 | |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 1627 | GLenum filter = GL_LINEAR; |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1628 | bool ignoreTransform = false; |
| 1629 | if (mSnapshot->transform->isPureTranslate()) { |
| 1630 | x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 1631 | y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 1632 | ignoreTransform = true; |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 1633 | filter = GL_NEAREST; |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1634 | } else { |
| 1635 | filter = FILTER(paint); |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | setupDraw(); |
| 1639 | setupDrawWithTexture(true); |
Romain Guy | 5b7a315 | 2011-03-23 17:15:38 -0700 | [diff] [blame] | 1640 | if (paint) { |
| 1641 | setupDrawAlpha8Color(paint->getColor(), alpha); |
| 1642 | } |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1643 | setupDrawColorFilter(); |
| 1644 | setupDrawShader(); |
| 1645 | setupDrawBlending(true, mode); |
| 1646 | setupDrawProgram(); |
| 1647 | setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform); |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 1648 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1649 | setupDrawTexture(texture->id); |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1650 | texture->setWrap(GL_CLAMP_TO_EDGE); |
| 1651 | texture->setFilter(filter); |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame] | 1652 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1653 | setupDrawPureColorUniforms(); |
| 1654 | setupDrawColorFilterUniforms(); |
| 1655 | setupDrawShaderUniforms(); |
| 1656 | setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); |
| 1657 | |
| 1658 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 1659 | |
| 1660 | finishDrawTexture(); |
| 1661 | } |
| 1662 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1663 | status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1664 | const float right = left + bitmap->width(); |
| 1665 | const float bottom = top + bitmap->height(); |
| 1666 | |
| 1667 | if (quickReject(left, top, right, bottom)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1668 | return DrawGlInfo::kStatusDone; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 1671 | mCaches.activeTexture(0); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1672 | Texture* texture = mCaches.textureCache.get(bitmap); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1673 | if (!texture) return DrawGlInfo::kStatusDone; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 1674 | const AutoTexture autoCleanup(texture); |
| 1675 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 1676 | if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1677 | drawAlphaBitmap(texture, left, top, paint); |
| 1678 | } else { |
| 1679 | drawTextureRect(left, top, right, bottom, texture, paint); |
| 1680 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1681 | |
| 1682 | return DrawGlInfo::kStatusDrew; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 1683 | } |
| 1684 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1685 | status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 1686 | Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); |
| 1687 | const mat4 transform(*matrix); |
| 1688 | transform.mapRect(r); |
| 1689 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1690 | if (quickReject(r.left, r.top, r.right, r.bottom)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1691 | return DrawGlInfo::kStatusDone; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1692 | } |
| 1693 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 1694 | mCaches.activeTexture(0); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1695 | Texture* texture = mCaches.textureCache.get(bitmap); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1696 | if (!texture) return DrawGlInfo::kStatusDone; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 1697 | const AutoTexture autoCleanup(texture); |
| 1698 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1699 | // This could be done in a cheaper way, all we need is pass the matrix |
| 1700 | // to the vertex shader. The save/restore is a bit overkill. |
| 1701 | save(SkCanvas::kMatrix_SaveFlag); |
| 1702 | concatMatrix(matrix); |
| 1703 | drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint); |
| 1704 | restore(); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1705 | |
| 1706 | return DrawGlInfo::kStatusDrew; |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1709 | status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) { |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 1710 | const float right = left + bitmap->width(); |
| 1711 | const float bottom = top + bitmap->height(); |
| 1712 | |
| 1713 | if (quickReject(left, top, right, bottom)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1714 | return DrawGlInfo::kStatusDone; |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | mCaches.activeTexture(0); |
| 1718 | Texture* texture = mCaches.textureCache.getTransient(bitmap); |
| 1719 | const AutoTexture autoCleanup(texture); |
| 1720 | |
| 1721 | drawTextureRect(left, top, right, bottom, texture, paint); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1722 | |
| 1723 | return DrawGlInfo::kStatusDrew; |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 1724 | } |
| 1725 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1726 | status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight, |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1727 | float* vertices, int* colors, SkPaint* paint) { |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1728 | if (!vertices || mSnapshot->isIgnored()) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1729 | return DrawGlInfo::kStatusDone; |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1730 | } |
| 1731 | |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 1732 | // TODO: We should compute the bounding box when recording the display list |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 1733 | float left = FLT_MAX; |
| 1734 | float top = FLT_MAX; |
| 1735 | float right = FLT_MIN; |
| 1736 | float bottom = FLT_MIN; |
| 1737 | |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 1738 | const uint32_t count = meshWidth * meshHeight * 6; |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 1739 | |
Romain Guy | a566b7c | 2011-01-23 16:36:11 -0800 | [diff] [blame] | 1740 | // TODO: Support the colors array |
| 1741 | TextureVertex mesh[count]; |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1742 | TextureVertex* vertex = mesh; |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 1743 | |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1744 | for (int32_t y = 0; y < meshHeight; y++) { |
| 1745 | for (int32_t x = 0; x < meshWidth; x++) { |
| 1746 | uint32_t i = (y * (meshWidth + 1) + x) * 2; |
| 1747 | |
| 1748 | float u1 = float(x) / meshWidth; |
| 1749 | float u2 = float(x + 1) / meshWidth; |
| 1750 | float v1 = float(y) / meshHeight; |
| 1751 | float v2 = float(y + 1) / meshHeight; |
| 1752 | |
| 1753 | int ax = i + (meshWidth + 1) * 2; |
| 1754 | int ay = ax + 1; |
| 1755 | int bx = i; |
| 1756 | int by = bx + 1; |
| 1757 | int cx = i + 2; |
| 1758 | int cy = cx + 1; |
| 1759 | int dx = i + (meshWidth + 1) * 2 + 2; |
| 1760 | int dy = dx + 1; |
| 1761 | |
| 1762 | TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); |
| 1763 | TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1); |
| 1764 | TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); |
| 1765 | |
| 1766 | TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); |
| 1767 | TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); |
| 1768 | TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2); |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 1769 | |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 1770 | // TODO: This could be optimized to avoid unnecessary ops |
| 1771 | left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx]))); |
| 1772 | top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy]))); |
| 1773 | right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx]))); |
| 1774 | bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy]))); |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 1778 | if (quickReject(left, top, right, bottom)) { |
| 1779 | return DrawGlInfo::kStatusDone; |
| 1780 | } |
| 1781 | |
| 1782 | mCaches.activeTexture(0); |
| 1783 | Texture* texture = mCaches.textureCache.get(bitmap); |
| 1784 | if (!texture) return DrawGlInfo::kStatusDone; |
| 1785 | const AutoTexture autoCleanup(texture); |
| 1786 | |
| 1787 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
| 1788 | texture->setFilter(FILTER(paint), true); |
| 1789 | |
| 1790 | int alpha; |
| 1791 | SkXfermode::Mode mode; |
| 1792 | getAlphaAndMode(paint, &alpha, &mode); |
| 1793 | |
| 1794 | if (hasLayer()) { |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 1795 | dirtyLayer(left, top, right, bottom, *mSnapshot->transform); |
| 1796 | } |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 1797 | |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1798 | drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f, |
| 1799 | mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0], |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 1800 | GL_TRIANGLES, count, false, false, 0, false, false); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1801 | |
| 1802 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1803 | } |
| 1804 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1805 | status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1806 | float srcLeft, float srcTop, float srcRight, float srcBottom, |
| 1807 | float dstLeft, float dstTop, float dstRight, float dstBottom, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 1808 | SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1809 | if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1810 | return DrawGlInfo::kStatusDone; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 1813 | mCaches.activeTexture(0); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1814 | Texture* texture = mCaches.textureCache.get(bitmap); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1815 | if (!texture) return DrawGlInfo::kStatusDone; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 1816 | const AutoTexture autoCleanup(texture); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1817 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1818 | const float width = texture->width; |
| 1819 | const float height = texture->height; |
| 1820 | |
Romain Guy | 6816972 | 2011-08-22 17:33:33 -0700 | [diff] [blame] | 1821 | const float u1 = fmax(0.0f, srcLeft / width); |
| 1822 | const float v1 = fmax(0.0f, srcTop / height); |
| 1823 | const float u2 = fmin(1.0f, srcRight / width); |
| 1824 | const float v2 = fmin(1.0f, srcBottom / height); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1825 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1826 | mCaches.unbindMeshBuffer(); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1827 | resetDrawTextureTexCoords(u1, v1, u2, v2); |
| 1828 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1829 | int alpha; |
| 1830 | SkXfermode::Mode mode; |
| 1831 | getAlphaAndMode(paint, &alpha, &mode); |
| 1832 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1833 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
| 1834 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 1835 | if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1836 | const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f); |
| 1837 | const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f); |
| 1838 | |
Romain Guy | b501498 | 2011-07-28 15:39:12 -0700 | [diff] [blame] | 1839 | GLenum filter = GL_NEAREST; |
Romain Guy | 631582f | 2011-08-24 11:51:35 -0700 | [diff] [blame] | 1840 | // Enable linear filtering if the source rectangle is scaled |
| 1841 | if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1842 | filter = FILTER(paint); |
Romain Guy | b501498 | 2011-07-28 15:39:12 -0700 | [diff] [blame] | 1843 | } |
Romain Guy | b501498 | 2011-07-28 15:39:12 -0700 | [diff] [blame] | 1844 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1845 | texture->setFilter(filter, true); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1846 | drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop), |
| 1847 | texture->id, alpha / 255.0f, mode, texture->blend, |
| 1848 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 1849 | GL_TRIANGLE_STRIP, gMeshCount, false, true); |
| 1850 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1851 | texture->setFilter(FILTER(paint), true); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1852 | drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f, |
| 1853 | mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 1854 | GL_TRIANGLE_STRIP, gMeshCount); |
| 1855 | } |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1856 | |
| 1857 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1858 | |
| 1859 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1862 | status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 1863 | 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] | 1864 | float left, float top, float right, float bottom, SkPaint* paint) { |
Romain Guy | be6f9dc | 2012-07-16 12:41:17 -0700 | [diff] [blame] | 1865 | int alpha; |
| 1866 | SkXfermode::Mode mode; |
| 1867 | getAlphaAndModeDirect(paint, &alpha, &mode); |
| 1868 | |
| 1869 | return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors, |
| 1870 | left, top, right, bottom, alpha, mode); |
| 1871 | } |
| 1872 | |
| 1873 | status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, |
| 1874 | const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, |
| 1875 | float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1876 | if (quickReject(left, top, right, bottom)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1877 | return DrawGlInfo::kStatusDone; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1878 | } |
| 1879 | |
Romain Guy | be6f9dc | 2012-07-16 12:41:17 -0700 | [diff] [blame] | 1880 | alpha *= mSnapshot->alpha; |
| 1881 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 1882 | mCaches.activeTexture(0); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1883 | Texture* texture = mCaches.textureCache.get(bitmap); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1884 | if (!texture) return DrawGlInfo::kStatusDone; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 1885 | const AutoTexture autoCleanup(texture); |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1886 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
| 1887 | texture->setFilter(GL_LINEAR, true); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 1888 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 1889 | const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 1890 | right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 1891 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 1892 | if (CC_LIKELY(mesh && mesh->verticesCount > 0)) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1893 | const bool pureTranslate = mSnapshot->transform->isPureTranslate(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1894 | // 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] | 1895 | if (hasLayer() && mesh->hasEmptyQuads) { |
Romain Guy | c78b5d5 | 2011-02-04 14:00:42 -0800 | [diff] [blame] | 1896 | const float offsetX = left + mSnapshot->transform->getTranslateX(); |
| 1897 | const float offsetY = top + mSnapshot->transform->getTranslateY(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1898 | const size_t count = mesh->quads.size(); |
| 1899 | for (size_t i = 0; i < count; i++) { |
Romain Guy | 8ab4079 | 2010-12-07 13:30:10 -0800 | [diff] [blame] | 1900 | const Rect& bounds = mesh->quads.itemAt(i); |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 1901 | if (CC_LIKELY(pureTranslate)) { |
Romain Guy | c78b5d5 | 2011-02-04 14:00:42 -0800 | [diff] [blame] | 1902 | const float x = (int) floorf(bounds.left + offsetX + 0.5f); |
| 1903 | const float y = (int) floorf(bounds.top + offsetY + 0.5f); |
| 1904 | dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight()); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1905 | } else { |
Romain Guy | c78b5d5 | 2011-02-04 14:00:42 -0800 | [diff] [blame] | 1906 | dirtyLayer(left + bounds.left, top + bounds.top, |
| 1907 | left + bounds.right, top + bounds.bottom, *mSnapshot->transform); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1908 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1909 | } |
| 1910 | } |
| 1911 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 1912 | if (CC_LIKELY(pureTranslate)) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1913 | const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 1914 | const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 1915 | |
| 1916 | drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f, |
| 1917 | mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, |
| 1918 | GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer, |
| 1919 | true, !mesh->hasEmptyQuads); |
| 1920 | } else { |
| 1921 | drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, |
| 1922 | mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, |
| 1923 | GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer, |
| 1924 | true, !mesh->hasEmptyQuads); |
| 1925 | } |
Romain Guy | 054dc18 | 2010-10-15 17:55:25 -0700 | [diff] [blame] | 1926 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 1927 | |
| 1928 | return DrawGlInfo::kStatusDrew; |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1931 | /** |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1932 | * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to |
| 1933 | * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in |
| 1934 | * screen space in all directions. However, instead of using a fragment shader to compute the |
| 1935 | * translucency of the color from its position, we simply use a varying parameter to define how far |
| 1936 | * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used. |
| 1937 | * |
| 1938 | * Doesn't yet support joins, caps, or path effects. |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 1939 | */ |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1940 | void OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) { |
| 1941 | int color = paint->getColor(); |
| 1942 | SkPaint::Style style = paint->getStyle(); |
| 1943 | SkXfermode::Mode mode = getXfermode(paint->getXfermode()); |
| 1944 | bool isAA = paint->isAntiAlias(); |
| 1945 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1946 | VertexBuffer vertexBuffer; |
| 1947 | // TODO: try clipping large paths to viewport |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1948 | PathRenderer::convexPathVertices(path, paint, mSnapshot->transform, vertexBuffer); |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 1949 | |
Chris Craik | bf09ffb | 2012-10-01 13:50:37 -0700 | [diff] [blame] | 1950 | if (!vertexBuffer.getSize()) { |
| 1951 | // no vertices to draw |
| 1952 | return; |
| 1953 | } |
| 1954 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1955 | setupDraw(); |
| 1956 | setupDrawNoTexture(); |
| 1957 | if (isAA) setupDrawAA(); |
| 1958 | setupDrawVertexShape(); |
| 1959 | setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha); |
| 1960 | setupDrawColorFilter(); |
| 1961 | setupDrawShader(); |
| 1962 | setupDrawBlending(isAA, mode); |
| 1963 | setupDrawProgram(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1964 | setupDrawModelViewIdentity(); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1965 | setupDrawColorUniforms(); |
| 1966 | setupDrawColorFilterUniforms(); |
| 1967 | setupDrawShaderIdentityUniforms(); |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 1968 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1969 | void* vertices = vertexBuffer.getBuffer(); |
| 1970 | bool force = mCaches.unbindMeshBuffer(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1971 | mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1972 | mCaches.resetTexCoordsVertexPointer(); |
| 1973 | mCaches.unbindIndicesBuffer(); |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 1974 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1975 | int alphaSlot = -1; |
| 1976 | if (isAA) { |
| 1977 | void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset; |
| 1978 | alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha"); |
Chris Craik | 6ebdc11 | 2012-08-31 18:24:33 -0700 | [diff] [blame] | 1979 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1980 | // TODO: avoid enable/disable in back to back uses of the alpha attribute |
Chris Craik | 6ebdc11 | 2012-08-31 18:24:33 -0700 | [diff] [blame] | 1981 | glEnableVertexAttribArray(alphaSlot); |
| 1982 | glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1983 | } |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 1984 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1985 | SkRect bounds = PathRenderer::computePathBounds(path, paint); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1986 | dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform); |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 1987 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1988 | glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize()); |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 1989 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1990 | if (isAA) { |
Chris Craik | 6ebdc11 | 2012-08-31 18:24:33 -0700 | [diff] [blame] | 1991 | glDisableVertexAttribArray(alphaSlot); |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 1992 | } |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | /** |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1996 | * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization |
| 1997 | * rules for those lines produces some unexpected results, and may vary between hardware devices. |
| 1998 | * The basics of lines-as-quads is easy; we simply find the normal to the line and position the |
| 1999 | * corners of the quads on either side of each line endpoint, separated by the strokeWidth |
| 2000 | * of the line. Hairlines are more involved because we need to account for transform scaling |
| 2001 | * to end up with a one-pixel-wide line in screen space.. |
| 2002 | * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader |
| 2003 | * in combination with values that we calculate and pass down in this method. The basic approach |
| 2004 | * is that the quad we create contains both the core line area plus a bounding area in which |
| 2005 | * the translucent/AA pixels are drawn. The values we calculate tell the shader what |
| 2006 | * proportion of the width and the length of a given segment is represented by the boundary |
| 2007 | * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad. |
| 2008 | * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel |
| 2009 | * on the inside). This ends up giving the result we want, with pixels that are completely |
| 2010 | * 'inside' the line area being filled opaquely and the other pixels being filled according to |
| 2011 | * how far into the boundary region they are, which is determined by shader interpolation. |
| 2012 | */ |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2013 | status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { |
| 2014 | if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2015 | |
| 2016 | const bool isAA = paint->isAntiAlias(); |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2017 | // We use half the stroke width here because we're going to position the quad |
| 2018 | // corner vertices half of the width away from the line endpoints |
| 2019 | float halfStrokeWidth = paint->getStrokeWidth() * 0.5f; |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2020 | // A stroke width of 0 has a special meaning in Skia: |
| 2021 | // it draws a line 1 px wide regardless of current transform |
| 2022 | bool isHairLine = paint->getStrokeWidth() == 0.0f; |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2023 | |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2024 | float inverseScaleX = 1.0f; |
| 2025 | float inverseScaleY = 1.0f; |
| 2026 | bool scaled = false; |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2027 | |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2028 | int alpha; |
| 2029 | SkXfermode::Mode mode; |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2030 | |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2031 | int generatedVerticesCount = 0; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2032 | int verticesCount = count; |
| 2033 | if (count > 4) { |
Chet Haase | c54ed96 | 2011-05-06 14:13:05 -0700 | [diff] [blame] | 2034 | // Polyline: account for extra vertices needed for continuous tri-strip |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2035 | verticesCount += (count - 4); |
| 2036 | } |
| 2037 | |
| 2038 | if (isHairLine || isAA) { |
| 2039 | // The quad that we use for AA and hairlines needs to account for scaling. For hairlines |
| 2040 | // the line on the screen should always be one pixel wide regardless of scale. For |
| 2041 | // AA lines, we only want one pixel of translucent boundary around the quad. |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2042 | if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) { |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2043 | Matrix4 *mat = mSnapshot->transform; |
| 2044 | float m00 = mat->data[Matrix4::kScaleX]; |
| 2045 | float m01 = mat->data[Matrix4::kSkewY]; |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2046 | float m10 = mat->data[Matrix4::kSkewX]; |
Chris Craik | 9147cd4 | 2012-09-06 16:44:51 -0700 | [diff] [blame] | 2047 | float m11 = mat->data[Matrix4::kScaleY]; |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2048 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2049 | float scaleX = sqrtf(m00 * m00 + m01 * m01); |
| 2050 | float scaleY = sqrtf(m10 * m10 + m11 * m11); |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2051 | |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2052 | inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0; |
| 2053 | inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0; |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2054 | |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2055 | if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) { |
| 2056 | scaled = true; |
| 2057 | } |
| 2058 | } |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2059 | } |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2060 | |
| 2061 | getAlphaAndMode(paint, &alpha, &mode); |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 2062 | |
| 2063 | mCaches.enableScissor(); |
| 2064 | |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2065 | setupDraw(); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 2066 | setupDrawNoTexture(); |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2067 | if (isAA) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2068 | setupDrawAA(); |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2069 | } |
| 2070 | setupDrawColor(paint->getColor(), alpha); |
| 2071 | setupDrawColorFilter(); |
| 2072 | setupDrawShader(); |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2073 | setupDrawBlending(isAA, mode); |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2074 | setupDrawProgram(); |
Chris Craik | b30cb10 | 2012-10-05 19:11:37 -0700 | [diff] [blame] | 2075 | setupDrawModelViewIdentity(true); |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2076 | setupDrawColorUniforms(); |
| 2077 | setupDrawColorFilterUniforms(); |
| 2078 | setupDrawShaderIdentityUniforms(); |
| 2079 | |
| 2080 | if (isHairLine) { |
| 2081 | // Set a real stroke width to be used in quad construction |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2082 | halfStrokeWidth = isAA? 1 : .5; |
| 2083 | } else if (isAA && !scaled) { |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2084 | // Expand boundary to enable AA calculations on the quad border |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2085 | halfStrokeWidth += .5f; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2086 | } |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2087 | |
| 2088 | int widthSlot; |
| 2089 | int lengthSlot; |
| 2090 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2091 | Vertex lines[verticesCount]; |
| 2092 | Vertex* vertices = &lines[0]; |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2093 | |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 2094 | AAVertex wLines[verticesCount]; |
| 2095 | AAVertex* aaVertices = &wLines[0]; |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2096 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2097 | if (CC_UNLIKELY(!isAA)) { |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2098 | setupDrawVertices(vertices); |
| 2099 | } else { |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 2100 | void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset; |
| 2101 | void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset; |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2102 | // 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] | 2103 | // AA stroke width (the base stroke width expanded by a half pixel on either side). |
| 2104 | // 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] | 2105 | // We will need to calculate the actual width proportion on each segment for |
| 2106 | // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled. |
Chris Craik | 8f5ad76 | 2012-09-04 14:35:19 -0700 | [diff] [blame] | 2107 | float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth); |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2108 | setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, |
| 2109 | boundaryWidthProportion, widthSlot, lengthSlot); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2112 | AAVertex* prevAAVertex = NULL; |
| 2113 | Vertex* prevVertex = NULL; |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 2114 | |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 2115 | int boundaryLengthSlot = -1; |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2116 | int boundaryWidthSlot = -1; |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2117 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2118 | for (int i = 0; i < count; i += 4) { |
| 2119 | // a = start point, b = end point |
| 2120 | vec2 a(points[i], points[i + 1]); |
| 2121 | vec2 b(points[i + 2], points[i + 3]); |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2122 | |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 2123 | float length = 0; |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2124 | float boundaryLengthProportion = 0; |
| 2125 | float boundaryWidthProportion = 0; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2126 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2127 | // Find the normal to the line |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2128 | vec2 n = (b - a).copyNormalized() * halfStrokeWidth; |
Chris Craik | 75040f8 | 2012-09-07 13:56:43 -0700 | [diff] [blame] | 2129 | float x = n.x; |
| 2130 | n.x = -n.y; |
| 2131 | n.y = x; |
| 2132 | |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2133 | if (isHairLine) { |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2134 | if (isAA) { |
| 2135 | float wideningFactor; |
| 2136 | if (fabs(n.x) >= fabs(n.y)) { |
| 2137 | wideningFactor = fabs(1.0f / n.x); |
| 2138 | } else { |
| 2139 | wideningFactor = fabs(1.0f / n.y); |
| 2140 | } |
| 2141 | n *= wideningFactor; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2142 | } |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2143 | |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2144 | if (scaled) { |
| 2145 | n.x *= inverseScaleX; |
| 2146 | n.y *= inverseScaleY; |
| 2147 | } |
| 2148 | } else if (scaled) { |
| 2149 | // Extend n by .5 pixel on each side, post-transform |
| 2150 | vec2 extendedN = n.copyNormalized(); |
| 2151 | extendedN /= 2; |
| 2152 | extendedN.x *= inverseScaleX; |
| 2153 | extendedN.y *= inverseScaleY; |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2154 | |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2155 | float extendedNLength = extendedN.length(); |
| 2156 | // We need to set this value on the shader prior to drawing |
Chris Craik | 75040f8 | 2012-09-07 13:56:43 -0700 | [diff] [blame] | 2157 | boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength); |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2158 | n += extendedN; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2159 | } |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2160 | |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 2161 | // aa lines expand the endpoint vertices to encompass the AA boundary |
| 2162 | if (isAA) { |
| 2163 | vec2 abVector = (b - a); |
| 2164 | length = abVector.length(); |
| 2165 | abVector.normalize(); |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2166 | |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2167 | if (scaled) { |
| 2168 | abVector.x *= inverseScaleX; |
| 2169 | abVector.y *= inverseScaleY; |
| 2170 | float abLength = abVector.length(); |
Chris Craik | 8f5ad76 | 2012-09-04 14:35:19 -0700 | [diff] [blame] | 2171 | boundaryLengthProportion = .5 - abLength / (length + abLength); |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2172 | } else { |
Chris Craik | 8f5ad76 | 2012-09-04 14:35:19 -0700 | [diff] [blame] | 2173 | boundaryLengthProportion = .5 - .5 / (length + 1); |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2174 | } |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2175 | |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2176 | abVector /= 2; |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 2177 | a -= abVector; |
| 2178 | b += abVector; |
| 2179 | } |
| 2180 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2181 | // Four corners of the rectangle defining a thick line |
| 2182 | vec2 p1 = a - n; |
| 2183 | vec2 p2 = a + n; |
| 2184 | vec2 p3 = b + n; |
| 2185 | vec2 p4 = b - n; |
| 2186 | |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 2187 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2188 | const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x))); |
| 2189 | const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x))); |
| 2190 | const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y))); |
| 2191 | const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y))); |
| 2192 | |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 2193 | if (!quickRejectNoScissor(left, top, right, bottom)) { |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2194 | if (!isAA) { |
| 2195 | if (prevVertex != NULL) { |
| 2196 | // Issue two repeat vertices to create degenerate triangles to bridge |
| 2197 | // between the previous line and the new one. This is necessary because |
| 2198 | // we are creating a single triangle_strip which will contain |
| 2199 | // potentially discontinuous line segments. |
| 2200 | Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]); |
| 2201 | Vertex::set(vertices++, p1.x, p1.y); |
| 2202 | generatedVerticesCount += 2; |
| 2203 | } |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2204 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2205 | Vertex::set(vertices++, p1.x, p1.y); |
| 2206 | Vertex::set(vertices++, p2.x, p2.y); |
| 2207 | Vertex::set(vertices++, p4.x, p4.y); |
| 2208 | Vertex::set(vertices++, p3.x, p3.y); |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2209 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2210 | prevVertex = vertices - 1; |
| 2211 | generatedVerticesCount += 4; |
| 2212 | } else { |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2213 | if (!isHairLine && scaled) { |
| 2214 | // Must set width proportions per-segment for scaled non-hairlines to use the |
| 2215 | // correct AA boundary dimensions |
| 2216 | if (boundaryWidthSlot < 0) { |
| 2217 | boundaryWidthSlot = |
| 2218 | mCaches.currentProgram->getUniform("boundaryWidth"); |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2219 | } |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2220 | |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2221 | glUniform1f(boundaryWidthSlot, boundaryWidthProportion); |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2222 | } |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2223 | |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 2224 | if (boundaryLengthSlot < 0) { |
| 2225 | boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength"); |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 2226 | } |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2227 | |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2228 | glUniform1f(boundaryLengthSlot, boundaryLengthProportion); |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 2229 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2230 | if (prevAAVertex != NULL) { |
| 2231 | // Issue two repeat vertices to create degenerate triangles to bridge |
| 2232 | // between the previous line and the new one. This is necessary because |
| 2233 | // we are creating a single triangle_strip which will contain |
| 2234 | // potentially discontinuous line segments. |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 2235 | AAVertex::set(aaVertices++,prevAAVertex->position[0], |
| 2236 | prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length); |
| 2237 | AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2238 | generatedVerticesCount += 2; |
| 2239 | } |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2240 | |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 2241 | AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); |
| 2242 | AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0); |
| 2243 | AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1); |
| 2244 | AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0); |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2245 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2246 | prevAAVertex = aaVertices - 1; |
| 2247 | generatedVerticesCount += 4; |
| 2248 | } |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2249 | |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 2250 | dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top, |
| 2251 | a.x == b.x ? right: right, a.y == b.y ? bottom: bottom, |
| 2252 | *mSnapshot->transform); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2253 | } |
| 2254 | } |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2255 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2256 | if (generatedVerticesCount > 0) { |
| 2257 | glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount); |
| 2258 | } |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2259 | |
| 2260 | if (isAA) { |
| 2261 | finishDrawAALine(widthSlot, lengthSlot); |
| 2262 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2263 | |
| 2264 | return DrawGlInfo::kStatusDrew; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2265 | } |
| 2266 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2267 | status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) { |
| 2268 | if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2269 | |
| 2270 | // TODO: The paint's cap style defines whether the points are square or circular |
| 2271 | // TODO: Handle AA for round points |
| 2272 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2273 | // A stroke width of 0 has a special meaning in Skia: |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2274 | // it draws an unscaled 1px point |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2275 | float strokeWidth = paint->getStrokeWidth(); |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2276 | const bool isHairLine = paint->getStrokeWidth() == 0.0f; |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2277 | if (isHairLine) { |
| 2278 | // Now that we know it's hairline, we can set the effective width, to be used later |
| 2279 | strokeWidth = 1.0f; |
| 2280 | } |
| 2281 | const float halfWidth = strokeWidth / 2; |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2282 | int alpha; |
| 2283 | SkXfermode::Mode mode; |
| 2284 | getAlphaAndMode(paint, &alpha, &mode); |
| 2285 | |
| 2286 | int verticesCount = count >> 1; |
| 2287 | int generatedVerticesCount = 0; |
| 2288 | |
| 2289 | TextureVertex pointsData[verticesCount]; |
| 2290 | TextureVertex* vertex = &pointsData[0]; |
| 2291 | |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 2292 | // TODO: We should optimize this method to not generate vertices for points |
| 2293 | // that lie outside of the clip. |
| 2294 | mCaches.enableScissor(); |
| 2295 | |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2296 | setupDraw(); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 2297 | setupDrawNoTexture(); |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2298 | setupDrawPoint(strokeWidth); |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2299 | setupDrawColor(paint->getColor(), alpha); |
| 2300 | setupDrawColorFilter(); |
| 2301 | setupDrawShader(); |
| 2302 | setupDrawBlending(mode); |
| 2303 | setupDrawProgram(); |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2304 | setupDrawModelViewIdentity(true); |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2305 | setupDrawColorUniforms(); |
| 2306 | setupDrawColorFilterUniforms(); |
| 2307 | setupDrawPointUniforms(); |
| 2308 | setupDrawShaderIdentityUniforms(); |
| 2309 | setupDrawMesh(vertex); |
| 2310 | |
| 2311 | for (int i = 0; i < count; i += 2) { |
| 2312 | TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f); |
| 2313 | generatedVerticesCount++; |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2314 | |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2315 | float left = points[i] - halfWidth; |
| 2316 | float right = points[i] + halfWidth; |
| 2317 | float top = points[i + 1] - halfWidth; |
| 2318 | float bottom = points [i + 1] + halfWidth; |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2319 | |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2320 | dirtyLayer(left, top, right, bottom, *mSnapshot->transform); |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
| 2323 | glDrawArrays(GL_POINTS, 0, generatedVerticesCount); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2324 | |
| 2325 | return DrawGlInfo::kStatusDrew; |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2326 | } |
| 2327 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2328 | status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 2329 | // No need to check against the clip, we fill the clip region |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2330 | if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 2331 | |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 2332 | Rect& clip(*mSnapshot->clipRect); |
| 2333 | clip.snapToPixelBoundaries(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 2334 | |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 2335 | drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2336 | |
| 2337 | return DrawGlInfo::kStatusDrew; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 2338 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 2339 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2340 | status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, |
| 2341 | SkPaint* paint) { |
| 2342 | if (!texture) return DrawGlInfo::kStatusDone; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2343 | const AutoTexture autoCleanup(texture); |
| 2344 | |
| 2345 | const float x = left + texture->left - texture->offset; |
| 2346 | const float y = top + texture->top - texture->offset; |
| 2347 | |
| 2348 | drawPathTexture(texture, x, y, paint); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2349 | |
| 2350 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2351 | } |
| 2352 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2353 | status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom, |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2354 | float rx, float ry, SkPaint* p) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2355 | if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2356 | return DrawGlInfo::kStatusDone; |
| 2357 | } |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 2358 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2359 | if (p->getPathEffect() != 0) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2360 | mCaches.activeTexture(0); |
| 2361 | const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect( |
| 2362 | right - left, bottom - top, rx, ry, p); |
| 2363 | return drawShape(left, top, texture, p); |
| 2364 | } |
| 2365 | |
| 2366 | SkPath path; |
| 2367 | SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2368 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2369 | float outset = p->getStrokeWidth() / 2; |
| 2370 | rect.outset(outset, outset); |
| 2371 | rx += outset; |
| 2372 | ry += outset; |
| 2373 | } |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2374 | path.addRoundRect(rect, rx, ry); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2375 | drawConvexPath(path, p); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2376 | |
| 2377 | return DrawGlInfo::kStatusDrew; |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 2378 | } |
| 2379 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2380 | status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2381 | if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius, |
| 2382 | x + radius, y + radius, p)) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2383 | return DrawGlInfo::kStatusDone; |
| 2384 | } |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2385 | if (p->getPathEffect() != 0) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2386 | mCaches.activeTexture(0); |
| 2387 | const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p); |
| 2388 | return drawShape(x - radius, y - radius, texture, p); |
| 2389 | } |
| 2390 | |
| 2391 | SkPath path; |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2392 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2393 | path.addCircle(x, y, radius + p->getStrokeWidth() / 2); |
| 2394 | } else { |
| 2395 | path.addCircle(x, y, radius); |
| 2396 | } |
| 2397 | drawConvexPath(path, p); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2398 | |
| 2399 | return DrawGlInfo::kStatusDrew; |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 2400 | } |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2401 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2402 | status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom, |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2403 | SkPaint* p) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2404 | if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2405 | return DrawGlInfo::kStatusDone; |
| 2406 | } |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2407 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2408 | if (p->getPathEffect() != 0) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2409 | mCaches.activeTexture(0); |
| 2410 | const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p); |
| 2411 | return drawShape(left, top, texture, p); |
| 2412 | } |
| 2413 | |
| 2414 | SkPath path; |
| 2415 | SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2416 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2417 | rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2); |
| 2418 | } |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2419 | path.addOval(rect); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2420 | drawConvexPath(path, p); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2421 | |
| 2422 | return DrawGlInfo::kStatusDrew; |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 2423 | } |
| 2424 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2425 | status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom, |
Chris Craik | 780c128 | 2012-10-04 14:10:49 -0700 | [diff] [blame] | 2426 | float startAngle, float sweepAngle, bool useCenter, SkPaint* p) { |
| 2427 | if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) { |
| 2428 | return DrawGlInfo::kStatusDone; |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 2429 | } |
| 2430 | |
Chris Craik | 780c128 | 2012-10-04 14:10:49 -0700 | [diff] [blame] | 2431 | if (fabs(sweepAngle) >= 360.0f) { |
| 2432 | return drawOval(left, top, right, bottom, p); |
| 2433 | } |
| 2434 | |
| 2435 | // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180) |
Chris Craik | 7fae521 | 2012-11-01 11:27:03 -0700 | [diff] [blame] | 2436 | if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || p->getStrokeCap() != SkPaint::kButt_Cap || useCenter) { |
Chris Craik | 780c128 | 2012-10-04 14:10:49 -0700 | [diff] [blame] | 2437 | mCaches.activeTexture(0); |
| 2438 | const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top, |
| 2439 | startAngle, sweepAngle, useCenter, p); |
| 2440 | return drawShape(left, top, texture, p); |
| 2441 | } |
| 2442 | |
| 2443 | SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); |
| 2444 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2445 | rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2); |
| 2446 | } |
| 2447 | |
| 2448 | SkPath path; |
| 2449 | if (useCenter) { |
| 2450 | path.moveTo(rect.centerX(), rect.centerY()); |
| 2451 | } |
| 2452 | path.arcTo(rect, startAngle, sweepAngle, !useCenter); |
| 2453 | if (useCenter) { |
| 2454 | path.close(); |
| 2455 | } |
| 2456 | drawConvexPath(path, p); |
| 2457 | |
| 2458 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 2459 | } |
| 2460 | |
Romain Guy | cf8675e | 2012-10-02 12:32:25 -0700 | [diff] [blame] | 2461 | // See SkPaintDefaults.h |
| 2462 | #define SkPaintDefaults_MiterLimit SkIntToScalar(4) |
| 2463 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2464 | status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2465 | if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2466 | return DrawGlInfo::kStatusDone; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2467 | } |
| 2468 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2469 | if (p->getStyle() != SkPaint::kFill_Style) { |
Romain Guy | cf8675e | 2012-10-02 12:32:25 -0700 | [diff] [blame] | 2470 | // only fill style is supported by drawConvexPath, since others have to handle joins |
| 2471 | if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join || |
| 2472 | p->getStrokeMiter() != SkPaintDefaults_MiterLimit) { |
| 2473 | mCaches.activeTexture(0); |
| 2474 | const PathTexture* texture = |
| 2475 | mCaches.rectShapeCache.getRect(right - left, bottom - top, p); |
| 2476 | return drawShape(left, top, texture, p); |
| 2477 | } |
| 2478 | |
| 2479 | SkPath path; |
| 2480 | SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); |
| 2481 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2482 | rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2); |
| 2483 | } |
| 2484 | path.addRect(rect); |
| 2485 | drawConvexPath(path, p); |
| 2486 | |
| 2487 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 2488 | } |
| 2489 | |
Romain Guy | 181d0a6 | 2011-06-09 18:52:38 -0700 | [diff] [blame] | 2490 | if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2491 | SkPath path; |
| 2492 | path.addRect(left, top, right, bottom); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2493 | drawConvexPath(path, p); |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 2494 | } else { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2495 | drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode())); |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 2496 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2497 | |
| 2498 | return DrawGlInfo::kStatusDrew; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 2499 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 2500 | |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2501 | void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count, |
| 2502 | const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode, |
| 2503 | float x, float y) { |
| 2504 | mCaches.activeTexture(0); |
| 2505 | |
| 2506 | // NOTE: The drop shadow will not perform gamma correction |
| 2507 | // if shader-based correction is enabled |
| 2508 | mCaches.dropShadowCache.setFontRenderer(fontRenderer); |
| 2509 | const ShadowTexture* shadow = mCaches.dropShadowCache.get( |
| 2510 | paint, text, bytesCount, count, mShadowRadius, positions); |
| 2511 | const AutoTexture autoCleanup(shadow); |
| 2512 | |
| 2513 | const float sx = x - shadow->left + mShadowDx; |
| 2514 | const float sy = y - shadow->top + mShadowDy; |
| 2515 | |
| 2516 | const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha; |
| 2517 | int shadowColor = mShadowColor; |
| 2518 | if (mShader) { |
| 2519 | shadowColor = 0xffffffff; |
| 2520 | } |
| 2521 | |
| 2522 | setupDraw(); |
| 2523 | setupDrawWithTexture(true); |
| 2524 | setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha); |
| 2525 | setupDrawColorFilter(); |
| 2526 | setupDrawShader(); |
| 2527 | setupDrawBlending(true, mode); |
| 2528 | setupDrawProgram(); |
| 2529 | setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height); |
| 2530 | setupDrawTexture(shadow->id); |
| 2531 | setupDrawPureColorUniforms(); |
| 2532 | setupDrawColorFilterUniforms(); |
| 2533 | setupDrawShaderUniforms(); |
| 2534 | setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); |
| 2535 | |
| 2536 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 2537 | } |
| 2538 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2539 | status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count, |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 2540 | const float* positions, SkPaint* paint) { |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2541 | if (text == NULL || count == 0 || mSnapshot->isIgnored() || |
Romain Guy | 9c0b188 | 2012-07-13 12:13:07 -0700 | [diff] [blame] | 2542 | (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2543 | return DrawGlInfo::kStatusDone; |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 2544 | } |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 2545 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2546 | // NOTE: Skia does not support perspective transform on drawPosText yet |
| 2547 | if (!mSnapshot->transform->isSimple()) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2548 | return DrawGlInfo::kStatusDone; |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | float x = 0.0f; |
| 2552 | float y = 0.0f; |
| 2553 | const bool pureTranslate = mSnapshot->transform->isPureTranslate(); |
| 2554 | if (pureTranslate) { |
| 2555 | x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); |
| 2556 | y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); |
| 2557 | } |
| 2558 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 2559 | FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2560 | fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), |
| 2561 | paint->getTextSize()); |
| 2562 | |
| 2563 | int alpha; |
| 2564 | SkXfermode::Mode mode; |
| 2565 | getAlphaAndMode(paint, &alpha, &mode); |
| 2566 | |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2567 | if (CC_UNLIKELY(mHasShadow)) { |
| 2568 | drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode, |
| 2569 | 0.0f, 0.0f); |
| 2570 | } |
| 2571 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2572 | // Pick the appropriate texture filtering |
| 2573 | bool linearFilter = mSnapshot->transform->changesBounds(); |
| 2574 | if (pureTranslate && !linearFilter) { |
| 2575 | linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; |
| 2576 | } |
| 2577 | |
| 2578 | mCaches.activeTexture(0); |
| 2579 | setupDraw(); |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 2580 | setupDrawTextGamma(paint); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2581 | setupDrawDirtyRegionsDisabled(); |
| 2582 | setupDrawWithTexture(true); |
| 2583 | setupDrawAlpha8Color(paint->getColor(), alpha); |
| 2584 | setupDrawColorFilter(); |
| 2585 | setupDrawShader(); |
| 2586 | setupDrawBlending(true, mode); |
| 2587 | setupDrawProgram(); |
| 2588 | setupDrawModelView(x, y, x, y, pureTranslate, true); |
| 2589 | setupDrawTexture(fontRenderer.getTexture(linearFilter)); |
| 2590 | setupDrawPureColorUniforms(); |
| 2591 | setupDrawColorFilterUniforms(); |
| 2592 | setupDrawShaderUniforms(pureTranslate); |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 2593 | setupDrawTextGammaUniforms(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2594 | |
| 2595 | const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); |
| 2596 | Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); |
| 2597 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2598 | const bool hasActiveLayer = hasLayer(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2599 | |
| 2600 | if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y, |
| 2601 | positions, hasActiveLayer ? &bounds : NULL)) { |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2602 | if (hasActiveLayer) { |
| 2603 | if (!pureTranslate) { |
| 2604 | mSnapshot->transform->mapRect(bounds); |
| 2605 | } |
| 2606 | dirtyLayerUnchecked(bounds, getRegion()); |
| 2607 | } |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2608 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2609 | |
| 2610 | return DrawGlInfo::kStatusDrew; |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 2611 | } |
| 2612 | |
Romain Guy | c252595 | 2012-07-27 16:41:22 -0700 | [diff] [blame] | 2613 | status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, |
Raph Levien | 996e57c | 2012-07-23 15:22:52 -0700 | [diff] [blame] | 2614 | float x, float y, const float* positions, SkPaint* paint, float length) { |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2615 | if (text == NULL || count == 0 || mSnapshot->isIgnored() || |
Romain Guy | 9c0b188 | 2012-07-13 12:13:07 -0700 | [diff] [blame] | 2616 | (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2617 | return DrawGlInfo::kStatusDone; |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 2618 | } |
| 2619 | |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 2620 | if (length < 0.0f) length = paint->measureText(text, bytesCount); |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 2621 | switch (paint->getTextAlign()) { |
| 2622 | case SkPaint::kCenter_Align: |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 2623 | x -= length / 2.0f; |
| 2624 | break; |
| 2625 | case SkPaint::kRight_Align: |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 2626 | x -= length; |
| 2627 | break; |
| 2628 | default: |
| 2629 | break; |
| 2630 | } |
| 2631 | |
Romain Guy | cac5fd3 | 2011-12-01 20:08:50 -0800 | [diff] [blame] | 2632 | SkPaint::FontMetrics metrics; |
| 2633 | paint->getFontMetrics(&metrics, 0.0f); |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 2634 | if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2635 | return DrawGlInfo::kStatusDone; |
Romain Guy | cac5fd3 | 2011-12-01 20:08:50 -0800 | [diff] [blame] | 2636 | } |
| 2637 | |
Romain Guy | 3a3fa1b | 2010-12-06 18:47:50 -0800 | [diff] [blame] | 2638 | const float oldX = x; |
| 2639 | const float oldY = y; |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2640 | const bool pureTranslate = mSnapshot->transform->isPureTranslate(); |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2641 | if (CC_LIKELY(pureTranslate)) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2642 | x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); |
| 2643 | y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); |
| 2644 | } |
| 2645 | |
Fabrice Di Meglio | ef9bb3c | 2011-10-17 11:06:46 -0700 | [diff] [blame] | 2646 | #if DEBUG_GLYPHS |
Romain Guy | c252595 | 2012-07-27 16:41:22 -0700 | [diff] [blame] | 2647 | ALOGD("OpenGLRenderer drawText() with FontID=%d", |
Raph Levien | 996e57c | 2012-07-23 15:22:52 -0700 | [diff] [blame] | 2648 | SkTypeface::UniqueID(paint->getTypeface())); |
Fabrice Di Meglio | ef9bb3c | 2011-10-17 11:06:46 -0700 | [diff] [blame] | 2649 | #endif |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 2650 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 2651 | FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 2652 | fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 2653 | paint->getTextSize()); |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 2654 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2655 | int alpha; |
| 2656 | SkXfermode::Mode mode; |
| 2657 | getAlphaAndMode(paint, &alpha, &mode); |
Romain Guy | 9d13fe25 | 2010-10-15 16:06:03 -0700 | [diff] [blame] | 2658 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2659 | if (CC_UNLIKELY(mHasShadow)) { |
Raph Levien | 996e57c | 2012-07-23 15:22:52 -0700 | [diff] [blame] | 2660 | drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode, |
| 2661 | oldX, oldY); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 2662 | } |
| 2663 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2664 | // Pick the appropriate texture filtering |
| 2665 | bool linearFilter = mSnapshot->transform->changesBounds(); |
| 2666 | if (pureTranslate && !linearFilter) { |
| 2667 | linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; |
| 2668 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2669 | |
Romain Guy | 16c8808 | 2012-06-11 16:03:47 -0700 | [diff] [blame] | 2670 | // The font renderer will always use texture unit 0 |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 2671 | mCaches.activeTexture(0); |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2672 | setupDraw(); |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 2673 | setupDrawTextGamma(paint); |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2674 | setupDrawDirtyRegionsDisabled(); |
| 2675 | setupDrawWithTexture(true); |
| 2676 | setupDrawAlpha8Color(paint->getColor(), alpha); |
| 2677 | setupDrawColorFilter(); |
| 2678 | setupDrawShader(); |
| 2679 | setupDrawBlending(true, mode); |
| 2680 | setupDrawProgram(); |
| 2681 | setupDrawModelView(x, y, x, y, pureTranslate, true); |
Romain Guy | 16c8808 | 2012-06-11 16:03:47 -0700 | [diff] [blame] | 2682 | // See comment above; the font renderer must use texture unit 0 |
| 2683 | // assert(mTextureUnit == 0) |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2684 | setupDrawTexture(fontRenderer.getTexture(linearFilter)); |
| 2685 | setupDrawPureColorUniforms(); |
| 2686 | setupDrawColorFilterUniforms(); |
| 2687 | setupDrawShaderUniforms(pureTranslate); |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 2688 | setupDrawTextGammaUniforms(); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 2689 | |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 2690 | const Rect* clip = pureTranslate ? mSnapshot->clipRect : |
| 2691 | (mSnapshot->hasPerspectiveTransform() ? NULL : &mSnapshot->getLocalClip()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2692 | Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); |
| 2693 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2694 | const bool hasActiveLayer = hasLayer(); |
Alex Sakhartchouk | 894df17 | 2011-02-17 16:45:37 -0800 | [diff] [blame] | 2695 | |
Raph Levien | 996e57c | 2012-07-23 15:22:52 -0700 | [diff] [blame] | 2696 | bool status; |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 2697 | if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) { |
Raph Levien | 8b4072d | 2012-07-30 15:50:00 -0700 | [diff] [blame] | 2698 | SkPaint paintCopy(*paint); |
| 2699 | paintCopy.setTextAlign(SkPaint::kLeft_Align); |
| 2700 | status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y, |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 2701 | positions, hasActiveLayer ? &bounds : NULL); |
Raph Levien | 996e57c | 2012-07-23 15:22:52 -0700 | [diff] [blame] | 2702 | } else { |
Raph Levien | 8b4072d | 2012-07-30 15:50:00 -0700 | [diff] [blame] | 2703 | status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y, |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 2704 | positions, hasActiveLayer ? &bounds : NULL); |
Raph Levien | 996e57c | 2012-07-23 15:22:52 -0700 | [diff] [blame] | 2705 | } |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 2706 | |
| 2707 | if (status && hasActiveLayer) { |
| 2708 | if (!pureTranslate) { |
| 2709 | mSnapshot->transform->mapRect(bounds); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2710 | } |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 2711 | dirtyLayerUnchecked(bounds, getRegion()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2712 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 2713 | |
Romain Guy | 3a3fa1b | 2010-12-06 18:47:50 -0800 | [diff] [blame] | 2714 | drawTextDecorations(text, bytesCount, length, oldX, oldY, paint); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2715 | |
| 2716 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2719 | status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path, |
Romain Guy | 325740f | 2012-02-24 16:48:34 -0800 | [diff] [blame] | 2720 | float hOffset, float vOffset, SkPaint* paint) { |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 2721 | if (text == NULL || count == 0 || mSnapshot->isIgnored() || |
| 2722 | (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2723 | return DrawGlInfo::kStatusDone; |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 2724 | } |
| 2725 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 2726 | FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint); |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 2727 | fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), |
| 2728 | paint->getTextSize()); |
| 2729 | |
| 2730 | int alpha; |
| 2731 | SkXfermode::Mode mode; |
| 2732 | getAlphaAndMode(paint, &alpha, &mode); |
| 2733 | |
| 2734 | mCaches.activeTexture(0); |
| 2735 | setupDraw(); |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 2736 | setupDrawTextGamma(paint); |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 2737 | setupDrawDirtyRegionsDisabled(); |
| 2738 | setupDrawWithTexture(true); |
| 2739 | setupDrawAlpha8Color(paint->getColor(), alpha); |
| 2740 | setupDrawColorFilter(); |
| 2741 | setupDrawShader(); |
| 2742 | setupDrawBlending(true, mode); |
| 2743 | setupDrawProgram(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 2744 | setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true); |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 2745 | setupDrawTexture(fontRenderer.getTexture(true)); |
| 2746 | setupDrawPureColorUniforms(); |
| 2747 | setupDrawColorFilterUniforms(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 2748 | setupDrawShaderUniforms(false); |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 2749 | setupDrawTextGammaUniforms(); |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 2750 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 2751 | const Rect* clip = &mSnapshot->getLocalClip(); |
| 2752 | Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 2753 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 2754 | const bool hasActiveLayer = hasLayer(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 2755 | |
| 2756 | if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path, |
| 2757 | hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) { |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 2758 | if (hasActiveLayer) { |
| 2759 | mSnapshot->transform->mapRect(bounds); |
| 2760 | dirtyLayerUnchecked(bounds, getRegion()); |
| 2761 | } |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 2762 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2763 | |
| 2764 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 325740f | 2012-02-24 16:48:34 -0800 | [diff] [blame] | 2765 | } |
| 2766 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2767 | status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { |
| 2768 | if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 2769 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 2770 | mCaches.activeTexture(0); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 2771 | |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 2772 | // TODO: Perform early clip test before we rasterize the path |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 2773 | const PathTexture* texture = mCaches.pathCache.get(path, paint); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2774 | if (!texture) return DrawGlInfo::kStatusDone; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 2775 | const AutoTexture autoCleanup(texture); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 2776 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 2777 | const float x = texture->left - texture->offset; |
| 2778 | const float y = texture->top - texture->offset; |
| 2779 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2780 | drawPathTexture(texture, x, y, paint); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2781 | |
| 2782 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 2783 | } |
| 2784 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2785 | status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) { |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 2786 | if (!layer) { |
| 2787 | return DrawGlInfo::kStatusDone; |
| 2788 | } |
| 2789 | |
Romain Guy | b2e2f24 | 2012-10-17 18:18:35 -0700 | [diff] [blame] | 2790 | mat4* transform = NULL; |
| 2791 | if (layer->isTextureLayer()) { |
| 2792 | transform = &layer->getTransform(); |
| 2793 | if (!transform->isIdentity()) { |
| 2794 | save(0); |
| 2795 | mSnapshot->transform->multiply(*transform); |
| 2796 | } |
| 2797 | } |
| 2798 | |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 2799 | Rect transformed; |
| 2800 | Rect clip; |
| 2801 | const bool rejected = quickRejectNoScissor(x, y, |
| 2802 | x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip); |
| 2803 | |
| 2804 | if (rejected) { |
Romain Guy | b2e2f24 | 2012-10-17 18:18:35 -0700 | [diff] [blame] | 2805 | if (transform && !transform->isIdentity()) { |
| 2806 | restore(); |
| 2807 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2808 | return DrawGlInfo::kStatusDone; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 2809 | } |
| 2810 | |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 2811 | bool debugLayerUpdate = false; |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 2812 | if (updateLayer(layer, true)) { |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 2813 | debugLayerUpdate = mCaches.debugLayersUpdates; |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 2814 | } |
| 2815 | |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 2816 | mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed)); |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 2817 | mCaches.activeTexture(0); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 2818 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2819 | if (CC_LIKELY(!layer->region.isEmpty())) { |
Romain Guy | e529ece | 2012-09-26 11:23:17 -0700 | [diff] [blame] | 2820 | SkiaColorFilter* oldFilter = mColorFilter; |
| 2821 | mColorFilter = layer->getColorFilter(); |
| 2822 | |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2823 | if (layer->region.isRect()) { |
Romain Guy | 4066767 | 2011-03-18 14:34:03 -0700 | [diff] [blame] | 2824 | composeLayerRect(layer, layer->regionRect); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2825 | } else if (layer->mesh) { |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 2826 | const float a = layer->getAlpha() / 255.0f; |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2827 | setupDraw(); |
| 2828 | setupDrawWithTexture(); |
Romain Guy | 8168396 | 2011-01-24 20:40:18 -0800 | [diff] [blame] | 2829 | setupDrawColor(a, a, a, a); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2830 | setupDrawColorFilter(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 2831 | setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2832 | setupDrawProgram(); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2833 | setupDrawPureColorUniforms(); |
| 2834 | setupDrawColorFilterUniforms(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 2835 | setupDrawTexture(layer->getTexture()); |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2836 | if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) { |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 2837 | int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); |
| 2838 | int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 2839 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 2840 | layer->setFilter(GL_NEAREST); |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 2841 | setupDrawModelViewTranslate(tx, ty, |
| 2842 | tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 2843 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 2844 | layer->setFilter(GL_LINEAR); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 2845 | setupDrawModelViewTranslate(x, y, |
| 2846 | x + layer->layer.getWidth(), y + layer->layer.getHeight()); |
| 2847 | } |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2848 | setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2849 | |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2850 | glDrawElements(GL_TRIANGLES, layer->meshElementCount, |
| 2851 | GL_UNSIGNED_SHORT, layer->meshIndices); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2852 | |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2853 | finishDrawTexture(); |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 2854 | |
| 2855 | #if DEBUG_LAYERS_AS_REGIONS |
| 2856 | drawRegionRects(layer->region); |
| 2857 | #endif |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2858 | } |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 2859 | |
Romain Guy | e529ece | 2012-09-26 11:23:17 -0700 | [diff] [blame] | 2860 | mColorFilter = oldFilter; |
| 2861 | |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 2862 | if (debugLayerUpdate) { |
| 2863 | drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), |
| 2864 | 0x7f00ff00, SkXfermode::kSrcOver_Mode); |
| 2865 | } |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2866 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2867 | |
Romain Guy | b2e2f24 | 2012-10-17 18:18:35 -0700 | [diff] [blame] | 2868 | if (transform && !transform->isIdentity()) { |
| 2869 | restore(); |
| 2870 | } |
| 2871 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2872 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 2873 | } |
| 2874 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2875 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 2876 | // Shaders |
| 2877 | /////////////////////////////////////////////////////////////////////////////// |
| 2878 | |
| 2879 | void OpenGLRenderer::resetShader() { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 2880 | mShader = NULL; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 2881 | } |
| 2882 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 2883 | void OpenGLRenderer::setupShader(SkiaShader* shader) { |
| 2884 | mShader = shader; |
| 2885 | if (mShader) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 2886 | mShader->set(&mCaches.textureCache, &mCaches.gradientCache); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 2887 | } |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 2888 | } |
| 2889 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 2890 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 2891 | // Color filters |
| 2892 | /////////////////////////////////////////////////////////////////////////////// |
| 2893 | |
| 2894 | void OpenGLRenderer::resetColorFilter() { |
| 2895 | mColorFilter = NULL; |
| 2896 | } |
| 2897 | |
| 2898 | void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { |
| 2899 | mColorFilter = filter; |
| 2900 | } |
| 2901 | |
| 2902 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 2903 | // Drop shadow |
| 2904 | /////////////////////////////////////////////////////////////////////////////// |
| 2905 | |
| 2906 | void OpenGLRenderer::resetShadow() { |
| 2907 | mHasShadow = false; |
| 2908 | } |
| 2909 | |
| 2910 | void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { |
| 2911 | mHasShadow = true; |
| 2912 | mShadowRadius = radius; |
| 2913 | mShadowDx = dx; |
| 2914 | mShadowDy = dy; |
| 2915 | mShadowColor = color; |
| 2916 | } |
| 2917 | |
| 2918 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 2919 | // Draw filters |
| 2920 | /////////////////////////////////////////////////////////////////////////////// |
| 2921 | |
| 2922 | void OpenGLRenderer::resetPaintFilter() { |
| 2923 | mHasDrawFilter = false; |
| 2924 | } |
| 2925 | |
| 2926 | void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) { |
| 2927 | mHasDrawFilter = true; |
| 2928 | mPaintFilterClearBits = clearBits & SkPaint::kAllFlags; |
| 2929 | mPaintFilterSetBits = setBits & SkPaint::kAllFlags; |
| 2930 | } |
| 2931 | |
| 2932 | SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) { |
Romain Guy | dd7c8e4c | 2012-03-01 12:08:38 -0800 | [diff] [blame] | 2933 | if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint; |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 2934 | |
| 2935 | uint32_t flags = paint->getFlags(); |
| 2936 | |
| 2937 | mFilteredPaint = *paint; |
| 2938 | mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits); |
| 2939 | |
| 2940 | return &mFilteredPaint; |
| 2941 | } |
| 2942 | |
| 2943 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2944 | // Drawing implementation |
| 2945 | /////////////////////////////////////////////////////////////////////////////// |
| 2946 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2947 | void OpenGLRenderer::drawPathTexture(const PathTexture* texture, |
| 2948 | float x, float y, SkPaint* paint) { |
| 2949 | if (quickReject(x, y, x + texture->width, y + texture->height)) { |
| 2950 | return; |
| 2951 | } |
| 2952 | |
| 2953 | int alpha; |
| 2954 | SkXfermode::Mode mode; |
| 2955 | getAlphaAndMode(paint, &alpha, &mode); |
| 2956 | |
| 2957 | setupDraw(); |
| 2958 | setupDrawWithTexture(true); |
| 2959 | setupDrawAlpha8Color(paint->getColor(), alpha); |
| 2960 | setupDrawColorFilter(); |
| 2961 | setupDrawShader(); |
| 2962 | setupDrawBlending(true, mode); |
| 2963 | setupDrawProgram(); |
| 2964 | setupDrawModelView(x, y, x + texture->width, y + texture->height); |
| 2965 | setupDrawTexture(texture->id); |
| 2966 | setupDrawPureColorUniforms(); |
| 2967 | setupDrawColorFilterUniforms(); |
| 2968 | setupDrawShaderUniforms(); |
| 2969 | setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); |
| 2970 | |
| 2971 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 2972 | |
| 2973 | finishDrawTexture(); |
| 2974 | } |
| 2975 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 2976 | // Same values used by Skia |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2977 | #define kStdStrikeThru_Offset (-6.0f / 21.0f) |
| 2978 | #define kStdUnderline_Offset (1.0f / 9.0f) |
| 2979 | #define kStdUnderline_Thickness (1.0f / 18.0f) |
| 2980 | |
| 2981 | void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, |
| 2982 | float x, float y, SkPaint* paint) { |
| 2983 | // Handle underline and strike-through |
| 2984 | uint32_t flags = paint->getFlags(); |
| 2985 | if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 2986 | SkPaint paintCopy(*paint); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2987 | float underlineWidth = length; |
| 2988 | // If length is > 0.0f, we already measured the text for the text alignment |
| 2989 | if (length <= 0.0f) { |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 2990 | underlineWidth = paintCopy.measureText(text, bytesCount); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2991 | } |
| 2992 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2993 | if (CC_LIKELY(underlineWidth > 0.0f)) { |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 2994 | const float textSize = paintCopy.getTextSize(); |
Romain Guy | f683447 | 2011-01-23 13:32:12 -0800 | [diff] [blame] | 2995 | const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2996 | |
Raph Levien | 8b4072d | 2012-07-30 15:50:00 -0700 | [diff] [blame] | 2997 | const float left = x; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2998 | float top = 0.0f; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 2999 | |
Romain Guy | f683447 | 2011-01-23 13:32:12 -0800 | [diff] [blame] | 3000 | int linesCount = 0; |
| 3001 | if (flags & SkPaint::kUnderlineText_Flag) linesCount++; |
| 3002 | if (flags & SkPaint::kStrikeThruText_Flag) linesCount++; |
| 3003 | |
| 3004 | const int pointsCount = 4 * linesCount; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3005 | float points[pointsCount]; |
| 3006 | int currentPoint = 0; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3007 | |
| 3008 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 3009 | top = y + textSize * kStdUnderline_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3010 | points[currentPoint++] = left; |
| 3011 | points[currentPoint++] = top; |
| 3012 | points[currentPoint++] = left + underlineWidth; |
| 3013 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 3017 | top = y + textSize * kStdStrikeThru_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3018 | points[currentPoint++] = left; |
| 3019 | points[currentPoint++] = top; |
| 3020 | points[currentPoint++] = left + underlineWidth; |
| 3021 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3022 | } |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3023 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 3024 | paintCopy.setStrokeWidth(strokeWidth); |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3025 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 3026 | drawLines(&points[0], pointsCount, &paintCopy); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3027 | } |
| 3028 | } |
| 3029 | } |
| 3030 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 3031 | void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 3032 | int color, SkXfermode::Mode mode, bool ignoreTransform) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3033 | // If a shader is set, preserve only the alpha |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 3034 | if (mShader) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3035 | color |= 0x00ffffff; |
| 3036 | } |
| 3037 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3038 | setupDraw(); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 3039 | setupDrawNoTexture(); |
Romain Guy | 9c0b188 | 2012-07-13 12:13:07 -0700 | [diff] [blame] | 3040 | setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3041 | setupDrawShader(); |
| 3042 | setupDrawColorFilter(); |
| 3043 | setupDrawBlending(mode); |
| 3044 | setupDrawProgram(); |
| 3045 | setupDrawModelView(left, top, right, bottom, ignoreTransform); |
| 3046 | setupDrawColorUniforms(); |
| 3047 | setupDrawShaderUniforms(ignoreTransform); |
| 3048 | setupDrawColorFilterUniforms(); |
| 3049 | setupDrawSimpleMesh(); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 3050 | |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 3051 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 3052 | } |
| 3053 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3054 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 3055 | Texture* texture, SkPaint* paint) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3056 | int alpha; |
| 3057 | SkXfermode::Mode mode; |
| 3058 | getAlphaAndMode(paint, &alpha, &mode); |
| 3059 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 3060 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 3061 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 3062 | if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 3063 | const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 3064 | const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 3065 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 3066 | texture->setFilter(GL_NEAREST, true); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 3067 | drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id, |
| 3068 | alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL, |
| 3069 | (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true); |
| 3070 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 3071 | texture->setFilter(FILTER(paint), true); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 3072 | drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, |
| 3073 | texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, |
| 3074 | GL_TRIANGLE_STRIP, gMeshCount); |
| 3075 | } |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 3076 | } |
| 3077 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 3078 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 3079 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { |
| 3080 | drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 3081 | (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 3085 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 3086 | GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 3087 | bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3088 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 3089 | setupDraw(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3090 | setupDrawWithTexture(); |
| 3091 | setupDrawColor(alpha, alpha, alpha, alpha); |
| 3092 | setupDrawColorFilter(); |
| 3093 | setupDrawBlending(blend, mode, swapSrcDst); |
| 3094 | setupDrawProgram(); |
| 3095 | if (!dirty) { |
| 3096 | setupDrawDirtyRegionsDisabled(); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 3097 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 3098 | if (!ignoreScale) { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3099 | setupDrawModelView(left, top, right, bottom, ignoreTransform); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 3100 | } else { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3101 | setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 3102 | } |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 3103 | setupDrawPureColorUniforms(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3104 | setupDrawColorFilterUniforms(); |
| 3105 | setupDrawTexture(texture); |
| 3106 | setupDrawMesh(vertices, texCoords, vbo); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 3107 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 3108 | glDrawArrays(drawMode, 0, elementsCount); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3109 | |
| 3110 | finishDrawTexture(); |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3111 | } |
| 3112 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 3113 | void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 3114 | ProgramDescription& description, bool swapSrcDst) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3115 | blend = blend || mode != SkXfermode::kSrcOver_Mode; |
Romain Guy | c189ef5 | 2012-04-25 20:02:53 -0700 | [diff] [blame] | 3116 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3117 | if (blend) { |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 3118 | // These blend modes are not supported by OpenGL directly and have |
| 3119 | // to be implemented using shaders. Since the shader will perform |
| 3120 | // the blending, turn blending off here |
| 3121 | // If the blend mode cannot be implemented using shaders, fall |
| 3122 | // back to the default SrcOver blend mode instead |
Romain Guy | 33fa1f7 | 2012-08-07 19:09:57 -0700 | [diff] [blame] | 3123 | if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) { |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 3124 | if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 3125 | description.framebufferMode = mode; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 3126 | description.swapSrcDst = swapSrcDst; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 3127 | |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 3128 | if (mCaches.blend) { |
| 3129 | glDisable(GL_BLEND); |
| 3130 | mCaches.blend = false; |
| 3131 | } |
| 3132 | |
| 3133 | return; |
| 3134 | } else { |
| 3135 | mode = SkXfermode::kSrcOver_Mode; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 3136 | } |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | if (!mCaches.blend) { |
| 3140 | glEnable(GL_BLEND); |
| 3141 | } |
| 3142 | |
| 3143 | GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; |
| 3144 | GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; |
| 3145 | |
| 3146 | if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { |
| 3147 | glBlendFunc(sourceMode, destMode); |
| 3148 | mCaches.lastSrcMode = sourceMode; |
| 3149 | mCaches.lastDstMode = destMode; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3150 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3151 | } else if (mCaches.blend) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3152 | glDisable(GL_BLEND); |
| 3153 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3154 | mCaches.blend = blend; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 3155 | } |
| 3156 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 3157 | bool OpenGLRenderer::useProgram(Program* program) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3158 | if (!program->isInUse()) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3159 | if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3160 | program->use(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3161 | mCaches.currentProgram = program; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 3162 | return false; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 3163 | } |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 3164 | return true; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 3165 | } |
| 3166 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 3167 | void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 3168 | TextureVertex* v = &mMeshVertices[0]; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3169 | TextureVertex::setUV(v++, u1, v1); |
| 3170 | TextureVertex::setUV(v++, u2, v1); |
| 3171 | TextureVertex::setUV(v++, u1, v2); |
| 3172 | TextureVertex::setUV(v++, u2, v2); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 3173 | } |
| 3174 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 3175 | void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { |
Romain Guy | be6f9dc | 2012-07-16 12:41:17 -0700 | [diff] [blame] | 3176 | getAlphaAndModeDirect(paint, alpha, mode); |
Chet Haase | db8c9a6 | 2012-03-21 18:54:18 -0700 | [diff] [blame] | 3177 | *alpha *= mSnapshot->alpha; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 3178 | } |
| 3179 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 3180 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 3181 | }; // namespace android |