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